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)
18 #define MAX(x, y) ((x) > (y) ? (x) : (y))
20 static unsigned short little_to_big(int data)
22 /* Swap lower and upper byte. DMCU uses big endian format. */
23 return (0xff & (data >> 8)) + ((data & 0xff) << 8);
27 * Everything below this comment was copied directly from drm_dsc.c.
28 * Only the functions needed in DC are included.
29 * Please keep this file synced with upstream.
35 * These functions contain some common logic and helpers to deal with VESA
36 * Display Stream Compression standard required for DSC on Display Port/eDP or
37 * MIPI display interfaces.
41 * drm_dsc_pps_payload_pack() - Populates the DSC PPS
44 * Bitwise struct for DSC Picture Parameter Set. This is defined
45 * by &struct drm_dsc_picture_parameter_set
47 * DSC Configuration data filled by driver as defined by
48 * &struct drm_dsc_config
50 * DSC source device sends a picture parameter set (PPS) containing the
51 * information required by the sink to decode the compressed frame. Driver
52 * populates the DSC PPS struct using the DSC configuration parameters in
53 * the order expected by the DSC Display Sink device. For the DSC, the sink
54 * device expects the PPS payload in big endian format for fields
55 * that span more than 1 byte.
57 void drm_dsc_pps_payload_pack(struct drm_dsc_picture_parameter_set *pps_payload,
58 const struct drm_dsc_config *dsc_cfg)
62 /* Protect against someone accidently changing struct size */
63 BUILD_BUG_ON(sizeof(*pps_payload) !=
64 DP_SDP_PPS_HEADER_PAYLOAD_BYTES_MINUS_1 + 1);
66 memset(pps_payload, 0, sizeof(*pps_payload));
69 pps_payload->dsc_version =
70 dsc_cfg->dsc_version_minor |
71 dsc_cfg->dsc_version_major << DSC_PPS_VERSION_MAJOR_SHIFT;
77 dsc_cfg->line_buf_depth |
78 dsc_cfg->bits_per_component << DSC_PPS_BPC_SHIFT;
82 ((dsc_cfg->bits_per_pixel & DSC_PPS_BPP_HIGH_MASK) >>
84 dsc_cfg->vbr_enable << DSC_PPS_VBR_EN_SHIFT |
85 dsc_cfg->simple_422 << DSC_PPS_SIMPLE422_SHIFT |
86 dsc_cfg->convert_rgb << DSC_PPS_CONVERT_RGB_SHIFT |
87 dsc_cfg->block_pred_enable << DSC_PPS_BLOCK_PRED_EN_SHIFT;
90 pps_payload->bits_per_pixel_low =
91 (dsc_cfg->bits_per_pixel & DSC_PPS_LSB_MASK);
94 * The DSC panel expects the PPS packet to have big endian format
95 * for data spanning 2 bytes. Use a macro cpu_to_be16() to convert
96 * to big endian format. If format is little endian, it will swap
97 * bytes to convert to Big endian else keep it unchanged.
101 pps_payload->pic_height = cpu_to_be16(dsc_cfg->pic_height);
104 pps_payload->pic_width = cpu_to_be16(dsc_cfg->pic_width);
107 pps_payload->slice_height = cpu_to_be16(dsc_cfg->slice_height);
110 pps_payload->slice_width = cpu_to_be16(dsc_cfg->slice_width);
113 pps_payload->chunk_size = cpu_to_be16(dsc_cfg->slice_chunk_size);
116 pps_payload->initial_xmit_delay_high =
117 ((dsc_cfg->initial_xmit_delay &
118 DSC_PPS_INIT_XMIT_DELAY_HIGH_MASK) >>
122 pps_payload->initial_xmit_delay_low =
123 (dsc_cfg->initial_xmit_delay & DSC_PPS_LSB_MASK);
126 pps_payload->initial_dec_delay =
127 cpu_to_be16(dsc_cfg->initial_dec_delay);
132 pps_payload->initial_scale_value =
133 dsc_cfg->initial_scale_value;
136 pps_payload->scale_increment_interval =
137 cpu_to_be16(dsc_cfg->scale_increment_interval);
140 pps_payload->scale_decrement_interval_high =
141 ((dsc_cfg->scale_decrement_interval &
142 DSC_PPS_SCALE_DEC_INT_HIGH_MASK) >>
146 pps_payload->scale_decrement_interval_low =
147 (dsc_cfg->scale_decrement_interval & DSC_PPS_LSB_MASK);
149 /* PPS 26[7:0], PPS 27[7:5] RESERVED */
152 pps_payload->first_line_bpg_offset =
153 dsc_cfg->first_line_bpg_offset;
156 pps_payload->nfl_bpg_offset =
157 cpu_to_be16(dsc_cfg->nfl_bpg_offset);
160 pps_payload->slice_bpg_offset =
161 cpu_to_be16(dsc_cfg->slice_bpg_offset);
164 pps_payload->initial_offset =
165 cpu_to_be16(dsc_cfg->initial_offset);
168 pps_payload->final_offset = cpu_to_be16(dsc_cfg->final_offset);
171 pps_payload->flatness_min_qp = dsc_cfg->flatness_min_qp;
174 pps_payload->flatness_max_qp = dsc_cfg->flatness_max_qp;
177 pps_payload->rc_model_size =
178 cpu_to_be16(DSC_RC_MODEL_SIZE_CONST);
181 pps_payload->rc_edge_factor = DSC_RC_EDGE_FACTOR_CONST;
184 pps_payload->rc_quant_incr_limit0 =
185 dsc_cfg->rc_quant_incr_limit0;
188 pps_payload->rc_quant_incr_limit1 =
189 dsc_cfg->rc_quant_incr_limit1;
192 pps_payload->rc_tgt_offset = DSC_RC_TGT_OFFSET_LO_CONST |
193 DSC_RC_TGT_OFFSET_HI_CONST << DSC_PPS_RC_TGT_OFFSET_HI_SHIFT;
196 for (i = 0; i < DSC_NUM_BUF_RANGES - 1; i++)
197 pps_payload->rc_buf_thresh[i] =
198 dsc_cfg->rc_buf_thresh[i];
202 * For DSC sink programming the RC Range parameter fields
203 * are as follows: Min_qp[15:11], max_qp[10:6], offset[5:0]
205 for (i = 0; i < DSC_NUM_BUF_RANGES; i++) {
206 pps_payload->rc_range_parameters[i] =
207 ((dsc_cfg->rc_range_params[i].range_min_qp <<
208 DSC_PPS_RC_RANGE_MINQP_SHIFT) |
209 (dsc_cfg->rc_range_params[i].range_max_qp <<
210 DSC_PPS_RC_RANGE_MAXQP_SHIFT) |
211 (dsc_cfg->rc_range_params[i].range_bpg_offset));
212 pps_payload->rc_range_parameters[i] =
213 cpu_to_be16(pps_payload->rc_range_parameters[i]);
217 pps_payload->native_422_420 = dsc_cfg->native_422 |
218 dsc_cfg->native_420 << DSC_PPS_NATIVE_420_SHIFT;
221 pps_payload->second_line_bpg_offset =
222 dsc_cfg->second_line_bpg_offset;
225 pps_payload->nsl_bpg_offset =
226 cpu_to_be16(dsc_cfg->nsl_bpg_offset);
229 pps_payload->second_line_offset_adj =
230 cpu_to_be16(dsc_cfg->second_line_offset_adj);
232 /* PPS 94 - 127 are O */
234 EXPORT_SYMBOL(drm_dsc_pps_payload_pack);
236 static int compute_offset(struct drm_dsc_config *vdsc_cfg, int pixels_per_group,
237 int groups_per_line, int grpcnt)
240 int grpcnt_id = DIV_ROUND_UP(vdsc_cfg->initial_xmit_delay, pixels_per_group);
242 if (grpcnt <= grpcnt_id)
243 offset = DIV_ROUND_UP(grpcnt * pixels_per_group * vdsc_cfg->bits_per_pixel, 16);
245 offset = DIV_ROUND_UP(grpcnt_id * pixels_per_group * vdsc_cfg->bits_per_pixel, 16)
246 - (((grpcnt - grpcnt_id) * vdsc_cfg->slice_bpg_offset) >> 11);
248 if (grpcnt <= groups_per_line)
249 offset += grpcnt * vdsc_cfg->first_line_bpg_offset;
251 offset += groups_per_line * vdsc_cfg->first_line_bpg_offset
252 - (((grpcnt - groups_per_line) * vdsc_cfg->nfl_bpg_offset) >> 11);
254 if (vdsc_cfg->native_420) {
255 if (grpcnt <= groups_per_line)
256 offset -= (grpcnt * vdsc_cfg->nsl_bpg_offset) >> 11;
257 else if (grpcnt <= 2 * groups_per_line)
258 offset += (grpcnt - groups_per_line) * vdsc_cfg->second_line_bpg_offset
259 - ((groups_per_line * vdsc_cfg->nsl_bpg_offset) >> 11);
261 offset += (grpcnt - groups_per_line) * vdsc_cfg->second_line_bpg_offset
262 - (((grpcnt - groups_per_line) * vdsc_cfg->nsl_bpg_offset) >> 11);
269 * drm_dsc_compute_rc_parameters() - Write rate control
270 * parameters to the dsc configuration defined in
271 * &struct drm_dsc_config in accordance with the DSC 1.2
272 * specification. Some configuration fields must be present
276 * DSC Configuration data partially filled by driver
278 int drm_dsc_compute_rc_parameters(struct drm_dsc_config *vdsc_cfg)
280 unsigned long groups_per_line = 0;
281 unsigned long groups_total = 0;
282 unsigned long num_extra_mux_bits = 0;
283 unsigned long slice_bits = 0;
284 unsigned long hrd_delay = 0;
285 unsigned long final_scale = 0;
286 unsigned long rbs_min = 0;
287 unsigned long max_offset = 0;
289 if (vdsc_cfg->native_420 || vdsc_cfg->native_422) {
290 /* Number of groups used to code each line of a slice */
291 groups_per_line = DIV_ROUND_UP(vdsc_cfg->slice_width / 2,
292 DSC_RC_PIXELS_PER_GROUP);
294 /* chunksize in Bytes */
295 vdsc_cfg->slice_chunk_size = DIV_ROUND_UP(vdsc_cfg->slice_width / 2 *
296 vdsc_cfg->bits_per_pixel,
299 /* Number of groups used to code each line of a slice */
300 groups_per_line = DIV_ROUND_UP(vdsc_cfg->slice_width,
301 DSC_RC_PIXELS_PER_GROUP);
303 /* chunksize in Bytes */
304 vdsc_cfg->slice_chunk_size = DIV_ROUND_UP(vdsc_cfg->slice_width *
305 vdsc_cfg->bits_per_pixel,
309 if (vdsc_cfg->convert_rgb)
310 num_extra_mux_bits = 3 * (vdsc_cfg->mux_word_size +
311 (4 * vdsc_cfg->bits_per_component + 4)
313 else if (vdsc_cfg->native_422)
314 num_extra_mux_bits = 4 * vdsc_cfg->mux_word_size +
315 (4 * vdsc_cfg->bits_per_component + 4) +
316 3 * (4 * vdsc_cfg->bits_per_component) - 2;
318 num_extra_mux_bits = 3 * vdsc_cfg->mux_word_size +
319 (4 * vdsc_cfg->bits_per_component + 4) +
320 2 * (4 * vdsc_cfg->bits_per_component) - 2;
321 /* Number of bits in one Slice */
322 slice_bits = 8 * vdsc_cfg->slice_chunk_size * vdsc_cfg->slice_height;
324 while ((num_extra_mux_bits > 0) &&
325 ((slice_bits - num_extra_mux_bits) % vdsc_cfg->mux_word_size))
326 num_extra_mux_bits--;
328 if (groups_per_line < vdsc_cfg->initial_scale_value - 8)
329 vdsc_cfg->initial_scale_value = groups_per_line + 8;
331 /* scale_decrement_interval calculation according to DSC spec 1.11 */
332 if (vdsc_cfg->initial_scale_value > 8)
333 vdsc_cfg->scale_decrement_interval = groups_per_line /
334 (vdsc_cfg->initial_scale_value - 8);
336 vdsc_cfg->scale_decrement_interval = DSC_SCALE_DECREMENT_INTERVAL_MAX;
338 vdsc_cfg->final_offset = vdsc_cfg->rc_model_size -
339 (vdsc_cfg->initial_xmit_delay *
340 vdsc_cfg->bits_per_pixel + 8) / 16 + num_extra_mux_bits;
342 if (vdsc_cfg->final_offset >= vdsc_cfg->rc_model_size) {
343 DRM_DEBUG_KMS("FinalOfs < RcModelSze for this InitialXmitDelay\n");
347 final_scale = (vdsc_cfg->rc_model_size * 8) /
348 (vdsc_cfg->rc_model_size - vdsc_cfg->final_offset);
349 if (vdsc_cfg->slice_height > 1)
351 * NflBpgOffset is 16 bit value with 11 fractional bits
352 * hence we multiply by 2^11 for preserving the
355 vdsc_cfg->nfl_bpg_offset = DIV_ROUND_UP((vdsc_cfg->first_line_bpg_offset << 11),
356 (vdsc_cfg->slice_height - 1));
358 vdsc_cfg->nfl_bpg_offset = 0;
361 if (vdsc_cfg->nfl_bpg_offset > 65535) {
362 DRM_DEBUG_KMS("NflBpgOffset is too large for this slice height\n");
366 if (vdsc_cfg->slice_height > 2)
367 vdsc_cfg->nsl_bpg_offset = DIV_ROUND_UP((vdsc_cfg->second_line_bpg_offset << 11),
368 (vdsc_cfg->slice_height - 1));
370 vdsc_cfg->nsl_bpg_offset = 0;
372 if (vdsc_cfg->nsl_bpg_offset > 65535) {
373 DRM_DEBUG_KMS("NslBpgOffset is too large for this slice height\n");
377 /* Number of groups used to code the entire slice */
378 groups_total = groups_per_line * vdsc_cfg->slice_height;
380 /* slice_bpg_offset is 16 bit value with 11 fractional bits */
381 vdsc_cfg->slice_bpg_offset = DIV_ROUND_UP(((vdsc_cfg->rc_model_size -
382 vdsc_cfg->initial_offset +
383 num_extra_mux_bits) << 11),
386 if (final_scale > 9) {
388 * ScaleIncrementInterval =
389 * finaloffset/((NflBpgOffset + SliceBpgOffset)*8(finalscale - 1.125))
390 * as (NflBpgOffset + SliceBpgOffset) has 11 bit fractional value,
391 * we need divide by 2^11 from pstDscCfg values
393 vdsc_cfg->scale_increment_interval =
394 (vdsc_cfg->final_offset * (1 << 11)) /
395 ((vdsc_cfg->nfl_bpg_offset +
396 vdsc_cfg->nsl_bpg_offset +
397 vdsc_cfg->slice_bpg_offset) *
401 * If finalScaleValue is less than or equal to 9, a value of 0 should
402 * be used to disable the scale increment at the end of the slice
404 vdsc_cfg->scale_increment_interval = 0;
407 if (vdsc_cfg->scale_increment_interval > 65535) {
408 DRM_DEBUG_KMS("ScaleIncrementInterval is large for slice height\n");
413 * DSC spec mentions that bits_per_pixel specifies the target
414 * bits/pixel (bpp) rate that is used by the encoder,
415 * in steps of 1/16 of a bit per pixel
417 if (vdsc_cfg->dsc_version_minor == 2 && (vdsc_cfg->native_420 || vdsc_cfg->native_422)) {
419 max_offset = compute_offset(vdsc_cfg, DSC_RC_PIXELS_PER_GROUP, groups_per_line,
420 DIV_ROUND_UP(vdsc_cfg->initial_xmit_delay,
421 DSC_RC_PIXELS_PER_GROUP));
423 max_offset = MAX(max_offset,
424 compute_offset(vdsc_cfg, DSC_RC_PIXELS_PER_GROUP, groups_per_line,
425 DIV_ROUND_UP(vdsc_cfg->initial_xmit_delay,
428 max_offset = MAX(max_offset,
429 compute_offset(vdsc_cfg, DSC_RC_PIXELS_PER_GROUP, groups_per_line,
430 DIV_ROUND_UP(vdsc_cfg->initial_xmit_delay,
431 groups_per_line * 2)));
433 rbs_min = vdsc_cfg->rc_model_size - vdsc_cfg->initial_offset + max_offset;
435 rbs_min = vdsc_cfg->rc_model_size - vdsc_cfg->initial_offset +
436 DIV_ROUND_UP(vdsc_cfg->initial_xmit_delay *
437 vdsc_cfg->bits_per_pixel, 16) +
438 groups_per_line * vdsc_cfg->first_line_bpg_offset;
441 hrd_delay = DIV_ROUND_UP((rbs_min * 16), vdsc_cfg->bits_per_pixel);
442 vdsc_cfg->rc_bits = (hrd_delay * vdsc_cfg->bits_per_pixel) / 16;
443 vdsc_cfg->initial_dec_delay = hrd_delay - vdsc_cfg->initial_xmit_delay;
447 EXPORT_SYMBOL(drm_dsc_compute_rc_parameters);