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