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