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