]>
Commit | Line | Data |
---|---|---|
a408c857 MN |
1 | // SPDX-License-Identifier: MIT |
2 | /* | |
3 | * Copyright © 2018 Intel Corp | |
4 | * | |
5 | * Author: | |
6 | * Manasi Navare <manasi.d.navare@intel.com> | |
7 | */ | |
8 | ||
9 | #include <linux/kernel.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/init.h> | |
12 | #include <linux/errno.h> | |
13 | #include <linux/byteorder/generic.h> | |
dc43332b | 14 | #include <drm/drm_print.h> |
a408c857 MN |
15 | #include <drm/drm_dp_helper.h> |
16 | #include <drm/drm_dsc.h> | |
17 | ||
18 | /** | |
19 | * DOC: dsc helpers | |
20 | * | |
05bad235 MN |
21 | * VESA specification for DP 1.4 adds a new feature called Display Stream |
22 | * Compression (DSC) used to compress the pixel bits before sending it on | |
23 | * DP/eDP/MIPI DSI interface. DSC is required to be enabled so that the existing | |
24 | * display interfaces can support high resolutions at higher frames rates uisng | |
25 | * the maximum available link capacity of these interfaces. | |
26 | * | |
a408c857 MN |
27 | * These functions contain some common logic and helpers to deal with VESA |
28 | * Display Stream Compression standard required for DSC on Display Port/eDP or | |
29 | * MIPI display interfaces. | |
30 | */ | |
31 | ||
32 | /** | |
33 | * drm_dsc_dp_pps_header_init() - Initializes the PPS Header | |
34 | * for DisplayPort as per the DP 1.4 spec. | |
dbfbe717 DF |
35 | * @pps_header: Secondary data packet header for DSC Picture |
36 | * Parameter Set as defined in &struct dp_sdp_header | |
05bad235 MN |
37 | * |
38 | * DP 1.4 spec defines the secondary data packet for sending the | |
39 | * picture parameter infoframes from the source to the sink. | |
dbfbe717 DF |
40 | * This function populates the SDP header defined in |
41 | * &struct dp_sdp_header. | |
a408c857 | 42 | */ |
dbfbe717 | 43 | void drm_dsc_dp_pps_header_init(struct dp_sdp_header *pps_header) |
a408c857 | 44 | { |
dbfbe717 | 45 | memset(pps_header, 0, sizeof(*pps_header)); |
a408c857 | 46 | |
dbfbe717 DF |
47 | pps_header->HB1 = DP_SDP_PPS; |
48 | pps_header->HB2 = DP_SDP_PPS_HEADER_PAYLOAD_BYTES_MINUS_1; | |
a408c857 MN |
49 | } |
50 | EXPORT_SYMBOL(drm_dsc_dp_pps_header_init); | |
51 | ||
52 | /** | |
dbfbe717 | 53 | * drm_dsc_pps_payload_pack() - Populates the DSC PPS |
a408c857 | 54 | * |
dbfbe717 DF |
55 | * @pps_payload: |
56 | * Bitwise struct for DSC Picture Parameter Set. This is defined | |
57 | * by &struct drm_dsc_picture_parameter_set | |
a408c857 | 58 | * @dsc_cfg: |
05bad235 MN |
59 | * DSC Configuration data filled by driver as defined by |
60 | * &struct drm_dsc_config | |
61 | * | |
dbfbe717 DF |
62 | * DSC source device sends a picture parameter set (PPS) containing the |
63 | * information required by the sink to decode the compressed frame. Driver | |
64 | * populates the DSC PPS struct using the DSC configuration parameters in | |
65 | * the order expected by the DSC Display Sink device. For the DSC, the sink | |
66 | * device expects the PPS payload in big endian format for fields | |
67 | * that span more than 1 byte. | |
a408c857 | 68 | */ |
dbfbe717 | 69 | void drm_dsc_pps_payload_pack(struct drm_dsc_picture_parameter_set *pps_payload, |
a408c857 MN |
70 | const struct drm_dsc_config *dsc_cfg) |
71 | { | |
72 | int i; | |
73 | ||
74 | /* Protect against someone accidently changing struct size */ | |
dbfbe717 | 75 | BUILD_BUG_ON(sizeof(*pps_payload) != |
a408c857 MN |
76 | DP_SDP_PPS_HEADER_PAYLOAD_BYTES_MINUS_1 + 1); |
77 | ||
dbfbe717 | 78 | memset(pps_payload, 0, sizeof(*pps_payload)); |
a408c857 MN |
79 | |
80 | /* PPS 0 */ | |
dbfbe717 | 81 | pps_payload->dsc_version = |
a408c857 MN |
82 | dsc_cfg->dsc_version_minor | |
83 | dsc_cfg->dsc_version_major << DSC_PPS_VERSION_MAJOR_SHIFT; | |
84 | ||
85 | /* PPS 1, 2 is 0 */ | |
86 | ||
87 | /* PPS 3 */ | |
dbfbe717 | 88 | pps_payload->pps_3 = |
a408c857 MN |
89 | dsc_cfg->line_buf_depth | |
90 | dsc_cfg->bits_per_component << DSC_PPS_BPC_SHIFT; | |
91 | ||
92 | /* PPS 4 */ | |
dbfbe717 | 93 | pps_payload->pps_4 = |
a408c857 MN |
94 | ((dsc_cfg->bits_per_pixel & DSC_PPS_BPP_HIGH_MASK) >> |
95 | DSC_PPS_MSB_SHIFT) | | |
96 | dsc_cfg->vbr_enable << DSC_PPS_VBR_EN_SHIFT | | |
06d7cecd | 97 | dsc_cfg->simple_422 << DSC_PPS_SIMPLE422_SHIFT | |
a408c857 MN |
98 | dsc_cfg->convert_rgb << DSC_PPS_CONVERT_RGB_SHIFT | |
99 | dsc_cfg->block_pred_enable << DSC_PPS_BLOCK_PRED_EN_SHIFT; | |
100 | ||
101 | /* PPS 5 */ | |
dbfbe717 | 102 | pps_payload->bits_per_pixel_low = |
a408c857 MN |
103 | (dsc_cfg->bits_per_pixel & DSC_PPS_LSB_MASK); |
104 | ||
105 | /* | |
106 | * The DSC panel expects the PPS packet to have big endian format | |
107 | * for data spanning 2 bytes. Use a macro cpu_to_be16() to convert | |
108 | * to big endian format. If format is little endian, it will swap | |
109 | * bytes to convert to Big endian else keep it unchanged. | |
110 | */ | |
111 | ||
112 | /* PPS 6, 7 */ | |
dbfbe717 | 113 | pps_payload->pic_height = cpu_to_be16(dsc_cfg->pic_height); |
a408c857 MN |
114 | |
115 | /* PPS 8, 9 */ | |
dbfbe717 | 116 | pps_payload->pic_width = cpu_to_be16(dsc_cfg->pic_width); |
a408c857 MN |
117 | |
118 | /* PPS 10, 11 */ | |
dbfbe717 | 119 | pps_payload->slice_height = cpu_to_be16(dsc_cfg->slice_height); |
a408c857 MN |
120 | |
121 | /* PPS 12, 13 */ | |
dbfbe717 | 122 | pps_payload->slice_width = cpu_to_be16(dsc_cfg->slice_width); |
a408c857 MN |
123 | |
124 | /* PPS 14, 15 */ | |
dbfbe717 | 125 | pps_payload->chunk_size = cpu_to_be16(dsc_cfg->slice_chunk_size); |
a408c857 MN |
126 | |
127 | /* PPS 16 */ | |
dbfbe717 | 128 | pps_payload->initial_xmit_delay_high = |
a408c857 MN |
129 | ((dsc_cfg->initial_xmit_delay & |
130 | DSC_PPS_INIT_XMIT_DELAY_HIGH_MASK) >> | |
131 | DSC_PPS_MSB_SHIFT); | |
132 | ||
133 | /* PPS 17 */ | |
dbfbe717 | 134 | pps_payload->initial_xmit_delay_low = |
a408c857 MN |
135 | (dsc_cfg->initial_xmit_delay & DSC_PPS_LSB_MASK); |
136 | ||
137 | /* PPS 18, 19 */ | |
dbfbe717 | 138 | pps_payload->initial_dec_delay = |
a408c857 MN |
139 | cpu_to_be16(dsc_cfg->initial_dec_delay); |
140 | ||
141 | /* PPS 20 is 0 */ | |
142 | ||
143 | /* PPS 21 */ | |
dbfbe717 | 144 | pps_payload->initial_scale_value = |
a408c857 MN |
145 | dsc_cfg->initial_scale_value; |
146 | ||
147 | /* PPS 22, 23 */ | |
dbfbe717 | 148 | pps_payload->scale_increment_interval = |
a408c857 MN |
149 | cpu_to_be16(dsc_cfg->scale_increment_interval); |
150 | ||
151 | /* PPS 24 */ | |
dbfbe717 | 152 | pps_payload->scale_decrement_interval_high = |
a408c857 MN |
153 | ((dsc_cfg->scale_decrement_interval & |
154 | DSC_PPS_SCALE_DEC_INT_HIGH_MASK) >> | |
155 | DSC_PPS_MSB_SHIFT); | |
156 | ||
157 | /* PPS 25 */ | |
dbfbe717 | 158 | pps_payload->scale_decrement_interval_low = |
a408c857 MN |
159 | (dsc_cfg->scale_decrement_interval & DSC_PPS_LSB_MASK); |
160 | ||
161 | /* PPS 26[7:0], PPS 27[7:5] RESERVED */ | |
162 | ||
163 | /* PPS 27 */ | |
dbfbe717 | 164 | pps_payload->first_line_bpg_offset = |
a408c857 MN |
165 | dsc_cfg->first_line_bpg_offset; |
166 | ||
167 | /* PPS 28, 29 */ | |
dbfbe717 | 168 | pps_payload->nfl_bpg_offset = |
a408c857 MN |
169 | cpu_to_be16(dsc_cfg->nfl_bpg_offset); |
170 | ||
171 | /* PPS 30, 31 */ | |
dbfbe717 | 172 | pps_payload->slice_bpg_offset = |
a408c857 MN |
173 | cpu_to_be16(dsc_cfg->slice_bpg_offset); |
174 | ||
175 | /* PPS 32, 33 */ | |
dbfbe717 | 176 | pps_payload->initial_offset = |
a408c857 MN |
177 | cpu_to_be16(dsc_cfg->initial_offset); |
178 | ||
179 | /* PPS 34, 35 */ | |
dbfbe717 | 180 | pps_payload->final_offset = cpu_to_be16(dsc_cfg->final_offset); |
a408c857 MN |
181 | |
182 | /* PPS 36 */ | |
dbfbe717 | 183 | pps_payload->flatness_min_qp = dsc_cfg->flatness_min_qp; |
a408c857 MN |
184 | |
185 | /* PPS 37 */ | |
dbfbe717 | 186 | pps_payload->flatness_max_qp = dsc_cfg->flatness_max_qp; |
a408c857 MN |
187 | |
188 | /* PPS 38, 39 */ | |
dbfbe717 | 189 | pps_payload->rc_model_size = |
a408c857 MN |
190 | cpu_to_be16(DSC_RC_MODEL_SIZE_CONST); |
191 | ||
192 | /* PPS 40 */ | |
dbfbe717 | 193 | pps_payload->rc_edge_factor = DSC_RC_EDGE_FACTOR_CONST; |
a408c857 MN |
194 | |
195 | /* PPS 41 */ | |
dbfbe717 | 196 | pps_payload->rc_quant_incr_limit0 = |
a408c857 MN |
197 | dsc_cfg->rc_quant_incr_limit0; |
198 | ||
199 | /* PPS 42 */ | |
dbfbe717 | 200 | pps_payload->rc_quant_incr_limit1 = |
a408c857 MN |
201 | dsc_cfg->rc_quant_incr_limit1; |
202 | ||
203 | /* PPS 43 */ | |
dbfbe717 | 204 | pps_payload->rc_tgt_offset = DSC_RC_TGT_OFFSET_LO_CONST | |
a408c857 MN |
205 | DSC_RC_TGT_OFFSET_HI_CONST << DSC_PPS_RC_TGT_OFFSET_HI_SHIFT; |
206 | ||
207 | /* PPS 44 - 57 */ | |
208 | for (i = 0; i < DSC_NUM_BUF_RANGES - 1; i++) | |
dbfbe717 | 209 | pps_payload->rc_buf_thresh[i] = |
a408c857 MN |
210 | dsc_cfg->rc_buf_thresh[i]; |
211 | ||
212 | /* PPS 58 - 87 */ | |
213 | /* | |
214 | * For DSC sink programming the RC Range parameter fields | |
215 | * are as follows: Min_qp[15:11], max_qp[10:6], offset[5:0] | |
216 | */ | |
217 | for (i = 0; i < DSC_NUM_BUF_RANGES; i++) { | |
dbfbe717 | 218 | pps_payload->rc_range_parameters[i] = |
a408c857 MN |
219 | ((dsc_cfg->rc_range_params[i].range_min_qp << |
220 | DSC_PPS_RC_RANGE_MINQP_SHIFT) | | |
221 | (dsc_cfg->rc_range_params[i].range_max_qp << | |
222 | DSC_PPS_RC_RANGE_MAXQP_SHIFT) | | |
223 | (dsc_cfg->rc_range_params[i].range_bpg_offset)); | |
dbfbe717 DF |
224 | pps_payload->rc_range_parameters[i] = |
225 | cpu_to_be16(pps_payload->rc_range_parameters[i]); | |
a408c857 MN |
226 | } |
227 | ||
228 | /* PPS 88 */ | |
dbfbe717 | 229 | pps_payload->native_422_420 = dsc_cfg->native_422 | |
a408c857 MN |
230 | dsc_cfg->native_420 << DSC_PPS_NATIVE_420_SHIFT; |
231 | ||
232 | /* PPS 89 */ | |
dbfbe717 | 233 | pps_payload->second_line_bpg_offset = |
a408c857 MN |
234 | dsc_cfg->second_line_bpg_offset; |
235 | ||
236 | /* PPS 90, 91 */ | |
dbfbe717 | 237 | pps_payload->nsl_bpg_offset = |
a408c857 MN |
238 | cpu_to_be16(dsc_cfg->nsl_bpg_offset); |
239 | ||
240 | /* PPS 92, 93 */ | |
dbfbe717 | 241 | pps_payload->second_line_offset_adj = |
a408c857 MN |
242 | cpu_to_be16(dsc_cfg->second_line_offset_adj); |
243 | ||
244 | /* PPS 94 - 127 are O */ | |
245 | } | |
dbfbe717 | 246 | EXPORT_SYMBOL(drm_dsc_pps_payload_pack); |
dc43332b DF |
247 | |
248 | /** | |
249 | * drm_dsc_compute_rc_parameters() - Write rate control | |
250 | * parameters to the dsc configuration defined in | |
06d7cecd | 251 | * &struct drm_dsc_config in accordance with the DSC 1.2 |
dc43332b DF |
252 | * specification. Some configuration fields must be present |
253 | * beforehand. | |
254 | * | |
255 | * @vdsc_cfg: | |
256 | * DSC Configuration data partially filled by driver | |
257 | */ | |
258 | int drm_dsc_compute_rc_parameters(struct drm_dsc_config *vdsc_cfg) | |
259 | { | |
260 | unsigned long groups_per_line = 0; | |
261 | unsigned long groups_total = 0; | |
262 | unsigned long num_extra_mux_bits = 0; | |
263 | unsigned long slice_bits = 0; | |
264 | unsigned long hrd_delay = 0; | |
265 | unsigned long final_scale = 0; | |
266 | unsigned long rbs_min = 0; | |
267 | ||
06d7cecd DF |
268 | if (vdsc_cfg->native_420 || vdsc_cfg->native_422) { |
269 | /* Number of groups used to code each line of a slice */ | |
270 | groups_per_line = DIV_ROUND_UP(vdsc_cfg->slice_width / 2, | |
271 | DSC_RC_PIXELS_PER_GROUP); | |
272 | ||
273 | /* chunksize in Bytes */ | |
274 | vdsc_cfg->slice_chunk_size = DIV_ROUND_UP(vdsc_cfg->slice_width / 2 * | |
275 | vdsc_cfg->bits_per_pixel, | |
276 | (8 * 16)); | |
277 | } else { | |
278 | /* Number of groups used to code each line of a slice */ | |
279 | groups_per_line = DIV_ROUND_UP(vdsc_cfg->slice_width, | |
280 | DSC_RC_PIXELS_PER_GROUP); | |
281 | ||
282 | /* chunksize in Bytes */ | |
283 | vdsc_cfg->slice_chunk_size = DIV_ROUND_UP(vdsc_cfg->slice_width * | |
284 | vdsc_cfg->bits_per_pixel, | |
285 | (8 * 16)); | |
286 | } | |
dc43332b DF |
287 | |
288 | if (vdsc_cfg->convert_rgb) | |
289 | num_extra_mux_bits = 3 * (vdsc_cfg->mux_word_size + | |
290 | (4 * vdsc_cfg->bits_per_component + 4) | |
291 | - 2); | |
06d7cecd DF |
292 | else if (vdsc_cfg->native_422) |
293 | num_extra_mux_bits = 4 * vdsc_cfg->mux_word_size + | |
294 | (4 * vdsc_cfg->bits_per_component + 4) + | |
295 | 3 * (4 * vdsc_cfg->bits_per_component) - 2; | |
dc43332b DF |
296 | else |
297 | num_extra_mux_bits = 3 * vdsc_cfg->mux_word_size + | |
298 | (4 * vdsc_cfg->bits_per_component + 4) + | |
299 | 2 * (4 * vdsc_cfg->bits_per_component) - 2; | |
300 | /* Number of bits in one Slice */ | |
301 | slice_bits = 8 * vdsc_cfg->slice_chunk_size * vdsc_cfg->slice_height; | |
302 | ||
303 | while ((num_extra_mux_bits > 0) && | |
304 | ((slice_bits - num_extra_mux_bits) % vdsc_cfg->mux_word_size)) | |
305 | num_extra_mux_bits--; | |
306 | ||
307 | if (groups_per_line < vdsc_cfg->initial_scale_value - 8) | |
308 | vdsc_cfg->initial_scale_value = groups_per_line + 8; | |
309 | ||
310 | /* scale_decrement_interval calculation according to DSC spec 1.11 */ | |
311 | if (vdsc_cfg->initial_scale_value > 8) | |
312 | vdsc_cfg->scale_decrement_interval = groups_per_line / | |
313 | (vdsc_cfg->initial_scale_value - 8); | |
314 | else | |
315 | vdsc_cfg->scale_decrement_interval = DSC_SCALE_DECREMENT_INTERVAL_MAX; | |
316 | ||
317 | vdsc_cfg->final_offset = vdsc_cfg->rc_model_size - | |
318 | (vdsc_cfg->initial_xmit_delay * | |
319 | vdsc_cfg->bits_per_pixel + 8) / 16 + num_extra_mux_bits; | |
320 | ||
321 | if (vdsc_cfg->final_offset >= vdsc_cfg->rc_model_size) { | |
322 | DRM_DEBUG_KMS("FinalOfs < RcModelSze for this InitialXmitDelay\n"); | |
323 | return -ERANGE; | |
324 | } | |
325 | ||
326 | final_scale = (vdsc_cfg->rc_model_size * 8) / | |
327 | (vdsc_cfg->rc_model_size - vdsc_cfg->final_offset); | |
328 | if (vdsc_cfg->slice_height > 1) | |
329 | /* | |
330 | * NflBpgOffset is 16 bit value with 11 fractional bits | |
331 | * hence we multiply by 2^11 for preserving the | |
332 | * fractional part | |
333 | */ | |
334 | vdsc_cfg->nfl_bpg_offset = DIV_ROUND_UP((vdsc_cfg->first_line_bpg_offset << 11), | |
335 | (vdsc_cfg->slice_height - 1)); | |
336 | else | |
337 | vdsc_cfg->nfl_bpg_offset = 0; | |
338 | ||
339 | /* 2^16 - 1 */ | |
340 | if (vdsc_cfg->nfl_bpg_offset > 65535) { | |
341 | DRM_DEBUG_KMS("NflBpgOffset is too large for this slice height\n"); | |
342 | return -ERANGE; | |
343 | } | |
344 | ||
345 | /* Number of groups used to code the entire slice */ | |
346 | groups_total = groups_per_line * vdsc_cfg->slice_height; | |
347 | ||
348 | /* slice_bpg_offset is 16 bit value with 11 fractional bits */ | |
349 | vdsc_cfg->slice_bpg_offset = DIV_ROUND_UP(((vdsc_cfg->rc_model_size - | |
350 | vdsc_cfg->initial_offset + | |
351 | num_extra_mux_bits) << 11), | |
352 | groups_total); | |
353 | ||
354 | if (final_scale > 9) { | |
355 | /* | |
356 | * ScaleIncrementInterval = | |
357 | * finaloffset/((NflBpgOffset + SliceBpgOffset)*8(finalscale - 1.125)) | |
358 | * as (NflBpgOffset + SliceBpgOffset) has 11 bit fractional value, | |
359 | * we need divide by 2^11 from pstDscCfg values | |
360 | */ | |
361 | vdsc_cfg->scale_increment_interval = | |
362 | (vdsc_cfg->final_offset * (1 << 11)) / | |
363 | ((vdsc_cfg->nfl_bpg_offset + | |
364 | vdsc_cfg->slice_bpg_offset) * | |
365 | (final_scale - 9)); | |
366 | } else { | |
367 | /* | |
368 | * If finalScaleValue is less than or equal to 9, a value of 0 should | |
369 | * be used to disable the scale increment at the end of the slice | |
370 | */ | |
371 | vdsc_cfg->scale_increment_interval = 0; | |
372 | } | |
373 | ||
374 | if (vdsc_cfg->scale_increment_interval > 65535) { | |
375 | DRM_DEBUG_KMS("ScaleIncrementInterval is large for slice height\n"); | |
376 | return -ERANGE; | |
377 | } | |
378 | ||
379 | /* | |
380 | * DSC spec mentions that bits_per_pixel specifies the target | |
381 | * bits/pixel (bpp) rate that is used by the encoder, | |
382 | * in steps of 1/16 of a bit per pixel | |
383 | */ | |
384 | rbs_min = vdsc_cfg->rc_model_size - vdsc_cfg->initial_offset + | |
385 | DIV_ROUND_UP(vdsc_cfg->initial_xmit_delay * | |
386 | vdsc_cfg->bits_per_pixel, 16) + | |
387 | groups_per_line * vdsc_cfg->first_line_bpg_offset; | |
388 | ||
389 | hrd_delay = DIV_ROUND_UP((rbs_min * 16), vdsc_cfg->bits_per_pixel); | |
390 | vdsc_cfg->rc_bits = (hrd_delay * vdsc_cfg->bits_per_pixel) / 16; | |
391 | vdsc_cfg->initial_dec_delay = hrd_delay - vdsc_cfg->initial_xmit_delay; | |
392 | ||
393 | return 0; | |
394 | } | |
395 | EXPORT_SYMBOL(drm_dsc_compute_rc_parameters); |