1 /*
2  * Copyright 2018 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  * Authors: AMD
23  *
24  */
25 
26 #include "mod_info_packet.h"
27 #include "core_types.h"
28 #include "dc_types.h"
29 #include "mod_shared.h"
30 #include "mod_freesync.h"
31 #include "dc.h"
32 
33 #define HDMI_INFOFRAME_TYPE_VENDOR 0x81
34 
35 // VTEM Byte Offset
36 #define VTEM_PB0		0
37 #define VTEM_PB1		1
38 #define VTEM_PB2		2
39 #define VTEM_PB3		3
40 #define VTEM_PB4		4
41 #define VTEM_PB5		5
42 #define VTEM_PB6		6
43 
44 #define VTEM_MD0		7
45 #define VTEM_MD1		8
46 #define VTEM_MD2		9
47 #define VTEM_MD3		10
48 
49 
50 // VTEM Byte Masks
51 //PB0
52 #define MASK_VTEM_PB0__RESERVED0  0x01
53 #define MASK_VTEM_PB0__SYNC       0x02
54 #define MASK_VTEM_PB0__VFR        0x04
55 #define MASK_VTEM_PB0__AFR        0x08
56 #define MASK_VTEM_PB0__DS_TYPE    0x30
57 	//0: Periodic pseudo-static EM Data Set
58 	//1: Periodic dynamic EM Data Set
59 	//2: Unique EM Data Set
60 	//3: Reserved
61 #define MASK_VTEM_PB0__END        0x40
62 #define MASK_VTEM_PB0__NEW        0x80
63 
64 //PB1
65 #define MASK_VTEM_PB1__RESERVED1 0xFF
66 
67 //PB2
68 #define MASK_VTEM_PB2__ORGANIZATION_ID 0xFF
69 	//0: This is a Vendor Specific EM Data Set
70 	//1: This EM Data Set is defined by This Specification (HDMI 2.1 r102.clean)
71 	//2: This EM Data Set is defined by CTA-861-G
72 	//3: This EM Data Set is defined by VESA
73 //PB3
74 #define MASK_VTEM_PB3__DATA_SET_TAG_MSB    0xFF
75 //PB4
76 #define MASK_VTEM_PB4__DATA_SET_TAG_LSB    0xFF
77 //PB5
78 #define MASK_VTEM_PB5__DATA_SET_LENGTH_MSB 0xFF
79 //PB6
80 #define MASK_VTEM_PB6__DATA_SET_LENGTH_LSB 0xFF
81 
82 
83 
84 //PB7-27 (20 bytes):
85 //PB7 = MD0
86 #define MASK_VTEM_MD0__VRR_EN         0x01
87 #define MASK_VTEM_MD0__M_CONST        0x02
88 #define MASK_VTEM_MD0__RESERVED2      0x0C
89 #define MASK_VTEM_MD0__FVA_FACTOR_M1  0xF0
90 
91 //MD1
92 #define MASK_VTEM_MD1__BASE_VFRONT    0xFF
93 
94 //MD2
95 #define MASK_VTEM_MD2__BASE_REFRESH_RATE_98  0x03
96 #define MASK_VTEM_MD2__RB                    0x04
97 #define MASK_VTEM_MD2__RESERVED3             0xF8
98 
99 //MD3
100 #define MASK_VTEM_MD3__BASE_REFRESH_RATE_07  0xFF
101 
102 enum ColorimetryRGBDP {
103 	ColorimetryRGB_DP_sRGB               = 0,
104 	ColorimetryRGB_DP_AdobeRGB           = 3,
105 	ColorimetryRGB_DP_P3                 = 4,
106 	ColorimetryRGB_DP_CustomColorProfile = 5,
107 	ColorimetryRGB_DP_ITU_R_BT2020RGB    = 6,
108 };
109 enum ColorimetryYCCDP {
110 	ColorimetryYCC_DP_ITU601        = 0,
111 	ColorimetryYCC_DP_ITU709        = 1,
112 	ColorimetryYCC_DP_AdobeYCC      = 5,
113 	ColorimetryYCC_DP_ITU2020YCC    = 6,
114 	ColorimetryYCC_DP_ITU2020YCbCr  = 7,
115 };
116 
117 void mod_build_vsc_infopacket(const struct dc_stream_state *stream,
118 		struct dc_info_packet *info_packet)
119 {
120 	unsigned int vscPacketRevision = 0;
121 	unsigned int i;
122 	unsigned int pixelEncoding = 0;
123 	unsigned int colorimetryFormat = 0;
124 	bool stereo3dSupport = false;
125 
126 	if (stream->timing.timing_3d_format != TIMING_3D_FORMAT_NONE && stream->view_format != VIEW_3D_FORMAT_NONE) {
127 		vscPacketRevision = 1;
128 		stereo3dSupport = true;
129 	}
130 
131 	/*VSC packet set to 2 when DP revision >= 1.2*/
132 	if (stream->psr_version != 0)
133 		vscPacketRevision = 2;
134 
135 	/* Update to revision 5 for extended colorimetry support for DPCD 1.4+ */
136 	if (stream->link->dpcd_caps.dpcd_rev.raw >= 0x14 &&
137 			stream->link->dpcd_caps.dprx_feature.bits.VSC_SDP_COLORIMETRY_SUPPORTED)
138 		vscPacketRevision = 5;
139 
140 	/* VSC packet not needed based on the features
141 	 * supported by this DP display
142 	 */
143 	if (vscPacketRevision == 0)
144 		return;
145 
146 	if (vscPacketRevision == 0x2) {
147 		/* Secondary-data Packet ID = 0*/
148 		info_packet->hb0 = 0x00;
149 		/* 07h - Packet Type Value indicating Video
150 		 * Stream Configuration packet
151 		 */
152 		info_packet->hb1 = 0x07;
153 		/* 02h = VSC SDP supporting 3D stereo and PSR
154 		 * (applies to eDP v1.3 or higher).
155 		 */
156 		info_packet->hb2 = 0x02;
157 		/* 08h = VSC packet supporting 3D stereo + PSR
158 		 * (HB2 = 02h).
159 		 */
160 		info_packet->hb3 = 0x08;
161 
162 		for (i = 0; i < 28; i++)
163 			info_packet->sb[i] = 0;
164 
165 		info_packet->valid = true;
166 	}
167 
168 	if (vscPacketRevision == 0x1) {
169 
170 		info_packet->hb0 = 0x00;	// Secondary-data Packet ID = 0
171 		info_packet->hb1 = 0x07;	// 07h = Packet Type Value indicating Video Stream Configuration packet
172 		info_packet->hb2 = 0x01;	// 01h = Revision number. VSC SDP supporting 3D stereo only
173 		info_packet->hb3 = 0x01;	// 01h = VSC SDP supporting 3D stereo only (HB2 = 01h).
174 
175 		info_packet->valid = true;
176 	}
177 
178 	if (stereo3dSupport) {
179 		/* ==============================================================================================================|
180 		 * A. STEREO 3D
181 		 * ==============================================================================================================|
182 		 * VSC Payload (1 byte) From DP1.2 spec
183 		 *
184 		 * Bits 3:0 (Stereo Interface Method Code)  |  Bits 7:4 (Stereo Interface Method Specific Parameter)
185 		 * -----------------------------------------------------------------------------------------------------
186 		 * 0 = Non Stereo Video                     |  Must be set to 0x0
187 		 * -----------------------------------------------------------------------------------------------------
188 		 * 1 = Frame/Field Sequential               |  0x0: L + R view indication based on MISC1 bit 2:1
189 		 *                                          |  0x1: Right when Stereo Signal = 1
190 		 *                                          |  0x2: Left when Stereo Signal = 1
191 		 *                                          |  (others reserved)
192 		 * -----------------------------------------------------------------------------------------------------
193 		 * 2 = Stacked Frame                        |  0x0: Left view is on top and right view on bottom
194 		 *                                          |  (others reserved)
195 		 * -----------------------------------------------------------------------------------------------------
196 		 * 3 = Pixel Interleaved                    |  0x0: horiz interleaved, right view pixels on even lines
197 		 *                                          |  0x1: horiz interleaved, right view pixels on odd lines
198 		 *                                          |  0x2: checker board, start with left view pixel
199 		 *                                          |  0x3: vertical interleaved, start with left view pixels
200 		 *                                          |  0x4: vertical interleaved, start with right view pixels
201 		 *                                          |  (others reserved)
202 		 * -----------------------------------------------------------------------------------------------------
203 		 * 4 = Side-by-side                         |  0x0: left half represents left eye view
204 		 *                                          |  0x1: left half represents right eye view
205 		 */
206 		switch (stream->timing.timing_3d_format) {
207 		case TIMING_3D_FORMAT_HW_FRAME_PACKING:
208 		case TIMING_3D_FORMAT_SW_FRAME_PACKING:
209 		case TIMING_3D_FORMAT_TOP_AND_BOTTOM:
210 		case TIMING_3D_FORMAT_TB_SW_PACKED:
211 			info_packet->sb[0] = 0x02; // Stacked Frame, Left view is on top and right view on bottom.
212 			break;
213 		case TIMING_3D_FORMAT_DP_HDMI_INBAND_FA:
214 		case TIMING_3D_FORMAT_INBAND_FA:
215 			info_packet->sb[0] = 0x01; // Frame/Field Sequential, L + R view indication based on MISC1 bit 2:1
216 			break;
217 		case TIMING_3D_FORMAT_SIDE_BY_SIDE:
218 		case TIMING_3D_FORMAT_SBS_SW_PACKED:
219 			info_packet->sb[0] = 0x04; // Side-by-side
220 			break;
221 		default:
222 			info_packet->sb[0] = 0x00; // No Stereo Video, Shall be cleared to 0x0.
223 			break;
224 		}
225 
226 	}
227 
228 	/* 05h = VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/Colorimetry Format indication.
229 	 *   Added in DP1.3, a DP Source device is allowed to indicate the pixel encoding/colorimetry
230 	 *   format to the DP Sink device with VSC SDP only when the DP Sink device supports it
231 	 *   (i.e., VSC_SDP_EXTENSION_FOR_COLORIMETRY_SUPPORTED bit in the DPRX_FEATURE_ENUMERATION_LIST
232 	 *   register (DPCD Address 02210h, bit 3) is set to 1).
233 	 *   (Requires VSC_SDP_EXTENSION_FOR_COLORIMETRY_SUPPORTED bit set to 1 in DPCD 02210h. This
234 	 *   DPCD register is exposed in the new Extended Receiver Capability field for DPCD Rev. 1.4
235 	 *   (and higher). When MISC1. bit 6. is Set to 1, a Source device uses a VSC SDP to indicate
236 	 *   the Pixel Encoding/Colorimetry Format and that a Sink device must ignore MISC1, bit 7, and
237 	 *   MISC0, bits 7:1 (MISC1, bit 7. and MISC0, bits 7:1 become "don't care").)
238 	 */
239 	if (vscPacketRevision == 0x5) {
240 		/* Secondary-data Packet ID = 0 */
241 		info_packet->hb0 = 0x00;
242 		/* 07h - Packet Type Value indicating Video Stream Configuration packet */
243 		info_packet->hb1 = 0x07;
244 		/* 05h = VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/Colorimetry Format indication. */
245 		info_packet->hb2 = 0x05;
246 		/* 13h = VSC SDP supporting 3D stereo, + PSR2, + Pixel Encoding/Colorimetry Format indication (HB2 = 05h). */
247 		info_packet->hb3 = 0x13;
248 
249 		info_packet->valid = true;
250 
251 		/* Set VSC SDP fields for pixel encoding and colorimetry format from DP 1.3 specs
252 		 * Data Bytes DB 18~16
253 		 * Bits 3:0 (Colorimetry Format)        |  Bits 7:4 (Pixel Encoding)
254 		 * ----------------------------------------------------------------------------------------------------
255 		 * 0x0 = sRGB                           |  0 = RGB
256 		 * 0x1 = RGB Wide Gamut Fixed Point
257 		 * 0x2 = RGB Wide Gamut Floating Point
258 		 * 0x3 = AdobeRGB
259 		 * 0x4 = DCI-P3
260 		 * 0x5 = CustomColorProfile
261 		 * (others reserved)
262 		 * ----------------------------------------------------------------------------------------------------
263 		 * 0x0 = ITU-R BT.601                   |  1 = YCbCr444
264 		 * 0x1 = ITU-R BT.709
265 		 * 0x2 = xvYCC601
266 		 * 0x3 = xvYCC709
267 		 * 0x4 = sYCC601
268 		 * 0x5 = AdobeYCC601
269 		 * 0x6 = ITU-R BT.2020 Y'cC'bcC'rc
270 		 * 0x7 = ITU-R BT.2020 Y'C'bC'r
271 		 * (others reserved)
272 		 * ----------------------------------------------------------------------------------------------------
273 		 * 0x0 = ITU-R BT.601                   |  2 = YCbCr422
274 		 * 0x1 = ITU-R BT.709
275 		 * 0x2 = xvYCC601
276 		 * 0x3 = xvYCC709
277 		 * 0x4 = sYCC601
278 		 * 0x5 = AdobeYCC601
279 		 * 0x6 = ITU-R BT.2020 Y'cC'bcC'rc
280 		 * 0x7 = ITU-R BT.2020 Y'C'bC'r
281 		 * (others reserved)
282 		 * ----------------------------------------------------------------------------------------------------
283 		 * 0x0 = ITU-R BT.601                   |  3 = YCbCr420
284 		 * 0x1 = ITU-R BT.709
285 		 * 0x2 = xvYCC601
286 		 * 0x3 = xvYCC709
287 		 * 0x4 = sYCC601
288 		 * 0x5 = AdobeYCC601
289 		 * 0x6 = ITU-R BT.2020 Y'cC'bcC'rc
290 		 * 0x7 = ITU-R BT.2020 Y'C'bC'r
291 		 * (others reserved)
292 		 * ----------------------------------------------------------------------------------------------------
293 		 * 0x0 =DICOM Part14 Grayscale          |  4 = Yonly
294 		 * Display Function
295 		 * (others reserved)
296 		 */
297 
298 		/* Set Pixel Encoding */
299 		switch (stream->timing.pixel_encoding) {
300 		case PIXEL_ENCODING_RGB:
301 			pixelEncoding = 0x0;  /* RGB = 0h */
302 			break;
303 		case PIXEL_ENCODING_YCBCR444:
304 			pixelEncoding = 0x1;  /* YCbCr444 = 1h */
305 			break;
306 		case PIXEL_ENCODING_YCBCR422:
307 			pixelEncoding = 0x2;  /* YCbCr422 = 2h */
308 			break;
309 		case PIXEL_ENCODING_YCBCR420:
310 			pixelEncoding = 0x3;  /* YCbCr420 = 3h */
311 			break;
312 		default:
313 			pixelEncoding = 0x0;  /* default RGB = 0h */
314 			break;
315 		}
316 
317 		/* Set Colorimetry format based on pixel encoding */
318 		switch (stream->timing.pixel_encoding) {
319 		case PIXEL_ENCODING_RGB:
320 			if ((stream->output_color_space == COLOR_SPACE_SRGB) ||
321 					(stream->output_color_space == COLOR_SPACE_SRGB_LIMITED))
322 				colorimetryFormat = ColorimetryRGB_DP_sRGB;
323 			else if (stream->output_color_space == COLOR_SPACE_ADOBERGB)
324 				colorimetryFormat = ColorimetryRGB_DP_AdobeRGB;
325 			else if ((stream->output_color_space == COLOR_SPACE_2020_RGB_FULLRANGE) ||
326 					(stream->output_color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE))
327 				colorimetryFormat = ColorimetryRGB_DP_ITU_R_BT2020RGB;
328 			break;
329 
330 		case PIXEL_ENCODING_YCBCR444:
331 		case PIXEL_ENCODING_YCBCR422:
332 		case PIXEL_ENCODING_YCBCR420:
333 			/* Note: xvYCC probably not supported correctly here on DP since colorspace translation
334 			 * loses distinction between BT601 vs xvYCC601 in translation
335 			 */
336 			if (stream->output_color_space == COLOR_SPACE_YCBCR601)
337 				colorimetryFormat = ColorimetryYCC_DP_ITU601;
338 			else if (stream->output_color_space == COLOR_SPACE_YCBCR709)
339 				colorimetryFormat = ColorimetryYCC_DP_ITU709;
340 			else if (stream->output_color_space == COLOR_SPACE_ADOBERGB)
341 				colorimetryFormat = ColorimetryYCC_DP_AdobeYCC;
342 			else if (stream->output_color_space == COLOR_SPACE_2020_YCBCR)
343 				colorimetryFormat = ColorimetryYCC_DP_ITU2020YCbCr;
344 			break;
345 
346 		default:
347 			colorimetryFormat = ColorimetryRGB_DP_sRGB;
348 			break;
349 		}
350 
351 		info_packet->sb[16] = (pixelEncoding << 4) | colorimetryFormat;
352 
353 		/* Set color depth */
354 		switch (stream->timing.display_color_depth) {
355 		case COLOR_DEPTH_666:
356 			/* NOTE: This is actually not valid for YCbCr pixel encoding to have 6 bpc
357 			 *       as of DP1.4 spec, but value of 0 probably reserved here for potential future use.
358 			 */
359 			info_packet->sb[17] = 0;
360 			break;
361 		case COLOR_DEPTH_888:
362 			info_packet->sb[17] = 1;
363 			break;
364 		case COLOR_DEPTH_101010:
365 			info_packet->sb[17] = 2;
366 			break;
367 		case COLOR_DEPTH_121212:
368 			info_packet->sb[17] = 3;
369 			break;
370 		/*case COLOR_DEPTH_141414: -- NO SUCH FORMAT IN DP SPEC */
371 		case COLOR_DEPTH_161616:
372 			info_packet->sb[17] = 4;
373 			break;
374 		default:
375 			info_packet->sb[17] = 0;
376 			break;
377 		}
378 
379 		/* all YCbCr are always limited range */
380 		if ((stream->output_color_space == COLOR_SPACE_SRGB_LIMITED) ||
381 				(stream->output_color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE) ||
382 				(pixelEncoding != 0x0)) {
383 			info_packet->sb[17] |= 0x80; /* DB17 bit 7 set to 1 for CEA timing. */
384 		}
385 
386 		/* Content Type (Bits 2:0)
387 		 *  0 = Not defined.
388 		 *  1 = Graphics.
389 		 *  2 = Photo.
390 		 *  3 = Video.
391 		 *  4 = Game.
392 		 */
393 		info_packet->sb[18] = 0;
394 	}
395 
396 }
397 
398