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