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 #include "clk_mgr.h" 34 #include "resource.h" 35 36 #define DC_LOGGER \ 37 dsc->ctx->logger 38 39 /* This module's internal functions */ 40 41 /* default DSC policy target bitrate limit is 16bpp */ 42 static uint32_t dsc_policy_max_target_bpp_limit = 16; 43 44 /* default DSC policy enables DSC only when needed */ 45 static bool dsc_policy_enable_dsc_when_not_needed; 46 47 static bool dsc_policy_disable_dsc_stream_overhead; 48 49 static bool disable_128b_132b_stream_overhead; 50 51 #ifndef MAX 52 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) 53 #endif 54 #ifndef MIN 55 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) 56 #endif 57 58 /* Need to account for padding due to pixel-to-symbol packing 59 * for uncompressed 128b/132b streams. 60 */ 61 static uint32_t apply_128b_132b_stream_overhead( 62 const struct dc_crtc_timing *timing, const uint32_t kbps) 63 { 64 uint32_t total_kbps = kbps; 65 66 if (disable_128b_132b_stream_overhead) 67 return kbps; 68 69 if (!timing->flags.DSC) { 70 struct fixed31_32 bpp; 71 struct fixed31_32 overhead_factor; 72 73 bpp = dc_fixpt_from_int(kbps); 74 bpp = dc_fixpt_div_int(bpp, timing->pix_clk_100hz / 10); 75 76 /* Symbols_per_HActive = HActive * bpp / (4 lanes * 32-bit symbol size) 77 * Overhead_factor = ceil(Symbols_per_HActive) / Symbols_per_HActive 78 */ 79 overhead_factor = dc_fixpt_from_int(timing->h_addressable); 80 overhead_factor = dc_fixpt_mul(overhead_factor, bpp); 81 overhead_factor = dc_fixpt_div_int(overhead_factor, 128); 82 overhead_factor = dc_fixpt_div( 83 dc_fixpt_from_int(dc_fixpt_ceil(overhead_factor)), 84 overhead_factor); 85 86 total_kbps = dc_fixpt_ceil( 87 dc_fixpt_mul_int(overhead_factor, total_kbps)); 88 } 89 90 return total_kbps; 91 } 92 93 uint32_t dc_bandwidth_in_kbps_from_timing( 94 const struct dc_crtc_timing *timing, 95 const enum dc_link_encoding_format link_encoding) 96 { 97 uint32_t bits_per_channel = 0; 98 uint32_t kbps; 99 100 if (timing->flags.DSC) 101 return dc_dsc_stream_bandwidth_in_kbps(timing, 102 timing->dsc_cfg.bits_per_pixel, 103 timing->dsc_cfg.num_slices_h, 104 timing->dsc_cfg.is_dp); 105 106 switch (timing->display_color_depth) { 107 case COLOR_DEPTH_666: 108 bits_per_channel = 6; 109 break; 110 case COLOR_DEPTH_888: 111 bits_per_channel = 8; 112 break; 113 case COLOR_DEPTH_101010: 114 bits_per_channel = 10; 115 break; 116 case COLOR_DEPTH_121212: 117 bits_per_channel = 12; 118 break; 119 case COLOR_DEPTH_141414: 120 bits_per_channel = 14; 121 break; 122 case COLOR_DEPTH_161616: 123 bits_per_channel = 16; 124 break; 125 default: 126 ASSERT(bits_per_channel != 0); 127 bits_per_channel = 8; 128 break; 129 } 130 131 kbps = timing->pix_clk_100hz / 10; 132 kbps *= bits_per_channel; 133 134 if (timing->flags.Y_ONLY != 1) { 135 /*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/ 136 kbps *= 3; 137 if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) 138 kbps /= 2; 139 else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422) 140 kbps = kbps * 2 / 3; 141 } 142 143 if (link_encoding == DC_LINK_ENCODING_DP_128b_132b) 144 kbps = apply_128b_132b_stream_overhead(timing, kbps); 145 146 if (link_encoding == DC_LINK_ENCODING_HDMI_FRL && 147 timing->vic == 0 && timing->hdmi_vic == 0 && 148 timing->frl_uncompressed_video_bandwidth_in_kbps != 0) 149 kbps = timing->frl_uncompressed_video_bandwidth_in_kbps; 150 151 return kbps; 152 } 153 154 /* Forward Declerations */ 155 static unsigned int get_min_dsc_slice_count_for_odm( 156 const struct display_stream_compressor *dsc, 157 const struct dsc_enc_caps *dsc_enc_caps, 158 const struct dc_crtc_timing *timing); 159 160 static bool decide_dsc_bandwidth_range( 161 const uint32_t min_bpp_x16, 162 const uint32_t max_bpp_x16, 163 const uint32_t num_slices_h, 164 const struct dsc_enc_caps *dsc_caps, 165 const struct dc_crtc_timing *timing, 166 const enum dc_link_encoding_format link_encoding, 167 struct dc_dsc_bw_range *range); 168 169 static uint32_t compute_bpp_x16_from_target_bandwidth( 170 const uint32_t bandwidth_in_kbps, 171 const struct dc_crtc_timing *timing, 172 const uint32_t num_slices_h, 173 const uint32_t bpp_increment_div, 174 const bool is_dp); 175 176 static void get_dsc_enc_caps( 177 const struct display_stream_compressor *dsc, 178 struct dsc_enc_caps *dsc_enc_caps, 179 int pixel_clock_100Hz); 180 181 static bool intersect_dsc_caps( 182 const struct dsc_dec_dpcd_caps *dsc_sink_caps, 183 const struct dsc_enc_caps *dsc_enc_caps, 184 enum dc_pixel_encoding pixel_encoding, 185 struct dsc_enc_caps *dsc_common_caps); 186 187 static bool setup_dsc_config( 188 const struct dsc_dec_dpcd_caps *dsc_sink_caps, 189 const struct dsc_enc_caps *dsc_enc_caps, 190 int target_bandwidth_kbps, 191 const struct dc_crtc_timing *timing, 192 const struct dc_dsc_config_options *options, 193 const enum dc_link_encoding_format link_encoding, 194 int min_slice_count, 195 struct dc_dsc_config *dsc_cfg); 196 197 static bool dsc_buff_block_size_from_dpcd(int dpcd_buff_block_size, int *buff_block_size) 198 { 199 200 switch (dpcd_buff_block_size) { 201 case DP_DSC_RC_BUF_BLK_SIZE_1: 202 *buff_block_size = 1024; 203 break; 204 case DP_DSC_RC_BUF_BLK_SIZE_4: 205 *buff_block_size = 4 * 1024; 206 break; 207 case DP_DSC_RC_BUF_BLK_SIZE_16: 208 *buff_block_size = 16 * 1024; 209 break; 210 case DP_DSC_RC_BUF_BLK_SIZE_64: 211 *buff_block_size = 64 * 1024; 212 break; 213 default: { 214 dm_error("%s: DPCD DSC buffer size not recognized.\n", __func__); 215 return false; 216 } 217 } 218 219 return true; 220 } 221 222 223 static bool dsc_line_buff_depth_from_dpcd(int dpcd_line_buff_bit_depth, int *line_buff_bit_depth) 224 { 225 if (0 <= dpcd_line_buff_bit_depth && dpcd_line_buff_bit_depth <= 7) 226 *line_buff_bit_depth = dpcd_line_buff_bit_depth + 9; 227 else if (dpcd_line_buff_bit_depth == 8) 228 *line_buff_bit_depth = 8; 229 else { 230 dm_error("%s: DPCD DSC buffer depth not recognized.\n", __func__); 231 return false; 232 } 233 234 return true; 235 } 236 237 238 static bool dsc_throughput_from_dpcd(int dpcd_throughput, int *throughput) 239 { 240 switch (dpcd_throughput) { 241 case DP_DSC_THROUGHPUT_MODE_0_UNSUPPORTED: 242 *throughput = 0; 243 break; 244 case DP_DSC_THROUGHPUT_MODE_0_170: 245 *throughput = 170; 246 break; 247 case DP_DSC_THROUGHPUT_MODE_0_340: 248 *throughput = 340; 249 break; 250 case DP_DSC_THROUGHPUT_MODE_0_400: 251 *throughput = 400; 252 break; 253 case DP_DSC_THROUGHPUT_MODE_0_450: 254 *throughput = 450; 255 break; 256 case DP_DSC_THROUGHPUT_MODE_0_500: 257 *throughput = 500; 258 break; 259 case DP_DSC_THROUGHPUT_MODE_0_550: 260 *throughput = 550; 261 break; 262 case DP_DSC_THROUGHPUT_MODE_0_600: 263 *throughput = 600; 264 break; 265 case DP_DSC_THROUGHPUT_MODE_0_650: 266 *throughput = 650; 267 break; 268 case DP_DSC_THROUGHPUT_MODE_0_700: 269 *throughput = 700; 270 break; 271 case DP_DSC_THROUGHPUT_MODE_0_750: 272 *throughput = 750; 273 break; 274 case DP_DSC_THROUGHPUT_MODE_0_800: 275 *throughput = 800; 276 break; 277 case DP_DSC_THROUGHPUT_MODE_0_850: 278 *throughput = 850; 279 break; 280 case DP_DSC_THROUGHPUT_MODE_0_900: 281 *throughput = 900; 282 break; 283 case DP_DSC_THROUGHPUT_MODE_0_950: 284 *throughput = 950; 285 break; 286 case DP_DSC_THROUGHPUT_MODE_0_1000: 287 *throughput = 1000; 288 break; 289 default: { 290 dm_error("%s: DPCD DSC throughput mode not recognized.\n", __func__); 291 return false; 292 } 293 } 294 295 return true; 296 } 297 298 299 static bool dsc_bpp_increment_div_from_dpcd(uint8_t bpp_increment_dpcd, uint32_t *bpp_increment_div) 300 { 301 // Mask bpp increment dpcd field to avoid reading other fields 302 bpp_increment_dpcd &= 0x7; 303 304 switch (bpp_increment_dpcd) { 305 case 0: 306 *bpp_increment_div = 16; 307 break; 308 case 1: 309 *bpp_increment_div = 8; 310 break; 311 case 2: 312 *bpp_increment_div = 4; 313 break; 314 case 3: 315 *bpp_increment_div = 2; 316 break; 317 case 4: 318 *bpp_increment_div = 1; 319 break; 320 default: { 321 dm_error("%s: DPCD DSC bits-per-pixel increment not recognized.\n", __func__); 322 return false; 323 } 324 } 325 326 return true; 327 } 328 329 330 331 bool dc_dsc_parse_dsc_dpcd(const struct dc *dc, 332 const uint8_t *dpcd_dsc_basic_data, 333 const uint8_t *dpcd_dsc_branch_decoder_caps, 334 struct dsc_dec_dpcd_caps *dsc_sink_caps) 335 { 336 if (!dpcd_dsc_basic_data) 337 return false; 338 339 dsc_sink_caps->is_dsc_supported = 340 (dpcd_dsc_basic_data[DP_DSC_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_DECOMPRESSION_IS_SUPPORTED) != 0; 341 if (!dsc_sink_caps->is_dsc_supported) 342 return false; 343 344 dsc_sink_caps->dsc_version = dpcd_dsc_basic_data[DP_DSC_REV - DP_DSC_SUPPORT]; 345 346 { 347 int buff_block_size; 348 int buff_size; 349 350 if (!dsc_buff_block_size_from_dpcd( 351 dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT] & 0x03, 352 &buff_block_size)) 353 return false; 354 355 buff_size = dpcd_dsc_basic_data[DP_DSC_RC_BUF_SIZE - DP_DSC_SUPPORT] + 1; 356 dsc_sink_caps->rc_buffer_size = buff_size * buff_block_size; 357 } 358 359 dsc_sink_caps->slice_caps1.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT]; 360 if (!dsc_line_buff_depth_from_dpcd(dpcd_dsc_basic_data[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT], 361 &dsc_sink_caps->lb_bit_depth)) 362 return false; 363 364 dsc_sink_caps->is_block_pred_supported = 365 (dpcd_dsc_basic_data[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] & 366 DP_DSC_BLK_PREDICTION_IS_SUPPORTED) != 0; 367 368 dsc_sink_caps->edp_max_bits_per_pixel = 369 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_LOW - DP_DSC_SUPPORT] | 370 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] << 8; 371 372 dsc_sink_caps->color_formats.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT]; 373 dsc_sink_caps->color_depth.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT]; 374 375 { 376 int dpcd_throughput = dpcd_dsc_basic_data[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT]; 377 int dsc_throughput_granular_delta; 378 379 dsc_throughput_granular_delta = dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT] >> 3; 380 dsc_throughput_granular_delta *= 2; 381 382 if (!dsc_throughput_from_dpcd(dpcd_throughput & DP_DSC_THROUGHPUT_MODE_0_MASK, 383 &dsc_sink_caps->throughput_mode_0_mps)) 384 return false; 385 dsc_sink_caps->throughput_mode_0_mps += dsc_throughput_granular_delta; 386 387 dpcd_throughput = (dpcd_throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >> DP_DSC_THROUGHPUT_MODE_1_SHIFT; 388 if (!dsc_throughput_from_dpcd(dpcd_throughput, &dsc_sink_caps->throughput_mode_1_mps)) 389 return false; 390 } 391 392 dsc_sink_caps->max_slice_width = dpcd_dsc_basic_data[DP_DSC_MAX_SLICE_WIDTH - DP_DSC_SUPPORT] * 320; 393 dsc_sink_caps->slice_caps2.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT]; 394 395 if (!dsc_bpp_increment_div_from_dpcd(dpcd_dsc_basic_data[DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT], 396 &dsc_sink_caps->bpp_increment_div)) 397 return false; 398 399 if (dc->debug.dsc_bpp_increment_div) { 400 /* dsc_bpp_increment_div should onl be 1, 2, 4, 8 or 16, but rather than rejecting invalid values, 401 * we'll accept all and get it into range. This also makes the above check against 0 redundant, 402 * but that one stresses out the override will be only used if it's not 0. 403 */ 404 if (dc->debug.dsc_bpp_increment_div >= 1) 405 dsc_sink_caps->bpp_increment_div = 1; 406 if (dc->debug.dsc_bpp_increment_div >= 2) 407 dsc_sink_caps->bpp_increment_div = 2; 408 if (dc->debug.dsc_bpp_increment_div >= 4) 409 dsc_sink_caps->bpp_increment_div = 4; 410 if (dc->debug.dsc_bpp_increment_div >= 8) 411 dsc_sink_caps->bpp_increment_div = 8; 412 if (dc->debug.dsc_bpp_increment_div >= 16) 413 dsc_sink_caps->bpp_increment_div = 16; 414 } 415 416 /* Extended caps */ 417 if (dpcd_dsc_branch_decoder_caps == NULL) { // branch decoder DPCD DSC data can be null for non branch device 418 dsc_sink_caps->branch_overall_throughput_0_mps = 0; 419 dsc_sink_caps->branch_overall_throughput_1_mps = 0; 420 dsc_sink_caps->branch_max_line_width = 0; 421 return true; 422 } 423 424 dsc_sink_caps->branch_overall_throughput_0_mps = 425 dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_0 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0]; 426 if (dsc_sink_caps->branch_overall_throughput_0_mps == 0) 427 dsc_sink_caps->branch_overall_throughput_0_mps = 0; 428 else if (dsc_sink_caps->branch_overall_throughput_0_mps == 1) 429 dsc_sink_caps->branch_overall_throughput_0_mps = 680; 430 else { 431 dsc_sink_caps->branch_overall_throughput_0_mps *= 50; 432 dsc_sink_caps->branch_overall_throughput_0_mps += 600; 433 } 434 435 dsc_sink_caps->branch_overall_throughput_1_mps = 436 dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_1 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0]; 437 if (dsc_sink_caps->branch_overall_throughput_1_mps == 0) 438 dsc_sink_caps->branch_overall_throughput_1_mps = 0; 439 else if (dsc_sink_caps->branch_overall_throughput_1_mps == 1) 440 dsc_sink_caps->branch_overall_throughput_1_mps = 680; 441 else { 442 dsc_sink_caps->branch_overall_throughput_1_mps *= 50; 443 dsc_sink_caps->branch_overall_throughput_1_mps += 600; 444 } 445 446 dsc_sink_caps->branch_max_line_width = 447 dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_MAX_LINE_WIDTH - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0] * 320; 448 ASSERT(dsc_sink_caps->branch_max_line_width == 0 || dsc_sink_caps->branch_max_line_width >= 5120); 449 450 dsc_sink_caps->is_dp = true; 451 return true; 452 } 453 454 /* If DSC is possbile, get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range and 455 * timing's pixel clock and uncompressed bandwidth. 456 * If DSC is not possible, leave '*range' untouched. 457 */ 458 bool dc_dsc_compute_bandwidth_range( 459 const struct display_stream_compressor *dsc, 460 uint32_t dsc_min_slice_height_override, 461 uint32_t min_bpp_x16, 462 uint32_t max_bpp_x16, 463 const struct dsc_dec_dpcd_caps *dsc_sink_caps, 464 const struct dc_crtc_timing *timing, 465 const enum dc_link_encoding_format link_encoding, 466 struct dc_dsc_bw_range *range) 467 { 468 bool is_dsc_possible = false; 469 unsigned int min_dsc_slice_count; 470 struct dsc_enc_caps dsc_enc_caps; 471 struct dsc_enc_caps dsc_common_caps; 472 struct dc_dsc_config config = {0}; 473 struct dc_dsc_config_options options = {0}; 474 475 options.dsc_min_slice_height_override = dsc_min_slice_height_override; 476 options.max_target_bpp_limit_override_x16 = max_bpp_x16; 477 options.slice_height_granularity = 1; 478 479 get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz); 480 481 min_dsc_slice_count = get_min_dsc_slice_count_for_odm(dsc, &dsc_enc_caps, timing); 482 483 is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, &dsc_enc_caps, 484 timing->pixel_encoding, &dsc_common_caps); 485 486 if (is_dsc_possible) 487 is_dsc_possible = setup_dsc_config(dsc_sink_caps, &dsc_enc_caps, 0, timing, 488 &options, link_encoding, min_dsc_slice_count, &config); 489 490 if (is_dsc_possible) 491 is_dsc_possible = decide_dsc_bandwidth_range(min_bpp_x16, max_bpp_x16, 492 config.num_slices_h, &dsc_common_caps, timing, link_encoding, range); 493 494 return is_dsc_possible; 495 } 496 497 void dc_dsc_dump_encoder_caps(const struct display_stream_compressor *dsc, 498 const struct dc_crtc_timing *timing) 499 { 500 struct dsc_enc_caps dsc_enc_caps; 501 502 get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz); 503 504 DC_LOG_DSC("dsc encoder caps:"); 505 DC_LOG_DSC("\tdsc_version 0x%x", dsc_enc_caps.dsc_version); 506 DC_LOG_DSC("\tslice_caps 0x%x", dsc_enc_caps.slice_caps.raw); 507 DC_LOG_DSC("\tlb_bit_depth %d", dsc_enc_caps.lb_bit_depth); 508 DC_LOG_DSC("\tis_block_pred_supported %d", dsc_enc_caps.is_block_pred_supported); 509 DC_LOG_DSC("\tcolor_formats 0x%x", dsc_enc_caps.color_formats.raw); 510 DC_LOG_DSC("\tcolor_depth 0x%x", dsc_enc_caps.color_depth.raw); 511 DC_LOG_DSC("\tmax_total_throughput_mps %d", dsc_enc_caps.max_total_throughput_mps); 512 DC_LOG_DSC("\tmax_slice_width %d", dsc_enc_caps.max_slice_width); 513 DC_LOG_DSC("\tbpp_increment_div %d", dsc_enc_caps.bpp_increment_div); 514 } 515 516 void dc_dsc_dump_decoder_caps(const struct display_stream_compressor *dsc, 517 const struct dsc_dec_dpcd_caps *dsc_sink_caps) 518 { 519 DC_LOG_DSC("dsc decoder caps:"); 520 DC_LOG_DSC("\tis_dsc_supported %d", dsc_sink_caps->is_dsc_supported); 521 DC_LOG_DSC("\tdsc_version 0x%x", dsc_sink_caps->dsc_version); 522 DC_LOG_DSC("\trc_buffer_size %d", dsc_sink_caps->rc_buffer_size); 523 DC_LOG_DSC("\tslice_caps1 0x%x", dsc_sink_caps->slice_caps1.raw); 524 DC_LOG_DSC("\tslice_caps2 0x%x", dsc_sink_caps->slice_caps2.raw); 525 DC_LOG_DSC("\tlb_bit_depth %d", dsc_sink_caps->lb_bit_depth); 526 DC_LOG_DSC("\tis_block_pred_supported %d", dsc_sink_caps->is_block_pred_supported); 527 DC_LOG_DSC("\tedp_max_bits_per_pixel %d", dsc_sink_caps->edp_max_bits_per_pixel); 528 DC_LOG_DSC("\tcolor_formats 0x%x", dsc_sink_caps->color_formats.raw); 529 DC_LOG_DSC("\tthroughput_mode_0_mps %d", dsc_sink_caps->throughput_mode_0_mps); 530 DC_LOG_DSC("\tthroughput_mode_1_mps %d", dsc_sink_caps->throughput_mode_1_mps); 531 DC_LOG_DSC("\tmax_slice_width %d", dsc_sink_caps->max_slice_width); 532 DC_LOG_DSC("\tbpp_increment_div %d", dsc_sink_caps->bpp_increment_div); 533 DC_LOG_DSC("\tbranch_overall_throughput_0_mps %d", dsc_sink_caps->branch_overall_throughput_0_mps); 534 DC_LOG_DSC("\tbranch_overall_throughput_1_mps %d", dsc_sink_caps->branch_overall_throughput_1_mps); 535 DC_LOG_DSC("\tbranch_max_line_width %d", dsc_sink_caps->branch_max_line_width); 536 DC_LOG_DSC("\tis_dp %d", dsc_sink_caps->is_dp); 537 } 538 539 540 static void build_dsc_enc_combined_slice_caps( 541 const struct dsc_enc_caps *single_dsc_enc_caps, 542 struct dsc_enc_caps *dsc_enc_caps, 543 unsigned int max_odm_combine_factor) 544 { 545 /* 1-16 slice configurations, single DSC */ 546 dsc_enc_caps->slice_caps.raw |= single_dsc_enc_caps->slice_caps.raw; 547 548 /* 2x DSC's */ 549 if (max_odm_combine_factor >= 2) { 550 /* 1 + 1 */ 551 dsc_enc_caps->slice_caps.bits.NUM_SLICES_2 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_1; 552 553 /* 2 + 2 */ 554 dsc_enc_caps->slice_caps.bits.NUM_SLICES_4 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_2; 555 556 /* 4 + 4 */ 557 dsc_enc_caps->slice_caps.bits.NUM_SLICES_8 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_4; 558 559 /* 8 + 8 */ 560 dsc_enc_caps->slice_caps.bits.NUM_SLICES_16 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_8; 561 } 562 563 /* 3x DSC's */ 564 if (max_odm_combine_factor >= 3) { 565 /* 4 + 4 + 4 */ 566 dsc_enc_caps->slice_caps.bits.NUM_SLICES_12 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_4; 567 } 568 569 /* 4x DSC's */ 570 if (max_odm_combine_factor >= 4) { 571 /* 1 + 1 + 1 + 1 */ 572 dsc_enc_caps->slice_caps.bits.NUM_SLICES_4 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_1; 573 574 /* 2 + 2 + 2 + 2 */ 575 dsc_enc_caps->slice_caps.bits.NUM_SLICES_8 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_2; 576 577 /* 3 + 3 + 3 + 3 */ 578 dsc_enc_caps->slice_caps.bits.NUM_SLICES_12 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_3; 579 580 /* 4 + 4 + 4 + 4 */ 581 dsc_enc_caps->slice_caps.bits.NUM_SLICES_16 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_4; 582 } 583 } 584 585 static void build_dsc_enc_caps( 586 const struct display_stream_compressor *dsc, 587 struct dsc_enc_caps *dsc_enc_caps) 588 { 589 unsigned int max_dscclk_khz; 590 unsigned int num_dsc; 591 unsigned int max_odm_combine_factor; 592 struct dsc_enc_caps single_dsc_enc_caps; 593 594 struct dc *dc; 595 596 if (!dsc || !dsc->ctx || !dsc->ctx->dc || !dsc->funcs->dsc_get_single_enc_caps) 597 return; 598 599 dc = dsc->ctx->dc; 600 601 if (!dc->clk_mgr || !dc->clk_mgr->funcs->get_max_clock_khz || !dc->res_pool || dc->debug.disable_dsc) 602 return; 603 604 /* get max DSCCLK from clk_mgr */ 605 max_dscclk_khz = dc->clk_mgr->funcs->get_max_clock_khz(dc->clk_mgr, CLK_TYPE_DSCCLK); 606 607 dsc->funcs->dsc_get_single_enc_caps(&single_dsc_enc_caps, max_dscclk_khz); 608 609 /* global capabilities */ 610 dsc_enc_caps->dsc_version = single_dsc_enc_caps.dsc_version; 611 dsc_enc_caps->lb_bit_depth = single_dsc_enc_caps.lb_bit_depth; 612 dsc_enc_caps->is_block_pred_supported = single_dsc_enc_caps.is_block_pred_supported; 613 dsc_enc_caps->max_slice_width = single_dsc_enc_caps.max_slice_width; 614 dsc_enc_caps->bpp_increment_div = single_dsc_enc_caps.bpp_increment_div; 615 dsc_enc_caps->color_formats.raw = single_dsc_enc_caps.color_formats.raw; 616 dsc_enc_caps->color_depth.raw = single_dsc_enc_caps.color_depth.raw; 617 618 /* expand per DSC capabilities to global */ 619 max_odm_combine_factor = dc->caps.max_odm_combine_factor; 620 num_dsc = dc->res_pool->res_cap->num_dsc; 621 max_odm_combine_factor = min(max_odm_combine_factor, num_dsc); 622 dsc_enc_caps->max_total_throughput_mps = 623 single_dsc_enc_caps.max_total_throughput_mps * 624 max_odm_combine_factor; 625 626 /* check slice counts possible for with ODM combine */ 627 build_dsc_enc_combined_slice_caps(&single_dsc_enc_caps, dsc_enc_caps, max_odm_combine_factor); 628 } 629 630 static inline uint32_t dsc_div_by_10_round_up(uint32_t value) 631 { 632 return (value + 9) / 10; 633 } 634 635 static unsigned int get_min_dsc_slice_count_for_odm( 636 const struct display_stream_compressor *dsc, 637 const struct dsc_enc_caps *dsc_enc_caps, 638 const struct dc_crtc_timing *timing) 639 { 640 unsigned int max_dispclk_khz; 641 642 /* get max pixel rate and combine caps */ 643 max_dispclk_khz = dsc_enc_caps->max_total_throughput_mps * 1000; 644 if (dsc && dsc->ctx->dc) { 645 if (dsc->ctx->dc->clk_mgr && 646 dsc->ctx->dc->clk_mgr->funcs->get_max_clock_khz) { 647 /* dispclk is available */ 648 max_dispclk_khz = dsc->ctx->dc->clk_mgr->funcs->get_max_clock_khz(dsc->ctx->dc->clk_mgr, CLK_TYPE_DISPCLK); 649 } 650 } 651 652 /* validate parameters */ 653 if (max_dispclk_khz == 0 || dsc_enc_caps->max_slice_width == 0) 654 return 1; 655 656 /* consider minimum odm slices required due to 657 * 1) display pipe throughput (dispclk) 658 * 2) max image width per slice 659 */ 660 return dc_fixpt_ceil(dc_fixpt_max( 661 dc_fixpt_div_int(dc_fixpt_from_int(dsc_div_by_10_round_up(timing->pix_clk_100hz)), 662 max_dispclk_khz), // throughput 663 dc_fixpt_div_int(dc_fixpt_from_int(timing->h_addressable + timing->h_border_left + timing->h_border_right), 664 dsc_enc_caps->max_slice_width))); // slice width 665 } 666 667 static void get_dsc_enc_caps( 668 const struct display_stream_compressor *dsc, 669 struct dsc_enc_caps *dsc_enc_caps, 670 int pixel_clock_100Hz) 671 { 672 memset(dsc_enc_caps, 0, sizeof(struct dsc_enc_caps)); 673 674 if (!dsc || !dsc->ctx || !dsc->ctx->dc || dsc->ctx->dc->debug.disable_dsc) 675 return; 676 677 /* check if reported cap global or only for a single DCN DSC enc */ 678 if (dsc->funcs->dsc_get_enc_caps) { 679 dsc->funcs->dsc_get_enc_caps(dsc_enc_caps, pixel_clock_100Hz); 680 } else { 681 build_dsc_enc_caps(dsc, dsc_enc_caps); 682 } 683 } 684 685 /* Returns 'false' if no intersection was found for at least one capability. 686 * It also implicitly validates some sink caps against invalid value of zero. 687 */ 688 static bool intersect_dsc_caps( 689 const struct dsc_dec_dpcd_caps *dsc_sink_caps, 690 const struct dsc_enc_caps *dsc_enc_caps, 691 enum dc_pixel_encoding pixel_encoding, 692 struct dsc_enc_caps *dsc_common_caps) 693 { 694 int32_t max_slices; 695 int32_t total_sink_throughput; 696 697 memset(dsc_common_caps, 0, sizeof(struct dsc_enc_caps)); 698 699 dsc_common_caps->dsc_version = min(dsc_sink_caps->dsc_version, dsc_enc_caps->dsc_version); 700 if (!dsc_common_caps->dsc_version) 701 return false; 702 703 dsc_common_caps->slice_caps.bits.NUM_SLICES_1 = 704 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_1 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_1; 705 dsc_common_caps->slice_caps.bits.NUM_SLICES_2 = 706 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_2 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_2; 707 dsc_common_caps->slice_caps.bits.NUM_SLICES_4 = 708 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_4 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_4; 709 dsc_common_caps->slice_caps.bits.NUM_SLICES_8 = 710 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_8 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_8; 711 dsc_common_caps->slice_caps.bits.NUM_SLICES_12 = 712 dsc_sink_caps->slice_caps1.bits.NUM_SLICES_12 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_12; 713 dsc_common_caps->slice_caps.bits.NUM_SLICES_16 = 714 dsc_sink_caps->slice_caps2.bits.NUM_SLICES_16 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_16; 715 716 if (!dsc_common_caps->slice_caps.raw) 717 return false; 718 719 dsc_common_caps->lb_bit_depth = min(dsc_sink_caps->lb_bit_depth, dsc_enc_caps->lb_bit_depth); 720 if (!dsc_common_caps->lb_bit_depth) 721 return false; 722 723 dsc_common_caps->is_block_pred_supported = 724 dsc_sink_caps->is_block_pred_supported && dsc_enc_caps->is_block_pred_supported; 725 726 dsc_common_caps->color_formats.raw = dsc_sink_caps->color_formats.raw & dsc_enc_caps->color_formats.raw; 727 if (!dsc_common_caps->color_formats.raw) 728 return false; 729 730 dsc_common_caps->color_depth.raw = dsc_sink_caps->color_depth.raw & dsc_enc_caps->color_depth.raw; 731 if (!dsc_common_caps->color_depth.raw) 732 return false; 733 734 max_slices = 0; 735 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_1) 736 max_slices = 1; 737 738 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_2) 739 max_slices = 2; 740 741 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_4) 742 max_slices = 4; 743 744 total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_0_mps; 745 if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420) 746 total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_1_mps; 747 748 dsc_common_caps->max_total_throughput_mps = min(total_sink_throughput, dsc_enc_caps->max_total_throughput_mps); 749 750 dsc_common_caps->max_slice_width = min(dsc_sink_caps->max_slice_width, dsc_enc_caps->max_slice_width); 751 if (!dsc_common_caps->max_slice_width) 752 return false; 753 754 dsc_common_caps->bpp_increment_div = min(dsc_sink_caps->bpp_increment_div, dsc_enc_caps->bpp_increment_div); 755 756 // TODO DSC: Remove this workaround for N422 and 420 once it's fixed, or move it to get_dsc_encoder_caps() 757 if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420) 758 dsc_common_caps->bpp_increment_div = min(dsc_common_caps->bpp_increment_div, (uint32_t)8); 759 760 dsc_common_caps->edp_sink_max_bits_per_pixel = dsc_sink_caps->edp_max_bits_per_pixel; 761 dsc_common_caps->is_dp = dsc_sink_caps->is_dp; 762 return true; 763 } 764 765 static uint32_t compute_bpp_x16_from_target_bandwidth( 766 const uint32_t bandwidth_in_kbps, 767 const struct dc_crtc_timing *timing, 768 const uint32_t num_slices_h, 769 const uint32_t bpp_increment_div, 770 const bool is_dp) 771 { 772 uint32_t overhead_in_kbps; 773 struct fixed31_32 effective_bandwidth_in_kbps; 774 struct fixed31_32 bpp_x16; 775 776 overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps( 777 timing, num_slices_h, is_dp); 778 effective_bandwidth_in_kbps = dc_fixpt_from_int(bandwidth_in_kbps); 779 effective_bandwidth_in_kbps = dc_fixpt_sub_int(effective_bandwidth_in_kbps, 780 overhead_in_kbps); 781 bpp_x16 = dc_fixpt_mul_int(effective_bandwidth_in_kbps, 10); 782 bpp_x16 = dc_fixpt_div_int(bpp_x16, timing->pix_clk_100hz); 783 bpp_x16 = dc_fixpt_from_int(dc_fixpt_floor(dc_fixpt_mul_int(bpp_x16, bpp_increment_div))); 784 bpp_x16 = dc_fixpt_div_int(bpp_x16, bpp_increment_div); 785 bpp_x16 = dc_fixpt_mul_int(bpp_x16, 16); 786 return dc_fixpt_floor(bpp_x16); 787 } 788 789 /* Decide DSC bandwidth range based on signal, timing, specs specific and input min and max 790 * requirements. 791 * The range output includes decided min/max target bpp, the respective bandwidth requirements 792 * and native timing bandwidth requirement when DSC is not used. 793 */ 794 static bool decide_dsc_bandwidth_range( 795 const uint32_t min_bpp_x16, 796 const uint32_t max_bpp_x16, 797 const uint32_t num_slices_h, 798 const struct dsc_enc_caps *dsc_caps, 799 const struct dc_crtc_timing *timing, 800 const enum dc_link_encoding_format link_encoding, 801 struct dc_dsc_bw_range *range) 802 { 803 uint32_t preferred_bpp_x16 = timing->dsc_fixed_bits_per_pixel_x16; 804 805 memset(range, 0, sizeof(*range)); 806 807 /* apply signal, timing, specs and explicitly specified DSC range requirements */ 808 if (preferred_bpp_x16) { 809 if (preferred_bpp_x16 <= max_bpp_x16 && 810 preferred_bpp_x16 >= min_bpp_x16) { 811 range->max_target_bpp_x16 = preferred_bpp_x16; 812 range->min_target_bpp_x16 = preferred_bpp_x16; 813 } 814 } 815 /* TODO - make this value generic to all signal types */ 816 else if (dsc_caps->edp_sink_max_bits_per_pixel) { 817 /* apply max bpp limitation from edp sink */ 818 range->max_target_bpp_x16 = MIN(dsc_caps->edp_sink_max_bits_per_pixel, 819 max_bpp_x16); 820 range->min_target_bpp_x16 = min_bpp_x16; 821 } 822 else { 823 range->max_target_bpp_x16 = max_bpp_x16; 824 range->min_target_bpp_x16 = min_bpp_x16; 825 } 826 827 /* populate output structure */ 828 if (range->max_target_bpp_x16 >= range->min_target_bpp_x16 && range->min_target_bpp_x16 > 0) { 829 /* native stream bandwidth */ 830 range->stream_kbps = dc_bandwidth_in_kbps_from_timing(timing, link_encoding); 831 832 /* max dsc target bpp */ 833 range->max_kbps = dc_dsc_stream_bandwidth_in_kbps(timing, 834 range->max_target_bpp_x16, num_slices_h, dsc_caps->is_dp); 835 836 /* min dsc target bpp */ 837 range->min_kbps = dc_dsc_stream_bandwidth_in_kbps(timing, 838 range->min_target_bpp_x16, num_slices_h, dsc_caps->is_dp); 839 } 840 841 return range->max_kbps >= range->min_kbps && range->min_kbps > 0; 842 } 843 844 /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy. 845 * 846 * Returns: 847 * - 'true' if target bpp is decided 848 * - 'false' if target bpp cannot be decided (e.g. cannot fit even with min DSC bpp), 849 */ 850 static bool decide_dsc_target_bpp_x16( 851 const struct dc_dsc_policy *policy, 852 const struct dc_dsc_config_options *options, 853 const struct dsc_enc_caps *dsc_common_caps, 854 const int target_bandwidth_kbps, 855 const struct dc_crtc_timing *timing, 856 const int num_slices_h, 857 const enum dc_link_encoding_format link_encoding, 858 int *target_bpp_x16) 859 { 860 struct dc_dsc_bw_range range; 861 862 *target_bpp_x16 = 0; 863 864 if (decide_dsc_bandwidth_range(policy->min_target_bpp * 16, policy->max_target_bpp * 16, 865 num_slices_h, dsc_common_caps, timing, link_encoding, &range)) { 866 if (target_bandwidth_kbps >= range.stream_kbps) { 867 if (policy->enable_dsc_when_not_needed || options->force_dsc_when_not_needed) 868 /* enable max bpp even dsc is not needed */ 869 *target_bpp_x16 = range.max_target_bpp_x16; 870 } else if (target_bandwidth_kbps >= range.max_kbps) { 871 /* use max target bpp allowed */ 872 *target_bpp_x16 = range.max_target_bpp_x16; 873 } else if (target_bandwidth_kbps >= range.min_kbps) { 874 /* use target bpp that can take entire target bandwidth */ 875 *target_bpp_x16 = compute_bpp_x16_from_target_bandwidth( 876 target_bandwidth_kbps, timing, num_slices_h, 877 dsc_common_caps->bpp_increment_div, 878 dsc_common_caps->is_dp); 879 } 880 } 881 882 return *target_bpp_x16 != 0; 883 } 884 885 #define MIN_AVAILABLE_SLICES_SIZE 6 886 887 static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps, int *available_slices) 888 { 889 int idx = 0; 890 891 if (slice_caps.bits.NUM_SLICES_1) 892 available_slices[idx++] = 1; 893 894 if (slice_caps.bits.NUM_SLICES_2) 895 available_slices[idx++] = 2; 896 897 if (slice_caps.bits.NUM_SLICES_4) 898 available_slices[idx++] = 4; 899 900 if (slice_caps.bits.NUM_SLICES_8) 901 available_slices[idx++] = 8; 902 903 if (slice_caps.bits.NUM_SLICES_12) 904 available_slices[idx++] = 12; 905 906 if (slice_caps.bits.NUM_SLICES_16) 907 available_slices[idx++] = 16; 908 909 return idx; 910 } 911 912 913 static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps) 914 { 915 int max_slices = 0; 916 int available_slices[MIN_AVAILABLE_SLICES_SIZE]; 917 int end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]); 918 919 if (end_idx > 0) 920 max_slices = available_slices[end_idx - 1]; 921 922 return max_slices; 923 } 924 925 926 // Increment slice number in available slice numbers stops if possible, or just increment if not 927 static int inc_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices) 928 { 929 // Get next bigger num slices available in common caps 930 int available_slices[MIN_AVAILABLE_SLICES_SIZE]; 931 int end_idx; 932 int i; 933 int new_num_slices = num_slices; 934 935 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]); 936 if (end_idx == 0) { 937 // No available slices found 938 new_num_slices++; 939 return new_num_slices; 940 } 941 942 // Numbers of slices found - get the next bigger number 943 for (i = 0; i < end_idx; i++) { 944 if (new_num_slices < available_slices[i]) { 945 new_num_slices = available_slices[i]; 946 break; 947 } 948 } 949 950 if (new_num_slices == num_slices) // No bigger number of slices found 951 new_num_slices++; 952 953 return new_num_slices; 954 } 955 956 957 // Decrement slice number in available slice numbers stops if possible, or just decrement if not. Stop at zero. 958 static int dec_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices) 959 { 960 // Get next bigger num slices available in common caps 961 int available_slices[MIN_AVAILABLE_SLICES_SIZE]; 962 int end_idx; 963 int i; 964 int new_num_slices = num_slices; 965 966 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]); 967 if (end_idx == 0 && new_num_slices > 0) { 968 // No numbers of slices found 969 new_num_slices++; 970 return new_num_slices; 971 } 972 973 // Numbers of slices found - get the next smaller number 974 for (i = end_idx - 1; i >= 0; i--) { 975 if (new_num_slices > available_slices[i]) { 976 new_num_slices = available_slices[i]; 977 break; 978 } 979 } 980 981 if (new_num_slices == num_slices) { 982 // No smaller number of slices found 983 new_num_slices--; 984 if (new_num_slices < 0) 985 new_num_slices = 0; 986 } 987 988 return new_num_slices; 989 } 990 991 992 // Choose next bigger number of slices if the requested number of slices is not available 993 static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps, int num_slices) 994 { 995 // Get next bigger num slices available in common caps 996 int available_slices[MIN_AVAILABLE_SLICES_SIZE]; 997 int end_idx; 998 int i; 999 int new_num_slices = num_slices; 1000 1001 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]); 1002 if (end_idx == 0) { 1003 // No available slices found 1004 new_num_slices++; 1005 return new_num_slices; 1006 } 1007 1008 // Numbers of slices found - get the equal or next bigger number 1009 for (i = 0; i < end_idx; i++) { 1010 if (new_num_slices <= available_slices[i]) { 1011 new_num_slices = available_slices[i]; 1012 break; 1013 } 1014 } 1015 1016 return new_num_slices; 1017 } 1018 1019 1020 /* Attempts to set DSC configuration for the stream, applying DSC policy. 1021 * Returns 'true' if successful or 'false' if not. 1022 * 1023 * Parameters: 1024 * 1025 * dsc_sink_caps - DSC sink decoder capabilities (from DPCD) 1026 * 1027 * dsc_enc_caps - DSC encoder capabilities 1028 * 1029 * target_bandwidth_kbps - Target bandwidth to fit the stream into. 1030 * If 0, do not calculate target bpp. 1031 * 1032 * timing - The stream timing to fit into 'target_bandwidth_kbps' or apply 1033 * maximum compression to, if 'target_badwidth == 0' 1034 * 1035 * dsc_cfg - DSC configuration to use if it was possible to come up with 1036 * one for the given inputs. 1037 * The target bitrate after DSC can be calculated by multiplying 1038 * dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g. 1039 * 1040 * dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0); 1041 */ 1042 static bool setup_dsc_config( 1043 const struct dsc_dec_dpcd_caps *dsc_sink_caps, 1044 const struct dsc_enc_caps *dsc_enc_caps, 1045 int target_bandwidth_kbps, 1046 const struct dc_crtc_timing *timing, 1047 const struct dc_dsc_config_options *options, 1048 const enum dc_link_encoding_format link_encoding, 1049 int min_slices_h, 1050 struct dc_dsc_config *dsc_cfg) 1051 { 1052 struct dsc_enc_caps dsc_common_caps; 1053 int max_slices_h = 0; 1054 int num_slices_h = 0; 1055 int pic_width; 1056 int slice_width; 1057 int target_bpp; 1058 int sink_per_slice_throughput_mps; 1059 int branch_max_throughput_mps = 0; 1060 bool is_dsc_possible = false; 1061 int pic_height; 1062 int slice_height; 1063 struct dc_dsc_policy policy; 1064 1065 memset(dsc_cfg, 0, sizeof(struct dc_dsc_config)); 1066 1067 dc_dsc_get_policy_for_timing(timing, options->max_target_bpp_limit_override_x16, &policy, link_encoding); 1068 pic_width = timing->h_addressable + timing->h_border_left + timing->h_border_right; 1069 pic_height = timing->v_addressable + timing->v_border_top + timing->v_border_bottom; 1070 1071 if (!dsc_sink_caps->is_dsc_supported) 1072 goto done; 1073 1074 if (dsc_sink_caps->branch_max_line_width && dsc_sink_caps->branch_max_line_width < pic_width) 1075 goto done; 1076 1077 // Intersect decoder with encoder DSC caps and validate DSC settings 1078 is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, dsc_enc_caps, timing->pixel_encoding, &dsc_common_caps); 1079 if (!is_dsc_possible) 1080 goto done; 1081 1082 sink_per_slice_throughput_mps = 0; 1083 1084 // Validate available DSC settings against the mode timing 1085 1086 // Validate color format (and pick up the throughput values) 1087 dsc_cfg->ycbcr422_simple = false; 1088 switch (timing->pixel_encoding) { 1089 case PIXEL_ENCODING_RGB: 1090 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.RGB; 1091 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps; 1092 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps; 1093 break; 1094 case PIXEL_ENCODING_YCBCR444: 1095 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_444; 1096 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps; 1097 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps; 1098 break; 1099 case PIXEL_ENCODING_YCBCR422: 1100 if (policy.ycbcr422_simple) { 1101 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_SIMPLE_422; 1102 dsc_cfg->ycbcr422_simple = is_dsc_possible; 1103 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps; 1104 } else { 1105 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_422; 1106 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps; 1107 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps; 1108 } 1109 break; 1110 case PIXEL_ENCODING_YCBCR420: 1111 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_420; 1112 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps; 1113 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps; 1114 break; 1115 default: 1116 is_dsc_possible = false; 1117 } 1118 1119 // Validate branch's maximum throughput 1120 if (branch_max_throughput_mps && dsc_div_by_10_round_up(timing->pix_clk_100hz) > branch_max_throughput_mps * 1000) 1121 is_dsc_possible = false; 1122 1123 if (!is_dsc_possible) 1124 goto done; 1125 1126 // Color depth 1127 switch (timing->display_color_depth) { 1128 case COLOR_DEPTH_888: 1129 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_8_BPC; 1130 break; 1131 case COLOR_DEPTH_101010: 1132 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_10_BPC; 1133 break; 1134 case COLOR_DEPTH_121212: 1135 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_12_BPC; 1136 break; 1137 default: 1138 is_dsc_possible = false; 1139 } 1140 1141 if (!is_dsc_possible) 1142 goto done; 1143 1144 // Slice width (i.e. number of slices per line) 1145 max_slices_h = get_max_dsc_slices(dsc_common_caps.slice_caps); 1146 1147 while (max_slices_h > 0) { 1148 if (pic_width % max_slices_h == 0) 1149 break; 1150 1151 max_slices_h = dec_num_slices(dsc_common_caps.slice_caps, max_slices_h); 1152 } 1153 1154 is_dsc_possible = (dsc_common_caps.max_slice_width > 0); 1155 if (!is_dsc_possible) 1156 goto done; 1157 1158 /* increase minimum slice count to meet sink slice width limitations */ 1159 min_slices_h = dc_fixpt_ceil(dc_fixpt_max( 1160 dc_fixpt_div_int(dc_fixpt_from_int(pic_width), dsc_common_caps.max_slice_width), // sink min 1161 dc_fixpt_from_int(min_slices_h))); // source min 1162 1163 min_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, min_slices_h); 1164 1165 /* increase minimum slice count to meet sink throughput limitations */ 1166 while (min_slices_h <= max_slices_h) { 1167 int pix_clk_per_slice_khz = dsc_div_by_10_round_up(timing->pix_clk_100hz) / min_slices_h; 1168 if (pix_clk_per_slice_khz <= sink_per_slice_throughput_mps * 1000) 1169 break; 1170 1171 min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h); 1172 } 1173 1174 /* increase minimum slice count to meet divisibility requirements */ 1175 while (pic_width % min_slices_h != 0 && min_slices_h <= max_slices_h) { 1176 min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h); 1177 } 1178 1179 is_dsc_possible = (min_slices_h <= max_slices_h) && max_slices_h != 0; 1180 if (!is_dsc_possible) 1181 goto done; 1182 1183 if (policy.use_min_slices_h) { 1184 if (min_slices_h > 0) 1185 num_slices_h = min_slices_h; 1186 else if (max_slices_h > 0) { // Fall back to max slices if min slices is not working out 1187 if (policy.max_slices_h) 1188 num_slices_h = min(policy.max_slices_h, max_slices_h); 1189 else 1190 num_slices_h = max_slices_h; 1191 } else 1192 is_dsc_possible = false; 1193 } else { 1194 if (max_slices_h > 0) { 1195 if (policy.max_slices_h) 1196 num_slices_h = min(policy.max_slices_h, max_slices_h); 1197 else 1198 num_slices_h = max_slices_h; 1199 } else if (min_slices_h > 0) // Fall back to min slices if max slices is not possible 1200 num_slices_h = min_slices_h; 1201 else 1202 is_dsc_possible = false; 1203 } 1204 // When we force ODM, num dsc h slices must be divisible by num odm h slices 1205 switch (options->dsc_force_odm_hslice_override) { 1206 case 0: 1207 case 1: 1208 break; 1209 case 2: 1210 if (num_slices_h < 2) 1211 num_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, 2); 1212 break; 1213 case 3: 1214 if (dsc_common_caps.slice_caps.bits.NUM_SLICES_12) 1215 num_slices_h = 12; 1216 else 1217 num_slices_h = 0; 1218 break; 1219 case 4: 1220 if (num_slices_h < 4) 1221 num_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, 4); 1222 break; 1223 default: 1224 break; 1225 } 1226 if (num_slices_h == 0) 1227 is_dsc_possible = false; 1228 if (!is_dsc_possible) 1229 goto done; 1230 1231 dsc_cfg->num_slices_h = num_slices_h; 1232 slice_width = pic_width / num_slices_h; 1233 1234 is_dsc_possible = slice_width <= dsc_common_caps.max_slice_width; 1235 if (!is_dsc_possible) 1236 goto done; 1237 1238 // Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by. 1239 // For 4:2:0 make sure the slice height is divisible by 2 as well. 1240 if (options->dsc_min_slice_height_override == 0) 1241 slice_height = min(policy.min_slice_height, pic_height); 1242 else 1243 slice_height = min((int)(options->dsc_min_slice_height_override), pic_height); 1244 1245 while (slice_height < pic_height && (pic_height % slice_height != 0 || 1246 slice_height % options->slice_height_granularity != 0 || 1247 (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 && slice_height % 2 != 0))) 1248 slice_height++; 1249 1250 if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) // For the case when pic_height < dsc_policy.min_sice_height 1251 is_dsc_possible = (slice_height % 2 == 0); 1252 1253 if (!is_dsc_possible) 1254 goto done; 1255 1256 if (slice_height > 0) { 1257 dsc_cfg->num_slices_v = pic_height / slice_height; 1258 } else { 1259 is_dsc_possible = false; 1260 goto done; 1261 } 1262 1263 if (target_bandwidth_kbps > 0) { 1264 is_dsc_possible = decide_dsc_target_bpp_x16( 1265 &policy, 1266 options, 1267 &dsc_common_caps, 1268 target_bandwidth_kbps, 1269 timing, 1270 num_slices_h, 1271 link_encoding, 1272 &target_bpp); 1273 dsc_cfg->bits_per_pixel = target_bpp; 1274 } 1275 if (!is_dsc_possible) 1276 goto done; 1277 1278 /* Fill out the rest of DSC settings */ 1279 dsc_cfg->block_pred_enable = dsc_common_caps.is_block_pred_supported; 1280 dsc_cfg->linebuf_depth = dsc_common_caps.lb_bit_depth; 1281 dsc_cfg->version_minor = (dsc_common_caps.dsc_version & 0xf0) >> 4; 1282 dsc_cfg->is_dp = dsc_sink_caps->is_dp; 1283 1284 done: 1285 if (!is_dsc_possible) 1286 memset(dsc_cfg, 0, sizeof(struct dc_dsc_config)); 1287 1288 return is_dsc_possible; 1289 } 1290 1291 bool dc_dsc_compute_config( 1292 const struct display_stream_compressor *dsc, 1293 const struct dsc_dec_dpcd_caps *dsc_sink_caps, 1294 const struct dc_dsc_config_options *options, 1295 uint32_t target_bandwidth_kbps, 1296 const struct dc_crtc_timing *timing, 1297 const enum dc_link_encoding_format link_encoding, 1298 struct dc_dsc_config *dsc_cfg) 1299 { 1300 bool is_dsc_possible = false; 1301 struct dsc_enc_caps dsc_enc_caps; 1302 unsigned int min_dsc_slice_count; 1303 get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz); 1304 1305 min_dsc_slice_count = get_min_dsc_slice_count_for_odm(dsc, &dsc_enc_caps, timing); 1306 1307 is_dsc_possible = setup_dsc_config(dsc_sink_caps, 1308 &dsc_enc_caps, 1309 target_bandwidth_kbps, 1310 timing, 1311 options, 1312 link_encoding, 1313 min_dsc_slice_count, 1314 dsc_cfg); 1315 return is_dsc_possible; 1316 } 1317 1318 uint32_t dc_dsc_stream_bandwidth_in_kbps(const struct dc_crtc_timing *timing, 1319 uint32_t bpp_x16, uint32_t num_slices_h, bool is_dp) 1320 { 1321 uint32_t overhead_in_kbps; 1322 struct fixed31_32 bpp; 1323 struct fixed31_32 actual_bandwidth_in_kbps; 1324 1325 overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps( 1326 timing, num_slices_h, is_dp); 1327 bpp = dc_fixpt_from_fraction(bpp_x16, 16); 1328 actual_bandwidth_in_kbps = dc_fixpt_from_fraction(timing->pix_clk_100hz, 10); 1329 actual_bandwidth_in_kbps = dc_fixpt_mul(actual_bandwidth_in_kbps, bpp); 1330 actual_bandwidth_in_kbps = dc_fixpt_add_int(actual_bandwidth_in_kbps, overhead_in_kbps); 1331 return dc_fixpt_ceil(actual_bandwidth_in_kbps); 1332 } 1333 1334 uint32_t dc_dsc_stream_bandwidth_overhead_in_kbps( 1335 const struct dc_crtc_timing *timing, 1336 const int num_slices_h, 1337 const bool is_dp) 1338 { 1339 struct fixed31_32 max_dsc_overhead; 1340 struct fixed31_32 refresh_rate; 1341 1342 if (dsc_policy_disable_dsc_stream_overhead || !is_dp) 1343 return 0; 1344 1345 /* use target bpp that can take entire target bandwidth */ 1346 refresh_rate = dc_fixpt_from_int(timing->pix_clk_100hz); 1347 refresh_rate = dc_fixpt_div_int(refresh_rate, timing->h_total); 1348 refresh_rate = dc_fixpt_div_int(refresh_rate, timing->v_total); 1349 refresh_rate = dc_fixpt_mul_int(refresh_rate, 100); 1350 1351 max_dsc_overhead = dc_fixpt_from_int(num_slices_h); 1352 max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, timing->v_total); 1353 max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, 256); 1354 max_dsc_overhead = dc_fixpt_div_int(max_dsc_overhead, 1000); 1355 max_dsc_overhead = dc_fixpt_mul(max_dsc_overhead, refresh_rate); 1356 1357 return dc_fixpt_ceil(max_dsc_overhead); 1358 } 1359 1360 void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing *timing, 1361 uint32_t max_target_bpp_limit_override_x16, 1362 struct dc_dsc_policy *policy, 1363 const enum dc_link_encoding_format link_encoding) 1364 { 1365 uint32_t bpc = 0; 1366 1367 policy->min_target_bpp = 0; 1368 policy->max_target_bpp = 0; 1369 1370 /* DSC Policy: Use minimum number of slices that fits the pixel clock */ 1371 policy->use_min_slices_h = true; 1372 1373 /* DSC Policy: Use max available slices 1374 * (in our case 4 for or 8, depending on the mode) 1375 */ 1376 policy->max_slices_h = 0; 1377 1378 /* DSC Policy: Use slice height recommended 1379 * by VESA DSC Spreadsheet user guide 1380 */ 1381 policy->min_slice_height = 108; 1382 1383 /* DSC Policy: follow DP specs with an internal upper limit to 16 bpp 1384 * for better interoperability 1385 */ 1386 switch (timing->display_color_depth) { 1387 case COLOR_DEPTH_888: 1388 bpc = 8; 1389 break; 1390 case COLOR_DEPTH_101010: 1391 bpc = 10; 1392 break; 1393 case COLOR_DEPTH_121212: 1394 bpc = 12; 1395 break; 1396 default: 1397 return; 1398 } 1399 switch (timing->pixel_encoding) { 1400 case PIXEL_ENCODING_RGB: 1401 case PIXEL_ENCODING_YCBCR444: 1402 case PIXEL_ENCODING_YCBCR422: /* assume no YCbCr422 native support */ 1403 /* DP specs limits to 8 */ 1404 policy->min_target_bpp = 8; 1405 /* DP specs limits to 3 x bpc */ 1406 policy->max_target_bpp = 3 * bpc; 1407 policy->ycbcr422_simple = true; 1408 break; 1409 case PIXEL_ENCODING_YCBCR420: 1410 /* DP specs limits to 6 */ 1411 policy->min_target_bpp = 6; 1412 /* DP specs limits to 1.5 x bpc assume bpc is an even number */ 1413 policy->max_target_bpp = bpc * 3 / 2; 1414 break; 1415 default: 1416 return; 1417 } 1418 1419 /* internal upper limit, default 16 bpp */ 1420 if (policy->max_target_bpp > dsc_policy_max_target_bpp_limit) 1421 policy->max_target_bpp = dsc_policy_max_target_bpp_limit; 1422 1423 /* apply override */ 1424 if (max_target_bpp_limit_override_x16 && policy->max_target_bpp > max_target_bpp_limit_override_x16 / 16) 1425 policy->max_target_bpp = max_target_bpp_limit_override_x16 / 16; 1426 1427 /* enable DSC when not needed, default false */ 1428 policy->enable_dsc_when_not_needed = dsc_policy_enable_dsc_when_not_needed; 1429 } 1430 1431 void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit) 1432 { 1433 dsc_policy_max_target_bpp_limit = limit; 1434 } 1435 1436 void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable) 1437 { 1438 dsc_policy_enable_dsc_when_not_needed = enable; 1439 } 1440 1441 void dc_dsc_policy_set_disable_dsc_stream_overhead(bool disable) 1442 { 1443 dsc_policy_disable_dsc_stream_overhead = disable; 1444 } 1445 1446 void dc_set_disable_128b_132b_stream_overhead(bool disable) 1447 { 1448 disable_128b_132b_stream_overhead = disable; 1449 } 1450 1451 void dc_dsc_get_default_config_option(const struct dc *dc, struct dc_dsc_config_options *options) 1452 { 1453 options->dsc_min_slice_height_override = dc->debug.dsc_min_slice_height_override; 1454 options->dsc_force_odm_hslice_override = dc->debug.force_odm_combine; 1455 options->max_target_bpp_limit_override_x16 = 0; 1456 options->slice_height_granularity = 1; 1457 options->force_dsc_when_not_needed = false; 1458 } 1459