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