xref: /linux/drivers/gpu/drm/i915/display/intel_bios.c (revision eb9fcf63857556d5eacd67f5c96078e643a8d15a)
1df0566a6SJani Nikula /*
2df0566a6SJani Nikula  * Copyright © 2006 Intel Corporation
3df0566a6SJani Nikula  *
4df0566a6SJani Nikula  * Permission is hereby granted, free of charge, to any person obtaining a
5df0566a6SJani Nikula  * copy of this software and associated documentation files (the "Software"),
6df0566a6SJani Nikula  * to deal in the Software without restriction, including without limitation
7df0566a6SJani Nikula  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8df0566a6SJani Nikula  * and/or sell copies of the Software, and to permit persons to whom the
9df0566a6SJani Nikula  * Software is furnished to do so, subject to the following conditions:
10df0566a6SJani Nikula  *
11df0566a6SJani Nikula  * The above copyright notice and this permission notice (including the next
12df0566a6SJani Nikula  * paragraph) shall be included in all copies or substantial portions of the
13df0566a6SJani Nikula  * Software.
14df0566a6SJani Nikula  *
15df0566a6SJani Nikula  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16df0566a6SJani Nikula  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17df0566a6SJani Nikula  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18df0566a6SJani Nikula  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19df0566a6SJani Nikula  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20df0566a6SJani Nikula  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21df0566a6SJani Nikula  * SOFTWARE.
22df0566a6SJani Nikula  *
23df0566a6SJani Nikula  * Authors:
24df0566a6SJani Nikula  *    Eric Anholt <eric@anholt.net>
25df0566a6SJani Nikula  *
26df0566a6SJani Nikula  */
27df0566a6SJani Nikula 
28df0566a6SJani Nikula #include <drm/drm_dp_helper.h>
29df0566a6SJani Nikula 
30d8fe2ab6SMatt Roper #include "display/intel_display.h"
311bf2f3bfSJani Nikula #include "display/intel_display_types.h"
32df0566a6SJani Nikula #include "display/intel_gmbus.h"
33df0566a6SJani Nikula 
34df0566a6SJani Nikula #include "i915_drv.h"
35df0566a6SJani Nikula 
36df0566a6SJani Nikula #define _INTEL_BIOS_PRIVATE
37df0566a6SJani Nikula #include "intel_vbt_defs.h"
38df0566a6SJani Nikula 
39df0566a6SJani Nikula /**
40df0566a6SJani Nikula  * DOC: Video BIOS Table (VBT)
41df0566a6SJani Nikula  *
42df0566a6SJani Nikula  * The Video BIOS Table, or VBT, provides platform and board specific
43df0566a6SJani Nikula  * configuration information to the driver that is not discoverable or available
44df0566a6SJani Nikula  * through other means. The configuration is mostly related to display
45df0566a6SJani Nikula  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
46df0566a6SJani Nikula  * the PCI ROM.
47df0566a6SJani Nikula  *
48df0566a6SJani Nikula  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
49df0566a6SJani Nikula  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
50df0566a6SJani Nikula  * contain the actual configuration information. The VBT Header, and thus the
51df0566a6SJani Nikula  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
52df0566a6SJani Nikula  * BDB Header. The data blocks are concatenated after the BDB Header. The data
53df0566a6SJani Nikula  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
54df0566a6SJani Nikula  * data. (Block 53, the MIPI Sequence Block is an exception.)
55df0566a6SJani Nikula  *
56df0566a6SJani Nikula  * The driver parses the VBT during load. The relevant information is stored in
57df0566a6SJani Nikula  * driver private data for ease of use, and the actual VBT is not read after
58df0566a6SJani Nikula  * that.
59df0566a6SJani Nikula  */
60df0566a6SJani Nikula 
610d9ef19bSJani Nikula /* Wrapper for VBT child device config */
623162d057SJani Nikula struct intel_bios_encoder_data {
637371fa34SJani Nikula 	struct drm_i915_private *i915;
647371fa34SJani Nikula 
650d9ef19bSJani Nikula 	struct child_device_config child;
666e0d46e9SJani Nikula 	struct dsc_compression_parameters_entry *dsc;
670d9ef19bSJani Nikula 	struct list_head node;
680d9ef19bSJani Nikula };
690d9ef19bSJani Nikula 
70df0566a6SJani Nikula #define	SLAVE_ADDR1	0x70
71df0566a6SJani Nikula #define	SLAVE_ADDR2	0x72
72df0566a6SJani Nikula 
73df0566a6SJani Nikula /* Get BDB block size given a pointer to Block ID. */
74df0566a6SJani Nikula static u32 _get_blocksize(const u8 *block_base)
75df0566a6SJani Nikula {
76df0566a6SJani Nikula 	/* The MIPI Sequence Block v3+ has a separate size field. */
77df0566a6SJani Nikula 	if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
78df0566a6SJani Nikula 		return *((const u32 *)(block_base + 4));
79df0566a6SJani Nikula 	else
80df0566a6SJani Nikula 		return *((const u16 *)(block_base + 1));
81df0566a6SJani Nikula }
82df0566a6SJani Nikula 
83df0566a6SJani Nikula /* Get BDB block size give a pointer to data after Block ID and Block Size. */
84df0566a6SJani Nikula static u32 get_blocksize(const void *block_data)
85df0566a6SJani Nikula {
86df0566a6SJani Nikula 	return _get_blocksize(block_data - 3);
87df0566a6SJani Nikula }
88df0566a6SJani Nikula 
89df0566a6SJani Nikula static const void *
90df0566a6SJani Nikula find_section(const void *_bdb, enum bdb_block_id section_id)
91df0566a6SJani Nikula {
92df0566a6SJani Nikula 	const struct bdb_header *bdb = _bdb;
93df0566a6SJani Nikula 	const u8 *base = _bdb;
94df0566a6SJani Nikula 	int index = 0;
95df0566a6SJani Nikula 	u32 total, current_size;
96df0566a6SJani Nikula 	enum bdb_block_id current_id;
97df0566a6SJani Nikula 
98df0566a6SJani Nikula 	/* skip to first section */
99df0566a6SJani Nikula 	index += bdb->header_size;
100df0566a6SJani Nikula 	total = bdb->bdb_size;
101df0566a6SJani Nikula 
102df0566a6SJani Nikula 	/* walk the sections looking for section_id */
103df0566a6SJani Nikula 	while (index + 3 < total) {
104df0566a6SJani Nikula 		current_id = *(base + index);
105df0566a6SJani Nikula 		current_size = _get_blocksize(base + index);
106df0566a6SJani Nikula 		index += 3;
107df0566a6SJani Nikula 
108df0566a6SJani Nikula 		if (index + current_size > total)
109df0566a6SJani Nikula 			return NULL;
110df0566a6SJani Nikula 
111df0566a6SJani Nikula 		if (current_id == section_id)
112df0566a6SJani Nikula 			return base + index;
113df0566a6SJani Nikula 
114df0566a6SJani Nikula 		index += current_size;
115df0566a6SJani Nikula 	}
116df0566a6SJani Nikula 
117df0566a6SJani Nikula 	return NULL;
118df0566a6SJani Nikula }
119df0566a6SJani Nikula 
120df0566a6SJani Nikula static void
121df0566a6SJani Nikula fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
122df0566a6SJani Nikula 			const struct lvds_dvo_timing *dvo_timing)
123df0566a6SJani Nikula {
124df0566a6SJani Nikula 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
125df0566a6SJani Nikula 		dvo_timing->hactive_lo;
126df0566a6SJani Nikula 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
127df0566a6SJani Nikula 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
128df0566a6SJani Nikula 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
129df0566a6SJani Nikula 		((dvo_timing->hsync_pulse_width_hi << 8) |
130df0566a6SJani Nikula 			dvo_timing->hsync_pulse_width_lo);
131df0566a6SJani Nikula 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
132df0566a6SJani Nikula 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
133df0566a6SJani Nikula 
134df0566a6SJani Nikula 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
135df0566a6SJani Nikula 		dvo_timing->vactive_lo;
136df0566a6SJani Nikula 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
137df0566a6SJani Nikula 		((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
138df0566a6SJani Nikula 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
139df0566a6SJani Nikula 		((dvo_timing->vsync_pulse_width_hi << 4) |
140df0566a6SJani Nikula 			dvo_timing->vsync_pulse_width_lo);
141df0566a6SJani Nikula 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
142df0566a6SJani Nikula 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
143df0566a6SJani Nikula 	panel_fixed_mode->clock = dvo_timing->clock * 10;
144df0566a6SJani Nikula 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
145df0566a6SJani Nikula 
146df0566a6SJani Nikula 	if (dvo_timing->hsync_positive)
147df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
148df0566a6SJani Nikula 	else
149df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
150df0566a6SJani Nikula 
151df0566a6SJani Nikula 	if (dvo_timing->vsync_positive)
152df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
153df0566a6SJani Nikula 	else
154df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
155df0566a6SJani Nikula 
156df0566a6SJani Nikula 	panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
157df0566a6SJani Nikula 		dvo_timing->himage_lo;
158df0566a6SJani Nikula 	panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
159df0566a6SJani Nikula 		dvo_timing->vimage_lo;
160df0566a6SJani Nikula 
161df0566a6SJani Nikula 	/* Some VBTs have bogus h/vtotal values */
162df0566a6SJani Nikula 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
163df0566a6SJani Nikula 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
164df0566a6SJani Nikula 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
165df0566a6SJani Nikula 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
166df0566a6SJani Nikula 
167df0566a6SJani Nikula 	drm_mode_set_name(panel_fixed_mode);
168df0566a6SJani Nikula }
169df0566a6SJani Nikula 
170df0566a6SJani Nikula static const struct lvds_dvo_timing *
171df0566a6SJani Nikula get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *lvds_lfp_data,
172df0566a6SJani Nikula 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs,
173df0566a6SJani Nikula 		    int index)
174df0566a6SJani Nikula {
175df0566a6SJani Nikula 	/*
176df0566a6SJani Nikula 	 * the size of fp_timing varies on the different platform.
177df0566a6SJani Nikula 	 * So calculate the DVO timing relative offset in LVDS data
178df0566a6SJani Nikula 	 * entry to get the DVO timing entry
179df0566a6SJani Nikula 	 */
180df0566a6SJani Nikula 
181df0566a6SJani Nikula 	int lfp_data_size =
182df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[1].dvo_timing_offset -
183df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset;
184df0566a6SJani Nikula 	int dvo_timing_offset =
185df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset -
186df0566a6SJani Nikula 		lvds_lfp_data_ptrs->ptr[0].fp_timing_offset;
187df0566a6SJani Nikula 	char *entry = (char *)lvds_lfp_data->data + lfp_data_size * index;
188df0566a6SJani Nikula 
189df0566a6SJani Nikula 	return (struct lvds_dvo_timing *)(entry + dvo_timing_offset);
190df0566a6SJani Nikula }
191df0566a6SJani Nikula 
192df0566a6SJani Nikula /* get lvds_fp_timing entry
193df0566a6SJani Nikula  * this function may return NULL if the corresponding entry is invalid
194df0566a6SJani Nikula  */
195df0566a6SJani Nikula static const struct lvds_fp_timing *
196df0566a6SJani Nikula get_lvds_fp_timing(const struct bdb_header *bdb,
197df0566a6SJani Nikula 		   const struct bdb_lvds_lfp_data *data,
198df0566a6SJani Nikula 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
199df0566a6SJani Nikula 		   int index)
200df0566a6SJani Nikula {
201df0566a6SJani Nikula 	size_t data_ofs = (const u8 *)data - (const u8 *)bdb;
202df0566a6SJani Nikula 	u16 data_size = ((const u16 *)data)[-1]; /* stored in header */
203df0566a6SJani Nikula 	size_t ofs;
204df0566a6SJani Nikula 
205df0566a6SJani Nikula 	if (index >= ARRAY_SIZE(ptrs->ptr))
206df0566a6SJani Nikula 		return NULL;
207df0566a6SJani Nikula 	ofs = ptrs->ptr[index].fp_timing_offset;
208df0566a6SJani Nikula 	if (ofs < data_ofs ||
209df0566a6SJani Nikula 	    ofs + sizeof(struct lvds_fp_timing) > data_ofs + data_size)
210df0566a6SJani Nikula 		return NULL;
211df0566a6SJani Nikula 	return (const struct lvds_fp_timing *)((const u8 *)bdb + ofs);
212df0566a6SJani Nikula }
213df0566a6SJani Nikula 
2149e7ecedfSMatt Roper /* Parse general panel options */
215df0566a6SJani Nikula static void
216dbd440d8SJani Nikula parse_panel_options(struct drm_i915_private *i915,
217df0566a6SJani Nikula 		    const struct bdb_header *bdb)
218df0566a6SJani Nikula {
219df0566a6SJani Nikula 	const struct bdb_lvds_options *lvds_options;
220df0566a6SJani Nikula 	int panel_type;
221df0566a6SJani Nikula 	int drrs_mode;
222df0566a6SJani Nikula 	int ret;
223df0566a6SJani Nikula 
224df0566a6SJani Nikula 	lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
225df0566a6SJani Nikula 	if (!lvds_options)
226df0566a6SJani Nikula 		return;
227df0566a6SJani Nikula 
228dbd440d8SJani Nikula 	i915->vbt.lvds_dither = lvds_options->pixel_dither;
229df0566a6SJani Nikula 
230dbd440d8SJani Nikula 	ret = intel_opregion_get_panel_type(i915);
231df0566a6SJani Nikula 	if (ret >= 0) {
232dbd440d8SJani Nikula 		drm_WARN_ON(&i915->drm, ret > 0xf);
233df0566a6SJani Nikula 		panel_type = ret;
234dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Panel type: %d (OpRegion)\n",
235e92cbf38SWambui Karuga 			    panel_type);
236df0566a6SJani Nikula 	} else {
237df0566a6SJani Nikula 		if (lvds_options->panel_type > 0xf) {
238dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
239e92cbf38SWambui Karuga 				    "Invalid VBT panel type 0x%x\n",
240df0566a6SJani Nikula 				    lvds_options->panel_type);
241df0566a6SJani Nikula 			return;
242df0566a6SJani Nikula 		}
243df0566a6SJani Nikula 		panel_type = lvds_options->panel_type;
244dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Panel type: %d (VBT)\n",
245e92cbf38SWambui Karuga 			    panel_type);
246df0566a6SJani Nikula 	}
247df0566a6SJani Nikula 
248dbd440d8SJani Nikula 	i915->vbt.panel_type = panel_type;
249df0566a6SJani Nikula 
250df0566a6SJani Nikula 	drrs_mode = (lvds_options->dps_panel_type_bits
251df0566a6SJani Nikula 				>> (panel_type * 2)) & MODE_MASK;
252df0566a6SJani Nikula 	/*
253df0566a6SJani Nikula 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
254df0566a6SJani Nikula 	 * The below piece of code is required to adjust vbt.drrs_type
255df0566a6SJani Nikula 	 * to match the enum drrs_support_type.
256df0566a6SJani Nikula 	 */
257df0566a6SJani Nikula 	switch (drrs_mode) {
258df0566a6SJani Nikula 	case 0:
259dbd440d8SJani Nikula 		i915->vbt.drrs_type = STATIC_DRRS_SUPPORT;
260dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS supported mode is static\n");
261df0566a6SJani Nikula 		break;
262df0566a6SJani Nikula 	case 2:
263dbd440d8SJani Nikula 		i915->vbt.drrs_type = SEAMLESS_DRRS_SUPPORT;
264dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
265e92cbf38SWambui Karuga 			    "DRRS supported mode is seamless\n");
266df0566a6SJani Nikula 		break;
267df0566a6SJani Nikula 	default:
268dbd440d8SJani Nikula 		i915->vbt.drrs_type = DRRS_NOT_SUPPORTED;
269dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
270e92cbf38SWambui Karuga 			    "DRRS not supported (VBT input)\n");
271df0566a6SJani Nikula 		break;
272df0566a6SJani Nikula 	}
2739e7ecedfSMatt Roper }
2749e7ecedfSMatt Roper 
2759e7ecedfSMatt Roper /* Try to find integrated panel timing data */
2769e7ecedfSMatt Roper static void
277dbd440d8SJani Nikula parse_lfp_panel_dtd(struct drm_i915_private *i915,
2789e7ecedfSMatt Roper 		    const struct bdb_header *bdb)
2799e7ecedfSMatt Roper {
2809e7ecedfSMatt Roper 	const struct bdb_lvds_lfp_data *lvds_lfp_data;
2819e7ecedfSMatt Roper 	const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs;
2829e7ecedfSMatt Roper 	const struct lvds_dvo_timing *panel_dvo_timing;
2839e7ecedfSMatt Roper 	const struct lvds_fp_timing *fp_timing;
2849e7ecedfSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
285dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
286df0566a6SJani Nikula 
287df0566a6SJani Nikula 	lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
288df0566a6SJani Nikula 	if (!lvds_lfp_data)
289df0566a6SJani Nikula 		return;
290df0566a6SJani Nikula 
291df0566a6SJani Nikula 	lvds_lfp_data_ptrs = find_section(bdb, BDB_LVDS_LFP_DATA_PTRS);
292df0566a6SJani Nikula 	if (!lvds_lfp_data_ptrs)
293df0566a6SJani Nikula 		return;
294df0566a6SJani Nikula 
295df0566a6SJani Nikula 	panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
296df0566a6SJani Nikula 					       lvds_lfp_data_ptrs,
297df0566a6SJani Nikula 					       panel_type);
298df0566a6SJani Nikula 
299df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
300df0566a6SJani Nikula 	if (!panel_fixed_mode)
301df0566a6SJani Nikula 		return;
302df0566a6SJani Nikula 
303df0566a6SJani Nikula 	fill_detail_timing_data(panel_fixed_mode, panel_dvo_timing);
304df0566a6SJani Nikula 
305dbd440d8SJani Nikula 	i915->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
306df0566a6SJani Nikula 
307dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
308e92cbf38SWambui Karuga 		    "Found panel mode in BIOS VBT legacy lfp table:\n");
309df0566a6SJani Nikula 	drm_mode_debug_printmodeline(panel_fixed_mode);
310df0566a6SJani Nikula 
311df0566a6SJani Nikula 	fp_timing = get_lvds_fp_timing(bdb, lvds_lfp_data,
312df0566a6SJani Nikula 				       lvds_lfp_data_ptrs,
313df0566a6SJani Nikula 				       panel_type);
314df0566a6SJani Nikula 	if (fp_timing) {
315df0566a6SJani Nikula 		/* check the resolution, just to be sure */
316df0566a6SJani Nikula 		if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
317df0566a6SJani Nikula 		    fp_timing->y_res == panel_fixed_mode->vdisplay) {
318dbd440d8SJani Nikula 			i915->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
319dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
320e92cbf38SWambui Karuga 				    "VBT initial LVDS value %x\n",
321dbd440d8SJani Nikula 				    i915->vbt.bios_lvds_val);
322df0566a6SJani Nikula 		}
323df0566a6SJani Nikula 	}
324df0566a6SJani Nikula }
325df0566a6SJani Nikula 
326df0566a6SJani Nikula static void
327dbd440d8SJani Nikula parse_generic_dtd(struct drm_i915_private *i915,
32833ef6d4fSMatt Roper 		  const struct bdb_header *bdb)
32933ef6d4fSMatt Roper {
33033ef6d4fSMatt Roper 	const struct bdb_generic_dtd *generic_dtd;
33133ef6d4fSMatt Roper 	const struct generic_dtd_entry *dtd;
33233ef6d4fSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
33333ef6d4fSMatt Roper 	int num_dtd;
33433ef6d4fSMatt Roper 
33533ef6d4fSMatt Roper 	generic_dtd = find_section(bdb, BDB_GENERIC_DTD);
33633ef6d4fSMatt Roper 	if (!generic_dtd)
33733ef6d4fSMatt Roper 		return;
33833ef6d4fSMatt Roper 
33933ef6d4fSMatt Roper 	if (generic_dtd->gdtd_size < sizeof(struct generic_dtd_entry)) {
340dbd440d8SJani Nikula 		drm_err(&i915->drm, "GDTD size %u is too small.\n",
34133ef6d4fSMatt Roper 			generic_dtd->gdtd_size);
34233ef6d4fSMatt Roper 		return;
34333ef6d4fSMatt Roper 	} else if (generic_dtd->gdtd_size !=
34433ef6d4fSMatt Roper 		   sizeof(struct generic_dtd_entry)) {
345dbd440d8SJani Nikula 		drm_err(&i915->drm, "Unexpected GDTD size %u\n",
346e92cbf38SWambui Karuga 			generic_dtd->gdtd_size);
34733ef6d4fSMatt Roper 		/* DTD has unknown fields, but keep going */
34833ef6d4fSMatt Roper 	}
34933ef6d4fSMatt Roper 
35033ef6d4fSMatt Roper 	num_dtd = (get_blocksize(generic_dtd) -
35133ef6d4fSMatt Roper 		   sizeof(struct bdb_generic_dtd)) / generic_dtd->gdtd_size;
352dbd440d8SJani Nikula 	if (i915->vbt.panel_type >= num_dtd) {
353dbd440d8SJani Nikula 		drm_err(&i915->drm,
354e92cbf38SWambui Karuga 			"Panel type %d not found in table of %d DTD's\n",
355dbd440d8SJani Nikula 			i915->vbt.panel_type, num_dtd);
35633ef6d4fSMatt Roper 		return;
35733ef6d4fSMatt Roper 	}
35833ef6d4fSMatt Roper 
359dbd440d8SJani Nikula 	dtd = &generic_dtd->dtd[i915->vbt.panel_type];
36033ef6d4fSMatt Roper 
36133ef6d4fSMatt Roper 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
36233ef6d4fSMatt Roper 	if (!panel_fixed_mode)
36333ef6d4fSMatt Roper 		return;
36433ef6d4fSMatt Roper 
36533ef6d4fSMatt Roper 	panel_fixed_mode->hdisplay = dtd->hactive;
36633ef6d4fSMatt Roper 	panel_fixed_mode->hsync_start =
36733ef6d4fSMatt Roper 		panel_fixed_mode->hdisplay + dtd->hfront_porch;
36833ef6d4fSMatt Roper 	panel_fixed_mode->hsync_end =
36933ef6d4fSMatt Roper 		panel_fixed_mode->hsync_start + dtd->hsync;
370ad278f35SVandita Kulkarni 	panel_fixed_mode->htotal =
371ad278f35SVandita Kulkarni 		panel_fixed_mode->hdisplay + dtd->hblank;
37233ef6d4fSMatt Roper 
37333ef6d4fSMatt Roper 	panel_fixed_mode->vdisplay = dtd->vactive;
37433ef6d4fSMatt Roper 	panel_fixed_mode->vsync_start =
37533ef6d4fSMatt Roper 		panel_fixed_mode->vdisplay + dtd->vfront_porch;
37633ef6d4fSMatt Roper 	panel_fixed_mode->vsync_end =
37733ef6d4fSMatt Roper 		panel_fixed_mode->vsync_start + dtd->vsync;
378ad278f35SVandita Kulkarni 	panel_fixed_mode->vtotal =
379ad278f35SVandita Kulkarni 		panel_fixed_mode->vdisplay + dtd->vblank;
38033ef6d4fSMatt Roper 
38133ef6d4fSMatt Roper 	panel_fixed_mode->clock = dtd->pixel_clock;
38233ef6d4fSMatt Roper 	panel_fixed_mode->width_mm = dtd->width_mm;
38333ef6d4fSMatt Roper 	panel_fixed_mode->height_mm = dtd->height_mm;
38433ef6d4fSMatt Roper 
38533ef6d4fSMatt Roper 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
38633ef6d4fSMatt Roper 	drm_mode_set_name(panel_fixed_mode);
38733ef6d4fSMatt Roper 
38833ef6d4fSMatt Roper 	if (dtd->hsync_positive_polarity)
38933ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
39033ef6d4fSMatt Roper 	else
39133ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
39233ef6d4fSMatt Roper 
39333ef6d4fSMatt Roper 	if (dtd->vsync_positive_polarity)
39433ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
39533ef6d4fSMatt Roper 	else
39633ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
39733ef6d4fSMatt Roper 
398dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
399e92cbf38SWambui Karuga 		    "Found panel mode in BIOS VBT generic dtd table:\n");
40033ef6d4fSMatt Roper 	drm_mode_debug_printmodeline(panel_fixed_mode);
40133ef6d4fSMatt Roper 
402dbd440d8SJani Nikula 	i915->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
40333ef6d4fSMatt Roper }
40433ef6d4fSMatt Roper 
40533ef6d4fSMatt Roper static void
406dbd440d8SJani Nikula parse_panel_dtd(struct drm_i915_private *i915,
40733ef6d4fSMatt Roper 		const struct bdb_header *bdb)
40833ef6d4fSMatt Roper {
40933ef6d4fSMatt Roper 	/*
41033ef6d4fSMatt Roper 	 * Older VBTs provided provided DTD information for internal displays
41133ef6d4fSMatt Roper 	 * through the "LFP panel DTD" block (42).  As of VBT revision 229,
41233ef6d4fSMatt Roper 	 * that block is now deprecated and DTD information should be provided
41333ef6d4fSMatt Roper 	 * via a newer "generic DTD" block (58).  Just to be safe, we'll
41433ef6d4fSMatt Roper 	 * try the new generic DTD block first on VBT >= 229, but still fall
41533ef6d4fSMatt Roper 	 * back to trying the old LFP block if that fails.
41633ef6d4fSMatt Roper 	 */
41733ef6d4fSMatt Roper 	if (bdb->version >= 229)
418dbd440d8SJani Nikula 		parse_generic_dtd(i915, bdb);
419dbd440d8SJani Nikula 	if (!i915->vbt.lfp_lvds_vbt_mode)
420dbd440d8SJani Nikula 		parse_lfp_panel_dtd(i915, bdb);
42133ef6d4fSMatt Roper }
42233ef6d4fSMatt Roper 
42333ef6d4fSMatt Roper static void
424dbd440d8SJani Nikula parse_lfp_backlight(struct drm_i915_private *i915,
425df0566a6SJani Nikula 		    const struct bdb_header *bdb)
426df0566a6SJani Nikula {
427df0566a6SJani Nikula 	const struct bdb_lfp_backlight_data *backlight_data;
428df0566a6SJani Nikula 	const struct lfp_backlight_data_entry *entry;
429dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
430d381baadSJosé Roberto de Souza 	u16 level;
431df0566a6SJani Nikula 
432df0566a6SJani Nikula 	backlight_data = find_section(bdb, BDB_LVDS_BACKLIGHT);
433df0566a6SJani Nikula 	if (!backlight_data)
434df0566a6SJani Nikula 		return;
435df0566a6SJani Nikula 
436df0566a6SJani Nikula 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
437dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
438e92cbf38SWambui Karuga 			    "Unsupported backlight data entry size %u\n",
439df0566a6SJani Nikula 			    backlight_data->entry_size);
440df0566a6SJani Nikula 		return;
441df0566a6SJani Nikula 	}
442df0566a6SJani Nikula 
443df0566a6SJani Nikula 	entry = &backlight_data->data[panel_type];
444df0566a6SJani Nikula 
445dbd440d8SJani Nikula 	i915->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
446dbd440d8SJani Nikula 	if (!i915->vbt.backlight.present) {
447dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
448e92cbf38SWambui Karuga 			    "PWM backlight not present in VBT (type %u)\n",
449df0566a6SJani Nikula 			    entry->type);
450df0566a6SJani Nikula 		return;
451df0566a6SJani Nikula 	}
452df0566a6SJani Nikula 
453dbd440d8SJani Nikula 	i915->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
4544378daf5SLukasz Majczak 	if (bdb->version >= 191) {
4554378daf5SLukasz Majczak 		size_t exp_size;
4564378daf5SLukasz Majczak 
4574378daf5SLukasz Majczak 		if (bdb->version >= 236)
4584378daf5SLukasz Majczak 			exp_size = sizeof(struct bdb_lfp_backlight_data);
4594378daf5SLukasz Majczak 		else if (bdb->version >= 234)
4604378daf5SLukasz Majczak 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_234;
4614378daf5SLukasz Majczak 		else
4624378daf5SLukasz Majczak 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_191;
4634378daf5SLukasz Majczak 
4644378daf5SLukasz Majczak 		if (get_blocksize(backlight_data) >= exp_size) {
465df0566a6SJani Nikula 			const struct lfp_backlight_control_method *method;
466df0566a6SJani Nikula 
467df0566a6SJani Nikula 			method = &backlight_data->backlight_control[panel_type];
468dbd440d8SJani Nikula 			i915->vbt.backlight.type = method->type;
469dbd440d8SJani Nikula 			i915->vbt.backlight.controller = method->controller;
470df0566a6SJani Nikula 		}
4714378daf5SLukasz Majczak 	}
472df0566a6SJani Nikula 
473dbd440d8SJani Nikula 	i915->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
474dbd440d8SJani Nikula 	i915->vbt.backlight.active_low_pwm = entry->active_low_pwm;
475d381baadSJosé Roberto de Souza 
476d381baadSJosé Roberto de Souza 	if (bdb->version >= 234) {
477d381baadSJosé Roberto de Souza 		u16 min_level;
478d381baadSJosé Roberto de Souza 		bool scale;
479d381baadSJosé Roberto de Souza 
480d381baadSJosé Roberto de Souza 		level = backlight_data->brightness_level[panel_type].level;
481d381baadSJosé Roberto de Souza 		min_level = backlight_data->brightness_min_level[panel_type].level;
482d381baadSJosé Roberto de Souza 
483d381baadSJosé Roberto de Souza 		if (bdb->version >= 236)
484d381baadSJosé Roberto de Souza 			scale = backlight_data->brightness_precision_bits[panel_type] == 16;
485d381baadSJosé Roberto de Souza 		else
486d381baadSJosé Roberto de Souza 			scale = level > 255;
487d381baadSJosé Roberto de Souza 
488d381baadSJosé Roberto de Souza 		if (scale)
489d381baadSJosé Roberto de Souza 			min_level = min_level / 255;
490d381baadSJosé Roberto de Souza 
491d381baadSJosé Roberto de Souza 		if (min_level > 255) {
492dbd440d8SJani Nikula 			drm_warn(&i915->drm, "Brightness min level > 255\n");
493d381baadSJosé Roberto de Souza 			level = 255;
494d381baadSJosé Roberto de Souza 		}
495dbd440d8SJani Nikula 		i915->vbt.backlight.min_brightness = min_level;
49684d3d71fSLee Shawn C 
49784d3d71fSLee Shawn C 		i915->vbt.backlight.brightness_precision_bits =
49884d3d71fSLee Shawn C 			backlight_data->brightness_precision_bits[panel_type];
499d381baadSJosé Roberto de Souza 	} else {
500d381baadSJosé Roberto de Souza 		level = backlight_data->level[panel_type];
501dbd440d8SJani Nikula 		i915->vbt.backlight.min_brightness = entry->min_brightness;
502d381baadSJosé Roberto de Souza 	}
503d381baadSJosé Roberto de Souza 
504dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
505e92cbf38SWambui Karuga 		    "VBT backlight PWM modulation frequency %u Hz, "
506df0566a6SJani Nikula 		    "active %s, min brightness %u, level %u, controller %u\n",
507dbd440d8SJani Nikula 		    i915->vbt.backlight.pwm_freq_hz,
508dbd440d8SJani Nikula 		    i915->vbt.backlight.active_low_pwm ? "low" : "high",
509dbd440d8SJani Nikula 		    i915->vbt.backlight.min_brightness,
510d381baadSJosé Roberto de Souza 		    level,
511dbd440d8SJani Nikula 		    i915->vbt.backlight.controller);
512df0566a6SJani Nikula }
513df0566a6SJani Nikula 
514df0566a6SJani Nikula /* Try to find sdvo panel data */
515df0566a6SJani Nikula static void
516dbd440d8SJani Nikula parse_sdvo_panel_data(struct drm_i915_private *i915,
517df0566a6SJani Nikula 		      const struct bdb_header *bdb)
518df0566a6SJani Nikula {
519df0566a6SJani Nikula 	const struct bdb_sdvo_panel_dtds *dtds;
520df0566a6SJani Nikula 	struct drm_display_mode *panel_fixed_mode;
521df0566a6SJani Nikula 	int index;
522df0566a6SJani Nikula 
523dbd440d8SJani Nikula 	index = i915->params.vbt_sdvo_panel_type;
524df0566a6SJani Nikula 	if (index == -2) {
525dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
526e92cbf38SWambui Karuga 			    "Ignore SDVO panel mode from BIOS VBT tables.\n");
527df0566a6SJani Nikula 		return;
528df0566a6SJani Nikula 	}
529df0566a6SJani Nikula 
530df0566a6SJani Nikula 	if (index == -1) {
531df0566a6SJani Nikula 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
532df0566a6SJani Nikula 
533df0566a6SJani Nikula 		sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
534df0566a6SJani Nikula 		if (!sdvo_lvds_options)
535df0566a6SJani Nikula 			return;
536df0566a6SJani Nikula 
537df0566a6SJani Nikula 		index = sdvo_lvds_options->panel_type;
538df0566a6SJani Nikula 	}
539df0566a6SJani Nikula 
540df0566a6SJani Nikula 	dtds = find_section(bdb, BDB_SDVO_PANEL_DTDS);
541df0566a6SJani Nikula 	if (!dtds)
542df0566a6SJani Nikula 		return;
543df0566a6SJani Nikula 
544df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
545df0566a6SJani Nikula 	if (!panel_fixed_mode)
546df0566a6SJani Nikula 		return;
547df0566a6SJani Nikula 
548df0566a6SJani Nikula 	fill_detail_timing_data(panel_fixed_mode, &dtds->dtds[index]);
549df0566a6SJani Nikula 
550dbd440d8SJani Nikula 	i915->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
551df0566a6SJani Nikula 
552dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
553e92cbf38SWambui Karuga 		    "Found SDVO panel mode in BIOS VBT tables:\n");
554df0566a6SJani Nikula 	drm_mode_debug_printmodeline(panel_fixed_mode);
555df0566a6SJani Nikula }
556df0566a6SJani Nikula 
557dbd440d8SJani Nikula static int intel_bios_ssc_frequency(struct drm_i915_private *i915,
558df0566a6SJani Nikula 				    bool alternate)
559df0566a6SJani Nikula {
560005e9537SMatt Roper 	switch (DISPLAY_VER(i915)) {
561df0566a6SJani Nikula 	case 2:
562df0566a6SJani Nikula 		return alternate ? 66667 : 48000;
563df0566a6SJani Nikula 	case 3:
564df0566a6SJani Nikula 	case 4:
565df0566a6SJani Nikula 		return alternate ? 100000 : 96000;
566df0566a6SJani Nikula 	default:
567df0566a6SJani Nikula 		return alternate ? 100000 : 120000;
568df0566a6SJani Nikula 	}
569df0566a6SJani Nikula }
570df0566a6SJani Nikula 
571df0566a6SJani Nikula static void
572dbd440d8SJani Nikula parse_general_features(struct drm_i915_private *i915,
573df0566a6SJani Nikula 		       const struct bdb_header *bdb)
574df0566a6SJani Nikula {
575df0566a6SJani Nikula 	const struct bdb_general_features *general;
576df0566a6SJani Nikula 
577df0566a6SJani Nikula 	general = find_section(bdb, BDB_GENERAL_FEATURES);
578df0566a6SJani Nikula 	if (!general)
579df0566a6SJani Nikula 		return;
580df0566a6SJani Nikula 
581dbd440d8SJani Nikula 	i915->vbt.int_tv_support = general->int_tv_support;
582df0566a6SJani Nikula 	/* int_crt_support can't be trusted on earlier platforms */
583df0566a6SJani Nikula 	if (bdb->version >= 155 &&
584dbd440d8SJani Nikula 	    (HAS_DDI(i915) || IS_VALLEYVIEW(i915)))
585dbd440d8SJani Nikula 		i915->vbt.int_crt_support = general->int_crt_support;
586dbd440d8SJani Nikula 	i915->vbt.lvds_use_ssc = general->enable_ssc;
587dbd440d8SJani Nikula 	i915->vbt.lvds_ssc_freq =
588dbd440d8SJani Nikula 		intel_bios_ssc_frequency(i915, general->ssc_freq);
589dbd440d8SJani Nikula 	i915->vbt.display_clock_mode = general->display_clock_mode;
590dbd440d8SJani Nikula 	i915->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
591df0566a6SJani Nikula 	if (bdb->version >= 181) {
592dbd440d8SJani Nikula 		i915->vbt.orientation = general->rotate_180 ?
593df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
594df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_NORMAL;
595df0566a6SJani Nikula 	} else {
596dbd440d8SJani Nikula 		i915->vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
597df0566a6SJani Nikula 	}
598dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
599e92cbf38SWambui Karuga 		    "BDB_GENERAL_FEATURES int_tv_support %d int_crt_support %d lvds_use_ssc %d lvds_ssc_freq %d display_clock_mode %d fdi_rx_polarity_inverted %d\n",
600dbd440d8SJani Nikula 		    i915->vbt.int_tv_support,
601dbd440d8SJani Nikula 		    i915->vbt.int_crt_support,
602dbd440d8SJani Nikula 		    i915->vbt.lvds_use_ssc,
603dbd440d8SJani Nikula 		    i915->vbt.lvds_ssc_freq,
604dbd440d8SJani Nikula 		    i915->vbt.display_clock_mode,
605dbd440d8SJani Nikula 		    i915->vbt.fdi_rx_polarity_inverted);
606df0566a6SJani Nikula }
607df0566a6SJani Nikula 
608df0566a6SJani Nikula static const struct child_device_config *
609df0566a6SJani Nikula child_device_ptr(const struct bdb_general_definitions *defs, int i)
610df0566a6SJani Nikula {
611df0566a6SJani Nikula 	return (const void *) &defs->devices[i * defs->child_dev_size];
612df0566a6SJani Nikula }
613df0566a6SJani Nikula 
614df0566a6SJani Nikula static void
615ef0096e4SJani Nikula parse_sdvo_device_mapping(struct drm_i915_private *i915)
616df0566a6SJani Nikula {
617df0566a6SJani Nikula 	struct sdvo_device_mapping *mapping;
6183162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
619df0566a6SJani Nikula 	const struct child_device_config *child;
6200d9ef19bSJani Nikula 	int count = 0;
621df0566a6SJani Nikula 
622df0566a6SJani Nikula 	/*
623df0566a6SJani Nikula 	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
624df0566a6SJani Nikula 	 * accurate and doesn't have to be, as long as it's not too strict.
625df0566a6SJani Nikula 	 */
62693e7e61eSLucas De Marchi 	if (!IS_DISPLAY_VER(i915, 3, 7)) {
627dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Skipping SDVO device mapping\n");
628df0566a6SJani Nikula 		return;
629df0566a6SJani Nikula 	}
630df0566a6SJani Nikula 
631dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
6320d9ef19bSJani Nikula 		child = &devdata->child;
633df0566a6SJani Nikula 
634df0566a6SJani Nikula 		if (child->slave_addr != SLAVE_ADDR1 &&
635df0566a6SJani Nikula 		    child->slave_addr != SLAVE_ADDR2) {
636df0566a6SJani Nikula 			/*
637df0566a6SJani Nikula 			 * If the slave address is neither 0x70 nor 0x72,
638df0566a6SJani Nikula 			 * it is not a SDVO device. Skip it.
639df0566a6SJani Nikula 			 */
640df0566a6SJani Nikula 			continue;
641df0566a6SJani Nikula 		}
642df0566a6SJani Nikula 		if (child->dvo_port != DEVICE_PORT_DVOB &&
643df0566a6SJani Nikula 		    child->dvo_port != DEVICE_PORT_DVOC) {
644df0566a6SJani Nikula 			/* skip the incorrect SDVO port */
645dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
646e92cbf38SWambui Karuga 				    "Incorrect SDVO port. Skip it\n");
647df0566a6SJani Nikula 			continue;
648df0566a6SJani Nikula 		}
649dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
650e92cbf38SWambui Karuga 			    "the SDVO device with slave addr %2x is found on"
651df0566a6SJani Nikula 			    " %s port\n",
652df0566a6SJani Nikula 			    child->slave_addr,
653df0566a6SJani Nikula 			    (child->dvo_port == DEVICE_PORT_DVOB) ?
654df0566a6SJani Nikula 			    "SDVOB" : "SDVOC");
655dbd440d8SJani Nikula 		mapping = &i915->vbt.sdvo_mappings[child->dvo_port - 1];
656df0566a6SJani Nikula 		if (!mapping->initialized) {
657df0566a6SJani Nikula 			mapping->dvo_port = child->dvo_port;
658df0566a6SJani Nikula 			mapping->slave_addr = child->slave_addr;
659df0566a6SJani Nikula 			mapping->dvo_wiring = child->dvo_wiring;
660df0566a6SJani Nikula 			mapping->ddc_pin = child->ddc_pin;
661df0566a6SJani Nikula 			mapping->i2c_pin = child->i2c_pin;
662df0566a6SJani Nikula 			mapping->initialized = 1;
663dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
664e92cbf38SWambui Karuga 				    "SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
665e92cbf38SWambui Karuga 				    mapping->dvo_port, mapping->slave_addr,
666e92cbf38SWambui Karuga 				    mapping->dvo_wiring, mapping->ddc_pin,
667df0566a6SJani Nikula 				    mapping->i2c_pin);
668df0566a6SJani Nikula 		} else {
669dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
670e92cbf38SWambui Karuga 				    "Maybe one SDVO port is shared by "
671df0566a6SJani Nikula 				    "two SDVO device.\n");
672df0566a6SJani Nikula 		}
673df0566a6SJani Nikula 		if (child->slave2_addr) {
674df0566a6SJani Nikula 			/* Maybe this is a SDVO device with multiple inputs */
675df0566a6SJani Nikula 			/* And the mapping info is not added */
676dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
677e92cbf38SWambui Karuga 				    "there exists the slave2_addr. Maybe this"
678df0566a6SJani Nikula 				    " is a SDVO device with multiple inputs.\n");
679df0566a6SJani Nikula 		}
680df0566a6SJani Nikula 		count++;
681df0566a6SJani Nikula 	}
682df0566a6SJani Nikula 
683df0566a6SJani Nikula 	if (!count) {
684df0566a6SJani Nikula 		/* No SDVO device info is found */
685dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
686e92cbf38SWambui Karuga 			    "No SDVO device info is found in VBT\n");
687df0566a6SJani Nikula 	}
688df0566a6SJani Nikula }
689df0566a6SJani Nikula 
690df0566a6SJani Nikula static void
691dbd440d8SJani Nikula parse_driver_features(struct drm_i915_private *i915,
692df0566a6SJani Nikula 		      const struct bdb_header *bdb)
693df0566a6SJani Nikula {
694df0566a6SJani Nikula 	const struct bdb_driver_features *driver;
695df0566a6SJani Nikula 
696df0566a6SJani Nikula 	driver = find_section(bdb, BDB_DRIVER_FEATURES);
697df0566a6SJani Nikula 	if (!driver)
698df0566a6SJani Nikula 		return;
699df0566a6SJani Nikula 
700005e9537SMatt Roper 	if (DISPLAY_VER(i915) >= 5) {
701df0566a6SJani Nikula 		/*
702df0566a6SJani Nikula 		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
703df0566a6SJani Nikula 		 * to mean "eDP". The VBT spec doesn't agree with that
704df0566a6SJani Nikula 		 * interpretation, but real world VBTs seem to.
705df0566a6SJani Nikula 		 */
706df0566a6SJani Nikula 		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
707dbd440d8SJani Nikula 			i915->vbt.int_lvds_support = 0;
708df0566a6SJani Nikula 	} else {
709df0566a6SJani Nikula 		/*
710df0566a6SJani Nikula 		 * FIXME it's not clear which BDB version has the LVDS config
711df0566a6SJani Nikula 		 * bits defined. Revision history in the VBT spec says:
712df0566a6SJani Nikula 		 * "0.92 | Add two definitions for VBT value of LVDS Active
713df0566a6SJani Nikula 		 *  Config (00b and 11b values defined) | 06/13/2005"
714df0566a6SJani Nikula 		 * but does not the specify the BDB version.
715df0566a6SJani Nikula 		 *
716df0566a6SJani Nikula 		 * So far version 134 (on i945gm) is the oldest VBT observed
717df0566a6SJani Nikula 		 * in the wild with the bits correctly populated. Version
718df0566a6SJani Nikula 		 * 108 (on i85x) does not have the bits correctly populated.
719df0566a6SJani Nikula 		 */
720df0566a6SJani Nikula 		if (bdb->version >= 134 &&
721df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
722df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
723dbd440d8SJani Nikula 			i915->vbt.int_lvds_support = 0;
724df0566a6SJani Nikula 	}
725df0566a6SJani Nikula 
726551fb93dSJosé Roberto de Souza 	if (bdb->version < 228) {
727dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS State Enabled:%d\n",
728e92cbf38SWambui Karuga 			    driver->drrs_enabled);
729df0566a6SJani Nikula 		/*
730df0566a6SJani Nikula 		 * If DRRS is not supported, drrs_type has to be set to 0.
731df0566a6SJani Nikula 		 * This is because, VBT is configured in such a way that
732df0566a6SJani Nikula 		 * static DRRS is 0 and DRRS not supported is represented by
733df0566a6SJani Nikula 		 * driver->drrs_enabled=false
734df0566a6SJani Nikula 		 */
735df0566a6SJani Nikula 		if (!driver->drrs_enabled)
736dbd440d8SJani Nikula 			i915->vbt.drrs_type = DRRS_NOT_SUPPORTED;
737551fb93dSJosé Roberto de Souza 
738dbd440d8SJani Nikula 		i915->vbt.psr.enable = driver->psr_enabled;
739df0566a6SJani Nikula 	}
740551fb93dSJosé Roberto de Souza }
741551fb93dSJosé Roberto de Souza 
742551fb93dSJosé Roberto de Souza static void
743dbd440d8SJani Nikula parse_power_conservation_features(struct drm_i915_private *i915,
744551fb93dSJosé Roberto de Souza 				  const struct bdb_header *bdb)
745551fb93dSJosé Roberto de Souza {
746551fb93dSJosé Roberto de Souza 	const struct bdb_lfp_power *power;
747dbd440d8SJani Nikula 	u8 panel_type = i915->vbt.panel_type;
748551fb93dSJosé Roberto de Souza 
749551fb93dSJosé Roberto de Souza 	if (bdb->version < 228)
750551fb93dSJosé Roberto de Souza 		return;
751551fb93dSJosé Roberto de Souza 
7524ec5abe9SJosé Roberto de Souza 	power = find_section(bdb, BDB_LFP_POWER);
753551fb93dSJosé Roberto de Souza 	if (!power)
754551fb93dSJosé Roberto de Souza 		return;
755551fb93dSJosé Roberto de Souza 
756dbd440d8SJani Nikula 	i915->vbt.psr.enable = power->psr & BIT(panel_type);
757551fb93dSJosé Roberto de Souza 
758551fb93dSJosé Roberto de Souza 	/*
759551fb93dSJosé Roberto de Souza 	 * If DRRS is not supported, drrs_type has to be set to 0.
760551fb93dSJosé Roberto de Souza 	 * This is because, VBT is configured in such a way that
761551fb93dSJosé Roberto de Souza 	 * static DRRS is 0 and DRRS not supported is represented by
762551fb93dSJosé Roberto de Souza 	 * power->drrs & BIT(panel_type)=false
763551fb93dSJosé Roberto de Souza 	 */
764551fb93dSJosé Roberto de Souza 	if (!(power->drrs & BIT(panel_type)))
765dbd440d8SJani Nikula 		i915->vbt.drrs_type = DRRS_NOT_SUPPORTED;
766f615cb6aSJosé Roberto de Souza 
767f615cb6aSJosé Roberto de Souza 	if (bdb->version >= 232)
768dbd440d8SJani Nikula 		i915->vbt.edp.hobl = power->hobl & BIT(panel_type);
769551fb93dSJosé Roberto de Souza }
770df0566a6SJani Nikula 
771df0566a6SJani Nikula static void
772dbd440d8SJani Nikula parse_edp(struct drm_i915_private *i915, const struct bdb_header *bdb)
773df0566a6SJani Nikula {
774df0566a6SJani Nikula 	const struct bdb_edp *edp;
775df0566a6SJani Nikula 	const struct edp_power_seq *edp_pps;
776df0566a6SJani Nikula 	const struct edp_fast_link_params *edp_link_params;
777dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
778df0566a6SJani Nikula 
779df0566a6SJani Nikula 	edp = find_section(bdb, BDB_EDP);
780df0566a6SJani Nikula 	if (!edp)
781df0566a6SJani Nikula 		return;
782df0566a6SJani Nikula 
783df0566a6SJani Nikula 	switch ((edp->color_depth >> (panel_type * 2)) & 3) {
784df0566a6SJani Nikula 	case EDP_18BPP:
785dbd440d8SJani Nikula 		i915->vbt.edp.bpp = 18;
786df0566a6SJani Nikula 		break;
787df0566a6SJani Nikula 	case EDP_24BPP:
788dbd440d8SJani Nikula 		i915->vbt.edp.bpp = 24;
789df0566a6SJani Nikula 		break;
790df0566a6SJani Nikula 	case EDP_30BPP:
791dbd440d8SJani Nikula 		i915->vbt.edp.bpp = 30;
792df0566a6SJani Nikula 		break;
793df0566a6SJani Nikula 	}
794df0566a6SJani Nikula 
795df0566a6SJani Nikula 	/* Get the eDP sequencing and link info */
796df0566a6SJani Nikula 	edp_pps = &edp->power_seqs[panel_type];
797df0566a6SJani Nikula 	edp_link_params = &edp->fast_link_params[panel_type];
798df0566a6SJani Nikula 
799dbd440d8SJani Nikula 	i915->vbt.edp.pps = *edp_pps;
800df0566a6SJani Nikula 
801df0566a6SJani Nikula 	switch (edp_link_params->rate) {
802df0566a6SJani Nikula 	case EDP_RATE_1_62:
803dbd440d8SJani Nikula 		i915->vbt.edp.rate = DP_LINK_BW_1_62;
804df0566a6SJani Nikula 		break;
805df0566a6SJani Nikula 	case EDP_RATE_2_7:
806dbd440d8SJani Nikula 		i915->vbt.edp.rate = DP_LINK_BW_2_7;
807df0566a6SJani Nikula 		break;
808df0566a6SJani Nikula 	default:
809dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
810e92cbf38SWambui Karuga 			    "VBT has unknown eDP link rate value %u\n",
811df0566a6SJani Nikula 			     edp_link_params->rate);
812df0566a6SJani Nikula 		break;
813df0566a6SJani Nikula 	}
814df0566a6SJani Nikula 
815df0566a6SJani Nikula 	switch (edp_link_params->lanes) {
816df0566a6SJani Nikula 	case EDP_LANE_1:
817dbd440d8SJani Nikula 		i915->vbt.edp.lanes = 1;
818df0566a6SJani Nikula 		break;
819df0566a6SJani Nikula 	case EDP_LANE_2:
820dbd440d8SJani Nikula 		i915->vbt.edp.lanes = 2;
821df0566a6SJani Nikula 		break;
822df0566a6SJani Nikula 	case EDP_LANE_4:
823dbd440d8SJani Nikula 		i915->vbt.edp.lanes = 4;
824df0566a6SJani Nikula 		break;
825df0566a6SJani Nikula 	default:
826dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
827e92cbf38SWambui Karuga 			    "VBT has unknown eDP lane count value %u\n",
828df0566a6SJani Nikula 			    edp_link_params->lanes);
829df0566a6SJani Nikula 		break;
830df0566a6SJani Nikula 	}
831df0566a6SJani Nikula 
832df0566a6SJani Nikula 	switch (edp_link_params->preemphasis) {
833df0566a6SJani Nikula 	case EDP_PREEMPHASIS_NONE:
834dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
835df0566a6SJani Nikula 		break;
836df0566a6SJani Nikula 	case EDP_PREEMPHASIS_3_5dB:
837dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
838df0566a6SJani Nikula 		break;
839df0566a6SJani Nikula 	case EDP_PREEMPHASIS_6dB:
840dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
841df0566a6SJani Nikula 		break;
842df0566a6SJani Nikula 	case EDP_PREEMPHASIS_9_5dB:
843dbd440d8SJani Nikula 		i915->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
844df0566a6SJani Nikula 		break;
845df0566a6SJani Nikula 	default:
846dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
847e92cbf38SWambui Karuga 			    "VBT has unknown eDP pre-emphasis value %u\n",
848df0566a6SJani Nikula 			    edp_link_params->preemphasis);
849df0566a6SJani Nikula 		break;
850df0566a6SJani Nikula 	}
851df0566a6SJani Nikula 
852df0566a6SJani Nikula 	switch (edp_link_params->vswing) {
853df0566a6SJani Nikula 	case EDP_VSWING_0_4V:
854dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
855df0566a6SJani Nikula 		break;
856df0566a6SJani Nikula 	case EDP_VSWING_0_6V:
857dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
858df0566a6SJani Nikula 		break;
859df0566a6SJani Nikula 	case EDP_VSWING_0_8V:
860dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
861df0566a6SJani Nikula 		break;
862df0566a6SJani Nikula 	case EDP_VSWING_1_2V:
863dbd440d8SJani Nikula 		i915->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
864df0566a6SJani Nikula 		break;
865df0566a6SJani Nikula 	default:
866dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
867e92cbf38SWambui Karuga 			    "VBT has unknown eDP voltage swing value %u\n",
868df0566a6SJani Nikula 			    edp_link_params->vswing);
869df0566a6SJani Nikula 		break;
870df0566a6SJani Nikula 	}
871df0566a6SJani Nikula 
872df0566a6SJani Nikula 	if (bdb->version >= 173) {
873df0566a6SJani Nikula 		u8 vswing;
874df0566a6SJani Nikula 
875df0566a6SJani Nikula 		/* Don't read from VBT if module parameter has valid value*/
876dbd440d8SJani Nikula 		if (i915->params.edp_vswing) {
877dbd440d8SJani Nikula 			i915->vbt.edp.low_vswing =
878dbd440d8SJani Nikula 				i915->params.edp_vswing == 1;
879df0566a6SJani Nikula 		} else {
880df0566a6SJani Nikula 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
881dbd440d8SJani Nikula 			i915->vbt.edp.low_vswing = vswing == 0;
882df0566a6SJani Nikula 		}
883df0566a6SJani Nikula 	}
884df0566a6SJani Nikula }
885df0566a6SJani Nikula 
886df0566a6SJani Nikula static void
887dbd440d8SJani Nikula parse_psr(struct drm_i915_private *i915, const struct bdb_header *bdb)
888df0566a6SJani Nikula {
889df0566a6SJani Nikula 	const struct bdb_psr *psr;
890df0566a6SJani Nikula 	const struct psr_table *psr_table;
891dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
892df0566a6SJani Nikula 
893df0566a6SJani Nikula 	psr = find_section(bdb, BDB_PSR);
894df0566a6SJani Nikula 	if (!psr) {
895dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No PSR BDB found.\n");
896df0566a6SJani Nikula 		return;
897df0566a6SJani Nikula 	}
898df0566a6SJani Nikula 
899df0566a6SJani Nikula 	psr_table = &psr->psr_table[panel_type];
900df0566a6SJani Nikula 
901dbd440d8SJani Nikula 	i915->vbt.psr.full_link = psr_table->full_link;
902dbd440d8SJani Nikula 	i915->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
903df0566a6SJani Nikula 
904df0566a6SJani Nikula 	/* Allowed VBT values goes from 0 to 15 */
905dbd440d8SJani Nikula 	i915->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
906df0566a6SJani Nikula 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
907df0566a6SJani Nikula 
908df0566a6SJani Nikula 	/*
909df0566a6SJani Nikula 	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
910df0566a6SJani Nikula 	 * Old decimal value is wake up time in multiples of 100 us.
911df0566a6SJani Nikula 	 */
912df0566a6SJani Nikula 	if (bdb->version >= 205 &&
9132446e1d6SMatt Roper 	    (DISPLAY_VER(i915) >= 9 && !IS_BROXTON(i915))) {
914df0566a6SJani Nikula 		switch (psr_table->tp1_wakeup_time) {
915df0566a6SJani Nikula 		case 0:
916dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 500;
917df0566a6SJani Nikula 			break;
918df0566a6SJani Nikula 		case 1:
919dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 100;
920df0566a6SJani Nikula 			break;
921df0566a6SJani Nikula 		case 3:
922dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 0;
923df0566a6SJani Nikula 			break;
924df0566a6SJani Nikula 		default:
925dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
926e92cbf38SWambui Karuga 				    "VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
927df0566a6SJani Nikula 				    psr_table->tp1_wakeup_time);
928df561f66SGustavo A. R. Silva 			fallthrough;
929df0566a6SJani Nikula 		case 2:
930dbd440d8SJani Nikula 			i915->vbt.psr.tp1_wakeup_time_us = 2500;
931df0566a6SJani Nikula 			break;
932df0566a6SJani Nikula 		}
933df0566a6SJani Nikula 
934df0566a6SJani Nikula 		switch (psr_table->tp2_tp3_wakeup_time) {
935df0566a6SJani Nikula 		case 0:
936dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 500;
937df0566a6SJani Nikula 			break;
938df0566a6SJani Nikula 		case 1:
939dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 100;
940df0566a6SJani Nikula 			break;
941df0566a6SJani Nikula 		case 3:
942dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 0;
943df0566a6SJani Nikula 			break;
944df0566a6SJani Nikula 		default:
945dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
946e92cbf38SWambui Karuga 				    "VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
947df0566a6SJani Nikula 				    psr_table->tp2_tp3_wakeup_time);
948df561f66SGustavo A. R. Silva 			fallthrough;
949df0566a6SJani Nikula 		case 2:
950dbd440d8SJani Nikula 			i915->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
951df0566a6SJani Nikula 		break;
952df0566a6SJani Nikula 		}
953df0566a6SJani Nikula 	} else {
954dbd440d8SJani Nikula 		i915->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
955dbd440d8SJani Nikula 		i915->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
956df0566a6SJani Nikula 	}
957df0566a6SJani Nikula 
958df0566a6SJani Nikula 	if (bdb->version >= 226) {
959b5ea9c93SDhinakaran Pandiyan 		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
960df0566a6SJani Nikula 
961df0566a6SJani Nikula 		wakeup_time = (wakeup_time >> (2 * panel_type)) & 0x3;
962df0566a6SJani Nikula 		switch (wakeup_time) {
963df0566a6SJani Nikula 		case 0:
964df0566a6SJani Nikula 			wakeup_time = 500;
965df0566a6SJani Nikula 			break;
966df0566a6SJani Nikula 		case 1:
967df0566a6SJani Nikula 			wakeup_time = 100;
968df0566a6SJani Nikula 			break;
969df0566a6SJani Nikula 		case 3:
970df0566a6SJani Nikula 			wakeup_time = 50;
971df0566a6SJani Nikula 			break;
972df0566a6SJani Nikula 		default:
973df0566a6SJani Nikula 		case 2:
974df0566a6SJani Nikula 			wakeup_time = 2500;
975df0566a6SJani Nikula 			break;
976df0566a6SJani Nikula 		}
977dbd440d8SJani Nikula 		i915->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
978df0566a6SJani Nikula 	} else {
979df0566a6SJani Nikula 		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
980dbd440d8SJani Nikula 		i915->vbt.psr.psr2_tp2_tp3_wakeup_time_us = i915->vbt.psr.tp2_tp3_wakeup_time_us;
981df0566a6SJani Nikula 	}
982df0566a6SJani Nikula }
983df0566a6SJani Nikula 
984dbd440d8SJani Nikula static void parse_dsi_backlight_ports(struct drm_i915_private *i915,
985df0566a6SJani Nikula 				      u16 version, enum port port)
986df0566a6SJani Nikula {
987dbd440d8SJani Nikula 	if (!i915->vbt.dsi.config->dual_link || version < 197) {
988dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(port);
989dbd440d8SJani Nikula 		if (i915->vbt.dsi.config->cabc_supported)
990dbd440d8SJani Nikula 			i915->vbt.dsi.cabc_ports = BIT(port);
991df0566a6SJani Nikula 
992df0566a6SJani Nikula 		return;
993df0566a6SJani Nikula 	}
994df0566a6SJani Nikula 
995dbd440d8SJani Nikula 	switch (i915->vbt.dsi.config->dl_dcs_backlight_ports) {
996df0566a6SJani Nikula 	case DL_DCS_PORT_A:
997dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(PORT_A);
998df0566a6SJani Nikula 		break;
999df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1000dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(PORT_C);
1001df0566a6SJani Nikula 		break;
1002df0566a6SJani Nikula 	default:
1003df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
1004dbd440d8SJani Nikula 		i915->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(PORT_C);
1005df0566a6SJani Nikula 		break;
1006df0566a6SJani Nikula 	}
1007df0566a6SJani Nikula 
1008dbd440d8SJani Nikula 	if (!i915->vbt.dsi.config->cabc_supported)
1009df0566a6SJani Nikula 		return;
1010df0566a6SJani Nikula 
1011dbd440d8SJani Nikula 	switch (i915->vbt.dsi.config->dl_dcs_cabc_ports) {
1012df0566a6SJani Nikula 	case DL_DCS_PORT_A:
1013dbd440d8SJani Nikula 		i915->vbt.dsi.cabc_ports = BIT(PORT_A);
1014df0566a6SJani Nikula 		break;
1015df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1016dbd440d8SJani Nikula 		i915->vbt.dsi.cabc_ports = BIT(PORT_C);
1017df0566a6SJani Nikula 		break;
1018df0566a6SJani Nikula 	default:
1019df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
1020dbd440d8SJani Nikula 		i915->vbt.dsi.cabc_ports =
1021df0566a6SJani Nikula 					BIT(PORT_A) | BIT(PORT_C);
1022df0566a6SJani Nikula 		break;
1023df0566a6SJani Nikula 	}
1024df0566a6SJani Nikula }
1025df0566a6SJani Nikula 
1026df0566a6SJani Nikula static void
1027dbd440d8SJani Nikula parse_mipi_config(struct drm_i915_private *i915,
1028df0566a6SJani Nikula 		  const struct bdb_header *bdb)
1029df0566a6SJani Nikula {
1030df0566a6SJani Nikula 	const struct bdb_mipi_config *start;
1031df0566a6SJani Nikula 	const struct mipi_config *config;
1032df0566a6SJani Nikula 	const struct mipi_pps_data *pps;
1033dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
1034df0566a6SJani Nikula 	enum port port;
1035df0566a6SJani Nikula 
1036df0566a6SJani Nikula 	/* parse MIPI blocks only if LFP type is MIPI */
1037dbd440d8SJani Nikula 	if (!intel_bios_is_dsi_present(i915, &port))
1038df0566a6SJani Nikula 		return;
1039df0566a6SJani Nikula 
1040df0566a6SJani Nikula 	/* Initialize this to undefined indicating no generic MIPI support */
1041dbd440d8SJani Nikula 	i915->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
1042df0566a6SJani Nikula 
1043df0566a6SJani Nikula 	/* Block #40 is already parsed and panel_fixed_mode is
1044dbd440d8SJani Nikula 	 * stored in i915->lfp_lvds_vbt_mode
1045df0566a6SJani Nikula 	 * resuse this when needed
1046df0566a6SJani Nikula 	 */
1047df0566a6SJani Nikula 
1048df0566a6SJani Nikula 	/* Parse #52 for panel index used from panel_type already
1049df0566a6SJani Nikula 	 * parsed
1050df0566a6SJani Nikula 	 */
1051df0566a6SJani Nikula 	start = find_section(bdb, BDB_MIPI_CONFIG);
1052df0566a6SJani Nikula 	if (!start) {
1053dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No MIPI config BDB found");
1054df0566a6SJani Nikula 		return;
1055df0566a6SJani Nikula 	}
1056df0566a6SJani Nikula 
1057dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI Config block, panel index = %d\n",
1058df0566a6SJani Nikula 		panel_type);
1059df0566a6SJani Nikula 
1060df0566a6SJani Nikula 	/*
1061df0566a6SJani Nikula 	 * get hold of the correct configuration block and pps data as per
1062df0566a6SJani Nikula 	 * the panel_type as index
1063df0566a6SJani Nikula 	 */
1064df0566a6SJani Nikula 	config = &start->config[panel_type];
1065df0566a6SJani Nikula 	pps = &start->pps[panel_type];
1066df0566a6SJani Nikula 
1067df0566a6SJani Nikula 	/* store as of now full data. Trim when we realise all is not needed */
1068dbd440d8SJani Nikula 	i915->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
1069dbd440d8SJani Nikula 	if (!i915->vbt.dsi.config)
1070df0566a6SJani Nikula 		return;
1071df0566a6SJani Nikula 
1072dbd440d8SJani Nikula 	i915->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
1073dbd440d8SJani Nikula 	if (!i915->vbt.dsi.pps) {
1074dbd440d8SJani Nikula 		kfree(i915->vbt.dsi.config);
1075df0566a6SJani Nikula 		return;
1076df0566a6SJani Nikula 	}
1077df0566a6SJani Nikula 
1078dbd440d8SJani Nikula 	parse_dsi_backlight_ports(i915, bdb->version, port);
1079df0566a6SJani Nikula 
1080df0566a6SJani Nikula 	/* FIXME is the 90 vs. 270 correct? */
1081df0566a6SJani Nikula 	switch (config->rotation) {
1082df0566a6SJani Nikula 	case ENABLE_ROTATION_0:
1083df0566a6SJani Nikula 		/*
1084df0566a6SJani Nikula 		 * Most (all?) VBTs claim 0 degrees despite having
1085df0566a6SJani Nikula 		 * an upside down panel, thus we do not trust this.
1086df0566a6SJani Nikula 		 */
1087dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1088df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1089df0566a6SJani Nikula 		break;
1090df0566a6SJani Nikula 	case ENABLE_ROTATION_90:
1091dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1092df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1093df0566a6SJani Nikula 		break;
1094df0566a6SJani Nikula 	case ENABLE_ROTATION_180:
1095dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1096df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1097df0566a6SJani Nikula 		break;
1098df0566a6SJani Nikula 	case ENABLE_ROTATION_270:
1099dbd440d8SJani Nikula 		i915->vbt.dsi.orientation =
1100df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1101df0566a6SJani Nikula 		break;
1102df0566a6SJani Nikula 	}
1103df0566a6SJani Nikula 
1104df0566a6SJani Nikula 	/* We have mandatory mipi config blocks. Initialize as generic panel */
1105dbd440d8SJani Nikula 	i915->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
1106df0566a6SJani Nikula }
1107df0566a6SJani Nikula 
1108df0566a6SJani Nikula /* Find the sequence block and size for the given panel. */
1109df0566a6SJani Nikula static const u8 *
1110df0566a6SJani Nikula find_panel_sequence_block(const struct bdb_mipi_sequence *sequence,
1111df0566a6SJani Nikula 			  u16 panel_id, u32 *seq_size)
1112df0566a6SJani Nikula {
1113df0566a6SJani Nikula 	u32 total = get_blocksize(sequence);
1114df0566a6SJani Nikula 	const u8 *data = &sequence->data[0];
1115df0566a6SJani Nikula 	u8 current_id;
1116df0566a6SJani Nikula 	u32 current_size;
1117df0566a6SJani Nikula 	int header_size = sequence->version >= 3 ? 5 : 3;
1118df0566a6SJani Nikula 	int index = 0;
1119df0566a6SJani Nikula 	int i;
1120df0566a6SJani Nikula 
1121df0566a6SJani Nikula 	/* skip new block size */
1122df0566a6SJani Nikula 	if (sequence->version >= 3)
1123df0566a6SJani Nikula 		data += 4;
1124df0566a6SJani Nikula 
1125df0566a6SJani Nikula 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
1126df0566a6SJani Nikula 		if (index + header_size > total) {
1127df0566a6SJani Nikula 			DRM_ERROR("Invalid sequence block (header)\n");
1128df0566a6SJani Nikula 			return NULL;
1129df0566a6SJani Nikula 		}
1130df0566a6SJani Nikula 
1131df0566a6SJani Nikula 		current_id = *(data + index);
1132df0566a6SJani Nikula 		if (sequence->version >= 3)
1133df0566a6SJani Nikula 			current_size = *((const u32 *)(data + index + 1));
1134df0566a6SJani Nikula 		else
1135df0566a6SJani Nikula 			current_size = *((const u16 *)(data + index + 1));
1136df0566a6SJani Nikula 
1137df0566a6SJani Nikula 		index += header_size;
1138df0566a6SJani Nikula 
1139df0566a6SJani Nikula 		if (index + current_size > total) {
1140df0566a6SJani Nikula 			DRM_ERROR("Invalid sequence block\n");
1141df0566a6SJani Nikula 			return NULL;
1142df0566a6SJani Nikula 		}
1143df0566a6SJani Nikula 
1144df0566a6SJani Nikula 		if (current_id == panel_id) {
1145df0566a6SJani Nikula 			*seq_size = current_size;
1146df0566a6SJani Nikula 			return data + index;
1147df0566a6SJani Nikula 		}
1148df0566a6SJani Nikula 
1149df0566a6SJani Nikula 		index += current_size;
1150df0566a6SJani Nikula 	}
1151df0566a6SJani Nikula 
1152df0566a6SJani Nikula 	DRM_ERROR("Sequence block detected but no valid configuration\n");
1153df0566a6SJani Nikula 
1154df0566a6SJani Nikula 	return NULL;
1155df0566a6SJani Nikula }
1156df0566a6SJani Nikula 
1157df0566a6SJani Nikula static int goto_next_sequence(const u8 *data, int index, int total)
1158df0566a6SJani Nikula {
1159df0566a6SJani Nikula 	u16 len;
1160df0566a6SJani Nikula 
1161df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1162df0566a6SJani Nikula 	for (index = index + 1; index < total; index += len) {
1163df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1164df0566a6SJani Nikula 		index++;
1165df0566a6SJani Nikula 
1166df0566a6SJani Nikula 		switch (operation_byte) {
1167df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_END:
1168df0566a6SJani Nikula 			return index;
1169df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1170df0566a6SJani Nikula 			if (index + 4 > total)
1171df0566a6SJani Nikula 				return 0;
1172df0566a6SJani Nikula 
1173df0566a6SJani Nikula 			len = *((const u16 *)(data + index + 2)) + 4;
1174df0566a6SJani Nikula 			break;
1175df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1176df0566a6SJani Nikula 			len = 4;
1177df0566a6SJani Nikula 			break;
1178df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1179df0566a6SJani Nikula 			len = 2;
1180df0566a6SJani Nikula 			break;
1181df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1182df0566a6SJani Nikula 			if (index + 7 > total)
1183df0566a6SJani Nikula 				return 0;
1184df0566a6SJani Nikula 			len = *(data + index + 6) + 7;
1185df0566a6SJani Nikula 			break;
1186df0566a6SJani Nikula 		default:
1187df0566a6SJani Nikula 			DRM_ERROR("Unknown operation byte\n");
1188df0566a6SJani Nikula 			return 0;
1189df0566a6SJani Nikula 		}
1190df0566a6SJani Nikula 	}
1191df0566a6SJani Nikula 
1192df0566a6SJani Nikula 	return 0;
1193df0566a6SJani Nikula }
1194df0566a6SJani Nikula 
1195df0566a6SJani Nikula static int goto_next_sequence_v3(const u8 *data, int index, int total)
1196df0566a6SJani Nikula {
1197df0566a6SJani Nikula 	int seq_end;
1198df0566a6SJani Nikula 	u16 len;
1199df0566a6SJani Nikula 	u32 size_of_sequence;
1200df0566a6SJani Nikula 
1201df0566a6SJani Nikula 	/*
1202df0566a6SJani Nikula 	 * Could skip sequence based on Size of Sequence alone, but also do some
1203df0566a6SJani Nikula 	 * checking on the structure.
1204df0566a6SJani Nikula 	 */
1205df0566a6SJani Nikula 	if (total < 5) {
1206df0566a6SJani Nikula 		DRM_ERROR("Too small sequence size\n");
1207df0566a6SJani Nikula 		return 0;
1208df0566a6SJani Nikula 	}
1209df0566a6SJani Nikula 
1210df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1211df0566a6SJani Nikula 	index++;
1212df0566a6SJani Nikula 
1213df0566a6SJani Nikula 	/*
1214df0566a6SJani Nikula 	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
1215df0566a6SJani Nikula 	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
1216df0566a6SJani Nikula 	 * byte.
1217df0566a6SJani Nikula 	 */
1218df0566a6SJani Nikula 	size_of_sequence = *((const u32 *)(data + index));
1219df0566a6SJani Nikula 	index += 4;
1220df0566a6SJani Nikula 
1221df0566a6SJani Nikula 	seq_end = index + size_of_sequence;
1222df0566a6SJani Nikula 	if (seq_end > total) {
1223df0566a6SJani Nikula 		DRM_ERROR("Invalid sequence size\n");
1224df0566a6SJani Nikula 		return 0;
1225df0566a6SJani Nikula 	}
1226df0566a6SJani Nikula 
1227df0566a6SJani Nikula 	for (; index < total; index += len) {
1228df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1229df0566a6SJani Nikula 		index++;
1230df0566a6SJani Nikula 
1231df0566a6SJani Nikula 		if (operation_byte == MIPI_SEQ_ELEM_END) {
1232df0566a6SJani Nikula 			if (index != seq_end) {
1233df0566a6SJani Nikula 				DRM_ERROR("Invalid element structure\n");
1234df0566a6SJani Nikula 				return 0;
1235df0566a6SJani Nikula 			}
1236df0566a6SJani Nikula 			return index;
1237df0566a6SJani Nikula 		}
1238df0566a6SJani Nikula 
1239df0566a6SJani Nikula 		len = *(data + index);
1240df0566a6SJani Nikula 		index++;
1241df0566a6SJani Nikula 
1242df0566a6SJani Nikula 		/*
1243df0566a6SJani Nikula 		 * FIXME: Would be nice to check elements like for v1/v2 in
1244df0566a6SJani Nikula 		 * goto_next_sequence() above.
1245df0566a6SJani Nikula 		 */
1246df0566a6SJani Nikula 		switch (operation_byte) {
1247df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1248df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1249df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1250df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1251df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SPI:
1252df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_PMIC:
1253df0566a6SJani Nikula 			break;
1254df0566a6SJani Nikula 		default:
1255df0566a6SJani Nikula 			DRM_ERROR("Unknown operation byte %u\n",
1256df0566a6SJani Nikula 				  operation_byte);
1257df0566a6SJani Nikula 			break;
1258df0566a6SJani Nikula 		}
1259df0566a6SJani Nikula 	}
1260df0566a6SJani Nikula 
1261df0566a6SJani Nikula 	return 0;
1262df0566a6SJani Nikula }
1263df0566a6SJani Nikula 
1264df0566a6SJani Nikula /*
1265df0566a6SJani Nikula  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
1266df0566a6SJani Nikula  * skip all delay + gpio operands and stop at the first DSI packet op.
1267df0566a6SJani Nikula  */
1268dbd440d8SJani Nikula static int get_init_otp_deassert_fragment_len(struct drm_i915_private *i915)
1269df0566a6SJani Nikula {
1270dbd440d8SJani Nikula 	const u8 *data = i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1271df0566a6SJani Nikula 	int index, len;
1272df0566a6SJani Nikula 
1273dbd440d8SJani Nikula 	if (drm_WARN_ON(&i915->drm,
1274dbd440d8SJani Nikula 			!data || i915->vbt.dsi.seq_version != 1))
1275df0566a6SJani Nikula 		return 0;
1276df0566a6SJani Nikula 
1277df0566a6SJani Nikula 	/* index = 1 to skip sequence byte */
1278df0566a6SJani Nikula 	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1279df0566a6SJani Nikula 		switch (data[index]) {
1280df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1281df0566a6SJani Nikula 			return index == 1 ? 0 : index;
1282df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1283df0566a6SJani Nikula 			len = 5; /* 1 byte for operand + uint32 */
1284df0566a6SJani Nikula 			break;
1285df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1286df0566a6SJani Nikula 			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1287df0566a6SJani Nikula 			break;
1288df0566a6SJani Nikula 		default:
1289df0566a6SJani Nikula 			return 0;
1290df0566a6SJani Nikula 		}
1291df0566a6SJani Nikula 	}
1292df0566a6SJani Nikula 
1293df0566a6SJani Nikula 	return 0;
1294df0566a6SJani Nikula }
1295df0566a6SJani Nikula 
1296df0566a6SJani Nikula /*
1297df0566a6SJani Nikula  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1298df0566a6SJani Nikula  * The deassert must be done before calling intel_dsi_device_ready, so for
1299df0566a6SJani Nikula  * these devices we split the init OTP sequence into a deassert sequence and
1300df0566a6SJani Nikula  * the actual init OTP part.
1301df0566a6SJani Nikula  */
1302dbd440d8SJani Nikula static void fixup_mipi_sequences(struct drm_i915_private *i915)
1303df0566a6SJani Nikula {
1304df0566a6SJani Nikula 	u8 *init_otp;
1305df0566a6SJani Nikula 	int len;
1306df0566a6SJani Nikula 
1307df0566a6SJani Nikula 	/* Limit this to VLV for now. */
1308dbd440d8SJani Nikula 	if (!IS_VALLEYVIEW(i915))
1309df0566a6SJani Nikula 		return;
1310df0566a6SJani Nikula 
1311df0566a6SJani Nikula 	/* Limit this to v1 vid-mode sequences */
1312dbd440d8SJani Nikula 	if (i915->vbt.dsi.config->is_cmd_mode ||
1313dbd440d8SJani Nikula 	    i915->vbt.dsi.seq_version != 1)
1314df0566a6SJani Nikula 		return;
1315df0566a6SJani Nikula 
1316df0566a6SJani Nikula 	/* Only do this if there are otp and assert seqs and no deassert seq */
1317dbd440d8SJani Nikula 	if (!i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
1318dbd440d8SJani Nikula 	    !i915->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
1319dbd440d8SJani Nikula 	    i915->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1320df0566a6SJani Nikula 		return;
1321df0566a6SJani Nikula 
1322df0566a6SJani Nikula 	/* The deassert-sequence ends at the first DSI packet */
1323dbd440d8SJani Nikula 	len = get_init_otp_deassert_fragment_len(i915);
1324df0566a6SJani Nikula 	if (!len)
1325df0566a6SJani Nikula 		return;
1326df0566a6SJani Nikula 
1327dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1328e92cbf38SWambui Karuga 		    "Using init OTP fragment to deassert reset\n");
1329df0566a6SJani Nikula 
1330df0566a6SJani Nikula 	/* Copy the fragment, update seq byte and terminate it */
1331dbd440d8SJani Nikula 	init_otp = (u8 *)i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1332dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
1333dbd440d8SJani Nikula 	if (!i915->vbt.dsi.deassert_seq)
1334df0566a6SJani Nikula 		return;
1335dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
1336dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1337df0566a6SJani Nikula 	/* Use the copy for deassert */
1338dbd440d8SJani Nikula 	i915->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
1339dbd440d8SJani Nikula 		i915->vbt.dsi.deassert_seq;
1340df0566a6SJani Nikula 	/* Replace the last byte of the fragment with init OTP seq byte */
1341df0566a6SJani Nikula 	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1342df0566a6SJani Nikula 	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
1343dbd440d8SJani Nikula 	i915->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
1344df0566a6SJani Nikula }
1345df0566a6SJani Nikula 
1346df0566a6SJani Nikula static void
1347dbd440d8SJani Nikula parse_mipi_sequence(struct drm_i915_private *i915,
1348df0566a6SJani Nikula 		    const struct bdb_header *bdb)
1349df0566a6SJani Nikula {
1350dbd440d8SJani Nikula 	int panel_type = i915->vbt.panel_type;
1351df0566a6SJani Nikula 	const struct bdb_mipi_sequence *sequence;
1352df0566a6SJani Nikula 	const u8 *seq_data;
1353df0566a6SJani Nikula 	u32 seq_size;
1354df0566a6SJani Nikula 	u8 *data;
1355df0566a6SJani Nikula 	int index = 0;
1356df0566a6SJani Nikula 
1357df0566a6SJani Nikula 	/* Only our generic panel driver uses the sequence block. */
1358dbd440d8SJani Nikula 	if (i915->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
1359df0566a6SJani Nikula 		return;
1360df0566a6SJani Nikula 
1361df0566a6SJani Nikula 	sequence = find_section(bdb, BDB_MIPI_SEQUENCE);
1362df0566a6SJani Nikula 	if (!sequence) {
1363dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1364e92cbf38SWambui Karuga 			    "No MIPI Sequence found, parsing complete\n");
1365df0566a6SJani Nikula 		return;
1366df0566a6SJani Nikula 	}
1367df0566a6SJani Nikula 
1368df0566a6SJani Nikula 	/* Fail gracefully for forward incompatible sequence block. */
1369df0566a6SJani Nikula 	if (sequence->version >= 4) {
1370dbd440d8SJani Nikula 		drm_err(&i915->drm,
1371e92cbf38SWambui Karuga 			"Unable to parse MIPI Sequence Block v%u\n",
1372df0566a6SJani Nikula 			sequence->version);
1373df0566a6SJani Nikula 		return;
1374df0566a6SJani Nikula 	}
1375df0566a6SJani Nikula 
1376dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI sequence block v%u\n",
1377e92cbf38SWambui Karuga 		sequence->version);
1378df0566a6SJani Nikula 
1379df0566a6SJani Nikula 	seq_data = find_panel_sequence_block(sequence, panel_type, &seq_size);
1380df0566a6SJani Nikula 	if (!seq_data)
1381df0566a6SJani Nikula 		return;
1382df0566a6SJani Nikula 
1383df0566a6SJani Nikula 	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
1384df0566a6SJani Nikula 	if (!data)
1385df0566a6SJani Nikula 		return;
1386df0566a6SJani Nikula 
1387df0566a6SJani Nikula 	/* Parse the sequences, store pointers to each sequence. */
1388df0566a6SJani Nikula 	for (;;) {
1389df0566a6SJani Nikula 		u8 seq_id = *(data + index);
1390df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_END)
1391df0566a6SJani Nikula 			break;
1392df0566a6SJani Nikula 
1393df0566a6SJani Nikula 		if (seq_id >= MIPI_SEQ_MAX) {
1394dbd440d8SJani Nikula 			drm_err(&i915->drm, "Unknown sequence %u\n",
1395e92cbf38SWambui Karuga 				seq_id);
1396df0566a6SJani Nikula 			goto err;
1397df0566a6SJani Nikula 		}
1398df0566a6SJani Nikula 
1399df0566a6SJani Nikula 		/* Log about presence of sequences we won't run. */
1400df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
1401dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1402e92cbf38SWambui Karuga 				    "Unsupported sequence %u\n", seq_id);
1403df0566a6SJani Nikula 
1404dbd440d8SJani Nikula 		i915->vbt.dsi.sequence[seq_id] = data + index;
1405df0566a6SJani Nikula 
1406df0566a6SJani Nikula 		if (sequence->version >= 3)
1407df0566a6SJani Nikula 			index = goto_next_sequence_v3(data, index, seq_size);
1408df0566a6SJani Nikula 		else
1409df0566a6SJani Nikula 			index = goto_next_sequence(data, index, seq_size);
1410df0566a6SJani Nikula 		if (!index) {
1411dbd440d8SJani Nikula 			drm_err(&i915->drm, "Invalid sequence %u\n",
1412e92cbf38SWambui Karuga 				seq_id);
1413df0566a6SJani Nikula 			goto err;
1414df0566a6SJani Nikula 		}
1415df0566a6SJani Nikula 	}
1416df0566a6SJani Nikula 
1417dbd440d8SJani Nikula 	i915->vbt.dsi.data = data;
1418dbd440d8SJani Nikula 	i915->vbt.dsi.size = seq_size;
1419dbd440d8SJani Nikula 	i915->vbt.dsi.seq_version = sequence->version;
1420df0566a6SJani Nikula 
1421dbd440d8SJani Nikula 	fixup_mipi_sequences(i915);
1422df0566a6SJani Nikula 
1423dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "MIPI related VBT parsing complete\n");
1424df0566a6SJani Nikula 	return;
1425df0566a6SJani Nikula 
1426df0566a6SJani Nikula err:
1427df0566a6SJani Nikula 	kfree(data);
1428dbd440d8SJani Nikula 	memset(i915->vbt.dsi.sequence, 0, sizeof(i915->vbt.dsi.sequence));
1429df0566a6SJani Nikula }
1430df0566a6SJani Nikula 
14316e0d46e9SJani Nikula static void
14326e0d46e9SJani Nikula parse_compression_parameters(struct drm_i915_private *i915,
14336e0d46e9SJani Nikula 			     const struct bdb_header *bdb)
14346e0d46e9SJani Nikula {
14356e0d46e9SJani Nikula 	const struct bdb_compression_parameters *params;
14363162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
14376e0d46e9SJani Nikula 	const struct child_device_config *child;
14386e0d46e9SJani Nikula 	u16 block_size;
14396e0d46e9SJani Nikula 	int index;
14406e0d46e9SJani Nikula 
14416e0d46e9SJani Nikula 	if (bdb->version < 198)
14426e0d46e9SJani Nikula 		return;
14436e0d46e9SJani Nikula 
14446e0d46e9SJani Nikula 	params = find_section(bdb, BDB_COMPRESSION_PARAMETERS);
14456e0d46e9SJani Nikula 	if (params) {
14466e0d46e9SJani Nikula 		/* Sanity checks */
14476e0d46e9SJani Nikula 		if (params->entry_size != sizeof(params->data[0])) {
1448e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1449e92cbf38SWambui Karuga 				    "VBT: unsupported compression param entry size\n");
14506e0d46e9SJani Nikula 			return;
14516e0d46e9SJani Nikula 		}
14526e0d46e9SJani Nikula 
14536e0d46e9SJani Nikula 		block_size = get_blocksize(params);
14546e0d46e9SJani Nikula 		if (block_size < sizeof(*params)) {
1455e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1456e92cbf38SWambui Karuga 				    "VBT: expected 16 compression param entries\n");
14576e0d46e9SJani Nikula 			return;
14586e0d46e9SJani Nikula 		}
14596e0d46e9SJani Nikula 	}
14606e0d46e9SJani Nikula 
14616e0d46e9SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
14626e0d46e9SJani Nikula 		child = &devdata->child;
14636e0d46e9SJani Nikula 
14646e0d46e9SJani Nikula 		if (!child->compression_enable)
14656e0d46e9SJani Nikula 			continue;
14666e0d46e9SJani Nikula 
14676e0d46e9SJani Nikula 		if (!params) {
1468e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1469e92cbf38SWambui Karuga 				    "VBT: compression params not available\n");
14706e0d46e9SJani Nikula 			continue;
14716e0d46e9SJani Nikula 		}
14726e0d46e9SJani Nikula 
14736e0d46e9SJani Nikula 		if (child->compression_method_cps) {
1474e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
1475e92cbf38SWambui Karuga 				    "VBT: CPS compression not supported\n");
14766e0d46e9SJani Nikula 			continue;
14776e0d46e9SJani Nikula 		}
14786e0d46e9SJani Nikula 
14796e0d46e9SJani Nikula 		index = child->compression_structure_index;
14806e0d46e9SJani Nikula 
14816e0d46e9SJani Nikula 		devdata->dsc = kmemdup(&params->data[index],
14826e0d46e9SJani Nikula 				       sizeof(*devdata->dsc), GFP_KERNEL);
14836e0d46e9SJani Nikula 	}
14846e0d46e9SJani Nikula }
14856e0d46e9SJani Nikula 
1486df0566a6SJani Nikula static u8 translate_iboost(u8 val)
1487df0566a6SJani Nikula {
1488df0566a6SJani Nikula 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
1489df0566a6SJani Nikula 
1490df0566a6SJani Nikula 	if (val >= ARRAY_SIZE(mapping)) {
1491df0566a6SJani Nikula 		DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
1492df0566a6SJani Nikula 		return 0;
1493df0566a6SJani Nikula 	}
1494df0566a6SJani Nikula 	return mapping[val];
1495df0566a6SJani Nikula }
1496df0566a6SJani Nikula 
14979e1dbc1aSJani Nikula static const u8 cnp_ddc_pin_map[] = {
14989e1dbc1aSJani Nikula 	[0] = 0, /* N/A */
14999e1dbc1aSJani Nikula 	[DDC_BUS_DDI_B] = GMBUS_PIN_1_BXT,
15009e1dbc1aSJani Nikula 	[DDC_BUS_DDI_C] = GMBUS_PIN_2_BXT,
15019e1dbc1aSJani Nikula 	[DDC_BUS_DDI_D] = GMBUS_PIN_4_CNP, /* sic */
15029e1dbc1aSJani Nikula 	[DDC_BUS_DDI_F] = GMBUS_PIN_3_BXT, /* sic */
15039e1dbc1aSJani Nikula };
15049e1dbc1aSJani Nikula 
15059e1dbc1aSJani Nikula static const u8 icp_ddc_pin_map[] = {
15069e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
15079e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
15089e1dbc1aSJani Nikula 	[TGL_DDC_BUS_DDI_C] = GMBUS_PIN_3_BXT,
15099e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_1] = GMBUS_PIN_9_TC1_ICP,
15109e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_2] = GMBUS_PIN_10_TC2_ICP,
15119e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_3] = GMBUS_PIN_11_TC3_ICP,
15129e1dbc1aSJani Nikula 	[ICL_DDC_BUS_PORT_4] = GMBUS_PIN_12_TC4_ICP,
15139e1dbc1aSJani Nikula 	[TGL_DDC_BUS_PORT_5] = GMBUS_PIN_13_TC5_TGP,
15149e1dbc1aSJani Nikula 	[TGL_DDC_BUS_PORT_6] = GMBUS_PIN_14_TC6_TGP,
15159e1dbc1aSJani Nikula };
15169e1dbc1aSJani Nikula 
15179e1dbc1aSJani Nikula static const u8 rkl_pch_tgp_ddc_pin_map[] = {
15189e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
15199e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
15209e1dbc1aSJani Nikula 	[RKL_DDC_BUS_DDI_D] = GMBUS_PIN_9_TC1_ICP,
15219e1dbc1aSJani Nikula 	[RKL_DDC_BUS_DDI_E] = GMBUS_PIN_10_TC2_ICP,
15229e1dbc1aSJani Nikula };
15239e1dbc1aSJani Nikula 
15249e1dbc1aSJani Nikula static const u8 adls_ddc_pin_map[] = {
15259e1dbc1aSJani Nikula 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
15269e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC1] = GMBUS_PIN_9_TC1_ICP,
15279e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC2] = GMBUS_PIN_10_TC2_ICP,
15289e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC3] = GMBUS_PIN_11_TC3_ICP,
15299e1dbc1aSJani Nikula 	[ADLS_DDC_BUS_PORT_TC4] = GMBUS_PIN_12_TC4_ICP,
15309e1dbc1aSJani Nikula };
15319e1dbc1aSJani Nikula 
15329e1dbc1aSJani Nikula static const u8 gen9bc_tgp_ddc_pin_map[] = {
15339e1dbc1aSJani Nikula 	[DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
15349e1dbc1aSJani Nikula 	[DDC_BUS_DDI_C] = GMBUS_PIN_9_TC1_ICP,
15359e1dbc1aSJani Nikula 	[DDC_BUS_DDI_D] = GMBUS_PIN_10_TC2_ICP,
15369e1dbc1aSJani Nikula };
15379e1dbc1aSJani Nikula 
1538af10ec31STejas Upadhyay static const u8 adlp_ddc_pin_map[] = {
1539af10ec31STejas Upadhyay 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
1540af10ec31STejas Upadhyay 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
1541af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC1] = GMBUS_PIN_9_TC1_ICP,
1542af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC2] = GMBUS_PIN_10_TC2_ICP,
1543af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC3] = GMBUS_PIN_11_TC3_ICP,
1544af10ec31STejas Upadhyay 	[ADLP_DDC_BUS_PORT_TC4] = GMBUS_PIN_12_TC4_ICP,
1545af10ec31STejas Upadhyay };
1546af10ec31STejas Upadhyay 
15479e1dbc1aSJani Nikula static u8 map_ddc_pin(struct drm_i915_private *i915, u8 vbt_pin)
15489e1dbc1aSJani Nikula {
15499e1dbc1aSJani Nikula 	const u8 *ddc_pin_map;
15509e1dbc1aSJani Nikula 	int n_entries;
15519e1dbc1aSJani Nikula 
1552af10ec31STejas Upadhyay 	if (IS_ALDERLAKE_P(i915)) {
1553af10ec31STejas Upadhyay 		ddc_pin_map = adlp_ddc_pin_map;
1554af10ec31STejas Upadhyay 		n_entries = ARRAY_SIZE(adlp_ddc_pin_map);
1555af10ec31STejas Upadhyay 	} else if (IS_ALDERLAKE_S(i915)) {
15569e1dbc1aSJani Nikula 		ddc_pin_map = adls_ddc_pin_map;
15579e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(adls_ddc_pin_map);
15589e1dbc1aSJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_DG1) {
15599e1dbc1aSJani Nikula 		return vbt_pin;
15609e1dbc1aSJani Nikula 	} else if (IS_ROCKETLAKE(i915) && INTEL_PCH_TYPE(i915) == PCH_TGP) {
15619e1dbc1aSJani Nikula 		ddc_pin_map = rkl_pch_tgp_ddc_pin_map;
15629e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(rkl_pch_tgp_ddc_pin_map);
15639e1dbc1aSJani Nikula 	} else if (HAS_PCH_TGP(i915) && DISPLAY_VER(i915) == 9) {
15649e1dbc1aSJani Nikula 		ddc_pin_map = gen9bc_tgp_ddc_pin_map;
15659e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(gen9bc_tgp_ddc_pin_map);
15669e1dbc1aSJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_ICP) {
15679e1dbc1aSJani Nikula 		ddc_pin_map = icp_ddc_pin_map;
15689e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
15699e1dbc1aSJani Nikula 	} else if (HAS_PCH_CNP(i915)) {
15709e1dbc1aSJani Nikula 		ddc_pin_map = cnp_ddc_pin_map;
15719e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
15729e1dbc1aSJani Nikula 	} else {
15739e1dbc1aSJani Nikula 		/* Assuming direct map */
15749e1dbc1aSJani Nikula 		return vbt_pin;
15759e1dbc1aSJani Nikula 	}
15769e1dbc1aSJani Nikula 
15779e1dbc1aSJani Nikula 	if (vbt_pin < n_entries && ddc_pin_map[vbt_pin] != 0)
15789e1dbc1aSJani Nikula 		return ddc_pin_map[vbt_pin];
15799e1dbc1aSJani Nikula 
15809e1dbc1aSJani Nikula 	drm_dbg_kms(&i915->drm,
15819e1dbc1aSJani Nikula 		    "Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
15829e1dbc1aSJani Nikula 		    vbt_pin);
15839e1dbc1aSJani Nikula 	return 0;
15849e1dbc1aSJani Nikula }
15859e1dbc1aSJani Nikula 
1586df0566a6SJani Nikula static enum port get_port_by_ddc_pin(struct drm_i915_private *i915, u8 ddc_pin)
1587df0566a6SJani Nikula {
15885a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata;
1589df0566a6SJani Nikula 	enum port port;
1590df0566a6SJani Nikula 
159195bbede5SJani Nikula 	if (!ddc_pin)
159295bbede5SJani Nikula 		return PORT_NONE;
159395bbede5SJani Nikula 
1594c4a774c4SJani Nikula 	for_each_port(port) {
15955a449e58SJani Nikula 		devdata = i915->vbt.ports[port];
1596df0566a6SJani Nikula 
15975a449e58SJani Nikula 		if (devdata && ddc_pin == devdata->child.ddc_pin)
1598df0566a6SJani Nikula 			return port;
1599df0566a6SJani Nikula 	}
1600df0566a6SJani Nikula 
1601df0566a6SJani Nikula 	return PORT_NONE;
1602df0566a6SJani Nikula }
1603df0566a6SJani Nikula 
1604dab8477bSJani Nikula static void sanitize_ddc_pin(struct intel_bios_encoder_data *devdata,
1605df0566a6SJani Nikula 			     enum port port)
1606df0566a6SJani Nikula {
1607dab8477bSJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
160845c0673aSJani Nikula 	struct child_device_config *child;
1609dab8477bSJani Nikula 	u8 mapped_ddc_pin;
1610df0566a6SJani Nikula 	enum port p;
1611df0566a6SJani Nikula 
1612dab8477bSJani Nikula 	if (!devdata->child.ddc_pin)
1613dab8477bSJani Nikula 		return;
1614dab8477bSJani Nikula 
1615dab8477bSJani Nikula 	mapped_ddc_pin = map_ddc_pin(i915, devdata->child.ddc_pin);
1616dab8477bSJani Nikula 	if (!intel_gmbus_is_valid_pin(i915, mapped_ddc_pin)) {
1617dab8477bSJani Nikula 		drm_dbg_kms(&i915->drm,
1618dab8477bSJani Nikula 			    "Port %c has invalid DDC pin %d, "
1619dab8477bSJani Nikula 			    "sticking to defaults\n",
1620dab8477bSJani Nikula 			    port_name(port), mapped_ddc_pin);
1621dab8477bSJani Nikula 		devdata->child.ddc_pin = 0;
1622dab8477bSJani Nikula 		return;
1623dab8477bSJani Nikula 	}
1624dab8477bSJani Nikula 
1625dab8477bSJani Nikula 	p = get_port_by_ddc_pin(i915, devdata->child.ddc_pin);
1626894d1739SJani Nikula 	if (p == PORT_NONE)
1627894d1739SJani Nikula 		return;
1628894d1739SJani Nikula 
1629dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1630e92cbf38SWambui Karuga 		    "port %c trying to use the same DDC pin (0x%x) as port %c, "
1631df0566a6SJani Nikula 		    "disabling port %c DVI/HDMI support\n",
1632dab8477bSJani Nikula 		    port_name(port), mapped_ddc_pin,
163341e35ffbSVille Syrjälä 		    port_name(p), port_name(p));
1634df0566a6SJani Nikula 
1635df0566a6SJani Nikula 	/*
1636894d1739SJani Nikula 	 * If we have multiple ports supposedly sharing the pin, then dvi/hdmi
1637894d1739SJani Nikula 	 * couldn't exist on the shared port. Otherwise they share the same ddc
1638894d1739SJani Nikula 	 * pin and system couldn't communicate with them separately.
1639df0566a6SJani Nikula 	 *
1640894d1739SJani Nikula 	 * Give inverse child device order the priority, last one wins. Yes,
1641894d1739SJani Nikula 	 * there are real machines (eg. Asrock B250M-HDV) where VBT has both
1642894d1739SJani Nikula 	 * port A and port E with the same AUX ch and we must pick port E :(
1643df0566a6SJani Nikula 	 */
16445a449e58SJani Nikula 	child = &i915->vbt.ports[p]->child;
164541e35ffbSVille Syrjälä 
164645c0673aSJani Nikula 	child->device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
164745c0673aSJani Nikula 	child->device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
164845c0673aSJani Nikula 
1649dab8477bSJani Nikula 	child->ddc_pin = 0;
1650df0566a6SJani Nikula }
1651df0566a6SJani Nikula 
1652df0566a6SJani Nikula static enum port get_port_by_aux_ch(struct drm_i915_private *i915, u8 aux_ch)
1653df0566a6SJani Nikula {
16545a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata;
1655df0566a6SJani Nikula 	enum port port;
1656df0566a6SJani Nikula 
165795bbede5SJani Nikula 	if (!aux_ch)
165895bbede5SJani Nikula 		return PORT_NONE;
165995bbede5SJani Nikula 
1660c4a774c4SJani Nikula 	for_each_port(port) {
16615a449e58SJani Nikula 		devdata = i915->vbt.ports[port];
1662df0566a6SJani Nikula 
16635a449e58SJani Nikula 		if (devdata && aux_ch == devdata->child.aux_channel)
1664df0566a6SJani Nikula 			return port;
1665df0566a6SJani Nikula 	}
1666df0566a6SJani Nikula 
1667df0566a6SJani Nikula 	return PORT_NONE;
1668df0566a6SJani Nikula }
1669df0566a6SJani Nikula 
167011182986SJani Nikula static void sanitize_aux_ch(struct intel_bios_encoder_data *devdata,
1671df0566a6SJani Nikula 			    enum port port)
1672df0566a6SJani Nikula {
167311182986SJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
167445c0673aSJani Nikula 	struct child_device_config *child;
1675df0566a6SJani Nikula 	enum port p;
1676df0566a6SJani Nikula 
167711182986SJani Nikula 	p = get_port_by_aux_ch(i915, devdata->child.aux_channel);
1678894d1739SJani Nikula 	if (p == PORT_NONE)
1679894d1739SJani Nikula 		return;
1680894d1739SJani Nikula 
1681dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1682e92cbf38SWambui Karuga 		    "port %c trying to use the same AUX CH (0x%x) as port %c, "
1683df0566a6SJani Nikula 		    "disabling port %c DP support\n",
168411182986SJani Nikula 		    port_name(port), devdata->child.aux_channel,
168541e35ffbSVille Syrjälä 		    port_name(p), port_name(p));
1686df0566a6SJani Nikula 
1687df0566a6SJani Nikula 	/*
1688894d1739SJani Nikula 	 * If we have multiple ports supposedly sharing the aux channel, then DP
1689894d1739SJani Nikula 	 * couldn't exist on the shared port. Otherwise they share the same aux
1690894d1739SJani Nikula 	 * channel and system couldn't communicate with them separately.
1691df0566a6SJani Nikula 	 *
1692894d1739SJani Nikula 	 * Give inverse child device order the priority, last one wins. Yes,
1693894d1739SJani Nikula 	 * there are real machines (eg. Asrock B250M-HDV) where VBT has both
1694894d1739SJani Nikula 	 * port A and port E with the same AUX ch and we must pick port E :(
1695df0566a6SJani Nikula 	 */
16965a449e58SJani Nikula 	child = &i915->vbt.ports[p]->child;
169741e35ffbSVille Syrjälä 
169845c0673aSJani Nikula 	child->device_type &= ~DEVICE_TYPE_DISPLAYPORT_OUTPUT;
169911182986SJani Nikula 	child->aux_channel = 0;
1700df0566a6SJani Nikula }
1701df0566a6SJani Nikula 
170232c2bc89SVille Syrjälä static u8 dvo_port_type(u8 dvo_port)
170332c2bc89SVille Syrjälä {
170432c2bc89SVille Syrjälä 	switch (dvo_port) {
170532c2bc89SVille Syrjälä 	case DVO_PORT_HDMIA:
170632c2bc89SVille Syrjälä 	case DVO_PORT_HDMIB:
170732c2bc89SVille Syrjälä 	case DVO_PORT_HDMIC:
170832c2bc89SVille Syrjälä 	case DVO_PORT_HDMID:
170932c2bc89SVille Syrjälä 	case DVO_PORT_HDMIE:
171032c2bc89SVille Syrjälä 	case DVO_PORT_HDMIF:
171132c2bc89SVille Syrjälä 	case DVO_PORT_HDMIG:
171232c2bc89SVille Syrjälä 	case DVO_PORT_HDMIH:
171332c2bc89SVille Syrjälä 	case DVO_PORT_HDMII:
171432c2bc89SVille Syrjälä 		return DVO_PORT_HDMIA;
171532c2bc89SVille Syrjälä 	case DVO_PORT_DPA:
171632c2bc89SVille Syrjälä 	case DVO_PORT_DPB:
171732c2bc89SVille Syrjälä 	case DVO_PORT_DPC:
171832c2bc89SVille Syrjälä 	case DVO_PORT_DPD:
171932c2bc89SVille Syrjälä 	case DVO_PORT_DPE:
172032c2bc89SVille Syrjälä 	case DVO_PORT_DPF:
172132c2bc89SVille Syrjälä 	case DVO_PORT_DPG:
172232c2bc89SVille Syrjälä 	case DVO_PORT_DPH:
172332c2bc89SVille Syrjälä 	case DVO_PORT_DPI:
172432c2bc89SVille Syrjälä 		return DVO_PORT_DPA;
172532c2bc89SVille Syrjälä 	case DVO_PORT_MIPIA:
172632c2bc89SVille Syrjälä 	case DVO_PORT_MIPIB:
172732c2bc89SVille Syrjälä 	case DVO_PORT_MIPIC:
172832c2bc89SVille Syrjälä 	case DVO_PORT_MIPID:
172932c2bc89SVille Syrjälä 		return DVO_PORT_MIPIA;
173032c2bc89SVille Syrjälä 	default:
173132c2bc89SVille Syrjälä 		return dvo_port;
173232c2bc89SVille Syrjälä 	}
173332c2bc89SVille Syrjälä }
173432c2bc89SVille Syrjälä 
17354628142aSLucas De Marchi static enum port __dvo_port_to_port(int n_ports, int n_dvo,
17364628142aSLucas De Marchi 				    const int port_mapping[][3], u8 dvo_port)
1737df0566a6SJani Nikula {
1738df0566a6SJani Nikula 	enum port port;
1739df0566a6SJani Nikula 	int i;
1740df0566a6SJani Nikula 
17414628142aSLucas De Marchi 	for (port = PORT_A; port < n_ports; port++) {
17424628142aSLucas De Marchi 		for (i = 0; i < n_dvo; i++) {
17434628142aSLucas De Marchi 			if (port_mapping[port][i] == -1)
1744df0566a6SJani Nikula 				break;
1745df0566a6SJani Nikula 
17464628142aSLucas De Marchi 			if (dvo_port == port_mapping[port][i])
1747df0566a6SJani Nikula 				return port;
1748df0566a6SJani Nikula 		}
1749df0566a6SJani Nikula 	}
1750df0566a6SJani Nikula 
1751df0566a6SJani Nikula 	return PORT_NONE;
1752df0566a6SJani Nikula }
1753df0566a6SJani Nikula 
1754dbd440d8SJani Nikula static enum port dvo_port_to_port(struct drm_i915_private *i915,
17554628142aSLucas De Marchi 				  u8 dvo_port)
17564628142aSLucas De Marchi {
17574628142aSLucas De Marchi 	/*
17584628142aSLucas De Marchi 	 * Each DDI port can have more than one value on the "DVO Port" field,
17594628142aSLucas De Marchi 	 * so look for all the possible values for each port.
17604628142aSLucas De Marchi 	 */
17614628142aSLucas De Marchi 	static const int port_mapping[][3] = {
17624628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
17634628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
17644628142aSLucas De Marchi 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
17654628142aSLucas De Marchi 		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
17668c1a8f12SMatt Roper 		[PORT_E] = { DVO_PORT_HDMIE, DVO_PORT_DPE, DVO_PORT_CRT },
17674628142aSLucas De Marchi 		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
17684628142aSLucas De Marchi 		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
1769176430ccSVille Syrjälä 		[PORT_H] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
1770176430ccSVille Syrjälä 		[PORT_I] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
17714628142aSLucas De Marchi 	};
17724628142aSLucas De Marchi 	/*
17731d8ca002SVille Syrjälä 	 * RKL VBT uses PHY based mapping. Combo PHYs A,B,C,D
17741d8ca002SVille Syrjälä 	 * map to DDI A,B,TC1,TC2 respectively.
17754628142aSLucas De Marchi 	 */
17764628142aSLucas De Marchi 	static const int rkl_port_mapping[][3] = {
17774628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
17784628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
17794628142aSLucas De Marchi 		[PORT_C] = { -1 },
17801d8ca002SVille Syrjälä 		[PORT_TC1] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
17811d8ca002SVille Syrjälä 		[PORT_TC2] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
17824628142aSLucas De Marchi 	};
178318c283dfSAditya Swarup 	/*
178418c283dfSAditya Swarup 	 * Alderlake S ports used in the driver are PORT_A, PORT_D, PORT_E,
178518c283dfSAditya Swarup 	 * PORT_F and PORT_G, we need to map that to correct VBT sections.
178618c283dfSAditya Swarup 	 */
178718c283dfSAditya Swarup 	static const int adls_port_mapping[][3] = {
178818c283dfSAditya Swarup 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
178918c283dfSAditya Swarup 		[PORT_B] = { -1 },
179018c283dfSAditya Swarup 		[PORT_C] = { -1 },
179118c283dfSAditya Swarup 		[PORT_TC1] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
179218c283dfSAditya Swarup 		[PORT_TC2] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
179318c283dfSAditya Swarup 		[PORT_TC3] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
179418c283dfSAditya Swarup 		[PORT_TC4] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
179518c283dfSAditya Swarup 	};
1796eeb63c54SJosé Roberto de Souza 	static const int xelpd_port_mapping[][3] = {
1797eeb63c54SJosé Roberto de Souza 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
1798eeb63c54SJosé Roberto de Souza 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
1799eeb63c54SJosé Roberto de Souza 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
1800eeb63c54SJosé Roberto de Souza 		[PORT_D_XELPD] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
1801eeb63c54SJosé Roberto de Souza 		[PORT_E_XELPD] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
1802eeb63c54SJosé Roberto de Souza 		[PORT_TC1] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
1803eeb63c54SJosé Roberto de Souza 		[PORT_TC2] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
1804eeb63c54SJosé Roberto de Souza 		[PORT_TC3] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
1805eeb63c54SJosé Roberto de Souza 		[PORT_TC4] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
1806eeb63c54SJosé Roberto de Souza 	};
18074628142aSLucas De Marchi 
1808eeb63c54SJosé Roberto de Souza 	if (DISPLAY_VER(i915) == 13)
1809eeb63c54SJosé Roberto de Souza 		return __dvo_port_to_port(ARRAY_SIZE(xelpd_port_mapping),
1810eeb63c54SJosé Roberto de Souza 					  ARRAY_SIZE(xelpd_port_mapping[0]),
1811eeb63c54SJosé Roberto de Souza 					  xelpd_port_mapping,
1812eeb63c54SJosé Roberto de Souza 					  dvo_port);
1813eeb63c54SJosé Roberto de Souza 	else if (IS_ALDERLAKE_S(i915))
181418c283dfSAditya Swarup 		return __dvo_port_to_port(ARRAY_SIZE(adls_port_mapping),
181518c283dfSAditya Swarup 					  ARRAY_SIZE(adls_port_mapping[0]),
181618c283dfSAditya Swarup 					  adls_port_mapping,
181718c283dfSAditya Swarup 					  dvo_port);
1818dbd440d8SJani Nikula 	else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
18194628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(rkl_port_mapping),
18204628142aSLucas De Marchi 					  ARRAY_SIZE(rkl_port_mapping[0]),
18214628142aSLucas De Marchi 					  rkl_port_mapping,
18224628142aSLucas De Marchi 					  dvo_port);
18234628142aSLucas De Marchi 	else
18244628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(port_mapping),
18254628142aSLucas De Marchi 					  ARRAY_SIZE(port_mapping[0]),
18264628142aSLucas De Marchi 					  port_mapping,
18274628142aSLucas De Marchi 					  dvo_port);
18284628142aSLucas De Marchi }
18294628142aSLucas De Marchi 
1830b60e320bSLee Shawn C static int parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)
1831b60e320bSLee Shawn C {
1832b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
1833b60e320bSLee Shawn C 	default:
1834b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_DEF:
1835b60e320bSLee Shawn C 		return 0;
1836b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR20:
1837b60e320bSLee Shawn C 		return 2000000;
1838b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR13P5:
1839b60e320bSLee Shawn C 		return 1350000;
1840b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR10:
1841b60e320bSLee Shawn C 		return 1000000;
1842b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR3:
1843b60e320bSLee Shawn C 		return 810000;
1844b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR2:
1845b60e320bSLee Shawn C 		return 540000;
1846b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR:
1847b60e320bSLee Shawn C 		return 270000;
1848b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_LBR:
1849b60e320bSLee Shawn C 		return 162000;
1850b60e320bSLee Shawn C 	}
1851b60e320bSLee Shawn C }
1852b60e320bSLee Shawn C 
1853b60e320bSLee Shawn C static int parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)
1854b60e320bSLee Shawn C {
1855b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
1856b60e320bSLee Shawn C 	default:
1857b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR3:
1858b60e320bSLee Shawn C 		return 810000;
1859b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR2:
1860b60e320bSLee Shawn C 		return 540000;
1861b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR:
1862b60e320bSLee Shawn C 		return 270000;
1863b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_LBR:
1864b60e320bSLee Shawn C 		return 162000;
1865b60e320bSLee Shawn C 	}
1866b60e320bSLee Shawn C }
1867b60e320bSLee Shawn C 
186872337aacSJani Nikula static int _intel_bios_dp_max_link_rate(const struct intel_bios_encoder_data *devdata)
186972337aacSJani Nikula {
187072337aacSJani Nikula 	if (!devdata || devdata->i915->vbt.version < 216)
187172337aacSJani Nikula 		return 0;
187272337aacSJani Nikula 
187372337aacSJani Nikula 	if (devdata->i915->vbt.version >= 230)
187472337aacSJani Nikula 		return parse_bdb_230_dp_max_link_rate(devdata->child.dp_max_link_rate);
187572337aacSJani Nikula 	else
187672337aacSJani Nikula 		return parse_bdb_216_dp_max_link_rate(devdata->child.dp_max_link_rate);
187772337aacSJani Nikula }
187872337aacSJani Nikula 
1879d0ab409dSJani Nikula static void sanitize_device_type(struct intel_bios_encoder_data *devdata,
1880d0ab409dSJani Nikula 				 enum port port)
1881d0ab409dSJani Nikula {
1882d0ab409dSJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
1883d0ab409dSJani Nikula 	bool is_hdmi;
1884d0ab409dSJani Nikula 
1885005e9537SMatt Roper 	if (port != PORT_A || DISPLAY_VER(i915) >= 12)
1886d0ab409dSJani Nikula 		return;
1887d0ab409dSJani Nikula 
1888d0ab409dSJani Nikula 	if (!(devdata->child.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING))
1889d0ab409dSJani Nikula 		return;
1890d0ab409dSJani Nikula 
1891d0ab409dSJani Nikula 	is_hdmi = !(devdata->child.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT);
1892d0ab409dSJani Nikula 
1893d0ab409dSJani Nikula 	drm_dbg_kms(&i915->drm, "VBT claims port A supports DVI%s, ignoring\n",
1894d0ab409dSJani Nikula 		    is_hdmi ? "/HDMI" : "");
1895d0ab409dSJani Nikula 
1896d0ab409dSJani Nikula 	devdata->child.device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
1897d0ab409dSJani Nikula 	devdata->child.device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
1898d0ab409dSJani Nikula }
1899d0ab409dSJani Nikula 
1900d0ab409dSJani Nikula static bool
1901d0ab409dSJani Nikula intel_bios_encoder_supports_crt(const struct intel_bios_encoder_data *devdata)
1902d0ab409dSJani Nikula {
1903d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
1904d0ab409dSJani Nikula }
1905d0ab409dSJani Nikula 
190645c0673aSJani Nikula bool
1907d0ab409dSJani Nikula intel_bios_encoder_supports_dvi(const struct intel_bios_encoder_data *devdata)
1908d0ab409dSJani Nikula {
1909d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
1910d0ab409dSJani Nikula }
1911d0ab409dSJani Nikula 
191245c0673aSJani Nikula bool
1913d0ab409dSJani Nikula intel_bios_encoder_supports_hdmi(const struct intel_bios_encoder_data *devdata)
1914d0ab409dSJani Nikula {
1915d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dvi(devdata) &&
1916d0ab409dSJani Nikula 		(devdata->child.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
1917d0ab409dSJani Nikula }
1918d0ab409dSJani Nikula 
191945c0673aSJani Nikula bool
1920d0ab409dSJani Nikula intel_bios_encoder_supports_dp(const struct intel_bios_encoder_data *devdata)
1921d0ab409dSJani Nikula {
1922d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
1923d0ab409dSJani Nikula }
1924d0ab409dSJani Nikula 
1925d0ab409dSJani Nikula static bool
1926d0ab409dSJani Nikula intel_bios_encoder_supports_edp(const struct intel_bios_encoder_data *devdata)
1927d0ab409dSJani Nikula {
1928d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dp(devdata) &&
1929d0ab409dSJani Nikula 		devdata->child.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR;
1930d0ab409dSJani Nikula }
1931d0ab409dSJani Nikula 
1932a9a56e76SJani Nikula static int _intel_bios_hdmi_level_shift(const struct intel_bios_encoder_data *devdata)
1933a9a56e76SJani Nikula {
1934a9a56e76SJani Nikula 	if (!devdata || devdata->i915->vbt.version < 158)
1935a9a56e76SJani Nikula 		return -1;
1936a9a56e76SJani Nikula 
1937a9a56e76SJani Nikula 	return devdata->child.hdmi_level_shifter_value;
1938a9a56e76SJani Nikula }
1939a9a56e76SJani Nikula 
19406ba69981SJani Nikula static int _intel_bios_max_tmds_clock(const struct intel_bios_encoder_data *devdata)
19416ba69981SJani Nikula {
19426ba69981SJani Nikula 	if (!devdata || devdata->i915->vbt.version < 204)
19436ba69981SJani Nikula 		return 0;
19446ba69981SJani Nikula 
19456ba69981SJani Nikula 	switch (devdata->child.hdmi_max_data_rate) {
19466ba69981SJani Nikula 	default:
19476ba69981SJani Nikula 		MISSING_CASE(devdata->child.hdmi_max_data_rate);
19486ba69981SJani Nikula 		fallthrough;
19496ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_PLATFORM:
19506ba69981SJani Nikula 		return 0;
19516ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_297:
19526ba69981SJani Nikula 		return 297000;
19536ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_165:
19546ba69981SJani Nikula 		return 165000;
19556ba69981SJani Nikula 	}
19566ba69981SJani Nikula }
19576ba69981SJani Nikula 
19585a9d38b2SLucas De Marchi static bool is_port_valid(struct drm_i915_private *i915, enum port port)
19595a9d38b2SLucas De Marchi {
19605a9d38b2SLucas De Marchi 	/*
1961cad83b40SLucas De Marchi 	 * On some ICL SKUs port F is not present, but broken VBTs mark
19625a9d38b2SLucas De Marchi 	 * the port as present. Only try to initialize port F for the
19635a9d38b2SLucas De Marchi 	 * SKUs that may actually have it.
19645a9d38b2SLucas De Marchi 	 */
1965cad83b40SLucas De Marchi 	if (port == PORT_F && IS_ICELAKE(i915))
1966cad83b40SLucas De Marchi 		return IS_ICL_WITH_PORT_F(i915);
19675a9d38b2SLucas De Marchi 
19685a9d38b2SLucas De Marchi 	return true;
19695a9d38b2SLucas De Marchi }
19705a9d38b2SLucas De Marchi 
1971dbd440d8SJani Nikula static void parse_ddi_port(struct drm_i915_private *i915,
19723162d057SJani Nikula 			   struct intel_bios_encoder_data *devdata)
1973df0566a6SJani Nikula {
1974d1dad6f4SJani Nikula 	const struct child_device_config *child = &devdata->child;
1975f08fbe6aSJani Nikula 	bool is_dvi, is_hdmi, is_dp, is_edp, is_crt, supports_typec_usb, supports_tbt;
197672337aacSJani Nikula 	int dp_boost_level, dp_max_link_rate, hdmi_boost_level, hdmi_level_shift, max_tmds_clock;
1977df0566a6SJani Nikula 	enum port port;
1978df0566a6SJani Nikula 
1979dbd440d8SJani Nikula 	port = dvo_port_to_port(i915, child->dvo_port);
1980df0566a6SJani Nikula 	if (port == PORT_NONE)
1981df0566a6SJani Nikula 		return;
1982df0566a6SJani Nikula 
19835a9d38b2SLucas De Marchi 	if (!is_port_valid(i915, port)) {
19845a9d38b2SLucas De Marchi 		drm_dbg_kms(&i915->drm,
19855a9d38b2SLucas De Marchi 			    "VBT reports port %c as supported, but that can't be true: skipping\n",
19865a9d38b2SLucas De Marchi 			    port_name(port));
19875a9d38b2SLucas De Marchi 		return;
19885a9d38b2SLucas De Marchi 	}
19895a9d38b2SLucas De Marchi 
19905a449e58SJani Nikula 	if (i915->vbt.ports[port]) {
1991dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1992e92cbf38SWambui Karuga 			    "More than one child device for port %c in VBT, using the first.\n",
1993df0566a6SJani Nikula 			    port_name(port));
1994df0566a6SJani Nikula 		return;
1995df0566a6SJani Nikula 	}
1996df0566a6SJani Nikula 
1997d0ab409dSJani Nikula 	sanitize_device_type(devdata, port);
1998df0566a6SJani Nikula 
1999d0ab409dSJani Nikula 	is_dvi = intel_bios_encoder_supports_dvi(devdata);
2000d0ab409dSJani Nikula 	is_dp = intel_bios_encoder_supports_dp(devdata);
2001d0ab409dSJani Nikula 	is_crt = intel_bios_encoder_supports_crt(devdata);
2002d0ab409dSJani Nikula 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2003d0ab409dSJani Nikula 	is_edp = intel_bios_encoder_supports_edp(devdata);
2004df0566a6SJani Nikula 
2005f08fbe6aSJani Nikula 	supports_typec_usb = intel_bios_encoder_supports_typec_usb(devdata);
2006f08fbe6aSJani Nikula 	supports_tbt = intel_bios_encoder_supports_tbt(devdata);
2007df0566a6SJani Nikula 
2008dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
2009e92cbf38SWambui Karuga 		    "Port %c VBT info: CRT:%d DVI:%d HDMI:%d DP:%d eDP:%d LSPCON:%d USB-Type-C:%d TBT:%d DSC:%d\n",
2010df0566a6SJani Nikula 		    port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp,
2011dbd440d8SJani Nikula 		    HAS_LSPCON(i915) && child->lspcon,
2012f08fbe6aSJani Nikula 		    supports_typec_usb, supports_tbt,
20136e0d46e9SJani Nikula 		    devdata->dsc != NULL);
2014df0566a6SJani Nikula 
2015dab8477bSJani Nikula 	if (is_dvi)
2016dab8477bSJani Nikula 		sanitize_ddc_pin(devdata, port);
2017df0566a6SJani Nikula 
201811182986SJani Nikula 	if (is_dp)
201911182986SJani Nikula 		sanitize_aux_ch(devdata, port);
2020df0566a6SJani Nikula 
2021a9a56e76SJani Nikula 	hdmi_level_shift = _intel_bios_hdmi_level_shift(devdata);
2022a9a56e76SJani Nikula 	if (hdmi_level_shift >= 0) {
2023dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
20246ee8d381SJani Nikula 			    "Port %c VBT HDMI level shift: %d\n",
2025a9a56e76SJani Nikula 			    port_name(port), hdmi_level_shift);
2026df0566a6SJani Nikula 	}
2027df0566a6SJani Nikula 
20286ba69981SJani Nikula 	max_tmds_clock = _intel_bios_max_tmds_clock(devdata);
2029df0566a6SJani Nikula 	if (max_tmds_clock)
2030dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
20316ee8d381SJani Nikula 			    "Port %c VBT HDMI max TMDS clock: %d kHz\n",
2032df0566a6SJani Nikula 			    port_name(port), max_tmds_clock);
2033df0566a6SJani Nikula 
2034c0a950d1SJani Nikula 	/* I_boost config for SKL and above */
2035c0a950d1SJani Nikula 	dp_boost_level = intel_bios_encoder_dp_boost_level(devdata);
2036c0a950d1SJani Nikula 	if (dp_boost_level)
2037dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
20386ee8d381SJani Nikula 			    "Port %c VBT (e)DP boost level: %d\n",
2039c0a950d1SJani Nikula 			    port_name(port), dp_boost_level);
2040c0a950d1SJani Nikula 
2041c0a950d1SJani Nikula 	hdmi_boost_level = intel_bios_encoder_hdmi_boost_level(devdata);
2042c0a950d1SJani Nikula 	if (hdmi_boost_level)
2043dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
20446ee8d381SJani Nikula 			    "Port %c VBT HDMI boost level: %d\n",
2045c0a950d1SJani Nikula 			    port_name(port), hdmi_boost_level);
2046df0566a6SJani Nikula 
204772337aacSJani Nikula 	dp_max_link_rate = _intel_bios_dp_max_link_rate(devdata);
204872337aacSJani Nikula 	if (dp_max_link_rate)
2049dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
20506ee8d381SJani Nikula 			    "Port %c VBT DP max link rate: %d\n",
205172337aacSJani Nikula 			    port_name(port), dp_max_link_rate);
2052df0566a6SJani Nikula 
20535a449e58SJani Nikula 	i915->vbt.ports[port] = devdata;
2054df0566a6SJani Nikula }
2055df0566a6SJani Nikula 
2056b90b6e41SVille Syrjälä static bool has_ddi_port_info(struct drm_i915_private *i915)
2057b90b6e41SVille Syrjälä {
2058*eb9fcf63SVille Syrjälä 	return HAS_DDI(i915) || IS_CHERRYVIEW(i915);
2059b90b6e41SVille Syrjälä }
2060b90b6e41SVille Syrjälä 
2061ef0096e4SJani Nikula static void parse_ddi_ports(struct drm_i915_private *i915)
2062df0566a6SJani Nikula {
20633162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2064df0566a6SJani Nikula 
2065*eb9fcf63SVille Syrjälä 	if (!has_ddi_port_info(i915))
2066df0566a6SJani Nikula 		return;
2067df0566a6SJani Nikula 
2068ef0096e4SJani Nikula 	if (i915->vbt.version < 155)
2069df0566a6SJani Nikula 		return;
2070df0566a6SJani Nikula 
2071dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node)
2072ef0096e4SJani Nikula 		parse_ddi_port(i915, devdata);
2073df0566a6SJani Nikula }
2074df0566a6SJani Nikula 
2075df0566a6SJani Nikula static void
2076dbd440d8SJani Nikula parse_general_definitions(struct drm_i915_private *i915,
2077df0566a6SJani Nikula 			  const struct bdb_header *bdb)
2078df0566a6SJani Nikula {
2079df0566a6SJani Nikula 	const struct bdb_general_definitions *defs;
20803162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2081df0566a6SJani Nikula 	const struct child_device_config *child;
20820d9ef19bSJani Nikula 	int i, child_device_num;
2083df0566a6SJani Nikula 	u8 expected_size;
2084df0566a6SJani Nikula 	u16 block_size;
2085df0566a6SJani Nikula 	int bus_pin;
2086df0566a6SJani Nikula 
2087df0566a6SJani Nikula 	defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
2088df0566a6SJani Nikula 	if (!defs) {
2089dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2090e92cbf38SWambui Karuga 			    "No general definition block is found, no devices defined.\n");
2091df0566a6SJani Nikula 		return;
2092df0566a6SJani Nikula 	}
2093df0566a6SJani Nikula 
2094df0566a6SJani Nikula 	block_size = get_blocksize(defs);
2095df0566a6SJani Nikula 	if (block_size < sizeof(*defs)) {
2096dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2097e92cbf38SWambui Karuga 			    "General definitions block too small (%u)\n",
2098df0566a6SJani Nikula 			    block_size);
2099df0566a6SJani Nikula 		return;
2100df0566a6SJani Nikula 	}
2101df0566a6SJani Nikula 
2102df0566a6SJani Nikula 	bus_pin = defs->crt_ddc_gmbus_pin;
2103dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "crt_ddc_bus_pin: %d\n", bus_pin);
2104dbd440d8SJani Nikula 	if (intel_gmbus_is_valid_pin(i915, bus_pin))
2105dbd440d8SJani Nikula 		i915->vbt.crt_ddc_pin = bus_pin;
2106df0566a6SJani Nikula 
2107df0566a6SJani Nikula 	if (bdb->version < 106) {
2108df0566a6SJani Nikula 		expected_size = 22;
2109df0566a6SJani Nikula 	} else if (bdb->version < 111) {
2110df0566a6SJani Nikula 		expected_size = 27;
2111df0566a6SJani Nikula 	} else if (bdb->version < 195) {
2112df0566a6SJani Nikula 		expected_size = LEGACY_CHILD_DEVICE_CONFIG_SIZE;
2113df0566a6SJani Nikula 	} else if (bdb->version == 195) {
2114df0566a6SJani Nikula 		expected_size = 37;
2115df0566a6SJani Nikula 	} else if (bdb->version <= 215) {
2116df0566a6SJani Nikula 		expected_size = 38;
2117e4b3c3b3SJosé Roberto de Souza 	} else if (bdb->version <= 237) {
2118df0566a6SJani Nikula 		expected_size = 39;
2119df0566a6SJani Nikula 	} else {
2120df0566a6SJani Nikula 		expected_size = sizeof(*child);
2121df0566a6SJani Nikula 		BUILD_BUG_ON(sizeof(*child) < 39);
2122dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
2123e92cbf38SWambui Karuga 			"Expected child device config size for VBT version %u not known; assuming %u\n",
2124df0566a6SJani Nikula 			bdb->version, expected_size);
2125df0566a6SJani Nikula 	}
2126df0566a6SJani Nikula 
2127df0566a6SJani Nikula 	/* Flag an error for unexpected size, but continue anyway. */
2128df0566a6SJani Nikula 	if (defs->child_dev_size != expected_size)
2129dbd440d8SJani Nikula 		drm_err(&i915->drm,
2130e92cbf38SWambui Karuga 			"Unexpected child device config size %u (expected %u for VBT version %u)\n",
2131df0566a6SJani Nikula 			defs->child_dev_size, expected_size, bdb->version);
2132df0566a6SJani Nikula 
2133df0566a6SJani Nikula 	/* The legacy sized child device config is the minimum we need. */
2134df0566a6SJani Nikula 	if (defs->child_dev_size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
2135dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2136e92cbf38SWambui Karuga 			    "Child device config size %u is too small.\n",
2137df0566a6SJani Nikula 			    defs->child_dev_size);
2138df0566a6SJani Nikula 		return;
2139df0566a6SJani Nikula 	}
2140df0566a6SJani Nikula 
2141df0566a6SJani Nikula 	/* get the number of child device */
2142df0566a6SJani Nikula 	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
2143df0566a6SJani Nikula 
2144df0566a6SJani Nikula 	for (i = 0; i < child_device_num; i++) {
2145df0566a6SJani Nikula 		child = child_device_ptr(defs, i);
2146df0566a6SJani Nikula 		if (!child->device_type)
2147df0566a6SJani Nikula 			continue;
2148df0566a6SJani Nikula 
2149dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2150e92cbf38SWambui Karuga 			    "Found VBT child device with type 0x%x\n",
2151bdeb18dbSMatt Roper 			    child->device_type);
2152bdeb18dbSMatt Roper 
21530d9ef19bSJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
21540d9ef19bSJani Nikula 		if (!devdata)
21550d9ef19bSJani Nikula 			break;
21560d9ef19bSJani Nikula 
21577371fa34SJani Nikula 		devdata->i915 = i915;
21587371fa34SJani Nikula 
2159df0566a6SJani Nikula 		/*
2160df0566a6SJani Nikula 		 * Copy as much as we know (sizeof) and is available
21610d9ef19bSJani Nikula 		 * (child_dev_size) of the child device config. Accessing the
21620d9ef19bSJani Nikula 		 * data must depend on VBT version.
2163df0566a6SJani Nikula 		 */
21640d9ef19bSJani Nikula 		memcpy(&devdata->child, child,
2165df0566a6SJani Nikula 		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
21660d9ef19bSJani Nikula 
2167dbd440d8SJani Nikula 		list_add_tail(&devdata->node, &i915->vbt.display_devices);
2168df0566a6SJani Nikula 	}
21690d9ef19bSJani Nikula 
2170dbd440d8SJani Nikula 	if (list_empty(&i915->vbt.display_devices))
2171dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2172e92cbf38SWambui Karuga 			    "no child dev is parsed from VBT\n");
2173df0566a6SJani Nikula }
2174df0566a6SJani Nikula 
2175df0566a6SJani Nikula /* Common defaults which may be overridden by VBT. */
2176df0566a6SJani Nikula static void
2177dbd440d8SJani Nikula init_vbt_defaults(struct drm_i915_private *i915)
2178df0566a6SJani Nikula {
2179dbd440d8SJani Nikula 	i915->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
2180df0566a6SJani Nikula 
2181df0566a6SJani Nikula 	/* Default to having backlight */
2182dbd440d8SJani Nikula 	i915->vbt.backlight.present = true;
2183df0566a6SJani Nikula 
2184df0566a6SJani Nikula 	/* LFP panel data */
2185dbd440d8SJani Nikula 	i915->vbt.lvds_dither = 1;
2186df0566a6SJani Nikula 
2187df0566a6SJani Nikula 	/* SDVO panel data */
2188dbd440d8SJani Nikula 	i915->vbt.sdvo_lvds_vbt_mode = NULL;
2189df0566a6SJani Nikula 
2190df0566a6SJani Nikula 	/* general features */
2191dbd440d8SJani Nikula 	i915->vbt.int_tv_support = 1;
2192dbd440d8SJani Nikula 	i915->vbt.int_crt_support = 1;
2193df0566a6SJani Nikula 
2194df0566a6SJani Nikula 	/* driver features */
2195dbd440d8SJani Nikula 	i915->vbt.int_lvds_support = 1;
2196df0566a6SJani Nikula 
2197df0566a6SJani Nikula 	/* Default to using SSC */
2198dbd440d8SJani Nikula 	i915->vbt.lvds_use_ssc = 1;
2199df0566a6SJani Nikula 	/*
2200df0566a6SJani Nikula 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
2201df0566a6SJani Nikula 	 * clock for LVDS.
2202df0566a6SJani Nikula 	 */
2203dbd440d8SJani Nikula 	i915->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(i915,
2204dbd440d8SJani Nikula 							   !HAS_PCH_SPLIT(i915));
2205dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "Set default to SSC at %d kHz\n",
2206dbd440d8SJani Nikula 		    i915->vbt.lvds_ssc_freq);
2207df0566a6SJani Nikula }
2208df0566a6SJani Nikula 
2209df0566a6SJani Nikula /* Defaults to initialize only if there is no VBT. */
2210df0566a6SJani Nikula static void
2211dbd440d8SJani Nikula init_vbt_missing_defaults(struct drm_i915_private *i915)
2212df0566a6SJani Nikula {
2213df0566a6SJani Nikula 	enum port port;
22149b52aa72SRodrigo Vivi 	int ports = BIT(PORT_A) | BIT(PORT_B) | BIT(PORT_C) |
22159b52aa72SRodrigo Vivi 		    BIT(PORT_D) | BIT(PORT_E) | BIT(PORT_F);
2216df0566a6SJani Nikula 
2217e20e4037SJani Nikula 	if (!HAS_DDI(i915) && !IS_CHERRYVIEW(i915))
2218e20e4037SJani Nikula 		return;
2219e20e4037SJani Nikula 
22203ae04c0cSJani Nikula 	for_each_port_masked(port, ports) {
22213162d057SJani Nikula 		struct intel_bios_encoder_data *devdata;
222251f57481SJani Nikula 		struct child_device_config *child;
2223dbd440d8SJani Nikula 		enum phy phy = intel_port_to_phy(i915, port);
2224df0566a6SJani Nikula 
2225df0566a6SJani Nikula 		/*
2226df0566a6SJani Nikula 		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
2227df0566a6SJani Nikula 		 * to detect it.
2228df0566a6SJani Nikula 		 */
2229dbd440d8SJani Nikula 		if (intel_phy_is_tc(i915, phy))
2230df0566a6SJani Nikula 			continue;
2231df0566a6SJani Nikula 
223251f57481SJani Nikula 		/* Create fake child device config */
223351f57481SJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
223451f57481SJani Nikula 		if (!devdata)
223551f57481SJani Nikula 			break;
223651f57481SJani Nikula 
22377371fa34SJani Nikula 		devdata->i915 = i915;
223851f57481SJani Nikula 		child = &devdata->child;
223951f57481SJani Nikula 
224051f57481SJani Nikula 		if (port == PORT_F)
224151f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIF;
224251f57481SJani Nikula 		else if (port == PORT_E)
224351f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIE;
224451f57481SJani Nikula 		else
224551f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIA + port;
224651f57481SJani Nikula 
224751f57481SJani Nikula 		if (port != PORT_A && port != PORT_E)
224851f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_TMDS_DVI_SIGNALING;
224951f57481SJani Nikula 
225051f57481SJani Nikula 		if (port != PORT_E)
225151f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_DISPLAYPORT_OUTPUT;
225251f57481SJani Nikula 
225351f57481SJani Nikula 		if (port == PORT_A)
225451f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_INTERNAL_CONNECTOR;
225551f57481SJani Nikula 
225651f57481SJani Nikula 		list_add_tail(&devdata->node, &i915->vbt.display_devices);
225751f57481SJani Nikula 
225851f57481SJani Nikula 		drm_dbg_kms(&i915->drm,
225951f57481SJani Nikula 			    "Generating default VBT child device with type 0x04%x on port %c\n",
226051f57481SJani Nikula 			    child->device_type, port_name(port));
2261df0566a6SJani Nikula 	}
226251f57481SJani Nikula 
226351f57481SJani Nikula 	/* Bypass some minimum baseline VBT version checks */
226451f57481SJani Nikula 	i915->vbt.version = 155;
2265df0566a6SJani Nikula }
2266df0566a6SJani Nikula 
2267df0566a6SJani Nikula static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
2268df0566a6SJani Nikula {
2269df0566a6SJani Nikula 	const void *_vbt = vbt;
2270df0566a6SJani Nikula 
2271df0566a6SJani Nikula 	return _vbt + vbt->bdb_offset;
2272df0566a6SJani Nikula }
2273df0566a6SJani Nikula 
2274df0566a6SJani Nikula /**
2275df0566a6SJani Nikula  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
2276df0566a6SJani Nikula  * @buf:	pointer to a buffer to validate
2277df0566a6SJani Nikula  * @size:	size of the buffer
2278df0566a6SJani Nikula  *
2279df0566a6SJani Nikula  * Returns true on valid VBT.
2280df0566a6SJani Nikula  */
2281df0566a6SJani Nikula bool intel_bios_is_valid_vbt(const void *buf, size_t size)
2282df0566a6SJani Nikula {
2283df0566a6SJani Nikula 	const struct vbt_header *vbt = buf;
2284df0566a6SJani Nikula 	const struct bdb_header *bdb;
2285df0566a6SJani Nikula 
2286df0566a6SJani Nikula 	if (!vbt)
2287df0566a6SJani Nikula 		return false;
2288df0566a6SJani Nikula 
2289df0566a6SJani Nikula 	if (sizeof(struct vbt_header) > size) {
2290df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
2291df0566a6SJani Nikula 		return false;
2292df0566a6SJani Nikula 	}
2293df0566a6SJani Nikula 
2294df0566a6SJani Nikula 	if (memcmp(vbt->signature, "$VBT", 4)) {
2295df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("VBT invalid signature\n");
2296df0566a6SJani Nikula 		return false;
2297df0566a6SJani Nikula 	}
2298df0566a6SJani Nikula 
2299ff00ff96SLucas De Marchi 	if (vbt->vbt_size > size) {
2300ff00ff96SLucas De Marchi 		DRM_DEBUG_DRIVER("VBT incomplete (vbt_size overflows)\n");
2301ff00ff96SLucas De Marchi 		return false;
2302ff00ff96SLucas De Marchi 	}
2303ff00ff96SLucas De Marchi 
2304ff00ff96SLucas De Marchi 	size = vbt->vbt_size;
2305ff00ff96SLucas De Marchi 
2306df0566a6SJani Nikula 	if (range_overflows_t(size_t,
2307df0566a6SJani Nikula 			      vbt->bdb_offset,
2308df0566a6SJani Nikula 			      sizeof(struct bdb_header),
2309df0566a6SJani Nikula 			      size)) {
2310df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("BDB header incomplete\n");
2311df0566a6SJani Nikula 		return false;
2312df0566a6SJani Nikula 	}
2313df0566a6SJani Nikula 
2314df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
2315df0566a6SJani Nikula 	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
2316df0566a6SJani Nikula 		DRM_DEBUG_DRIVER("BDB incomplete\n");
2317df0566a6SJani Nikula 		return false;
2318df0566a6SJani Nikula 	}
2319df0566a6SJani Nikula 
2320df0566a6SJani Nikula 	return vbt;
2321df0566a6SJani Nikula }
2322df0566a6SJani Nikula 
2323a36e7dc0SClint Taylor static struct vbt_header *spi_oprom_get_vbt(struct drm_i915_private *i915)
2324a36e7dc0SClint Taylor {
2325a36e7dc0SClint Taylor 	u32 count, data, found, store = 0;
2326a36e7dc0SClint Taylor 	u32 static_region, oprom_offset;
2327a36e7dc0SClint Taylor 	u32 oprom_size = 0x200000;
2328a36e7dc0SClint Taylor 	u16 vbt_size;
2329a36e7dc0SClint Taylor 	u32 *vbt;
2330a36e7dc0SClint Taylor 
2331a36e7dc0SClint Taylor 	static_region = intel_uncore_read(&i915->uncore, SPI_STATIC_REGIONS);
2332a36e7dc0SClint Taylor 	static_region &= OPTIONROM_SPI_REGIONID_MASK;
2333a36e7dc0SClint Taylor 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_REGIONID, static_region);
2334a36e7dc0SClint Taylor 
2335a36e7dc0SClint Taylor 	oprom_offset = intel_uncore_read(&i915->uncore, OROM_OFFSET);
2336a36e7dc0SClint Taylor 	oprom_offset &= OROM_OFFSET_MASK;
2337a36e7dc0SClint Taylor 
2338a36e7dc0SClint Taylor 	for (count = 0; count < oprom_size; count += 4) {
2339a36e7dc0SClint Taylor 		intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, oprom_offset + count);
2340a36e7dc0SClint Taylor 		data = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
2341a36e7dc0SClint Taylor 
2342a36e7dc0SClint Taylor 		if (data == *((const u32 *)"$VBT")) {
2343a36e7dc0SClint Taylor 			found = oprom_offset + count;
2344a36e7dc0SClint Taylor 			break;
2345a36e7dc0SClint Taylor 		}
2346a36e7dc0SClint Taylor 	}
2347a36e7dc0SClint Taylor 
2348a36e7dc0SClint Taylor 	if (count >= oprom_size)
2349a36e7dc0SClint Taylor 		goto err_not_found;
2350a36e7dc0SClint Taylor 
2351a36e7dc0SClint Taylor 	/* Get VBT size and allocate space for the VBT */
2352a36e7dc0SClint Taylor 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, found +
2353a36e7dc0SClint Taylor 		   offsetof(struct vbt_header, vbt_size));
2354a36e7dc0SClint Taylor 	vbt_size = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
2355a36e7dc0SClint Taylor 	vbt_size &= 0xffff;
2356a36e7dc0SClint Taylor 
2357980f42e7SJani Nikula 	vbt = kzalloc(round_up(vbt_size, 4), GFP_KERNEL);
2358a36e7dc0SClint Taylor 	if (!vbt)
2359a36e7dc0SClint Taylor 		goto err_not_found;
2360a36e7dc0SClint Taylor 
2361a36e7dc0SClint Taylor 	for (count = 0; count < vbt_size; count += 4) {
2362a36e7dc0SClint Taylor 		intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, found + count);
2363a36e7dc0SClint Taylor 		data = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
2364a36e7dc0SClint Taylor 		*(vbt + store++) = data;
2365a36e7dc0SClint Taylor 	}
2366a36e7dc0SClint Taylor 
2367a36e7dc0SClint Taylor 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
2368a36e7dc0SClint Taylor 		goto err_free_vbt;
2369a36e7dc0SClint Taylor 
2370a36e7dc0SClint Taylor 	drm_dbg_kms(&i915->drm, "Found valid VBT in SPI flash\n");
2371a36e7dc0SClint Taylor 
2372a36e7dc0SClint Taylor 	return (struct vbt_header *)vbt;
2373a36e7dc0SClint Taylor 
2374a36e7dc0SClint Taylor err_free_vbt:
2375a36e7dc0SClint Taylor 	kfree(vbt);
2376a36e7dc0SClint Taylor err_not_found:
2377a36e7dc0SClint Taylor 	return NULL;
2378a36e7dc0SClint Taylor }
2379a36e7dc0SClint Taylor 
2380dbd440d8SJani Nikula static struct vbt_header *oprom_get_vbt(struct drm_i915_private *i915)
2381df0566a6SJani Nikula {
2382dbd440d8SJani Nikula 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
23832cded152SLucas De Marchi 	void __iomem *p = NULL, *oprom;
2384fd0186ceSLucas De Marchi 	struct vbt_header *vbt;
2385fd0186ceSLucas De Marchi 	u16 vbt_size;
23862cded152SLucas De Marchi 	size_t i, size;
23872cded152SLucas De Marchi 
23882cded152SLucas De Marchi 	oprom = pci_map_rom(pdev, &size);
23892cded152SLucas De Marchi 	if (!oprom)
23902cded152SLucas De Marchi 		return NULL;
2391df0566a6SJani Nikula 
2392df0566a6SJani Nikula 	/* Scour memory looking for the VBT signature. */
239398cf5c9aSLucas De Marchi 	for (i = 0; i + 4 < size; i += 4) {
2394496f50a6SLucas De Marchi 		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
2395df0566a6SJani Nikula 			continue;
2396df0566a6SJani Nikula 
2397fd0186ceSLucas De Marchi 		p = oprom + i;
2398fd0186ceSLucas De Marchi 		size -= i;
2399df0566a6SJani Nikula 		break;
2400df0566a6SJani Nikula 	}
2401df0566a6SJani Nikula 
2402fd0186ceSLucas De Marchi 	if (!p)
24032cded152SLucas De Marchi 		goto err_unmap_oprom;
2404fd0186ceSLucas De Marchi 
2405fd0186ceSLucas De Marchi 	if (sizeof(struct vbt_header) > size) {
2406dbd440d8SJani Nikula 		drm_dbg(&i915->drm, "VBT header incomplete\n");
24072cded152SLucas De Marchi 		goto err_unmap_oprom;
2408fd0186ceSLucas De Marchi 	}
2409fd0186ceSLucas De Marchi 
2410fd0186ceSLucas De Marchi 	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
2411fd0186ceSLucas De Marchi 	if (vbt_size > size) {
2412dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
2413e92cbf38SWambui Karuga 			"VBT incomplete (vbt_size overflows)\n");
24142cded152SLucas De Marchi 		goto err_unmap_oprom;
2415fd0186ceSLucas De Marchi 	}
2416fd0186ceSLucas De Marchi 
2417fd0186ceSLucas De Marchi 	/* The rest will be validated by intel_bios_is_valid_vbt() */
2418fd0186ceSLucas De Marchi 	vbt = kmalloc(vbt_size, GFP_KERNEL);
2419fd0186ceSLucas De Marchi 	if (!vbt)
24202cded152SLucas De Marchi 		goto err_unmap_oprom;
2421fd0186ceSLucas De Marchi 
2422fd0186ceSLucas De Marchi 	memcpy_fromio(vbt, p, vbt_size);
2423fd0186ceSLucas De Marchi 
2424fd0186ceSLucas De Marchi 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
2425fd0186ceSLucas De Marchi 		goto err_free_vbt;
2426fd0186ceSLucas De Marchi 
24272cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
24282cded152SLucas De Marchi 
2429a36e7dc0SClint Taylor 	drm_dbg_kms(&i915->drm, "Found valid VBT in PCI ROM\n");
2430a36e7dc0SClint Taylor 
2431fd0186ceSLucas De Marchi 	return vbt;
2432fd0186ceSLucas De Marchi 
2433fd0186ceSLucas De Marchi err_free_vbt:
2434fd0186ceSLucas De Marchi 	kfree(vbt);
24352cded152SLucas De Marchi err_unmap_oprom:
24362cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
2437fd0186ceSLucas De Marchi 
2438df0566a6SJani Nikula 	return NULL;
2439df0566a6SJani Nikula }
2440df0566a6SJani Nikula 
2441df0566a6SJani Nikula /**
2442df0566a6SJani Nikula  * intel_bios_init - find VBT and initialize settings from the BIOS
2443dbd440d8SJani Nikula  * @i915: i915 device instance
2444df0566a6SJani Nikula  *
2445df0566a6SJani Nikula  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
2446df0566a6SJani Nikula  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
2447df0566a6SJani Nikula  * initialize some defaults if the VBT is not present at all.
2448df0566a6SJani Nikula  */
2449dbd440d8SJani Nikula void intel_bios_init(struct drm_i915_private *i915)
2450df0566a6SJani Nikula {
2451dbd440d8SJani Nikula 	const struct vbt_header *vbt = i915->opregion.vbt;
24522cded152SLucas De Marchi 	struct vbt_header *oprom_vbt = NULL;
2453df0566a6SJani Nikula 	const struct bdb_header *bdb;
2454df0566a6SJani Nikula 
2455dbd440d8SJani Nikula 	INIT_LIST_HEAD(&i915->vbt.display_devices);
24560d9ef19bSJani Nikula 
2457dbd440d8SJani Nikula 	if (!HAS_DISPLAY(i915)) {
2458dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2459e92cbf38SWambui Karuga 			    "Skipping VBT init due to disabled display.\n");
2460df0566a6SJani Nikula 		return;
2461df0566a6SJani Nikula 	}
2462df0566a6SJani Nikula 
2463dbd440d8SJani Nikula 	init_vbt_defaults(i915);
2464df0566a6SJani Nikula 
2465a36e7dc0SClint Taylor 	/*
2466a36e7dc0SClint Taylor 	 * If the OpRegion does not have VBT, look in SPI flash through MMIO or
2467a36e7dc0SClint Taylor 	 * PCI mapping
2468a36e7dc0SClint Taylor 	 */
2469a36e7dc0SClint Taylor 	if (!vbt && IS_DGFX(i915)) {
2470a36e7dc0SClint Taylor 		oprom_vbt = spi_oprom_get_vbt(i915);
2471a36e7dc0SClint Taylor 		vbt = oprom_vbt;
2472a36e7dc0SClint Taylor 	}
2473a36e7dc0SClint Taylor 
2474df0566a6SJani Nikula 	if (!vbt) {
2475dbd440d8SJani Nikula 		oprom_vbt = oprom_get_vbt(i915);
24762cded152SLucas De Marchi 		vbt = oprom_vbt;
2477df0566a6SJani Nikula 	}
2478df0566a6SJani Nikula 
2479a36e7dc0SClint Taylor 	if (!vbt)
2480a36e7dc0SClint Taylor 		goto out;
2481a36e7dc0SClint Taylor 
2482df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
2483ef0096e4SJani Nikula 	i915->vbt.version = bdb->version;
2484df0566a6SJani Nikula 
2485dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
2486e92cbf38SWambui Karuga 		    "VBT signature \"%.*s\", BDB version %d\n",
2487df0566a6SJani Nikula 		    (int)sizeof(vbt->signature), vbt->signature, bdb->version);
2488df0566a6SJani Nikula 
2489df0566a6SJani Nikula 	/* Grab useful general definitions */
2490dbd440d8SJani Nikula 	parse_general_features(i915, bdb);
2491dbd440d8SJani Nikula 	parse_general_definitions(i915, bdb);
2492dbd440d8SJani Nikula 	parse_panel_options(i915, bdb);
2493dbd440d8SJani Nikula 	parse_panel_dtd(i915, bdb);
2494dbd440d8SJani Nikula 	parse_lfp_backlight(i915, bdb);
2495dbd440d8SJani Nikula 	parse_sdvo_panel_data(i915, bdb);
2496dbd440d8SJani Nikula 	parse_driver_features(i915, bdb);
2497dbd440d8SJani Nikula 	parse_power_conservation_features(i915, bdb);
2498dbd440d8SJani Nikula 	parse_edp(i915, bdb);
2499dbd440d8SJani Nikula 	parse_psr(i915, bdb);
2500dbd440d8SJani Nikula 	parse_mipi_config(i915, bdb);
2501dbd440d8SJani Nikula 	parse_mipi_sequence(i915, bdb);
2502df0566a6SJani Nikula 
25036e0d46e9SJani Nikula 	/* Depends on child device list */
2504dbd440d8SJani Nikula 	parse_compression_parameters(i915, bdb);
25056e0d46e9SJani Nikula 
2506df0566a6SJani Nikula out:
2507df0566a6SJani Nikula 	if (!vbt) {
2508dbd440d8SJani Nikula 		drm_info(&i915->drm,
2509e92cbf38SWambui Karuga 			 "Failed to find VBIOS tables (VBT)\n");
2510dbd440d8SJani Nikula 		init_vbt_missing_defaults(i915);
2511df0566a6SJani Nikula 	}
2512df0566a6SJani Nikula 
251351f57481SJani Nikula 	/* Further processing on pre-parsed or generated child device data */
251451f57481SJani Nikula 	parse_sdvo_device_mapping(i915);
251551f57481SJani Nikula 	parse_ddi_ports(i915);
251651f57481SJani Nikula 
25172cded152SLucas De Marchi 	kfree(oprom_vbt);
2518df0566a6SJani Nikula }
2519df0566a6SJani Nikula 
2520df0566a6SJani Nikula /**
252178dae1acSJanusz Krzysztofik  * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
2522dbd440d8SJani Nikula  * @i915: i915 device instance
2523df0566a6SJani Nikula  */
2524dbd440d8SJani Nikula void intel_bios_driver_remove(struct drm_i915_private *i915)
2525df0566a6SJani Nikula {
25263162d057SJani Nikula 	struct intel_bios_encoder_data *devdata, *n;
25270d9ef19bSJani Nikula 
2528dbd440d8SJani Nikula 	list_for_each_entry_safe(devdata, n, &i915->vbt.display_devices, node) {
25290d9ef19bSJani Nikula 		list_del(&devdata->node);
25306e0d46e9SJani Nikula 		kfree(devdata->dsc);
25310d9ef19bSJani Nikula 		kfree(devdata);
25320d9ef19bSJani Nikula 	}
25330d9ef19bSJani Nikula 
2534dbd440d8SJani Nikula 	kfree(i915->vbt.sdvo_lvds_vbt_mode);
2535dbd440d8SJani Nikula 	i915->vbt.sdvo_lvds_vbt_mode = NULL;
2536dbd440d8SJani Nikula 	kfree(i915->vbt.lfp_lvds_vbt_mode);
2537dbd440d8SJani Nikula 	i915->vbt.lfp_lvds_vbt_mode = NULL;
2538dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.data);
2539dbd440d8SJani Nikula 	i915->vbt.dsi.data = NULL;
2540dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.pps);
2541dbd440d8SJani Nikula 	i915->vbt.dsi.pps = NULL;
2542dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.config);
2543dbd440d8SJani Nikula 	i915->vbt.dsi.config = NULL;
2544dbd440d8SJani Nikula 	kfree(i915->vbt.dsi.deassert_seq);
2545dbd440d8SJani Nikula 	i915->vbt.dsi.deassert_seq = NULL;
2546df0566a6SJani Nikula }
2547df0566a6SJani Nikula 
2548df0566a6SJani Nikula /**
2549df0566a6SJani Nikula  * intel_bios_is_tv_present - is integrated TV present in VBT
2550dbd440d8SJani Nikula  * @i915: i915 device instance
2551df0566a6SJani Nikula  *
2552df0566a6SJani Nikula  * Return true if TV is present. If no child devices were parsed from VBT,
2553df0566a6SJani Nikula  * assume TV is present.
2554df0566a6SJani Nikula  */
2555dbd440d8SJani Nikula bool intel_bios_is_tv_present(struct drm_i915_private *i915)
2556df0566a6SJani Nikula {
25573162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
2558df0566a6SJani Nikula 	const struct child_device_config *child;
2559df0566a6SJani Nikula 
2560dbd440d8SJani Nikula 	if (!i915->vbt.int_tv_support)
2561df0566a6SJani Nikula 		return false;
2562df0566a6SJani Nikula 
2563dbd440d8SJani Nikula 	if (list_empty(&i915->vbt.display_devices))
2564df0566a6SJani Nikula 		return true;
2565df0566a6SJani Nikula 
2566dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
25670d9ef19bSJani Nikula 		child = &devdata->child;
25680d9ef19bSJani Nikula 
2569df0566a6SJani Nikula 		/*
2570df0566a6SJani Nikula 		 * If the device type is not TV, continue.
2571df0566a6SJani Nikula 		 */
2572df0566a6SJani Nikula 		switch (child->device_type) {
2573df0566a6SJani Nikula 		case DEVICE_TYPE_INT_TV:
2574df0566a6SJani Nikula 		case DEVICE_TYPE_TV:
2575df0566a6SJani Nikula 		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
2576df0566a6SJani Nikula 			break;
2577df0566a6SJani Nikula 		default:
2578df0566a6SJani Nikula 			continue;
2579df0566a6SJani Nikula 		}
2580df0566a6SJani Nikula 		/* Only when the addin_offset is non-zero, it is regarded
2581df0566a6SJani Nikula 		 * as present.
2582df0566a6SJani Nikula 		 */
2583df0566a6SJani Nikula 		if (child->addin_offset)
2584df0566a6SJani Nikula 			return true;
2585df0566a6SJani Nikula 	}
2586df0566a6SJani Nikula 
2587df0566a6SJani Nikula 	return false;
2588df0566a6SJani Nikula }
2589df0566a6SJani Nikula 
2590df0566a6SJani Nikula /**
2591df0566a6SJani Nikula  * intel_bios_is_lvds_present - is LVDS present in VBT
2592dbd440d8SJani Nikula  * @i915:	i915 device instance
2593df0566a6SJani Nikula  * @i2c_pin:	i2c pin for LVDS if present
2594df0566a6SJani Nikula  *
2595df0566a6SJani Nikula  * Return true if LVDS is present. If no child devices were parsed from VBT,
2596df0566a6SJani Nikula  * assume LVDS is present.
2597df0566a6SJani Nikula  */
2598dbd440d8SJani Nikula bool intel_bios_is_lvds_present(struct drm_i915_private *i915, u8 *i2c_pin)
2599df0566a6SJani Nikula {
26003162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
2601df0566a6SJani Nikula 	const struct child_device_config *child;
2602df0566a6SJani Nikula 
2603dbd440d8SJani Nikula 	if (list_empty(&i915->vbt.display_devices))
2604df0566a6SJani Nikula 		return true;
2605df0566a6SJani Nikula 
2606dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
26070d9ef19bSJani Nikula 		child = &devdata->child;
2608df0566a6SJani Nikula 
2609df0566a6SJani Nikula 		/* If the device type is not LFP, continue.
2610df0566a6SJani Nikula 		 * We have to check both the new identifiers as well as the
2611df0566a6SJani Nikula 		 * old for compatibility with some BIOSes.
2612df0566a6SJani Nikula 		 */
2613df0566a6SJani Nikula 		if (child->device_type != DEVICE_TYPE_INT_LFP &&
2614df0566a6SJani Nikula 		    child->device_type != DEVICE_TYPE_LFP)
2615df0566a6SJani Nikula 			continue;
2616df0566a6SJani Nikula 
2617dbd440d8SJani Nikula 		if (intel_gmbus_is_valid_pin(i915, child->i2c_pin))
2618df0566a6SJani Nikula 			*i2c_pin = child->i2c_pin;
2619df0566a6SJani Nikula 
2620df0566a6SJani Nikula 		/* However, we cannot trust the BIOS writers to populate
2621df0566a6SJani Nikula 		 * the VBT correctly.  Since LVDS requires additional
2622df0566a6SJani Nikula 		 * information from AIM blocks, a non-zero addin offset is
2623df0566a6SJani Nikula 		 * a good indicator that the LVDS is actually present.
2624df0566a6SJani Nikula 		 */
2625df0566a6SJani Nikula 		if (child->addin_offset)
2626df0566a6SJani Nikula 			return true;
2627df0566a6SJani Nikula 
2628df0566a6SJani Nikula 		/* But even then some BIOS writers perform some black magic
2629df0566a6SJani Nikula 		 * and instantiate the device without reference to any
2630df0566a6SJani Nikula 		 * additional data.  Trust that if the VBT was written into
2631df0566a6SJani Nikula 		 * the OpRegion then they have validated the LVDS's existence.
2632df0566a6SJani Nikula 		 */
2633dbd440d8SJani Nikula 		if (i915->opregion.vbt)
2634df0566a6SJani Nikula 			return true;
2635df0566a6SJani Nikula 	}
2636df0566a6SJani Nikula 
2637df0566a6SJani Nikula 	return false;
2638df0566a6SJani Nikula }
2639df0566a6SJani Nikula 
2640df0566a6SJani Nikula /**
2641df0566a6SJani Nikula  * intel_bios_is_port_present - is the specified digital port present
2642dbd440d8SJani Nikula  * @i915:	i915 device instance
2643df0566a6SJani Nikula  * @port:	port to check
2644df0566a6SJani Nikula  *
2645df0566a6SJani Nikula  * Return true if the device in %port is present.
2646df0566a6SJani Nikula  */
2647dbd440d8SJani Nikula bool intel_bios_is_port_present(struct drm_i915_private *i915, enum port port)
2648df0566a6SJani Nikula {
26493162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
2650df0566a6SJani Nikula 	const struct child_device_config *child;
2651df0566a6SJani Nikula 	static const struct {
2652df0566a6SJani Nikula 		u16 dp, hdmi;
2653df0566a6SJani Nikula 	} port_mapping[] = {
2654df0566a6SJani Nikula 		[PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
2655df0566a6SJani Nikula 		[PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
2656df0566a6SJani Nikula 		[PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
2657df0566a6SJani Nikula 		[PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
2658df0566a6SJani Nikula 		[PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
2659df0566a6SJani Nikula 	};
2660df0566a6SJani Nikula 
2661b90b6e41SVille Syrjälä 	if (has_ddi_port_info(i915))
26625a449e58SJani Nikula 		return i915->vbt.ports[port];
2663df0566a6SJani Nikula 
2664df0566a6SJani Nikula 	/* FIXME maybe deal with port A as well? */
2665dbd440d8SJani Nikula 	if (drm_WARN_ON(&i915->drm,
2666f4224a4cSPankaj Bharadiya 			port == PORT_A) || port >= ARRAY_SIZE(port_mapping))
2667df0566a6SJani Nikula 		return false;
2668df0566a6SJani Nikula 
2669dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
26700d9ef19bSJani Nikula 		child = &devdata->child;
2671df0566a6SJani Nikula 
2672df0566a6SJani Nikula 		if ((child->dvo_port == port_mapping[port].dp ||
2673df0566a6SJani Nikula 		     child->dvo_port == port_mapping[port].hdmi) &&
2674df0566a6SJani Nikula 		    (child->device_type & (DEVICE_TYPE_TMDS_DVI_SIGNALING |
2675df0566a6SJani Nikula 					   DEVICE_TYPE_DISPLAYPORT_OUTPUT)))
2676df0566a6SJani Nikula 			return true;
2677df0566a6SJani Nikula 	}
2678df0566a6SJani Nikula 
2679df0566a6SJani Nikula 	return false;
2680df0566a6SJani Nikula }
2681df0566a6SJani Nikula 
2682df0566a6SJani Nikula /**
2683df0566a6SJani Nikula  * intel_bios_is_port_edp - is the device in given port eDP
2684dbd440d8SJani Nikula  * @i915:	i915 device instance
2685df0566a6SJani Nikula  * @port:	port to check
2686df0566a6SJani Nikula  *
2687df0566a6SJani Nikula  * Return true if the device in %port is eDP.
2688df0566a6SJani Nikula  */
2689dbd440d8SJani Nikula bool intel_bios_is_port_edp(struct drm_i915_private *i915, enum port port)
2690df0566a6SJani Nikula {
26913162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
2692df0566a6SJani Nikula 	const struct child_device_config *child;
2693df0566a6SJani Nikula 	static const short port_mapping[] = {
2694df0566a6SJani Nikula 		[PORT_B] = DVO_PORT_DPB,
2695df0566a6SJani Nikula 		[PORT_C] = DVO_PORT_DPC,
2696df0566a6SJani Nikula 		[PORT_D] = DVO_PORT_DPD,
2697df0566a6SJani Nikula 		[PORT_E] = DVO_PORT_DPE,
2698df0566a6SJani Nikula 		[PORT_F] = DVO_PORT_DPF,
2699df0566a6SJani Nikula 	};
2700df0566a6SJani Nikula 
2701b90b6e41SVille Syrjälä 	if (has_ddi_port_info(i915)) {
270245c0673aSJani Nikula 		const struct intel_bios_encoder_data *devdata;
270345c0673aSJani Nikula 
270445c0673aSJani Nikula 		devdata = intel_bios_encoder_data_lookup(i915, port);
270545c0673aSJani Nikula 
270645c0673aSJani Nikula 		return devdata && intel_bios_encoder_supports_edp(devdata);
270745c0673aSJani Nikula 	}
2708df0566a6SJani Nikula 
2709dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
27100d9ef19bSJani Nikula 		child = &devdata->child;
2711df0566a6SJani Nikula 
2712df0566a6SJani Nikula 		if (child->dvo_port == port_mapping[port] &&
2713df0566a6SJani Nikula 		    (child->device_type & DEVICE_TYPE_eDP_BITS) ==
2714df0566a6SJani Nikula 		    (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
2715df0566a6SJani Nikula 			return true;
2716df0566a6SJani Nikula 	}
2717df0566a6SJani Nikula 
2718df0566a6SJani Nikula 	return false;
2719df0566a6SJani Nikula }
2720df0566a6SJani Nikula 
272132c2bc89SVille Syrjälä static bool child_dev_is_dp_dual_mode(const struct child_device_config *child)
272232c2bc89SVille Syrjälä {
272332c2bc89SVille Syrjälä 	if ((child->device_type & DEVICE_TYPE_DP_DUAL_MODE_BITS) !=
272432c2bc89SVille Syrjälä 	    (DEVICE_TYPE_DP_DUAL_MODE & DEVICE_TYPE_DP_DUAL_MODE_BITS))
272532c2bc89SVille Syrjälä 		return false;
272632c2bc89SVille Syrjälä 
272732c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_DPA)
272832c2bc89SVille Syrjälä 		return true;
272932c2bc89SVille Syrjälä 
273032c2bc89SVille Syrjälä 	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
273132c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_HDMIA &&
273232c2bc89SVille Syrjälä 	    child->aux_channel != 0)
273332c2bc89SVille Syrjälä 		return true;
273432c2bc89SVille Syrjälä 
273532c2bc89SVille Syrjälä 	return false;
273632c2bc89SVille Syrjälä }
273732c2bc89SVille Syrjälä 
273832c2bc89SVille Syrjälä bool intel_bios_is_port_dp_dual_mode(struct drm_i915_private *i915,
2739df0566a6SJani Nikula 				     enum port port)
2740df0566a6SJani Nikula {
2741df0566a6SJani Nikula 	static const struct {
2742df0566a6SJani Nikula 		u16 dp, hdmi;
2743df0566a6SJani Nikula 	} port_mapping[] = {
2744df0566a6SJani Nikula 		/*
2745df0566a6SJani Nikula 		 * Buggy VBTs may declare DP ports as having
2746df0566a6SJani Nikula 		 * HDMI type dvo_port :( So let's check both.
2747df0566a6SJani Nikula 		 */
2748df0566a6SJani Nikula 		[PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
2749df0566a6SJani Nikula 		[PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
2750df0566a6SJani Nikula 		[PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
2751df0566a6SJani Nikula 		[PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
2752df0566a6SJani Nikula 		[PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
2753df0566a6SJani Nikula 	};
275432c2bc89SVille Syrjälä 	const struct intel_bios_encoder_data *devdata;
275532c2bc89SVille Syrjälä 
2756b90b6e41SVille Syrjälä 	if (has_ddi_port_info(i915)) {
275732c2bc89SVille Syrjälä 		const struct intel_bios_encoder_data *devdata;
275832c2bc89SVille Syrjälä 
275932c2bc89SVille Syrjälä 		devdata = intel_bios_encoder_data_lookup(i915, port);
276032c2bc89SVille Syrjälä 
276132c2bc89SVille Syrjälä 		return devdata && child_dev_is_dp_dual_mode(&devdata->child);
276232c2bc89SVille Syrjälä 	}
2763df0566a6SJani Nikula 
2764df0566a6SJani Nikula 	if (port == PORT_A || port >= ARRAY_SIZE(port_mapping))
2765df0566a6SJani Nikula 		return false;
2766df0566a6SJani Nikula 
2767dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
276832c2bc89SVille Syrjälä 		if ((devdata->child.dvo_port == port_mapping[port].dp ||
276932c2bc89SVille Syrjälä 		     devdata->child.dvo_port == port_mapping[port].hdmi) &&
277032c2bc89SVille Syrjälä 		    child_dev_is_dp_dual_mode(&devdata->child))
2771df0566a6SJani Nikula 			return true;
2772df0566a6SJani Nikula 	}
2773df0566a6SJani Nikula 
2774df0566a6SJani Nikula 	return false;
2775df0566a6SJani Nikula }
2776df0566a6SJani Nikula 
2777df0566a6SJani Nikula /**
2778df0566a6SJani Nikula  * intel_bios_is_dsi_present - is DSI present in VBT
2779dbd440d8SJani Nikula  * @i915:	i915 device instance
2780df0566a6SJani Nikula  * @port:	port for DSI if present
2781df0566a6SJani Nikula  *
2782df0566a6SJani Nikula  * Return true if DSI is present, and return the port in %port.
2783df0566a6SJani Nikula  */
2784dbd440d8SJani Nikula bool intel_bios_is_dsi_present(struct drm_i915_private *i915,
2785df0566a6SJani Nikula 			       enum port *port)
2786df0566a6SJani Nikula {
27873162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
2788df0566a6SJani Nikula 	const struct child_device_config *child;
2789df0566a6SJani Nikula 	u8 dvo_port;
2790df0566a6SJani Nikula 
2791dbd440d8SJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
27920d9ef19bSJani Nikula 		child = &devdata->child;
2793df0566a6SJani Nikula 
2794df0566a6SJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
2795df0566a6SJani Nikula 			continue;
2796df0566a6SJani Nikula 
2797df0566a6SJani Nikula 		dvo_port = child->dvo_port;
2798df0566a6SJani Nikula 
2799df0566a6SJani Nikula 		if (dvo_port == DVO_PORT_MIPIA ||
2800005e9537SMatt Roper 		    (dvo_port == DVO_PORT_MIPIB && DISPLAY_VER(i915) >= 11) ||
2801005e9537SMatt Roper 		    (dvo_port == DVO_PORT_MIPIC && DISPLAY_VER(i915) < 11)) {
2802df0566a6SJani Nikula 			if (port)
2803df0566a6SJani Nikula 				*port = dvo_port - DVO_PORT_MIPIA;
2804df0566a6SJani Nikula 			return true;
2805df0566a6SJani Nikula 		} else if (dvo_port == DVO_PORT_MIPIB ||
2806df0566a6SJani Nikula 			   dvo_port == DVO_PORT_MIPIC ||
2807df0566a6SJani Nikula 			   dvo_port == DVO_PORT_MIPID) {
2808dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
2809e92cbf38SWambui Karuga 				    "VBT has unsupported DSI port %c\n",
2810df0566a6SJani Nikula 				    port_name(dvo_port - DVO_PORT_MIPIA));
2811df0566a6SJani Nikula 		}
2812df0566a6SJani Nikula 	}
2813df0566a6SJani Nikula 
2814df0566a6SJani Nikula 	return false;
2815df0566a6SJani Nikula }
2816df0566a6SJani Nikula 
28171bf2f3bfSJani Nikula static void fill_dsc(struct intel_crtc_state *crtc_state,
28181bf2f3bfSJani Nikula 		     struct dsc_compression_parameters_entry *dsc,
28191bf2f3bfSJani Nikula 		     int dsc_max_bpc)
28201bf2f3bfSJani Nikula {
28211bf2f3bfSJani Nikula 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
28221bf2f3bfSJani Nikula 	int bpc = 8;
28231bf2f3bfSJani Nikula 
28241bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_major = dsc->version_major;
28251bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_minor = dsc->version_minor;
28261bf2f3bfSJani Nikula 
28271bf2f3bfSJani Nikula 	if (dsc->support_12bpc && dsc_max_bpc >= 12)
28281bf2f3bfSJani Nikula 		bpc = 12;
28291bf2f3bfSJani Nikula 	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
28301bf2f3bfSJani Nikula 		bpc = 10;
28311bf2f3bfSJani Nikula 	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
28321bf2f3bfSJani Nikula 		bpc = 8;
28331bf2f3bfSJani Nikula 	else
28341bf2f3bfSJani Nikula 		DRM_DEBUG_KMS("VBT: Unsupported BPC %d for DCS\n",
28351bf2f3bfSJani Nikula 			      dsc_max_bpc);
28361bf2f3bfSJani Nikula 
28371bf2f3bfSJani Nikula 	crtc_state->pipe_bpp = bpc * 3;
28381bf2f3bfSJani Nikula 
28391bf2f3bfSJani Nikula 	crtc_state->dsc.compressed_bpp = min(crtc_state->pipe_bpp,
28401bf2f3bfSJani Nikula 					     VBT_DSC_MAX_BPP(dsc->max_bpp));
28411bf2f3bfSJani Nikula 
28421bf2f3bfSJani Nikula 	/*
28431bf2f3bfSJani Nikula 	 * FIXME: This is ugly, and slice count should take DSC engine
28441bf2f3bfSJani Nikula 	 * throughput etc. into account.
28451bf2f3bfSJani Nikula 	 *
28461bf2f3bfSJani Nikula 	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
28471bf2f3bfSJani Nikula 	 */
28481bf2f3bfSJani Nikula 	if (dsc->slices_per_line & BIT(2)) {
28491bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 4;
28501bf2f3bfSJani Nikula 	} else if (dsc->slices_per_line & BIT(1)) {
28511bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 2;
28521bf2f3bfSJani Nikula 	} else {
28531bf2f3bfSJani Nikula 		/* FIXME */
28541bf2f3bfSJani Nikula 		if (!(dsc->slices_per_line & BIT(0)))
28551bf2f3bfSJani Nikula 			DRM_DEBUG_KMS("VBT: Unsupported DSC slice count for DSI\n");
28561bf2f3bfSJani Nikula 
28571bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 1;
28581bf2f3bfSJani Nikula 	}
28591bf2f3bfSJani Nikula 
28601bf2f3bfSJani Nikula 	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
28611bf2f3bfSJani Nikula 	    crtc_state->dsc.slice_count != 0)
28621bf2f3bfSJani Nikula 		DRM_DEBUG_KMS("VBT: DSC hdisplay %d not divisible by slice count %d\n",
28631bf2f3bfSJani Nikula 			      crtc_state->hw.adjusted_mode.crtc_hdisplay,
28641bf2f3bfSJani Nikula 			      crtc_state->dsc.slice_count);
28651bf2f3bfSJani Nikula 
28661bf2f3bfSJani Nikula 	/*
28671bf2f3bfSJani Nikula 	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
2868fd8a5b27SJani Nikula 	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63.
28691bf2f3bfSJani Nikula 	 */
2870fd8a5b27SJani Nikula 	vdsc_cfg->rc_model_size = drm_dsc_dp_rc_buffer_size(dsc->rc_buffer_block_size,
2871fd8a5b27SJani Nikula 							    dsc->rc_buffer_size);
28721bf2f3bfSJani Nikula 
28731bf2f3bfSJani Nikula 	/* FIXME: DSI spec says bpc + 1 for this one */
28741bf2f3bfSJani Nikula 	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
28751bf2f3bfSJani Nikula 
28761bf2f3bfSJani Nikula 	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
28771bf2f3bfSJani Nikula 
28781bf2f3bfSJani Nikula 	vdsc_cfg->slice_height = dsc->slice_height;
28791bf2f3bfSJani Nikula }
28801bf2f3bfSJani Nikula 
28811bf2f3bfSJani Nikula /* FIXME: initially DSI specific */
28821bf2f3bfSJani Nikula bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
28831bf2f3bfSJani Nikula 			       struct intel_crtc_state *crtc_state,
28841bf2f3bfSJani Nikula 			       int dsc_max_bpc)
28851bf2f3bfSJani Nikula {
28861bf2f3bfSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
28873162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
28881bf2f3bfSJani Nikula 	const struct child_device_config *child;
28891bf2f3bfSJani Nikula 
28901bf2f3bfSJani Nikula 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
28911bf2f3bfSJani Nikula 		child = &devdata->child;
28921bf2f3bfSJani Nikula 
28931bf2f3bfSJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
28941bf2f3bfSJani Nikula 			continue;
28951bf2f3bfSJani Nikula 
28961bf2f3bfSJani Nikula 		if (child->dvo_port - DVO_PORT_MIPIA == encoder->port) {
28971bf2f3bfSJani Nikula 			if (!devdata->dsc)
28981bf2f3bfSJani Nikula 				return false;
28991bf2f3bfSJani Nikula 
29001bf2f3bfSJani Nikula 			if (crtc_state)
29011bf2f3bfSJani Nikula 				fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
29021bf2f3bfSJani Nikula 
29031bf2f3bfSJani Nikula 			return true;
29041bf2f3bfSJani Nikula 		}
29051bf2f3bfSJani Nikula 	}
29061bf2f3bfSJani Nikula 
29071bf2f3bfSJani Nikula 	return false;
29081bf2f3bfSJani Nikula }
29091bf2f3bfSJani Nikula 
2910df0566a6SJani Nikula /**
2911df0566a6SJani Nikula  * intel_bios_is_port_hpd_inverted - is HPD inverted for %port
2912df0566a6SJani Nikula  * @i915:	i915 device instance
2913df0566a6SJani Nikula  * @port:	port to check
2914df0566a6SJani Nikula  *
2915df0566a6SJani Nikula  * Return true if HPD should be inverted for %port.
2916df0566a6SJani Nikula  */
2917df0566a6SJani Nikula bool
2918df0566a6SJani Nikula intel_bios_is_port_hpd_inverted(const struct drm_i915_private *i915,
2919df0566a6SJani Nikula 				enum port port)
2920df0566a6SJani Nikula {
29215a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[port];
2922df0566a6SJani Nikula 
29232446e1d6SMatt Roper 	if (drm_WARN_ON_ONCE(&i915->drm,
29242446e1d6SMatt Roper 			     !IS_GEMINILAKE(i915) && !IS_BROXTON(i915)))
2925df0566a6SJani Nikula 		return false;
2926df0566a6SJani Nikula 
2927dbc13742SJani Nikula 	return devdata && devdata->child.hpd_invert;
2928df0566a6SJani Nikula }
2929df0566a6SJani Nikula 
2930df0566a6SJani Nikula /**
2931df0566a6SJani Nikula  * intel_bios_is_lspcon_present - if LSPCON is attached on %port
2932df0566a6SJani Nikula  * @i915:	i915 device instance
2933df0566a6SJani Nikula  * @port:	port to check
2934df0566a6SJani Nikula  *
2935df0566a6SJani Nikula  * Return true if LSPCON is present on this port
2936df0566a6SJani Nikula  */
2937df0566a6SJani Nikula bool
2938df0566a6SJani Nikula intel_bios_is_lspcon_present(const struct drm_i915_private *i915,
2939df0566a6SJani Nikula 			     enum port port)
2940df0566a6SJani Nikula {
29415a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[port];
2942df0566a6SJani Nikula 
2943dbc13742SJani Nikula 	return HAS_LSPCON(i915) && devdata && devdata->child.lspcon;
2944df0566a6SJani Nikula }
2945df0566a6SJani Nikula 
2946aaab24bbSUma Shankar /**
2947aaab24bbSUma Shankar  * intel_bios_is_lane_reversal_needed - if lane reversal needed on port
2948aaab24bbSUma Shankar  * @i915:       i915 device instance
2949aaab24bbSUma Shankar  * @port:       port to check
2950aaab24bbSUma Shankar  *
2951aaab24bbSUma Shankar  * Return true if port requires lane reversal
2952aaab24bbSUma Shankar  */
2953aaab24bbSUma Shankar bool
2954aaab24bbSUma Shankar intel_bios_is_lane_reversal_needed(const struct drm_i915_private *i915,
2955aaab24bbSUma Shankar 				   enum port port)
2956aaab24bbSUma Shankar {
29575a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[port];
2958aaab24bbSUma Shankar 
2959dbc13742SJani Nikula 	return devdata && devdata->child.lane_reversal;
2960aaab24bbSUma Shankar }
2961aaab24bbSUma Shankar 
2962dbd440d8SJani Nikula enum aux_ch intel_bios_port_aux_ch(struct drm_i915_private *i915,
2963df0566a6SJani Nikula 				   enum port port)
2964df0566a6SJani Nikula {
29655a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[port];
2966df0566a6SJani Nikula 	enum aux_ch aux_ch;
2967df0566a6SJani Nikula 
29685a449e58SJani Nikula 	if (!devdata || !devdata->child.aux_channel) {
2969df0566a6SJani Nikula 		aux_ch = (enum aux_ch)port;
2970df0566a6SJani Nikula 
2971dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2972e92cbf38SWambui Karuga 			    "using AUX %c for port %c (platform default)\n",
2973df0566a6SJani Nikula 			    aux_ch_name(aux_ch), port_name(port));
2974df0566a6SJani Nikula 		return aux_ch;
2975df0566a6SJani Nikula 	}
2976df0566a6SJani Nikula 
297718c283dfSAditya Swarup 	/*
297818c283dfSAditya Swarup 	 * RKL/DG1 VBT uses PHY based mapping. Combo PHYs A,B,C,D
297918c283dfSAditya Swarup 	 * map to DDI A,B,TC1,TC2 respectively.
298018c283dfSAditya Swarup 	 *
298118c283dfSAditya Swarup 	 * ADL-S VBT uses PHY based mapping. Combo PHYs A,B,C,D,E
298218c283dfSAditya Swarup 	 * map to DDI A,TC1,TC2,TC3,TC4 respectively.
298318c283dfSAditya Swarup 	 */
29845a449e58SJani Nikula 	switch (devdata->child.aux_channel) {
2985df0566a6SJani Nikula 	case DP_AUX_A:
2986df0566a6SJani Nikula 		aux_ch = AUX_CH_A;
2987df0566a6SJani Nikula 		break;
2988df0566a6SJani Nikula 	case DP_AUX_B:
2989dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
299018c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC1;
299118c283dfSAditya Swarup 		else
2992df0566a6SJani Nikula 			aux_ch = AUX_CH_B;
2993df0566a6SJani Nikula 		break;
2994df0566a6SJani Nikula 	case DP_AUX_C:
2995dbd440d8SJani Nikula 		if (IS_ALDERLAKE_S(i915))
299618c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC2;
2997dbd440d8SJani Nikula 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
299818c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC1;
299918c283dfSAditya Swarup 		else
300018c283dfSAditya Swarup 			aux_ch = AUX_CH_C;
3001df0566a6SJani Nikula 		break;
3002df0566a6SJani Nikula 	case DP_AUX_D:
3003ed2615a8SMatt Roper 		if (DISPLAY_VER(i915) == 13)
3004ed2615a8SMatt Roper 			aux_ch = AUX_CH_D_XELPD;
3005ed2615a8SMatt Roper 		else if (IS_ALDERLAKE_S(i915))
300618c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC3;
3007dbd440d8SJani Nikula 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
300818c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC2;
300918c283dfSAditya Swarup 		else
301018c283dfSAditya Swarup 			aux_ch = AUX_CH_D;
3011df0566a6SJani Nikula 		break;
3012df0566a6SJani Nikula 	case DP_AUX_E:
3013ed2615a8SMatt Roper 		if (DISPLAY_VER(i915) == 13)
3014ed2615a8SMatt Roper 			aux_ch = AUX_CH_E_XELPD;
3015ed2615a8SMatt Roper 		else if (IS_ALDERLAKE_S(i915))
301618c283dfSAditya Swarup 			aux_ch = AUX_CH_USBC4;
301718c283dfSAditya Swarup 		else
3018df0566a6SJani Nikula 			aux_ch = AUX_CH_E;
3019df0566a6SJani Nikula 		break;
3020df0566a6SJani Nikula 	case DP_AUX_F:
3021ed2615a8SMatt Roper 		if (DISPLAY_VER(i915) == 13)
3022ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC1;
3023ed2615a8SMatt Roper 		else
3024df0566a6SJani Nikula 			aux_ch = AUX_CH_F;
3025df0566a6SJani Nikula 		break;
3026eb8de23cSKhaled Almahallawy 	case DP_AUX_G:
3027ed2615a8SMatt Roper 		if (DISPLAY_VER(i915) == 13)
3028ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC2;
3029ed2615a8SMatt Roper 		else
3030eb8de23cSKhaled Almahallawy 			aux_ch = AUX_CH_G;
3031eb8de23cSKhaled Almahallawy 		break;
30325bf22ee4SVille Syrjälä 	case DP_AUX_H:
3033ed2615a8SMatt Roper 		if (DISPLAY_VER(i915) == 13)
3034ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC3;
3035ed2615a8SMatt Roper 		else
30365bf22ee4SVille Syrjälä 			aux_ch = AUX_CH_H;
30375bf22ee4SVille Syrjälä 		break;
30385bf22ee4SVille Syrjälä 	case DP_AUX_I:
3039ed2615a8SMatt Roper 		if (DISPLAY_VER(i915) == 13)
3040ed2615a8SMatt Roper 			aux_ch = AUX_CH_USBC4;
3041ed2615a8SMatt Roper 		else
30425bf22ee4SVille Syrjälä 			aux_ch = AUX_CH_I;
30435bf22ee4SVille Syrjälä 		break;
3044df0566a6SJani Nikula 	default:
30455a449e58SJani Nikula 		MISSING_CASE(devdata->child.aux_channel);
3046df0566a6SJani Nikula 		aux_ch = AUX_CH_A;
3047df0566a6SJani Nikula 		break;
3048df0566a6SJani Nikula 	}
3049df0566a6SJani Nikula 
3050dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "using AUX %c for port %c (VBT)\n",
3051df0566a6SJani Nikula 		    aux_ch_name(aux_ch), port_name(port));
3052df0566a6SJani Nikula 
3053df0566a6SJani Nikula 	return aux_ch;
3054df0566a6SJani Nikula }
3055d9ee2111SJani Nikula 
3056d9ee2111SJani Nikula int intel_bios_max_tmds_clock(struct intel_encoder *encoder)
3057d9ee2111SJani Nikula {
3058d9ee2111SJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
30595a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[encoder->port];
3060d9ee2111SJani Nikula 
30616ba69981SJani Nikula 	return _intel_bios_max_tmds_clock(devdata);
3062d9ee2111SJani Nikula }
30630aed3bdeSJani Nikula 
3064a9a56e76SJani Nikula /* This is an index in the HDMI/DVI DDI buffer translation table, or -1 */
30650aed3bdeSJani Nikula int intel_bios_hdmi_level_shift(struct intel_encoder *encoder)
30660aed3bdeSJani Nikula {
30670aed3bdeSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
30685a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[encoder->port];
30690aed3bdeSJani Nikula 
3070a9a56e76SJani Nikula 	return _intel_bios_hdmi_level_shift(devdata);
30710aed3bdeSJani Nikula }
3072605a1872SJani Nikula 
3073c0a950d1SJani Nikula int intel_bios_encoder_dp_boost_level(const struct intel_bios_encoder_data *devdata)
3074605a1872SJani Nikula {
3075c0a950d1SJani Nikula 	if (!devdata || devdata->i915->vbt.version < 196 || !devdata->child.iboost)
3076c0a950d1SJani Nikula 		return 0;
3077605a1872SJani Nikula 
3078c0a950d1SJani Nikula 	return translate_iboost(devdata->child.dp_iboost_level);
3079605a1872SJani Nikula }
308001a60883SJani Nikula 
3081c0a950d1SJani Nikula int intel_bios_encoder_hdmi_boost_level(const struct intel_bios_encoder_data *devdata)
308201a60883SJani Nikula {
3083c0a950d1SJani Nikula 	if (!devdata || devdata->i915->vbt.version < 196 || !devdata->child.iboost)
3084c0a950d1SJani Nikula 		return 0;
308501a60883SJani Nikula 
3086c0a950d1SJani Nikula 	return translate_iboost(devdata->child.hdmi_iboost_level);
308701a60883SJani Nikula }
3088f83acdabSJani Nikula 
3089f83acdabSJani Nikula int intel_bios_dp_max_link_rate(struct intel_encoder *encoder)
3090f83acdabSJani Nikula {
3091f83acdabSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
30925a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[encoder->port];
3093f83acdabSJani Nikula 
309472337aacSJani Nikula 	return _intel_bios_dp_max_link_rate(devdata);
3095f83acdabSJani Nikula }
309617004bfbSJani Nikula 
309717004bfbSJani Nikula int intel_bios_alternate_ddc_pin(struct intel_encoder *encoder)
309817004bfbSJani Nikula {
309917004bfbSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
31005a449e58SJani Nikula 	const struct intel_bios_encoder_data *devdata = i915->vbt.ports[encoder->port];
310117004bfbSJani Nikula 
3102dab8477bSJani Nikula 	if (!devdata || !devdata->child.ddc_pin)
3103dab8477bSJani Nikula 		return 0;
3104dab8477bSJani Nikula 
3105dab8477bSJani Nikula 	return map_ddc_pin(i915, devdata->child.ddc_pin);
310617004bfbSJani Nikula }
3107c5faae5aSJani Nikula 
3108f08fbe6aSJani Nikula bool intel_bios_encoder_supports_typec_usb(const struct intel_bios_encoder_data *devdata)
3109c5faae5aSJani Nikula {
3110f08fbe6aSJani Nikula 	return devdata->i915->vbt.version >= 195 && devdata->child.dp_usb_type_c;
3111c5faae5aSJani Nikula }
3112c5faae5aSJani Nikula 
3113f08fbe6aSJani Nikula bool intel_bios_encoder_supports_tbt(const struct intel_bios_encoder_data *devdata)
3114c5faae5aSJani Nikula {
3115f08fbe6aSJani Nikula 	return devdata->i915->vbt.version >= 209 && devdata->child.tbt;
3116c5faae5aSJani Nikula }
311745c0673aSJani Nikula 
311845c0673aSJani Nikula const struct intel_bios_encoder_data *
311945c0673aSJani Nikula intel_bios_encoder_data_lookup(struct drm_i915_private *i915, enum port port)
312045c0673aSJani Nikula {
31215a449e58SJani Nikula 	return i915->vbt.ports[port];
312245c0673aSJani Nikula }
3123