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