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