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 
31 #define HDMI_INFOFRAME_TYPE_VENDOR 0x81
32 
33 enum ColorimetryRGBDP {
34 	ColorimetryRGB_DP_sRGB               = 0,
35 	ColorimetryRGB_DP_AdobeRGB           = 3,
36 	ColorimetryRGB_DP_P3                 = 4,
37 	ColorimetryRGB_DP_CustomColorProfile = 5,
38 	ColorimetryRGB_DP_ITU_R_BT2020RGB    = 6,
39 };
40 enum ColorimetryYCCDP {
41 	ColorimetryYCC_DP_ITU601        = 0,
42 	ColorimetryYCC_DP_ITU709        = 1,
43 	ColorimetryYCC_DP_AdobeYCC      = 5,
44 	ColorimetryYCC_DP_ITU2020YCC    = 6,
45 	ColorimetryYCC_DP_ITU2020YCbCr  = 7,
46 };
47 
48 void mod_build_vsc_infopacket(const struct dc_stream_state *stream,
49 		struct dc_info_packet *info_packet)
50 {
51 	unsigned int vscPacketRevision = 0;
52 	unsigned int i;
53 	unsigned int pixelEncoding = 0;
54 	unsigned int colorimetryFormat = 0;
55 	bool stereo3dSupport = false;
56 
57 	if (stream->timing.timing_3d_format != TIMING_3D_FORMAT_NONE && stream->view_format != VIEW_3D_FORMAT_NONE) {
58 		vscPacketRevision = 1;
59 		stereo3dSupport = true;
60 	}
61 
62 	/*VSC packet set to 2 when DP revision >= 1.2*/
63 	if (stream->psr_version != 0)
64 		vscPacketRevision = 2;
65 
66 	if (stream->timing.pixel_encoding == PIXEL_ENCODING_YCBCR420)
67 		vscPacketRevision = 5;
68 
69 	/* VSC packet not needed based on the features
70 	 * supported by this DP display
71 	 */
72 	if (vscPacketRevision == 0)
73 		return;
74 
75 	if (vscPacketRevision == 0x2) {
76 		/* Secondary-data Packet ID = 0*/
77 		info_packet->hb0 = 0x00;
78 		/* 07h - Packet Type Value indicating Video
79 		 * Stream Configuration packet
80 		 */
81 		info_packet->hb1 = 0x07;
82 		/* 02h = VSC SDP supporting 3D stereo and PSR
83 		 * (applies to eDP v1.3 or higher).
84 		 */
85 		info_packet->hb2 = 0x02;
86 		/* 08h = VSC packet supporting 3D stereo + PSR
87 		 * (HB2 = 02h).
88 		 */
89 		info_packet->hb3 = 0x08;
90 
91 		for (i = 0; i < 28; i++)
92 			info_packet->sb[i] = 0;
93 
94 		info_packet->valid = true;
95 	}
96 
97 	if (vscPacketRevision == 0x1) {
98 
99 		info_packet->hb0 = 0x00;	// Secondary-data Packet ID = 0
100 		info_packet->hb1 = 0x07;	// 07h = Packet Type Value indicating Video Stream Configuration packet
101 		info_packet->hb2 = 0x01;	// 01h = Revision number. VSC SDP supporting 3D stereo only
102 		info_packet->hb3 = 0x01;	// 01h = VSC SDP supporting 3D stereo only (HB2 = 01h).
103 
104 		info_packet->valid = true;
105 	}
106 
107 	if (stereo3dSupport) {
108 		/* ==============================================================================================================|
109 		 * A. STEREO 3D
110 		 * ==============================================================================================================|
111 		 * VSC Payload (1 byte) From DP1.2 spec
112 		 *
113 		 * Bits 3:0 (Stereo Interface Method Code)  |  Bits 7:4 (Stereo Interface Method Specific Parameter)
114 		 * -----------------------------------------------------------------------------------------------------
115 		 * 0 = Non Stereo Video                     |  Must be set to 0x0
116 		 * -----------------------------------------------------------------------------------------------------
117 		 * 1 = Frame/Field Sequential               |  0x0: L + R view indication based on MISC1 bit 2:1
118 		 *                                          |  0x1: Right when Stereo Signal = 1
119 		 *                                          |  0x2: Left when Stereo Signal = 1
120 		 *                                          |  (others reserved)
121 		 * -----------------------------------------------------------------------------------------------------
122 		 * 2 = Stacked Frame                        |  0x0: Left view is on top and right view on bottom
123 		 *                                          |  (others reserved)
124 		 * -----------------------------------------------------------------------------------------------------
125 		 * 3 = Pixel Interleaved                    |  0x0: horiz interleaved, right view pixels on even lines
126 		 *                                          |  0x1: horiz interleaved, right view pixels on odd lines
127 		 *                                          |  0x2: checker board, start with left view pixel
128 		 *                                          |  0x3: vertical interleaved, start with left view pixels
129 		 *                                          |  0x4: vertical interleaved, start with right view pixels
130 		 *                                          |  (others reserved)
131 		 * -----------------------------------------------------------------------------------------------------
132 		 * 4 = Side-by-side                         |  0x0: left half represents left eye view
133 		 *                                          |  0x1: left half represents right eye view
134 		 */
135 		switch (stream->timing.timing_3d_format) {
136 		case TIMING_3D_FORMAT_HW_FRAME_PACKING:
137 		case TIMING_3D_FORMAT_SW_FRAME_PACKING:
138 		case TIMING_3D_FORMAT_TOP_AND_BOTTOM:
139 		case TIMING_3D_FORMAT_TB_SW_PACKED:
140 			info_packet->sb[0] = 0x02; // Stacked Frame, Left view is on top and right view on bottom.
141 			break;
142 		case TIMING_3D_FORMAT_DP_HDMI_INBAND_FA:
143 		case TIMING_3D_FORMAT_INBAND_FA:
144 			info_packet->sb[0] = 0x01; // Frame/Field Sequential, L + R view indication based on MISC1 bit 2:1
145 			break;
146 		case TIMING_3D_FORMAT_SIDE_BY_SIDE:
147 		case TIMING_3D_FORMAT_SBS_SW_PACKED:
148 			info_packet->sb[0] = 0x04; // Side-by-side
149 			break;
150 		default:
151 			info_packet->sb[0] = 0x00; // No Stereo Video, Shall be cleared to 0x0.
152 			break;
153 		}
154 
155 	}
156 
157 	/* 05h = VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/Colorimetry Format indication.
158 	 *   Added in DP1.3, a DP Source device is allowed to indicate the pixel encoding/colorimetry
159 	 *   format to the DP Sink device with VSC SDP only when the DP Sink device supports it
160 	 *   (i.e., VSC_SDP_EXTENSION_FOR_COLORIMETRY_SUPPORTED bit in the DPRX_FEATURE_ENUMERATION_LIST
161 	 *   register (DPCD Address 02210h, bit 3) is set to 1).
162 	 *   (Requires VSC_SDP_EXTENSION_FOR_COLORIMETRY_SUPPORTED bit set to 1 in DPCD 02210h. This
163 	 *   DPCD register is exposed in the new Extended Receiver Capability field for DPCD Rev. 1.4
164 	 *   (and higher). When MISC1. bit 6. is Set to 1, a Source device uses a VSC SDP to indicate
165 	 *   the Pixel Encoding/Colorimetry Format and that a Sink device must ignore MISC1, bit 7, and
166 	 *   MISC0, bits 7:1 (MISC1, bit 7. and MISC0, bits 7:1 become "don't care").)
167 	 */
168 	if (vscPacketRevision == 0x5) {
169 		/* Secondary-data Packet ID = 0 */
170 		info_packet->hb0 = 0x00;
171 		/* 07h - Packet Type Value indicating Video Stream Configuration packet */
172 		info_packet->hb1 = 0x07;
173 		/* 05h = VSC SDP supporting 3D stereo, PSR2, and Pixel Encoding/Colorimetry Format indication. */
174 		info_packet->hb2 = 0x05;
175 		/* 13h = VSC SDP supporting 3D stereo, + PSR2, + Pixel Encoding/Colorimetry Format indication (HB2 = 05h). */
176 		info_packet->hb3 = 0x13;
177 
178 		info_packet->valid = true;
179 
180 		/* Set VSC SDP fields for pixel encoding and colorimetry format from DP 1.3 specs
181 		 * Data Bytes DB 18~16
182 		 * Bits 3:0 (Colorimetry Format)        |  Bits 7:4 (Pixel Encoding)
183 		 * ----------------------------------------------------------------------------------------------------
184 		 * 0x0 = sRGB                           |  0 = RGB
185 		 * 0x1 = RGB Wide Gamut Fixed Point
186 		 * 0x2 = RGB Wide Gamut Floating Point
187 		 * 0x3 = AdobeRGB
188 		 * 0x4 = DCI-P3
189 		 * 0x5 = CustomColorProfile
190 		 * (others reserved)
191 		 * ----------------------------------------------------------------------------------------------------
192 		 * 0x0 = ITU-R BT.601                   |  1 = YCbCr444
193 		 * 0x1 = ITU-R BT.709
194 		 * 0x2 = xvYCC601
195 		 * 0x3 = xvYCC709
196 		 * 0x4 = sYCC601
197 		 * 0x5 = AdobeYCC601
198 		 * 0x6 = ITU-R BT.2020 Y'cC'bcC'rc
199 		 * 0x7 = ITU-R BT.2020 Y'C'bC'r
200 		 * (others reserved)
201 		 * ----------------------------------------------------------------------------------------------------
202 		 * 0x0 = ITU-R BT.601                   |  2 = YCbCr422
203 		 * 0x1 = ITU-R BT.709
204 		 * 0x2 = xvYCC601
205 		 * 0x3 = xvYCC709
206 		 * 0x4 = sYCC601
207 		 * 0x5 = AdobeYCC601
208 		 * 0x6 = ITU-R BT.2020 Y'cC'bcC'rc
209 		 * 0x7 = ITU-R BT.2020 Y'C'bC'r
210 		 * (others reserved)
211 		 * ----------------------------------------------------------------------------------------------------
212 		 * 0x0 = ITU-R BT.601                   |  3 = YCbCr420
213 		 * 0x1 = ITU-R BT.709
214 		 * 0x2 = xvYCC601
215 		 * 0x3 = xvYCC709
216 		 * 0x4 = sYCC601
217 		 * 0x5 = AdobeYCC601
218 		 * 0x6 = ITU-R BT.2020 Y'cC'bcC'rc
219 		 * 0x7 = ITU-R BT.2020 Y'C'bC'r
220 		 * (others reserved)
221 		 * ----------------------------------------------------------------------------------------------------
222 		 * 0x0 =DICOM Part14 Grayscale          |  4 = Yonly
223 		 * Display Function
224 		 * (others reserved)
225 		 */
226 
227 		/* Set Pixel Encoding */
228 		switch (stream->timing.pixel_encoding) {
229 		case PIXEL_ENCODING_RGB:
230 			pixelEncoding = 0x0;  /* RGB = 0h */
231 			break;
232 		case PIXEL_ENCODING_YCBCR444:
233 			pixelEncoding = 0x1;  /* YCbCr444 = 1h */
234 			break;
235 		case PIXEL_ENCODING_YCBCR422:
236 			pixelEncoding = 0x2;  /* YCbCr422 = 2h */
237 			break;
238 		case PIXEL_ENCODING_YCBCR420:
239 			pixelEncoding = 0x3;  /* YCbCr420 = 3h */
240 			break;
241 		default:
242 			pixelEncoding = 0x0;  /* default RGB = 0h */
243 			break;
244 		}
245 
246 		/* Set Colorimetry format based on pixel encoding */
247 		switch (stream->timing.pixel_encoding) {
248 		case PIXEL_ENCODING_RGB:
249 			if ((stream->output_color_space == COLOR_SPACE_SRGB) ||
250 					(stream->output_color_space == COLOR_SPACE_SRGB_LIMITED))
251 				colorimetryFormat = ColorimetryRGB_DP_sRGB;
252 			else if (stream->output_color_space == COLOR_SPACE_ADOBERGB)
253 				colorimetryFormat = ColorimetryRGB_DP_AdobeRGB;
254 			else if ((stream->output_color_space == COLOR_SPACE_2020_RGB_FULLRANGE) ||
255 					(stream->output_color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE))
256 				colorimetryFormat = ColorimetryRGB_DP_ITU_R_BT2020RGB;
257 			break;
258 
259 		case PIXEL_ENCODING_YCBCR444:
260 		case PIXEL_ENCODING_YCBCR422:
261 		case PIXEL_ENCODING_YCBCR420:
262 			/* Note: xvYCC probably not supported correctly here on DP since colorspace translation
263 			 * loses distinction between BT601 vs xvYCC601 in translation
264 			 */
265 			if (stream->output_color_space == COLOR_SPACE_YCBCR601)
266 				colorimetryFormat = ColorimetryYCC_DP_ITU601;
267 			else if (stream->output_color_space == COLOR_SPACE_YCBCR709)
268 				colorimetryFormat = ColorimetryYCC_DP_ITU709;
269 			else if (stream->output_color_space == COLOR_SPACE_ADOBERGB)
270 				colorimetryFormat = ColorimetryYCC_DP_AdobeYCC;
271 			else if (stream->output_color_space == COLOR_SPACE_2020_YCBCR)
272 				colorimetryFormat = ColorimetryYCC_DP_ITU2020YCbCr;
273 			break;
274 
275 		default:
276 			colorimetryFormat = ColorimetryRGB_DP_sRGB;
277 			break;
278 		}
279 
280 		info_packet->sb[16] = (pixelEncoding << 4) | colorimetryFormat;
281 
282 		/* Set color depth */
283 		switch (stream->timing.display_color_depth) {
284 		case COLOR_DEPTH_666:
285 			/* NOTE: This is actually not valid for YCbCr pixel encoding to have 6 bpc
286 			 *       as of DP1.4 spec, but value of 0 probably reserved here for potential future use.
287 			 */
288 			info_packet->sb[17] = 0;
289 			break;
290 		case COLOR_DEPTH_888:
291 			info_packet->sb[17] = 1;
292 			break;
293 		case COLOR_DEPTH_101010:
294 			info_packet->sb[17] = 2;
295 			break;
296 		case COLOR_DEPTH_121212:
297 			info_packet->sb[17] = 3;
298 			break;
299 		/*case COLOR_DEPTH_141414: -- NO SUCH FORMAT IN DP SPEC */
300 		case COLOR_DEPTH_161616:
301 			info_packet->sb[17] = 4;
302 			break;
303 		default:
304 			info_packet->sb[17] = 0;
305 			break;
306 		}
307 
308 		/* all YCbCr are always limited range */
309 		if ((stream->output_color_space == COLOR_SPACE_SRGB_LIMITED) ||
310 				(stream->output_color_space == COLOR_SPACE_2020_RGB_LIMITEDRANGE) ||
311 				(pixelEncoding != 0x0)) {
312 			info_packet->sb[17] |= 0x80; /* DB17 bit 7 set to 1 for CEA timing. */
313 		}
314 
315 		/* Content Type (Bits 2:0)
316 		 *  0 = Not defined.
317 		 *  1 = Graphics.
318 		 *  2 = Photo.
319 		 *  3 = Video.
320 		 *  4 = Game.
321 		 */
322 		info_packet->sb[18] = 0;
323 	}
324 
325 }
326 
327