1 // SPDX-License-Identifier: MIT
3 * Copyright © 2018 Intel Corp
6 * Manasi Navare <manasi.d.navare@intel.com>
9 /* DC versions of linux includes */
10 #include <include/drm_dsc_dc.h>
12 #define EXPORT_SYMBOL(symbol) /* nothing */
13 #define BUILD_BUG_ON(cond) /* nothing */
14 #define DIV_ROUND_UP(a, b) (((b) + (a) - 1) / (b))
16 #define DRM_DEBUG_KMS(msg) /* nothing */
17 #define cpu_to_be16(__x) little_to_big(__x)
19 static unsigned short little_to_big(int data)
21 /* Swap lower and upper byte. DMCU uses big endian format. */
22 return (0xff & (data >> 8)) + ((data & 0xff) << 8);
26 * Everything below this comment was copied directly from drm_dsc.c.
27 * Only the functions needed in DC are included.
28 * Please keep this file synced with upstream.
34 * These functions contain some common logic and helpers to deal with VESA
35 * Display Stream Compression standard required for DSC on Display Port/eDP or
36 * MIPI display interfaces.
40 * drm_dsc_pps_payload_pack() - Populates the DSC PPS
43 * Bitwise struct for DSC Picture Parameter Set. This is defined
44 * by &struct drm_dsc_picture_parameter_set
46 * DSC Configuration data filled by driver as defined by
47 * &struct drm_dsc_config
49 * DSC source device sends a picture parameter set (PPS) containing the
50 * information required by the sink to decode the compressed frame. Driver
51 * populates the DSC PPS struct using the DSC configuration parameters in
52 * the order expected by the DSC Display Sink device. For the DSC, the sink
53 * device expects the PPS payload in big endian format for fields
54 * that span more than 1 byte.
56 void drm_dsc_pps_payload_pack(struct drm_dsc_picture_parameter_set *pps_payload,
57 const struct drm_dsc_config *dsc_cfg)
61 /* Protect against someone accidently changing struct size */
62 BUILD_BUG_ON(sizeof(*pps_payload) !=
63 DP_SDP_PPS_HEADER_PAYLOAD_BYTES_MINUS_1 + 1);
65 memset(pps_payload, 0, sizeof(*pps_payload));
68 pps_payload->dsc_version =
69 dsc_cfg->dsc_version_minor |
70 dsc_cfg->dsc_version_major << DSC_PPS_VERSION_MAJOR_SHIFT;
76 dsc_cfg->line_buf_depth |
77 dsc_cfg->bits_per_component << DSC_PPS_BPC_SHIFT;
81 ((dsc_cfg->bits_per_pixel & DSC_PPS_BPP_HIGH_MASK) >>
83 dsc_cfg->vbr_enable << DSC_PPS_VBR_EN_SHIFT |
84 dsc_cfg->simple_422 << DSC_PPS_SIMPLE422_SHIFT |
85 dsc_cfg->convert_rgb << DSC_PPS_CONVERT_RGB_SHIFT |
86 dsc_cfg->block_pred_enable << DSC_PPS_BLOCK_PRED_EN_SHIFT;
89 pps_payload->bits_per_pixel_low =
90 (dsc_cfg->bits_per_pixel & DSC_PPS_LSB_MASK);
93 * The DSC panel expects the PPS packet to have big endian format
94 * for data spanning 2 bytes. Use a macro cpu_to_be16() to convert
95 * to big endian format. If format is little endian, it will swap
96 * bytes to convert to Big endian else keep it unchanged.
100 pps_payload->pic_height = cpu_to_be16(dsc_cfg->pic_height);
103 pps_payload->pic_width = cpu_to_be16(dsc_cfg->pic_width);
106 pps_payload->slice_height = cpu_to_be16(dsc_cfg->slice_height);
109 pps_payload->slice_width = cpu_to_be16(dsc_cfg->slice_width);
112 pps_payload->chunk_size = cpu_to_be16(dsc_cfg->slice_chunk_size);
115 pps_payload->initial_xmit_delay_high =
116 ((dsc_cfg->initial_xmit_delay &
117 DSC_PPS_INIT_XMIT_DELAY_HIGH_MASK) >>
121 pps_payload->initial_xmit_delay_low =
122 (dsc_cfg->initial_xmit_delay & DSC_PPS_LSB_MASK);
125 pps_payload->initial_dec_delay =
126 cpu_to_be16(dsc_cfg->initial_dec_delay);
131 pps_payload->initial_scale_value =
132 dsc_cfg->initial_scale_value;
135 pps_payload->scale_increment_interval =
136 cpu_to_be16(dsc_cfg->scale_increment_interval);
139 pps_payload->scale_decrement_interval_high =
140 ((dsc_cfg->scale_decrement_interval &
141 DSC_PPS_SCALE_DEC_INT_HIGH_MASK) >>
145 pps_payload->scale_decrement_interval_low =
146 (dsc_cfg->scale_decrement_interval & DSC_PPS_LSB_MASK);
148 /* PPS 26[7:0], PPS 27[7:5] RESERVED */
151 pps_payload->first_line_bpg_offset =
152 dsc_cfg->first_line_bpg_offset;
155 pps_payload->nfl_bpg_offset =
156 cpu_to_be16(dsc_cfg->nfl_bpg_offset);
159 pps_payload->slice_bpg_offset =
160 cpu_to_be16(dsc_cfg->slice_bpg_offset);
163 pps_payload->initial_offset =
164 cpu_to_be16(dsc_cfg->initial_offset);
167 pps_payload->final_offset = cpu_to_be16(dsc_cfg->final_offset);
170 pps_payload->flatness_min_qp = dsc_cfg->flatness_min_qp;
173 pps_payload->flatness_max_qp = dsc_cfg->flatness_max_qp;
176 pps_payload->rc_model_size =
177 cpu_to_be16(DSC_RC_MODEL_SIZE_CONST);
180 pps_payload->rc_edge_factor = DSC_RC_EDGE_FACTOR_CONST;
183 pps_payload->rc_quant_incr_limit0 =
184 dsc_cfg->rc_quant_incr_limit0;
187 pps_payload->rc_quant_incr_limit1 =
188 dsc_cfg->rc_quant_incr_limit1;
191 pps_payload->rc_tgt_offset = DSC_RC_TGT_OFFSET_LO_CONST |
192 DSC_RC_TGT_OFFSET_HI_CONST << DSC_PPS_RC_TGT_OFFSET_HI_SHIFT;
195 for (i = 0; i < DSC_NUM_BUF_RANGES - 1; i++)
196 pps_payload->rc_buf_thresh[i] =
197 dsc_cfg->rc_buf_thresh[i];
201 * For DSC sink programming the RC Range parameter fields
202 * are as follows: Min_qp[15:11], max_qp[10:6], offset[5:0]
204 for (i = 0; i < DSC_NUM_BUF_RANGES; i++) {
205 pps_payload->rc_range_parameters[i] =
206 ((dsc_cfg->rc_range_params[i].range_min_qp <<
207 DSC_PPS_RC_RANGE_MINQP_SHIFT) |
208 (dsc_cfg->rc_range_params[i].range_max_qp <<
209 DSC_PPS_RC_RANGE_MAXQP_SHIFT) |
210 (dsc_cfg->rc_range_params[i].range_bpg_offset));
211 pps_payload->rc_range_parameters[i] =
212 cpu_to_be16(pps_payload->rc_range_parameters[i]);
216 pps_payload->native_422_420 = dsc_cfg->native_422 |
217 dsc_cfg->native_420 << DSC_PPS_NATIVE_420_SHIFT;
220 pps_payload->second_line_bpg_offset =
221 dsc_cfg->second_line_bpg_offset;
224 pps_payload->nsl_bpg_offset =
225 cpu_to_be16(dsc_cfg->nsl_bpg_offset);
228 pps_payload->second_line_offset_adj =
229 cpu_to_be16(dsc_cfg->second_line_offset_adj);
231 /* PPS 94 - 127 are O */
233 EXPORT_SYMBOL(drm_dsc_pps_payload_pack);
236 * drm_dsc_compute_rc_parameters() - Write rate control
237 * parameters to the dsc configuration defined in
238 * &struct drm_dsc_config in accordance with the DSC 1.2
239 * specification. Some configuration fields must be present
243 * DSC Configuration data partially filled by driver
245 int drm_dsc_compute_rc_parameters(struct drm_dsc_config *vdsc_cfg)
247 unsigned long groups_per_line = 0;
248 unsigned long groups_total = 0;
249 unsigned long num_extra_mux_bits = 0;
250 unsigned long slice_bits = 0;
251 unsigned long hrd_delay = 0;
252 unsigned long final_scale = 0;
253 unsigned long rbs_min = 0;
255 if (vdsc_cfg->native_420 || vdsc_cfg->native_422) {
256 /* Number of groups used to code each line of a slice */
257 groups_per_line = DIV_ROUND_UP(vdsc_cfg->slice_width / 2,
258 DSC_RC_PIXELS_PER_GROUP);
260 /* chunksize in Bytes */
261 vdsc_cfg->slice_chunk_size = DIV_ROUND_UP(vdsc_cfg->slice_width / 2 *
262 vdsc_cfg->bits_per_pixel,
265 /* Number of groups used to code each line of a slice */
266 groups_per_line = DIV_ROUND_UP(vdsc_cfg->slice_width,
267 DSC_RC_PIXELS_PER_GROUP);
269 /* chunksize in Bytes */
270 vdsc_cfg->slice_chunk_size = DIV_ROUND_UP(vdsc_cfg->slice_width *
271 vdsc_cfg->bits_per_pixel,
275 if (vdsc_cfg->convert_rgb)
276 num_extra_mux_bits = 3 * (vdsc_cfg->mux_word_size +
277 (4 * vdsc_cfg->bits_per_component + 4)
279 else if (vdsc_cfg->native_422)
280 num_extra_mux_bits = 4 * vdsc_cfg->mux_word_size +
281 (4 * vdsc_cfg->bits_per_component + 4) +
282 3 * (4 * vdsc_cfg->bits_per_component) - 2;
284 num_extra_mux_bits = 3 * vdsc_cfg->mux_word_size +
285 (4 * vdsc_cfg->bits_per_component + 4) +
286 2 * (4 * vdsc_cfg->bits_per_component) - 2;
287 /* Number of bits in one Slice */
288 slice_bits = 8 * vdsc_cfg->slice_chunk_size * vdsc_cfg->slice_height;
290 while ((num_extra_mux_bits > 0) &&
291 ((slice_bits - num_extra_mux_bits) % vdsc_cfg->mux_word_size))
292 num_extra_mux_bits--;
294 if (groups_per_line < vdsc_cfg->initial_scale_value - 8)
295 vdsc_cfg->initial_scale_value = groups_per_line + 8;
297 /* scale_decrement_interval calculation according to DSC spec 1.11 */
298 if (vdsc_cfg->initial_scale_value > 8)
299 vdsc_cfg->scale_decrement_interval = groups_per_line /
300 (vdsc_cfg->initial_scale_value - 8);
302 vdsc_cfg->scale_decrement_interval = DSC_SCALE_DECREMENT_INTERVAL_MAX;
304 vdsc_cfg->final_offset = vdsc_cfg->rc_model_size -
305 (vdsc_cfg->initial_xmit_delay *
306 vdsc_cfg->bits_per_pixel + 8) / 16 + num_extra_mux_bits;
308 if (vdsc_cfg->final_offset >= vdsc_cfg->rc_model_size) {
309 DRM_DEBUG_KMS("FinalOfs < RcModelSze for this InitialXmitDelay\n");
313 final_scale = (vdsc_cfg->rc_model_size * 8) /
314 (vdsc_cfg->rc_model_size - vdsc_cfg->final_offset);
315 if (vdsc_cfg->slice_height > 1)
317 * NflBpgOffset is 16 bit value with 11 fractional bits
318 * hence we multiply by 2^11 for preserving the
321 vdsc_cfg->nfl_bpg_offset = DIV_ROUND_UP((vdsc_cfg->first_line_bpg_offset << 11),
322 (vdsc_cfg->slice_height - 1));
324 vdsc_cfg->nfl_bpg_offset = 0;
327 if (vdsc_cfg->nfl_bpg_offset > 65535) {
328 DRM_DEBUG_KMS("NflBpgOffset is too large for this slice height\n");
332 /* Number of groups used to code the entire slice */
333 groups_total = groups_per_line * vdsc_cfg->slice_height;
335 /* slice_bpg_offset is 16 bit value with 11 fractional bits */
336 vdsc_cfg->slice_bpg_offset = DIV_ROUND_UP(((vdsc_cfg->rc_model_size -
337 vdsc_cfg->initial_offset +
338 num_extra_mux_bits) << 11),
341 if (final_scale > 9) {
343 * ScaleIncrementInterval =
344 * finaloffset/((NflBpgOffset + SliceBpgOffset)*8(finalscale - 1.125))
345 * as (NflBpgOffset + SliceBpgOffset) has 11 bit fractional value,
346 * we need divide by 2^11 from pstDscCfg values
348 vdsc_cfg->scale_increment_interval =
349 (vdsc_cfg->final_offset * (1 << 11)) /
350 ((vdsc_cfg->nfl_bpg_offset +
351 vdsc_cfg->slice_bpg_offset) *
355 * If finalScaleValue is less than or equal to 9, a value of 0 should
356 * be used to disable the scale increment at the end of the slice
358 vdsc_cfg->scale_increment_interval = 0;
361 if (vdsc_cfg->scale_increment_interval > 65535) {
362 DRM_DEBUG_KMS("ScaleIncrementInterval is large for slice height\n");
367 * DSC spec mentions that bits_per_pixel specifies the target
368 * bits/pixel (bpp) rate that is used by the encoder,
369 * in steps of 1/16 of a bit per pixel
371 rbs_min = vdsc_cfg->rc_model_size - vdsc_cfg->initial_offset +
372 DIV_ROUND_UP(vdsc_cfg->initial_xmit_delay *
373 vdsc_cfg->bits_per_pixel, 16) +
374 groups_per_line * vdsc_cfg->first_line_bpg_offset;
376 hrd_delay = DIV_ROUND_UP((rbs_min * 16), vdsc_cfg->bits_per_pixel);
377 vdsc_cfg->rc_bits = (hrd_delay * vdsc_cfg->bits_per_pixel) / 16;
378 vdsc_cfg->initial_dec_delay = hrd_delay - vdsc_cfg->initial_xmit_delay;
380 /* As per DSC spec v1.2a recommendation: */
381 if (vdsc_cfg->native_420)
382 vdsc_cfg->second_line_offset_adj = 512;
384 vdsc_cfg->second_line_offset_adj = 0;
388 EXPORT_SYMBOL(drm_dsc_compute_rc_parameters);