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