LCOV - code coverage report
Current view: top level - drivers/gpu/drm/amd/display/dc/dsc - dc_dsc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 440 0.0 %
Date: 2022-12-09 01:23:36 Functions: 0 22 0.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright 2019 Advanced Micro Devices, Inc.
       3             :  *
       4             :  * Permission is hereby granted, free of charge, to any person obtaining a
       5             :  * copy of this software and associated documentation files (the "Software"),
       6             :  * to deal in the Software without restriction, including without limitation
       7             :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
       8             :  * and/or sell copies of the Software, and to permit persons to whom the
       9             :  * Software is furnished to do so, subject to the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included in
      12             :  * all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      15             :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      16             :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
      17             :  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
      18             :  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
      19             :  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
      20             :  * OTHER DEALINGS IN THE SOFTWARE.
      21             :  *
      22             :  * Author: AMD
      23             :  */
      24             : 
      25             : #include <drm/display/drm_dp_helper.h>
      26             : #include <drm/display/drm_dsc_helper.h>
      27             : #include "dc_hw_types.h"
      28             : #include "dsc.h"
      29             : #include "dc.h"
      30             : #include "rc_calc.h"
      31             : #include "fixed31_32.h"
      32             : 
      33             : /* This module's internal functions */
      34             : 
      35             : /* default DSC policy target bitrate limit is 16bpp */
      36             : static uint32_t dsc_policy_max_target_bpp_limit = 16;
      37             : 
      38             : /* default DSC policy enables DSC only when needed */
      39             : static bool dsc_policy_enable_dsc_when_not_needed;
      40             : 
      41             : static bool dsc_policy_disable_dsc_stream_overhead;
      42             : 
      43             : #ifndef MAX
      44             : #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
      45             : #endif
      46             : #ifndef MIN
      47             : #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
      48             : #endif
      49             : 
      50             : /* Forward Declerations */
      51             : static bool decide_dsc_bandwidth_range(
      52             :                 const uint32_t min_bpp_x16,
      53             :                 const uint32_t max_bpp_x16,
      54             :                 const uint32_t num_slices_h,
      55             :                 const struct dsc_enc_caps *dsc_caps,
      56             :                 const struct dc_crtc_timing *timing,
      57             :                 struct dc_dsc_bw_range *range);
      58             : 
      59             : static uint32_t compute_bpp_x16_from_target_bandwidth(
      60             :                 const uint32_t bandwidth_in_kbps,
      61             :                 const struct dc_crtc_timing *timing,
      62             :                 const uint32_t num_slices_h,
      63             :                 const uint32_t bpp_increment_div,
      64             :                 const bool is_dp);
      65             : 
      66             : static void get_dsc_enc_caps(
      67             :                 const struct display_stream_compressor *dsc,
      68             :                 struct dsc_enc_caps *dsc_enc_caps,
      69             :                 int pixel_clock_100Hz);
      70             : 
      71             : static bool intersect_dsc_caps(
      72             :                 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
      73             :                 const struct dsc_enc_caps *dsc_enc_caps,
      74             :                 enum dc_pixel_encoding pixel_encoding,
      75             :                 struct dsc_enc_caps *dsc_common_caps);
      76             : 
      77             : static bool setup_dsc_config(
      78             :                 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
      79             :                 const struct dsc_enc_caps *dsc_enc_caps,
      80             :                 int target_bandwidth_kbps,
      81             :                 const struct dc_crtc_timing *timing,
      82             :                 int min_slice_height_override,
      83             :                 int max_dsc_target_bpp_limit_override_x16,
      84             :                 struct dc_dsc_config *dsc_cfg);
      85             : 
      86           0 : static bool dsc_buff_block_size_from_dpcd(int dpcd_buff_block_size, int *buff_block_size)
      87             : {
      88             : 
      89           0 :         switch (dpcd_buff_block_size) {
      90             :         case DP_DSC_RC_BUF_BLK_SIZE_1:
      91           0 :                 *buff_block_size = 1024;
      92           0 :                 break;
      93             :         case DP_DSC_RC_BUF_BLK_SIZE_4:
      94           0 :                 *buff_block_size = 4 * 1024;
      95           0 :                 break;
      96             :         case DP_DSC_RC_BUF_BLK_SIZE_16:
      97           0 :                 *buff_block_size = 16 * 1024;
      98           0 :                 break;
      99             :         case DP_DSC_RC_BUF_BLK_SIZE_64:
     100           0 :                 *buff_block_size = 64 * 1024;
     101           0 :                 break;
     102             :         default: {
     103           0 :                         dm_error("%s: DPCD DSC buffer size not recognized.\n", __func__);
     104           0 :                         return false;
     105             :                 }
     106             :         }
     107             : 
     108             :         return true;
     109             : }
     110             : 
     111             : 
     112             : static bool dsc_line_buff_depth_from_dpcd(int dpcd_line_buff_bit_depth, int *line_buff_bit_depth)
     113             : {
     114           0 :         if (0 <= dpcd_line_buff_bit_depth && dpcd_line_buff_bit_depth <= 7)
     115           0 :                 *line_buff_bit_depth = dpcd_line_buff_bit_depth + 9;
     116           0 :         else if (dpcd_line_buff_bit_depth == 8)
     117           0 :                 *line_buff_bit_depth = 8;
     118             :         else {
     119           0 :                 dm_error("%s: DPCD DSC buffer depth not recognized.\n", __func__);
     120             :                 return false;
     121             :         }
     122             : 
     123             :         return true;
     124             : }
     125             : 
     126             : 
     127           0 : static bool dsc_throughput_from_dpcd(int dpcd_throughput, int *throughput)
     128             : {
     129           0 :         switch (dpcd_throughput) {
     130             :         case DP_DSC_THROUGHPUT_MODE_0_UNSUPPORTED:
     131           0 :                 *throughput = 0;
     132           0 :                 break;
     133             :         case DP_DSC_THROUGHPUT_MODE_0_170:
     134           0 :                 *throughput = 170;
     135           0 :                 break;
     136             :         case DP_DSC_THROUGHPUT_MODE_0_340:
     137           0 :                 *throughput = 340;
     138           0 :                 break;
     139             :         case DP_DSC_THROUGHPUT_MODE_0_400:
     140           0 :                 *throughput = 400;
     141           0 :                 break;
     142             :         case DP_DSC_THROUGHPUT_MODE_0_450:
     143           0 :                 *throughput = 450;
     144           0 :                 break;
     145             :         case DP_DSC_THROUGHPUT_MODE_0_500:
     146           0 :                 *throughput = 500;
     147           0 :                 break;
     148             :         case DP_DSC_THROUGHPUT_MODE_0_550:
     149           0 :                 *throughput = 550;
     150           0 :                 break;
     151             :         case DP_DSC_THROUGHPUT_MODE_0_600:
     152           0 :                 *throughput = 600;
     153           0 :                 break;
     154             :         case DP_DSC_THROUGHPUT_MODE_0_650:
     155           0 :                 *throughput = 650;
     156           0 :                 break;
     157             :         case DP_DSC_THROUGHPUT_MODE_0_700:
     158           0 :                 *throughput = 700;
     159           0 :                 break;
     160             :         case DP_DSC_THROUGHPUT_MODE_0_750:
     161           0 :                 *throughput = 750;
     162           0 :                 break;
     163             :         case DP_DSC_THROUGHPUT_MODE_0_800:
     164           0 :                 *throughput = 800;
     165           0 :                 break;
     166             :         case DP_DSC_THROUGHPUT_MODE_0_850:
     167           0 :                 *throughput = 850;
     168           0 :                 break;
     169             :         case DP_DSC_THROUGHPUT_MODE_0_900:
     170           0 :                 *throughput = 900;
     171           0 :                 break;
     172             :         case DP_DSC_THROUGHPUT_MODE_0_950:
     173           0 :                 *throughput = 950;
     174           0 :                 break;
     175             :         case DP_DSC_THROUGHPUT_MODE_0_1000:
     176           0 :                 *throughput = 1000;
     177           0 :                 break;
     178             :         default: {
     179           0 :                         dm_error("%s: DPCD DSC throughput mode not recognized.\n", __func__);
     180           0 :                         return false;
     181             :                 }
     182             :         }
     183             : 
     184             :         return true;
     185             : }
     186             : 
     187             : 
     188           0 : static bool dsc_bpp_increment_div_from_dpcd(uint8_t bpp_increment_dpcd, uint32_t *bpp_increment_div)
     189             : {
     190             :         // Mask bpp increment dpcd field to avoid reading other fields
     191           0 :         bpp_increment_dpcd &= 0x7;
     192             : 
     193           0 :         switch (bpp_increment_dpcd) {
     194             :         case 0:
     195           0 :                 *bpp_increment_div = 16;
     196           0 :                 break;
     197             :         case 1:
     198           0 :                 *bpp_increment_div = 8;
     199           0 :                 break;
     200             :         case 2:
     201           0 :                 *bpp_increment_div = 4;
     202           0 :                 break;
     203             :         case 3:
     204           0 :                 *bpp_increment_div = 2;
     205           0 :                 break;
     206             :         case 4:
     207           0 :                 *bpp_increment_div = 1;
     208           0 :                 break;
     209             :         default: {
     210           0 :                 dm_error("%s: DPCD DSC bits-per-pixel increment not recognized.\n", __func__);
     211           0 :                 return false;
     212             :         }
     213             :         }
     214             : 
     215             :         return true;
     216             : }
     217             : 
     218             : 
     219             : 
     220           0 : bool dc_dsc_parse_dsc_dpcd(const struct dc *dc,
     221             :                 const uint8_t *dpcd_dsc_basic_data,
     222             :                 const uint8_t *dpcd_dsc_branch_decoder_caps,
     223             :                 struct dsc_dec_dpcd_caps *dsc_sink_caps)
     224             : {
     225           0 :         if (!dpcd_dsc_basic_data)
     226             :                 return false;
     227             : 
     228           0 :         dsc_sink_caps->is_dsc_supported =
     229           0 :                 (dpcd_dsc_basic_data[DP_DSC_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_DECOMPRESSION_IS_SUPPORTED) != 0;
     230           0 :         if (!dsc_sink_caps->is_dsc_supported)
     231             :                 return false;
     232             : 
     233           0 :         dsc_sink_caps->dsc_version = dpcd_dsc_basic_data[DP_DSC_REV - DP_DSC_SUPPORT];
     234             : 
     235             :         {
     236             :                 int buff_block_size;
     237             :                 int buff_size;
     238             : 
     239           0 :                 if (!dsc_buff_block_size_from_dpcd(dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT],
     240             :                                                                                    &buff_block_size))
     241           0 :                         return false;
     242             : 
     243           0 :                 buff_size = dpcd_dsc_basic_data[DP_DSC_RC_BUF_SIZE - DP_DSC_SUPPORT] + 1;
     244           0 :                 dsc_sink_caps->rc_buffer_size = buff_size * buff_block_size;
     245             :         }
     246             : 
     247           0 :         dsc_sink_caps->slice_caps1.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
     248           0 :         if (!dsc_line_buff_depth_from_dpcd(dpcd_dsc_basic_data[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT],
     249           0 :                                                                            &dsc_sink_caps->lb_bit_depth))
     250             :                 return false;
     251             : 
     252           0 :         dsc_sink_caps->is_block_pred_supported =
     253           0 :                 (dpcd_dsc_basic_data[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] &
     254           0 :                  DP_DSC_BLK_PREDICTION_IS_SUPPORTED) != 0;
     255             : 
     256           0 :         dsc_sink_caps->edp_max_bits_per_pixel =
     257           0 :                 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_LOW - DP_DSC_SUPPORT] |
     258           0 :                 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] << 8;
     259             : 
     260           0 :         dsc_sink_caps->color_formats.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT];
     261           0 :         dsc_sink_caps->color_depth.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
     262             : 
     263             :         {
     264           0 :                 int dpcd_throughput = dpcd_dsc_basic_data[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];
     265             : 
     266           0 :                 if (!dsc_throughput_from_dpcd(dpcd_throughput & DP_DSC_THROUGHPUT_MODE_0_MASK,
     267           0 :                                                                           &dsc_sink_caps->throughput_mode_0_mps))
     268             :                         return false;
     269             : 
     270           0 :                 dpcd_throughput = (dpcd_throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >> DP_DSC_THROUGHPUT_MODE_1_SHIFT;
     271           0 :                 if (!dsc_throughput_from_dpcd(dpcd_throughput, &dsc_sink_caps->throughput_mode_1_mps))
     272             :                         return false;
     273             :         }
     274             : 
     275           0 :         dsc_sink_caps->max_slice_width = dpcd_dsc_basic_data[DP_DSC_MAX_SLICE_WIDTH - DP_DSC_SUPPORT] * 320;
     276           0 :         dsc_sink_caps->slice_caps2.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
     277             : 
     278           0 :         if (!dsc_bpp_increment_div_from_dpcd(dpcd_dsc_basic_data[DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT],
     279             :                                                                                  &dsc_sink_caps->bpp_increment_div))
     280             :                 return false;
     281             : 
     282           0 :         if (dc->debug.dsc_bpp_increment_div) {
     283             :                 /* dsc_bpp_increment_div should onl be 1, 2, 4, 8 or 16, but rather than rejecting invalid values,
     284             :                  * we'll accept all and get it into range. This also makes the above check against 0 redundant,
     285             :                  * but that one stresses out the override will be only used if it's not 0.
     286             :                  */
     287           0 :                 if (dc->debug.dsc_bpp_increment_div >= 1)
     288           0 :                         dsc_sink_caps->bpp_increment_div = 1;
     289           0 :                 if (dc->debug.dsc_bpp_increment_div >= 2)
     290           0 :                         dsc_sink_caps->bpp_increment_div = 2;
     291           0 :                 if (dc->debug.dsc_bpp_increment_div >= 4)
     292           0 :                         dsc_sink_caps->bpp_increment_div = 4;
     293           0 :                 if (dc->debug.dsc_bpp_increment_div >= 8)
     294           0 :                         dsc_sink_caps->bpp_increment_div = 8;
     295           0 :                 if (dc->debug.dsc_bpp_increment_div >= 16)
     296           0 :                         dsc_sink_caps->bpp_increment_div = 16;
     297             :         }
     298             : 
     299             :         /* Extended caps */
     300           0 :         if (dpcd_dsc_branch_decoder_caps == NULL) { // branch decoder DPCD DSC data can be null for non branch device
     301           0 :                 dsc_sink_caps->branch_overall_throughput_0_mps = 0;
     302           0 :                 dsc_sink_caps->branch_overall_throughput_1_mps = 0;
     303           0 :                 dsc_sink_caps->branch_max_line_width = 0;
     304           0 :                 return true;
     305             :         }
     306             : 
     307           0 :         dsc_sink_caps->branch_overall_throughput_0_mps =
     308           0 :                 dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_0 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];
     309           0 :         if (dsc_sink_caps->branch_overall_throughput_0_mps == 0)
     310           0 :                 dsc_sink_caps->branch_overall_throughput_0_mps = 0;
     311           0 :         else if (dsc_sink_caps->branch_overall_throughput_0_mps == 1)
     312           0 :                 dsc_sink_caps->branch_overall_throughput_0_mps = 680;
     313             :         else {
     314           0 :                 dsc_sink_caps->branch_overall_throughput_0_mps *= 50;
     315           0 :                 dsc_sink_caps->branch_overall_throughput_0_mps += 600;
     316             :         }
     317             : 
     318           0 :         dsc_sink_caps->branch_overall_throughput_1_mps =
     319           0 :                 dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_1 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];
     320           0 :         if (dsc_sink_caps->branch_overall_throughput_1_mps == 0)
     321           0 :                 dsc_sink_caps->branch_overall_throughput_1_mps = 0;
     322           0 :         else if (dsc_sink_caps->branch_overall_throughput_1_mps == 1)
     323           0 :                 dsc_sink_caps->branch_overall_throughput_1_mps = 680;
     324             :         else {
     325           0 :                 dsc_sink_caps->branch_overall_throughput_1_mps *= 50;
     326           0 :                 dsc_sink_caps->branch_overall_throughput_1_mps += 600;
     327             :         }
     328             : 
     329           0 :         dsc_sink_caps->branch_max_line_width =
     330           0 :                 dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_MAX_LINE_WIDTH - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0] * 320;
     331           0 :         ASSERT(dsc_sink_caps->branch_max_line_width == 0 || dsc_sink_caps->branch_max_line_width >= 5120);
     332             : 
     333           0 :         dsc_sink_caps->is_dp = true;
     334           0 :         return true;
     335             : }
     336             : 
     337             : 
     338             : /* If DSC is possbile, get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range and
     339             :  * timing's pixel clock and uncompressed bandwidth.
     340             :  * If DSC is not possible, leave '*range' untouched.
     341             :  */
     342           0 : bool dc_dsc_compute_bandwidth_range(
     343             :                 const struct display_stream_compressor *dsc,
     344             :                 uint32_t dsc_min_slice_height_override,
     345             :                 uint32_t min_bpp_x16,
     346             :                 uint32_t max_bpp_x16,
     347             :                 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
     348             :                 const struct dc_crtc_timing *timing,
     349             :                 struct dc_dsc_bw_range *range)
     350             : {
     351           0 :         bool is_dsc_possible = false;
     352             :         struct dsc_enc_caps dsc_enc_caps;
     353             :         struct dsc_enc_caps dsc_common_caps;
     354             :         struct dc_dsc_config config;
     355             : 
     356           0 :         get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
     357             : 
     358           0 :         is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, &dsc_enc_caps,
     359             :                         timing->pixel_encoding, &dsc_common_caps);
     360             : 
     361           0 :         if (is_dsc_possible)
     362           0 :                 is_dsc_possible = setup_dsc_config(dsc_sink_caps, &dsc_enc_caps, 0, timing,
     363             :                                 dsc_min_slice_height_override, max_bpp_x16, &config);
     364             : 
     365           0 :         if (is_dsc_possible)
     366           0 :                 is_dsc_possible = decide_dsc_bandwidth_range(min_bpp_x16, max_bpp_x16,
     367             :                                 config.num_slices_h, &dsc_common_caps, timing, range);
     368             : 
     369           0 :         return is_dsc_possible;
     370             : }
     371             : 
     372           0 : static void get_dsc_enc_caps(
     373             :                 const struct display_stream_compressor *dsc,
     374             :                 struct dsc_enc_caps *dsc_enc_caps,
     375             :                 int pixel_clock_100Hz)
     376             : {
     377             :         // This is a static HW query, so we can use any DSC
     378             : 
     379           0 :         memset(dsc_enc_caps, 0, sizeof(struct dsc_enc_caps));
     380           0 :         if (dsc) {
     381           0 :                 if (!dsc->ctx->dc->debug.disable_dsc)
     382           0 :                         dsc->funcs->dsc_get_enc_caps(dsc_enc_caps, pixel_clock_100Hz);
     383           0 :                 if (dsc->ctx->dc->debug.native422_support)
     384           0 :                         dsc_enc_caps->color_formats.bits.YCBCR_NATIVE_422 = 1;
     385             :         }
     386           0 : }
     387             : 
     388             : /* Returns 'false' if no intersection was found for at least one capability.
     389             :  * It also implicitly validates some sink caps against invalid value of zero.
     390             :  */
     391           0 : static bool intersect_dsc_caps(
     392             :                 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
     393             :                 const struct dsc_enc_caps *dsc_enc_caps,
     394             :                 enum dc_pixel_encoding pixel_encoding,
     395             :                 struct dsc_enc_caps *dsc_common_caps)
     396             : {
     397             :         int32_t max_slices;
     398             :         int32_t total_sink_throughput;
     399             : 
     400           0 :         memset(dsc_common_caps, 0, sizeof(struct dsc_enc_caps));
     401             : 
     402           0 :         dsc_common_caps->dsc_version = min(dsc_sink_caps->dsc_version, dsc_enc_caps->dsc_version);
     403           0 :         if (!dsc_common_caps->dsc_version)
     404             :                 return false;
     405             : 
     406           0 :         dsc_common_caps->slice_caps.bits.NUM_SLICES_1 =
     407           0 :                 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_1 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_1;
     408           0 :         dsc_common_caps->slice_caps.bits.NUM_SLICES_2 =
     409           0 :                 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_2 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_2;
     410           0 :         dsc_common_caps->slice_caps.bits.NUM_SLICES_4 =
     411           0 :                 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_4 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_4;
     412           0 :         dsc_common_caps->slice_caps.bits.NUM_SLICES_8 =
     413           0 :                 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_8 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_8;
     414           0 :         if (!dsc_common_caps->slice_caps.raw)
     415             :                 return false;
     416             : 
     417           0 :         dsc_common_caps->lb_bit_depth = min(dsc_sink_caps->lb_bit_depth, dsc_enc_caps->lb_bit_depth);
     418           0 :         if (!dsc_common_caps->lb_bit_depth)
     419             :                 return false;
     420             : 
     421           0 :         dsc_common_caps->is_block_pred_supported =
     422           0 :                 dsc_sink_caps->is_block_pred_supported && dsc_enc_caps->is_block_pred_supported;
     423             : 
     424           0 :         dsc_common_caps->color_formats.raw = dsc_sink_caps->color_formats.raw & dsc_enc_caps->color_formats.raw;
     425           0 :         if (!dsc_common_caps->color_formats.raw)
     426             :                 return false;
     427             : 
     428           0 :         dsc_common_caps->color_depth.raw = dsc_sink_caps->color_depth.raw & dsc_enc_caps->color_depth.raw;
     429           0 :         if (!dsc_common_caps->color_depth.raw)
     430             :                 return false;
     431             : 
     432           0 :         max_slices = 0;
     433           0 :         if (dsc_common_caps->slice_caps.bits.NUM_SLICES_1)
     434           0 :                 max_slices = 1;
     435             : 
     436           0 :         if (dsc_common_caps->slice_caps.bits.NUM_SLICES_2)
     437           0 :                 max_slices = 2;
     438             : 
     439           0 :         if (dsc_common_caps->slice_caps.bits.NUM_SLICES_4)
     440           0 :                 max_slices = 4;
     441             : 
     442           0 :         total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_0_mps;
     443           0 :         if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
     444           0 :                 total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_1_mps;
     445             : 
     446           0 :         dsc_common_caps->max_total_throughput_mps = min(total_sink_throughput, dsc_enc_caps->max_total_throughput_mps);
     447             : 
     448           0 :         dsc_common_caps->max_slice_width = min(dsc_sink_caps->max_slice_width, dsc_enc_caps->max_slice_width);
     449           0 :         if (!dsc_common_caps->max_slice_width)
     450             :                 return false;
     451             : 
     452           0 :         dsc_common_caps->bpp_increment_div = min(dsc_sink_caps->bpp_increment_div, dsc_enc_caps->bpp_increment_div);
     453             : 
     454             :         // TODO DSC: Remove this workaround for N422 and 420 once it's fixed, or move it to get_dsc_encoder_caps()
     455           0 :         if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
     456           0 :                 dsc_common_caps->bpp_increment_div = min(dsc_common_caps->bpp_increment_div, (uint32_t)8);
     457             : 
     458           0 :         dsc_common_caps->edp_sink_max_bits_per_pixel = dsc_sink_caps->edp_max_bits_per_pixel;
     459           0 :         dsc_common_caps->is_dp = dsc_sink_caps->is_dp;
     460           0 :         return true;
     461             : }
     462             : 
     463             : static inline uint32_t dsc_div_by_10_round_up(uint32_t value)
     464             : {
     465           0 :         return (value + 9) / 10;
     466             : }
     467             : 
     468           0 : static uint32_t compute_bpp_x16_from_target_bandwidth(
     469             :         const uint32_t bandwidth_in_kbps,
     470             :         const struct dc_crtc_timing *timing,
     471             :         const uint32_t num_slices_h,
     472             :         const uint32_t bpp_increment_div,
     473             :         const bool is_dp)
     474             : {
     475             :         uint32_t overhead_in_kbps;
     476             :         struct fixed31_32 effective_bandwidth_in_kbps;
     477             :         struct fixed31_32 bpp_x16;
     478             : 
     479           0 :         overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
     480             :                                 timing, num_slices_h, is_dp);
     481           0 :         effective_bandwidth_in_kbps = dc_fixpt_from_int(bandwidth_in_kbps);
     482           0 :         effective_bandwidth_in_kbps = dc_fixpt_sub_int(effective_bandwidth_in_kbps,
     483             :                         overhead_in_kbps);
     484           0 :         bpp_x16 = dc_fixpt_mul_int(effective_bandwidth_in_kbps, 10);
     485           0 :         bpp_x16 = dc_fixpt_div_int(bpp_x16, timing->pix_clk_100hz);
     486           0 :         bpp_x16 = dc_fixpt_from_int(dc_fixpt_floor(dc_fixpt_mul_int(bpp_x16, bpp_increment_div)));
     487           0 :         bpp_x16 = dc_fixpt_div_int(bpp_x16, bpp_increment_div);
     488           0 :         bpp_x16 = dc_fixpt_mul_int(bpp_x16, 16);
     489           0 :         return dc_fixpt_floor(bpp_x16);
     490             : }
     491             : 
     492             : /* Decide DSC bandwidth range based on signal, timing, specs specific and input min and max
     493             :  * requirements.
     494             :  * The range output includes decided min/max target bpp, the respective bandwidth requirements
     495             :  * and native timing bandwidth requirement when DSC is not used.
     496             :  */
     497           0 : static bool decide_dsc_bandwidth_range(
     498             :                 const uint32_t min_bpp_x16,
     499             :                 const uint32_t max_bpp_x16,
     500             :                 const uint32_t num_slices_h,
     501             :                 const struct dsc_enc_caps *dsc_caps,
     502             :                 const struct dc_crtc_timing *timing,
     503             :                 struct dc_dsc_bw_range *range)
     504             : {
     505           0 :         uint32_t preferred_bpp_x16 = timing->dsc_fixed_bits_per_pixel_x16;
     506             : 
     507           0 :         memset(range, 0, sizeof(*range));
     508             : 
     509             :         /* apply signal, timing, specs and explicitly specified DSC range requirements */
     510           0 :         if (preferred_bpp_x16) {
     511           0 :                 if (preferred_bpp_x16 <= max_bpp_x16 &&
     512           0 :                                 preferred_bpp_x16 >= min_bpp_x16) {
     513           0 :                         range->max_target_bpp_x16 = preferred_bpp_x16;
     514           0 :                         range->min_target_bpp_x16 = preferred_bpp_x16;
     515             :                 }
     516             :         }
     517             :         /* TODO - make this value generic to all signal types */
     518           0 :         else if (dsc_caps->edp_sink_max_bits_per_pixel) {
     519             :                 /* apply max bpp limitation from edp sink */
     520           0 :                 range->max_target_bpp_x16 = MIN(dsc_caps->edp_sink_max_bits_per_pixel,
     521             :                                 max_bpp_x16);
     522           0 :                 range->min_target_bpp_x16 = min_bpp_x16;
     523             :         }
     524             :         else {
     525           0 :                 range->max_target_bpp_x16 = max_bpp_x16;
     526           0 :                 range->min_target_bpp_x16 = min_bpp_x16;
     527             :         }
     528             : 
     529             :         /* populate output structure */
     530           0 :         if (range->max_target_bpp_x16 >= range->min_target_bpp_x16 && range->min_target_bpp_x16 > 0) {
     531             :                 /* native stream bandwidth */
     532           0 :                 range->stream_kbps = dc_bandwidth_in_kbps_from_timing(timing);
     533             : 
     534             :                 /* max dsc target bpp */
     535           0 :                 range->max_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
     536           0 :                                 range->max_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
     537             : 
     538             :                 /* min dsc target bpp */
     539           0 :                 range->min_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
     540           0 :                                 range->min_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
     541             :         }
     542             : 
     543           0 :         return range->max_kbps >= range->min_kbps && range->min_kbps > 0;
     544             : }
     545             : 
     546             : /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy.
     547             :  *
     548             :  * Returns:
     549             :  *     - 'true' if target bpp is decided
     550             :  *     - 'false' if target bpp cannot be decided (e.g. cannot fit even with min DSC bpp),
     551             :  */
     552           0 : static bool decide_dsc_target_bpp_x16(
     553             :                 const struct dc_dsc_policy *policy,
     554             :                 const struct dsc_enc_caps *dsc_common_caps,
     555             :                 const int target_bandwidth_kbps,
     556             :                 const struct dc_crtc_timing *timing,
     557             :                 const int num_slices_h,
     558             :                 int *target_bpp_x16)
     559             : {
     560             :         struct dc_dsc_bw_range range;
     561             : 
     562           0 :         *target_bpp_x16 = 0;
     563             : 
     564           0 :         if (decide_dsc_bandwidth_range(policy->min_target_bpp * 16, policy->max_target_bpp * 16,
     565             :                         num_slices_h, dsc_common_caps, timing, &range)) {
     566           0 :                 if (target_bandwidth_kbps >= range.stream_kbps) {
     567           0 :                         if (policy->enable_dsc_when_not_needed)
     568             :                                 /* enable max bpp even dsc is not needed */
     569           0 :                                 *target_bpp_x16 = range.max_target_bpp_x16;
     570           0 :                 } else if (target_bandwidth_kbps >= range.max_kbps) {
     571             :                         /* use max target bpp allowed */
     572           0 :                         *target_bpp_x16 = range.max_target_bpp_x16;
     573           0 :                 } else if (target_bandwidth_kbps >= range.min_kbps) {
     574             :                         /* use target bpp that can take entire target bandwidth */
     575           0 :                         *target_bpp_x16 = compute_bpp_x16_from_target_bandwidth(
     576             :                                         target_bandwidth_kbps, timing, num_slices_h,
     577             :                                         dsc_common_caps->bpp_increment_div,
     578           0 :                                         dsc_common_caps->is_dp);
     579             :                 }
     580             :         }
     581             : 
     582           0 :         return *target_bpp_x16 != 0;
     583             : }
     584             : 
     585             : #define MIN_AVAILABLE_SLICES_SIZE  6
     586             : 
     587           0 : static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps, int *available_slices)
     588             : {
     589           0 :         int idx = 0;
     590             : 
     591           0 :         memset(available_slices, -1, MIN_AVAILABLE_SLICES_SIZE);
     592             : 
     593           0 :         if (slice_caps.bits.NUM_SLICES_1)
     594           0 :                 available_slices[idx++] = 1;
     595             : 
     596           0 :         if (slice_caps.bits.NUM_SLICES_2)
     597           0 :                 available_slices[idx++] = 2;
     598             : 
     599           0 :         if (slice_caps.bits.NUM_SLICES_4)
     600           0 :                 available_slices[idx++] = 4;
     601             : 
     602           0 :         if (slice_caps.bits.NUM_SLICES_8)
     603           0 :                 available_slices[idx++] = 8;
     604             : 
     605           0 :         return idx;
     606             : }
     607             : 
     608             : 
     609             : static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps)
     610             : {
     611           0 :         int max_slices = 0;
     612             :         int available_slices[MIN_AVAILABLE_SLICES_SIZE];
     613           0 :         int end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
     614             : 
     615           0 :         if (end_idx > 0)
     616           0 :                 max_slices = available_slices[end_idx - 1];
     617             : 
     618             :         return max_slices;
     619             : }
     620             : 
     621             : 
     622             : // Increment slice number in available slice numbers stops if possible, or just increment if not
     623           0 : static int inc_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
     624             : {
     625             :         // Get next bigger num slices available in common caps
     626             :         int available_slices[MIN_AVAILABLE_SLICES_SIZE];
     627             :         int end_idx;
     628             :         int i;
     629           0 :         int new_num_slices = num_slices;
     630             : 
     631           0 :         end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
     632           0 :         if (end_idx == 0) {
     633             :                 // No available slices found
     634           0 :                 new_num_slices++;
     635           0 :                 return new_num_slices;
     636             :         }
     637             : 
     638             :         // Numbers of slices found - get the next bigger number
     639           0 :         for (i = 0; i < end_idx; i++) {
     640           0 :                 if (new_num_slices < available_slices[i]) {
     641             :                         new_num_slices = available_slices[i];
     642             :                         break;
     643             :                 }
     644             :         }
     645             : 
     646           0 :         if (new_num_slices == num_slices) // No biger number of slices found
     647           0 :                 new_num_slices++;
     648             : 
     649             :         return new_num_slices;
     650             : }
     651             : 
     652             : 
     653             : // Decrement slice number in available slice numbers stops if possible, or just decrement if not. Stop at zero.
     654           0 : static int dec_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
     655             : {
     656             :         // Get next bigger num slices available in common caps
     657             :         int available_slices[MIN_AVAILABLE_SLICES_SIZE];
     658             :         int end_idx;
     659             :         int i;
     660           0 :         int new_num_slices = num_slices;
     661             : 
     662           0 :         end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
     663           0 :         if (end_idx == 0 && new_num_slices > 0) {
     664             :                 // No numbers of slices found
     665           0 :                 new_num_slices++;
     666           0 :                 return new_num_slices;
     667             :         }
     668             : 
     669             :         // Numbers of slices found - get the next smaller number
     670           0 :         for (i = end_idx - 1; i >= 0; i--) {
     671           0 :                 if (new_num_slices > available_slices[i]) {
     672             :                         new_num_slices = available_slices[i];
     673             :                         break;
     674             :                 }
     675             :         }
     676             : 
     677           0 :         if (new_num_slices == num_slices) {
     678             :                 // No smaller number of slices found
     679           0 :                 new_num_slices--;
     680           0 :                 if (new_num_slices < 0)
     681           0 :                         new_num_slices = 0;
     682             :         }
     683             : 
     684             :         return new_num_slices;
     685             : }
     686             : 
     687             : 
     688             : // Choose next bigger number of slices if the requested number of slices is not available
     689           0 : static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps, int num_slices)
     690             : {
     691             :         // Get next bigger num slices available in common caps
     692             :         int available_slices[MIN_AVAILABLE_SLICES_SIZE];
     693             :         int end_idx;
     694             :         int i;
     695           0 :         int new_num_slices = num_slices;
     696             : 
     697           0 :         end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
     698           0 :         if (end_idx == 0) {
     699             :                 // No available slices found
     700           0 :                 new_num_slices++;
     701           0 :                 return new_num_slices;
     702             :         }
     703             : 
     704             :         // Numbers of slices found - get the equal or next bigger number
     705           0 :         for (i = 0; i < end_idx; i++) {
     706           0 :                 if (new_num_slices <= available_slices[i]) {
     707             :                         new_num_slices = available_slices[i];
     708             :                         break;
     709             :                 }
     710             :         }
     711             : 
     712             :         return new_num_slices;
     713             : }
     714             : 
     715             : 
     716             : /* Attempts to set DSC configuration for the stream, applying DSC policy.
     717             :  * Returns 'true' if successful or 'false' if not.
     718             :  *
     719             :  * Parameters:
     720             :  *
     721             :  * dsc_sink_caps       - DSC sink decoder capabilities (from DPCD)
     722             :  *
     723             :  * dsc_enc_caps        - DSC encoder capabilities
     724             :  *
     725             :  * target_bandwidth_kbps  - Target bandwidth to fit the stream into.
     726             :  *                          If 0, do not calculate target bpp.
     727             :  *
     728             :  * timing              - The stream timing to fit into 'target_bandwidth_kbps' or apply
     729             :  *                       maximum compression to, if 'target_badwidth == 0'
     730             :  *
     731             :  * dsc_cfg             - DSC configuration to use if it was possible to come up with
     732             :  *                       one for the given inputs.
     733             :  *                       The target bitrate after DSC can be calculated by multiplying
     734             :  *                       dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g.
     735             :  *
     736             :  *                       dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0);
     737             :  */
     738           0 : static bool setup_dsc_config(
     739             :                 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
     740             :                 const struct dsc_enc_caps *dsc_enc_caps,
     741             :                 int target_bandwidth_kbps,
     742             :                 const struct dc_crtc_timing *timing,
     743             :                 int min_slice_height_override,
     744             :                 int max_dsc_target_bpp_limit_override_x16,
     745             :                 struct dc_dsc_config *dsc_cfg)
     746             : {
     747             :         struct dsc_enc_caps dsc_common_caps;
     748             :         int max_slices_h;
     749             :         int min_slices_h;
     750             :         int num_slices_h;
     751             :         int pic_width;
     752             :         int slice_width;
     753             :         int target_bpp;
     754             :         int sink_per_slice_throughput_mps;
     755           0 :         int branch_max_throughput_mps = 0;
     756           0 :         bool is_dsc_possible = false;
     757             :         int pic_height;
     758             :         int slice_height;
     759             :         struct dc_dsc_policy policy;
     760             : 
     761           0 :         memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
     762             : 
     763           0 :         dc_dsc_get_policy_for_timing(timing, max_dsc_target_bpp_limit_override_x16, &policy);
     764           0 :         pic_width = timing->h_addressable + timing->h_border_left + timing->h_border_right;
     765           0 :         pic_height = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
     766             : 
     767           0 :         if (!dsc_sink_caps->is_dsc_supported)
     768             :                 goto done;
     769             : 
     770           0 :         if (dsc_sink_caps->branch_max_line_width && dsc_sink_caps->branch_max_line_width < pic_width)
     771             :                 goto done;
     772             : 
     773             :         // Intersect decoder with encoder DSC caps and validate DSC settings
     774           0 :         is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, dsc_enc_caps, timing->pixel_encoding, &dsc_common_caps);
     775           0 :         if (!is_dsc_possible)
     776             :                 goto done;
     777             : 
     778           0 :         sink_per_slice_throughput_mps = 0;
     779             : 
     780             :         // Validate available DSC settings against the mode timing
     781             : 
     782             :         // Validate color format (and pick up the throughput values)
     783           0 :         dsc_cfg->ycbcr422_simple = false;
     784           0 :         switch (timing->pixel_encoding)      {
     785             :         case PIXEL_ENCODING_RGB:
     786           0 :                 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.RGB;
     787           0 :                 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
     788           0 :                 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
     789           0 :                 break;
     790             :         case PIXEL_ENCODING_YCBCR444:
     791           0 :                 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_444;
     792           0 :                 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
     793           0 :                 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
     794           0 :                 break;
     795             :         case PIXEL_ENCODING_YCBCR422:
     796           0 :                 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_422;
     797           0 :                 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
     798           0 :                 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
     799           0 :                 if (!is_dsc_possible) {
     800           0 :                         is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_SIMPLE_422;
     801           0 :                         dsc_cfg->ycbcr422_simple = is_dsc_possible;
     802           0 :                         sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
     803             :                 }
     804             :                 break;
     805             :         case PIXEL_ENCODING_YCBCR420:
     806           0 :                 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_420;
     807           0 :                 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
     808           0 :                 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
     809           0 :                 break;
     810             :         default:
     811             :                 is_dsc_possible = false;
     812             :         }
     813             : 
     814             :         // Validate branch's maximum throughput
     815           0 :         if (branch_max_throughput_mps && dsc_div_by_10_round_up(timing->pix_clk_100hz) > branch_max_throughput_mps * 1000)
     816           0 :                 is_dsc_possible = false;
     817             : 
     818           0 :         if (!is_dsc_possible)
     819             :                 goto done;
     820             : 
     821             :         // Color depth
     822           0 :         switch (timing->display_color_depth) {
     823             :         case COLOR_DEPTH_888:
     824           0 :                 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_8_BPC;
     825           0 :                 break;
     826             :         case COLOR_DEPTH_101010:
     827           0 :                 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_10_BPC;
     828           0 :                 break;
     829             :         case COLOR_DEPTH_121212:
     830           0 :                 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_12_BPC;
     831           0 :                 break;
     832             :         default:
     833             :                 is_dsc_possible = false;
     834             :         }
     835             : 
     836           0 :         if (!is_dsc_possible)
     837             :                 goto done;
     838             : 
     839             :         // Slice width (i.e. number of slices per line)
     840           0 :         max_slices_h = get_max_dsc_slices(dsc_common_caps.slice_caps);
     841             : 
     842           0 :         while (max_slices_h > 0) {
     843           0 :                 if (pic_width % max_slices_h == 0)
     844             :                         break;
     845             : 
     846           0 :                 max_slices_h = dec_num_slices(dsc_common_caps.slice_caps, max_slices_h);
     847             :         }
     848             : 
     849           0 :         is_dsc_possible = (dsc_common_caps.max_slice_width > 0);
     850           0 :         if (!is_dsc_possible)
     851             :                 goto done;
     852             : 
     853           0 :         min_slices_h = pic_width / dsc_common_caps.max_slice_width;
     854           0 :         if (pic_width % dsc_common_caps.max_slice_width)
     855           0 :                 min_slices_h++;
     856             : 
     857           0 :         min_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, min_slices_h);
     858             : 
     859           0 :         while (min_slices_h <= max_slices_h) {
     860           0 :                 int pix_clk_per_slice_khz = dsc_div_by_10_round_up(timing->pix_clk_100hz) / min_slices_h;
     861           0 :                 if (pix_clk_per_slice_khz <= sink_per_slice_throughput_mps * 1000)
     862             :                         break;
     863             : 
     864           0 :                 min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
     865             :         }
     866             : 
     867           0 :         is_dsc_possible = (min_slices_h <= max_slices_h);
     868             : 
     869           0 :         if (pic_width % min_slices_h != 0)
     870           0 :                 min_slices_h = 0; // DSC TODO: Maybe try increasing the number of slices first?
     871             : 
     872           0 :         if (min_slices_h == 0 && max_slices_h == 0)
     873           0 :                 is_dsc_possible = false;
     874             : 
     875           0 :         if (!is_dsc_possible)
     876             :                 goto done;
     877             : 
     878           0 :         if (policy.use_min_slices_h) {
     879           0 :                 if (min_slices_h > 0)
     880             :                         num_slices_h = min_slices_h;
     881           0 :                 else if (max_slices_h > 0) { // Fall back to max slices if min slices is not working out
     882           0 :                         if (policy.max_slices_h)
     883           0 :                                 num_slices_h = min(policy.max_slices_h, max_slices_h);
     884             :                         else
     885             :                                 num_slices_h = max_slices_h;
     886             :                 } else
     887             :                         is_dsc_possible = false;
     888             :         } else {
     889           0 :                 if (max_slices_h > 0) {
     890           0 :                         if (policy.max_slices_h)
     891           0 :                                 num_slices_h = min(policy.max_slices_h, max_slices_h);
     892             :                         else
     893             :                                 num_slices_h = max_slices_h;
     894           0 :                 } else if (min_slices_h > 0) // Fall back to min slices if max slices is not possible
     895             :                         num_slices_h = min_slices_h;
     896             :                 else
     897           0 :                         is_dsc_possible = false;
     898             :         }
     899             : 
     900           0 :         if (!is_dsc_possible)
     901             :                 goto done;
     902             : 
     903           0 :         dsc_cfg->num_slices_h = num_slices_h;
     904           0 :         slice_width = pic_width / num_slices_h;
     905             : 
     906           0 :         is_dsc_possible = slice_width <= dsc_common_caps.max_slice_width;
     907           0 :         if (!is_dsc_possible)
     908             :                 goto done;
     909             : 
     910             :         // Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by.
     911             :         // For 4:2:0 make sure the slice height is divisible by 2 as well.
     912           0 :         if (min_slice_height_override == 0)
     913           0 :                 slice_height = min(policy.min_slice_height, pic_height);
     914             :         else
     915           0 :                 slice_height = min(min_slice_height_override, pic_height);
     916             : 
     917           0 :         while (slice_height < pic_height && (pic_height % slice_height != 0 ||
     918           0 :                 (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 && slice_height % 2 != 0)))
     919           0 :                 slice_height++;
     920             : 
     921           0 :         if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) // For the case when pic_height < dsc_policy.min_sice_height
     922           0 :                 is_dsc_possible = (slice_height % 2 == 0);
     923             : 
     924           0 :         if (!is_dsc_possible)
     925             :                 goto done;
     926             : 
     927           0 :         dsc_cfg->num_slices_v = pic_height/slice_height;
     928             : 
     929           0 :         if (target_bandwidth_kbps > 0) {
     930           0 :                 is_dsc_possible = decide_dsc_target_bpp_x16(
     931             :                                 &policy,
     932             :                                 &dsc_common_caps,
     933             :                                 target_bandwidth_kbps,
     934             :                                 timing,
     935             :                                 num_slices_h,
     936             :                                 &target_bpp);
     937           0 :                 dsc_cfg->bits_per_pixel = target_bpp;
     938             :         }
     939           0 :         if (!is_dsc_possible)
     940             :                 goto done;
     941             : 
     942             :         // Final decission: can we do DSC or not?
     943             :         if (is_dsc_possible) {
     944             :                 // Fill out the rest of DSC settings
     945           0 :                 dsc_cfg->block_pred_enable = dsc_common_caps.is_block_pred_supported;
     946           0 :                 dsc_cfg->linebuf_depth = dsc_common_caps.lb_bit_depth;
     947           0 :                 dsc_cfg->version_minor = (dsc_common_caps.dsc_version & 0xf0) >> 4;
     948           0 :                 dsc_cfg->is_dp = dsc_sink_caps->is_dp;
     949             :         }
     950             : 
     951             : done:
     952           0 :         if (!is_dsc_possible)
     953           0 :                 memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
     954             : 
     955           0 :         return is_dsc_possible;
     956             : }
     957             : 
     958           0 : bool dc_dsc_compute_config(
     959             :                 const struct display_stream_compressor *dsc,
     960             :                 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
     961             :                 uint32_t dsc_min_slice_height_override,
     962             :                 uint32_t max_target_bpp_limit_override,
     963             :                 uint32_t target_bandwidth_kbps,
     964             :                 const struct dc_crtc_timing *timing,
     965             :                 struct dc_dsc_config *dsc_cfg)
     966             : {
     967           0 :         bool is_dsc_possible = false;
     968             :         struct dsc_enc_caps dsc_enc_caps;
     969             : 
     970           0 :         get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
     971           0 :         is_dsc_possible = setup_dsc_config(dsc_sink_caps,
     972             :                 &dsc_enc_caps,
     973             :                 target_bandwidth_kbps,
     974             :                 timing, dsc_min_slice_height_override,
     975           0 :                 max_target_bpp_limit_override * 16, dsc_cfg);
     976           0 :         return is_dsc_possible;
     977             : }
     978             : 
     979           0 : uint32_t dc_dsc_stream_bandwidth_in_kbps(const struct dc_crtc_timing *timing,
     980             :         uint32_t bpp_x16, uint32_t num_slices_h, bool is_dp)
     981             : {
     982             :         uint32_t overhead_in_kbps;
     983             :         struct fixed31_32 bpp;
     984             :         struct fixed31_32 actual_bandwidth_in_kbps;
     985             : 
     986           0 :         overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
     987             :                 timing, num_slices_h, is_dp);
     988           0 :         bpp = dc_fixpt_from_fraction(bpp_x16, 16);
     989           0 :         actual_bandwidth_in_kbps = dc_fixpt_from_fraction(timing->pix_clk_100hz, 10);
     990           0 :         actual_bandwidth_in_kbps = dc_fixpt_mul(actual_bandwidth_in_kbps, bpp);
     991           0 :         actual_bandwidth_in_kbps = dc_fixpt_add_int(actual_bandwidth_in_kbps, overhead_in_kbps);
     992           0 :         return dc_fixpt_ceil(actual_bandwidth_in_kbps);
     993             : }
     994             : 
     995           0 : uint32_t dc_dsc_stream_bandwidth_overhead_in_kbps(
     996             :                 const struct dc_crtc_timing *timing,
     997             :                 const int num_slices_h,
     998             :                 const bool is_dp)
     999             : {
    1000             :         struct fixed31_32 max_dsc_overhead;
    1001             :         struct fixed31_32 refresh_rate;
    1002             : 
    1003           0 :         if (dsc_policy_disable_dsc_stream_overhead || !is_dp)
    1004             :                 return 0;
    1005             : 
    1006             :         /* use target bpp that can take entire target bandwidth */
    1007           0 :         refresh_rate = dc_fixpt_from_int(timing->pix_clk_100hz);
    1008           0 :         refresh_rate = dc_fixpt_div_int(refresh_rate, timing->h_total);
    1009           0 :         refresh_rate = dc_fixpt_div_int(refresh_rate, timing->v_total);
    1010           0 :         refresh_rate = dc_fixpt_mul_int(refresh_rate, 100);
    1011             : 
    1012           0 :         max_dsc_overhead = dc_fixpt_from_int(num_slices_h);
    1013           0 :         max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, timing->v_total);
    1014           0 :         max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, 256);
    1015           0 :         max_dsc_overhead = dc_fixpt_div_int(max_dsc_overhead, 1000);
    1016           0 :         max_dsc_overhead = dc_fixpt_mul(max_dsc_overhead, refresh_rate);
    1017             : 
    1018           0 :         return dc_fixpt_ceil(max_dsc_overhead);
    1019             : }
    1020             : 
    1021           0 : void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing *timing,
    1022             :                 uint32_t max_target_bpp_limit_override_x16,
    1023             :                 struct dc_dsc_policy *policy)
    1024             : {
    1025           0 :         uint32_t bpc = 0;
    1026             : 
    1027           0 :         policy->min_target_bpp = 0;
    1028           0 :         policy->max_target_bpp = 0;
    1029             : 
    1030             :         /* DSC Policy: Use minimum number of slices that fits the pixel clock */
    1031           0 :         policy->use_min_slices_h = true;
    1032             : 
    1033             :         /* DSC Policy: Use max available slices
    1034             :          * (in our case 4 for or 8, depending on the mode)
    1035             :          */
    1036           0 :         policy->max_slices_h = 0;
    1037             : 
    1038             :         /* DSC Policy: Use slice height recommended
    1039             :          * by VESA DSC Spreadsheet user guide
    1040             :          */
    1041           0 :         policy->min_slice_height = 108;
    1042             : 
    1043             :         /* DSC Policy: follow DP specs with an internal upper limit to 16 bpp
    1044             :          * for better interoperability
    1045             :          */
    1046           0 :         switch (timing->display_color_depth) {
    1047             :         case COLOR_DEPTH_888:
    1048             :                 bpc = 8;
    1049             :                 break;
    1050             :         case COLOR_DEPTH_101010:
    1051           0 :                 bpc = 10;
    1052           0 :                 break;
    1053             :         case COLOR_DEPTH_121212:
    1054           0 :                 bpc = 12;
    1055           0 :                 break;
    1056             :         default:
    1057             :                 return;
    1058             :         }
    1059           0 :         switch (timing->pixel_encoding) {
    1060             :         case PIXEL_ENCODING_RGB:
    1061             :         case PIXEL_ENCODING_YCBCR444:
    1062             :         case PIXEL_ENCODING_YCBCR422: /* assume no YCbCr422 native support */
    1063             :                 /* DP specs limits to 8 */
    1064           0 :                 policy->min_target_bpp = 8;
    1065             :                 /* DP specs limits to 3 x bpc */
    1066           0 :                 policy->max_target_bpp = 3 * bpc;
    1067           0 :                 break;
    1068             :         case PIXEL_ENCODING_YCBCR420:
    1069             :                 /* DP specs limits to 6 */
    1070           0 :                 policy->min_target_bpp = 6;
    1071             :                 /* DP specs limits to 1.5 x bpc assume bpc is an even number */
    1072           0 :                 policy->max_target_bpp = bpc * 3 / 2;
    1073           0 :                 break;
    1074             :         default:
    1075             :                 return;
    1076             :         }
    1077             : 
    1078             :         /* internal upper limit, default 16 bpp */
    1079           0 :         if (policy->max_target_bpp > dsc_policy_max_target_bpp_limit)
    1080           0 :                 policy->max_target_bpp = dsc_policy_max_target_bpp_limit;
    1081             : 
    1082             :         /* apply override */
    1083           0 :         if (max_target_bpp_limit_override_x16 && policy->max_target_bpp > max_target_bpp_limit_override_x16 / 16)
    1084           0 :                 policy->max_target_bpp = max_target_bpp_limit_override_x16 / 16;
    1085             : 
    1086             :         /* enable DSC when not needed, default false */
    1087           0 :         if (dsc_policy_enable_dsc_when_not_needed)
    1088           0 :                 policy->enable_dsc_when_not_needed = dsc_policy_enable_dsc_when_not_needed;
    1089             :         else
    1090           0 :                 policy->enable_dsc_when_not_needed = false;
    1091             : }
    1092             : 
    1093           0 : void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit)
    1094             : {
    1095           0 :         dsc_policy_max_target_bpp_limit = limit;
    1096           0 : }
    1097             : 
    1098           0 : void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable)
    1099             : {
    1100           0 :         dsc_policy_enable_dsc_when_not_needed = enable;
    1101           0 : }
    1102             : 
    1103           0 : void dc_dsc_policy_set_disable_dsc_stream_overhead(bool disable)
    1104             : {
    1105           0 :         dsc_policy_disable_dsc_stream_overhead = disable;
    1106           0 : }

Generated by: LCOV version 1.14