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