xref: /linux/drivers/gpu/drm/amd/display/dc/dsc/dc_dsc.c (revision 335bbdf01d25517ae832ac1807fd8323c1f4f3b9)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Author: AMD
23  */
24 
25 #include <drm/display/drm_dp_helper.h>
26 #include <drm/display/drm_dsc_helper.h>
27 #include "dc_hw_types.h"
28 #include "dsc.h"
29 #include "dc.h"
30 #include "rc_calc.h"
31 #include "fixed31_32.h"
32 
33 /* This module's internal functions */
34 
35 /* default DSC policy target bitrate limit is 16bpp */
36 static uint32_t dsc_policy_max_target_bpp_limit = 16;
37 
38 /* default DSC policy enables DSC only when needed */
39 static bool dsc_policy_enable_dsc_when_not_needed;
40 
41 static bool dsc_policy_disable_dsc_stream_overhead;
42 
43 static bool disable_128b_132b_stream_overhead;
44 
45 #ifndef MAX
46 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
47 #endif
48 #ifndef MIN
49 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
50 #endif
51 
52 /* Need to account for padding due to pixel-to-symbol packing
53  * for uncompressed 128b/132b streams.
54  */
55 static uint32_t apply_128b_132b_stream_overhead(
56 	const struct dc_crtc_timing *timing, const uint32_t kbps)
57 {
58 	uint32_t total_kbps = kbps;
59 
60 	if (disable_128b_132b_stream_overhead)
61 		return kbps;
62 
63 	if (!timing->flags.DSC) {
64 		struct fixed31_32 bpp;
65 		struct fixed31_32 overhead_factor;
66 
67 		bpp = dc_fixpt_from_int(kbps);
68 		bpp = dc_fixpt_div_int(bpp, timing->pix_clk_100hz / 10);
69 
70 		/* Symbols_per_HActive = HActive * bpp / (4 lanes * 32-bit symbol size)
71 		 * Overhead_factor = ceil(Symbols_per_HActive) / Symbols_per_HActive
72 		 */
73 		overhead_factor = dc_fixpt_from_int(timing->h_addressable);
74 		overhead_factor = dc_fixpt_mul(overhead_factor, bpp);
75 		overhead_factor = dc_fixpt_div_int(overhead_factor, 128);
76 		overhead_factor = dc_fixpt_div(
77 			dc_fixpt_from_int(dc_fixpt_ceil(overhead_factor)),
78 			overhead_factor);
79 
80 		total_kbps = dc_fixpt_ceil(
81 			dc_fixpt_mul_int(overhead_factor, total_kbps));
82 	}
83 
84 	return total_kbps;
85 }
86 
87 uint32_t dc_bandwidth_in_kbps_from_timing(
88 	const struct dc_crtc_timing *timing,
89 	const enum dc_link_encoding_format link_encoding)
90 {
91 	uint32_t bits_per_channel = 0;
92 	uint32_t kbps;
93 
94 	if (timing->flags.DSC)
95 		return dc_dsc_stream_bandwidth_in_kbps(timing,
96 				timing->dsc_cfg.bits_per_pixel,
97 				timing->dsc_cfg.num_slices_h,
98 				timing->dsc_cfg.is_dp);
99 
100 	switch (timing->display_color_depth) {
101 	case COLOR_DEPTH_666:
102 		bits_per_channel = 6;
103 		break;
104 	case COLOR_DEPTH_888:
105 		bits_per_channel = 8;
106 		break;
107 	case COLOR_DEPTH_101010:
108 		bits_per_channel = 10;
109 		break;
110 	case COLOR_DEPTH_121212:
111 		bits_per_channel = 12;
112 		break;
113 	case COLOR_DEPTH_141414:
114 		bits_per_channel = 14;
115 		break;
116 	case COLOR_DEPTH_161616:
117 		bits_per_channel = 16;
118 		break;
119 	default:
120 		ASSERT(bits_per_channel != 0);
121 		bits_per_channel = 8;
122 		break;
123 	}
124 
125 	kbps = timing->pix_clk_100hz / 10;
126 	kbps *= bits_per_channel;
127 
128 	if (timing->flags.Y_ONLY != 1) {
129 		/*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/
130 		kbps *= 3;
131 		if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
132 			kbps /= 2;
133 		else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
134 			kbps = kbps * 2 / 3;
135 	}
136 
137 	if (link_encoding == DC_LINK_ENCODING_DP_128b_132b)
138 		kbps = apply_128b_132b_stream_overhead(timing, kbps);
139 
140 	return kbps;
141 }
142 
143 
144 /* Forward Declerations */
145 static bool decide_dsc_bandwidth_range(
146 		const uint32_t min_bpp_x16,
147 		const uint32_t max_bpp_x16,
148 		const uint32_t num_slices_h,
149 		const struct dsc_enc_caps *dsc_caps,
150 		const struct dc_crtc_timing *timing,
151 		const enum dc_link_encoding_format link_encoding,
152 		struct dc_dsc_bw_range *range);
153 
154 static uint32_t compute_bpp_x16_from_target_bandwidth(
155 		const uint32_t bandwidth_in_kbps,
156 		const struct dc_crtc_timing *timing,
157 		const uint32_t num_slices_h,
158 		const uint32_t bpp_increment_div,
159 		const bool is_dp);
160 
161 static void get_dsc_enc_caps(
162 		const struct display_stream_compressor *dsc,
163 		struct dsc_enc_caps *dsc_enc_caps,
164 		int pixel_clock_100Hz);
165 
166 static bool intersect_dsc_caps(
167 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
168 		const struct dsc_enc_caps *dsc_enc_caps,
169 		enum dc_pixel_encoding pixel_encoding,
170 		struct dsc_enc_caps *dsc_common_caps);
171 
172 static bool setup_dsc_config(
173 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
174 		const struct dsc_enc_caps *dsc_enc_caps,
175 		int target_bandwidth_kbps,
176 		const struct dc_crtc_timing *timing,
177 		const struct dc_dsc_config_options *options,
178 		const enum dc_link_encoding_format link_encoding,
179 		struct dc_dsc_config *dsc_cfg);
180 
181 static bool dsc_buff_block_size_from_dpcd(int dpcd_buff_block_size, int *buff_block_size)
182 {
183 
184 	switch (dpcd_buff_block_size) {
185 	case DP_DSC_RC_BUF_BLK_SIZE_1:
186 		*buff_block_size = 1024;
187 		break;
188 	case DP_DSC_RC_BUF_BLK_SIZE_4:
189 		*buff_block_size = 4 * 1024;
190 		break;
191 	case DP_DSC_RC_BUF_BLK_SIZE_16:
192 		*buff_block_size = 16 * 1024;
193 		break;
194 	case DP_DSC_RC_BUF_BLK_SIZE_64:
195 		*buff_block_size = 64 * 1024;
196 		break;
197 	default: {
198 			dm_error("%s: DPCD DSC buffer size not recognized.\n", __func__);
199 			return false;
200 		}
201 	}
202 
203 	return true;
204 }
205 
206 
207 static bool dsc_line_buff_depth_from_dpcd(int dpcd_line_buff_bit_depth, int *line_buff_bit_depth)
208 {
209 	if (0 <= dpcd_line_buff_bit_depth && dpcd_line_buff_bit_depth <= 7)
210 		*line_buff_bit_depth = dpcd_line_buff_bit_depth + 9;
211 	else if (dpcd_line_buff_bit_depth == 8)
212 		*line_buff_bit_depth = 8;
213 	else {
214 		dm_error("%s: DPCD DSC buffer depth not recognized.\n", __func__);
215 		return false;
216 	}
217 
218 	return true;
219 }
220 
221 
222 static bool dsc_throughput_from_dpcd(int dpcd_throughput, int *throughput)
223 {
224 	switch (dpcd_throughput) {
225 	case DP_DSC_THROUGHPUT_MODE_0_UNSUPPORTED:
226 		*throughput = 0;
227 		break;
228 	case DP_DSC_THROUGHPUT_MODE_0_170:
229 		*throughput = 170;
230 		break;
231 	case DP_DSC_THROUGHPUT_MODE_0_340:
232 		*throughput = 340;
233 		break;
234 	case DP_DSC_THROUGHPUT_MODE_0_400:
235 		*throughput = 400;
236 		break;
237 	case DP_DSC_THROUGHPUT_MODE_0_450:
238 		*throughput = 450;
239 		break;
240 	case DP_DSC_THROUGHPUT_MODE_0_500:
241 		*throughput = 500;
242 		break;
243 	case DP_DSC_THROUGHPUT_MODE_0_550:
244 		*throughput = 550;
245 		break;
246 	case DP_DSC_THROUGHPUT_MODE_0_600:
247 		*throughput = 600;
248 		break;
249 	case DP_DSC_THROUGHPUT_MODE_0_650:
250 		*throughput = 650;
251 		break;
252 	case DP_DSC_THROUGHPUT_MODE_0_700:
253 		*throughput = 700;
254 		break;
255 	case DP_DSC_THROUGHPUT_MODE_0_750:
256 		*throughput = 750;
257 		break;
258 	case DP_DSC_THROUGHPUT_MODE_0_800:
259 		*throughput = 800;
260 		break;
261 	case DP_DSC_THROUGHPUT_MODE_0_850:
262 		*throughput = 850;
263 		break;
264 	case DP_DSC_THROUGHPUT_MODE_0_900:
265 		*throughput = 900;
266 		break;
267 	case DP_DSC_THROUGHPUT_MODE_0_950:
268 		*throughput = 950;
269 		break;
270 	case DP_DSC_THROUGHPUT_MODE_0_1000:
271 		*throughput = 1000;
272 		break;
273 	default: {
274 			dm_error("%s: DPCD DSC throughput mode not recognized.\n", __func__);
275 			return false;
276 		}
277 	}
278 
279 	return true;
280 }
281 
282 
283 static bool dsc_bpp_increment_div_from_dpcd(uint8_t bpp_increment_dpcd, uint32_t *bpp_increment_div)
284 {
285 	// Mask bpp increment dpcd field to avoid reading other fields
286 	bpp_increment_dpcd &= 0x7;
287 
288 	switch (bpp_increment_dpcd) {
289 	case 0:
290 		*bpp_increment_div = 16;
291 		break;
292 	case 1:
293 		*bpp_increment_div = 8;
294 		break;
295 	case 2:
296 		*bpp_increment_div = 4;
297 		break;
298 	case 3:
299 		*bpp_increment_div = 2;
300 		break;
301 	case 4:
302 		*bpp_increment_div = 1;
303 		break;
304 	default: {
305 		dm_error("%s: DPCD DSC bits-per-pixel increment not recognized.\n", __func__);
306 		return false;
307 	}
308 	}
309 
310 	return true;
311 }
312 
313 
314 
315 bool dc_dsc_parse_dsc_dpcd(const struct dc *dc,
316 		const uint8_t *dpcd_dsc_basic_data,
317 		const uint8_t *dpcd_dsc_branch_decoder_caps,
318 		struct dsc_dec_dpcd_caps *dsc_sink_caps)
319 {
320 	if (!dpcd_dsc_basic_data)
321 		return false;
322 
323 	dsc_sink_caps->is_dsc_supported =
324 		(dpcd_dsc_basic_data[DP_DSC_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_DECOMPRESSION_IS_SUPPORTED) != 0;
325 	if (!dsc_sink_caps->is_dsc_supported)
326 		return false;
327 
328 	dsc_sink_caps->dsc_version = dpcd_dsc_basic_data[DP_DSC_REV - DP_DSC_SUPPORT];
329 
330 	{
331 		int buff_block_size;
332 		int buff_size;
333 
334 		if (!dsc_buff_block_size_from_dpcd(dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT],
335 										   &buff_block_size))
336 			return false;
337 
338 		buff_size = dpcd_dsc_basic_data[DP_DSC_RC_BUF_SIZE - DP_DSC_SUPPORT] + 1;
339 		dsc_sink_caps->rc_buffer_size = buff_size * buff_block_size;
340 	}
341 
342 	dsc_sink_caps->slice_caps1.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
343 	if (!dsc_line_buff_depth_from_dpcd(dpcd_dsc_basic_data[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT],
344 									   &dsc_sink_caps->lb_bit_depth))
345 		return false;
346 
347 	dsc_sink_caps->is_block_pred_supported =
348 		(dpcd_dsc_basic_data[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] &
349 		 DP_DSC_BLK_PREDICTION_IS_SUPPORTED) != 0;
350 
351 	dsc_sink_caps->edp_max_bits_per_pixel =
352 		dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_LOW - DP_DSC_SUPPORT] |
353 		dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] << 8;
354 
355 	dsc_sink_caps->color_formats.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT];
356 	dsc_sink_caps->color_depth.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
357 
358 	{
359 		int dpcd_throughput = dpcd_dsc_basic_data[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];
360 
361 		if (!dsc_throughput_from_dpcd(dpcd_throughput & DP_DSC_THROUGHPUT_MODE_0_MASK,
362 									  &dsc_sink_caps->throughput_mode_0_mps))
363 			return false;
364 
365 		dpcd_throughput = (dpcd_throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >> DP_DSC_THROUGHPUT_MODE_1_SHIFT;
366 		if (!dsc_throughput_from_dpcd(dpcd_throughput, &dsc_sink_caps->throughput_mode_1_mps))
367 			return false;
368 	}
369 
370 	dsc_sink_caps->max_slice_width = dpcd_dsc_basic_data[DP_DSC_MAX_SLICE_WIDTH - DP_DSC_SUPPORT] * 320;
371 	dsc_sink_caps->slice_caps2.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
372 
373 	if (!dsc_bpp_increment_div_from_dpcd(dpcd_dsc_basic_data[DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT],
374 										 &dsc_sink_caps->bpp_increment_div))
375 		return false;
376 
377 	if (dc->debug.dsc_bpp_increment_div) {
378 		/* dsc_bpp_increment_div should onl be 1, 2, 4, 8 or 16, but rather than rejecting invalid values,
379 		 * we'll accept all and get it into range. This also makes the above check against 0 redundant,
380 		 * but that one stresses out the override will be only used if it's not 0.
381 		 */
382 		if (dc->debug.dsc_bpp_increment_div >= 1)
383 			dsc_sink_caps->bpp_increment_div = 1;
384 		if (dc->debug.dsc_bpp_increment_div >= 2)
385 			dsc_sink_caps->bpp_increment_div = 2;
386 		if (dc->debug.dsc_bpp_increment_div >= 4)
387 			dsc_sink_caps->bpp_increment_div = 4;
388 		if (dc->debug.dsc_bpp_increment_div >= 8)
389 			dsc_sink_caps->bpp_increment_div = 8;
390 		if (dc->debug.dsc_bpp_increment_div >= 16)
391 			dsc_sink_caps->bpp_increment_div = 16;
392 	}
393 
394 	/* Extended caps */
395 	if (dpcd_dsc_branch_decoder_caps == NULL) { // branch decoder DPCD DSC data can be null for non branch device
396 		dsc_sink_caps->branch_overall_throughput_0_mps = 0;
397 		dsc_sink_caps->branch_overall_throughput_1_mps = 0;
398 		dsc_sink_caps->branch_max_line_width = 0;
399 		return true;
400 	}
401 
402 	dsc_sink_caps->branch_overall_throughput_0_mps =
403 		dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_0 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];
404 	if (dsc_sink_caps->branch_overall_throughput_0_mps == 0)
405 		dsc_sink_caps->branch_overall_throughput_0_mps = 0;
406 	else if (dsc_sink_caps->branch_overall_throughput_0_mps == 1)
407 		dsc_sink_caps->branch_overall_throughput_0_mps = 680;
408 	else {
409 		dsc_sink_caps->branch_overall_throughput_0_mps *= 50;
410 		dsc_sink_caps->branch_overall_throughput_0_mps += 600;
411 	}
412 
413 	dsc_sink_caps->branch_overall_throughput_1_mps =
414 		dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_1 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];
415 	if (dsc_sink_caps->branch_overall_throughput_1_mps == 0)
416 		dsc_sink_caps->branch_overall_throughput_1_mps = 0;
417 	else if (dsc_sink_caps->branch_overall_throughput_1_mps == 1)
418 		dsc_sink_caps->branch_overall_throughput_1_mps = 680;
419 	else {
420 		dsc_sink_caps->branch_overall_throughput_1_mps *= 50;
421 		dsc_sink_caps->branch_overall_throughput_1_mps += 600;
422 	}
423 
424 	dsc_sink_caps->branch_max_line_width =
425 		dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_MAX_LINE_WIDTH - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0] * 320;
426 	ASSERT(dsc_sink_caps->branch_max_line_width == 0 || dsc_sink_caps->branch_max_line_width >= 5120);
427 
428 	dsc_sink_caps->is_dp = true;
429 	return true;
430 }
431 
432 
433 /* If DSC is possbile, get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range and
434  * timing's pixel clock and uncompressed bandwidth.
435  * If DSC is not possible, leave '*range' untouched.
436  */
437 bool dc_dsc_compute_bandwidth_range(
438 		const struct display_stream_compressor *dsc,
439 		uint32_t dsc_min_slice_height_override,
440 		uint32_t min_bpp_x16,
441 		uint32_t max_bpp_x16,
442 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
443 		const struct dc_crtc_timing *timing,
444 		const enum dc_link_encoding_format link_encoding,
445 		struct dc_dsc_bw_range *range)
446 {
447 	bool is_dsc_possible = false;
448 	struct dsc_enc_caps dsc_enc_caps;
449 	struct dsc_enc_caps dsc_common_caps;
450 	struct dc_dsc_config config;
451 	struct dc_dsc_config_options options = {0};
452 
453 	options.dsc_min_slice_height_override = dsc_min_slice_height_override;
454 	options.max_target_bpp_limit_override_x16 = max_bpp_x16;
455 	options.slice_height_granularity = 1;
456 
457 	get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
458 
459 	is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, &dsc_enc_caps,
460 			timing->pixel_encoding, &dsc_common_caps);
461 
462 	if (is_dsc_possible)
463 		is_dsc_possible = setup_dsc_config(dsc_sink_caps, &dsc_enc_caps, 0, timing,
464 				&options, link_encoding, &config);
465 
466 	if (is_dsc_possible)
467 		is_dsc_possible = decide_dsc_bandwidth_range(min_bpp_x16, max_bpp_x16,
468 				config.num_slices_h, &dsc_common_caps, timing, link_encoding, range);
469 
470 	return is_dsc_possible;
471 }
472 
473 static void get_dsc_enc_caps(
474 		const struct display_stream_compressor *dsc,
475 		struct dsc_enc_caps *dsc_enc_caps,
476 		int pixel_clock_100Hz)
477 {
478 	// This is a static HW query, so we can use any DSC
479 
480 	memset(dsc_enc_caps, 0, sizeof(struct dsc_enc_caps));
481 	if (dsc) {
482 		if (!dsc->ctx->dc->debug.disable_dsc)
483 			dsc->funcs->dsc_get_enc_caps(dsc_enc_caps, pixel_clock_100Hz);
484 		if (dsc->ctx->dc->debug.native422_support)
485 			dsc_enc_caps->color_formats.bits.YCBCR_NATIVE_422 = 1;
486 	}
487 }
488 
489 /* Returns 'false' if no intersection was found for at least one capability.
490  * It also implicitly validates some sink caps against invalid value of zero.
491  */
492 static bool intersect_dsc_caps(
493 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
494 		const struct dsc_enc_caps *dsc_enc_caps,
495 		enum dc_pixel_encoding pixel_encoding,
496 		struct dsc_enc_caps *dsc_common_caps)
497 {
498 	int32_t max_slices;
499 	int32_t total_sink_throughput;
500 
501 	memset(dsc_common_caps, 0, sizeof(struct dsc_enc_caps));
502 
503 	dsc_common_caps->dsc_version = min(dsc_sink_caps->dsc_version, dsc_enc_caps->dsc_version);
504 	if (!dsc_common_caps->dsc_version)
505 		return false;
506 
507 	dsc_common_caps->slice_caps.bits.NUM_SLICES_1 =
508 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_1 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_1;
509 	dsc_common_caps->slice_caps.bits.NUM_SLICES_2 =
510 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_2 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_2;
511 	dsc_common_caps->slice_caps.bits.NUM_SLICES_4 =
512 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_4 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_4;
513 	dsc_common_caps->slice_caps.bits.NUM_SLICES_8 =
514 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_8 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_8;
515 	dsc_common_caps->slice_caps.bits.NUM_SLICES_12 =
516 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_12 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_12;
517 	dsc_common_caps->slice_caps.bits.NUM_SLICES_16 =
518 		dsc_sink_caps->slice_caps2.bits.NUM_SLICES_16 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_16;
519 
520 	if (!dsc_common_caps->slice_caps.raw)
521 		return false;
522 
523 	dsc_common_caps->lb_bit_depth = min(dsc_sink_caps->lb_bit_depth, dsc_enc_caps->lb_bit_depth);
524 	if (!dsc_common_caps->lb_bit_depth)
525 		return false;
526 
527 	dsc_common_caps->is_block_pred_supported =
528 		dsc_sink_caps->is_block_pred_supported && dsc_enc_caps->is_block_pred_supported;
529 
530 	dsc_common_caps->color_formats.raw = dsc_sink_caps->color_formats.raw & dsc_enc_caps->color_formats.raw;
531 	if (!dsc_common_caps->color_formats.raw)
532 		return false;
533 
534 	dsc_common_caps->color_depth.raw = dsc_sink_caps->color_depth.raw & dsc_enc_caps->color_depth.raw;
535 	if (!dsc_common_caps->color_depth.raw)
536 		return false;
537 
538 	max_slices = 0;
539 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_1)
540 		max_slices = 1;
541 
542 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_2)
543 		max_slices = 2;
544 
545 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_4)
546 		max_slices = 4;
547 
548 	total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_0_mps;
549 	if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
550 		total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_1_mps;
551 
552 	dsc_common_caps->max_total_throughput_mps = min(total_sink_throughput, dsc_enc_caps->max_total_throughput_mps);
553 
554 	dsc_common_caps->max_slice_width = min(dsc_sink_caps->max_slice_width, dsc_enc_caps->max_slice_width);
555 	if (!dsc_common_caps->max_slice_width)
556 		return false;
557 
558 	dsc_common_caps->bpp_increment_div = min(dsc_sink_caps->bpp_increment_div, dsc_enc_caps->bpp_increment_div);
559 
560 	// TODO DSC: Remove this workaround for N422 and 420 once it's fixed, or move it to get_dsc_encoder_caps()
561 	if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
562 		dsc_common_caps->bpp_increment_div = min(dsc_common_caps->bpp_increment_div, (uint32_t)8);
563 
564 	dsc_common_caps->edp_sink_max_bits_per_pixel = dsc_sink_caps->edp_max_bits_per_pixel;
565 	dsc_common_caps->is_dp = dsc_sink_caps->is_dp;
566 	return true;
567 }
568 
569 static inline uint32_t dsc_div_by_10_round_up(uint32_t value)
570 {
571 	return (value + 9) / 10;
572 }
573 
574 static uint32_t compute_bpp_x16_from_target_bandwidth(
575 	const uint32_t bandwidth_in_kbps,
576 	const struct dc_crtc_timing *timing,
577 	const uint32_t num_slices_h,
578 	const uint32_t bpp_increment_div,
579 	const bool is_dp)
580 {
581 	uint32_t overhead_in_kbps;
582 	struct fixed31_32 effective_bandwidth_in_kbps;
583 	struct fixed31_32 bpp_x16;
584 
585 	overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
586 				timing, num_slices_h, is_dp);
587 	effective_bandwidth_in_kbps = dc_fixpt_from_int(bandwidth_in_kbps);
588 	effective_bandwidth_in_kbps = dc_fixpt_sub_int(effective_bandwidth_in_kbps,
589 			overhead_in_kbps);
590 	bpp_x16 = dc_fixpt_mul_int(effective_bandwidth_in_kbps, 10);
591 	bpp_x16 = dc_fixpt_div_int(bpp_x16, timing->pix_clk_100hz);
592 	bpp_x16 = dc_fixpt_from_int(dc_fixpt_floor(dc_fixpt_mul_int(bpp_x16, bpp_increment_div)));
593 	bpp_x16 = dc_fixpt_div_int(bpp_x16, bpp_increment_div);
594 	bpp_x16 = dc_fixpt_mul_int(bpp_x16, 16);
595 	return dc_fixpt_floor(bpp_x16);
596 }
597 
598 /* Decide DSC bandwidth range based on signal, timing, specs specific and input min and max
599  * requirements.
600  * The range output includes decided min/max target bpp, the respective bandwidth requirements
601  * and native timing bandwidth requirement when DSC is not used.
602  */
603 static bool decide_dsc_bandwidth_range(
604 		const uint32_t min_bpp_x16,
605 		const uint32_t max_bpp_x16,
606 		const uint32_t num_slices_h,
607 		const struct dsc_enc_caps *dsc_caps,
608 		const struct dc_crtc_timing *timing,
609 		const enum dc_link_encoding_format link_encoding,
610 		struct dc_dsc_bw_range *range)
611 {
612 	uint32_t preferred_bpp_x16 = timing->dsc_fixed_bits_per_pixel_x16;
613 
614 	memset(range, 0, sizeof(*range));
615 
616 	/* apply signal, timing, specs and explicitly specified DSC range requirements */
617 	if (preferred_bpp_x16) {
618 		if (preferred_bpp_x16 <= max_bpp_x16 &&
619 				preferred_bpp_x16 >= min_bpp_x16) {
620 			range->max_target_bpp_x16 = preferred_bpp_x16;
621 			range->min_target_bpp_x16 = preferred_bpp_x16;
622 		}
623 	}
624 	/* TODO - make this value generic to all signal types */
625 	else if (dsc_caps->edp_sink_max_bits_per_pixel) {
626 		/* apply max bpp limitation from edp sink */
627 		range->max_target_bpp_x16 = MIN(dsc_caps->edp_sink_max_bits_per_pixel,
628 				max_bpp_x16);
629 		range->min_target_bpp_x16 = min_bpp_x16;
630 	}
631 	else {
632 		range->max_target_bpp_x16 = max_bpp_x16;
633 		range->min_target_bpp_x16 = min_bpp_x16;
634 	}
635 
636 	/* populate output structure */
637 	if (range->max_target_bpp_x16 >= range->min_target_bpp_x16 && range->min_target_bpp_x16 > 0) {
638 		/* native stream bandwidth */
639 		range->stream_kbps = dc_bandwidth_in_kbps_from_timing(timing, link_encoding);
640 
641 		/* max dsc target bpp */
642 		range->max_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
643 				range->max_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
644 
645 		/* min dsc target bpp */
646 		range->min_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
647 				range->min_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
648 	}
649 
650 	return range->max_kbps >= range->min_kbps && range->min_kbps > 0;
651 }
652 
653 /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy.
654  *
655  * Returns:
656  *     - 'true' if target bpp is decided
657  *     - 'false' if target bpp cannot be decided (e.g. cannot fit even with min DSC bpp),
658  */
659 static bool decide_dsc_target_bpp_x16(
660 		const struct dc_dsc_policy *policy,
661 		const struct dsc_enc_caps *dsc_common_caps,
662 		const int target_bandwidth_kbps,
663 		const struct dc_crtc_timing *timing,
664 		const int num_slices_h,
665 		const enum dc_link_encoding_format link_encoding,
666 		int *target_bpp_x16)
667 {
668 	struct dc_dsc_bw_range range;
669 
670 	*target_bpp_x16 = 0;
671 
672 	if (decide_dsc_bandwidth_range(policy->min_target_bpp * 16, policy->max_target_bpp * 16,
673 			num_slices_h, dsc_common_caps, timing, link_encoding, &range)) {
674 		if (target_bandwidth_kbps >= range.stream_kbps) {
675 			if (policy->enable_dsc_when_not_needed)
676 				/* enable max bpp even dsc is not needed */
677 				*target_bpp_x16 = range.max_target_bpp_x16;
678 		} else if (target_bandwidth_kbps >= range.max_kbps) {
679 			/* use max target bpp allowed */
680 			*target_bpp_x16 = range.max_target_bpp_x16;
681 		} else if (target_bandwidth_kbps >= range.min_kbps) {
682 			/* use target bpp that can take entire target bandwidth */
683 			*target_bpp_x16 = compute_bpp_x16_from_target_bandwidth(
684 					target_bandwidth_kbps, timing, num_slices_h,
685 					dsc_common_caps->bpp_increment_div,
686 					dsc_common_caps->is_dp);
687 		}
688 	}
689 
690 	return *target_bpp_x16 != 0;
691 }
692 
693 #define MIN_AVAILABLE_SLICES_SIZE  6
694 
695 static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps, int *available_slices)
696 {
697 	int idx = 0;
698 
699 	if (slice_caps.bits.NUM_SLICES_1)
700 		available_slices[idx++] = 1;
701 
702 	if (slice_caps.bits.NUM_SLICES_2)
703 		available_slices[idx++] = 2;
704 
705 	if (slice_caps.bits.NUM_SLICES_4)
706 		available_slices[idx++] = 4;
707 
708 	if (slice_caps.bits.NUM_SLICES_8)
709 		available_slices[idx++] = 8;
710 
711 	if (slice_caps.bits.NUM_SLICES_12)
712 		available_slices[idx++] = 12;
713 
714 	if (slice_caps.bits.NUM_SLICES_16)
715 		available_slices[idx++] = 16;
716 
717 	return idx;
718 }
719 
720 
721 static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps)
722 {
723 	int max_slices = 0;
724 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
725 	int end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
726 
727 	if (end_idx > 0)
728 		max_slices = available_slices[end_idx - 1];
729 
730 	return max_slices;
731 }
732 
733 
734 // Increment slice number in available slice numbers stops if possible, or just increment if not
735 static int inc_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
736 {
737 	// Get next bigger num slices available in common caps
738 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
739 	int end_idx;
740 	int i;
741 	int new_num_slices = num_slices;
742 
743 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
744 	if (end_idx == 0) {
745 		// No available slices found
746 		new_num_slices++;
747 		return new_num_slices;
748 	}
749 
750 	// Numbers of slices found - get the next bigger number
751 	for (i = 0; i < end_idx; i++) {
752 		if (new_num_slices < available_slices[i]) {
753 			new_num_slices = available_slices[i];
754 			break;
755 		}
756 	}
757 
758 	if (new_num_slices == num_slices) // No bigger number of slices found
759 		new_num_slices++;
760 
761 	return new_num_slices;
762 }
763 
764 
765 // Decrement slice number in available slice numbers stops if possible, or just decrement if not. Stop at zero.
766 static int dec_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
767 {
768 	// Get next bigger num slices available in common caps
769 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
770 	int end_idx;
771 	int i;
772 	int new_num_slices = num_slices;
773 
774 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
775 	if (end_idx == 0 && new_num_slices > 0) {
776 		// No numbers of slices found
777 		new_num_slices++;
778 		return new_num_slices;
779 	}
780 
781 	// Numbers of slices found - get the next smaller number
782 	for (i = end_idx - 1; i >= 0; i--) {
783 		if (new_num_slices > available_slices[i]) {
784 			new_num_slices = available_slices[i];
785 			break;
786 		}
787 	}
788 
789 	if (new_num_slices == num_slices) {
790 		// No smaller number of slices found
791 		new_num_slices--;
792 		if (new_num_slices < 0)
793 			new_num_slices = 0;
794 	}
795 
796 	return new_num_slices;
797 }
798 
799 
800 // Choose next bigger number of slices if the requested number of slices is not available
801 static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps, int num_slices)
802 {
803 	// Get next bigger num slices available in common caps
804 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
805 	int end_idx;
806 	int i;
807 	int new_num_slices = num_slices;
808 
809 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
810 	if (end_idx == 0) {
811 		// No available slices found
812 		new_num_slices++;
813 		return new_num_slices;
814 	}
815 
816 	// Numbers of slices found - get the equal or next bigger number
817 	for (i = 0; i < end_idx; i++) {
818 		if (new_num_slices <= available_slices[i]) {
819 			new_num_slices = available_slices[i];
820 			break;
821 		}
822 	}
823 
824 	return new_num_slices;
825 }
826 
827 
828 /* Attempts to set DSC configuration for the stream, applying DSC policy.
829  * Returns 'true' if successful or 'false' if not.
830  *
831  * Parameters:
832  *
833  * dsc_sink_caps       - DSC sink decoder capabilities (from DPCD)
834  *
835  * dsc_enc_caps        - DSC encoder capabilities
836  *
837  * target_bandwidth_kbps  - Target bandwidth to fit the stream into.
838  *                          If 0, do not calculate target bpp.
839  *
840  * timing              - The stream timing to fit into 'target_bandwidth_kbps' or apply
841  *                       maximum compression to, if 'target_badwidth == 0'
842  *
843  * dsc_cfg             - DSC configuration to use if it was possible to come up with
844  *                       one for the given inputs.
845  *                       The target bitrate after DSC can be calculated by multiplying
846  *                       dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g.
847  *
848  *                       dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0);
849  */
850 static bool setup_dsc_config(
851 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
852 		const struct dsc_enc_caps *dsc_enc_caps,
853 		int target_bandwidth_kbps,
854 		const struct dc_crtc_timing *timing,
855 		const struct dc_dsc_config_options *options,
856 		const enum dc_link_encoding_format link_encoding,
857 		struct dc_dsc_config *dsc_cfg)
858 {
859 	struct dsc_enc_caps dsc_common_caps;
860 	int max_slices_h;
861 	int min_slices_h;
862 	int num_slices_h;
863 	int pic_width;
864 	int slice_width;
865 	int target_bpp;
866 	int sink_per_slice_throughput_mps;
867 	int branch_max_throughput_mps = 0;
868 	bool is_dsc_possible = false;
869 	int pic_height;
870 	int slice_height;
871 	struct dc_dsc_policy policy;
872 
873 	memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
874 
875 	dc_dsc_get_policy_for_timing(timing, options->max_target_bpp_limit_override_x16, &policy);
876 	pic_width = timing->h_addressable + timing->h_border_left + timing->h_border_right;
877 	pic_height = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
878 
879 	if (!dsc_sink_caps->is_dsc_supported)
880 		goto done;
881 
882 	if (dsc_sink_caps->branch_max_line_width && dsc_sink_caps->branch_max_line_width < pic_width)
883 		goto done;
884 
885 	// Intersect decoder with encoder DSC caps and validate DSC settings
886 	is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, dsc_enc_caps, timing->pixel_encoding, &dsc_common_caps);
887 	if (!is_dsc_possible)
888 		goto done;
889 
890 	sink_per_slice_throughput_mps = 0;
891 
892 	// Validate available DSC settings against the mode timing
893 
894 	// Validate color format (and pick up the throughput values)
895 	dsc_cfg->ycbcr422_simple = false;
896 	switch (timing->pixel_encoding)	{
897 	case PIXEL_ENCODING_RGB:
898 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.RGB;
899 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
900 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
901 		break;
902 	case PIXEL_ENCODING_YCBCR444:
903 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_444;
904 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
905 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
906 		break;
907 	case PIXEL_ENCODING_YCBCR422:
908 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_422;
909 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
910 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
911 		if (!is_dsc_possible) {
912 			is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_SIMPLE_422;
913 			dsc_cfg->ycbcr422_simple = is_dsc_possible;
914 			sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
915 		}
916 		break;
917 	case PIXEL_ENCODING_YCBCR420:
918 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_420;
919 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
920 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
921 		break;
922 	default:
923 		is_dsc_possible = false;
924 	}
925 
926 	// Validate branch's maximum throughput
927 	if (branch_max_throughput_mps && dsc_div_by_10_round_up(timing->pix_clk_100hz) > branch_max_throughput_mps * 1000)
928 		is_dsc_possible = false;
929 
930 	if (!is_dsc_possible)
931 		goto done;
932 
933 	// Color depth
934 	switch (timing->display_color_depth) {
935 	case COLOR_DEPTH_888:
936 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_8_BPC;
937 		break;
938 	case COLOR_DEPTH_101010:
939 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_10_BPC;
940 		break;
941 	case COLOR_DEPTH_121212:
942 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_12_BPC;
943 		break;
944 	default:
945 		is_dsc_possible = false;
946 	}
947 
948 	if (!is_dsc_possible)
949 		goto done;
950 
951 	// Slice width (i.e. number of slices per line)
952 	max_slices_h = get_max_dsc_slices(dsc_common_caps.slice_caps);
953 
954 	while (max_slices_h > 0) {
955 		if (pic_width % max_slices_h == 0)
956 			break;
957 
958 		max_slices_h = dec_num_slices(dsc_common_caps.slice_caps, max_slices_h);
959 	}
960 
961 	is_dsc_possible = (dsc_common_caps.max_slice_width > 0);
962 	if (!is_dsc_possible)
963 		goto done;
964 
965 	min_slices_h = pic_width / dsc_common_caps.max_slice_width;
966 	if (pic_width % dsc_common_caps.max_slice_width)
967 		min_slices_h++;
968 
969 	min_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, min_slices_h);
970 
971 	while (min_slices_h <= max_slices_h) {
972 		int pix_clk_per_slice_khz = dsc_div_by_10_round_up(timing->pix_clk_100hz) / min_slices_h;
973 		if (pix_clk_per_slice_khz <= sink_per_slice_throughput_mps * 1000)
974 			break;
975 
976 		min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
977 	}
978 
979 	is_dsc_possible = (min_slices_h <= max_slices_h);
980 
981 	if (pic_width % min_slices_h != 0)
982 		min_slices_h = 0; // DSC TODO: Maybe try increasing the number of slices first?
983 
984 	if (min_slices_h == 0 && max_slices_h == 0)
985 		is_dsc_possible = false;
986 
987 	if (!is_dsc_possible)
988 		goto done;
989 
990 	if (policy.use_min_slices_h) {
991 		if (min_slices_h > 0)
992 			num_slices_h = min_slices_h;
993 		else if (max_slices_h > 0) { // Fall back to max slices if min slices is not working out
994 			if (policy.max_slices_h)
995 				num_slices_h = min(policy.max_slices_h, max_slices_h);
996 			else
997 				num_slices_h = max_slices_h;
998 		} else
999 			is_dsc_possible = false;
1000 	} else {
1001 		if (max_slices_h > 0) {
1002 			if (policy.max_slices_h)
1003 				num_slices_h = min(policy.max_slices_h, max_slices_h);
1004 			else
1005 				num_slices_h = max_slices_h;
1006 		} else if (min_slices_h > 0) // Fall back to min slices if max slices is not possible
1007 			num_slices_h = min_slices_h;
1008 		else
1009 			is_dsc_possible = false;
1010 	}
1011 	// When we force 2:1 ODM, we can't have 1 slice to divide amongst 2 separate DSC instances
1012 	// need to enforce at minimum 2 horizontal slices
1013 	if (options->dsc_force_odm_hslice_override) {
1014 		num_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, 2);
1015 		if (num_slices_h == 0)
1016 			is_dsc_possible = false;
1017 	}
1018 
1019 	if (!is_dsc_possible)
1020 		goto done;
1021 
1022 	dsc_cfg->num_slices_h = num_slices_h;
1023 	slice_width = pic_width / num_slices_h;
1024 
1025 	is_dsc_possible = slice_width <= dsc_common_caps.max_slice_width;
1026 	if (!is_dsc_possible)
1027 		goto done;
1028 
1029 	// Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by.
1030 	// For 4:2:0 make sure the slice height is divisible by 2 as well.
1031 	if (options->dsc_min_slice_height_override == 0)
1032 		slice_height = min(policy.min_slice_height, pic_height);
1033 	else
1034 		slice_height = min((int)(options->dsc_min_slice_height_override), pic_height);
1035 
1036 	while (slice_height < pic_height && (pic_height % slice_height != 0 ||
1037 		slice_height % options->slice_height_granularity != 0 ||
1038 		(timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 && slice_height % 2 != 0)))
1039 		slice_height++;
1040 
1041 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) // For the case when pic_height < dsc_policy.min_sice_height
1042 		is_dsc_possible = (slice_height % 2 == 0);
1043 
1044 	if (!is_dsc_possible)
1045 		goto done;
1046 
1047 	dsc_cfg->num_slices_v = pic_height/slice_height;
1048 
1049 	if (target_bandwidth_kbps > 0) {
1050 		is_dsc_possible = decide_dsc_target_bpp_x16(
1051 				&policy,
1052 				&dsc_common_caps,
1053 				target_bandwidth_kbps,
1054 				timing,
1055 				num_slices_h,
1056 				link_encoding,
1057 				&target_bpp);
1058 		dsc_cfg->bits_per_pixel = target_bpp;
1059 	}
1060 	if (!is_dsc_possible)
1061 		goto done;
1062 
1063 	// Final decission: can we do DSC or not?
1064 	if (is_dsc_possible) {
1065 		// Fill out the rest of DSC settings
1066 		dsc_cfg->block_pred_enable = dsc_common_caps.is_block_pred_supported;
1067 		dsc_cfg->linebuf_depth = dsc_common_caps.lb_bit_depth;
1068 		dsc_cfg->version_minor = (dsc_common_caps.dsc_version & 0xf0) >> 4;
1069 		dsc_cfg->is_dp = dsc_sink_caps->is_dp;
1070 	}
1071 
1072 done:
1073 	if (!is_dsc_possible)
1074 		memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
1075 
1076 	return is_dsc_possible;
1077 }
1078 
1079 bool dc_dsc_compute_config(
1080 		const struct display_stream_compressor *dsc,
1081 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
1082 		const struct dc_dsc_config_options *options,
1083 		uint32_t target_bandwidth_kbps,
1084 		const struct dc_crtc_timing *timing,
1085 		const enum dc_link_encoding_format link_encoding,
1086 		struct dc_dsc_config *dsc_cfg)
1087 {
1088 	bool is_dsc_possible = false;
1089 	struct dsc_enc_caps dsc_enc_caps;
1090 
1091 	get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
1092 	is_dsc_possible = setup_dsc_config(dsc_sink_caps,
1093 		&dsc_enc_caps,
1094 		target_bandwidth_kbps,
1095 		timing, options, link_encoding, dsc_cfg);
1096 	return is_dsc_possible;
1097 }
1098 
1099 uint32_t dc_dsc_stream_bandwidth_in_kbps(const struct dc_crtc_timing *timing,
1100 	uint32_t bpp_x16, uint32_t num_slices_h, bool is_dp)
1101 {
1102 	uint32_t overhead_in_kbps;
1103 	struct fixed31_32 bpp;
1104 	struct fixed31_32 actual_bandwidth_in_kbps;
1105 
1106 	overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
1107 		timing, num_slices_h, is_dp);
1108 	bpp = dc_fixpt_from_fraction(bpp_x16, 16);
1109 	actual_bandwidth_in_kbps = dc_fixpt_from_fraction(timing->pix_clk_100hz, 10);
1110 	actual_bandwidth_in_kbps = dc_fixpt_mul(actual_bandwidth_in_kbps, bpp);
1111 	actual_bandwidth_in_kbps = dc_fixpt_add_int(actual_bandwidth_in_kbps, overhead_in_kbps);
1112 	return dc_fixpt_ceil(actual_bandwidth_in_kbps);
1113 }
1114 
1115 uint32_t dc_dsc_stream_bandwidth_overhead_in_kbps(
1116 		const struct dc_crtc_timing *timing,
1117 		const int num_slices_h,
1118 		const bool is_dp)
1119 {
1120 	struct fixed31_32 max_dsc_overhead;
1121 	struct fixed31_32 refresh_rate;
1122 
1123 	if (dsc_policy_disable_dsc_stream_overhead || !is_dp)
1124 		return 0;
1125 
1126 	/* use target bpp that can take entire target bandwidth */
1127 	refresh_rate = dc_fixpt_from_int(timing->pix_clk_100hz);
1128 	refresh_rate = dc_fixpt_div_int(refresh_rate, timing->h_total);
1129 	refresh_rate = dc_fixpt_div_int(refresh_rate, timing->v_total);
1130 	refresh_rate = dc_fixpt_mul_int(refresh_rate, 100);
1131 
1132 	max_dsc_overhead = dc_fixpt_from_int(num_slices_h);
1133 	max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, timing->v_total);
1134 	max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, 256);
1135 	max_dsc_overhead = dc_fixpt_div_int(max_dsc_overhead, 1000);
1136 	max_dsc_overhead = dc_fixpt_mul(max_dsc_overhead, refresh_rate);
1137 
1138 	return dc_fixpt_ceil(max_dsc_overhead);
1139 }
1140 
1141 void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing *timing,
1142 		uint32_t max_target_bpp_limit_override_x16,
1143 		struct dc_dsc_policy *policy)
1144 {
1145 	uint32_t bpc = 0;
1146 
1147 	policy->min_target_bpp = 0;
1148 	policy->max_target_bpp = 0;
1149 
1150 	/* DSC Policy: Use minimum number of slices that fits the pixel clock */
1151 	policy->use_min_slices_h = true;
1152 
1153 	/* DSC Policy: Use max available slices
1154 	 * (in our case 4 for or 8, depending on the mode)
1155 	 */
1156 	policy->max_slices_h = 0;
1157 
1158 	/* DSC Policy: Use slice height recommended
1159 	 * by VESA DSC Spreadsheet user guide
1160 	 */
1161 	policy->min_slice_height = 108;
1162 
1163 	/* DSC Policy: follow DP specs with an internal upper limit to 16 bpp
1164 	 * for better interoperability
1165 	 */
1166 	switch (timing->display_color_depth) {
1167 	case COLOR_DEPTH_888:
1168 		bpc = 8;
1169 		break;
1170 	case COLOR_DEPTH_101010:
1171 		bpc = 10;
1172 		break;
1173 	case COLOR_DEPTH_121212:
1174 		bpc = 12;
1175 		break;
1176 	default:
1177 		return;
1178 	}
1179 	switch (timing->pixel_encoding) {
1180 	case PIXEL_ENCODING_RGB:
1181 	case PIXEL_ENCODING_YCBCR444:
1182 	case PIXEL_ENCODING_YCBCR422: /* assume no YCbCr422 native support */
1183 		/* DP specs limits to 8 */
1184 		policy->min_target_bpp = 8;
1185 		/* DP specs limits to 3 x bpc */
1186 		policy->max_target_bpp = 3 * bpc;
1187 		break;
1188 	case PIXEL_ENCODING_YCBCR420:
1189 		/* DP specs limits to 6 */
1190 		policy->min_target_bpp = 6;
1191 		/* DP specs limits to 1.5 x bpc assume bpc is an even number */
1192 		policy->max_target_bpp = bpc * 3 / 2;
1193 		break;
1194 	default:
1195 		return;
1196 	}
1197 
1198 	/* internal upper limit, default 16 bpp */
1199 	if (policy->max_target_bpp > dsc_policy_max_target_bpp_limit)
1200 		policy->max_target_bpp = dsc_policy_max_target_bpp_limit;
1201 
1202 	/* apply override */
1203 	if (max_target_bpp_limit_override_x16 && policy->max_target_bpp > max_target_bpp_limit_override_x16 / 16)
1204 		policy->max_target_bpp = max_target_bpp_limit_override_x16 / 16;
1205 
1206 	/* enable DSC when not needed, default false */
1207 	if (dsc_policy_enable_dsc_when_not_needed)
1208 		policy->enable_dsc_when_not_needed = dsc_policy_enable_dsc_when_not_needed;
1209 	else
1210 		policy->enable_dsc_when_not_needed = false;
1211 }
1212 
1213 void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit)
1214 {
1215 	dsc_policy_max_target_bpp_limit = limit;
1216 }
1217 
1218 void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable)
1219 {
1220 	dsc_policy_enable_dsc_when_not_needed = enable;
1221 }
1222 
1223 void dc_dsc_policy_set_disable_dsc_stream_overhead(bool disable)
1224 {
1225 	dsc_policy_disable_dsc_stream_overhead = disable;
1226 }
1227 
1228 void dc_set_disable_128b_132b_stream_overhead(bool disable)
1229 {
1230 	disable_128b_132b_stream_overhead = disable;
1231 }
1232 
1233 void dc_dsc_get_default_config_option(const struct dc *dc, struct dc_dsc_config_options *options)
1234 {
1235 	options->dsc_min_slice_height_override = dc->debug.dsc_min_slice_height_override;
1236 	options->dsc_force_odm_hslice_override = dc->debug.force_odm_combine;
1237 	options->max_target_bpp_limit_override_x16 = 0;
1238 	options->slice_height_granularity = 1;
1239 }
1240