xref: /linux/drivers/gpu/drm/amd/display/dc/dsc/dc_dsc.c (revision d7b618bc41ee3d44c070212dff93949702ede997)
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_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_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_slice_count = get_min_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_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 	memset(&single_dsc_enc_caps, 0, sizeof(struct dsc_enc_caps));
597 
598 	if (!dsc || !dsc->ctx || !dsc->ctx->dc || !dsc->funcs->dsc_get_single_enc_caps)
599 		return;
600 
601 	dc = dsc->ctx->dc;
602 
603 	if (!dc->clk_mgr || !dc->clk_mgr->funcs->get_max_clock_khz || !dc->res_pool)
604 		return;
605 
606 	/* get max DSCCLK from clk_mgr */
607 	max_dscclk_khz = dc->clk_mgr->funcs->get_max_clock_khz(dc->clk_mgr, CLK_TYPE_DSCCLK);
608 
609 	dsc->funcs->dsc_get_single_enc_caps(&single_dsc_enc_caps, max_dscclk_khz);
610 
611 	/* global capabilities */
612 	dsc_enc_caps->dsc_version = single_dsc_enc_caps.dsc_version;
613 	dsc_enc_caps->lb_bit_depth = single_dsc_enc_caps.lb_bit_depth;
614 	dsc_enc_caps->is_block_pred_supported = single_dsc_enc_caps.is_block_pred_supported;
615 	dsc_enc_caps->max_slice_width = single_dsc_enc_caps.max_slice_width;
616 	dsc_enc_caps->bpp_increment_div = single_dsc_enc_caps.bpp_increment_div;
617 	dsc_enc_caps->color_formats.raw = single_dsc_enc_caps.color_formats.raw;
618 	dsc_enc_caps->color_depth.raw = single_dsc_enc_caps.color_depth.raw;
619 
620 	/* expand per DSC capabilities to global */
621 	max_odm_combine_factor = dc->caps.max_odm_combine_factor;
622 	num_dsc = dc->res_pool->res_cap->num_dsc;
623 	max_odm_combine_factor = min(max_odm_combine_factor, num_dsc);
624 	dsc_enc_caps->max_total_throughput_mps =
625 			single_dsc_enc_caps.max_total_throughput_mps *
626 			max_odm_combine_factor;
627 
628 	/* check slice counts possible for with ODM combine */
629 	build_dsc_enc_combined_slice_caps(&single_dsc_enc_caps, dsc_enc_caps, max_odm_combine_factor);
630 }
631 
632 static inline uint32_t dsc_div_by_10_round_up(uint32_t value)
633 {
634 	return (value + 9) / 10;
635 }
636 
637 static unsigned int get_min_slice_count_for_odm(
638 		const struct display_stream_compressor *dsc,
639 		const struct dsc_enc_caps *dsc_enc_caps,
640 		const struct dc_crtc_timing *timing)
641 {
642 	unsigned int max_dispclk_khz;
643 
644 	/* get max pixel rate and combine caps */
645 	max_dispclk_khz = dsc_enc_caps->max_total_throughput_mps * 1000;
646 	if (dsc && dsc->ctx->dc) {
647 		if (dsc->ctx->dc->clk_mgr &&
648 			dsc->ctx->dc->clk_mgr->funcs->get_max_clock_khz) {
649 			/* dispclk is available */
650 			max_dispclk_khz = dsc->ctx->dc->clk_mgr->funcs->get_max_clock_khz(dsc->ctx->dc->clk_mgr, CLK_TYPE_DISPCLK);
651 		}
652 	}
653 
654 	/* consider minimum odm slices required due to
655 	 * 1) display pipe throughput (dispclk)
656 	 * 2) max image width per slice
657 	 */
658 	return dc_fixpt_ceil(dc_fixpt_max(
659 			dc_fixpt_div_int(dc_fixpt_from_int(dsc_div_by_10_round_up(timing->pix_clk_100hz)),
660 			max_dispclk_khz), // throughput
661 			dc_fixpt_div_int(dc_fixpt_from_int(timing->h_addressable + timing->h_border_left + timing->h_border_right),
662 			dsc_enc_caps->max_slice_width))); // slice width
663 }
664 
665 static void get_dsc_enc_caps(
666 		const struct display_stream_compressor *dsc,
667 		struct dsc_enc_caps *dsc_enc_caps,
668 		int pixel_clock_100Hz)
669 {
670 	memset(dsc_enc_caps, 0, sizeof(struct dsc_enc_caps));
671 
672 	if (!dsc)
673 		return;
674 
675 	/* check if reported cap global or only for a single DCN DSC enc */
676 	if (dsc->funcs->dsc_get_enc_caps) {
677 		if (!dsc->ctx->dc->debug.disable_dsc)
678 			dsc->funcs->dsc_get_enc_caps(dsc_enc_caps, pixel_clock_100Hz);
679 	} else {
680 		build_dsc_enc_caps(dsc, dsc_enc_caps);
681 	}
682 
683 	if (dsc->ctx->dc->debug.native422_support)
684 		dsc_enc_caps->color_formats.bits.YCBCR_NATIVE_422 = 1;
685 }
686 
687 /* Returns 'false' if no intersection was found for at least one capability.
688  * It also implicitly validates some sink caps against invalid value of zero.
689  */
690 static bool intersect_dsc_caps(
691 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
692 		const struct dsc_enc_caps *dsc_enc_caps,
693 		enum dc_pixel_encoding pixel_encoding,
694 		struct dsc_enc_caps *dsc_common_caps)
695 {
696 	int32_t max_slices;
697 	int32_t total_sink_throughput;
698 
699 	memset(dsc_common_caps, 0, sizeof(struct dsc_enc_caps));
700 
701 	dsc_common_caps->dsc_version = min(dsc_sink_caps->dsc_version, dsc_enc_caps->dsc_version);
702 	if (!dsc_common_caps->dsc_version)
703 		return false;
704 
705 	dsc_common_caps->slice_caps.bits.NUM_SLICES_1 =
706 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_1 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_1;
707 	dsc_common_caps->slice_caps.bits.NUM_SLICES_2 =
708 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_2 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_2;
709 	dsc_common_caps->slice_caps.bits.NUM_SLICES_4 =
710 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_4 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_4;
711 	dsc_common_caps->slice_caps.bits.NUM_SLICES_8 =
712 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_8 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_8;
713 	dsc_common_caps->slice_caps.bits.NUM_SLICES_12 =
714 		dsc_sink_caps->slice_caps1.bits.NUM_SLICES_12 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_12;
715 	dsc_common_caps->slice_caps.bits.NUM_SLICES_16 =
716 		dsc_sink_caps->slice_caps2.bits.NUM_SLICES_16 && dsc_enc_caps->slice_caps.bits.NUM_SLICES_16;
717 
718 	if (!dsc_common_caps->slice_caps.raw)
719 		return false;
720 
721 	dsc_common_caps->lb_bit_depth = min(dsc_sink_caps->lb_bit_depth, dsc_enc_caps->lb_bit_depth);
722 	if (!dsc_common_caps->lb_bit_depth)
723 		return false;
724 
725 	dsc_common_caps->is_block_pred_supported =
726 		dsc_sink_caps->is_block_pred_supported && dsc_enc_caps->is_block_pred_supported;
727 
728 	dsc_common_caps->color_formats.raw = dsc_sink_caps->color_formats.raw & dsc_enc_caps->color_formats.raw;
729 	if (!dsc_common_caps->color_formats.raw)
730 		return false;
731 
732 	dsc_common_caps->color_depth.raw = dsc_sink_caps->color_depth.raw & dsc_enc_caps->color_depth.raw;
733 	if (!dsc_common_caps->color_depth.raw)
734 		return false;
735 
736 	max_slices = 0;
737 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_1)
738 		max_slices = 1;
739 
740 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_2)
741 		max_slices = 2;
742 
743 	if (dsc_common_caps->slice_caps.bits.NUM_SLICES_4)
744 		max_slices = 4;
745 
746 	total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_0_mps;
747 	if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
748 		total_sink_throughput = max_slices * dsc_sink_caps->throughput_mode_1_mps;
749 
750 	dsc_common_caps->max_total_throughput_mps = min(total_sink_throughput, dsc_enc_caps->max_total_throughput_mps);
751 
752 	dsc_common_caps->max_slice_width = min(dsc_sink_caps->max_slice_width, dsc_enc_caps->max_slice_width);
753 	if (!dsc_common_caps->max_slice_width)
754 		return false;
755 
756 	dsc_common_caps->bpp_increment_div = min(dsc_sink_caps->bpp_increment_div, dsc_enc_caps->bpp_increment_div);
757 
758 	// TODO DSC: Remove this workaround for N422 and 420 once it's fixed, or move it to get_dsc_encoder_caps()
759 	if (pixel_encoding == PIXEL_ENCODING_YCBCR422 || pixel_encoding == PIXEL_ENCODING_YCBCR420)
760 		dsc_common_caps->bpp_increment_div = min(dsc_common_caps->bpp_increment_div, (uint32_t)8);
761 
762 	dsc_common_caps->edp_sink_max_bits_per_pixel = dsc_sink_caps->edp_max_bits_per_pixel;
763 	dsc_common_caps->is_dp = dsc_sink_caps->is_dp;
764 	return true;
765 }
766 
767 static uint32_t compute_bpp_x16_from_target_bandwidth(
768 	const uint32_t bandwidth_in_kbps,
769 	const struct dc_crtc_timing *timing,
770 	const uint32_t num_slices_h,
771 	const uint32_t bpp_increment_div,
772 	const bool is_dp)
773 {
774 	uint32_t overhead_in_kbps;
775 	struct fixed31_32 effective_bandwidth_in_kbps;
776 	struct fixed31_32 bpp_x16;
777 
778 	overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
779 				timing, num_slices_h, is_dp);
780 	effective_bandwidth_in_kbps = dc_fixpt_from_int(bandwidth_in_kbps);
781 	effective_bandwidth_in_kbps = dc_fixpt_sub_int(effective_bandwidth_in_kbps,
782 			overhead_in_kbps);
783 	bpp_x16 = dc_fixpt_mul_int(effective_bandwidth_in_kbps, 10);
784 	bpp_x16 = dc_fixpt_div_int(bpp_x16, timing->pix_clk_100hz);
785 	bpp_x16 = dc_fixpt_from_int(dc_fixpt_floor(dc_fixpt_mul_int(bpp_x16, bpp_increment_div)));
786 	bpp_x16 = dc_fixpt_div_int(bpp_x16, bpp_increment_div);
787 	bpp_x16 = dc_fixpt_mul_int(bpp_x16, 16);
788 	return dc_fixpt_floor(bpp_x16);
789 }
790 
791 /* Decide DSC bandwidth range based on signal, timing, specs specific and input min and max
792  * requirements.
793  * The range output includes decided min/max target bpp, the respective bandwidth requirements
794  * and native timing bandwidth requirement when DSC is not used.
795  */
796 static bool decide_dsc_bandwidth_range(
797 		const uint32_t min_bpp_x16,
798 		const uint32_t max_bpp_x16,
799 		const uint32_t num_slices_h,
800 		const struct dsc_enc_caps *dsc_caps,
801 		const struct dc_crtc_timing *timing,
802 		const enum dc_link_encoding_format link_encoding,
803 		struct dc_dsc_bw_range *range)
804 {
805 	uint32_t preferred_bpp_x16 = timing->dsc_fixed_bits_per_pixel_x16;
806 
807 	memset(range, 0, sizeof(*range));
808 
809 	/* apply signal, timing, specs and explicitly specified DSC range requirements */
810 	if (preferred_bpp_x16) {
811 		if (preferred_bpp_x16 <= max_bpp_x16 &&
812 				preferred_bpp_x16 >= min_bpp_x16) {
813 			range->max_target_bpp_x16 = preferred_bpp_x16;
814 			range->min_target_bpp_x16 = preferred_bpp_x16;
815 		}
816 	}
817 	/* TODO - make this value generic to all signal types */
818 	else if (dsc_caps->edp_sink_max_bits_per_pixel) {
819 		/* apply max bpp limitation from edp sink */
820 		range->max_target_bpp_x16 = MIN(dsc_caps->edp_sink_max_bits_per_pixel,
821 				max_bpp_x16);
822 		range->min_target_bpp_x16 = min_bpp_x16;
823 	}
824 	else {
825 		range->max_target_bpp_x16 = max_bpp_x16;
826 		range->min_target_bpp_x16 = min_bpp_x16;
827 	}
828 
829 	/* populate output structure */
830 	if (range->max_target_bpp_x16 >= range->min_target_bpp_x16 && range->min_target_bpp_x16 > 0) {
831 		/* native stream bandwidth */
832 		range->stream_kbps = dc_bandwidth_in_kbps_from_timing(timing, link_encoding);
833 
834 		/* max dsc target bpp */
835 		range->max_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
836 				range->max_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
837 
838 		/* min dsc target bpp */
839 		range->min_kbps = dc_dsc_stream_bandwidth_in_kbps(timing,
840 				range->min_target_bpp_x16, num_slices_h, dsc_caps->is_dp);
841 	}
842 
843 	return range->max_kbps >= range->min_kbps && range->min_kbps > 0;
844 }
845 
846 /* Decides if DSC should be used and calculates target bpp if it should, applying DSC policy.
847  *
848  * Returns:
849  *     - 'true' if target bpp is decided
850  *     - 'false' if target bpp cannot be decided (e.g. cannot fit even with min DSC bpp),
851  */
852 static bool decide_dsc_target_bpp_x16(
853 		const struct dc_dsc_policy *policy,
854 		const struct dc_dsc_config_options *options,
855 		const struct dsc_enc_caps *dsc_common_caps,
856 		const int target_bandwidth_kbps,
857 		const struct dc_crtc_timing *timing,
858 		const int num_slices_h,
859 		const enum dc_link_encoding_format link_encoding,
860 		int *target_bpp_x16)
861 {
862 	struct dc_dsc_bw_range range;
863 
864 	*target_bpp_x16 = 0;
865 
866 	if (decide_dsc_bandwidth_range(policy->min_target_bpp * 16, policy->max_target_bpp * 16,
867 			num_slices_h, dsc_common_caps, timing, link_encoding, &range)) {
868 		if (target_bandwidth_kbps >= range.stream_kbps) {
869 			if (policy->enable_dsc_when_not_needed || options->force_dsc_when_not_needed)
870 				/* enable max bpp even dsc is not needed */
871 				*target_bpp_x16 = range.max_target_bpp_x16;
872 		} else if (target_bandwidth_kbps >= range.max_kbps) {
873 			/* use max target bpp allowed */
874 			*target_bpp_x16 = range.max_target_bpp_x16;
875 		} else if (target_bandwidth_kbps >= range.min_kbps) {
876 			/* use target bpp that can take entire target bandwidth */
877 			*target_bpp_x16 = compute_bpp_x16_from_target_bandwidth(
878 					target_bandwidth_kbps, timing, num_slices_h,
879 					dsc_common_caps->bpp_increment_div,
880 					dsc_common_caps->is_dp);
881 		}
882 	}
883 
884 	return *target_bpp_x16 != 0;
885 }
886 
887 #define MIN_AVAILABLE_SLICES_SIZE  6
888 
889 static int get_available_dsc_slices(union dsc_enc_slice_caps slice_caps, int *available_slices)
890 {
891 	int idx = 0;
892 
893 	if (slice_caps.bits.NUM_SLICES_1)
894 		available_slices[idx++] = 1;
895 
896 	if (slice_caps.bits.NUM_SLICES_2)
897 		available_slices[idx++] = 2;
898 
899 	if (slice_caps.bits.NUM_SLICES_4)
900 		available_slices[idx++] = 4;
901 
902 	if (slice_caps.bits.NUM_SLICES_8)
903 		available_slices[idx++] = 8;
904 
905 	if (slice_caps.bits.NUM_SLICES_12)
906 		available_slices[idx++] = 12;
907 
908 	if (slice_caps.bits.NUM_SLICES_16)
909 		available_slices[idx++] = 16;
910 
911 	return idx;
912 }
913 
914 
915 static int get_max_dsc_slices(union dsc_enc_slice_caps slice_caps)
916 {
917 	int max_slices = 0;
918 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
919 	int end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
920 
921 	if (end_idx > 0)
922 		max_slices = available_slices[end_idx - 1];
923 
924 	return max_slices;
925 }
926 
927 
928 // Increment slice number in available slice numbers stops if possible, or just increment if not
929 static int inc_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
930 {
931 	// Get next bigger num slices available in common caps
932 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
933 	int end_idx;
934 	int i;
935 	int new_num_slices = num_slices;
936 
937 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
938 	if (end_idx == 0) {
939 		// No available slices found
940 		new_num_slices++;
941 		return new_num_slices;
942 	}
943 
944 	// Numbers of slices found - get the next bigger number
945 	for (i = 0; i < end_idx; i++) {
946 		if (new_num_slices < available_slices[i]) {
947 			new_num_slices = available_slices[i];
948 			break;
949 		}
950 	}
951 
952 	if (new_num_slices == num_slices) // No bigger number of slices found
953 		new_num_slices++;
954 
955 	return new_num_slices;
956 }
957 
958 
959 // Decrement slice number in available slice numbers stops if possible, or just decrement if not. Stop at zero.
960 static int dec_num_slices(union dsc_enc_slice_caps slice_caps, int num_slices)
961 {
962 	// Get next bigger num slices available in common caps
963 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
964 	int end_idx;
965 	int i;
966 	int new_num_slices = num_slices;
967 
968 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
969 	if (end_idx == 0 && new_num_slices > 0) {
970 		// No numbers of slices found
971 		new_num_slices++;
972 		return new_num_slices;
973 	}
974 
975 	// Numbers of slices found - get the next smaller number
976 	for (i = end_idx - 1; i >= 0; i--) {
977 		if (new_num_slices > available_slices[i]) {
978 			new_num_slices = available_slices[i];
979 			break;
980 		}
981 	}
982 
983 	if (new_num_slices == num_slices) {
984 		// No smaller number of slices found
985 		new_num_slices--;
986 		if (new_num_slices < 0)
987 			new_num_slices = 0;
988 	}
989 
990 	return new_num_slices;
991 }
992 
993 
994 // Choose next bigger number of slices if the requested number of slices is not available
995 static int fit_num_slices_up(union dsc_enc_slice_caps slice_caps, int num_slices)
996 {
997 	// Get next bigger num slices available in common caps
998 	int available_slices[MIN_AVAILABLE_SLICES_SIZE];
999 	int end_idx;
1000 	int i;
1001 	int new_num_slices = num_slices;
1002 
1003 	end_idx = get_available_dsc_slices(slice_caps, &available_slices[0]);
1004 	if (end_idx == 0) {
1005 		// No available slices found
1006 		new_num_slices++;
1007 		return new_num_slices;
1008 	}
1009 
1010 	// Numbers of slices found - get the equal or next bigger number
1011 	for (i = 0; i < end_idx; i++) {
1012 		if (new_num_slices <= available_slices[i]) {
1013 			new_num_slices = available_slices[i];
1014 			break;
1015 		}
1016 	}
1017 
1018 	return new_num_slices;
1019 }
1020 
1021 
1022 /* Attempts to set DSC configuration for the stream, applying DSC policy.
1023  * Returns 'true' if successful or 'false' if not.
1024  *
1025  * Parameters:
1026  *
1027  * dsc_sink_caps       - DSC sink decoder capabilities (from DPCD)
1028  *
1029  * dsc_enc_caps        - DSC encoder capabilities
1030  *
1031  * target_bandwidth_kbps  - Target bandwidth to fit the stream into.
1032  *                          If 0, do not calculate target bpp.
1033  *
1034  * timing              - The stream timing to fit into 'target_bandwidth_kbps' or apply
1035  *                       maximum compression to, if 'target_badwidth == 0'
1036  *
1037  * dsc_cfg             - DSC configuration to use if it was possible to come up with
1038  *                       one for the given inputs.
1039  *                       The target bitrate after DSC can be calculated by multiplying
1040  *                       dsc_cfg.bits_per_pixel (in U6.4 format) by pixel rate, e.g.
1041  *
1042  *                       dsc_stream_bitrate_kbps = (int)ceil(timing->pix_clk_khz * dsc_cfg.bits_per_pixel / 16.0);
1043  */
1044 static bool setup_dsc_config(
1045 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
1046 		const struct dsc_enc_caps *dsc_enc_caps,
1047 		int target_bandwidth_kbps,
1048 		const struct dc_crtc_timing *timing,
1049 		const struct dc_dsc_config_options *options,
1050 		const enum dc_link_encoding_format link_encoding,
1051 		int min_slices_h,
1052 		struct dc_dsc_config *dsc_cfg)
1053 {
1054 	struct dsc_enc_caps dsc_common_caps;
1055 	int max_slices_h = 0;
1056 	int num_slices_h = 0;
1057 	int pic_width;
1058 	int slice_width;
1059 	int target_bpp;
1060 	int sink_per_slice_throughput_mps;
1061 	int 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_height = timing->v_addressable + timing->v_border_top + timing->v_border_bottom;
1072 
1073 	if (!dsc_sink_caps->is_dsc_supported)
1074 		goto done;
1075 
1076 	if (dsc_sink_caps->branch_max_line_width && dsc_sink_caps->branch_max_line_width < pic_width)
1077 		goto done;
1078 
1079 	// Intersect decoder with encoder DSC caps and validate DSC settings
1080 	is_dsc_possible = intersect_dsc_caps(dsc_sink_caps, dsc_enc_caps, timing->pixel_encoding, &dsc_common_caps);
1081 	if (!is_dsc_possible)
1082 		goto done;
1083 
1084 	sink_per_slice_throughput_mps = 0;
1085 
1086 	// Validate available DSC settings against the mode timing
1087 
1088 	// Validate color format (and pick up the throughput values)
1089 	dsc_cfg->ycbcr422_simple = false;
1090 	switch (timing->pixel_encoding)	{
1091 	case PIXEL_ENCODING_RGB:
1092 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.RGB;
1093 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
1094 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
1095 		break;
1096 	case PIXEL_ENCODING_YCBCR444:
1097 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_444;
1098 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
1099 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_0_mps;
1100 		break;
1101 	case PIXEL_ENCODING_YCBCR422:
1102 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_422;
1103 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
1104 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
1105 		if (!is_dsc_possible) {
1106 			is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_SIMPLE_422;
1107 			dsc_cfg->ycbcr422_simple = is_dsc_possible;
1108 			sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_0_mps;
1109 		}
1110 		break;
1111 	case PIXEL_ENCODING_YCBCR420:
1112 		is_dsc_possible = (bool)dsc_common_caps.color_formats.bits.YCBCR_NATIVE_420;
1113 		sink_per_slice_throughput_mps = dsc_sink_caps->throughput_mode_1_mps;
1114 		branch_max_throughput_mps = dsc_sink_caps->branch_overall_throughput_1_mps;
1115 		break;
1116 	default:
1117 		is_dsc_possible = false;
1118 	}
1119 
1120 	// Validate branch's maximum throughput
1121 	if (branch_max_throughput_mps && dsc_div_by_10_round_up(timing->pix_clk_100hz) > branch_max_throughput_mps * 1000)
1122 		is_dsc_possible = false;
1123 
1124 	if (!is_dsc_possible)
1125 		goto done;
1126 
1127 	// Color depth
1128 	switch (timing->display_color_depth) {
1129 	case COLOR_DEPTH_888:
1130 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_8_BPC;
1131 		break;
1132 	case COLOR_DEPTH_101010:
1133 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_10_BPC;
1134 		break;
1135 	case COLOR_DEPTH_121212:
1136 		is_dsc_possible = (bool)dsc_common_caps.color_depth.bits.COLOR_DEPTH_12_BPC;
1137 		break;
1138 	default:
1139 		is_dsc_possible = false;
1140 	}
1141 
1142 	if (!is_dsc_possible)
1143 		goto done;
1144 
1145 	// Slice width (i.e. number of slices per line)
1146 	max_slices_h = get_max_dsc_slices(dsc_common_caps.slice_caps);
1147 
1148 	while (max_slices_h > 0) {
1149 		if (pic_width % max_slices_h == 0)
1150 			break;
1151 
1152 		max_slices_h = dec_num_slices(dsc_common_caps.slice_caps, max_slices_h);
1153 	}
1154 
1155 	is_dsc_possible = (dsc_common_caps.max_slice_width > 0);
1156 	if (!is_dsc_possible)
1157 		goto done;
1158 
1159 	min_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, min_slices_h);
1160 
1161 	/* increase minimum slice count to meet sink throughput limitations */
1162 	while (min_slices_h <= max_slices_h) {
1163 		int pix_clk_per_slice_khz = dsc_div_by_10_round_up(timing->pix_clk_100hz) / min_slices_h;
1164 		if (pix_clk_per_slice_khz <= sink_per_slice_throughput_mps * 1000)
1165 			break;
1166 
1167 		min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
1168 	}
1169 
1170 	/* increase minimum slice count to meet divisibility requirements */
1171 	while (pic_width % min_slices_h != 0 && min_slices_h <= max_slices_h) {
1172 		min_slices_h = inc_num_slices(dsc_common_caps.slice_caps, min_slices_h);
1173 	}
1174 
1175 	is_dsc_possible = (min_slices_h <= max_slices_h) && max_slices_h != 0;
1176 	if (!is_dsc_possible)
1177 		goto done;
1178 
1179 	if (policy.use_min_slices_h) {
1180 		if (min_slices_h > 0)
1181 			num_slices_h = min_slices_h;
1182 		else if (max_slices_h > 0) { // Fall back to max slices if min slices is not working out
1183 			if (policy.max_slices_h)
1184 				num_slices_h = min(policy.max_slices_h, max_slices_h);
1185 			else
1186 				num_slices_h = max_slices_h;
1187 		} else
1188 			is_dsc_possible = false;
1189 	} else {
1190 		if (max_slices_h > 0) {
1191 			if (policy.max_slices_h)
1192 				num_slices_h = min(policy.max_slices_h, max_slices_h);
1193 			else
1194 				num_slices_h = max_slices_h;
1195 		} else if (min_slices_h > 0) // Fall back to min slices if max slices is not possible
1196 			num_slices_h = min_slices_h;
1197 		else
1198 			is_dsc_possible = false;
1199 	}
1200 	// When we force ODM, num dsc h slices must be divisible by num odm h slices
1201 	switch (options->dsc_force_odm_hslice_override) {
1202 	case 0:
1203 	case 1:
1204 		break;
1205 	case 2:
1206 		if (num_slices_h < 2)
1207 			num_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, 2);
1208 		break;
1209 	case 3:
1210 		if (dsc_common_caps.slice_caps.bits.NUM_SLICES_12)
1211 			num_slices_h = 12;
1212 		else
1213 			num_slices_h = 0;
1214 		break;
1215 	case 4:
1216 		if (num_slices_h < 4)
1217 			num_slices_h = fit_num_slices_up(dsc_common_caps.slice_caps, 4);
1218 		break;
1219 	default:
1220 		break;
1221 	}
1222 	if (num_slices_h == 0)
1223 		is_dsc_possible = false;
1224 	if (!is_dsc_possible)
1225 		goto done;
1226 
1227 	dsc_cfg->num_slices_h = num_slices_h;
1228 	slice_width = pic_width / num_slices_h;
1229 
1230 	is_dsc_possible = slice_width <= dsc_common_caps.max_slice_width;
1231 	if (!is_dsc_possible)
1232 		goto done;
1233 
1234 	// Slice height (i.e. number of slices per column): start with policy and pick the first one that height is divisible by.
1235 	// For 4:2:0 make sure the slice height is divisible by 2 as well.
1236 	if (options->dsc_min_slice_height_override == 0)
1237 		slice_height = min(policy.min_slice_height, pic_height);
1238 	else
1239 		slice_height = min((int)(options->dsc_min_slice_height_override), pic_height);
1240 
1241 	while (slice_height < pic_height && (pic_height % slice_height != 0 ||
1242 		slice_height % options->slice_height_granularity != 0 ||
1243 		(timing->pixel_encoding == PIXEL_ENCODING_YCBCR420 && slice_height % 2 != 0)))
1244 		slice_height++;
1245 
1246 	if (timing->pixel_encoding == PIXEL_ENCODING_YCBCR420) // For the case when pic_height < dsc_policy.min_sice_height
1247 		is_dsc_possible = (slice_height % 2 == 0);
1248 
1249 	if (!is_dsc_possible)
1250 		goto done;
1251 
1252 	if (slice_height > 0) {
1253 		dsc_cfg->num_slices_v = pic_height / slice_height;
1254 	} else {
1255 		is_dsc_possible = false;
1256 		goto done;
1257 	}
1258 
1259 	if (target_bandwidth_kbps > 0) {
1260 		is_dsc_possible = decide_dsc_target_bpp_x16(
1261 				&policy,
1262 				options,
1263 				&dsc_common_caps,
1264 				target_bandwidth_kbps,
1265 				timing,
1266 				num_slices_h,
1267 				link_encoding,
1268 				&target_bpp);
1269 		dsc_cfg->bits_per_pixel = target_bpp;
1270 	}
1271 	if (!is_dsc_possible)
1272 		goto done;
1273 
1274 	/* Fill out the rest of DSC settings */
1275 	dsc_cfg->block_pred_enable = dsc_common_caps.is_block_pred_supported;
1276 	dsc_cfg->linebuf_depth = dsc_common_caps.lb_bit_depth;
1277 	dsc_cfg->version_minor = (dsc_common_caps.dsc_version & 0xf0) >> 4;
1278 	dsc_cfg->is_dp = dsc_sink_caps->is_dp;
1279 
1280 done:
1281 	if (!is_dsc_possible)
1282 		memset(dsc_cfg, 0, sizeof(struct dc_dsc_config));
1283 
1284 	return is_dsc_possible;
1285 }
1286 
1287 bool dc_dsc_compute_config(
1288 		const struct display_stream_compressor *dsc,
1289 		const struct dsc_dec_dpcd_caps *dsc_sink_caps,
1290 		const struct dc_dsc_config_options *options,
1291 		uint32_t target_bandwidth_kbps,
1292 		const struct dc_crtc_timing *timing,
1293 		const enum dc_link_encoding_format link_encoding,
1294 		struct dc_dsc_config *dsc_cfg)
1295 {
1296 	bool is_dsc_possible = false;
1297 	struct dsc_enc_caps dsc_enc_caps;
1298 	unsigned int min_slice_count;
1299 	get_dsc_enc_caps(dsc, &dsc_enc_caps, timing->pix_clk_100hz);
1300 
1301 	min_slice_count = get_min_slice_count_for_odm(dsc, &dsc_enc_caps, timing);
1302 
1303 	is_dsc_possible = setup_dsc_config(dsc_sink_caps,
1304 		&dsc_enc_caps,
1305 		target_bandwidth_kbps,
1306 		timing,
1307 		options,
1308 		link_encoding,
1309 		min_slice_count,
1310 		dsc_cfg);
1311 	return is_dsc_possible;
1312 }
1313 
1314 uint32_t dc_dsc_stream_bandwidth_in_kbps(const struct dc_crtc_timing *timing,
1315 	uint32_t bpp_x16, uint32_t num_slices_h, bool is_dp)
1316 {
1317 	uint32_t overhead_in_kbps;
1318 	struct fixed31_32 bpp;
1319 	struct fixed31_32 actual_bandwidth_in_kbps;
1320 
1321 	overhead_in_kbps = dc_dsc_stream_bandwidth_overhead_in_kbps(
1322 		timing, num_slices_h, is_dp);
1323 	bpp = dc_fixpt_from_fraction(bpp_x16, 16);
1324 	actual_bandwidth_in_kbps = dc_fixpt_from_fraction(timing->pix_clk_100hz, 10);
1325 	actual_bandwidth_in_kbps = dc_fixpt_mul(actual_bandwidth_in_kbps, bpp);
1326 	actual_bandwidth_in_kbps = dc_fixpt_add_int(actual_bandwidth_in_kbps, overhead_in_kbps);
1327 	return dc_fixpt_ceil(actual_bandwidth_in_kbps);
1328 }
1329 
1330 uint32_t dc_dsc_stream_bandwidth_overhead_in_kbps(
1331 		const struct dc_crtc_timing *timing,
1332 		const int num_slices_h,
1333 		const bool is_dp)
1334 {
1335 	struct fixed31_32 max_dsc_overhead;
1336 	struct fixed31_32 refresh_rate;
1337 
1338 	if (dsc_policy_disable_dsc_stream_overhead || !is_dp)
1339 		return 0;
1340 
1341 	/* use target bpp that can take entire target bandwidth */
1342 	refresh_rate = dc_fixpt_from_int(timing->pix_clk_100hz);
1343 	refresh_rate = dc_fixpt_div_int(refresh_rate, timing->h_total);
1344 	refresh_rate = dc_fixpt_div_int(refresh_rate, timing->v_total);
1345 	refresh_rate = dc_fixpt_mul_int(refresh_rate, 100);
1346 
1347 	max_dsc_overhead = dc_fixpt_from_int(num_slices_h);
1348 	max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, timing->v_total);
1349 	max_dsc_overhead = dc_fixpt_mul_int(max_dsc_overhead, 256);
1350 	max_dsc_overhead = dc_fixpt_div_int(max_dsc_overhead, 1000);
1351 	max_dsc_overhead = dc_fixpt_mul(max_dsc_overhead, refresh_rate);
1352 
1353 	return dc_fixpt_ceil(max_dsc_overhead);
1354 }
1355 
1356 void dc_dsc_get_policy_for_timing(const struct dc_crtc_timing *timing,
1357 		uint32_t max_target_bpp_limit_override_x16,
1358 		struct dc_dsc_policy *policy,
1359 		const enum dc_link_encoding_format link_encoding)
1360 {
1361 	uint32_t bpc = 0;
1362 
1363 	policy->min_target_bpp = 0;
1364 	policy->max_target_bpp = 0;
1365 
1366 	/* DSC Policy: Use minimum number of slices that fits the pixel clock */
1367 	policy->use_min_slices_h = true;
1368 
1369 	/* DSC Policy: Use max available slices
1370 	 * (in our case 4 for or 8, depending on the mode)
1371 	 */
1372 	policy->max_slices_h = 0;
1373 
1374 	/* DSC Policy: Use slice height recommended
1375 	 * by VESA DSC Spreadsheet user guide
1376 	 */
1377 	policy->min_slice_height = 108;
1378 
1379 	/* DSC Policy: follow DP specs with an internal upper limit to 16 bpp
1380 	 * for better interoperability
1381 	 */
1382 	switch (timing->display_color_depth) {
1383 	case COLOR_DEPTH_888:
1384 		bpc = 8;
1385 		break;
1386 	case COLOR_DEPTH_101010:
1387 		bpc = 10;
1388 		break;
1389 	case COLOR_DEPTH_121212:
1390 		bpc = 12;
1391 		break;
1392 	default:
1393 		return;
1394 	}
1395 	switch (timing->pixel_encoding) {
1396 	case PIXEL_ENCODING_RGB:
1397 	case PIXEL_ENCODING_YCBCR444:
1398 	case PIXEL_ENCODING_YCBCR422: /* assume no YCbCr422 native support */
1399 		/* DP specs limits to 8 */
1400 		policy->min_target_bpp = 8;
1401 		/* DP specs limits to 3 x bpc */
1402 		policy->max_target_bpp = 3 * bpc;
1403 		break;
1404 	case PIXEL_ENCODING_YCBCR420:
1405 		/* DP specs limits to 6 */
1406 		policy->min_target_bpp = 6;
1407 		/* DP specs limits to 1.5 x bpc assume bpc is an even number */
1408 		policy->max_target_bpp = bpc * 3 / 2;
1409 		break;
1410 	default:
1411 		return;
1412 	}
1413 
1414 	/* internal upper limit, default 16 bpp */
1415 	if (policy->max_target_bpp > dsc_policy_max_target_bpp_limit)
1416 		policy->max_target_bpp = dsc_policy_max_target_bpp_limit;
1417 
1418 	/* apply override */
1419 	if (max_target_bpp_limit_override_x16 && policy->max_target_bpp > max_target_bpp_limit_override_x16 / 16)
1420 		policy->max_target_bpp = max_target_bpp_limit_override_x16 / 16;
1421 
1422 	/* enable DSC when not needed, default false */
1423 	policy->enable_dsc_when_not_needed = dsc_policy_enable_dsc_when_not_needed;
1424 }
1425 
1426 void dc_dsc_policy_set_max_target_bpp_limit(uint32_t limit)
1427 {
1428 	dsc_policy_max_target_bpp_limit = limit;
1429 }
1430 
1431 void dc_dsc_policy_set_enable_dsc_when_not_needed(bool enable)
1432 {
1433 	dsc_policy_enable_dsc_when_not_needed = enable;
1434 }
1435 
1436 void dc_dsc_policy_set_disable_dsc_stream_overhead(bool disable)
1437 {
1438 	dsc_policy_disable_dsc_stream_overhead = disable;
1439 }
1440 
1441 void dc_set_disable_128b_132b_stream_overhead(bool disable)
1442 {
1443 	disable_128b_132b_stream_overhead = disable;
1444 }
1445 
1446 void dc_dsc_get_default_config_option(const struct dc *dc, struct dc_dsc_config_options *options)
1447 {
1448 	options->dsc_min_slice_height_override = dc->debug.dsc_min_slice_height_override;
1449 	options->dsc_force_odm_hslice_override = dc->debug.force_odm_combine;
1450 	options->max_target_bpp_limit_override_x16 = 0;
1451 	options->slice_height_granularity = 1;
1452 	options->force_dsc_when_not_needed = false;
1453 }
1454