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