xref: /linux/drivers/gpu/drm/amd/display/dc/dsc/dc_dsc.c (revision c06b6cde2a1c3bcbb561bd57bb6f34eae9030921)
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/display/drm_dp_helper.h>
26 #include <drm/display/drm_dsc_helper.h>
27 #include "dc_hw_types.h"
28 #include "dsc.h"
29 #include "dc.h"
30 #include "rc_calc.h"
31 #include "fixed31_32.h"
32 
33 #include "clk_mgr.h"
34 #include "resource.h"
35 
36 #define DC_LOGGER \
37 	dsc->ctx->logger
38 
39 /* This module's internal functions */
40 
41 /* default DSC policy target bitrate limit is 16bpp */
42 static uint32_t dsc_policy_max_target_bpp_limit = 16;
43 
44 /* default DSC policy enables DSC only when needed */
45 static bool dsc_policy_enable_dsc_when_not_needed;
46 
47 static bool dsc_policy_disable_dsc_stream_overhead;
48 
49 static bool disable_128b_132b_stream_overhead;
50 
51 #ifndef MAX
52 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
53 #endif
54 #ifndef MIN
55 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
56 #endif
57 
58 /* Need to account for padding due to pixel-to-symbol packing
59  * for uncompressed 128b/132b streams.
60  */
61 static uint32_t apply_128b_132b_stream_overhead(
62 	const struct dc_crtc_timing *timing, const uint32_t kbps)
63 {
64 	uint32_t total_kbps = kbps;
65 
66 	if (disable_128b_132b_stream_overhead)
67 		return kbps;
68 
69 	if (!timing->flags.DSC) {
70 		struct fixed31_32 bpp;
71 		struct fixed31_32 overhead_factor;
72 
73 		bpp = dc_fixpt_from_int(kbps);
74 		bpp = dc_fixpt_div_int(bpp, timing->pix_clk_100hz / 10);
75 
76 		/* Symbols_per_HActive = HActive * bpp / (4 lanes * 32-bit symbol size)
77 		 * Overhead_factor = ceil(Symbols_per_HActive) / Symbols_per_HActive
78 		 */
79 		overhead_factor = dc_fixpt_from_int(timing->h_addressable);
80 		overhead_factor = dc_fixpt_mul(overhead_factor, bpp);
81 		overhead_factor = dc_fixpt_div_int(overhead_factor, 128);
82 		overhead_factor = dc_fixpt_div(
83 			dc_fixpt_from_int(dc_fixpt_ceil(overhead_factor)),
84 			overhead_factor);
85 
86 		total_kbps = dc_fixpt_ceil(
87 			dc_fixpt_mul_int(overhead_factor, total_kbps));
88 	}
89 
90 	return total_kbps;
91 }
92 
93 uint32_t dc_bandwidth_in_kbps_from_timing(
94 	const struct dc_crtc_timing *timing,
95 	const enum dc_link_encoding_format link_encoding)
96 {
97 	uint32_t bits_per_channel = 0;
98 	uint32_t kbps;
99 
100 	if (timing->flags.DSC)
101 		return dc_dsc_stream_bandwidth_in_kbps(timing,
102 				timing->dsc_cfg.bits_per_pixel,
103 				timing->dsc_cfg.num_slices_h,
104 				timing->dsc_cfg.is_dp);
105 
106 	switch (timing->display_color_depth) {
107 	case COLOR_DEPTH_666:
108 		bits_per_channel = 6;
109 		break;
110 	case COLOR_DEPTH_888:
111 		bits_per_channel = 8;
112 		break;
113 	case COLOR_DEPTH_101010:
114 		bits_per_channel = 10;
115 		break;
116 	case COLOR_DEPTH_121212:
117 		bits_per_channel = 12;
118 		break;
119 	case COLOR_DEPTH_141414:
120 		bits_per_channel = 14;
121 		break;
122 	case COLOR_DEPTH_161616:
123 		bits_per_channel = 16;
124 		break;
125 	default:
126 		ASSERT(bits_per_channel != 0);
127 		bits_per_channel = 8;
128 		break;
129 	}
130 
131 	kbps = timing->pix_clk_100hz / 10;
132 	kbps *= bits_per_channel;
133 
134 	if (timing->flags.Y_ONLY != 1) {
135 		/*Only YOnly make reduce bandwidth by 1/3 compares to RGB*/
136 		kbps *= 3;
137 		if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420)
138 			kbps /= 2;
139 		else if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR422)
140 			kbps = kbps * 2 / 3;
141 	}
142 
143 	if (link_encoding == DC_LINK_ENCODING_DP_128b_132b)
144 		kbps = apply_128b_132b_stream_overhead(timing, kbps);
145 
146 	if (link_encoding == DC_LINK_ENCODING_HDMI_FRL &&
147 			timing->vic == 0 && timing->hdmi_vic == 0 &&
148 			timing->frl_uncompressed_video_bandwidth_in_kbps != 0)
149 		kbps = timing->frl_uncompressed_video_bandwidth_in_kbps;
150 
151 	return kbps;
152 }
153 
154 /* Forward Declerations */
155 static unsigned int get_min_dsc_slice_count_for_odm(
156 		const struct display_stream_compressor *dsc,
157 		const struct dsc_enc_caps *dsc_enc_caps,
158 		const struct dc_crtc_timing *timing);
159 
160 static bool decide_dsc_bandwidth_range(
161 		const uint32_t min_bpp_x16,
162 		const uint32_t max_bpp_x16,
163 		const uint32_t num_slices_h,
164 		const struct dsc_enc_caps *dsc_caps,
165 		const struct dc_crtc_timing *timing,
166 		const enum dc_link_encoding_format link_encoding,
167 		struct dc_dsc_bw_range *range);
168 
169 static uint32_t compute_bpp_x16_from_target_bandwidth(
170 		const uint32_t bandwidth_in_kbps,
171 		const struct dc_crtc_timing *timing,
172 		const uint32_t num_slices_h,
173 		const uint32_t bpp_increment_div,
174 		const bool is_dp);
175 
176 static void get_dsc_enc_caps(
177 		const struct display_stream_compressor *dsc,
178 		struct dsc_enc_caps *dsc_enc_caps,
179 		int pixel_clock_100Hz);
180 
181 static bool intersect_dsc_caps(
182 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
183 		const struct dsc_enc_caps *dsc_enc_caps,
184 		enum dc_pixel_encoding pixel_encoding,
185 		struct dsc_enc_caps *dsc_common_caps);
186 
187 static bool setup_dsc_config(
188 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
189 		const struct dsc_enc_caps *dsc_enc_caps,
190 		int target_bandwidth_kbps,
191 		const struct dc_crtc_timing *timing,
192 		const struct dc_dsc_config_options *options,
193 		const enum dc_link_encoding_format link_encoding,
194 		int min_slice_count,
195 		struct dc_dsc_config *dsc_cfg);
196 
197 static bool dsc_buff_block_size_from_dpcd(int dpcd_buff_block_size, int *buff_block_size)
198 {
199 
200 	switch (dpcd_buff_block_size) {
201 	case DP_DSC_RC_BUF_BLK_SIZE_1:
202 		*buff_block_size = 1024;
203 		break;
204 	case DP_DSC_RC_BUF_BLK_SIZE_4:
205 		*buff_block_size = 4 * 1024;
206 		break;
207 	case DP_DSC_RC_BUF_BLK_SIZE_16:
208 		*buff_block_size = 16 * 1024;
209 		break;
210 	case DP_DSC_RC_BUF_BLK_SIZE_64:
211 		*buff_block_size = 64 * 1024;
212 		break;
213 	default: {
214 			dm_error("%s: DPCD DSC buffer size not recognized.\n", __func__);
215 			return false;
216 		}
217 	}
218 
219 	return true;
220 }
221 
222 
223 static bool dsc_line_buff_depth_from_dpcd(int dpcd_line_buff_bit_depth, int *line_buff_bit_depth)
224 {
225 	if (0 <= dpcd_line_buff_bit_depth && dpcd_line_buff_bit_depth <= 7)
226 		*line_buff_bit_depth = dpcd_line_buff_bit_depth + 9;
227 	else if (dpcd_line_buff_bit_depth == 8)
228 		*line_buff_bit_depth = 8;
229 	else {
230 		dm_error("%s: DPCD DSC buffer depth not recognized.\n", __func__);
231 		return false;
232 	}
233 
234 	return true;
235 }
236 
237 
238 static bool dsc_throughput_from_dpcd(int dpcd_throughput, int *throughput)
239 {
240 	switch (dpcd_throughput) {
241 	case DP_DSC_THROUGHPUT_MODE_0_UNSUPPORTED:
242 		*throughput = 0;
243 		break;
244 	case DP_DSC_THROUGHPUT_MODE_0_170:
245 		*throughput = 170;
246 		break;
247 	case DP_DSC_THROUGHPUT_MODE_0_340:
248 		*throughput = 340;
249 		break;
250 	case DP_DSC_THROUGHPUT_MODE_0_400:
251 		*throughput = 400;
252 		break;
253 	case DP_DSC_THROUGHPUT_MODE_0_450:
254 		*throughput = 450;
255 		break;
256 	case DP_DSC_THROUGHPUT_MODE_0_500:
257 		*throughput = 500;
258 		break;
259 	case DP_DSC_THROUGHPUT_MODE_0_550:
260 		*throughput = 550;
261 		break;
262 	case DP_DSC_THROUGHPUT_MODE_0_600:
263 		*throughput = 600;
264 		break;
265 	case DP_DSC_THROUGHPUT_MODE_0_650:
266 		*throughput = 650;
267 		break;
268 	case DP_DSC_THROUGHPUT_MODE_0_700:
269 		*throughput = 700;
270 		break;
271 	case DP_DSC_THROUGHPUT_MODE_0_750:
272 		*throughput = 750;
273 		break;
274 	case DP_DSC_THROUGHPUT_MODE_0_800:
275 		*throughput = 800;
276 		break;
277 	case DP_DSC_THROUGHPUT_MODE_0_850:
278 		*throughput = 850;
279 		break;
280 	case DP_DSC_THROUGHPUT_MODE_0_900:
281 		*throughput = 900;
282 		break;
283 	case DP_DSC_THROUGHPUT_MODE_0_950:
284 		*throughput = 950;
285 		break;
286 	case DP_DSC_THROUGHPUT_MODE_0_1000:
287 		*throughput = 1000;
288 		break;
289 	default: {
290 			dm_error("%s: DPCD DSC throughput mode not recognized.\n", __func__);
291 			return false;
292 		}
293 	}
294 
295 	return true;
296 }
297 
298 
299 static bool dsc_bpp_increment_div_from_dpcd(uint8_t bpp_increment_dpcd, uint32_t *bpp_increment_div)
300 {
301 	// Mask bpp increment dpcd field to avoid reading other fields
302 	bpp_increment_dpcd &= 0x7;
303 
304 	switch (bpp_increment_dpcd) {
305 	case 0:
306 		*bpp_increment_div = 16;
307 		break;
308 	case 1:
309 		*bpp_increment_div = 8;
310 		break;
311 	case 2:
312 		*bpp_increment_div = 4;
313 		break;
314 	case 3:
315 		*bpp_increment_div = 2;
316 		break;
317 	case 4:
318 		*bpp_increment_div = 1;
319 		break;
320 	default: {
321 		dm_error("%s: DPCD DSC bits-per-pixel increment not recognized.\n", __func__);
322 		return false;
323 	}
324 	}
325 
326 	return true;
327 }
328 
329 
330 
331 bool dc_dsc_parse_dsc_dpcd(const struct dc *dc,
332 		const uint8_t *dpcd_dsc_basic_data,
333 		const uint8_t *dpcd_dsc_branch_decoder_caps,
334 		struct dsc_dec_dpcd_caps *dsc_sink_caps)
335 {
336 	if (!dpcd_dsc_basic_data)
337 		return false;
338 
339 	dsc_sink_caps->is_dsc_supported =
340 		(dpcd_dsc_basic_data[DP_DSC_SUPPORT - DP_DSC_SUPPORT] & DP_DSC_DECOMPRESSION_IS_SUPPORTED) != 0;
341 	if (!dsc_sink_caps->is_dsc_supported)
342 		return false;
343 
344 	dsc_sink_caps->dsc_version = dpcd_dsc_basic_data[DP_DSC_REV - DP_DSC_SUPPORT];
345 
346 	{
347 		int buff_block_size;
348 		int buff_size;
349 
350 		if (!dsc_buff_block_size_from_dpcd(
351 				dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT] & 0x03,
352 				&buff_block_size))
353 			return false;
354 
355 		buff_size = dpcd_dsc_basic_data[DP_DSC_RC_BUF_SIZE - DP_DSC_SUPPORT] + 1;
356 		dsc_sink_caps->rc_buffer_size = buff_size * buff_block_size;
357 	}
358 
359 	dsc_sink_caps->slice_caps1.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_1 - DP_DSC_SUPPORT];
360 	if (!dsc_line_buff_depth_from_dpcd(dpcd_dsc_basic_data[DP_DSC_LINE_BUF_BIT_DEPTH - DP_DSC_SUPPORT],
361 									   &dsc_sink_caps->lb_bit_depth))
362 		return false;
363 
364 	dsc_sink_caps->is_block_pred_supported =
365 		(dpcd_dsc_basic_data[DP_DSC_BLK_PREDICTION_SUPPORT - DP_DSC_SUPPORT] &
366 		 DP_DSC_BLK_PREDICTION_IS_SUPPORTED) != 0;
367 
368 	dsc_sink_caps->edp_max_bits_per_pixel =
369 		dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_LOW - DP_DSC_SUPPORT] |
370 		dpcd_dsc_basic_data[DP_DSC_MAX_BITS_PER_PIXEL_HI - DP_DSC_SUPPORT] << 8;
371 
372 	dsc_sink_caps->color_formats.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_FORMAT_CAP - DP_DSC_SUPPORT];
373 	dsc_sink_caps->color_depth.raw = dpcd_dsc_basic_data[DP_DSC_DEC_COLOR_DEPTH_CAP - DP_DSC_SUPPORT];
374 
375 	{
376 		int dpcd_throughput = dpcd_dsc_basic_data[DP_DSC_PEAK_THROUGHPUT - DP_DSC_SUPPORT];
377 		int dsc_throughput_granular_delta;
378 
379 		dsc_throughput_granular_delta = dpcd_dsc_basic_data[DP_DSC_RC_BUF_BLK_SIZE - DP_DSC_SUPPORT] >> 3;
380 		dsc_throughput_granular_delta *= 2;
381 
382 		if (!dsc_throughput_from_dpcd(dpcd_throughput & DP_DSC_THROUGHPUT_MODE_0_MASK,
383 									  &dsc_sink_caps->throughput_mode_0_mps))
384 			return false;
385 		dsc_sink_caps->throughput_mode_0_mps += dsc_throughput_granular_delta;
386 
387 		dpcd_throughput = (dpcd_throughput & DP_DSC_THROUGHPUT_MODE_1_MASK) >> DP_DSC_THROUGHPUT_MODE_1_SHIFT;
388 		if (!dsc_throughput_from_dpcd(dpcd_throughput, &dsc_sink_caps->throughput_mode_1_mps))
389 			return false;
390 	}
391 
392 	dsc_sink_caps->max_slice_width = dpcd_dsc_basic_data[DP_DSC_MAX_SLICE_WIDTH - DP_DSC_SUPPORT] * 320;
393 	dsc_sink_caps->slice_caps2.raw = dpcd_dsc_basic_data[DP_DSC_SLICE_CAP_2 - DP_DSC_SUPPORT];
394 
395 	if (!dsc_bpp_increment_div_from_dpcd(dpcd_dsc_basic_data[DP_DSC_BITS_PER_PIXEL_INC - DP_DSC_SUPPORT],
396 										 &dsc_sink_caps->bpp_increment_div))
397 		return false;
398 
399 	if (dc->debug.dsc_bpp_increment_div) {
400 		/* dsc_bpp_increment_div should onl be 1, 2, 4, 8 or 16, but rather than rejecting invalid values,
401 		 * we'll accept all and get it into range. This also makes the above check against 0 redundant,
402 		 * but that one stresses out the override will be only used if it's not 0.
403 		 */
404 		if (dc->debug.dsc_bpp_increment_div >= 1)
405 			dsc_sink_caps->bpp_increment_div = 1;
406 		if (dc->debug.dsc_bpp_increment_div >= 2)
407 			dsc_sink_caps->bpp_increment_div = 2;
408 		if (dc->debug.dsc_bpp_increment_div >= 4)
409 			dsc_sink_caps->bpp_increment_div = 4;
410 		if (dc->debug.dsc_bpp_increment_div >= 8)
411 			dsc_sink_caps->bpp_increment_div = 8;
412 		if (dc->debug.dsc_bpp_increment_div >= 16)
413 			dsc_sink_caps->bpp_increment_div = 16;
414 	}
415 
416 	/* Extended caps */
417 	if (dpcd_dsc_branch_decoder_caps == NULL) { // branch decoder DPCD DSC data can be null for non branch device
418 		dsc_sink_caps->branch_overall_throughput_0_mps = 0;
419 		dsc_sink_caps->branch_overall_throughput_1_mps = 0;
420 		dsc_sink_caps->branch_max_line_width = 0;
421 		return true;
422 	}
423 
424 	dsc_sink_caps->branch_overall_throughput_0_mps =
425 		dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_0 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];
426 	if (dsc_sink_caps->branch_overall_throughput_0_mps == 0)
427 		dsc_sink_caps->branch_overall_throughput_0_mps = 0;
428 	else if (dsc_sink_caps->branch_overall_throughput_0_mps == 1)
429 		dsc_sink_caps->branch_overall_throughput_0_mps = 680;
430 	else {
431 		dsc_sink_caps->branch_overall_throughput_0_mps *= 50;
432 		dsc_sink_caps->branch_overall_throughput_0_mps += 600;
433 	}
434 
435 	dsc_sink_caps->branch_overall_throughput_1_mps =
436 		dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_OVERALL_THROUGHPUT_1 - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0];
437 	if (dsc_sink_caps->branch_overall_throughput_1_mps == 0)
438 		dsc_sink_caps->branch_overall_throughput_1_mps = 0;
439 	else if (dsc_sink_caps->branch_overall_throughput_1_mps == 1)
440 		dsc_sink_caps->branch_overall_throughput_1_mps = 680;
441 	else {
442 		dsc_sink_caps->branch_overall_throughput_1_mps *= 50;
443 		dsc_sink_caps->branch_overall_throughput_1_mps += 600;
444 	}
445 
446 	dsc_sink_caps->branch_max_line_width =
447 		dpcd_dsc_branch_decoder_caps[DP_DSC_BRANCH_MAX_LINE_WIDTH - DP_DSC_BRANCH_OVERALL_THROUGHPUT_0] * 320;
448 	ASSERT(dsc_sink_caps->branch_max_line_width == 0 || dsc_sink_caps->branch_max_line_width >= 5120);
449 
450 	dsc_sink_caps->is_dp = true;
451 	return true;
452 }
453 
454 /* If DSC is possbile, get DSC bandwidth range based on [min_bpp, max_bpp] target bitrate range and
455  * timing's pixel clock and uncompressed bandwidth.
456  * If DSC is not possible, leave '*range' untouched.
457  */
458 bool dc_dsc_compute_bandwidth_range(
459 		const struct display_stream_compressor *dsc,
460 		uint32_t dsc_min_slice_height_override,
461 		uint32_t min_bpp_x16,
462 		uint32_t max_bpp_x16,
463 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
464 		const struct dc_crtc_timing *timing,
465 		const enum dc_link_encoding_format link_encoding,
466 		struct dc_dsc_bw_range *range)
467 {
468 	bool is_dsc_possible = false;
469 	unsigned int min_dsc_slice_count;
470 	struct dsc_enc_caps dsc_enc_caps;
471 	struct dsc_enc_caps dsc_common_caps;
472 	struct dc_dsc_config config = {0};
473 	struct dc_dsc_config_options options = {0};
474 
475 	options.dsc_min_slice_height_override = dsc_min_slice_height_override;
476 	options.max_target_bpp_limit_override_x16 = max_bpp_x16;
477 	options.slice_height_granularity = 1;
478 
479 	get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
480 
481 	min_dsc_slice_count = get_min_dsc_slice_count_for_odm(dsc, &dsc_enc_caps, timing);
482 
483 	is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, &dsc_enc_caps,
484 			timing->pixel_encoding, &dsc_common_caps);
485 
486 	if (is_dsc_possible)
487 		is_dsc_possible = setup_dsc_config(dsc_sink_caps, &dsc_enc_caps, 0, timing,
488 				&options, link_encoding, min_dsc_slice_count, &config);
489 
490 	if (is_dsc_possible)
491 		is_dsc_possible = decide_dsc_bandwidth_range(min_bpp_x16, max_bpp_x16,
492 				config.num_slices_h, &dsc_common_caps, timing, link_encoding, range);
493 
494 	return is_dsc_possible;
495 }
496 
497 void dc_dsc_dump_encoder_caps(const struct display_stream_compressor *dsc,
498 			      const struct dc_crtc_timing *timing)
499 {
500 	struct dsc_enc_caps dsc_enc_caps;
501 
502 	get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
503 
504 	DC_LOG_DSC("dsc encoder caps:");
505 	DC_LOG_DSC("\tdsc_version 0x%x", dsc_enc_caps.dsc_version);
506 	DC_LOG_DSC("\tslice_caps 0x%x", dsc_enc_caps.slice_caps.raw);
507 	DC_LOG_DSC("\tlb_bit_depth %d", dsc_enc_caps.lb_bit_depth);
508 	DC_LOG_DSC("\tis_block_pred_supported %d", dsc_enc_caps.is_block_pred_supported);
509 	DC_LOG_DSC("\tcolor_formats 0x%x", dsc_enc_caps.color_formats.raw);
510 	DC_LOG_DSC("\tcolor_depth 0x%x", dsc_enc_caps.color_depth.raw);
511 	DC_LOG_DSC("\tmax_total_throughput_mps %d", dsc_enc_caps.max_total_throughput_mps);
512 	DC_LOG_DSC("\tmax_slice_width %d", dsc_enc_caps.max_slice_width);
513 	DC_LOG_DSC("\tbpp_increment_div %d", dsc_enc_caps.bpp_increment_div);
514 }
515 
516 void dc_dsc_dump_decoder_caps(const struct display_stream_compressor *dsc,
517 			      const struct dsc_dec_dpcd_caps *dsc_sink_caps)
518 {
519 	DC_LOG_DSC("dsc decoder caps:");
520 	DC_LOG_DSC("\tis_dsc_supported %d", dsc_sink_caps->is_dsc_supported);
521 	DC_LOG_DSC("\tdsc_version 0x%x", dsc_sink_caps->dsc_version);
522 	DC_LOG_DSC("\trc_buffer_size %d", dsc_sink_caps->rc_buffer_size);
523 	DC_LOG_DSC("\tslice_caps1 0x%x", dsc_sink_caps->slice_caps1.raw);
524 	DC_LOG_DSC("\tslice_caps2 0x%x", dsc_sink_caps->slice_caps2.raw);
525 	DC_LOG_DSC("\tlb_bit_depth %d", dsc_sink_caps->lb_bit_depth);
526 	DC_LOG_DSC("\tis_block_pred_supported %d", dsc_sink_caps->is_block_pred_supported);
527 	DC_LOG_DSC("\tedp_max_bits_per_pixel %d", dsc_sink_caps->edp_max_bits_per_pixel);
528 	DC_LOG_DSC("\tcolor_formats 0x%x", dsc_sink_caps->color_formats.raw);
529 	DC_LOG_DSC("\tthroughput_mode_0_mps %d", dsc_sink_caps->throughput_mode_0_mps);
530 	DC_LOG_DSC("\tthroughput_mode_1_mps %d", dsc_sink_caps->throughput_mode_1_mps);
531 	DC_LOG_DSC("\tmax_slice_width %d", dsc_sink_caps->max_slice_width);
532 	DC_LOG_DSC("\tbpp_increment_div %d", dsc_sink_caps->bpp_increment_div);
533 	DC_LOG_DSC("\tbranch_overall_throughput_0_mps %d", dsc_sink_caps->branch_overall_throughput_0_mps);
534 	DC_LOG_DSC("\tbranch_overall_throughput_1_mps %d", dsc_sink_caps->branch_overall_throughput_1_mps);
535 	DC_LOG_DSC("\tbranch_max_line_width %d", dsc_sink_caps->branch_max_line_width);
536 	DC_LOG_DSC("\tis_dp %d", dsc_sink_caps->is_dp);
537 }
538 
539 
540 static void build_dsc_enc_combined_slice_caps(
541 		const struct dsc_enc_caps *single_dsc_enc_caps,
542 		struct dsc_enc_caps *dsc_enc_caps,
543 		unsigned int max_odm_combine_factor)
544 {
545 	/* 1-16 slice configurations, single DSC */
546 	dsc_enc_caps->slice_caps.raw |= single_dsc_enc_caps->slice_caps.raw;
547 
548 	/* 2x DSC's */
549 	if (max_odm_combine_factor >= 2) {
550 		/* 1 + 1 */
551 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_2 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_1;
552 
553 		/* 2 + 2 */
554 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_4 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_2;
555 
556 		/* 4 + 4 */
557 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_8 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_4;
558 
559 		/* 8 + 8 */
560 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_16 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_8;
561 	}
562 
563 	/* 3x DSC's */
564 	if (max_odm_combine_factor >= 3) {
565 		/* 4 + 4 + 4 */
566 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_12 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_4;
567 	}
568 
569 	/* 4x DSC's */
570 	if (max_odm_combine_factor >= 4) {
571 		/* 1 + 1 + 1 + 1 */
572 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_4 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_1;
573 
574 		/* 2 + 2 + 2 + 2 */
575 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_8 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_2;
576 
577 		/* 3 + 3 + 3 + 3 */
578 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_12 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_3;
579 
580 		/* 4 + 4 + 4 + 4 */
581 		dsc_enc_caps->slice_caps.bits.NUM_SLICES_16 |= single_dsc_enc_caps->slice_caps.bits.NUM_SLICES_4;
582 	}
583 }
584 
585 static void build_dsc_enc_caps(
586 		const struct display_stream_compressor *dsc,
587 		struct dsc_enc_caps *dsc_enc_caps)
588 {
589 	unsigned int max_dscclk_khz;
590 	unsigned int num_dsc;
591 	unsigned int max_odm_combine_factor;
592 	struct dsc_enc_caps single_dsc_enc_caps;
593 
594 	struct dc *dc;
595 
596 	if (!dsc || !dsc->ctx || !dsc->ctx->dc || !dsc->funcs->dsc_get_single_enc_caps)
597 		return;
598 
599 	dc = dsc->ctx->dc;
600 
601 	if (!dc->clk_mgr || !dc->clk_mgr->funcs->get_max_clock_khz || !dc->res_pool || dc->debug.disable_dsc)
602 		return;
603 
604 	/* get max DSCCLK from clk_mgr */
605 	max_dscclk_khz = dc->clk_mgr->funcs->get_max_clock_khz(dc->clk_mgr, CLK_TYPE_DSCCLK);
606 
607 	dsc->funcs->dsc_get_single_enc_caps(&single_dsc_enc_caps, max_dscclk_khz);
608 
609 	/* global capabilities */
610 	dsc_enc_caps->dsc_version = single_dsc_enc_caps.dsc_version;
611 	dsc_enc_caps->lb_bit_depth = single_dsc_enc_caps.lb_bit_depth;
612 	dsc_enc_caps->is_block_pred_supported = single_dsc_enc_caps.is_block_pred_supported;
613 	dsc_enc_caps->max_slice_width = single_dsc_enc_caps.max_slice_width;
614 	dsc_enc_caps->bpp_increment_div = single_dsc_enc_caps.bpp_increment_div;
615 	dsc_enc_caps->color_formats.raw = single_dsc_enc_caps.color_formats.raw;
616 	dsc_enc_caps->color_depth.raw = single_dsc_enc_caps.color_depth.raw;
617 
618 	/* expand per DSC capabilities to global */
619 	max_odm_combine_factor = dc->caps.max_odm_combine_factor;
620 	num_dsc = dc->res_pool->res_cap->num_dsc;
621 	max_odm_combine_factor = min(max_odm_combine_factor, num_dsc);
622 	dsc_enc_caps->max_total_throughput_mps =
623 			single_dsc_enc_caps.max_total_throughput_mps *
624 			max_odm_combine_factor;
625 
626 	/* check slice counts possible for with ODM combine */
627 	build_dsc_enc_combined_slice_caps(&single_dsc_enc_caps, dsc_enc_caps, max_odm_combine_factor);
628 }
629 
630 static inline uint32_t dsc_div_by_10_round_up(uint32_t value)
631 {
632 	return (value + 9) / 10;
633 }
634 
635 static unsigned int get_min_dsc_slice_count_for_odm(
636 		const struct display_stream_compressor *dsc,
637 		const struct dsc_enc_caps *dsc_enc_caps,
638 		const struct dc_crtc_timing *timing)
639 {
640 	unsigned int max_dispclk_khz;
641 
642 	/* get max pixel rate and combine caps */
643 	max_dispclk_khz = dsc_enc_caps->max_total_throughput_mps * 1000;
644 	if (dsc && dsc->ctx->dc) {
645 		if (dsc->ctx->dc->clk_mgr &&
646 			dsc->ctx->dc->clk_mgr->funcs->get_max_clock_khz) {
647 			/* dispclk is available */
648 			max_dispclk_khz = dsc->ctx->dc->clk_mgr->funcs->get_max_clock_khz(dsc->ctx->dc->clk_mgr, CLK_TYPE_DISPCLK);
649 		}
650 	}
651 
652 	/* validate parameters */
653 	if (max_dispclk_khz == 0 || dsc_enc_caps->max_slice_width == 0)
654 		return 1;
655 
656 	/* consider minimum odm slices required due to
657 	 * 1) display pipe throughput (dispclk)
658 	 * 2) max image width per slice
659 	 */
660 	return dc_fixpt_ceil(dc_fixpt_max(
661 			dc_fixpt_div_int(dc_fixpt_from_int(dsc_div_by_10_round_up(timing->pix_clk_100hz)),
662 			max_dispclk_khz), // throughput
663 			dc_fixpt_div_int(dc_fixpt_from_int(timing->h_addressable + timing->h_border_left + timing->h_border_right),
664 			dsc_enc_caps->max_slice_width))); // slice width
665 }
666 
667 static void get_dsc_enc_caps(
668 		const struct display_stream_compressor *dsc,
669 		struct dsc_enc_caps *dsc_enc_caps,
670 		int pixel_clock_100Hz)
671 {
672 	memset(dsc_enc_caps, 0, sizeof(struct dsc_enc_caps));
673 
674 	if (!dsc || !dsc->ctx || !dsc->ctx->dc || dsc->ctx->dc->debug.disable_dsc)
675 		return;
676 
677 	/* check if reported cap global or only for a single DCN DSC enc */
678 	if (dsc->funcs->dsc_get_enc_caps) {
679 		dsc->funcs->dsc_get_enc_caps(dsc_enc_caps, pixel_clock_100Hz);
680 	} else {
681 		build_dsc_enc_caps(dsc, dsc_enc_caps);
682 	}
683 }
684 
685 /* Returns 'false' if no intersection was found for at least one capability.
686  * It also implicitly validates some sink caps against invalid value of zero.
687  */
688 static bool intersect_dsc_caps(
689 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
690 		const struct dsc_enc_caps *dsc_enc_caps,
691 		enum dc_pixel_encoding pixel_encoding,
692 		struct dsc_enc_caps *dsc_common_caps)
693 {
694 	int32_t max_slices;
695 	int32_t total_sink_throughput;
696 
697 	memset(dsc_common_caps, 0, sizeof(struct dsc_enc_caps));
698 
699 	dsc_common_caps->dsc_version = min(dsc_sink_caps->dsc_version, dsc_enc_caps->dsc_version);
700 	if (!dsc_common_caps->dsc_version)
701 		return false;
702 
703 	dsc_common_caps->slice_caps.bits.NUM_SLICES_1 =
704 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_1 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_1;
705 	dsc_common_caps->slice_caps.bits.NUM_SLICES_2 =
706 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_2 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_2;
707 	dsc_common_caps->slice_caps.bits.NUM_SLICES_4 =
708 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_4 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_4;
709 	dsc_common_caps->slice_caps.bits.NUM_SLICES_8 =
710 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_8 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_8;
711 	dsc_common_caps->slice_caps.bits.NUM_SLICES_12 =
712 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_12 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_12;
713 	dsc_common_caps->slice_caps.bits.NUM_SLICES_16 =
714 		dsc_sink_caps->slice_caps2.bits.NUM_SLICES_16 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_16;
715 
716 	if (!dsc_common_caps->slice_caps.raw)
717 		return false;
718 
719 	dsc_common_caps->lb_bit_depth = min(dsc_sink_caps->lb_bit_depth, dsc_enc_caps->lb_bit_depth);
720 	if (!dsc_common_caps->lb_bit_depth)
721 		return false;
722 
723 	dsc_common_caps->is_block_pred_supported =
724 		dsc_sink_caps->is_block_pred_supported && dsc_enc_caps->is_block_pred_supported;
725 
726 	dsc_common_caps->color_formats.raw = dsc_sink_caps->color_formats.raw & dsc_enc_caps->color_formats.raw;
727 	if (!dsc_common_caps->color_formats.raw)
728 		return false;
729 
730 	dsc_common_caps->color_depth.raw = dsc_sink_caps->color_depth.raw & dsc_enc_caps->color_depth.raw;
731 	if (!dsc_common_caps->color_depth.raw)
732 		return false;
733 
734 	max_slices = 0;
735 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_1)
736 		max_slices = 1;
737 
738 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_2)
739 		max_slices = 2;
740 
741 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_4)
742 		max_slices = 4;
743 
744 	total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_0_mps;
745 	if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
746 		total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_1_mps;
747 
748 	dsc_common_caps->max_total_throughput_mps = min(total_sink_throughput, dsc_enc_caps->max_total_throughput_mps);
749 
750 	dsc_common_caps->max_slice_width = min(dsc_sink_caps->max_slice_width, dsc_enc_caps->max_slice_width);
751 	if (!dsc_common_caps->max_slice_width)
752 		return false;
753 
754 	dsc_common_caps->bpp_increment_div = min(dsc_sink_caps->bpp_increment_div, dsc_enc_caps->bpp_increment_div);
755 
756 	// TODO DSC: Remove this workaround for N422 and 420 once it's fixed, or move it to get_dsc_encoder_caps()
757 	if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
758 		dsc_common_caps->bpp_increment_div = min(dsc_common_caps->bpp_increment_div, (uint32_t)8);
759 
760 	dsc_common_caps->edp_sink_max_bits_per_pixel = dsc_sink_caps->edp_max_bits_per_pixel;
761 	dsc_common_caps->is_dp = dsc_sink_caps->is_dp;
762 	return true;
763 }
764 
765 static uint32_t compute_bpp_x16_from_target_bandwidth(
766 	const uint32_t bandwidth_in_kbps,
767 	const struct dc_crtc_timing *timing,
768 	const uint32_t num_slices_h,
769 	const uint32_t bpp_increment_div,
770 	const bool is_dp)
771 {
772 	uint32_t overhead_in_kbps;
773 	struct fixed31_32 effective_bandwidth_in_kbps;
774 	struct fixed31_32 bpp_x16;
775 
776 	overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
777 				timing, num_slices_h, is_dp);
778 	effective_bandwidth_in_kbps = dc_fixpt_from_int(bandwidth_in_kbps);
779 	effective_bandwidth_in_kbps = dc_fixpt_sub_int(effective_bandwidth_in_kbps,
780 			overhead_in_kbps);
781 	bpp_x16 = dc_fixpt_mul_int(effective_bandwidth_in_kbps, 10);
782 	bpp_x16 = dc_fixpt_div_int(bpp_x16, timing->pix_clk_100hz);
783 	bpp_x16 = dc_fixpt_from_int(dc_fixpt_floor(dc_fixpt_mul_int(bpp_x16, bpp_increment_div)));
784 	bpp_x16 = dc_fixpt_div_int(bpp_x16, bpp_increment_div);
785 	bpp_x16 = dc_fixpt_mul_int(bpp_x16, 16);
786 	return dc_fixpt_floor(bpp_x16);
787 }
788 
789 /* Decide DSC bandwidth range based on signal, timing, specs specific and input min and max
790  * requirements.
791  * The range output includes decided min/max target bpp, the respective bandwidth requirements
792  * and native timing bandwidth requirement when DSC is not used.
793  */
794 static bool decide_dsc_bandwidth_range(
795 		const uint32_t min_bpp_x16,
796 		const uint32_t max_bpp_x16,
797 		const uint32_t num_slices_h,
798 		const struct dsc_enc_caps *dsc_caps,
799 		const struct dc_crtc_timing *timing,
800 		const enum dc_link_encoding_format link_encoding,
801 		struct dc_dsc_bw_range *range)
802 {
803 	uint32_t preferred_bpp_x16 = timing->dsc_fixed_bits_per_pixel_x16;
804 
805 	memset(range, 0, sizeof(*range));
806 
807 	/* apply signal, timing, specs and explicitly specified DSC range requirements */
808 	if (preferred_bpp_x16) {
809 		if (preferred_bpp_x16 <= max_bpp_x16 &&
810 				preferred_bpp_x16 >= min_bpp_x16) {
811 			range->max_target_bpp_x16 = preferred_bpp_x16;
812 			range->min_target_bpp_x16 = preferred_bpp_x16;
813 		}
814 	}
815 	/* TODO - make this value generic to all signal types */
816 	else if (dsc_caps->edp_sink_max_bits_per_pixel) {
817 		/* apply max bpp limitation from edp sink */
818 		range->max_target_bpp_x16 = MIN(dsc_caps->edp_sink_max_bits_per_pixel,
819 				max_bpp_x16);
820 		range->min_target_bpp_x16 = min_bpp_x16;
821 	}
822 	else {
823 		range->max_target_bpp_x16 = max_bpp_x16;
824 		range->min_target_bpp_x16 = min_bpp_x16;
825 	}
826 
827 	/* populate output structure */
828 	if (range->max_target_bpp_x16 >= range->min_target_bpp_x16 && range->min_target_bpp_x16 > 0) {
829 		/* native stream bandwidth */
830 		range->stream_kbps = dc_bandwidth_in_kbps_from_timing(timing, link_encoding);
831 
832 		/* max dsc target bpp */
833 		range->max_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
834 				range->max_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
835 
836 		/* min dsc target bpp */
837 		range->min_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
838 				range->min_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
839 	}
840 
841 	return range->max_kbps >= range->min_kbps && range->min_kbps > 0;
842 }
843 
844 /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy.
845  *
846  * Returns:
847  *     - 'true' if target bpp is decided
848  *     - 'false' if target bpp cannot be decided (e.g. cannot fit even with min DSC bpp),
849  */
850 static bool decide_dsc_target_bpp_x16(
851 		const struct dc_dsc_policy *policy,
852 		const struct dc_dsc_config_options *options,
853 		const struct dsc_enc_caps *dsc_common_caps,
854 		const int target_bandwidth_kbps,
855 		const struct dc_crtc_timing *timing,
856 		const int num_slices_h,
857 		const enum dc_link_encoding_format link_encoding,
858 		int *target_bpp_x16)
859 {
860 	struct dc_dsc_bw_range range;
861 	uint32_t target_bandwidth_kbps_u = (uint32_t)target_bandwidth_kbps;
862 
863 	*target_bpp_x16 = 0;
864 
865 	if (decide_dsc_bandwidth_range(policy->min_target_bpp * 16, policy->max_target_bpp * 16,
866 			num_slices_h, dsc_common_caps, timing, link_encoding, &range)) {
867 		if (target_bandwidth_kbps_u >= range.stream_kbps) {
868 			if (policy->enable_dsc_when_not_needed || options->force_dsc_when_not_needed)
869 				/* enable max bpp even dsc is not needed */
870 				*target_bpp_x16 = range.max_target_bpp_x16;
871 		} else if (target_bandwidth_kbps_u >= range.max_kbps) {
872 			/* use max target bpp allowed */
873 			*target_bpp_x16 = range.max_target_bpp_x16;
874 		} else if (target_bandwidth_kbps_u >= range.min_kbps) {
875 			/* use target bpp that can take entire target bandwidth */
876 			*target_bpp_x16 = compute_bpp_x16_from_target_bandwidth(
877 					target_bandwidth_kbps, timing, num_slices_h,
878 					dsc_common_caps->bpp_increment_div,
879 					dsc_common_caps->is_dp);
880 		}
881 	}
882 
883 	return *target_bpp_x16 != 0;
884 }
885 
886 #define MIN_AVAILABLE_SLICES_SIZE  6
887 
888 static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps, int *available_slices)
889 {
890 	int idx = 0;
891 
892 	if (slice_caps.bits.NUM_SLICES_1)
893 		available_slices[idx++] = 1;
894 
895 	if (slice_caps.bits.NUM_SLICES_2)
896 		available_slices[idx++] = 2;
897 
898 	if (slice_caps.bits.NUM_SLICES_4)
899 		available_slices[idx++] = 4;
900 
901 	if (slice_caps.bits.NUM_SLICES_8)
902 		available_slices[idx++] = 8;
903 
904 	if (slice_caps.bits.NUM_SLICES_12)
905 		available_slices[idx++] = 12;
906 
907 	if (slice_caps.bits.NUM_SLICES_16)
908 		available_slices[idx++] = 16;
909 
910 	return idx;
911 }
912 
913 
914 static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps)
915 {
916 	int max_slices = 0;
917 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
918 	int end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
919 
920 	if (end_idx > 0)
921 		max_slices = available_slices[end_idx - 1];
922 
923 	return max_slices;
924 }
925 
926 
927 // Increment slice number in available slice numbers stops if possible, or just increment if not
928 static int inc_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
929 {
930 	// Get next bigger num slices available in common caps
931 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
932 	int end_idx;
933 	int i;
934 	int new_num_slices = num_slices;
935 
936 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
937 	if (end_idx == 0) {
938 		// No available slices found
939 		new_num_slices++;
940 		return new_num_slices;
941 	}
942 
943 	// Numbers of slices found - get the next bigger number
944 	for (i = 0; i < end_idx; i++) {
945 		if (new_num_slices < available_slices[i]) {
946 			new_num_slices = available_slices[i];
947 			break;
948 		}
949 	}
950 
951 	if (new_num_slices == num_slices) // No bigger number of slices found
952 		new_num_slices++;
953 
954 	return new_num_slices;
955 }
956 
957 
958 // Decrement slice number in available slice numbers stops if possible, or just decrement if not. Stop at zero.
959 static int dec_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
960 {
961 	// Get next bigger num slices available in common caps
962 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
963 	int end_idx;
964 	int i;
965 	int new_num_slices = num_slices;
966 
967 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
968 	if (end_idx == 0 && new_num_slices > 0) {
969 		// No numbers of slices found
970 		new_num_slices++;
971 		return new_num_slices;
972 	}
973 
974 	// Numbers of slices found - get the next smaller number
975 	for (i = end_idx - 1; i >= 0; i--) {
976 		if (new_num_slices > available_slices[i]) {
977 			new_num_slices = available_slices[i];
978 			break;
979 		}
980 	}
981 
982 	if (new_num_slices == num_slices) {
983 		// No smaller number of slices found
984 		new_num_slices--;
985 		if (new_num_slices < 0)
986 			new_num_slices = 0;
987 	}
988 
989 	return new_num_slices;
990 }
991 
992 
993 // Choose next bigger number of slices if the requested number of slices is not available
994 static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps, int num_slices)
995 {
996 	// Get next bigger num slices available in common caps
997 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
998 	int end_idx;
999 	int i;
1000 	int new_num_slices = num_slices;
1001 
1002 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
1003 	if (end_idx == 0) {
1004 		// No available slices found
1005 		new_num_slices++;
1006 		return new_num_slices;
1007 	}
1008 
1009 	// Numbers of slices found - get the equal or next bigger number
1010 	for (i = 0; i < end_idx; i++) {
1011 		if (new_num_slices <= available_slices[i]) {
1012 			new_num_slices = available_slices[i];
1013 			break;
1014 		}
1015 	}
1016 
1017 	return new_num_slices;
1018 }
1019 
1020 
1021 /* Attempts to set DSC configuration for the stream, applying DSC policy.
1022  * Returns 'true' if successful or 'false' if not.
1023  *
1024  * Parameters:
1025  *
1026  * dsc_sink_caps       - DSC sink decoder capabilities (from DPCD)
1027  *
1028  * dsc_enc_caps        - DSC encoder capabilities
1029  *
1030  * target_bandwidth_kbps  - Target bandwidth to fit the stream into.
1031  *                          If 0, do not calculate target bpp.
1032  *
1033  * timing              - The stream timing to fit into 'target_bandwidth_kbps' or apply
1034  *                       maximum compression to, if 'target_badwidth == 0'
1035  *
1036  * dsc_cfg             - DSC configuration to use if it was possible to come up with
1037  *                       one for the given inputs.
1038  *                       The target bitrate after DSC can be calculated by multiplying
1039  *                       dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g.
1040  *
1041  *                       dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0);
1042  */
1043 static bool setup_dsc_config(
1044 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
1045 		const struct dsc_enc_caps *dsc_enc_caps,
1046 		int target_bandwidth_kbps,
1047 		const struct dc_crtc_timing *timing,
1048 		const struct dc_dsc_config_options *options,
1049 		const enum dc_link_encoding_format link_encoding,
1050 		int min_slices_h,
1051 		struct dc_dsc_config *dsc_cfg)
1052 {
1053 	struct dsc_enc_caps dsc_common_caps;
1054 	int max_slices_h = 0;
1055 	int num_slices_h = 0;
1056 	int pic_width;
1057 	uint32_t pic_width_u;
1058 	int slice_width;
1059 	int target_bpp;
1060 	int sink_per_slice_throughput_mps;
1061 	uint32_t branch_max_throughput_mps = 0;
1062 	bool is_dsc_possible = false;
1063 	int pic_height;
1064 	int slice_height;
1065 	struct dc_dsc_policy policy;
1066 
1067 	memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
1068 
1069 	dc_dsc_get_policy_for_timing(timing, options->max_target_bpp_limit_override_x16, &policy, link_encoding);
1070 	pic_width = timing->h_addressable + timing->h_border_left + timing->h_border_right;
1071 	pic_width_u = (uint32_t)pic_width;
1072 	pic_height = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
1073 
1074 	if (!dsc_sink_caps->is_dsc_supported)
1075 		goto done;
1076 
1077 	if (dsc_sink_caps->branch_max_line_width && dsc_sink_caps->branch_max_line_width < pic_width_u)
1078 		goto done;
1079 
1080 	// Intersect decoder with encoder DSC caps and validate DSC settings
1081 	is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, dsc_enc_caps, timing->pixel_encoding, &dsc_common_caps);
1082 	if (!is_dsc_possible)
1083 		goto done;
1084 
1085 	sink_per_slice_throughput_mps = 0;
1086 
1087 	// Validate available DSC settings against the mode timing
1088 
1089 	// Validate color format (and pick up the throughput values)
1090 	dsc_cfg->ycbcr422_simple = false;
1091 	switch (timing->pixel_encoding)	{
1092 	case PIXEL_ENCODING_RGB:
1093 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.RGB;
1094 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
1095 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
1096 		break;
1097 	case PIXEL_ENCODING_YCBCR444:
1098 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_444;
1099 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
1100 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
1101 		break;
1102 	case PIXEL_ENCODING_YCBCR422:
1103 		if (policy.ycbcr422_simple) {
1104 			is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_SIMPLE_422;
1105 			dsc_cfg->ycbcr422_simple = is_dsc_possible;
1106 			sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
1107 		} else {
1108 			is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_422;
1109 			sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
1110 			branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
1111 		}
1112 		break;
1113 	case PIXEL_ENCODING_YCBCR420:
1114 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_420;
1115 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
1116 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
1117 		break;
1118 	default:
1119 		is_dsc_possible = false;
1120 	}
1121 
1122 	// Validate branch's maximum throughput
1123 	if (branch_max_throughput_mps && dsc_div_by_10_round_up(timing->pix_clk_100hz) > branch_max_throughput_mps * 1000)
1124 		is_dsc_possible = false;
1125 
1126 	if (!is_dsc_possible)
1127 		goto done;
1128 
1129 	// Color depth
1130 	switch (timing->display_color_depth) {
1131 	case COLOR_DEPTH_888:
1132 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_8_BPC;
1133 		break;
1134 	case COLOR_DEPTH_101010:
1135 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_10_BPC;
1136 		break;
1137 	case COLOR_DEPTH_121212:
1138 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_12_BPC;
1139 		break;
1140 	default:
1141 		is_dsc_possible = false;
1142 	}
1143 
1144 	if (!is_dsc_possible)
1145 		goto done;
1146 
1147 	// Slice width (i.e. number of slices per line)
1148 	max_slices_h = get_max_dsc_slices(dsc_common_caps.slice_caps);
1149 
1150 	while (max_slices_h > 0) {
1151 		if (pic_width % max_slices_h == 0)
1152 			break;
1153 
1154 		max_slices_h = dec_num_slices(dsc_common_caps.slice_caps, max_slices_h);
1155 	}
1156 
1157 	is_dsc_possible = (dsc_common_caps.max_slice_width > 0);
1158 	if (!is_dsc_possible)
1159 		goto done;
1160 
1161 	/* increase minimum slice count to meet sink slice width limitations */
1162 	min_slices_h = dc_fixpt_ceil(dc_fixpt_max(
1163 			dc_fixpt_div_int(dc_fixpt_from_int(pic_width), dsc_common_caps.max_slice_width), // sink min
1164 			dc_fixpt_from_int(min_slices_h))); // source min
1165 
1166 	min_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, min_slices_h);
1167 
1168 	/* increase minimum slice count to meet sink throughput limitations */
1169 	while (min_slices_h <= max_slices_h) {
1170 		int pix_clk_per_slice_khz = dsc_div_by_10_round_up(timing->pix_clk_100hz) / min_slices_h;
1171 		if (pix_clk_per_slice_khz <= sink_per_slice_throughput_mps * 1000)
1172 			break;
1173 
1174 		min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
1175 	}
1176 
1177 	/* increase minimum slice count to meet divisibility requirements */
1178 	while (pic_width % min_slices_h != 0 && min_slices_h <= max_slices_h) {
1179 		min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
1180 	}
1181 
1182 	is_dsc_possible = (min_slices_h <= max_slices_h) && max_slices_h != 0;
1183 	if (!is_dsc_possible)
1184 		goto done;
1185 
1186 	if (policy.use_min_slices_h) {
1187 		if (min_slices_h > 0)
1188 			num_slices_h = min_slices_h;
1189 		else if (max_slices_h > 0) { // Fall back to max slices if min slices is not working out
1190 			if (policy.max_slices_h)
1191 				num_slices_h = min(policy.max_slices_h, max_slices_h);
1192 			else
1193 				num_slices_h = max_slices_h;
1194 		} else
1195 			is_dsc_possible = false;
1196 	} else {
1197 		if (max_slices_h > 0) {
1198 			if (policy.max_slices_h)
1199 				num_slices_h = min(policy.max_slices_h, max_slices_h);
1200 			else
1201 				num_slices_h = max_slices_h;
1202 		} else if (min_slices_h > 0) // Fall back to min slices if max slices is not possible
1203 			num_slices_h = min_slices_h;
1204 		else
1205 			is_dsc_possible = false;
1206 	}
1207 	// When we force ODM, num dsc h slices must be divisible by num odm h slices
1208 	switch (options->dsc_force_odm_hslice_override) {
1209 	case 0:
1210 	case 1:
1211 		break;
1212 	case 2:
1213 		if (num_slices_h < 2)
1214 			num_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, 2);
1215 		break;
1216 	case 3:
1217 		if (dsc_common_caps.slice_caps.bits.NUM_SLICES_12)
1218 			num_slices_h = 12;
1219 		else
1220 			num_slices_h = 0;
1221 		break;
1222 	case 4:
1223 		if (num_slices_h < 4)
1224 			num_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, 4);
1225 		break;
1226 	default:
1227 		break;
1228 	}
1229 	if (num_slices_h == 0)
1230 		is_dsc_possible = false;
1231 	if (!is_dsc_possible)
1232 		goto done;
1233 
1234 	dsc_cfg->num_slices_h = num_slices_h;
1235 	slice_width = pic_width / num_slices_h;
1236 
1237 	is_dsc_possible = slice_width <= dsc_common_caps.max_slice_width;
1238 	if (!is_dsc_possible)
1239 		goto done;
1240 
1241 	// Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by.
1242 	// For 4:2:0 make sure the slice height is divisible by 2 as well.
1243 	if (options->dsc_min_slice_height_override == 0)
1244 		slice_height = min(policy.min_slice_height, pic_height);
1245 	else
1246 		slice_height = min((int)(options->dsc_min_slice_height_override), pic_height);
1247 
1248 	while (slice_height < pic_height && (pic_height % slice_height != 0 ||
1249 		slice_height % options->slice_height_granularity != 0 ||
1250 		(timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 && slice_height % 2 != 0)))
1251 		slice_height++;
1252 
1253 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) // For the case when pic_height < dsc_policy.min_sice_height
1254 		is_dsc_possible = (slice_height % 2 == 0);
1255 
1256 	if (!is_dsc_possible)
1257 		goto done;
1258 
1259 	if (slice_height > 0) {
1260 		dsc_cfg->num_slices_v = pic_height / slice_height;
1261 	} else {
1262 		is_dsc_possible = false;
1263 		goto done;
1264 	}
1265 
1266 	if (target_bandwidth_kbps > 0) {
1267 		is_dsc_possible = decide_dsc_target_bpp_x16(
1268 				&policy,
1269 				options,
1270 				&dsc_common_caps,
1271 				target_bandwidth_kbps,
1272 				timing,
1273 				num_slices_h,
1274 				link_encoding,
1275 				&target_bpp);
1276 		dsc_cfg->bits_per_pixel = target_bpp;
1277 	}
1278 	if (!is_dsc_possible)
1279 		goto done;
1280 
1281 	/* Fill out the rest of DSC settings */
1282 	dsc_cfg->block_pred_enable = dsc_common_caps.is_block_pred_supported;
1283 	dsc_cfg->linebuf_depth = dsc_common_caps.lb_bit_depth;
1284 	dsc_cfg->version_minor = (dsc_common_caps.dsc_version & 0xf0) >> 4;
1285 	dsc_cfg->is_dp = dsc_sink_caps->is_dp;
1286 
1287 done:
1288 	if (!is_dsc_possible)
1289 		memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
1290 
1291 	return is_dsc_possible;
1292 }
1293 
1294 bool dc_dsc_compute_config(
1295 		const struct display_stream_compressor *dsc,
1296 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
1297 		const struct dc_dsc_config_options *options,
1298 		uint32_t target_bandwidth_kbps,
1299 		const struct dc_crtc_timing *timing,
1300 		const enum dc_link_encoding_format link_encoding,
1301 		struct dc_dsc_config *dsc_cfg)
1302 {
1303 	bool is_dsc_possible = false;
1304 	struct dsc_enc_caps dsc_enc_caps;
1305 	unsigned int min_dsc_slice_count;
1306 	get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
1307 
1308 	min_dsc_slice_count = get_min_dsc_slice_count_for_odm(dsc, &dsc_enc_caps, timing);
1309 
1310 	is_dsc_possible = setup_dsc_config(dsc_sink_caps,
1311 		&dsc_enc_caps,
1312 		target_bandwidth_kbps,
1313 		timing,
1314 		options,
1315 		link_encoding,
1316 		min_dsc_slice_count,
1317 		dsc_cfg);
1318 	return is_dsc_possible;
1319 }
1320 
1321 uint32_t dc_dsc_stream_bandwidth_in_kbps(const struct dc_crtc_timing *timing,
1322 	uint32_t bpp_x16, uint32_t num_slices_h, bool is_dp)
1323 {
1324 	uint32_t overhead_in_kbps;
1325 	struct fixed31_32 bpp;
1326 	struct fixed31_32 actual_bandwidth_in_kbps;
1327 
1328 	overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
1329 		timing, num_slices_h, is_dp);
1330 	bpp = dc_fixpt_from_fraction(bpp_x16, 16);
1331 	actual_bandwidth_in_kbps = dc_fixpt_from_fraction(timing->pix_clk_100hz, 10);
1332 	actual_bandwidth_in_kbps = dc_fixpt_mul(actual_bandwidth_in_kbps, bpp);
1333 	actual_bandwidth_in_kbps = dc_fixpt_add_int(actual_bandwidth_in_kbps, overhead_in_kbps);
1334 	return dc_fixpt_ceil(actual_bandwidth_in_kbps);
1335 }
1336 
1337 uint32_t dc_dsc_stream_bandwidth_overhead_in_kbps(
1338 		const struct dc_crtc_timing *timing,
1339 		const uint32_t num_slices_h,
1340 		const bool is_dp)
1341 {
1342 	struct fixed31_32 max_dsc_overhead;
1343 	struct fixed31_32 refresh_rate;
1344 
1345 	if (dsc_policy_disable_dsc_stream_overhead || !is_dp)
1346 		return 0;
1347 
1348 	/* use target bpp that can take entire target bandwidth */
1349 	refresh_rate = dc_fixpt_from_int(timing->pix_clk_100hz);
1350 	refresh_rate = dc_fixpt_div_int(refresh_rate, timing->h_total);
1351 	refresh_rate = dc_fixpt_div_int(refresh_rate, timing->v_total);
1352 	refresh_rate = dc_fixpt_mul_int(refresh_rate, 100);
1353 
1354 	max_dsc_overhead = dc_fixpt_from_int(num_slices_h);
1355 	max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, timing->v_total);
1356 	max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, 256);
1357 	max_dsc_overhead = dc_fixpt_div_int(max_dsc_overhead, 1000);
1358 	max_dsc_overhead = dc_fixpt_mul(max_dsc_overhead, refresh_rate);
1359 
1360 	return dc_fixpt_ceil(max_dsc_overhead);
1361 }
1362 
1363 void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing *timing,
1364 		uint32_t max_target_bpp_limit_override_x16,
1365 		struct dc_dsc_policy *policy,
1366 		const enum dc_link_encoding_format link_encoding)
1367 {
1368 	uint32_t bpc = 0;
1369 
1370 	policy->min_target_bpp = 0;
1371 	policy->max_target_bpp = 0;
1372 
1373 	/* DSC Policy: Use minimum number of slices that fits the pixel clock */
1374 	policy->use_min_slices_h = true;
1375 
1376 	/* DSC Policy: Use max available slices
1377 	 * (in our case 4 for or 8, depending on the mode)
1378 	 */
1379 	policy->max_slices_h = 0;
1380 
1381 	/* DSC Policy: Use slice height recommended
1382 	 * by VESA DSC Spreadsheet user guide
1383 	 */
1384 	policy->min_slice_height = 108;
1385 
1386 	/* DSC Policy: follow DP specs with an internal upper limit to 16 bpp
1387 	 * for better interoperability
1388 	 */
1389 	switch (timing->display_color_depth) {
1390 	case COLOR_DEPTH_888:
1391 		bpc = 8;
1392 		break;
1393 	case COLOR_DEPTH_101010:
1394 		bpc = 10;
1395 		break;
1396 	case COLOR_DEPTH_121212:
1397 		bpc = 12;
1398 		break;
1399 	default:
1400 		return;
1401 	}
1402 	switch (timing->pixel_encoding) {
1403 	case PIXEL_ENCODING_RGB:
1404 	case PIXEL_ENCODING_YCBCR444:
1405 	case PIXEL_ENCODING_YCBCR422: /* assume no YCbCr422 native support */
1406 		/* DP specs limits to 8 */
1407 		policy->min_target_bpp = 8;
1408 		/* DP specs limits to 3 x bpc */
1409 		policy->max_target_bpp = 3 * bpc;
1410 		policy->ycbcr422_simple = true;
1411 		break;
1412 	case PIXEL_ENCODING_YCBCR420:
1413 		/* DP specs limits to 6 */
1414 		policy->min_target_bpp = 6;
1415 		/* DP specs limits to 1.5 x bpc assume bpc is an even number */
1416 		policy->max_target_bpp = bpc * 3 / 2;
1417 		break;
1418 	default:
1419 		return;
1420 	}
1421 
1422 	/* internal upper limit, default 16 bpp */
1423 	if (policy->max_target_bpp > dsc_policy_max_target_bpp_limit)
1424 		policy->max_target_bpp = dsc_policy_max_target_bpp_limit;
1425 
1426 	/* apply override */
1427 	if (max_target_bpp_limit_override_x16 && policy->max_target_bpp > max_target_bpp_limit_override_x16 / 16)
1428 		policy->max_target_bpp = max_target_bpp_limit_override_x16 / 16;
1429 
1430 	/* enable DSC when not needed, default false */
1431 	policy->enable_dsc_when_not_needed = dsc_policy_enable_dsc_when_not_needed;
1432 }
1433 
1434 void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit)
1435 {
1436 	dsc_policy_max_target_bpp_limit = limit;
1437 }
1438 
1439 void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable)
1440 {
1441 	dsc_policy_enable_dsc_when_not_needed = enable;
1442 }
1443 
1444 void dc_dsc_policy_set_disable_dsc_stream_overhead(bool disable)
1445 {
1446 	dsc_policy_disable_dsc_stream_overhead = disable;
1447 }
1448 
1449 void dc_set_disable_128b_132b_stream_overhead(bool disable)
1450 {
1451 	disable_128b_132b_stream_overhead = disable;
1452 }
1453 
1454 void dc_dsc_get_default_config_option(const struct dc *dc, struct dc_dsc_config_options *options)
1455 {
1456 	options->dsc_min_slice_height_override = dc->debug.dsc_min_slice_height_override;
1457 	options->dsc_force_odm_hslice_override = dc->debug.force_odm_combine;
1458 	options->max_target_bpp_limit_override_x16 = 0;
1459 	options->slice_height_granularity = 1;
1460 	options->force_dsc_when_not_needed = false;
1461 }
1462