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