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