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