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