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