WIP FPC-III support
[linux/fpc-iii.git] / drivers / gpu / drm / amd / display / dc / dsc / dc_dsc.c
blobc62d0eddc9c61164afb68b12099155b1105ff9a5
1 /*
2 * Copyright 2019 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Author: AMD
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"
32 /* This module's internal functions */
34 /* default DSC policy target bitrate limit is 16bpp */
35 static uint32_t dsc_policy_max_target_bpp_limit = 16;
37 /* default DSC policy enables DSC only when needed */
38 static bool dsc_policy_enable_dsc_when_not_needed;
40 static uint32_t dc_dsc_bandwidth_in_kbps_from_timing(
41 const struct dc_crtc_timing *timing)
43 uint32_t bits_per_channel = 0;
44 uint32_t kbps;
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;
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;
75 ASSERT(bits_per_channel != 0);
77 kbps = timing->pix_clk_100hz / 10;
78 kbps *= bits_per_channel;
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;
89 return kbps;
93 static bool dsc_buff_block_size_from_dpcd(int dpcd_buff_block_size, int *buff_block_size)
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;
115 return true;
119 static bool dsc_line_buff_depth_from_dpcd(int dpcd_line_buff_bit_depth, int *line_buff_bit_depth)
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;
130 return true;
134 static bool dsc_throughput_from_dpcd(int dpcd_throughput, int *throughput)
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;
191 return true;
195 static bool dsc_bpp_increment_div_from_dpcd(uint8_t bpp_increment_dpcd, uint32_t *bpp_increment_div)
197 // Mask bpp increment dpcd field to avoid reading other fields
198 bpp_increment_dpcd &= 0x7;
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;
222 return true;
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)
230 // This is a static HW query, so we can use any DSC
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;
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.
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)
250 int32_t max_slices;
251 int32_t total_sink_throughput;
253 memset(dsc_common_caps, 0, sizeof(struct dsc_enc_caps));
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;
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;
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;
270 dsc_common_caps->is_block_pred_supported = dsc_sink_caps->is_block_pred_supported && dsc_enc_caps->is_block_pred_supported;
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;
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;
280 max_slices = 0;
281 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_1)
282 max_slices = 1;
284 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_2)
285 max_slices = 2;
287 if (dsc_common_caps->slice_caps.bits.NUM_SLICES_4)
288 max_slices = 4;
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;
294 dsc_common_caps->max_total_throughput_mps = min(total_sink_throughput, dsc_enc_caps->max_total_throughput_mps);
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;
300 dsc_common_caps->bpp_increment_div = min(dsc_sink_caps->bpp_increment_div, dsc_enc_caps->bpp_increment_div);
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);
306 return true;
309 static inline uint32_t dsc_div_by_10_round_up(uint32_t value)
311 return (value + 9) / 10;
314 /* Get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range, and timing's pixel clock
315 * and uncompressed bandwidth.
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)
324 /* native stream bandwidth */
325 range->stream_kbps = dc_dsc_bandwidth_in_kbps_from_timing(timing);
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);
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;
347 /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy.
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.
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)
361 bool should_use_dsc = false;
362 struct dc_dsc_bw_range range;
364 memset(&range, 0, sizeof(range));
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 (target_bandwidth_kbps >= range.max_kbps) {
373 /* use max target bpp allowed */
374 *target_bpp_x16 = range.max_target_bpp_x16;
375 should_use_dsc = true;
376 } else if (target_bandwidth_kbps >= range.min_kbps) {
377 /* use target bpp that can take entire target bandwidth */
378 *target_bpp_x16 = calc_dsc_bpp_x16(target_bandwidth_kbps, timing->pix_clk_100hz, dsc_common_caps->bpp_increment_div);
379 should_use_dsc = true;
380 } else {
381 /* not enough bandwidth to fulfill minimum requirement */
382 *target_bpp_x16 = 0;
383 should_use_dsc = false;
386 return should_use_dsc;
389 #define MIN_AVAILABLE_SLICES_SIZE 4
391 static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps, int *available_slices)
393 int idx = 0;
395 memset(available_slices, -1, MIN_AVAILABLE_SLICES_SIZE);
397 if (slice_caps.bits.NUM_SLICES_1)
398 available_slices[idx++] = 1;
400 if (slice_caps.bits.NUM_SLICES_2)
401 available_slices[idx++] = 2;
403 if (slice_caps.bits.NUM_SLICES_4)
404 available_slices[idx++] = 4;
406 if (slice_caps.bits.NUM_SLICES_8)
407 available_slices[idx++] = 8;
409 return idx;
413 static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps)
415 int max_slices = 0;
416 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
417 int end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
419 if (end_idx > 0)
420 max_slices = available_slices[end_idx - 1];
422 return max_slices;
426 // Increment sice number in available sice numbers stops if possible, or just increment if not
427 static int inc_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
429 // Get next bigger num slices available in common caps
430 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
431 int end_idx;
432 int i;
433 int new_num_slices = num_slices;
435 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
436 if (end_idx == 0) {
437 // No available slices found
438 new_num_slices++;
439 return new_num_slices;
442 // Numbers of slices found - get the next bigger number
443 for (i = 0; i < end_idx; i++) {
444 if (new_num_slices < available_slices[i]) {
445 new_num_slices = available_slices[i];
446 break;
450 if (new_num_slices == num_slices) // No biger number of slices found
451 new_num_slices++;
453 return new_num_slices;
457 // Decrement sice number in available sice numbers stops if possible, or just decrement if not. Stop at zero.
458 static int dec_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
460 // Get next bigger num slices available in common caps
461 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
462 int end_idx;
463 int i;
464 int new_num_slices = num_slices;
466 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
467 if (end_idx == 0 && new_num_slices > 0) {
468 // No numbers of slices found
469 new_num_slices++;
470 return new_num_slices;
473 // Numbers of slices found - get the next smaller number
474 for (i = end_idx - 1; i >= 0; i--) {
475 if (new_num_slices > available_slices[i]) {
476 new_num_slices = available_slices[i];
477 break;
481 if (new_num_slices == num_slices) {
482 // No smaller number of slices found
483 new_num_slices--;
484 if (new_num_slices < 0)
485 new_num_slices = 0;
488 return new_num_slices;
492 // Choose next bigger number of slices if the requested number of slices is not available
493 static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps, int num_slices)
495 // Get next bigger num slices available in common caps
496 int available_slices[MIN_AVAILABLE_SLICES_SIZE];
497 int end_idx;
498 int i;
499 int new_num_slices = num_slices;
501 end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
502 if (end_idx == 0) {
503 // No available slices found
504 new_num_slices++;
505 return new_num_slices;
508 // Numbers of slices found - get the equal or next bigger number
509 for (i = 0; i < end_idx; i++) {
510 if (new_num_slices <= available_slices[i]) {
511 new_num_slices = available_slices[i];
512 break;
516 return new_num_slices;
520 /* Attempts to set DSC configuration for the stream, applying DSC policy.
521 * Returns 'true' if successful or 'false' if not.
523 * Parameters:
525 * dsc_sink_caps - DSC sink decoder capabilities (from DPCD)
527 * dsc_enc_caps - DSC encoder capabilities
529 * target_bandwidth_kbps - Target bandwidth to fit the stream into.
530 * If 0, do not calculate target bpp.
532 * timing - The stream timing to fit into 'target_bandwidth_kbps' or apply
533 * maximum compression to, if 'target_badwidth == 0'
535 * dsc_cfg - DSC configuration to use if it was possible to come up with
536 * one for the given inputs.
537 * The target bitrate after DSC can be calculated by multiplying
538 * dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g.
540 * dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0);
542 static bool setup_dsc_config(
543 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
544 const struct dsc_enc_caps *dsc_enc_caps,
545 int target_bandwidth_kbps,
546 const struct dc_crtc_timing *timing,
547 int min_slice_height_override,
548 int max_dsc_target_bpp_limit_override,
549 struct dc_dsc_config *dsc_cfg)
551 struct dsc_enc_caps dsc_common_caps;
552 int max_slices_h;
553 int min_slices_h;
554 int num_slices_h;
555 int pic_width;
556 int slice_width;
557 int target_bpp;
558 int sink_per_slice_throughput_mps;
559 int branch_max_throughput_mps = 0;
560 bool is_dsc_possible = false;
561 int pic_height;
562 int slice_height;
563 struct dc_dsc_policy policy;
565 memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
567 dc_dsc_get_policy_for_timing(timing, max_dsc_target_bpp_limit_override, &policy);
568 pic_width = timing->h_addressable + timing->h_border_left + timing->h_border_right;
569 pic_height = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
571 if (!dsc_sink_caps->is_dsc_supported)
572 goto done;
574 if (dsc_sink_caps->branch_max_line_width && dsc_sink_caps->branch_max_line_width < pic_width)
575 goto done;
577 // Intersect decoder with encoder DSC caps and validate DSC settings
578 is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, dsc_enc_caps, timing->pixel_encoding, &dsc_common_caps);
579 if (!is_dsc_possible)
580 goto done;
582 if (target_bandwidth_kbps > 0) {
583 is_dsc_possible = decide_dsc_target_bpp_x16(
584 &policy,
585 &dsc_common_caps,
586 target_bandwidth_kbps,
587 timing,
588 &target_bpp);
589 dsc_cfg->bits_per_pixel = target_bpp;
591 if (!is_dsc_possible)
592 goto done;
594 sink_per_slice_throughput_mps = 0;
596 // Validate available DSC settings against the mode timing
598 // Validate color format (and pick up the throughput values)
599 dsc_cfg->ycbcr422_simple = false;
600 switch (timing->pixel_encoding) {
601 case PIXEL_ENCODING_RGB:
602 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.RGB;
603 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
604 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
605 break;
606 case PIXEL_ENCODING_YCBCR444:
607 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_444;
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_YCBCR422:
612 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_422;
613 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
614 branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
615 if (!is_dsc_possible) {
616 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_SIMPLE_422;
617 dsc_cfg->ycbcr422_simple = is_dsc_possible;
618 sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
620 break;
621 case PIXEL_ENCODING_YCBCR420:
622 is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_420;
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 break;
626 default:
627 is_dsc_possible = false;
630 // Validate branch's maximum throughput
631 if (branch_max_throughput_mps && dsc_div_by_10_round_up(timing->pix_clk_100hz) > branch_max_throughput_mps * 1000)
632 is_dsc_possible = false;
634 if (!is_dsc_possible)
635 goto done;
637 // Color depth
638 switch (timing->display_color_depth) {
639 case COLOR_DEPTH_888:
640 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_8_BPC;
641 break;
642 case COLOR_DEPTH_101010:
643 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_10_BPC;
644 break;
645 case COLOR_DEPTH_121212:
646 is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_12_BPC;
647 break;
648 default:
649 is_dsc_possible = false;
652 if (!is_dsc_possible)
653 goto done;
655 // Slice width (i.e. number of slices per line)
656 max_slices_h = get_max_dsc_slices(dsc_common_caps.slice_caps);
658 while (max_slices_h > 0) {
659 if (pic_width % max_slices_h == 0)
660 break;
662 max_slices_h = dec_num_slices(dsc_common_caps.slice_caps, max_slices_h);
665 is_dsc_possible = (dsc_common_caps.max_slice_width > 0);
666 if (!is_dsc_possible)
667 goto done;
669 min_slices_h = pic_width / dsc_common_caps.max_slice_width;
670 if (pic_width % dsc_common_caps.max_slice_width)
671 min_slices_h++;
673 min_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, min_slices_h);
675 while (min_slices_h <= max_slices_h) {
676 int pix_clk_per_slice_khz = dsc_div_by_10_round_up(timing->pix_clk_100hz) / min_slices_h;
677 if (pix_clk_per_slice_khz <= sink_per_slice_throughput_mps * 1000)
678 break;
680 min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
683 if (pic_width % min_slices_h != 0)
684 min_slices_h = 0; // DSC TODO: Maybe try increasing the number of slices first?
686 is_dsc_possible = (min_slices_h <= max_slices_h);
687 if (!is_dsc_possible)
688 goto done;
690 if (policy.use_min_slices_h) {
691 if (min_slices_h > 0)
692 num_slices_h = min_slices_h;
693 else if (max_slices_h > 0) { // Fall back to max slices if min slices is not working out
694 if (policy.max_slices_h)
695 num_slices_h = min(policy.max_slices_h, max_slices_h);
696 else
697 num_slices_h = max_slices_h;
698 } else
699 is_dsc_possible = false;
700 } else {
701 if (max_slices_h > 0) {
702 if (policy.max_slices_h)
703 num_slices_h = min(policy.max_slices_h, max_slices_h);
704 else
705 num_slices_h = max_slices_h;
706 } else if (min_slices_h > 0) // Fall back to min slices if max slices is not possible
707 num_slices_h = min_slices_h;
708 else
709 is_dsc_possible = false;
712 if (!is_dsc_possible)
713 goto done;
715 dsc_cfg->num_slices_h = num_slices_h;
716 slice_width = pic_width / num_slices_h;
718 is_dsc_possible = slice_width <= dsc_common_caps.max_slice_width;
719 if (!is_dsc_possible)
720 goto done;
722 // Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by.
723 // For 4:2:0 make sure the slice height is divisible by 2 as well.
724 if (min_slice_height_override == 0)
725 slice_height = min(policy.min_slice_height, pic_height);
726 else
727 slice_height = min(min_slice_height_override, pic_height);
729 while (slice_height < pic_height && (pic_height % slice_height != 0 ||
730 (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 && slice_height % 2 != 0)))
731 slice_height++;
733 if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) // For the case when pic_height < dsc_policy.min_sice_height
734 is_dsc_possible = (slice_height % 2 == 0);
736 if (!is_dsc_possible)
737 goto done;
739 dsc_cfg->num_slices_v = pic_height/slice_height;
741 // Final decission: can we do DSC or not?
742 if (is_dsc_possible) {
743 // Fill out the rest of DSC settings
744 dsc_cfg->block_pred_enable = dsc_common_caps.is_block_pred_supported;
745 dsc_cfg->linebuf_depth = dsc_common_caps.lb_bit_depth;
746 dsc_cfg->version_minor = (dsc_common_caps.dsc_version & 0xf0) >> 4;
749 done:
750 if (!is_dsc_possible)
751 memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
753 return is_dsc_possible;
756 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)
758 if (!dpcd_dsc_basic_data)
759 return false;
761 dsc_sink_caps->is_dsc_supported = (dpcd_dsc_basic_data[DP_DSC_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_DECOMPRESSION_IS_SUPPORTED) != 0;
762 if (!dsc_sink_caps->is_dsc_supported)
763 return false;
765 dsc_sink_caps->dsc_version = dpcd_dsc_basic_data[DP_DSC_REV - DP_DSC_SUPPORT];
768 int buff_block_size;
769 int buff_size;
771 if (!dsc_buff_block_size_from_dpcd(dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT], &buff_block_size))
772 return false;
774 buff_size = dpcd_dsc_basic_data[DP_DSC_RC_BUF_SIZE - DP_DSC_SUPPORT] + 1;
775 dsc_sink_caps->rc_buffer_size = buff_size * buff_block_size;
778 dsc_sink_caps->slice_caps1.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
779 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))
780 return false;
782 dsc_sink_caps->is_block_pred_supported =
783 (dpcd_dsc_basic_data[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_BLK_PREDICTION_IS_SUPPORTED) != 0;
785 dsc_sink_caps->edp_max_bits_per_pixel =
786 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_LOW - DP_DSC_SUPPORT] |
787 dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] << 8;
789 dsc_sink_caps->color_formats.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT];
790 dsc_sink_caps->color_depth.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
793 int dpcd_throughput = dpcd_dsc_basic_data[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];
795 if (!dsc_throughput_from_dpcd(dpcd_throughput & DP_DSC_THROUGHPUT_MODE_0_MASK, &dsc_sink_caps->throughput_mode_0_mps))
796 return false;
798 dpcd_throughput = (dpcd_throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >> DP_DSC_THROUGHPUT_MODE_1_SHIFT;
799 if (!dsc_throughput_from_dpcd(dpcd_throughput, &dsc_sink_caps->throughput_mode_1_mps))
800 return false;
803 dsc_sink_caps->max_slice_width = dpcd_dsc_basic_data[DP_DSC_MAX_SLICE_WIDTH - DP_DSC_SUPPORT] * 320;
804 dsc_sink_caps->slice_caps2.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
806 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))
807 return false;
809 if (dc->debug.dsc_bpp_increment_div) {
810 /* dsc_bpp_increment_div should onl be 1, 2, 4, 8 or 16, but rather than rejecting invalid values,
811 * we'll accept all and get it into range. This also makes the above check against 0 redundant,
812 * but that one stresses out the override will be only used if it's not 0.
814 if (dc->debug.dsc_bpp_increment_div >= 1)
815 dsc_sink_caps->bpp_increment_div = 1;
816 if (dc->debug.dsc_bpp_increment_div >= 2)
817 dsc_sink_caps->bpp_increment_div = 2;
818 if (dc->debug.dsc_bpp_increment_div >= 4)
819 dsc_sink_caps->bpp_increment_div = 4;
820 if (dc->debug.dsc_bpp_increment_div >= 8)
821 dsc_sink_caps->bpp_increment_div = 8;
822 if (dc->debug.dsc_bpp_increment_div >= 16)
823 dsc_sink_caps->bpp_increment_div = 16;
826 /* Extended caps */
827 if (dpcd_dsc_branch_decoder_caps == NULL) { // branch decoder DPCD DSC data can be null for non branch device
828 dsc_sink_caps->branch_overall_throughput_0_mps = 0;
829 dsc_sink_caps->branch_overall_throughput_1_mps = 0;
830 dsc_sink_caps->branch_max_line_width = 0;
831 return true;
834 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];
835 if (dsc_sink_caps->branch_overall_throughput_0_mps == 0)
836 dsc_sink_caps->branch_overall_throughput_0_mps = 0;
837 else if (dsc_sink_caps->branch_overall_throughput_0_mps == 1)
838 dsc_sink_caps->branch_overall_throughput_0_mps = 680;
839 else {
840 dsc_sink_caps->branch_overall_throughput_0_mps *= 50;
841 dsc_sink_caps->branch_overall_throughput_0_mps += 600;
844 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];
845 if (dsc_sink_caps->branch_overall_throughput_1_mps == 0)
846 dsc_sink_caps->branch_overall_throughput_1_mps = 0;
847 else if (dsc_sink_caps->branch_overall_throughput_1_mps == 1)
848 dsc_sink_caps->branch_overall_throughput_1_mps = 680;
849 else {
850 dsc_sink_caps->branch_overall_throughput_1_mps *= 50;
851 dsc_sink_caps->branch_overall_throughput_1_mps += 600;
854 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;
855 ASSERT(dsc_sink_caps->branch_max_line_width == 0 || dsc_sink_caps->branch_max_line_width >= 5120);
857 return true;
861 /* If DSC is possbile, get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range and
862 * timing's pixel clock and uncompressed bandwidth.
863 * If DSC is not possible, leave '*range' untouched.
865 bool dc_dsc_compute_bandwidth_range(
866 const struct display_stream_compressor *dsc,
867 uint32_t dsc_min_slice_height_override,
868 uint32_t min_bpp,
869 uint32_t max_bpp,
870 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
871 const struct dc_crtc_timing *timing,
872 struct dc_dsc_bw_range *range)
874 bool is_dsc_possible = false;
875 struct dsc_enc_caps dsc_enc_caps;
876 struct dsc_enc_caps dsc_common_caps;
877 struct dc_dsc_config config;
879 get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
881 is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, &dsc_enc_caps,
882 timing->pixel_encoding, &dsc_common_caps);
884 if (is_dsc_possible)
885 is_dsc_possible = setup_dsc_config(dsc_sink_caps, &dsc_enc_caps, 0, timing,
886 dsc_min_slice_height_override, max_bpp, &config);
888 if (is_dsc_possible)
889 get_dsc_bandwidth_range(min_bpp, max_bpp, &dsc_common_caps, timing, range);
891 return is_dsc_possible;
894 bool dc_dsc_compute_config(
895 const struct display_stream_compressor *dsc,
896 const struct dsc_dec_dpcd_caps *dsc_sink_caps,
897 uint32_t dsc_min_slice_height_override,
898 uint32_t max_target_bpp_limit_override,
899 uint32_t target_bandwidth_kbps,
900 const struct dc_crtc_timing *timing,
901 struct dc_dsc_config *dsc_cfg)
903 bool is_dsc_possible = false;
904 struct dsc_enc_caps dsc_enc_caps;
906 get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
907 is_dsc_possible = setup_dsc_config(dsc_sink_caps,
908 &dsc_enc_caps,
909 target_bandwidth_kbps,
910 timing, dsc_min_slice_height_override,
911 max_target_bpp_limit_override, dsc_cfg);
912 return is_dsc_possible;
915 void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing *timing, uint32_t max_target_bpp_limit_override, struct dc_dsc_policy *policy)
917 uint32_t bpc = 0;
919 policy->min_target_bpp = 0;
920 policy->max_target_bpp = 0;
922 /* DSC Policy: Use minimum number of slices that fits the pixel clock */
923 policy->use_min_slices_h = true;
925 /* DSC Policy: Use max available slices
926 * (in our case 4 for or 8, depending on the mode)
928 policy->max_slices_h = 0;
930 /* DSC Policy: Use slice height recommended
931 * by VESA DSC Spreadsheet user guide
933 policy->min_slice_height = 108;
935 /* DSC Policy: follow DP specs with an internal upper limit to 16 bpp
936 * for better interoperability
938 switch (timing->display_color_depth) {
939 case COLOR_DEPTH_888:
940 bpc = 8;
941 break;
942 case COLOR_DEPTH_101010:
943 bpc = 10;
944 break;
945 case COLOR_DEPTH_121212:
946 bpc = 12;
947 break;
948 default:
949 return;
951 switch (timing->pixel_encoding) {
952 case PIXEL_ENCODING_RGB:
953 case PIXEL_ENCODING_YCBCR444:
954 case PIXEL_ENCODING_YCBCR422: /* assume no YCbCr422 native support */
955 /* DP specs limits to 8 */
956 policy->min_target_bpp = 8;
957 /* DP specs limits to 3 x bpc */
958 policy->max_target_bpp = 3 * bpc;
959 break;
960 case PIXEL_ENCODING_YCBCR420:
961 /* DP specs limits to 6 */
962 policy->min_target_bpp = 6;
963 /* DP specs limits to 1.5 x bpc assume bpc is an even number */
964 policy->max_target_bpp = bpc * 3 / 2;
965 break;
966 default:
967 return;
970 /* internal upper limit, default 16 bpp */
971 if (policy->max_target_bpp > dsc_policy_max_target_bpp_limit)
972 policy->max_target_bpp = dsc_policy_max_target_bpp_limit;
974 /* apply override */
975 if (max_target_bpp_limit_override && policy->max_target_bpp > max_target_bpp_limit_override)
976 policy->max_target_bpp = max_target_bpp_limit_override;
978 /* enable DSC when not needed, default false */
979 if (dsc_policy_enable_dsc_when_not_needed)
980 policy->enable_dsc_when_not_needed = dsc_policy_enable_dsc_when_not_needed;
981 else
982 policy->enable_dsc_when_not_needed = false;
985 void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit)
987 dsc_policy_max_target_bpp_limit = limit;
990 void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable)
992 dsc_policy_enable_dsc_when_not_needed = enable;