xref: /linux/drivers/gpu/drm/i915/display/intel_bios.c (revision ff9bc20cd21c23a8fca0d5039e23142898383d97)
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 
28da68386dSThomas Zimmermann #include <drm/display/drm_dp_helper.h>
292a64b147SThomas Zimmermann #include <drm/display/drm_dsc_helper.h>
30cfc10489SJani Nikula #include <drm/drm_edid.h>
31df0566a6SJani Nikula 
32df0566a6SJani Nikula #include "i915_drv.h"
33ce2fce25SMatt Roper #include "i915_reg.h"
34cfc10489SJani Nikula #include "intel_display.h"
35cfc10489SJani Nikula #include "intel_display_types.h"
36cfc10489SJani Nikula #include "intel_gmbus.h"
37df0566a6SJani Nikula 
38df0566a6SJani Nikula #define _INTEL_BIOS_PRIVATE
39df0566a6SJani Nikula #include "intel_vbt_defs.h"
40df0566a6SJani Nikula 
41df0566a6SJani Nikula /**
42df0566a6SJani Nikula  * DOC: Video BIOS Table (VBT)
43df0566a6SJani Nikula  *
44df0566a6SJani Nikula  * The Video BIOS Table, or VBT, provides platform and board specific
45df0566a6SJani Nikula  * configuration information to the driver that is not discoverable or available
46df0566a6SJani Nikula  * through other means. The configuration is mostly related to display
47df0566a6SJani Nikula  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
48df0566a6SJani Nikula  * the PCI ROM.
49df0566a6SJani Nikula  *
50df0566a6SJani Nikula  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
51df0566a6SJani Nikula  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
52df0566a6SJani Nikula  * contain the actual configuration information. The VBT Header, and thus the
53df0566a6SJani Nikula  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
54df0566a6SJani Nikula  * BDB Header. The data blocks are concatenated after the BDB Header. The data
55df0566a6SJani Nikula  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
56df0566a6SJani Nikula  * data. (Block 53, the MIPI Sequence Block is an exception.)
57df0566a6SJani Nikula  *
58df0566a6SJani Nikula  * The driver parses the VBT during load. The relevant information is stored in
59df0566a6SJani Nikula  * driver private data for ease of use, and the actual VBT is not read after
60df0566a6SJani Nikula  * that.
61df0566a6SJani Nikula  */
62df0566a6SJani Nikula 
630d9ef19bSJani Nikula /* Wrapper for VBT child device config */
643162d057SJani Nikula struct intel_bios_encoder_data {
657371fa34SJani Nikula 	struct drm_i915_private *i915;
667371fa34SJani Nikula 
670d9ef19bSJani Nikula 	struct child_device_config child;
686e0d46e9SJani Nikula 	struct dsc_compression_parameters_entry *dsc;
690d9ef19bSJani Nikula 	struct list_head node;
700d9ef19bSJani Nikula };
710d9ef19bSJani Nikula 
72df0566a6SJani Nikula #define	SLAVE_ADDR1	0x70
73df0566a6SJani Nikula #define	SLAVE_ADDR2	0x72
74df0566a6SJani Nikula 
75df0566a6SJani Nikula /* Get BDB block size given a pointer to Block ID. */
76df0566a6SJani Nikula static u32 _get_blocksize(const u8 *block_base)
77df0566a6SJani Nikula {
78df0566a6SJani Nikula 	/* The MIPI Sequence Block v3+ has a separate size field. */
79df0566a6SJani Nikula 	if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
80df0566a6SJani Nikula 		return *((const u32 *)(block_base + 4));
81df0566a6SJani Nikula 	else
82df0566a6SJani Nikula 		return *((const u16 *)(block_base + 1));
83df0566a6SJani Nikula }
84df0566a6SJani Nikula 
85df0566a6SJani Nikula /* Get BDB block size give a pointer to data after Block ID and Block Size. */
86df0566a6SJani Nikula static u32 get_blocksize(const void *block_data)
87df0566a6SJani Nikula {
88df0566a6SJani Nikula 	return _get_blocksize(block_data - 3);
89df0566a6SJani Nikula }
90df0566a6SJani Nikula 
91df0566a6SJani Nikula static const void *
92e163cfb4SVille Syrjälä find_raw_section(const void *_bdb, enum bdb_block_id section_id)
93df0566a6SJani Nikula {
94df0566a6SJani Nikula 	const struct bdb_header *bdb = _bdb;
95df0566a6SJani Nikula 	const u8 *base = _bdb;
96df0566a6SJani Nikula 	int index = 0;
97df0566a6SJani Nikula 	u32 total, current_size;
98df0566a6SJani Nikula 	enum bdb_block_id current_id;
99df0566a6SJani Nikula 
100df0566a6SJani Nikula 	/* skip to first section */
101df0566a6SJani Nikula 	index += bdb->header_size;
102df0566a6SJani Nikula 	total = bdb->bdb_size;
103df0566a6SJani Nikula 
104df0566a6SJani Nikula 	/* walk the sections looking for section_id */
105df0566a6SJani Nikula 	while (index + 3 < total) {
106df0566a6SJani Nikula 		current_id = *(base + index);
107df0566a6SJani Nikula 		current_size = _get_blocksize(base + index);
108df0566a6SJani Nikula 		index += 3;
109df0566a6SJani Nikula 
110df0566a6SJani Nikula 		if (index + current_size > total)
111df0566a6SJani Nikula 			return NULL;
112df0566a6SJani Nikula 
113df0566a6SJani Nikula 		if (current_id == section_id)
114df0566a6SJani Nikula 			return base + index;
115df0566a6SJani Nikula 
116df0566a6SJani Nikula 		index += current_size;
117df0566a6SJani Nikula 	}
118df0566a6SJani Nikula 
119df0566a6SJani Nikula 	return NULL;
120df0566a6SJani Nikula }
121df0566a6SJani Nikula 
122e163cfb4SVille Syrjälä /*
123e163cfb4SVille Syrjälä  * Offset from the start of BDB to the start of the
124e163cfb4SVille Syrjälä  * block data (just past the block header).
125e163cfb4SVille Syrjälä  */
12639b1bc4bSVille Syrjälä static u32 raw_block_offset(const void *bdb, enum bdb_block_id section_id)
127e163cfb4SVille Syrjälä {
128e163cfb4SVille Syrjälä 	const void *block;
129e163cfb4SVille Syrjälä 
130e163cfb4SVille Syrjälä 	block = find_raw_section(bdb, section_id);
131e163cfb4SVille Syrjälä 	if (!block)
132e163cfb4SVille Syrjälä 		return 0;
133e163cfb4SVille Syrjälä 
134e163cfb4SVille Syrjälä 	return block - bdb;
135e163cfb4SVille Syrjälä }
136e163cfb4SVille Syrjälä 
137e163cfb4SVille Syrjälä struct bdb_block_entry {
138e163cfb4SVille Syrjälä 	struct list_head node;
139e163cfb4SVille Syrjälä 	enum bdb_block_id section_id;
140e163cfb4SVille Syrjälä 	u8 data[];
141e163cfb4SVille Syrjälä };
142e163cfb4SVille Syrjälä 
143e163cfb4SVille Syrjälä static const void *
1440a93eeb5SMaarten Lankhorst bdb_find_section(struct drm_i915_private *i915,
145e163cfb4SVille Syrjälä 		 enum bdb_block_id section_id)
146e163cfb4SVille Syrjälä {
147e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry;
148e163cfb4SVille Syrjälä 
149a434689cSJani Nikula 	list_for_each_entry(entry, &i915->display.vbt.bdb_blocks, node) {
150e163cfb4SVille Syrjälä 		if (entry->section_id == section_id)
151e163cfb4SVille Syrjälä 			return entry->data + 3;
152e163cfb4SVille Syrjälä 	}
153e163cfb4SVille Syrjälä 
154e163cfb4SVille Syrjälä 	return NULL;
155e163cfb4SVille Syrjälä }
156e163cfb4SVille Syrjälä 
157e163cfb4SVille Syrjälä static const struct {
158e163cfb4SVille Syrjälä 	enum bdb_block_id section_id;
159e163cfb4SVille Syrjälä 	size_t min_size;
160e163cfb4SVille Syrjälä } bdb_blocks[] = {
161e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERAL_FEATURES,
162e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_general_features), },
163e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERAL_DEFINITIONS,
164e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_general_definitions), },
165e163cfb4SVille Syrjälä 	{ .section_id = BDB_PSR,
166e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_psr), },
167e163cfb4SVille Syrjälä 	{ .section_id = BDB_DRIVER_FEATURES,
168e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_driver_features), },
169e163cfb4SVille Syrjälä 	{ .section_id = BDB_SDVO_LVDS_OPTIONS,
170e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_sdvo_lvds_options), },
171e163cfb4SVille Syrjälä 	{ .section_id = BDB_SDVO_PANEL_DTDS,
172e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_sdvo_panel_dtds), },
173e163cfb4SVille Syrjälä 	{ .section_id = BDB_EDP,
174e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_edp), },
175e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_OPTIONS,
176e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lvds_options), },
177901a0cadSVille Syrjälä 	/*
178901a0cadSVille Syrjälä 	 * BDB_LVDS_LFP_DATA depends on BDB_LVDS_LFP_DATA_PTRS,
179901a0cadSVille Syrjälä 	 * so keep the two ordered.
180901a0cadSVille Syrjälä 	 */
181e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_LFP_DATA_PTRS,
182e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lvds_lfp_data_ptrs), },
183e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_LFP_DATA,
184901a0cadSVille Syrjälä 	  .min_size = 0, /* special case */ },
185e163cfb4SVille Syrjälä 	{ .section_id = BDB_LVDS_BACKLIGHT,
186e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_backlight_data), },
187e163cfb4SVille Syrjälä 	{ .section_id = BDB_LFP_POWER,
188e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_lfp_power), },
189e163cfb4SVille Syrjälä 	{ .section_id = BDB_MIPI_CONFIG,
190e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_mipi_config), },
191e163cfb4SVille Syrjälä 	{ .section_id = BDB_MIPI_SEQUENCE,
192e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_mipi_sequence) },
193e163cfb4SVille Syrjälä 	{ .section_id = BDB_COMPRESSION_PARAMETERS,
194e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_compression_parameters), },
195e163cfb4SVille Syrjälä 	{ .section_id = BDB_GENERIC_DTD,
196e163cfb4SVille Syrjälä 	  .min_size = sizeof(struct bdb_generic_dtd), },
197e163cfb4SVille Syrjälä };
198e163cfb4SVille Syrjälä 
199901a0cadSVille Syrjälä static size_t lfp_data_min_size(struct drm_i915_private *i915)
200901a0cadSVille Syrjälä {
201901a0cadSVille Syrjälä 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
202901a0cadSVille Syrjälä 	size_t size;
203901a0cadSVille Syrjälä 
2040a93eeb5SMaarten Lankhorst 	ptrs = bdb_find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
205901a0cadSVille Syrjälä 	if (!ptrs)
206901a0cadSVille Syrjälä 		return 0;
207901a0cadSVille Syrjälä 
208901a0cadSVille Syrjälä 	size = sizeof(struct bdb_lvds_lfp_data);
209901a0cadSVille Syrjälä 	if (ptrs->panel_name.table_size)
210901a0cadSVille Syrjälä 		size = max(size, ptrs->panel_name.offset +
211901a0cadSVille Syrjälä 			   sizeof(struct bdb_lvds_lfp_data_tail));
212901a0cadSVille Syrjälä 
213901a0cadSVille Syrjälä 	return size;
214901a0cadSVille Syrjälä }
215901a0cadSVille Syrjälä 
216514003e1SVille Syrjälä static bool validate_lfp_data_ptrs(const void *bdb,
217514003e1SVille Syrjälä 				   const struct bdb_lvds_lfp_data_ptrs *ptrs)
218514003e1SVille Syrjälä {
2195ab58d69SVille Syrjälä 	int fp_timing_size, dvo_timing_size, panel_pnp_id_size, panel_name_size;
220514003e1SVille Syrjälä 	int data_block_size, lfp_data_size;
2214e78d602SVille Syrjälä 	const void *data_block;
222514003e1SVille Syrjälä 	int i;
223514003e1SVille Syrjälä 
2244e78d602SVille Syrjälä 	data_block = find_raw_section(bdb, BDB_LVDS_LFP_DATA);
2254e78d602SVille Syrjälä 	if (!data_block)
2264e78d602SVille Syrjälä 		return false;
2274e78d602SVille Syrjälä 
2284e78d602SVille Syrjälä 	data_block_size = get_blocksize(data_block);
229514003e1SVille Syrjälä 	if (data_block_size == 0)
230514003e1SVille Syrjälä 		return false;
231514003e1SVille Syrjälä 
232514003e1SVille Syrjälä 	/* always 3 indicating the presence of fp_timing+dvo_timing+panel_pnp_id */
233514003e1SVille Syrjälä 	if (ptrs->lvds_entries != 3)
234514003e1SVille Syrjälä 		return false;
235514003e1SVille Syrjälä 
236514003e1SVille Syrjälä 	fp_timing_size = ptrs->ptr[0].fp_timing.table_size;
237514003e1SVille Syrjälä 	dvo_timing_size = ptrs->ptr[0].dvo_timing.table_size;
238514003e1SVille Syrjälä 	panel_pnp_id_size = ptrs->ptr[0].panel_pnp_id.table_size;
2395ab58d69SVille Syrjälä 	panel_name_size = ptrs->panel_name.table_size;
240514003e1SVille Syrjälä 
241514003e1SVille Syrjälä 	/* fp_timing has variable size */
242514003e1SVille Syrjälä 	if (fp_timing_size < 32 ||
243514003e1SVille Syrjälä 	    dvo_timing_size != sizeof(struct lvds_dvo_timing) ||
244514003e1SVille Syrjälä 	    panel_pnp_id_size != sizeof(struct lvds_pnp_id))
245514003e1SVille Syrjälä 		return false;
246514003e1SVille Syrjälä 
2475ab58d69SVille Syrjälä 	/* panel_name is not present in old VBTs */
2485ab58d69SVille Syrjälä 	if (panel_name_size != 0 &&
2495ab58d69SVille Syrjälä 	    panel_name_size != sizeof(struct lvds_lfp_panel_name))
2505ab58d69SVille Syrjälä 		return false;
2515ab58d69SVille Syrjälä 
252514003e1SVille Syrjälä 	lfp_data_size = ptrs->ptr[1].fp_timing.offset - ptrs->ptr[0].fp_timing.offset;
253514003e1SVille Syrjälä 	if (16 * lfp_data_size > data_block_size)
254514003e1SVille Syrjälä 		return false;
255514003e1SVille Syrjälä 
256514003e1SVille Syrjälä 	/* make sure the table entries have uniform size */
257514003e1SVille Syrjälä 	for (i = 1; i < 16; i++) {
258514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.table_size != fp_timing_size ||
259514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.table_size != dvo_timing_size ||
260514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.table_size != panel_pnp_id_size)
261514003e1SVille Syrjälä 			return false;
262514003e1SVille Syrjälä 
263514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset - ptrs->ptr[i-1].fp_timing.offset != lfp_data_size ||
264514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset - ptrs->ptr[i-1].dvo_timing.offset != lfp_data_size ||
265514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset - ptrs->ptr[i-1].panel_pnp_id.offset != lfp_data_size)
266514003e1SVille Syrjälä 			return false;
267514003e1SVille Syrjälä 	}
268514003e1SVille Syrjälä 
2694e78d602SVille Syrjälä 	/*
2704e78d602SVille Syrjälä 	 * Except for vlv/chv machines all real VBTs seem to have 6
2714e78d602SVille Syrjälä 	 * unaccounted bytes in the fp_timing table. And it doesn't
2724e78d602SVille Syrjälä 	 * appear to be a really intentional hole as the fp_timing
2734e78d602SVille Syrjälä 	 * 0xffff terminator is always within those 6 missing bytes.
2744e78d602SVille Syrjälä 	 */
2754e78d602SVille Syrjälä 	if (fp_timing_size + 6 + dvo_timing_size + panel_pnp_id_size == lfp_data_size)
2764e78d602SVille Syrjälä 		fp_timing_size += 6;
2774e78d602SVille Syrjälä 
2784e78d602SVille Syrjälä 	if (fp_timing_size + dvo_timing_size + panel_pnp_id_size != lfp_data_size)
2794e78d602SVille Syrjälä 		return false;
2804e78d602SVille Syrjälä 
2814e78d602SVille Syrjälä 	if (ptrs->ptr[0].fp_timing.offset + fp_timing_size != ptrs->ptr[0].dvo_timing.offset ||
2824e78d602SVille Syrjälä 	    ptrs->ptr[0].dvo_timing.offset + dvo_timing_size != ptrs->ptr[0].panel_pnp_id.offset ||
2834e78d602SVille Syrjälä 	    ptrs->ptr[0].panel_pnp_id.offset + panel_pnp_id_size != lfp_data_size)
2844e78d602SVille Syrjälä 		return false;
2854e78d602SVille Syrjälä 
286514003e1SVille Syrjälä 	/* make sure the tables fit inside the data block */
287514003e1SVille Syrjälä 	for (i = 0; i < 16; i++) {
288514003e1SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset + fp_timing_size > data_block_size ||
289514003e1SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset + dvo_timing_size > data_block_size ||
290514003e1SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset + panel_pnp_id_size > data_block_size)
291514003e1SVille Syrjälä 			return false;
292514003e1SVille Syrjälä 	}
293514003e1SVille Syrjälä 
2945ab58d69SVille Syrjälä 	if (ptrs->panel_name.offset + 16 * panel_name_size > data_block_size)
2955ab58d69SVille Syrjälä 		return false;
2965ab58d69SVille Syrjälä 
2974e78d602SVille Syrjälä 	/* make sure fp_timing terminators are present at expected locations */
2984e78d602SVille Syrjälä 	for (i = 0; i < 16; i++) {
2994e78d602SVille Syrjälä 		const u16 *t = data_block + ptrs->ptr[i].fp_timing.offset +
3004e78d602SVille Syrjälä 			fp_timing_size - 2;
3014e78d602SVille Syrjälä 
3024e78d602SVille Syrjälä 		if (*t != 0xffff)
3034e78d602SVille Syrjälä 			return false;
3044e78d602SVille Syrjälä 	}
3054e78d602SVille Syrjälä 
306514003e1SVille Syrjälä 	return true;
307514003e1SVille Syrjälä }
308514003e1SVille Syrjälä 
309918f3025SVille Syrjälä /* make the data table offsets relative to the data block */
310918f3025SVille Syrjälä static bool fixup_lfp_data_ptrs(const void *bdb, void *ptrs_block)
311918f3025SVille Syrjälä {
312918f3025SVille Syrjälä 	struct bdb_lvds_lfp_data_ptrs *ptrs = ptrs_block;
313918f3025SVille Syrjälä 	u32 offset;
314918f3025SVille Syrjälä 	int i;
315918f3025SVille Syrjälä 
31639b1bc4bSVille Syrjälä 	offset = raw_block_offset(bdb, BDB_LVDS_LFP_DATA);
317918f3025SVille Syrjälä 
318918f3025SVille Syrjälä 	for (i = 0; i < 16; i++) {
319918f3025SVille Syrjälä 		if (ptrs->ptr[i].fp_timing.offset < offset ||
320918f3025SVille Syrjälä 		    ptrs->ptr[i].dvo_timing.offset < offset ||
321918f3025SVille Syrjälä 		    ptrs->ptr[i].panel_pnp_id.offset < offset)
322918f3025SVille Syrjälä 			return false;
323918f3025SVille Syrjälä 
324918f3025SVille Syrjälä 		ptrs->ptr[i].fp_timing.offset -= offset;
325918f3025SVille Syrjälä 		ptrs->ptr[i].dvo_timing.offset -= offset;
326918f3025SVille Syrjälä 		ptrs->ptr[i].panel_pnp_id.offset -= offset;
327918f3025SVille Syrjälä 	}
328918f3025SVille Syrjälä 
3295ab58d69SVille Syrjälä 	if (ptrs->panel_name.table_size) {
3305ab58d69SVille Syrjälä 		if (ptrs->panel_name.offset < offset)
3315ab58d69SVille Syrjälä 			return false;
3325ab58d69SVille Syrjälä 
3335ab58d69SVille Syrjälä 		ptrs->panel_name.offset -= offset;
3345ab58d69SVille Syrjälä 	}
3355ab58d69SVille Syrjälä 
336514003e1SVille Syrjälä 	return validate_lfp_data_ptrs(bdb, ptrs);
337918f3025SVille Syrjälä }
338918f3025SVille Syrjälä 
339a87d0a84SVille Syrjälä static int make_lfp_data_ptr(struct lvds_lfp_data_ptr_table *table,
340a87d0a84SVille Syrjälä 			     int table_size, int total_size)
341a87d0a84SVille Syrjälä {
342a87d0a84SVille Syrjälä 	if (total_size < table_size)
343a87d0a84SVille Syrjälä 		return total_size;
344a87d0a84SVille Syrjälä 
345a87d0a84SVille Syrjälä 	table->table_size = table_size;
346a87d0a84SVille Syrjälä 	table->offset = total_size - table_size;
347a87d0a84SVille Syrjälä 
348a87d0a84SVille Syrjälä 	return total_size - table_size;
349a87d0a84SVille Syrjälä }
350a87d0a84SVille Syrjälä 
351a87d0a84SVille Syrjälä static void next_lfp_data_ptr(struct lvds_lfp_data_ptr_table *next,
352a87d0a84SVille Syrjälä 			      const struct lvds_lfp_data_ptr_table *prev,
353a87d0a84SVille Syrjälä 			      int size)
354a87d0a84SVille Syrjälä {
355a87d0a84SVille Syrjälä 	next->table_size = prev->table_size;
356a87d0a84SVille Syrjälä 	next->offset = prev->offset + size;
357a87d0a84SVille Syrjälä }
358a87d0a84SVille Syrjälä 
359a87d0a84SVille Syrjälä static void *generate_lfp_data_ptrs(struct drm_i915_private *i915,
360a87d0a84SVille Syrjälä 				    const void *bdb)
361a87d0a84SVille Syrjälä {
362d3a70518SVille Syrjälä 	int i, size, table_size, block_size, offset, fp_timing_size;
363a87d0a84SVille Syrjälä 	struct bdb_lvds_lfp_data_ptrs *ptrs;
364d3a70518SVille Syrjälä 	const void *block;
365a87d0a84SVille Syrjälä 	void *ptrs_block;
366a87d0a84SVille Syrjälä 
367d3a70518SVille Syrjälä 	/*
368d3a70518SVille Syrjälä 	 * The hardcoded fp_timing_size is only valid for
369d3a70518SVille Syrjälä 	 * modernish VBTs. All older VBTs definitely should
370d3a70518SVille Syrjälä 	 * include block 41 and thus we don't need to
371d3a70518SVille Syrjälä 	 * generate one.
372d3a70518SVille Syrjälä 	 */
373d3a70518SVille Syrjälä 	if (i915->display.vbt.version < 155)
374d3a70518SVille Syrjälä 		return NULL;
375d3a70518SVille Syrjälä 
376d3a70518SVille Syrjälä 	fp_timing_size = 38;
377d3a70518SVille Syrjälä 
378a87d0a84SVille Syrjälä 	block = find_raw_section(bdb, BDB_LVDS_LFP_DATA);
379a87d0a84SVille Syrjälä 	if (!block)
380a87d0a84SVille Syrjälä 		return NULL;
381a87d0a84SVille Syrjälä 
382a87d0a84SVille Syrjälä 	drm_dbg_kms(&i915->drm, "Generating LFP data table pointers\n");
383a87d0a84SVille Syrjälä 
384a87d0a84SVille Syrjälä 	block_size = get_blocksize(block);
385a87d0a84SVille Syrjälä 
386d3a70518SVille Syrjälä 	size = fp_timing_size + sizeof(struct lvds_dvo_timing) +
387d3a70518SVille Syrjälä 		sizeof(struct lvds_pnp_id);
388a87d0a84SVille Syrjälä 	if (size * 16 > block_size)
389a87d0a84SVille Syrjälä 		return NULL;
390a87d0a84SVille Syrjälä 
391a87d0a84SVille Syrjälä 	ptrs_block = kzalloc(sizeof(*ptrs) + 3, GFP_KERNEL);
392a87d0a84SVille Syrjälä 	if (!ptrs_block)
393a87d0a84SVille Syrjälä 		return NULL;
394a87d0a84SVille Syrjälä 
395a87d0a84SVille Syrjälä 	*(u8 *)(ptrs_block + 0) = BDB_LVDS_LFP_DATA_PTRS;
396a87d0a84SVille Syrjälä 	*(u16 *)(ptrs_block + 1) = sizeof(*ptrs);
397a87d0a84SVille Syrjälä 	ptrs = ptrs_block + 3;
398a87d0a84SVille Syrjälä 
399a87d0a84SVille Syrjälä 	table_size = sizeof(struct lvds_pnp_id);
400a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].panel_pnp_id, table_size, size);
401a87d0a84SVille Syrjälä 
402a87d0a84SVille Syrjälä 	table_size = sizeof(struct lvds_dvo_timing);
403a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].dvo_timing, table_size, size);
404a87d0a84SVille Syrjälä 
405d3a70518SVille Syrjälä 	table_size = fp_timing_size;
406a87d0a84SVille Syrjälä 	size = make_lfp_data_ptr(&ptrs->ptr[0].fp_timing, table_size, size);
407a87d0a84SVille Syrjälä 
408a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].fp_timing.table_size)
409a87d0a84SVille Syrjälä 		ptrs->lvds_entries++;
410a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].dvo_timing.table_size)
411a87d0a84SVille Syrjälä 		ptrs->lvds_entries++;
412a87d0a84SVille Syrjälä 	if (ptrs->ptr[0].panel_pnp_id.table_size)
413a87d0a84SVille Syrjälä 		ptrs->lvds_entries++;
414a87d0a84SVille Syrjälä 
415a87d0a84SVille Syrjälä 	if (size != 0 || ptrs->lvds_entries != 3) {
4167674cd0bSXia Fukun 		kfree(ptrs_block);
417a87d0a84SVille Syrjälä 		return NULL;
418a87d0a84SVille Syrjälä 	}
419a87d0a84SVille Syrjälä 
420d3a70518SVille Syrjälä 	size = fp_timing_size + sizeof(struct lvds_dvo_timing) +
421d3a70518SVille Syrjälä 		sizeof(struct lvds_pnp_id);
422a87d0a84SVille Syrjälä 	for (i = 1; i < 16; i++) {
423a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].fp_timing, &ptrs->ptr[i-1].fp_timing, size);
424a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].dvo_timing, &ptrs->ptr[i-1].dvo_timing, size);
425a87d0a84SVille Syrjälä 		next_lfp_data_ptr(&ptrs->ptr[i].panel_pnp_id, &ptrs->ptr[i-1].panel_pnp_id, size);
426a87d0a84SVille Syrjälä 	}
427a87d0a84SVille Syrjälä 
428a87d0a84SVille Syrjälä 	table_size = sizeof(struct lvds_lfp_panel_name);
429a87d0a84SVille Syrjälä 
430a87d0a84SVille Syrjälä 	if (16 * (size + table_size) <= block_size) {
431a87d0a84SVille Syrjälä 		ptrs->panel_name.table_size = table_size;
432a87d0a84SVille Syrjälä 		ptrs->panel_name.offset = size * 16;
433a87d0a84SVille Syrjälä 	}
434a87d0a84SVille Syrjälä 
435a87d0a84SVille Syrjälä 	offset = block - bdb;
436a87d0a84SVille Syrjälä 
437a87d0a84SVille Syrjälä 	for (i = 0; i < 16; i++) {
438a87d0a84SVille Syrjälä 		ptrs->ptr[i].fp_timing.offset += offset;
439a87d0a84SVille Syrjälä 		ptrs->ptr[i].dvo_timing.offset += offset;
440a87d0a84SVille Syrjälä 		ptrs->ptr[i].panel_pnp_id.offset += offset;
441a87d0a84SVille Syrjälä 	}
442a87d0a84SVille Syrjälä 
443a87d0a84SVille Syrjälä 	if (ptrs->panel_name.table_size)
444a87d0a84SVille Syrjälä 		ptrs->panel_name.offset += offset;
445a87d0a84SVille Syrjälä 
446a87d0a84SVille Syrjälä 	return ptrs_block;
447a87d0a84SVille Syrjälä }
448a87d0a84SVille Syrjälä 
449e163cfb4SVille Syrjälä static void
450e163cfb4SVille Syrjälä init_bdb_block(struct drm_i915_private *i915,
451e163cfb4SVille Syrjälä 	       const void *bdb, enum bdb_block_id section_id,
452e163cfb4SVille Syrjälä 	       size_t min_size)
453e163cfb4SVille Syrjälä {
454e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry;
455a87d0a84SVille Syrjälä 	void *temp_block = NULL;
456e163cfb4SVille Syrjälä 	const void *block;
457e163cfb4SVille Syrjälä 	size_t block_size;
458e163cfb4SVille Syrjälä 
459e163cfb4SVille Syrjälä 	block = find_raw_section(bdb, section_id);
460a87d0a84SVille Syrjälä 
461a87d0a84SVille Syrjälä 	/* Modern VBTs lack the LFP data table pointers block, make one up */
462a87d0a84SVille Syrjälä 	if (!block && section_id == BDB_LVDS_LFP_DATA_PTRS) {
463a87d0a84SVille Syrjälä 		temp_block = generate_lfp_data_ptrs(i915, bdb);
464a87d0a84SVille Syrjälä 		if (temp_block)
465a87d0a84SVille Syrjälä 			block = temp_block + 3;
466a87d0a84SVille Syrjälä 	}
467e163cfb4SVille Syrjälä 	if (!block)
468e163cfb4SVille Syrjälä 		return;
469e163cfb4SVille Syrjälä 
470e163cfb4SVille Syrjälä 	drm_WARN(&i915->drm, min_size == 0,
471e163cfb4SVille Syrjälä 		 "Block %d min_size is zero\n", section_id);
472e163cfb4SVille Syrjälä 
473e163cfb4SVille Syrjälä 	block_size = get_blocksize(block);
474e163cfb4SVille Syrjälä 
475a06289f3SVille Syrjälä 	/*
476a06289f3SVille Syrjälä 	 * Version number and new block size are considered
477a06289f3SVille Syrjälä 	 * part of the header for MIPI sequenece block v3+.
478a06289f3SVille Syrjälä 	 */
479a06289f3SVille Syrjälä 	if (section_id == BDB_MIPI_SEQUENCE && *(const u8 *)block >= 3)
480a06289f3SVille Syrjälä 		block_size += 5;
481a06289f3SVille Syrjälä 
482e163cfb4SVille Syrjälä 	entry = kzalloc(struct_size(entry, data, max(min_size, block_size) + 3),
483e163cfb4SVille Syrjälä 			GFP_KERNEL);
484a87d0a84SVille Syrjälä 	if (!entry) {
485a87d0a84SVille Syrjälä 		kfree(temp_block);
486e163cfb4SVille Syrjälä 		return;
487a87d0a84SVille Syrjälä 	}
488e163cfb4SVille Syrjälä 
489e163cfb4SVille Syrjälä 	entry->section_id = section_id;
490e163cfb4SVille Syrjälä 	memcpy(entry->data, block - 3, block_size + 3);
491e163cfb4SVille Syrjälä 
492a87d0a84SVille Syrjälä 	kfree(temp_block);
493a87d0a84SVille Syrjälä 
494e163cfb4SVille Syrjälä 	drm_dbg_kms(&i915->drm, "Found BDB block %d (size %zu, min size %zu)\n",
495e163cfb4SVille Syrjälä 		    section_id, block_size, min_size);
496e163cfb4SVille Syrjälä 
497918f3025SVille Syrjälä 	if (section_id == BDB_LVDS_LFP_DATA_PTRS &&
498918f3025SVille Syrjälä 	    !fixup_lfp_data_ptrs(bdb, entry->data + 3)) {
499918f3025SVille Syrjälä 		drm_err(&i915->drm, "VBT has malformed LFP data table pointers\n");
500918f3025SVille Syrjälä 		kfree(entry);
501918f3025SVille Syrjälä 		return;
502918f3025SVille Syrjälä 	}
503918f3025SVille Syrjälä 
504a434689cSJani Nikula 	list_add_tail(&entry->node, &i915->display.vbt.bdb_blocks);
505e163cfb4SVille Syrjälä }
506e163cfb4SVille Syrjälä 
507e163cfb4SVille Syrjälä static void init_bdb_blocks(struct drm_i915_private *i915,
508e163cfb4SVille Syrjälä 			    const void *bdb)
509e163cfb4SVille Syrjälä {
510e163cfb4SVille Syrjälä 	int i;
511e163cfb4SVille Syrjälä 
512e163cfb4SVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(bdb_blocks); i++) {
513e163cfb4SVille Syrjälä 		enum bdb_block_id section_id = bdb_blocks[i].section_id;
514e163cfb4SVille Syrjälä 		size_t min_size = bdb_blocks[i].min_size;
515e163cfb4SVille Syrjälä 
516901a0cadSVille Syrjälä 		if (section_id == BDB_LVDS_LFP_DATA)
517901a0cadSVille Syrjälä 			min_size = lfp_data_min_size(i915);
518901a0cadSVille Syrjälä 
519e163cfb4SVille Syrjälä 		init_bdb_block(i915, bdb, section_id, min_size);
520e163cfb4SVille Syrjälä 	}
521e163cfb4SVille Syrjälä }
522e163cfb4SVille Syrjälä 
523df0566a6SJani Nikula static void
524bb6f53d4SVille Syrjälä fill_detail_timing_data(struct drm_i915_private *i915,
525bb6f53d4SVille Syrjälä 			struct drm_display_mode *panel_fixed_mode,
526df0566a6SJani Nikula 			const struct lvds_dvo_timing *dvo_timing)
527df0566a6SJani Nikula {
528df0566a6SJani Nikula 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
529df0566a6SJani Nikula 		dvo_timing->hactive_lo;
530df0566a6SJani Nikula 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
531df0566a6SJani Nikula 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
532df0566a6SJani Nikula 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
533df0566a6SJani Nikula 		((dvo_timing->hsync_pulse_width_hi << 8) |
534df0566a6SJani Nikula 			dvo_timing->hsync_pulse_width_lo);
535df0566a6SJani Nikula 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
536df0566a6SJani Nikula 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
537df0566a6SJani Nikula 
538df0566a6SJani Nikula 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
539df0566a6SJani Nikula 		dvo_timing->vactive_lo;
540df0566a6SJani Nikula 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
541df0566a6SJani Nikula 		((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
542df0566a6SJani Nikula 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
543df0566a6SJani Nikula 		((dvo_timing->vsync_pulse_width_hi << 4) |
544df0566a6SJani Nikula 			dvo_timing->vsync_pulse_width_lo);
545df0566a6SJani Nikula 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
546df0566a6SJani Nikula 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
547df0566a6SJani Nikula 	panel_fixed_mode->clock = dvo_timing->clock * 10;
548df0566a6SJani Nikula 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
549df0566a6SJani Nikula 
550df0566a6SJani Nikula 	if (dvo_timing->hsync_positive)
551df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
552df0566a6SJani Nikula 	else
553df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
554df0566a6SJani Nikula 
555df0566a6SJani Nikula 	if (dvo_timing->vsync_positive)
556df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
557df0566a6SJani Nikula 	else
558df0566a6SJani Nikula 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
559df0566a6SJani Nikula 
560df0566a6SJani Nikula 	panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
561df0566a6SJani Nikula 		dvo_timing->himage_lo;
562df0566a6SJani Nikula 	panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
563df0566a6SJani Nikula 		dvo_timing->vimage_lo;
564df0566a6SJani Nikula 
565bb6f53d4SVille Syrjälä 	/* Some VBTs have bogus h/vsync_end values */
566bb6f53d4SVille Syrjälä 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal) {
567bb6f53d4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "reducing hsync_end %d->%d\n",
568bb6f53d4SVille Syrjälä 			    panel_fixed_mode->hsync_end, panel_fixed_mode->htotal);
569bb6f53d4SVille Syrjälä 		panel_fixed_mode->hsync_end = panel_fixed_mode->htotal;
570bb6f53d4SVille Syrjälä 	}
571bb6f53d4SVille Syrjälä 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal) {
572bb6f53d4SVille Syrjälä 		drm_dbg_kms(&i915->drm, "reducing vsync_end %d->%d\n",
573bb6f53d4SVille Syrjälä 			    panel_fixed_mode->vsync_end, panel_fixed_mode->vtotal);
574bb6f53d4SVille Syrjälä 		panel_fixed_mode->vsync_end = panel_fixed_mode->vtotal;
575bb6f53d4SVille Syrjälä 	}
576df0566a6SJani Nikula 
577df0566a6SJani Nikula 	drm_mode_set_name(panel_fixed_mode);
578df0566a6SJani Nikula }
579df0566a6SJani Nikula 
580df0566a6SJani Nikula static const struct lvds_dvo_timing *
58158b2e382SVille Syrjälä get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *data,
58258b2e382SVille Syrjälä 		    const struct bdb_lvds_lfp_data_ptrs *ptrs,
583df0566a6SJani Nikula 		    int index)
584df0566a6SJani Nikula {
58558b2e382SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].dvo_timing.offset;
586df0566a6SJani Nikula }
587df0566a6SJani Nikula 
588df0566a6SJani Nikula static const struct lvds_fp_timing *
589918f3025SVille Syrjälä get_lvds_fp_timing(const struct bdb_lvds_lfp_data *data,
590df0566a6SJani Nikula 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
591df0566a6SJani Nikula 		   int index)
592df0566a6SJani Nikula {
59358b2e382SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].fp_timing.offset;
594df0566a6SJani Nikula }
595df0566a6SJani Nikula 
596c518a775SVille Syrjälä static const struct lvds_pnp_id *
597c518a775SVille Syrjälä get_lvds_pnp_id(const struct bdb_lvds_lfp_data *data,
598c518a775SVille Syrjälä 		const struct bdb_lvds_lfp_data_ptrs *ptrs,
599c518a775SVille Syrjälä 		int index)
600c518a775SVille Syrjälä {
601c518a775SVille Syrjälä 	return (const void *)data + ptrs->ptr[index].panel_pnp_id.offset;
602c518a775SVille Syrjälä }
603c518a775SVille Syrjälä 
604901a0cadSVille Syrjälä static const struct bdb_lvds_lfp_data_tail *
605901a0cadSVille Syrjälä get_lfp_data_tail(const struct bdb_lvds_lfp_data *data,
606901a0cadSVille Syrjälä 		  const struct bdb_lvds_lfp_data_ptrs *ptrs)
607901a0cadSVille Syrjälä {
608901a0cadSVille Syrjälä 	if (ptrs->panel_name.table_size)
609901a0cadSVille Syrjälä 		return (const void *)data + ptrs->panel_name.offset;
610901a0cadSVille Syrjälä 	else
611901a0cadSVille Syrjälä 		return NULL;
612901a0cadSVille Syrjälä }
613901a0cadSVille Syrjälä 
61406bfa86eSVille Syrjälä static void dump_pnp_id(struct drm_i915_private *i915,
61506bfa86eSVille Syrjälä 			const struct lvds_pnp_id *pnp_id,
61606bfa86eSVille Syrjälä 			const char *name)
61706bfa86eSVille Syrjälä {
61806bfa86eSVille Syrjälä 	u16 mfg_name = be16_to_cpu((__force __be16)pnp_id->mfg_name);
61906bfa86eSVille Syrjälä 	char vend[4];
62006bfa86eSVille Syrjälä 
62106bfa86eSVille Syrjälä 	drm_dbg_kms(&i915->drm, "%s PNPID mfg: %s (0x%x), prod: %u, serial: %u, week: %d, year: %d\n",
62206bfa86eSVille Syrjälä 		    name, drm_edid_decode_mfg_id(mfg_name, vend),
62306bfa86eSVille Syrjälä 		    pnp_id->mfg_name, pnp_id->product_code, pnp_id->serial,
62406bfa86eSVille Syrjälä 		    pnp_id->mfg_week, pnp_id->mfg_year + 1990);
62506bfa86eSVille Syrjälä }
62606bfa86eSVille Syrjälä 
627c518a775SVille Syrjälä static int opregion_get_panel_type(struct drm_i915_private *i915,
6286434cf63SAnimesh Manna 				   const struct intel_bios_encoder_data *devdata,
629c36225a1SJani Nikula 				   const struct drm_edid *drm_edid, bool use_fallback)
630cc589f2dSVille Syrjälä {
631cc589f2dSVille Syrjälä 	return intel_opregion_get_panel_type(i915);
632cc589f2dSVille Syrjälä }
633cc589f2dSVille Syrjälä 
634c518a775SVille Syrjälä static int vbt_get_panel_type(struct drm_i915_private *i915,
6356434cf63SAnimesh Manna 			      const struct intel_bios_encoder_data *devdata,
636c36225a1SJani Nikula 			      const struct drm_edid *drm_edid, bool use_fallback)
637719f4c51SVille Syrjälä {
638719f4c51SVille Syrjälä 	const struct bdb_lvds_options *lvds_options;
639719f4c51SVille Syrjälä 
6400a93eeb5SMaarten Lankhorst 	lvds_options = bdb_find_section(i915, BDB_LVDS_OPTIONS);
641719f4c51SVille Syrjälä 	if (!lvds_options)
642719f4c51SVille Syrjälä 		return -1;
643719f4c51SVille Syrjälä 
644c518a775SVille Syrjälä 	if (lvds_options->panel_type > 0xf &&
645c518a775SVille Syrjälä 	    lvds_options->panel_type != 0xff) {
646719f4c51SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Invalid VBT panel type 0x%x\n",
647719f4c51SVille Syrjälä 			    lvds_options->panel_type);
648719f4c51SVille Syrjälä 		return -1;
649719f4c51SVille Syrjälä 	}
650719f4c51SVille Syrjälä 
6516434cf63SAnimesh Manna 	if (devdata && devdata->child.handle == DEVICE_HANDLE_LFP2)
6526434cf63SAnimesh Manna 		return lvds_options->panel_type2;
6536434cf63SAnimesh Manna 
6546434cf63SAnimesh Manna 	drm_WARN_ON(&i915->drm, devdata && devdata->child.handle != DEVICE_HANDLE_LFP1);
6556434cf63SAnimesh Manna 
656719f4c51SVille Syrjälä 	return lvds_options->panel_type;
657719f4c51SVille Syrjälä }
658719f4c51SVille Syrjälä 
659c518a775SVille Syrjälä static int pnpid_get_panel_type(struct drm_i915_private *i915,
6606434cf63SAnimesh Manna 				const struct intel_bios_encoder_data *devdata,
661c36225a1SJani Nikula 				const struct drm_edid *drm_edid, bool use_fallback)
662c518a775SVille Syrjälä {
663c518a775SVille Syrjälä 	const struct bdb_lvds_lfp_data *data;
664c518a775SVille Syrjälä 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
665c518a775SVille Syrjälä 	const struct lvds_pnp_id *edid_id;
666c518a775SVille Syrjälä 	struct lvds_pnp_id edid_id_nodate;
667c36225a1SJani Nikula 	const struct edid *edid = drm_edid_raw(drm_edid); /* FIXME */
668c518a775SVille Syrjälä 	int i, best = -1;
669c518a775SVille Syrjälä 
670c518a775SVille Syrjälä 	if (!edid)
671c518a775SVille Syrjälä 		return -1;
672c518a775SVille Syrjälä 
673c518a775SVille Syrjälä 	edid_id = (const void *)&edid->mfg_id[0];
674c518a775SVille Syrjälä 
675c518a775SVille Syrjälä 	edid_id_nodate = *edid_id;
676c518a775SVille Syrjälä 	edid_id_nodate.mfg_week = 0;
677c518a775SVille Syrjälä 	edid_id_nodate.mfg_year = 0;
678c518a775SVille Syrjälä 
67906bfa86eSVille Syrjälä 	dump_pnp_id(i915, edid_id, "EDID");
68006bfa86eSVille Syrjälä 
6810a93eeb5SMaarten Lankhorst 	ptrs = bdb_find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
682c518a775SVille Syrjälä 	if (!ptrs)
683c518a775SVille Syrjälä 		return -1;
684c518a775SVille Syrjälä 
6850a93eeb5SMaarten Lankhorst 	data = bdb_find_section(i915, BDB_LVDS_LFP_DATA);
686c518a775SVille Syrjälä 	if (!data)
687c518a775SVille Syrjälä 		return -1;
688c518a775SVille Syrjälä 
689c518a775SVille Syrjälä 	for (i = 0; i < 16; i++) {
690c518a775SVille Syrjälä 		const struct lvds_pnp_id *vbt_id =
691c518a775SVille Syrjälä 			get_lvds_pnp_id(data, ptrs, i);
692c518a775SVille Syrjälä 
693c518a775SVille Syrjälä 		/* full match? */
694c518a775SVille Syrjälä 		if (!memcmp(vbt_id, edid_id, sizeof(*vbt_id)))
695c518a775SVille Syrjälä 			return i;
696c518a775SVille Syrjälä 
697c518a775SVille Syrjälä 		/*
698c518a775SVille Syrjälä 		 * Accept a match w/o date if no full match is found,
699c518a775SVille Syrjälä 		 * and the VBT entry does not specify a date.
700c518a775SVille Syrjälä 		 */
701c518a775SVille Syrjälä 		if (best < 0 &&
702c518a775SVille Syrjälä 		    !memcmp(vbt_id, &edid_id_nodate, sizeof(*vbt_id)))
703c518a775SVille Syrjälä 			best = i;
704c518a775SVille Syrjälä 	}
705c518a775SVille Syrjälä 
706c518a775SVille Syrjälä 	return best;
707c518a775SVille Syrjälä }
708c518a775SVille Syrjälä 
709c518a775SVille Syrjälä static int fallback_get_panel_type(struct drm_i915_private *i915,
7106434cf63SAnimesh Manna 				   const struct intel_bios_encoder_data *devdata,
711c36225a1SJani Nikula 				   const struct drm_edid *drm_edid, bool use_fallback)
712cc589f2dSVille Syrjälä {
7133f9ffce5SVille Syrjälä 	return use_fallback ? 0 : -1;
714cc589f2dSVille Syrjälä }
715cc589f2dSVille Syrjälä 
716cc589f2dSVille Syrjälä enum panel_type {
717cc589f2dSVille Syrjälä 	PANEL_TYPE_OPREGION,
718cc589f2dSVille Syrjälä 	PANEL_TYPE_VBT,
719c518a775SVille Syrjälä 	PANEL_TYPE_PNPID,
720cc589f2dSVille Syrjälä 	PANEL_TYPE_FALLBACK,
721cc589f2dSVille Syrjälä };
722cc589f2dSVille Syrjälä 
723c518a775SVille Syrjälä static int get_panel_type(struct drm_i915_private *i915,
7246434cf63SAnimesh Manna 			  const struct intel_bios_encoder_data *devdata,
725c36225a1SJani Nikula 			  const struct drm_edid *drm_edid, bool use_fallback)
726719f4c51SVille Syrjälä {
727cc589f2dSVille Syrjälä 	struct {
728cc589f2dSVille Syrjälä 		const char *name;
729c518a775SVille Syrjälä 		int (*get_panel_type)(struct drm_i915_private *i915,
7306434cf63SAnimesh Manna 				      const struct intel_bios_encoder_data *devdata,
731c36225a1SJani Nikula 				      const struct drm_edid *drm_edid, bool use_fallback);
732cc589f2dSVille Syrjälä 		int panel_type;
733cc589f2dSVille Syrjälä 	} panel_types[] = {
734cc589f2dSVille Syrjälä 		[PANEL_TYPE_OPREGION] = {
735cc589f2dSVille Syrjälä 			.name = "OpRegion",
736cc589f2dSVille Syrjälä 			.get_panel_type = opregion_get_panel_type,
737cc589f2dSVille Syrjälä 		},
738cc589f2dSVille Syrjälä 		[PANEL_TYPE_VBT] = {
739cc589f2dSVille Syrjälä 			.name = "VBT",
740cc589f2dSVille Syrjälä 			.get_panel_type = vbt_get_panel_type,
741cc589f2dSVille Syrjälä 		},
742c518a775SVille Syrjälä 		[PANEL_TYPE_PNPID] = {
743c518a775SVille Syrjälä 			.name = "PNPID",
744c518a775SVille Syrjälä 			.get_panel_type = pnpid_get_panel_type,
745c518a775SVille Syrjälä 		},
746cc589f2dSVille Syrjälä 		[PANEL_TYPE_FALLBACK] = {
747cc589f2dSVille Syrjälä 			.name = "fallback",
748cc589f2dSVille Syrjälä 			.get_panel_type = fallback_get_panel_type,
749cc589f2dSVille Syrjälä 		},
750cc589f2dSVille Syrjälä 	};
751cc589f2dSVille Syrjälä 	int i;
752719f4c51SVille Syrjälä 
753cc589f2dSVille Syrjälä 	for (i = 0; i < ARRAY_SIZE(panel_types); i++) {
7543f9ffce5SVille Syrjälä 		panel_types[i].panel_type = panel_types[i].get_panel_type(i915, devdata,
755c36225a1SJani Nikula 									  drm_edid, use_fallback);
756cc589f2dSVille Syrjälä 
757c518a775SVille Syrjälä 		drm_WARN_ON(&i915->drm, panel_types[i].panel_type > 0xf &&
758c518a775SVille Syrjälä 			    panel_types[i].panel_type != 0xff);
759cc589f2dSVille Syrjälä 
760cc589f2dSVille Syrjälä 		if (panel_types[i].panel_type >= 0)
761cc589f2dSVille Syrjälä 			drm_dbg_kms(&i915->drm, "Panel type (%s): %d\n",
762cc589f2dSVille Syrjälä 				    panel_types[i].name, panel_types[i].panel_type);
763719f4c51SVille Syrjälä 	}
764719f4c51SVille Syrjälä 
765cc589f2dSVille Syrjälä 	if (panel_types[PANEL_TYPE_OPREGION].panel_type >= 0)
766cc589f2dSVille Syrjälä 		i = PANEL_TYPE_OPREGION;
767c518a775SVille Syrjälä 	else if (panel_types[PANEL_TYPE_VBT].panel_type == 0xff &&
768c518a775SVille Syrjälä 		 panel_types[PANEL_TYPE_PNPID].panel_type >= 0)
769c518a775SVille Syrjälä 		i = PANEL_TYPE_PNPID;
770c518a775SVille Syrjälä 	else if (panel_types[PANEL_TYPE_VBT].panel_type != 0xff &&
771c518a775SVille Syrjälä 		 panel_types[PANEL_TYPE_VBT].panel_type >= 0)
772cc589f2dSVille Syrjälä 		i = PANEL_TYPE_VBT;
773cc589f2dSVille Syrjälä 	else
774cc589f2dSVille Syrjälä 		i = PANEL_TYPE_FALLBACK;
775719f4c51SVille Syrjälä 
776cc589f2dSVille Syrjälä 	drm_dbg_kms(&i915->drm, "Selected panel type (%s): %d\n",
777cc589f2dSVille Syrjälä 		    panel_types[i].name, panel_types[i].panel_type);
778cc589f2dSVille Syrjälä 
779cc589f2dSVille Syrjälä 	return panel_types[i].panel_type;
780719f4c51SVille Syrjälä }
781719f4c51SVille Syrjälä 
782a50cc495SVille Syrjälä static unsigned int panel_bits(unsigned int value, int panel_type, int num_bits)
783a50cc495SVille Syrjälä {
784a50cc495SVille Syrjälä 	return (value >> (panel_type * num_bits)) & (BIT(num_bits) - 1);
785a50cc495SVille Syrjälä }
786a50cc495SVille Syrjälä 
787a50cc495SVille Syrjälä static bool panel_bool(unsigned int value, int panel_type)
788a50cc495SVille Syrjälä {
789a50cc495SVille Syrjälä 	return panel_bits(value, panel_type, 1);
790a50cc495SVille Syrjälä }
791a50cc495SVille Syrjälä 
7929e7ecedfSMatt Roper /* Parse general panel options */
793df0566a6SJani Nikula static void
7943cf05076SVille Syrjälä parse_panel_options(struct drm_i915_private *i915,
7950256ea13SVille Syrjälä 		    struct intel_panel *panel)
796df0566a6SJani Nikula {
797df0566a6SJani Nikula 	const struct bdb_lvds_options *lvds_options;
7980256ea13SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
799df0566a6SJani Nikula 	int drrs_mode;
800df0566a6SJani Nikula 
8010a93eeb5SMaarten Lankhorst 	lvds_options = bdb_find_section(i915, BDB_LVDS_OPTIONS);
802df0566a6SJani Nikula 	if (!lvds_options)
803df0566a6SJani Nikula 		return;
804df0566a6SJani Nikula 
8053cf05076SVille Syrjälä 	panel->vbt.lvds_dither = lvds_options->pixel_dither;
806df0566a6SJani Nikula 
8075c9016b2SVille Syrjälä 	/*
8085c9016b2SVille Syrjälä 	 * Empirical evidence indicates the block size can be
8095c9016b2SVille Syrjälä 	 * either 4,14,16,24+ bytes. For older VBTs no clear
8105c9016b2SVille Syrjälä 	 * relationship between the block size vs. BDB version.
8115c9016b2SVille Syrjälä 	 */
8125c9016b2SVille Syrjälä 	if (get_blocksize(lvds_options) < 16)
8135c9016b2SVille Syrjälä 		return;
814df0566a6SJani Nikula 
815a50cc495SVille Syrjälä 	drrs_mode = panel_bits(lvds_options->dps_panel_type_bits,
816a50cc495SVille Syrjälä 			       panel_type, 2);
817df0566a6SJani Nikula 	/*
818df0566a6SJani Nikula 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
819df0566a6SJani Nikula 	 * The below piece of code is required to adjust vbt.drrs_type
820df0566a6SJani Nikula 	 * to match the enum drrs_support_type.
821df0566a6SJani Nikula 	 */
822df0566a6SJani Nikula 	switch (drrs_mode) {
823df0566a6SJani Nikula 	case 0:
8243cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_STATIC;
825dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS supported mode is static\n");
826df0566a6SJani Nikula 		break;
827df0566a6SJani Nikula 	case 2:
8283cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_SEAMLESS;
829dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
830e92cbf38SWambui Karuga 			    "DRRS supported mode is seamless\n");
831df0566a6SJani Nikula 		break;
832df0566a6SJani Nikula 	default:
8333cf05076SVille Syrjälä 		panel->vbt.drrs_type = DRRS_TYPE_NONE;
834dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
835e92cbf38SWambui Karuga 			    "DRRS not supported (VBT input)\n");
836df0566a6SJani Nikula 		break;
837df0566a6SJani Nikula 	}
8389e7ecedfSMatt Roper }
8399e7ecedfSMatt Roper 
8409e7ecedfSMatt Roper static void
84113367132SVille Syrjälä parse_lfp_panel_dtd(struct drm_i915_private *i915,
8423cf05076SVille Syrjälä 		    struct intel_panel *panel,
84313367132SVille Syrjälä 		    const struct bdb_lvds_lfp_data *lvds_lfp_data,
84413367132SVille Syrjälä 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs)
8459e7ecedfSMatt Roper {
8469e7ecedfSMatt Roper 	const struct lvds_dvo_timing *panel_dvo_timing;
8479e7ecedfSMatt Roper 	const struct lvds_fp_timing *fp_timing;
8489e7ecedfSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
8493cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
850df0566a6SJani Nikula 
851df0566a6SJani Nikula 	panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
852df0566a6SJani Nikula 					       lvds_lfp_data_ptrs,
853df0566a6SJani Nikula 					       panel_type);
854df0566a6SJani Nikula 
855df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
856df0566a6SJani Nikula 	if (!panel_fixed_mode)
857df0566a6SJani Nikula 		return;
858df0566a6SJani Nikula 
859bb6f53d4SVille Syrjälä 	fill_detail_timing_data(i915, panel_fixed_mode, panel_dvo_timing);
860df0566a6SJani Nikula 
8613cf05076SVille Syrjälä 	panel->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
862df0566a6SJani Nikula 
863dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
864f01bae2dSVille Syrjälä 		    "Found panel mode in BIOS VBT legacy lfp table: " DRM_MODE_FMT "\n",
865f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
866df0566a6SJani Nikula 
867918f3025SVille Syrjälä 	fp_timing = get_lvds_fp_timing(lvds_lfp_data,
868df0566a6SJani Nikula 				       lvds_lfp_data_ptrs,
869df0566a6SJani Nikula 				       panel_type);
87058b2e382SVille Syrjälä 
871df0566a6SJani Nikula 	/* check the resolution, just to be sure */
872df0566a6SJani Nikula 	if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
873df0566a6SJani Nikula 	    fp_timing->y_res == panel_fixed_mode->vdisplay) {
8743cf05076SVille Syrjälä 		panel->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
875dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
876e92cbf38SWambui Karuga 			    "VBT initial LVDS value %x\n",
8773cf05076SVille Syrjälä 			    panel->vbt.bios_lvds_val);
878df0566a6SJani Nikula 	}
879df0566a6SJani Nikula }
880df0566a6SJani Nikula 
881df0566a6SJani Nikula static void
8823cf05076SVille Syrjälä parse_lfp_data(struct drm_i915_private *i915,
8833cf05076SVille Syrjälä 	       struct intel_panel *panel)
88413367132SVille Syrjälä {
88513367132SVille Syrjälä 	const struct bdb_lvds_lfp_data *data;
886901a0cadSVille Syrjälä 	const struct bdb_lvds_lfp_data_tail *tail;
88713367132SVille Syrjälä 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
88806bfa86eSVille Syrjälä 	const struct lvds_pnp_id *pnp_id;
8893cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
89013367132SVille Syrjälä 
8910a93eeb5SMaarten Lankhorst 	ptrs = bdb_find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
89213367132SVille Syrjälä 	if (!ptrs)
89313367132SVille Syrjälä 		return;
89413367132SVille Syrjälä 
8950a93eeb5SMaarten Lankhorst 	data = bdb_find_section(i915, BDB_LVDS_LFP_DATA);
89613367132SVille Syrjälä 	if (!data)
89713367132SVille Syrjälä 		return;
89813367132SVille Syrjälä 
8993cf05076SVille Syrjälä 	if (!panel->vbt.lfp_lvds_vbt_mode)
9003cf05076SVille Syrjälä 		parse_lfp_panel_dtd(i915, panel, data, ptrs);
901901a0cadSVille Syrjälä 
90206bfa86eSVille Syrjälä 	pnp_id = get_lvds_pnp_id(data, ptrs, panel_type);
90306bfa86eSVille Syrjälä 	dump_pnp_id(i915, pnp_id, "Panel");
90406bfa86eSVille Syrjälä 
905901a0cadSVille Syrjälä 	tail = get_lfp_data_tail(data, ptrs);
906901a0cadSVille Syrjälä 	if (!tail)
907901a0cadSVille Syrjälä 		return;
908901a0cadSVille Syrjälä 
90906bfa86eSVille Syrjälä 	drm_dbg_kms(&i915->drm, "Panel name: %.*s\n",
91006bfa86eSVille Syrjälä 		    (int)sizeof(tail->panel_name[0].name),
91106bfa86eSVille Syrjälä 		    tail->panel_name[panel_type].name);
91206bfa86eSVille Syrjälä 
913a434689cSJani Nikula 	if (i915->display.vbt.version >= 188) {
9143cf05076SVille Syrjälä 		panel->vbt.seamless_drrs_min_refresh_rate =
915790b45f1SVille Syrjälä 			tail->seamless_drrs_min_refresh_rate[panel_type];
916790b45f1SVille Syrjälä 		drm_dbg_kms(&i915->drm,
917790b45f1SVille Syrjälä 			    "Seamless DRRS min refresh rate: %d Hz\n",
9183cf05076SVille Syrjälä 			    panel->vbt.seamless_drrs_min_refresh_rate);
919790b45f1SVille Syrjälä 	}
92013367132SVille Syrjälä }
92113367132SVille Syrjälä 
92213367132SVille Syrjälä static void
9233cf05076SVille Syrjälä parse_generic_dtd(struct drm_i915_private *i915,
9243cf05076SVille Syrjälä 		  struct intel_panel *panel)
92533ef6d4fSMatt Roper {
92633ef6d4fSMatt Roper 	const struct bdb_generic_dtd *generic_dtd;
92733ef6d4fSMatt Roper 	const struct generic_dtd_entry *dtd;
92833ef6d4fSMatt Roper 	struct drm_display_mode *panel_fixed_mode;
92933ef6d4fSMatt Roper 	int num_dtd;
93033ef6d4fSMatt Roper 
93113367132SVille Syrjälä 	/*
93213367132SVille Syrjälä 	 * Older VBTs provided DTD information for internal displays through
93313367132SVille Syrjälä 	 * the "LFP panel tables" block (42).  As of VBT revision 229 the
93413367132SVille Syrjälä 	 * DTD information should be provided via a newer "generic DTD"
93513367132SVille Syrjälä 	 * block (58).  Just to be safe, we'll try the new generic DTD block
93613367132SVille Syrjälä 	 * first on VBT >= 229, but still fall back to trying the old LFP
93713367132SVille Syrjälä 	 * block if that fails.
93813367132SVille Syrjälä 	 */
939a434689cSJani Nikula 	if (i915->display.vbt.version < 229)
94013367132SVille Syrjälä 		return;
94113367132SVille Syrjälä 
9420a93eeb5SMaarten Lankhorst 	generic_dtd = bdb_find_section(i915, BDB_GENERIC_DTD);
94333ef6d4fSMatt Roper 	if (!generic_dtd)
94433ef6d4fSMatt Roper 		return;
94533ef6d4fSMatt Roper 
94633ef6d4fSMatt Roper 	if (generic_dtd->gdtd_size < sizeof(struct generic_dtd_entry)) {
947dbd440d8SJani Nikula 		drm_err(&i915->drm, "GDTD size %u is too small.\n",
94833ef6d4fSMatt Roper 			generic_dtd->gdtd_size);
94933ef6d4fSMatt Roper 		return;
95033ef6d4fSMatt Roper 	} else if (generic_dtd->gdtd_size !=
95133ef6d4fSMatt Roper 		   sizeof(struct generic_dtd_entry)) {
952dbd440d8SJani Nikula 		drm_err(&i915->drm, "Unexpected GDTD size %u\n",
953e92cbf38SWambui Karuga 			generic_dtd->gdtd_size);
95433ef6d4fSMatt Roper 		/* DTD has unknown fields, but keep going */
95533ef6d4fSMatt Roper 	}
95633ef6d4fSMatt Roper 
95733ef6d4fSMatt Roper 	num_dtd = (get_blocksize(generic_dtd) -
95833ef6d4fSMatt Roper 		   sizeof(struct bdb_generic_dtd)) / generic_dtd->gdtd_size;
9593cf05076SVille Syrjälä 	if (panel->vbt.panel_type >= num_dtd) {
960dbd440d8SJani Nikula 		drm_err(&i915->drm,
961e92cbf38SWambui Karuga 			"Panel type %d not found in table of %d DTD's\n",
9623cf05076SVille Syrjälä 			panel->vbt.panel_type, num_dtd);
96333ef6d4fSMatt Roper 		return;
96433ef6d4fSMatt Roper 	}
96533ef6d4fSMatt Roper 
9663cf05076SVille Syrjälä 	dtd = &generic_dtd->dtd[panel->vbt.panel_type];
96733ef6d4fSMatt Roper 
96833ef6d4fSMatt Roper 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
96933ef6d4fSMatt Roper 	if (!panel_fixed_mode)
97033ef6d4fSMatt Roper 		return;
97133ef6d4fSMatt Roper 
97233ef6d4fSMatt Roper 	panel_fixed_mode->hdisplay = dtd->hactive;
97333ef6d4fSMatt Roper 	panel_fixed_mode->hsync_start =
97433ef6d4fSMatt Roper 		panel_fixed_mode->hdisplay + dtd->hfront_porch;
97533ef6d4fSMatt Roper 	panel_fixed_mode->hsync_end =
97633ef6d4fSMatt Roper 		panel_fixed_mode->hsync_start + dtd->hsync;
977ad278f35SVandita Kulkarni 	panel_fixed_mode->htotal =
978ad278f35SVandita Kulkarni 		panel_fixed_mode->hdisplay + dtd->hblank;
97933ef6d4fSMatt Roper 
98033ef6d4fSMatt Roper 	panel_fixed_mode->vdisplay = dtd->vactive;
98133ef6d4fSMatt Roper 	panel_fixed_mode->vsync_start =
98233ef6d4fSMatt Roper 		panel_fixed_mode->vdisplay + dtd->vfront_porch;
98333ef6d4fSMatt Roper 	panel_fixed_mode->vsync_end =
98433ef6d4fSMatt Roper 		panel_fixed_mode->vsync_start + dtd->vsync;
985ad278f35SVandita Kulkarni 	panel_fixed_mode->vtotal =
986ad278f35SVandita Kulkarni 		panel_fixed_mode->vdisplay + dtd->vblank;
98733ef6d4fSMatt Roper 
98833ef6d4fSMatt Roper 	panel_fixed_mode->clock = dtd->pixel_clock;
98933ef6d4fSMatt Roper 	panel_fixed_mode->width_mm = dtd->width_mm;
99033ef6d4fSMatt Roper 	panel_fixed_mode->height_mm = dtd->height_mm;
99133ef6d4fSMatt Roper 
99233ef6d4fSMatt Roper 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
99333ef6d4fSMatt Roper 	drm_mode_set_name(panel_fixed_mode);
99433ef6d4fSMatt Roper 
99533ef6d4fSMatt Roper 	if (dtd->hsync_positive_polarity)
99633ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
99733ef6d4fSMatt Roper 	else
99833ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
99933ef6d4fSMatt Roper 
100033ef6d4fSMatt Roper 	if (dtd->vsync_positive_polarity)
100133ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
100233ef6d4fSMatt Roper 	else
100333ef6d4fSMatt Roper 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
100433ef6d4fSMatt Roper 
1005dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1006f01bae2dSVille Syrjälä 		    "Found panel mode in BIOS VBT generic dtd table: " DRM_MODE_FMT "\n",
1007f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
100833ef6d4fSMatt Roper 
10093cf05076SVille Syrjälä 	panel->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
101033ef6d4fSMatt Roper }
101133ef6d4fSMatt Roper 
101233ef6d4fSMatt Roper static void
10133cf05076SVille Syrjälä parse_lfp_backlight(struct drm_i915_private *i915,
10143cf05076SVille Syrjälä 		    struct intel_panel *panel)
1015df0566a6SJani Nikula {
1016df0566a6SJani Nikula 	const struct bdb_lfp_backlight_data *backlight_data;
1017df0566a6SJani Nikula 	const struct lfp_backlight_data_entry *entry;
10183cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1019d381baadSJosé Roberto de Souza 	u16 level;
1020df0566a6SJani Nikula 
10210a93eeb5SMaarten Lankhorst 	backlight_data = bdb_find_section(i915, BDB_LVDS_BACKLIGHT);
1022df0566a6SJani Nikula 	if (!backlight_data)
1023df0566a6SJani Nikula 		return;
1024df0566a6SJani Nikula 
1025df0566a6SJani Nikula 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
1026dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1027e92cbf38SWambui Karuga 			    "Unsupported backlight data entry size %u\n",
1028df0566a6SJani Nikula 			    backlight_data->entry_size);
1029df0566a6SJani Nikula 		return;
1030df0566a6SJani Nikula 	}
1031df0566a6SJani Nikula 
1032df0566a6SJani Nikula 	entry = &backlight_data->data[panel_type];
1033df0566a6SJani Nikula 
10343cf05076SVille Syrjälä 	panel->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
10353cf05076SVille Syrjälä 	if (!panel->vbt.backlight.present) {
1036dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1037e92cbf38SWambui Karuga 			    "PWM backlight not present in VBT (type %u)\n",
1038df0566a6SJani Nikula 			    entry->type);
1039df0566a6SJani Nikula 		return;
1040df0566a6SJani Nikula 	}
1041df0566a6SJani Nikula 
10423cf05076SVille Syrjälä 	panel->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
1043a0dcb06dSJani Nikula 	panel->vbt.backlight.controller = 0;
1044a434689cSJani Nikula 	if (i915->display.vbt.version >= 191) {
10454378daf5SLukasz Majczak 		size_t exp_size;
10464378daf5SLukasz Majczak 
1047a434689cSJani Nikula 		if (i915->display.vbt.version >= 236)
10484378daf5SLukasz Majczak 			exp_size = sizeof(struct bdb_lfp_backlight_data);
1049a434689cSJani Nikula 		else if (i915->display.vbt.version >= 234)
10504378daf5SLukasz Majczak 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_234;
10514378daf5SLukasz Majczak 		else
10524378daf5SLukasz Majczak 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_191;
10534378daf5SLukasz Majczak 
10544378daf5SLukasz Majczak 		if (get_blocksize(backlight_data) >= exp_size) {
1055df0566a6SJani Nikula 			const struct lfp_backlight_control_method *method;
1056df0566a6SJani Nikula 
1057df0566a6SJani Nikula 			method = &backlight_data->backlight_control[panel_type];
10583cf05076SVille Syrjälä 			panel->vbt.backlight.type = method->type;
10593cf05076SVille Syrjälä 			panel->vbt.backlight.controller = method->controller;
1060df0566a6SJani Nikula 		}
10614378daf5SLukasz Majczak 	}
1062df0566a6SJani Nikula 
10633cf05076SVille Syrjälä 	panel->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
10643cf05076SVille Syrjälä 	panel->vbt.backlight.active_low_pwm = entry->active_low_pwm;
1065d381baadSJosé Roberto de Souza 
1066a434689cSJani Nikula 	if (i915->display.vbt.version >= 234) {
1067d381baadSJosé Roberto de Souza 		u16 min_level;
1068d381baadSJosé Roberto de Souza 		bool scale;
1069d381baadSJosé Roberto de Souza 
1070d381baadSJosé Roberto de Souza 		level = backlight_data->brightness_level[panel_type].level;
1071d381baadSJosé Roberto de Souza 		min_level = backlight_data->brightness_min_level[panel_type].level;
1072d381baadSJosé Roberto de Souza 
1073a434689cSJani Nikula 		if (i915->display.vbt.version >= 236)
1074d381baadSJosé Roberto de Souza 			scale = backlight_data->brightness_precision_bits[panel_type] == 16;
1075d381baadSJosé Roberto de Souza 		else
1076d381baadSJosé Roberto de Souza 			scale = level > 255;
1077d381baadSJosé Roberto de Souza 
1078d381baadSJosé Roberto de Souza 		if (scale)
1079d381baadSJosé Roberto de Souza 			min_level = min_level / 255;
1080d381baadSJosé Roberto de Souza 
1081d381baadSJosé Roberto de Souza 		if (min_level > 255) {
1082dbd440d8SJani Nikula 			drm_warn(&i915->drm, "Brightness min level > 255\n");
1083d381baadSJosé Roberto de Souza 			level = 255;
1084d381baadSJosé Roberto de Souza 		}
10853cf05076SVille Syrjälä 		panel->vbt.backlight.min_brightness = min_level;
108684d3d71fSLee Shawn C 
10873cf05076SVille Syrjälä 		panel->vbt.backlight.brightness_precision_bits =
108884d3d71fSLee Shawn C 			backlight_data->brightness_precision_bits[panel_type];
1089d381baadSJosé Roberto de Souza 	} else {
1090d381baadSJosé Roberto de Souza 		level = backlight_data->level[panel_type];
10913cf05076SVille Syrjälä 		panel->vbt.backlight.min_brightness = entry->min_brightness;
1092d381baadSJosé Roberto de Souza 	}
1093d381baadSJosé Roberto de Souza 
1094fe82b93fSVille Syrjälä 	if (i915->display.vbt.version >= 239)
1095fe82b93fSVille Syrjälä 		panel->vbt.backlight.hdr_dpcd_refresh_timeout =
1096fe82b93fSVille Syrjälä 			DIV_ROUND_UP(backlight_data->hdr_dpcd_refresh_timeout[panel_type], 100);
1097fe82b93fSVille Syrjälä 	else
1098fe82b93fSVille Syrjälä 		panel->vbt.backlight.hdr_dpcd_refresh_timeout = 30;
1099fe82b93fSVille Syrjälä 
1100dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1101e92cbf38SWambui Karuga 		    "VBT backlight PWM modulation frequency %u Hz, "
1102df0566a6SJani Nikula 		    "active %s, min brightness %u, level %u, controller %u\n",
11033cf05076SVille Syrjälä 		    panel->vbt.backlight.pwm_freq_hz,
11043cf05076SVille Syrjälä 		    panel->vbt.backlight.active_low_pwm ? "low" : "high",
11053cf05076SVille Syrjälä 		    panel->vbt.backlight.min_brightness,
1106d381baadSJosé Roberto de Souza 		    level,
11073cf05076SVille Syrjälä 		    panel->vbt.backlight.controller);
1108df0566a6SJani Nikula }
1109df0566a6SJani Nikula 
1110df0566a6SJani Nikula /* Try to find sdvo panel data */
1111df0566a6SJani Nikula static void
11123cf05076SVille Syrjälä parse_sdvo_panel_data(struct drm_i915_private *i915,
11133cf05076SVille Syrjälä 		      struct intel_panel *panel)
1114df0566a6SJani Nikula {
1115df0566a6SJani Nikula 	const struct bdb_sdvo_panel_dtds *dtds;
1116df0566a6SJani Nikula 	struct drm_display_mode *panel_fixed_mode;
1117df0566a6SJani Nikula 	int index;
1118df0566a6SJani Nikula 
11195fb2e673SJouni Högander 	index = i915->display.params.vbt_sdvo_panel_type;
1120df0566a6SJani Nikula 	if (index == -2) {
1121dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1122e92cbf38SWambui Karuga 			    "Ignore SDVO panel mode from BIOS VBT tables.\n");
1123df0566a6SJani Nikula 		return;
1124df0566a6SJani Nikula 	}
1125df0566a6SJani Nikula 
1126df0566a6SJani Nikula 	if (index == -1) {
1127df0566a6SJani Nikula 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
1128df0566a6SJani Nikula 
11290a93eeb5SMaarten Lankhorst 		sdvo_lvds_options = bdb_find_section(i915, BDB_SDVO_LVDS_OPTIONS);
1130df0566a6SJani Nikula 		if (!sdvo_lvds_options)
1131df0566a6SJani Nikula 			return;
1132df0566a6SJani Nikula 
1133df0566a6SJani Nikula 		index = sdvo_lvds_options->panel_type;
1134df0566a6SJani Nikula 	}
1135df0566a6SJani Nikula 
11360a93eeb5SMaarten Lankhorst 	dtds = bdb_find_section(i915, BDB_SDVO_PANEL_DTDS);
1137df0566a6SJani Nikula 	if (!dtds)
1138df0566a6SJani Nikula 		return;
1139df0566a6SJani Nikula 
1140df0566a6SJani Nikula 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
1141df0566a6SJani Nikula 	if (!panel_fixed_mode)
1142df0566a6SJani Nikula 		return;
1143df0566a6SJani Nikula 
1144bb6f53d4SVille Syrjälä 	fill_detail_timing_data(i915, panel_fixed_mode, &dtds->dtds[index]);
1145df0566a6SJani Nikula 
11463cf05076SVille Syrjälä 	panel->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
1147df0566a6SJani Nikula 
1148dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1149f01bae2dSVille Syrjälä 		    "Found SDVO panel mode in BIOS VBT tables: " DRM_MODE_FMT "\n",
1150f01bae2dSVille Syrjälä 		    DRM_MODE_ARG(panel_fixed_mode));
1151df0566a6SJani Nikula }
1152df0566a6SJani Nikula 
1153dbd440d8SJani Nikula static int intel_bios_ssc_frequency(struct drm_i915_private *i915,
1154df0566a6SJani Nikula 				    bool alternate)
1155df0566a6SJani Nikula {
1156005e9537SMatt Roper 	switch (DISPLAY_VER(i915)) {
1157df0566a6SJani Nikula 	case 2:
1158df0566a6SJani Nikula 		return alternate ? 66667 : 48000;
1159df0566a6SJani Nikula 	case 3:
1160df0566a6SJani Nikula 	case 4:
1161df0566a6SJani Nikula 		return alternate ? 100000 : 96000;
1162df0566a6SJani Nikula 	default:
1163df0566a6SJani Nikula 		return alternate ? 100000 : 120000;
1164df0566a6SJani Nikula 	}
1165df0566a6SJani Nikula }
1166df0566a6SJani Nikula 
1167df0566a6SJani Nikula static void
1168e163cfb4SVille Syrjälä parse_general_features(struct drm_i915_private *i915)
1169df0566a6SJani Nikula {
1170df0566a6SJani Nikula 	const struct bdb_general_features *general;
1171df0566a6SJani Nikula 
11720a93eeb5SMaarten Lankhorst 	general = bdb_find_section(i915, BDB_GENERAL_FEATURES);
1173df0566a6SJani Nikula 	if (!general)
1174df0566a6SJani Nikula 		return;
1175df0566a6SJani Nikula 
1176a434689cSJani Nikula 	i915->display.vbt.int_tv_support = general->int_tv_support;
1177df0566a6SJani Nikula 	/* int_crt_support can't be trusted on earlier platforms */
1178a434689cSJani Nikula 	if (i915->display.vbt.version >= 155 &&
1179dbd440d8SJani Nikula 	    (HAS_DDI(i915) || IS_VALLEYVIEW(i915)))
1180a434689cSJani Nikula 		i915->display.vbt.int_crt_support = general->int_crt_support;
1181a434689cSJani Nikula 	i915->display.vbt.lvds_use_ssc = general->enable_ssc;
1182a434689cSJani Nikula 	i915->display.vbt.lvds_ssc_freq =
1183dbd440d8SJani Nikula 		intel_bios_ssc_frequency(i915, general->ssc_freq);
1184a434689cSJani Nikula 	i915->display.vbt.display_clock_mode = general->display_clock_mode;
1185a434689cSJani Nikula 	i915->display.vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
1186a434689cSJani Nikula 	if (i915->display.vbt.version >= 181) {
1187a434689cSJani Nikula 		i915->display.vbt.orientation = general->rotate_180 ?
1188df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
1189df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_NORMAL;
1190df0566a6SJani Nikula 	} else {
1191a434689cSJani Nikula 		i915->display.vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1192df0566a6SJani Nikula 	}
1193b70ad01aSJosé Roberto de Souza 
1194a434689cSJani Nikula 	if (i915->display.vbt.version >= 249 && general->afc_startup_config) {
1195a434689cSJani Nikula 		i915->display.vbt.override_afc_startup = true;
1196a434689cSJani Nikula 		i915->display.vbt.override_afc_startup_val = general->afc_startup_config == 0x1 ? 0x0 : 0x7;
1197b70ad01aSJosé Roberto de Souza 	}
1198b70ad01aSJosé Roberto de Souza 
1199dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1200e92cbf38SWambui 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",
1201a434689cSJani Nikula 		    i915->display.vbt.int_tv_support,
1202a434689cSJani Nikula 		    i915->display.vbt.int_crt_support,
1203a434689cSJani Nikula 		    i915->display.vbt.lvds_use_ssc,
1204a434689cSJani Nikula 		    i915->display.vbt.lvds_ssc_freq,
1205a434689cSJani Nikula 		    i915->display.vbt.display_clock_mode,
1206a434689cSJani Nikula 		    i915->display.vbt.fdi_rx_polarity_inverted);
1207df0566a6SJani Nikula }
1208df0566a6SJani Nikula 
1209df0566a6SJani Nikula static const struct child_device_config *
1210df0566a6SJani Nikula child_device_ptr(const struct bdb_general_definitions *defs, int i)
1211df0566a6SJani Nikula {
1212df0566a6SJani Nikula 	return (const void *) &defs->devices[i * defs->child_dev_size];
1213df0566a6SJani Nikula }
1214df0566a6SJani Nikula 
1215df0566a6SJani Nikula static void
1216ef0096e4SJani Nikula parse_sdvo_device_mapping(struct drm_i915_private *i915)
1217df0566a6SJani Nikula {
12183162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
12190d9ef19bSJani Nikula 	int count = 0;
1220df0566a6SJani Nikula 
1221df0566a6SJani Nikula 	/*
1222df0566a6SJani Nikula 	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
1223df0566a6SJani Nikula 	 * accurate and doesn't have to be, as long as it's not too strict.
1224df0566a6SJani Nikula 	 */
122593e7e61eSLucas De Marchi 	if (!IS_DISPLAY_VER(i915, 3, 7)) {
1226dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "Skipping SDVO device mapping\n");
1227df0566a6SJani Nikula 		return;
1228df0566a6SJani Nikula 	}
1229df0566a6SJani Nikula 
1230a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
1231d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
1232d24b3475SVille Syrjälä 		struct sdvo_device_mapping *mapping;
1233df0566a6SJani Nikula 
1234df0566a6SJani Nikula 		if (child->slave_addr != SLAVE_ADDR1 &&
1235df0566a6SJani Nikula 		    child->slave_addr != SLAVE_ADDR2) {
1236df0566a6SJani Nikula 			/*
1237df0566a6SJani Nikula 			 * If the slave address is neither 0x70 nor 0x72,
1238df0566a6SJani Nikula 			 * it is not a SDVO device. Skip it.
1239df0566a6SJani Nikula 			 */
1240df0566a6SJani Nikula 			continue;
1241df0566a6SJani Nikula 		}
1242df0566a6SJani Nikula 		if (child->dvo_port != DEVICE_PORT_DVOB &&
1243df0566a6SJani Nikula 		    child->dvo_port != DEVICE_PORT_DVOC) {
1244df0566a6SJani Nikula 			/* skip the incorrect SDVO port */
1245dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1246e92cbf38SWambui Karuga 				    "Incorrect SDVO port. Skip it\n");
1247df0566a6SJani Nikula 			continue;
1248df0566a6SJani Nikula 		}
1249dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1250e92cbf38SWambui Karuga 			    "the SDVO device with slave addr %2x is found on"
1251df0566a6SJani Nikula 			    " %s port\n",
1252df0566a6SJani Nikula 			    child->slave_addr,
1253df0566a6SJani Nikula 			    (child->dvo_port == DEVICE_PORT_DVOB) ?
1254df0566a6SJani Nikula 			    "SDVOB" : "SDVOC");
1255a434689cSJani Nikula 		mapping = &i915->display.vbt.sdvo_mappings[child->dvo_port - 1];
1256df0566a6SJani Nikula 		if (!mapping->initialized) {
1257df0566a6SJani Nikula 			mapping->dvo_port = child->dvo_port;
1258df0566a6SJani Nikula 			mapping->slave_addr = child->slave_addr;
1259df0566a6SJani Nikula 			mapping->dvo_wiring = child->dvo_wiring;
1260df0566a6SJani Nikula 			mapping->ddc_pin = child->ddc_pin;
1261df0566a6SJani Nikula 			mapping->i2c_pin = child->i2c_pin;
1262df0566a6SJani Nikula 			mapping->initialized = 1;
1263dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1264e92cbf38SWambui Karuga 				    "SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
1265e92cbf38SWambui Karuga 				    mapping->dvo_port, mapping->slave_addr,
1266e92cbf38SWambui Karuga 				    mapping->dvo_wiring, mapping->ddc_pin,
1267df0566a6SJani Nikula 				    mapping->i2c_pin);
1268df0566a6SJani Nikula 		} else {
1269dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1270e92cbf38SWambui Karuga 				    "Maybe one SDVO port is shared by "
1271df0566a6SJani Nikula 				    "two SDVO device.\n");
1272df0566a6SJani Nikula 		}
1273df0566a6SJani Nikula 		if (child->slave2_addr) {
1274df0566a6SJani Nikula 			/* Maybe this is a SDVO device with multiple inputs */
1275df0566a6SJani Nikula 			/* And the mapping info is not added */
1276dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1277e92cbf38SWambui Karuga 				    "there exists the slave2_addr. Maybe this"
1278df0566a6SJani Nikula 				    " is a SDVO device with multiple inputs.\n");
1279df0566a6SJani Nikula 		}
1280df0566a6SJani Nikula 		count++;
1281df0566a6SJani Nikula 	}
1282df0566a6SJani Nikula 
1283df0566a6SJani Nikula 	if (!count) {
1284df0566a6SJani Nikula 		/* No SDVO device info is found */
1285dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1286e92cbf38SWambui Karuga 			    "No SDVO device info is found in VBT\n");
1287df0566a6SJani Nikula 	}
1288df0566a6SJani Nikula }
1289df0566a6SJani Nikula 
1290df0566a6SJani Nikula static void
1291e163cfb4SVille Syrjälä parse_driver_features(struct drm_i915_private *i915)
1292df0566a6SJani Nikula {
1293df0566a6SJani Nikula 	const struct bdb_driver_features *driver;
1294df0566a6SJani Nikula 
12950a93eeb5SMaarten Lankhorst 	driver = bdb_find_section(i915, BDB_DRIVER_FEATURES);
1296df0566a6SJani Nikula 	if (!driver)
1297df0566a6SJani Nikula 		return;
1298df0566a6SJani Nikula 
1299005e9537SMatt Roper 	if (DISPLAY_VER(i915) >= 5) {
1300df0566a6SJani Nikula 		/*
1301df0566a6SJani Nikula 		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
1302df0566a6SJani Nikula 		 * to mean "eDP". The VBT spec doesn't agree with that
1303df0566a6SJani Nikula 		 * interpretation, but real world VBTs seem to.
1304df0566a6SJani Nikula 		 */
1305df0566a6SJani Nikula 		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
1306a434689cSJani Nikula 			i915->display.vbt.int_lvds_support = 0;
1307df0566a6SJani Nikula 	} else {
1308df0566a6SJani Nikula 		/*
1309df0566a6SJani Nikula 		 * FIXME it's not clear which BDB version has the LVDS config
1310df0566a6SJani Nikula 		 * bits defined. Revision history in the VBT spec says:
1311df0566a6SJani Nikula 		 * "0.92 | Add two definitions for VBT value of LVDS Active
1312df0566a6SJani Nikula 		 *  Config (00b and 11b values defined) | 06/13/2005"
1313df0566a6SJani Nikula 		 * but does not the specify the BDB version.
1314df0566a6SJani Nikula 		 *
1315df0566a6SJani Nikula 		 * So far version 134 (on i945gm) is the oldest VBT observed
1316df0566a6SJani Nikula 		 * in the wild with the bits correctly populated. Version
1317df0566a6SJani Nikula 		 * 108 (on i85x) does not have the bits correctly populated.
1318df0566a6SJani Nikula 		 */
1319a434689cSJani Nikula 		if (i915->display.vbt.version >= 134 &&
1320df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
1321df0566a6SJani Nikula 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
1322a434689cSJani Nikula 			i915->display.vbt.int_lvds_support = 0;
1323df0566a6SJani Nikula 	}
1324c3fbcf60SVille Syrjälä }
1325c3fbcf60SVille Syrjälä 
1326c3fbcf60SVille Syrjälä static void
13273cf05076SVille Syrjälä parse_panel_driver_features(struct drm_i915_private *i915,
13283cf05076SVille Syrjälä 			    struct intel_panel *panel)
1329c3fbcf60SVille Syrjälä {
1330c3fbcf60SVille Syrjälä 	const struct bdb_driver_features *driver;
1331c3fbcf60SVille Syrjälä 
13320a93eeb5SMaarten Lankhorst 	driver = bdb_find_section(i915, BDB_DRIVER_FEATURES);
1333c3fbcf60SVille Syrjälä 	if (!driver)
1334c3fbcf60SVille Syrjälä 		return;
1335df0566a6SJani Nikula 
1336a434689cSJani Nikula 	if (i915->display.vbt.version < 228) {
1337dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "DRRS State Enabled:%d\n",
1338e92cbf38SWambui Karuga 			    driver->drrs_enabled);
1339df0566a6SJani Nikula 		/*
1340df0566a6SJani Nikula 		 * If DRRS is not supported, drrs_type has to be set to 0.
1341df0566a6SJani Nikula 		 * This is because, VBT is configured in such a way that
1342df0566a6SJani Nikula 		 * static DRRS is 0 and DRRS not supported is represented by
1343df0566a6SJani Nikula 		 * driver->drrs_enabled=false
1344df0566a6SJani Nikula 		 */
13455a18db2eSVille Syrjälä 		if (!driver->drrs_enabled && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
13465a18db2eSVille Syrjälä 			/*
13475a18db2eSVille Syrjälä 			 * FIXME Should DMRRS perhaps be treated as seamless
13485a18db2eSVille Syrjälä 			 * but without the automatic downclocking?
13495a18db2eSVille Syrjälä 			 */
13505a18db2eSVille Syrjälä 			if (driver->dmrrs_enabled)
13515a18db2eSVille Syrjälä 				panel->vbt.drrs_type = DRRS_TYPE_STATIC;
13525a18db2eSVille Syrjälä 			else
13533cf05076SVille Syrjälä 				panel->vbt.drrs_type = DRRS_TYPE_NONE;
13545a18db2eSVille Syrjälä 		}
1355551fb93dSJosé Roberto de Souza 
13563cf05076SVille Syrjälä 		panel->vbt.psr.enable = driver->psr_enabled;
1357df0566a6SJani Nikula 	}
1358551fb93dSJosé Roberto de Souza }
1359551fb93dSJosé Roberto de Souza 
1360551fb93dSJosé Roberto de Souza static void
13613cf05076SVille Syrjälä parse_power_conservation_features(struct drm_i915_private *i915,
13623cf05076SVille Syrjälä 				  struct intel_panel *panel)
1363551fb93dSJosé Roberto de Souza {
1364551fb93dSJosé Roberto de Souza 	const struct bdb_lfp_power *power;
13653cf05076SVille Syrjälä 	u8 panel_type = panel->vbt.panel_type;
1366551fb93dSJosé Roberto de Souza 
1367fba99b1aSVille Syrjälä 	panel->vbt.vrr = true; /* matches Windows behaviour */
1368551fb93dSJosé Roberto de Souza 
1369a434689cSJani Nikula 	if (i915->display.vbt.version < 228)
1370551fb93dSJosé Roberto de Souza 		return;
1371551fb93dSJosé Roberto de Souza 
13720a93eeb5SMaarten Lankhorst 	power = bdb_find_section(i915, BDB_LFP_POWER);
1373551fb93dSJosé Roberto de Souza 	if (!power)
1374551fb93dSJosé Roberto de Souza 		return;
1375551fb93dSJosé Roberto de Souza 
1376a50cc495SVille Syrjälä 	panel->vbt.psr.enable = panel_bool(power->psr, panel_type);
1377551fb93dSJosé Roberto de Souza 
1378551fb93dSJosé Roberto de Souza 	/*
1379551fb93dSJosé Roberto de Souza 	 * If DRRS is not supported, drrs_type has to be set to 0.
1380551fb93dSJosé Roberto de Souza 	 * This is because, VBT is configured in such a way that
1381551fb93dSJosé Roberto de Souza 	 * static DRRS is 0 and DRRS not supported is represented by
1382551fb93dSJosé Roberto de Souza 	 * power->drrs & BIT(panel_type)=false
1383551fb93dSJosé Roberto de Souza 	 */
1384a50cc495SVille Syrjälä 	if (!panel_bool(power->drrs, panel_type) && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
13855a18db2eSVille Syrjälä 		/*
13865a18db2eSVille Syrjälä 		 * FIXME Should DMRRS perhaps be treated as seamless
13875a18db2eSVille Syrjälä 		 * but without the automatic downclocking?
13885a18db2eSVille Syrjälä 		 */
1389a50cc495SVille Syrjälä 		if (panel_bool(power->dmrrs, panel_type))
13905a18db2eSVille Syrjälä 			panel->vbt.drrs_type = DRRS_TYPE_STATIC;
13915a18db2eSVille Syrjälä 		else
13923cf05076SVille Syrjälä 			panel->vbt.drrs_type = DRRS_TYPE_NONE;
13935a18db2eSVille Syrjälä 	}
1394f615cb6aSJosé Roberto de Souza 
1395a434689cSJani Nikula 	if (i915->display.vbt.version >= 232)
1396a50cc495SVille Syrjälä 		panel->vbt.edp.hobl = panel_bool(power->hobl, panel_type);
1397fba99b1aSVille Syrjälä 
1398a434689cSJani Nikula 	if (i915->display.vbt.version >= 233)
1399a50cc495SVille Syrjälä 		panel->vbt.vrr = panel_bool(power->vrr_feature_enabled,
1400a50cc495SVille Syrjälä 					    panel_type);
1401551fb93dSJosé Roberto de Souza }
1402df0566a6SJani Nikula 
1403df0566a6SJani Nikula static void
14043cf05076SVille Syrjälä parse_edp(struct drm_i915_private *i915,
14053cf05076SVille Syrjälä 	  struct intel_panel *panel)
1406df0566a6SJani Nikula {
1407df0566a6SJani Nikula 	const struct bdb_edp *edp;
1408df0566a6SJani Nikula 	const struct edp_power_seq *edp_pps;
1409df0566a6SJani Nikula 	const struct edp_fast_link_params *edp_link_params;
14103cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1411df0566a6SJani Nikula 
14120a93eeb5SMaarten Lankhorst 	edp = bdb_find_section(i915, BDB_EDP);
1413df0566a6SJani Nikula 	if (!edp)
1414df0566a6SJani Nikula 		return;
1415df0566a6SJani Nikula 
1416a50cc495SVille Syrjälä 	switch (panel_bits(edp->color_depth, panel_type, 2)) {
1417df0566a6SJani Nikula 	case EDP_18BPP:
14183cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 18;
1419df0566a6SJani Nikula 		break;
1420df0566a6SJani Nikula 	case EDP_24BPP:
14213cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 24;
1422df0566a6SJani Nikula 		break;
1423df0566a6SJani Nikula 	case EDP_30BPP:
14243cf05076SVille Syrjälä 		panel->vbt.edp.bpp = 30;
1425df0566a6SJani Nikula 		break;
1426df0566a6SJani Nikula 	}
1427df0566a6SJani Nikula 
1428df0566a6SJani Nikula 	/* Get the eDP sequencing and link info */
1429df0566a6SJani Nikula 	edp_pps = &edp->power_seqs[panel_type];
1430df0566a6SJani Nikula 	edp_link_params = &edp->fast_link_params[panel_type];
1431df0566a6SJani Nikula 
14323cf05076SVille Syrjälä 	panel->vbt.edp.pps = *edp_pps;
1433df0566a6SJani Nikula 
1434a434689cSJani Nikula 	if (i915->display.vbt.version >= 224) {
1435f06d1d66SVille Syrjälä 		panel->vbt.edp.rate =
1436f06d1d66SVille Syrjälä 			edp->edp_fast_link_training_rate[panel_type] * 20;
1437f06d1d66SVille Syrjälä 	} else {
1438df0566a6SJani Nikula 		switch (edp_link_params->rate) {
1439df0566a6SJani Nikula 		case EDP_RATE_1_62:
1440f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 162000;
1441df0566a6SJani Nikula 			break;
1442df0566a6SJani Nikula 		case EDP_RATE_2_7:
1443f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 270000;
1444f06d1d66SVille Syrjälä 			break;
1445f06d1d66SVille Syrjälä 		case EDP_RATE_5_4:
1446f06d1d66SVille Syrjälä 			panel->vbt.edp.rate = 540000;
1447df0566a6SJani Nikula 			break;
1448df0566a6SJani Nikula 		default:
1449dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1450e92cbf38SWambui Karuga 				    "VBT has unknown eDP link rate value %u\n",
1451df0566a6SJani Nikula 				    edp_link_params->rate);
1452df0566a6SJani Nikula 			break;
1453df0566a6SJani Nikula 		}
1454f06d1d66SVille Syrjälä 	}
1455df0566a6SJani Nikula 
1456df0566a6SJani Nikula 	switch (edp_link_params->lanes) {
1457df0566a6SJani Nikula 	case EDP_LANE_1:
14583cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 1;
1459df0566a6SJani Nikula 		break;
1460df0566a6SJani Nikula 	case EDP_LANE_2:
14613cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 2;
1462df0566a6SJani Nikula 		break;
1463df0566a6SJani Nikula 	case EDP_LANE_4:
14643cf05076SVille Syrjälä 		panel->vbt.edp.lanes = 4;
1465df0566a6SJani Nikula 		break;
1466df0566a6SJani Nikula 	default:
1467dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1468e92cbf38SWambui Karuga 			    "VBT has unknown eDP lane count value %u\n",
1469df0566a6SJani Nikula 			    edp_link_params->lanes);
1470df0566a6SJani Nikula 		break;
1471df0566a6SJani Nikula 	}
1472df0566a6SJani Nikula 
1473df0566a6SJani Nikula 	switch (edp_link_params->preemphasis) {
1474df0566a6SJani Nikula 	case EDP_PREEMPHASIS_NONE:
14753cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
1476df0566a6SJani Nikula 		break;
1477df0566a6SJani Nikula 	case EDP_PREEMPHASIS_3_5dB:
14783cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
1479df0566a6SJani Nikula 		break;
1480df0566a6SJani Nikula 	case EDP_PREEMPHASIS_6dB:
14813cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
1482df0566a6SJani Nikula 		break;
1483df0566a6SJani Nikula 	case EDP_PREEMPHASIS_9_5dB:
14843cf05076SVille Syrjälä 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
1485df0566a6SJani Nikula 		break;
1486df0566a6SJani Nikula 	default:
1487dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1488e92cbf38SWambui Karuga 			    "VBT has unknown eDP pre-emphasis value %u\n",
1489df0566a6SJani Nikula 			    edp_link_params->preemphasis);
1490df0566a6SJani Nikula 		break;
1491df0566a6SJani Nikula 	}
1492df0566a6SJani Nikula 
1493df0566a6SJani Nikula 	switch (edp_link_params->vswing) {
1494df0566a6SJani Nikula 	case EDP_VSWING_0_4V:
14953cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
1496df0566a6SJani Nikula 		break;
1497df0566a6SJani Nikula 	case EDP_VSWING_0_6V:
14983cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
1499df0566a6SJani Nikula 		break;
1500df0566a6SJani Nikula 	case EDP_VSWING_0_8V:
15013cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
1502df0566a6SJani Nikula 		break;
1503df0566a6SJani Nikula 	case EDP_VSWING_1_2V:
15043cf05076SVille Syrjälä 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
1505df0566a6SJani Nikula 		break;
1506df0566a6SJani Nikula 	default:
1507dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
1508e92cbf38SWambui Karuga 			    "VBT has unknown eDP voltage swing value %u\n",
1509df0566a6SJani Nikula 			    edp_link_params->vswing);
1510df0566a6SJani Nikula 		break;
1511df0566a6SJani Nikula 	}
1512df0566a6SJani Nikula 
1513a434689cSJani Nikula 	if (i915->display.vbt.version >= 173) {
1514df0566a6SJani Nikula 		u8 vswing;
1515df0566a6SJani Nikula 
1516df0566a6SJani Nikula 		/* Don't read from VBT if module parameter has valid value*/
151787706a67SJouni Högander 		if (i915->display.params.edp_vswing) {
15183cf05076SVille Syrjälä 			panel->vbt.edp.low_vswing =
151987706a67SJouni Högander 				i915->display.params.edp_vswing == 1;
1520df0566a6SJani Nikula 		} else {
1521df0566a6SJani Nikula 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
15223cf05076SVille Syrjälä 			panel->vbt.edp.low_vswing = vswing == 0;
1523df0566a6SJani Nikula 		}
1524df0566a6SJani Nikula 	}
1525b395c29aSVille Syrjälä 
15263cf05076SVille Syrjälä 	panel->vbt.edp.drrs_msa_timing_delay =
1527a50cc495SVille Syrjälä 		panel_bits(edp->sdrrs_msa_timing_delay, panel_type, 2);
152824b8b74eSVille Syrjälä 
1529a434689cSJani Nikula 	if (i915->display.vbt.version >= 244)
153024b8b74eSVille Syrjälä 		panel->vbt.edp.max_link_rate =
153124b8b74eSVille Syrjälä 			edp->edp_max_port_link_rate[panel_type] * 20;
1532df0566a6SJani Nikula }
1533df0566a6SJani Nikula 
1534df0566a6SJani Nikula static void
15353cf05076SVille Syrjälä parse_psr(struct drm_i915_private *i915,
15363cf05076SVille Syrjälä 	  struct intel_panel *panel)
1537df0566a6SJani Nikula {
1538df0566a6SJani Nikula 	const struct bdb_psr *psr;
1539df0566a6SJani Nikula 	const struct psr_table *psr_table;
15403cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1541df0566a6SJani Nikula 
15420a93eeb5SMaarten Lankhorst 	psr = bdb_find_section(i915, BDB_PSR);
1543df0566a6SJani Nikula 	if (!psr) {
1544dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No PSR BDB found.\n");
1545df0566a6SJani Nikula 		return;
1546df0566a6SJani Nikula 	}
1547df0566a6SJani Nikula 
1548df0566a6SJani Nikula 	psr_table = &psr->psr_table[panel_type];
1549df0566a6SJani Nikula 
15503cf05076SVille Syrjälä 	panel->vbt.psr.full_link = psr_table->full_link;
15513cf05076SVille Syrjälä 	panel->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
1552df0566a6SJani Nikula 
1553df0566a6SJani Nikula 	/* Allowed VBT values goes from 0 to 15 */
15543cf05076SVille Syrjälä 	panel->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
1555df0566a6SJani Nikula 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
1556df0566a6SJani Nikula 
1557df0566a6SJani Nikula 	/*
1558df0566a6SJani Nikula 	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
1559df0566a6SJani Nikula 	 * Old decimal value is wake up time in multiples of 100 us.
1560df0566a6SJani Nikula 	 */
1561a434689cSJani Nikula 	if (i915->display.vbt.version >= 205 &&
15622446e1d6SMatt Roper 	    (DISPLAY_VER(i915) >= 9 && !IS_BROXTON(i915))) {
1563df0566a6SJani Nikula 		switch (psr_table->tp1_wakeup_time) {
1564df0566a6SJani Nikula 		case 0:
15653cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 500;
1566df0566a6SJani Nikula 			break;
1567df0566a6SJani Nikula 		case 1:
15683cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 100;
1569df0566a6SJani Nikula 			break;
1570df0566a6SJani Nikula 		case 3:
15713cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 0;
1572df0566a6SJani Nikula 			break;
1573df0566a6SJani Nikula 		default:
1574dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1575e92cbf38SWambui Karuga 				    "VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1576df0566a6SJani Nikula 				    psr_table->tp1_wakeup_time);
1577df561f66SGustavo A. R. Silva 			fallthrough;
1578df0566a6SJani Nikula 		case 2:
15793cf05076SVille Syrjälä 			panel->vbt.psr.tp1_wakeup_time_us = 2500;
1580df0566a6SJani Nikula 			break;
1581df0566a6SJani Nikula 		}
1582df0566a6SJani Nikula 
1583df0566a6SJani Nikula 		switch (psr_table->tp2_tp3_wakeup_time) {
1584df0566a6SJani Nikula 		case 0:
15853cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 500;
1586df0566a6SJani Nikula 			break;
1587df0566a6SJani Nikula 		case 1:
15883cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 100;
1589df0566a6SJani Nikula 			break;
1590df0566a6SJani Nikula 		case 3:
15913cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 0;
1592df0566a6SJani Nikula 			break;
1593df0566a6SJani Nikula 		default:
1594dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
1595e92cbf38SWambui Karuga 				    "VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1596df0566a6SJani Nikula 				    psr_table->tp2_tp3_wakeup_time);
1597df561f66SGustavo A. R. Silva 			fallthrough;
1598df0566a6SJani Nikula 		case 2:
15993cf05076SVille Syrjälä 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
1600df0566a6SJani Nikula 		break;
1601df0566a6SJani Nikula 		}
1602df0566a6SJani Nikula 	} else {
16033cf05076SVille Syrjälä 		panel->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
16043cf05076SVille Syrjälä 		panel->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
1605df0566a6SJani Nikula 	}
1606df0566a6SJani Nikula 
1607a434689cSJani Nikula 	if (i915->display.vbt.version >= 226) {
1608b5ea9c93SDhinakaran Pandiyan 		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
1609df0566a6SJani Nikula 
1610a50cc495SVille Syrjälä 		wakeup_time = panel_bits(wakeup_time, panel_type, 2);
1611df0566a6SJani Nikula 		switch (wakeup_time) {
1612df0566a6SJani Nikula 		case 0:
1613df0566a6SJani Nikula 			wakeup_time = 500;
1614df0566a6SJani Nikula 			break;
1615df0566a6SJani Nikula 		case 1:
1616df0566a6SJani Nikula 			wakeup_time = 100;
1617df0566a6SJani Nikula 			break;
1618df0566a6SJani Nikula 		case 3:
1619df0566a6SJani Nikula 			wakeup_time = 50;
1620df0566a6SJani Nikula 			break;
1621df0566a6SJani Nikula 		default:
1622df0566a6SJani Nikula 		case 2:
1623df0566a6SJani Nikula 			wakeup_time = 2500;
1624df0566a6SJani Nikula 			break;
1625df0566a6SJani Nikula 		}
16263cf05076SVille Syrjälä 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
1627df0566a6SJani Nikula 	} else {
1628df0566a6SJani Nikula 		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
16293cf05076SVille Syrjälä 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = panel->vbt.psr.tp2_tp3_wakeup_time_us;
1630df0566a6SJani Nikula 	}
1631df0566a6SJani Nikula }
1632df0566a6SJani Nikula 
1633dbd440d8SJani Nikula static void parse_dsi_backlight_ports(struct drm_i915_private *i915,
16343cf05076SVille Syrjälä 				      struct intel_panel *panel,
16353cf05076SVille Syrjälä 				      enum port port)
1636df0566a6SJani Nikula {
1637ab55165dSJani Nikula 	enum port port_bc = DISPLAY_VER(i915) >= 11 ? PORT_B : PORT_C;
1638ab55165dSJani Nikula 
1639a434689cSJani Nikula 	if (!panel->vbt.dsi.config->dual_link || i915->display.vbt.version < 197) {
16403cf05076SVille Syrjälä 		panel->vbt.dsi.bl_ports = BIT(port);
16413cf05076SVille Syrjälä 		if (panel->vbt.dsi.config->cabc_supported)
16423cf05076SVille Syrjälä 			panel->vbt.dsi.cabc_ports = BIT(port);
1643df0566a6SJani Nikula 
1644df0566a6SJani Nikula 		return;
1645df0566a6SJani Nikula 	}
1646df0566a6SJani Nikula 
16473cf05076SVille Syrjälä 	switch (panel->vbt.dsi.config->dl_dcs_backlight_ports) {
1648df0566a6SJani Nikula 	case DL_DCS_PORT_A:
16493cf05076SVille Syrjälä 		panel->vbt.dsi.bl_ports = BIT(PORT_A);
1650df0566a6SJani Nikula 		break;
1651df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1652ab55165dSJani Nikula 		panel->vbt.dsi.bl_ports = BIT(port_bc);
1653df0566a6SJani Nikula 		break;
1654df0566a6SJani Nikula 	default:
1655df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
1656ab55165dSJani Nikula 		panel->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(port_bc);
1657df0566a6SJani Nikula 		break;
1658df0566a6SJani Nikula 	}
1659df0566a6SJani Nikula 
16603cf05076SVille Syrjälä 	if (!panel->vbt.dsi.config->cabc_supported)
1661df0566a6SJani Nikula 		return;
1662df0566a6SJani Nikula 
16633cf05076SVille Syrjälä 	switch (panel->vbt.dsi.config->dl_dcs_cabc_ports) {
1664df0566a6SJani Nikula 	case DL_DCS_PORT_A:
16653cf05076SVille Syrjälä 		panel->vbt.dsi.cabc_ports = BIT(PORT_A);
1666df0566a6SJani Nikula 		break;
1667df0566a6SJani Nikula 	case DL_DCS_PORT_C:
1668ab55165dSJani Nikula 		panel->vbt.dsi.cabc_ports = BIT(port_bc);
1669df0566a6SJani Nikula 		break;
1670df0566a6SJani Nikula 	default:
1671df0566a6SJani Nikula 	case DL_DCS_PORT_A_AND_C:
16723cf05076SVille Syrjälä 		panel->vbt.dsi.cabc_ports =
1673ab55165dSJani Nikula 					BIT(PORT_A) | BIT(port_bc);
1674df0566a6SJani Nikula 		break;
1675df0566a6SJani Nikula 	}
1676df0566a6SJani Nikula }
1677df0566a6SJani Nikula 
1678df0566a6SJani Nikula static void
16793cf05076SVille Syrjälä parse_mipi_config(struct drm_i915_private *i915,
16803cf05076SVille Syrjälä 		  struct intel_panel *panel)
1681df0566a6SJani Nikula {
1682df0566a6SJani Nikula 	const struct bdb_mipi_config *start;
1683df0566a6SJani Nikula 	const struct mipi_config *config;
1684df0566a6SJani Nikula 	const struct mipi_pps_data *pps;
16853cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
1686df0566a6SJani Nikula 	enum port port;
1687df0566a6SJani Nikula 
1688df0566a6SJani Nikula 	/* parse MIPI blocks only if LFP type is MIPI */
1689dbd440d8SJani Nikula 	if (!intel_bios_is_dsi_present(i915, &port))
1690df0566a6SJani Nikula 		return;
1691df0566a6SJani Nikula 
1692df0566a6SJani Nikula 	/* Initialize this to undefined indicating no generic MIPI support */
16933cf05076SVille Syrjälä 	panel->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
1694df0566a6SJani Nikula 
1695df0566a6SJani Nikula 	/* Block #40 is already parsed and panel_fixed_mode is
1696dbd440d8SJani Nikula 	 * stored in i915->lfp_lvds_vbt_mode
1697df0566a6SJani Nikula 	 * resuse this when needed
1698df0566a6SJani Nikula 	 */
1699df0566a6SJani Nikula 
1700df0566a6SJani Nikula 	/* Parse #52 for panel index used from panel_type already
1701df0566a6SJani Nikula 	 * parsed
1702df0566a6SJani Nikula 	 */
17030a93eeb5SMaarten Lankhorst 	start = bdb_find_section(i915, BDB_MIPI_CONFIG);
1704df0566a6SJani Nikula 	if (!start) {
1705dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm, "No MIPI config BDB found");
1706df0566a6SJani Nikula 		return;
1707df0566a6SJani Nikula 	}
1708df0566a6SJani Nikula 
1709dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI Config block, panel index = %d\n",
1710df0566a6SJani Nikula 		panel_type);
1711df0566a6SJani Nikula 
1712df0566a6SJani Nikula 	/*
1713df0566a6SJani Nikula 	 * get hold of the correct configuration block and pps data as per
1714df0566a6SJani Nikula 	 * the panel_type as index
1715df0566a6SJani Nikula 	 */
1716df0566a6SJani Nikula 	config = &start->config[panel_type];
1717df0566a6SJani Nikula 	pps = &start->pps[panel_type];
1718df0566a6SJani Nikula 
1719df0566a6SJani Nikula 	/* store as of now full data. Trim when we realise all is not needed */
17203cf05076SVille Syrjälä 	panel->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
17213cf05076SVille Syrjälä 	if (!panel->vbt.dsi.config)
1722df0566a6SJani Nikula 		return;
1723df0566a6SJani Nikula 
17243cf05076SVille Syrjälä 	panel->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
17253cf05076SVille Syrjälä 	if (!panel->vbt.dsi.pps) {
17263cf05076SVille Syrjälä 		kfree(panel->vbt.dsi.config);
1727df0566a6SJani Nikula 		return;
1728df0566a6SJani Nikula 	}
1729df0566a6SJani Nikula 
17303cf05076SVille Syrjälä 	parse_dsi_backlight_ports(i915, panel, port);
1731df0566a6SJani Nikula 
1732df0566a6SJani Nikula 	/* FIXME is the 90 vs. 270 correct? */
1733df0566a6SJani Nikula 	switch (config->rotation) {
1734df0566a6SJani Nikula 	case ENABLE_ROTATION_0:
1735df0566a6SJani Nikula 		/*
1736df0566a6SJani Nikula 		 * Most (all?) VBTs claim 0 degrees despite having
1737df0566a6SJani Nikula 		 * an upside down panel, thus we do not trust this.
1738df0566a6SJani Nikula 		 */
17393cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1740df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1741df0566a6SJani Nikula 		break;
1742df0566a6SJani Nikula 	case ENABLE_ROTATION_90:
17433cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1744df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1745df0566a6SJani Nikula 		break;
1746df0566a6SJani Nikula 	case ENABLE_ROTATION_180:
17473cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1748df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1749df0566a6SJani Nikula 		break;
1750df0566a6SJani Nikula 	case ENABLE_ROTATION_270:
17513cf05076SVille Syrjälä 		panel->vbt.dsi.orientation =
1752df0566a6SJani Nikula 			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1753df0566a6SJani Nikula 		break;
1754df0566a6SJani Nikula 	}
1755df0566a6SJani Nikula 
1756df0566a6SJani Nikula 	/* We have mandatory mipi config blocks. Initialize as generic panel */
17573cf05076SVille Syrjälä 	panel->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
1758df0566a6SJani Nikula }
1759df0566a6SJani Nikula 
1760df0566a6SJani Nikula /* Find the sequence block and size for the given panel. */
1761df0566a6SJani Nikula static const u8 *
1762*ff9bc20cSVille Syrjälä find_panel_sequence_block(struct drm_i915_private *i915,
1763*ff9bc20cSVille Syrjälä 			  const struct bdb_mipi_sequence *sequence,
1764df0566a6SJani Nikula 			  u16 panel_id, u32 *seq_size)
1765df0566a6SJani Nikula {
1766df0566a6SJani Nikula 	u32 total = get_blocksize(sequence);
1767df0566a6SJani Nikula 	const u8 *data = &sequence->data[0];
1768df0566a6SJani Nikula 	u8 current_id;
1769df0566a6SJani Nikula 	u32 current_size;
1770df0566a6SJani Nikula 	int header_size = sequence->version >= 3 ? 5 : 3;
1771df0566a6SJani Nikula 	int index = 0;
1772df0566a6SJani Nikula 	int i;
1773df0566a6SJani Nikula 
1774df0566a6SJani Nikula 	/* skip new block size */
1775df0566a6SJani Nikula 	if (sequence->version >= 3)
1776df0566a6SJani Nikula 		data += 4;
1777df0566a6SJani Nikula 
1778df0566a6SJani Nikula 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
1779df0566a6SJani Nikula 		if (index + header_size > total) {
1780*ff9bc20cSVille Syrjälä 			drm_err(&i915->drm, "Invalid sequence block (header)\n");
1781df0566a6SJani Nikula 			return NULL;
1782df0566a6SJani Nikula 		}
1783df0566a6SJani Nikula 
1784df0566a6SJani Nikula 		current_id = *(data + index);
1785df0566a6SJani Nikula 		if (sequence->version >= 3)
1786df0566a6SJani Nikula 			current_size = *((const u32 *)(data + index + 1));
1787df0566a6SJani Nikula 		else
1788df0566a6SJani Nikula 			current_size = *((const u16 *)(data + index + 1));
1789df0566a6SJani Nikula 
1790df0566a6SJani Nikula 		index += header_size;
1791df0566a6SJani Nikula 
1792df0566a6SJani Nikula 		if (index + current_size > total) {
1793*ff9bc20cSVille Syrjälä 			drm_err(&i915->drm, "Invalid sequence block\n");
1794df0566a6SJani Nikula 			return NULL;
1795df0566a6SJani Nikula 		}
1796df0566a6SJani Nikula 
1797df0566a6SJani Nikula 		if (current_id == panel_id) {
1798df0566a6SJani Nikula 			*seq_size = current_size;
1799df0566a6SJani Nikula 			return data + index;
1800df0566a6SJani Nikula 		}
1801df0566a6SJani Nikula 
1802df0566a6SJani Nikula 		index += current_size;
1803df0566a6SJani Nikula 	}
1804df0566a6SJani Nikula 
1805*ff9bc20cSVille Syrjälä 	drm_err(&i915->drm, "Sequence block detected but no valid configuration\n");
1806df0566a6SJani Nikula 
1807df0566a6SJani Nikula 	return NULL;
1808df0566a6SJani Nikula }
1809df0566a6SJani Nikula 
1810*ff9bc20cSVille Syrjälä static int goto_next_sequence(struct drm_i915_private *i915,
1811*ff9bc20cSVille Syrjälä 			      const u8 *data, int index, int total)
1812df0566a6SJani Nikula {
1813df0566a6SJani Nikula 	u16 len;
1814df0566a6SJani Nikula 
1815df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1816df0566a6SJani Nikula 	for (index = index + 1; index < total; index += len) {
1817df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1818df0566a6SJani Nikula 		index++;
1819df0566a6SJani Nikula 
1820df0566a6SJani Nikula 		switch (operation_byte) {
1821df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_END:
1822df0566a6SJani Nikula 			return index;
1823df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1824df0566a6SJani Nikula 			if (index + 4 > total)
1825df0566a6SJani Nikula 				return 0;
1826df0566a6SJani Nikula 
1827df0566a6SJani Nikula 			len = *((const u16 *)(data + index + 2)) + 4;
1828df0566a6SJani Nikula 			break;
1829df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1830df0566a6SJani Nikula 			len = 4;
1831df0566a6SJani Nikula 			break;
1832df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1833df0566a6SJani Nikula 			len = 2;
1834df0566a6SJani Nikula 			break;
1835df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1836df0566a6SJani Nikula 			if (index + 7 > total)
1837df0566a6SJani Nikula 				return 0;
1838df0566a6SJani Nikula 			len = *(data + index + 6) + 7;
1839df0566a6SJani Nikula 			break;
1840df0566a6SJani Nikula 		default:
1841*ff9bc20cSVille Syrjälä 			drm_err(&i915->drm, "Unknown operation byte\n");
1842df0566a6SJani Nikula 			return 0;
1843df0566a6SJani Nikula 		}
1844df0566a6SJani Nikula 	}
1845df0566a6SJani Nikula 
1846df0566a6SJani Nikula 	return 0;
1847df0566a6SJani Nikula }
1848df0566a6SJani Nikula 
1849*ff9bc20cSVille Syrjälä static int goto_next_sequence_v3(struct drm_i915_private *i915,
1850*ff9bc20cSVille Syrjälä 				 const u8 *data, int index, int total)
1851df0566a6SJani Nikula {
1852df0566a6SJani Nikula 	int seq_end;
1853df0566a6SJani Nikula 	u16 len;
1854df0566a6SJani Nikula 	u32 size_of_sequence;
1855df0566a6SJani Nikula 
1856df0566a6SJani Nikula 	/*
1857df0566a6SJani Nikula 	 * Could skip sequence based on Size of Sequence alone, but also do some
1858df0566a6SJani Nikula 	 * checking on the structure.
1859df0566a6SJani Nikula 	 */
1860df0566a6SJani Nikula 	if (total < 5) {
1861*ff9bc20cSVille Syrjälä 		drm_err(&i915->drm, "Too small sequence size\n");
1862df0566a6SJani Nikula 		return 0;
1863df0566a6SJani Nikula 	}
1864df0566a6SJani Nikula 
1865df0566a6SJani Nikula 	/* Skip Sequence Byte. */
1866df0566a6SJani Nikula 	index++;
1867df0566a6SJani Nikula 
1868df0566a6SJani Nikula 	/*
1869df0566a6SJani Nikula 	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
1870df0566a6SJani Nikula 	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
1871df0566a6SJani Nikula 	 * byte.
1872df0566a6SJani Nikula 	 */
1873df0566a6SJani Nikula 	size_of_sequence = *((const u32 *)(data + index));
1874df0566a6SJani Nikula 	index += 4;
1875df0566a6SJani Nikula 
1876df0566a6SJani Nikula 	seq_end = index + size_of_sequence;
1877df0566a6SJani Nikula 	if (seq_end > total) {
1878*ff9bc20cSVille Syrjälä 		drm_err(&i915->drm, "Invalid sequence size\n");
1879df0566a6SJani Nikula 		return 0;
1880df0566a6SJani Nikula 	}
1881df0566a6SJani Nikula 
1882df0566a6SJani Nikula 	for (; index < total; index += len) {
1883df0566a6SJani Nikula 		u8 operation_byte = *(data + index);
1884df0566a6SJani Nikula 		index++;
1885df0566a6SJani Nikula 
1886df0566a6SJani Nikula 		if (operation_byte == MIPI_SEQ_ELEM_END) {
1887df0566a6SJani Nikula 			if (index != seq_end) {
1888*ff9bc20cSVille Syrjälä 				drm_err(&i915->drm, "Invalid element structure\n");
1889df0566a6SJani Nikula 				return 0;
1890df0566a6SJani Nikula 			}
1891df0566a6SJani Nikula 			return index;
1892df0566a6SJani Nikula 		}
1893df0566a6SJani Nikula 
1894df0566a6SJani Nikula 		len = *(data + index);
1895df0566a6SJani Nikula 		index++;
1896df0566a6SJani Nikula 
1897df0566a6SJani Nikula 		/*
1898df0566a6SJani Nikula 		 * FIXME: Would be nice to check elements like for v1/v2 in
1899df0566a6SJani Nikula 		 * goto_next_sequence() above.
1900df0566a6SJani Nikula 		 */
1901df0566a6SJani Nikula 		switch (operation_byte) {
1902df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1903df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1904df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1905df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_I2C:
1906df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SPI:
1907df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_PMIC:
1908df0566a6SJani Nikula 			break;
1909df0566a6SJani Nikula 		default:
1910*ff9bc20cSVille Syrjälä 			drm_err(&i915->drm, "Unknown operation byte %u\n",
1911df0566a6SJani Nikula 				operation_byte);
1912df0566a6SJani Nikula 			break;
1913df0566a6SJani Nikula 		}
1914df0566a6SJani Nikula 	}
1915df0566a6SJani Nikula 
1916df0566a6SJani Nikula 	return 0;
1917df0566a6SJani Nikula }
1918df0566a6SJani Nikula 
1919df0566a6SJani Nikula /*
1920df0566a6SJani Nikula  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
1921df0566a6SJani Nikula  * skip all delay + gpio operands and stop at the first DSI packet op.
1922df0566a6SJani Nikula  */
19233cf05076SVille Syrjälä static int get_init_otp_deassert_fragment_len(struct drm_i915_private *i915,
19243cf05076SVille Syrjälä 					      struct intel_panel *panel)
1925df0566a6SJani Nikula {
19263cf05076SVille Syrjälä 	const u8 *data = panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1927df0566a6SJani Nikula 	int index, len;
1928df0566a6SJani Nikula 
1929dbd440d8SJani Nikula 	if (drm_WARN_ON(&i915->drm,
19303cf05076SVille Syrjälä 			!data || panel->vbt.dsi.seq_version != 1))
1931df0566a6SJani Nikula 		return 0;
1932df0566a6SJani Nikula 
1933df0566a6SJani Nikula 	/* index = 1 to skip sequence byte */
1934df0566a6SJani Nikula 	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1935df0566a6SJani Nikula 		switch (data[index]) {
1936df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_SEND_PKT:
1937df0566a6SJani Nikula 			return index == 1 ? 0 : index;
1938df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_DELAY:
1939df0566a6SJani Nikula 			len = 5; /* 1 byte for operand + uint32 */
1940df0566a6SJani Nikula 			break;
1941df0566a6SJani Nikula 		case MIPI_SEQ_ELEM_GPIO:
1942df0566a6SJani Nikula 			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1943df0566a6SJani Nikula 			break;
1944df0566a6SJani Nikula 		default:
1945df0566a6SJani Nikula 			return 0;
1946df0566a6SJani Nikula 		}
1947df0566a6SJani Nikula 	}
1948df0566a6SJani Nikula 
1949df0566a6SJani Nikula 	return 0;
1950df0566a6SJani Nikula }
1951df0566a6SJani Nikula 
1952df0566a6SJani Nikula /*
1953df0566a6SJani Nikula  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1954df0566a6SJani Nikula  * The deassert must be done before calling intel_dsi_device_ready, so for
1955df0566a6SJani Nikula  * these devices we split the init OTP sequence into a deassert sequence and
1956df0566a6SJani Nikula  * the actual init OTP part.
1957df0566a6SJani Nikula  */
19583cf05076SVille Syrjälä static void fixup_mipi_sequences(struct drm_i915_private *i915,
19593cf05076SVille Syrjälä 				 struct intel_panel *panel)
1960df0566a6SJani Nikula {
1961df0566a6SJani Nikula 	u8 *init_otp;
1962df0566a6SJani Nikula 	int len;
1963df0566a6SJani Nikula 
1964df0566a6SJani Nikula 	/* Limit this to VLV for now. */
1965dbd440d8SJani Nikula 	if (!IS_VALLEYVIEW(i915))
1966df0566a6SJani Nikula 		return;
1967df0566a6SJani Nikula 
1968df0566a6SJani Nikula 	/* Limit this to v1 vid-mode sequences */
19693cf05076SVille Syrjälä 	if (panel->vbt.dsi.config->is_cmd_mode ||
19703cf05076SVille Syrjälä 	    panel->vbt.dsi.seq_version != 1)
1971df0566a6SJani Nikula 		return;
1972df0566a6SJani Nikula 
1973df0566a6SJani Nikula 	/* Only do this if there are otp and assert seqs and no deassert seq */
19743cf05076SVille Syrjälä 	if (!panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
19753cf05076SVille Syrjälä 	    !panel->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
19763cf05076SVille Syrjälä 	    panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1977df0566a6SJani Nikula 		return;
1978df0566a6SJani Nikula 
1979df0566a6SJani Nikula 	/* The deassert-sequence ends at the first DSI packet */
19803cf05076SVille Syrjälä 	len = get_init_otp_deassert_fragment_len(i915, panel);
1981df0566a6SJani Nikula 	if (!len)
1982df0566a6SJani Nikula 		return;
1983df0566a6SJani Nikula 
1984dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
1985e92cbf38SWambui Karuga 		    "Using init OTP fragment to deassert reset\n");
1986df0566a6SJani Nikula 
1987df0566a6SJani Nikula 	/* Copy the fragment, update seq byte and terminate it */
19883cf05076SVille Syrjälä 	init_otp = (u8 *)panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
19893cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
19903cf05076SVille Syrjälä 	if (!panel->vbt.dsi.deassert_seq)
1991df0566a6SJani Nikula 		return;
19923cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
19933cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1994df0566a6SJani Nikula 	/* Use the copy for deassert */
19953cf05076SVille Syrjälä 	panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
19963cf05076SVille Syrjälä 		panel->vbt.dsi.deassert_seq;
1997df0566a6SJani Nikula 	/* Replace the last byte of the fragment with init OTP seq byte */
1998df0566a6SJani Nikula 	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1999df0566a6SJani Nikula 	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
20003cf05076SVille Syrjälä 	panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
2001df0566a6SJani Nikula }
2002df0566a6SJani Nikula 
2003df0566a6SJani Nikula static void
20043cf05076SVille Syrjälä parse_mipi_sequence(struct drm_i915_private *i915,
20053cf05076SVille Syrjälä 		    struct intel_panel *panel)
2006df0566a6SJani Nikula {
20073cf05076SVille Syrjälä 	int panel_type = panel->vbt.panel_type;
2008df0566a6SJani Nikula 	const struct bdb_mipi_sequence *sequence;
2009df0566a6SJani Nikula 	const u8 *seq_data;
2010df0566a6SJani Nikula 	u32 seq_size;
2011df0566a6SJani Nikula 	u8 *data;
2012df0566a6SJani Nikula 	int index = 0;
2013df0566a6SJani Nikula 
2014df0566a6SJani Nikula 	/* Only our generic panel driver uses the sequence block. */
20153cf05076SVille Syrjälä 	if (panel->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
2016df0566a6SJani Nikula 		return;
2017df0566a6SJani Nikula 
20180a93eeb5SMaarten Lankhorst 	sequence = bdb_find_section(i915, BDB_MIPI_SEQUENCE);
2019df0566a6SJani Nikula 	if (!sequence) {
2020dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2021e92cbf38SWambui Karuga 			    "No MIPI Sequence found, parsing complete\n");
2022df0566a6SJani Nikula 		return;
2023df0566a6SJani Nikula 	}
2024df0566a6SJani Nikula 
2025df0566a6SJani Nikula 	/* Fail gracefully for forward incompatible sequence block. */
2026df0566a6SJani Nikula 	if (sequence->version >= 4) {
2027dbd440d8SJani Nikula 		drm_err(&i915->drm,
2028e92cbf38SWambui Karuga 			"Unable to parse MIPI Sequence Block v%u\n",
2029df0566a6SJani Nikula 			sequence->version);
2030df0566a6SJani Nikula 		return;
2031df0566a6SJani Nikula 	}
2032df0566a6SJani Nikula 
2033dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "Found MIPI sequence block v%u\n",
2034e92cbf38SWambui Karuga 		sequence->version);
2035df0566a6SJani Nikula 
2036*ff9bc20cSVille Syrjälä 	seq_data = find_panel_sequence_block(i915, sequence, panel_type, &seq_size);
2037df0566a6SJani Nikula 	if (!seq_data)
2038df0566a6SJani Nikula 		return;
2039df0566a6SJani Nikula 
2040df0566a6SJani Nikula 	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
2041df0566a6SJani Nikula 	if (!data)
2042df0566a6SJani Nikula 		return;
2043df0566a6SJani Nikula 
2044df0566a6SJani Nikula 	/* Parse the sequences, store pointers to each sequence. */
2045df0566a6SJani Nikula 	for (;;) {
2046df0566a6SJani Nikula 		u8 seq_id = *(data + index);
2047df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_END)
2048df0566a6SJani Nikula 			break;
2049df0566a6SJani Nikula 
2050df0566a6SJani Nikula 		if (seq_id >= MIPI_SEQ_MAX) {
2051dbd440d8SJani Nikula 			drm_err(&i915->drm, "Unknown sequence %u\n",
2052e92cbf38SWambui Karuga 				seq_id);
2053df0566a6SJani Nikula 			goto err;
2054df0566a6SJani Nikula 		}
2055df0566a6SJani Nikula 
2056df0566a6SJani Nikula 		/* Log about presence of sequences we won't run. */
2057df0566a6SJani Nikula 		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
2058dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
2059e92cbf38SWambui Karuga 				    "Unsupported sequence %u\n", seq_id);
2060df0566a6SJani Nikula 
20613cf05076SVille Syrjälä 		panel->vbt.dsi.sequence[seq_id] = data + index;
2062df0566a6SJani Nikula 
2063df0566a6SJani Nikula 		if (sequence->version >= 3)
2064*ff9bc20cSVille Syrjälä 			index = goto_next_sequence_v3(i915, data, index, seq_size);
2065df0566a6SJani Nikula 		else
2066*ff9bc20cSVille Syrjälä 			index = goto_next_sequence(i915, data, index, seq_size);
2067df0566a6SJani Nikula 		if (!index) {
2068dbd440d8SJani Nikula 			drm_err(&i915->drm, "Invalid sequence %u\n",
2069e92cbf38SWambui Karuga 				seq_id);
2070df0566a6SJani Nikula 			goto err;
2071df0566a6SJani Nikula 		}
2072df0566a6SJani Nikula 	}
2073df0566a6SJani Nikula 
20743cf05076SVille Syrjälä 	panel->vbt.dsi.data = data;
20753cf05076SVille Syrjälä 	panel->vbt.dsi.size = seq_size;
20763cf05076SVille Syrjälä 	panel->vbt.dsi.seq_version = sequence->version;
2077df0566a6SJani Nikula 
20783cf05076SVille Syrjälä 	fixup_mipi_sequences(i915, panel);
2079df0566a6SJani Nikula 
2080dbd440d8SJani Nikula 	drm_dbg(&i915->drm, "MIPI related VBT parsing complete\n");
2081df0566a6SJani Nikula 	return;
2082df0566a6SJani Nikula 
2083df0566a6SJani Nikula err:
2084df0566a6SJani Nikula 	kfree(data);
20853cf05076SVille Syrjälä 	memset(panel->vbt.dsi.sequence, 0, sizeof(panel->vbt.dsi.sequence));
2086df0566a6SJani Nikula }
2087df0566a6SJani Nikula 
20886e0d46e9SJani Nikula static void
2089e163cfb4SVille Syrjälä parse_compression_parameters(struct drm_i915_private *i915)
20906e0d46e9SJani Nikula {
20916e0d46e9SJani Nikula 	const struct bdb_compression_parameters *params;
20923162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
20936e0d46e9SJani Nikula 	u16 block_size;
20946e0d46e9SJani Nikula 	int index;
20956e0d46e9SJani Nikula 
2096a434689cSJani Nikula 	if (i915->display.vbt.version < 198)
20976e0d46e9SJani Nikula 		return;
20986e0d46e9SJani Nikula 
20990a93eeb5SMaarten Lankhorst 	params = bdb_find_section(i915, BDB_COMPRESSION_PARAMETERS);
21006e0d46e9SJani Nikula 	if (params) {
21016e0d46e9SJani Nikula 		/* Sanity checks */
21026e0d46e9SJani Nikula 		if (params->entry_size != sizeof(params->data[0])) {
2103e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2104e92cbf38SWambui Karuga 				    "VBT: unsupported compression param entry size\n");
21056e0d46e9SJani Nikula 			return;
21066e0d46e9SJani Nikula 		}
21076e0d46e9SJani Nikula 
21086e0d46e9SJani Nikula 		block_size = get_blocksize(params);
21096e0d46e9SJani Nikula 		if (block_size < sizeof(*params)) {
2110e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2111e92cbf38SWambui Karuga 				    "VBT: expected 16 compression param entries\n");
21126e0d46e9SJani Nikula 			return;
21136e0d46e9SJani Nikula 		}
21146e0d46e9SJani Nikula 	}
21156e0d46e9SJani Nikula 
2116a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
2117d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
21186e0d46e9SJani Nikula 
21196e0d46e9SJani Nikula 		if (!child->compression_enable)
21206e0d46e9SJani Nikula 			continue;
21216e0d46e9SJani Nikula 
21226e0d46e9SJani Nikula 		if (!params) {
2123e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2124e92cbf38SWambui Karuga 				    "VBT: compression params not available\n");
21256e0d46e9SJani Nikula 			continue;
21266e0d46e9SJani Nikula 		}
21276e0d46e9SJani Nikula 
21286e0d46e9SJani Nikula 		if (child->compression_method_cps) {
2129e92cbf38SWambui Karuga 			drm_dbg_kms(&i915->drm,
2130e92cbf38SWambui Karuga 				    "VBT: CPS compression not supported\n");
21316e0d46e9SJani Nikula 			continue;
21326e0d46e9SJani Nikula 		}
21336e0d46e9SJani Nikula 
21346e0d46e9SJani Nikula 		index = child->compression_structure_index;
21356e0d46e9SJani Nikula 
21366e0d46e9SJani Nikula 		devdata->dsc = kmemdup(&params->data[index],
21376e0d46e9SJani Nikula 				       sizeof(*devdata->dsc), GFP_KERNEL);
21386e0d46e9SJani Nikula 	}
21396e0d46e9SJani Nikula }
21406e0d46e9SJani Nikula 
2141*ff9bc20cSVille Syrjälä static u8 translate_iboost(struct drm_i915_private *i915, u8 val)
2142df0566a6SJani Nikula {
2143df0566a6SJani Nikula 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
2144df0566a6SJani Nikula 
2145df0566a6SJani Nikula 	if (val >= ARRAY_SIZE(mapping)) {
2146*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm,
2147*ff9bc20cSVille Syrjälä 			    "Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
2148df0566a6SJani Nikula 		return 0;
2149df0566a6SJani Nikula 	}
2150df0566a6SJani Nikula 	return mapping[val];
2151df0566a6SJani Nikula }
2152df0566a6SJani Nikula 
21539e1dbc1aSJani Nikula static const u8 cnp_ddc_pin_map[] = {
21549e1dbc1aSJani Nikula 	[0] = 0, /* N/A */
21553d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = DDC_BUS_DDI_B,
21563d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = DDC_BUS_DDI_C,
21573d7af6cfSVille Syrjälä 	[GMBUS_PIN_4_CNP] = DDC_BUS_DDI_D, /* sic */
21583d7af6cfSVille Syrjälä 	[GMBUS_PIN_3_BXT] = DDC_BUS_DDI_F, /* sic */
21599e1dbc1aSJani Nikula };
21609e1dbc1aSJani Nikula 
21619e1dbc1aSJani Nikula static const u8 icp_ddc_pin_map[] = {
21623d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
21633d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
21643d7af6cfSVille Syrjälä 	[GMBUS_PIN_3_BXT] = TGL_DDC_BUS_DDI_C,
21653d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = ICL_DDC_BUS_PORT_1,
21663d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = ICL_DDC_BUS_PORT_2,
21673d7af6cfSVille Syrjälä 	[GMBUS_PIN_11_TC3_ICP] = ICL_DDC_BUS_PORT_3,
21683d7af6cfSVille Syrjälä 	[GMBUS_PIN_12_TC4_ICP] = ICL_DDC_BUS_PORT_4,
21693d7af6cfSVille Syrjälä 	[GMBUS_PIN_13_TC5_TGP] = TGL_DDC_BUS_PORT_5,
21703d7af6cfSVille Syrjälä 	[GMBUS_PIN_14_TC6_TGP] = TGL_DDC_BUS_PORT_6,
21719e1dbc1aSJani Nikula };
21729e1dbc1aSJani Nikula 
21739e1dbc1aSJani Nikula static const u8 rkl_pch_tgp_ddc_pin_map[] = {
21743d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
21753d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
21763d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = RKL_DDC_BUS_DDI_D,
21773d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = RKL_DDC_BUS_DDI_E,
21789e1dbc1aSJani Nikula };
21799e1dbc1aSJani Nikula 
21809e1dbc1aSJani Nikula static const u8 adls_ddc_pin_map[] = {
21813d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
21823d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = ADLS_DDC_BUS_PORT_TC1,
21833d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = ADLS_DDC_BUS_PORT_TC2,
21843d7af6cfSVille Syrjälä 	[GMBUS_PIN_11_TC3_ICP] = ADLS_DDC_BUS_PORT_TC3,
21853d7af6cfSVille Syrjälä 	[GMBUS_PIN_12_TC4_ICP] = ADLS_DDC_BUS_PORT_TC4,
21869e1dbc1aSJani Nikula };
21879e1dbc1aSJani Nikula 
21889e1dbc1aSJani Nikula static const u8 gen9bc_tgp_ddc_pin_map[] = {
21893d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = DDC_BUS_DDI_B,
21903d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = DDC_BUS_DDI_C,
21913d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = DDC_BUS_DDI_D,
21929e1dbc1aSJani Nikula };
21939e1dbc1aSJani Nikula 
2194af10ec31STejas Upadhyay static const u8 adlp_ddc_pin_map[] = {
21953d7af6cfSVille Syrjälä 	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
21963d7af6cfSVille Syrjälä 	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
21973d7af6cfSVille Syrjälä 	[GMBUS_PIN_9_TC1_ICP] = ADLP_DDC_BUS_PORT_TC1,
21983d7af6cfSVille Syrjälä 	[GMBUS_PIN_10_TC2_ICP] = ADLP_DDC_BUS_PORT_TC2,
21993d7af6cfSVille Syrjälä 	[GMBUS_PIN_11_TC3_ICP] = ADLP_DDC_BUS_PORT_TC3,
22003d7af6cfSVille Syrjälä 	[GMBUS_PIN_12_TC4_ICP] = ADLP_DDC_BUS_PORT_TC4,
2201af10ec31STejas Upadhyay };
2202af10ec31STejas Upadhyay 
22039e1dbc1aSJani Nikula static u8 map_ddc_pin(struct drm_i915_private *i915, u8 vbt_pin)
22049e1dbc1aSJani Nikula {
22059e1dbc1aSJani Nikula 	const u8 *ddc_pin_map;
22063d7af6cfSVille Syrjälä 	int i, n_entries;
22079e1dbc1aSJani Nikula 
2208562f3383SClint Taylor 	if (IS_DGFX(i915))
2209562f3383SClint Taylor 		return vbt_pin;
2210562f3383SClint Taylor 
221193cbc1acSHaridhar Kalvala 	if (INTEL_PCH_TYPE(i915) >= PCH_MTL || IS_ALDERLAKE_P(i915)) {
2212af10ec31STejas Upadhyay 		ddc_pin_map = adlp_ddc_pin_map;
2213af10ec31STejas Upadhyay 		n_entries = ARRAY_SIZE(adlp_ddc_pin_map);
2214af10ec31STejas Upadhyay 	} else if (IS_ALDERLAKE_S(i915)) {
22159e1dbc1aSJani Nikula 		ddc_pin_map = adls_ddc_pin_map;
22169e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(adls_ddc_pin_map);
22179e1dbc1aSJani Nikula 	} else if (IS_ROCKETLAKE(i915) && INTEL_PCH_TYPE(i915) == PCH_TGP) {
22189e1dbc1aSJani Nikula 		ddc_pin_map = rkl_pch_tgp_ddc_pin_map;
22199e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(rkl_pch_tgp_ddc_pin_map);
22209e1dbc1aSJani Nikula 	} else if (HAS_PCH_TGP(i915) && DISPLAY_VER(i915) == 9) {
22219e1dbc1aSJani Nikula 		ddc_pin_map = gen9bc_tgp_ddc_pin_map;
22229e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(gen9bc_tgp_ddc_pin_map);
22239e1dbc1aSJani Nikula 	} else if (INTEL_PCH_TYPE(i915) >= PCH_ICP) {
22249e1dbc1aSJani Nikula 		ddc_pin_map = icp_ddc_pin_map;
22259e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
22269e1dbc1aSJani Nikula 	} else if (HAS_PCH_CNP(i915)) {
22279e1dbc1aSJani Nikula 		ddc_pin_map = cnp_ddc_pin_map;
22289e1dbc1aSJani Nikula 		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
22299e1dbc1aSJani Nikula 	} else {
22309e1dbc1aSJani Nikula 		/* Assuming direct map */
22319e1dbc1aSJani Nikula 		return vbt_pin;
22329e1dbc1aSJani Nikula 	}
22339e1dbc1aSJani Nikula 
22343d7af6cfSVille Syrjälä 	for (i = 0; i < n_entries; i++) {
22353d7af6cfSVille Syrjälä 		if (ddc_pin_map[i] == vbt_pin)
22363d7af6cfSVille Syrjälä 			return i;
22373d7af6cfSVille Syrjälä 	}
22389e1dbc1aSJani Nikula 
22399e1dbc1aSJani Nikula 	drm_dbg_kms(&i915->drm,
22409e1dbc1aSJani Nikula 		    "Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
22419e1dbc1aSJani Nikula 		    vbt_pin);
22429e1dbc1aSJani Nikula 	return 0;
22439e1dbc1aSJani Nikula }
22449e1dbc1aSJani Nikula 
224532c2bc89SVille Syrjälä static u8 dvo_port_type(u8 dvo_port)
224632c2bc89SVille Syrjälä {
224732c2bc89SVille Syrjälä 	switch (dvo_port) {
224832c2bc89SVille Syrjälä 	case DVO_PORT_HDMIA:
224932c2bc89SVille Syrjälä 	case DVO_PORT_HDMIB:
225032c2bc89SVille Syrjälä 	case DVO_PORT_HDMIC:
225132c2bc89SVille Syrjälä 	case DVO_PORT_HDMID:
225232c2bc89SVille Syrjälä 	case DVO_PORT_HDMIE:
225332c2bc89SVille Syrjälä 	case DVO_PORT_HDMIF:
225432c2bc89SVille Syrjälä 	case DVO_PORT_HDMIG:
225532c2bc89SVille Syrjälä 	case DVO_PORT_HDMIH:
225632c2bc89SVille Syrjälä 	case DVO_PORT_HDMII:
225732c2bc89SVille Syrjälä 		return DVO_PORT_HDMIA;
225832c2bc89SVille Syrjälä 	case DVO_PORT_DPA:
225932c2bc89SVille Syrjälä 	case DVO_PORT_DPB:
226032c2bc89SVille Syrjälä 	case DVO_PORT_DPC:
226132c2bc89SVille Syrjälä 	case DVO_PORT_DPD:
226232c2bc89SVille Syrjälä 	case DVO_PORT_DPE:
226332c2bc89SVille Syrjälä 	case DVO_PORT_DPF:
226432c2bc89SVille Syrjälä 	case DVO_PORT_DPG:
226532c2bc89SVille Syrjälä 	case DVO_PORT_DPH:
226632c2bc89SVille Syrjälä 	case DVO_PORT_DPI:
226732c2bc89SVille Syrjälä 		return DVO_PORT_DPA;
226832c2bc89SVille Syrjälä 	case DVO_PORT_MIPIA:
226932c2bc89SVille Syrjälä 	case DVO_PORT_MIPIB:
227032c2bc89SVille Syrjälä 	case DVO_PORT_MIPIC:
227132c2bc89SVille Syrjälä 	case DVO_PORT_MIPID:
227232c2bc89SVille Syrjälä 		return DVO_PORT_MIPIA;
227332c2bc89SVille Syrjälä 	default:
227432c2bc89SVille Syrjälä 		return dvo_port;
227532c2bc89SVille Syrjälä 	}
227632c2bc89SVille Syrjälä }
227732c2bc89SVille Syrjälä 
22784628142aSLucas De Marchi static enum port __dvo_port_to_port(int n_ports, int n_dvo,
22794628142aSLucas De Marchi 				    const int port_mapping[][3], u8 dvo_port)
2280df0566a6SJani Nikula {
2281df0566a6SJani Nikula 	enum port port;
2282df0566a6SJani Nikula 	int i;
2283df0566a6SJani Nikula 
22844628142aSLucas De Marchi 	for (port = PORT_A; port < n_ports; port++) {
22854628142aSLucas De Marchi 		for (i = 0; i < n_dvo; i++) {
22864628142aSLucas De Marchi 			if (port_mapping[port][i] == -1)
2287df0566a6SJani Nikula 				break;
2288df0566a6SJani Nikula 
22894628142aSLucas De Marchi 			if (dvo_port == port_mapping[port][i])
2290df0566a6SJani Nikula 				return port;
2291df0566a6SJani Nikula 		}
2292df0566a6SJani Nikula 	}
2293df0566a6SJani Nikula 
2294df0566a6SJani Nikula 	return PORT_NONE;
2295df0566a6SJani Nikula }
2296df0566a6SJani Nikula 
2297dbd440d8SJani Nikula static enum port dvo_port_to_port(struct drm_i915_private *i915,
22984628142aSLucas De Marchi 				  u8 dvo_port)
22994628142aSLucas De Marchi {
23004628142aSLucas De Marchi 	/*
23014628142aSLucas De Marchi 	 * Each DDI port can have more than one value on the "DVO Port" field,
23024628142aSLucas De Marchi 	 * so look for all the possible values for each port.
23034628142aSLucas De Marchi 	 */
23044628142aSLucas De Marchi 	static const int port_mapping[][3] = {
23054628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
23064628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
23074628142aSLucas De Marchi 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
23084628142aSLucas De Marchi 		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
23098c1a8f12SMatt Roper 		[PORT_E] = { DVO_PORT_HDMIE, DVO_PORT_DPE, DVO_PORT_CRT },
23104628142aSLucas De Marchi 		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
23114628142aSLucas De Marchi 		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2312176430ccSVille Syrjälä 		[PORT_H] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2313176430ccSVille Syrjälä 		[PORT_I] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
23144628142aSLucas De Marchi 	};
23154628142aSLucas De Marchi 	/*
23161d8ca002SVille Syrjälä 	 * RKL VBT uses PHY based mapping. Combo PHYs A,B,C,D
23171d8ca002SVille Syrjälä 	 * map to DDI A,B,TC1,TC2 respectively.
23184628142aSLucas De Marchi 	 */
23194628142aSLucas De Marchi 	static const int rkl_port_mapping[][3] = {
23204628142aSLucas De Marchi 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
23214628142aSLucas De Marchi 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
23224628142aSLucas De Marchi 		[PORT_C] = { -1 },
23231d8ca002SVille Syrjälä 		[PORT_TC1] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
23241d8ca002SVille Syrjälä 		[PORT_TC2] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
23254628142aSLucas De Marchi 	};
232618c283dfSAditya Swarup 	/*
232718c283dfSAditya Swarup 	 * Alderlake S ports used in the driver are PORT_A, PORT_D, PORT_E,
232818c283dfSAditya Swarup 	 * PORT_F and PORT_G, we need to map that to correct VBT sections.
232918c283dfSAditya Swarup 	 */
233018c283dfSAditya Swarup 	static const int adls_port_mapping[][3] = {
233118c283dfSAditya Swarup 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
233218c283dfSAditya Swarup 		[PORT_B] = { -1 },
233318c283dfSAditya Swarup 		[PORT_C] = { -1 },
233418c283dfSAditya Swarup 		[PORT_TC1] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
233518c283dfSAditya Swarup 		[PORT_TC2] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
233618c283dfSAditya Swarup 		[PORT_TC3] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
233718c283dfSAditya Swarup 		[PORT_TC4] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
233818c283dfSAditya Swarup 	};
2339eeb63c54SJosé Roberto de Souza 	static const int xelpd_port_mapping[][3] = {
2340eeb63c54SJosé Roberto de Souza 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2341eeb63c54SJosé Roberto de Souza 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2342eeb63c54SJosé Roberto de Souza 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2343eeb63c54SJosé Roberto de Souza 		[PORT_D_XELPD] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2344eeb63c54SJosé Roberto de Souza 		[PORT_E_XELPD] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
2345eeb63c54SJosé Roberto de Souza 		[PORT_TC1] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
2346eeb63c54SJosé Roberto de Souza 		[PORT_TC2] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2347eeb63c54SJosé Roberto de Souza 		[PORT_TC3] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2348eeb63c54SJosé Roberto de Souza 		[PORT_TC4] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
2349eeb63c54SJosé Roberto de Souza 	};
23504628142aSLucas De Marchi 
2351612dc414SImre Deak 	if (DISPLAY_VER(i915) >= 13)
2352eeb63c54SJosé Roberto de Souza 		return __dvo_port_to_port(ARRAY_SIZE(xelpd_port_mapping),
2353eeb63c54SJosé Roberto de Souza 					  ARRAY_SIZE(xelpd_port_mapping[0]),
2354eeb63c54SJosé Roberto de Souza 					  xelpd_port_mapping,
2355eeb63c54SJosé Roberto de Souza 					  dvo_port);
2356eeb63c54SJosé Roberto de Souza 	else if (IS_ALDERLAKE_S(i915))
235718c283dfSAditya Swarup 		return __dvo_port_to_port(ARRAY_SIZE(adls_port_mapping),
235818c283dfSAditya Swarup 					  ARRAY_SIZE(adls_port_mapping[0]),
235918c283dfSAditya Swarup 					  adls_port_mapping,
236018c283dfSAditya Swarup 					  dvo_port);
2361dbd440d8SJani Nikula 	else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
23624628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(rkl_port_mapping),
23634628142aSLucas De Marchi 					  ARRAY_SIZE(rkl_port_mapping[0]),
23644628142aSLucas De Marchi 					  rkl_port_mapping,
23654628142aSLucas De Marchi 					  dvo_port);
23664628142aSLucas De Marchi 	else
23674628142aSLucas De Marchi 		return __dvo_port_to_port(ARRAY_SIZE(port_mapping),
23684628142aSLucas De Marchi 					  ARRAY_SIZE(port_mapping[0]),
23694628142aSLucas De Marchi 					  port_mapping,
23704628142aSLucas De Marchi 					  dvo_port);
23714628142aSLucas De Marchi }
23724628142aSLucas De Marchi 
2373118b5c13SVille Syrjälä static enum port
2374118b5c13SVille Syrjälä dsi_dvo_port_to_port(struct drm_i915_private *i915, u8 dvo_port)
2375118b5c13SVille Syrjälä {
2376118b5c13SVille Syrjälä 	switch (dvo_port) {
2377118b5c13SVille Syrjälä 	case DVO_PORT_MIPIA:
2378118b5c13SVille Syrjälä 		return PORT_A;
2379118b5c13SVille Syrjälä 	case DVO_PORT_MIPIC:
2380118b5c13SVille Syrjälä 		if (DISPLAY_VER(i915) >= 11)
2381118b5c13SVille Syrjälä 			return PORT_B;
2382118b5c13SVille Syrjälä 		else
2383118b5c13SVille Syrjälä 			return PORT_C;
2384118b5c13SVille Syrjälä 	default:
2385118b5c13SVille Syrjälä 		return PORT_NONE;
2386118b5c13SVille Syrjälä 	}
2387118b5c13SVille Syrjälä }
2388118b5c13SVille Syrjälä 
2389021a62a5SVille Syrjälä enum port intel_bios_encoder_port(const struct intel_bios_encoder_data *devdata)
2390d84b1945SVille Syrjälä {
2391d84b1945SVille Syrjälä 	struct drm_i915_private *i915 = devdata->i915;
2392d84b1945SVille Syrjälä 	const struct child_device_config *child = &devdata->child;
2393d84b1945SVille Syrjälä 	enum port port;
2394d84b1945SVille Syrjälä 
2395d84b1945SVille Syrjälä 	port = dvo_port_to_port(i915, child->dvo_port);
2396d84b1945SVille Syrjälä 	if (port == PORT_NONE && DISPLAY_VER(i915) >= 11)
2397d84b1945SVille Syrjälä 		port = dsi_dvo_port_to_port(i915, child->dvo_port);
2398d84b1945SVille Syrjälä 
2399d84b1945SVille Syrjälä 	return port;
2400d84b1945SVille Syrjälä }
2401d84b1945SVille Syrjälä 
2402b60e320bSLee Shawn C static int parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)
2403b60e320bSLee Shawn C {
2404b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
2405b60e320bSLee Shawn C 	default:
2406b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_DEF:
2407b60e320bSLee Shawn C 		return 0;
2408b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR20:
2409b60e320bSLee Shawn C 		return 2000000;
2410b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR13P5:
2411b60e320bSLee Shawn C 		return 1350000;
2412b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR10:
2413b60e320bSLee Shawn C 		return 1000000;
2414b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR3:
2415b60e320bSLee Shawn C 		return 810000;
2416b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR2:
2417b60e320bSLee Shawn C 		return 540000;
2418b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR:
2419b60e320bSLee Shawn C 		return 270000;
2420b60e320bSLee Shawn C 	case BDB_230_VBT_DP_MAX_LINK_RATE_LBR:
2421b60e320bSLee Shawn C 		return 162000;
2422b60e320bSLee Shawn C 	}
2423b60e320bSLee Shawn C }
2424b60e320bSLee Shawn C 
2425b60e320bSLee Shawn C static int parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)
2426b60e320bSLee Shawn C {
2427b60e320bSLee Shawn C 	switch (vbt_max_link_rate) {
2428b60e320bSLee Shawn C 	default:
2429b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR3:
2430b60e320bSLee Shawn C 		return 810000;
2431b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR2:
2432b60e320bSLee Shawn C 		return 540000;
2433b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR:
2434b60e320bSLee Shawn C 		return 270000;
2435b60e320bSLee Shawn C 	case BDB_216_VBT_DP_MAX_LINK_RATE_LBR:
2436b60e320bSLee Shawn C 		return 162000;
2437b60e320bSLee Shawn C 	}
2438b60e320bSLee Shawn C }
2439b60e320bSLee Shawn C 
244002107ef1SVille Syrjälä int intel_bios_dp_max_link_rate(const struct intel_bios_encoder_data *devdata)
244172337aacSJani Nikula {
2442a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 216)
244372337aacSJani Nikula 		return 0;
244472337aacSJani Nikula 
2445a434689cSJani Nikula 	if (devdata->i915->display.vbt.version >= 230)
244672337aacSJani Nikula 		return parse_bdb_230_dp_max_link_rate(devdata->child.dp_max_link_rate);
244772337aacSJani Nikula 	else
244872337aacSJani Nikula 		return parse_bdb_216_dp_max_link_rate(devdata->child.dp_max_link_rate);
244972337aacSJani Nikula }
245072337aacSJani Nikula 
245102107ef1SVille Syrjälä int intel_bios_dp_max_lane_count(const struct intel_bios_encoder_data *devdata)
24524182a311SVille Syrjälä {
24534182a311SVille Syrjälä 	if (!devdata || devdata->i915->display.vbt.version < 244)
24544182a311SVille Syrjälä 		return 0;
24554182a311SVille Syrjälä 
24564182a311SVille Syrjälä 	return devdata->child.dp_max_lane_count + 1;
24574182a311SVille Syrjälä }
24584182a311SVille Syrjälä 
2459d0ab409dSJani Nikula static void sanitize_device_type(struct intel_bios_encoder_data *devdata,
2460d0ab409dSJani Nikula 				 enum port port)
2461d0ab409dSJani Nikula {
2462d0ab409dSJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
2463d0ab409dSJani Nikula 	bool is_hdmi;
2464d0ab409dSJani Nikula 
2465005e9537SMatt Roper 	if (port != PORT_A || DISPLAY_VER(i915) >= 12)
2466d0ab409dSJani Nikula 		return;
2467d0ab409dSJani Nikula 
246886996822SJani Nikula 	if (!intel_bios_encoder_supports_dvi(devdata))
2469d0ab409dSJani Nikula 		return;
2470d0ab409dSJani Nikula 
247186996822SJani Nikula 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2472d0ab409dSJani Nikula 
2473d0ab409dSJani Nikula 	drm_dbg_kms(&i915->drm, "VBT claims port A supports DVI%s, ignoring\n",
2474d0ab409dSJani Nikula 		    is_hdmi ? "/HDMI" : "");
2475d0ab409dSJani Nikula 
2476d0ab409dSJani Nikula 	devdata->child.device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
2477d0ab409dSJani Nikula 	devdata->child.device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
2478d0ab409dSJani Nikula }
2479d0ab409dSJani Nikula 
24809e372744SVille Syrjälä static void sanitize_hdmi_level_shift(struct intel_bios_encoder_data *devdata,
24819e372744SVille Syrjälä 				      enum port port)
24829e372744SVille Syrjälä {
24839e372744SVille Syrjälä 	struct drm_i915_private *i915 = devdata->i915;
24849e372744SVille Syrjälä 
24859e372744SVille Syrjälä 	if (!intel_bios_encoder_supports_dvi(devdata))
24869e372744SVille Syrjälä 		return;
24879e372744SVille Syrjälä 
24889e372744SVille Syrjälä 	/*
24899e372744SVille Syrjälä 	 * Some BDW machines (eg. HP Pavilion 15-ab) shipped
24909e372744SVille Syrjälä 	 * with a HSW VBT where the level shifter value goes
24919e372744SVille Syrjälä 	 * up to 11, whereas the BDW max is 9.
24929e372744SVille Syrjälä 	 */
24939e372744SVille Syrjälä 	if (IS_BROADWELL(i915) && devdata->child.hdmi_level_shifter_value > 9) {
24949e372744SVille Syrjälä 		drm_dbg_kms(&i915->drm, "Bogus port %c VBT HDMI level shift %d, adjusting to %d\n",
24959e372744SVille Syrjälä 			    port_name(port), devdata->child.hdmi_level_shifter_value, 9);
24969e372744SVille Syrjälä 
24979e372744SVille Syrjälä 		devdata->child.hdmi_level_shifter_value = 9;
24989e372744SVille Syrjälä 	}
24999e372744SVille Syrjälä }
25009e372744SVille Syrjälä 
2501d0ab409dSJani Nikula static bool
2502d0ab409dSJani Nikula intel_bios_encoder_supports_crt(const struct intel_bios_encoder_data *devdata)
2503d0ab409dSJani Nikula {
2504d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
2505d0ab409dSJani Nikula }
2506d0ab409dSJani Nikula 
250745c0673aSJani Nikula bool
2508d0ab409dSJani Nikula intel_bios_encoder_supports_dvi(const struct intel_bios_encoder_data *devdata)
2509d0ab409dSJani Nikula {
2510d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
2511d0ab409dSJani Nikula }
2512d0ab409dSJani Nikula 
251345c0673aSJani Nikula bool
2514d0ab409dSJani Nikula intel_bios_encoder_supports_hdmi(const struct intel_bios_encoder_data *devdata)
2515d0ab409dSJani Nikula {
2516d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dvi(devdata) &&
2517d0ab409dSJani Nikula 		(devdata->child.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
2518d0ab409dSJani Nikula }
2519d0ab409dSJani Nikula 
252045c0673aSJani Nikula bool
2521d0ab409dSJani Nikula intel_bios_encoder_supports_dp(const struct intel_bios_encoder_data *devdata)
2522d0ab409dSJani Nikula {
2523d0ab409dSJani Nikula 	return devdata->child.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2524d0ab409dSJani Nikula }
2525d0ab409dSJani Nikula 
25269d4b7af5SVille Syrjälä bool
2527d0ab409dSJani Nikula intel_bios_encoder_supports_edp(const struct intel_bios_encoder_data *devdata)
2528d0ab409dSJani Nikula {
2529d0ab409dSJani Nikula 	return intel_bios_encoder_supports_dp(devdata) &&
2530d0ab409dSJani Nikula 		devdata->child.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR;
2531d0ab409dSJani Nikula }
2532d0ab409dSJani Nikula 
2533021a62a5SVille Syrjälä bool
2534ba00eb6aSVille Syrjälä intel_bios_encoder_supports_dsi(const struct intel_bios_encoder_data *devdata)
2535ba00eb6aSVille Syrjälä {
2536ba00eb6aSVille Syrjälä 	return devdata->child.device_type & DEVICE_TYPE_MIPI_OUTPUT;
2537ba00eb6aSVille Syrjälä }
2538ba00eb6aSVille Syrjälä 
2539db5d650fSVille Syrjälä bool
2540db5d650fSVille Syrjälä intel_bios_encoder_is_lspcon(const struct intel_bios_encoder_data *devdata)
2541db5d650fSVille Syrjälä {
2542db5d650fSVille Syrjälä 	return devdata && HAS_LSPCON(devdata->i915) && devdata->child.lspcon;
2543db5d650fSVille Syrjälä }
2544db5d650fSVille Syrjälä 
254502107ef1SVille Syrjälä /* This is an index in the HDMI/DVI DDI buffer translation table, or -1 */
254602107ef1SVille Syrjälä int intel_bios_hdmi_level_shift(const struct intel_bios_encoder_data *devdata)
2547a9a56e76SJani Nikula {
2548d9c078d3SRadhakrishna Sripada 	if (!devdata || devdata->i915->display.vbt.version < 158 ||
2549d9c078d3SRadhakrishna Sripada 	    DISPLAY_VER(devdata->i915) >= 14)
2550a9a56e76SJani Nikula 		return -1;
2551a9a56e76SJani Nikula 
2552a9a56e76SJani Nikula 	return devdata->child.hdmi_level_shifter_value;
2553a9a56e76SJani Nikula }
2554a9a56e76SJani Nikula 
255502107ef1SVille Syrjälä int intel_bios_hdmi_max_tmds_clock(const struct intel_bios_encoder_data *devdata)
25566ba69981SJani Nikula {
2557a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 204)
25586ba69981SJani Nikula 		return 0;
25596ba69981SJani Nikula 
25606ba69981SJani Nikula 	switch (devdata->child.hdmi_max_data_rate) {
25616ba69981SJani Nikula 	default:
25626ba69981SJani Nikula 		MISSING_CASE(devdata->child.hdmi_max_data_rate);
25636ba69981SJani Nikula 		fallthrough;
25646ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_PLATFORM:
25656ba69981SJani Nikula 		return 0;
25665708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_594:
25675708fe0dSLee Shawn C 		return 594000;
25685708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_340:
25695708fe0dSLee Shawn C 		return 340000;
25705708fe0dSLee Shawn C 	case HDMI_MAX_DATA_RATE_300:
25715708fe0dSLee Shawn C 		return 300000;
25726ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_297:
25736ba69981SJani Nikula 		return 297000;
25746ba69981SJani Nikula 	case HDMI_MAX_DATA_RATE_165:
25756ba69981SJani Nikula 		return 165000;
25766ba69981SJani Nikula 	}
25776ba69981SJani Nikula }
25786ba69981SJani Nikula 
25795a9d38b2SLucas De Marchi static bool is_port_valid(struct drm_i915_private *i915, enum port port)
25805a9d38b2SLucas De Marchi {
25815a9d38b2SLucas De Marchi 	/*
2582cad83b40SLucas De Marchi 	 * On some ICL SKUs port F is not present, but broken VBTs mark
25835a9d38b2SLucas De Marchi 	 * the port as present. Only try to initialize port F for the
25845a9d38b2SLucas De Marchi 	 * SKUs that may actually have it.
25855a9d38b2SLucas De Marchi 	 */
2586cad83b40SLucas De Marchi 	if (port == PORT_F && IS_ICELAKE(i915))
2587cad83b40SLucas De Marchi 		return IS_ICL_WITH_PORT_F(i915);
25885a9d38b2SLucas De Marchi 
25895a9d38b2SLucas De Marchi 	return true;
25905a9d38b2SLucas De Marchi }
25915a9d38b2SLucas De Marchi 
2592021a62a5SVille Syrjälä static void print_ddi_port(const struct intel_bios_encoder_data *devdata)
2593df0566a6SJani Nikula {
2594c78783f3SJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
2595d1dad6f4SJani Nikula 	const struct child_device_config *child = &devdata->child;
2596ba00eb6aSVille Syrjälä 	bool is_dvi, is_hdmi, is_dp, is_edp, is_dsi, is_crt, supports_typec_usb, supports_tbt;
259772337aacSJani Nikula 	int dp_boost_level, dp_max_link_rate, hdmi_boost_level, hdmi_level_shift, max_tmds_clock;
2598021a62a5SVille Syrjälä 	enum port port;
2599021a62a5SVille Syrjälä 
2600021a62a5SVille Syrjälä 	port = intel_bios_encoder_port(devdata);
2601021a62a5SVille Syrjälä 	if (port == PORT_NONE)
2602021a62a5SVille Syrjälä 		return;
2603df0566a6SJani Nikula 
2604d0ab409dSJani Nikula 	is_dvi = intel_bios_encoder_supports_dvi(devdata);
2605d0ab409dSJani Nikula 	is_dp = intel_bios_encoder_supports_dp(devdata);
2606d0ab409dSJani Nikula 	is_crt = intel_bios_encoder_supports_crt(devdata);
2607d0ab409dSJani Nikula 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2608d0ab409dSJani Nikula 	is_edp = intel_bios_encoder_supports_edp(devdata);
2609ba00eb6aSVille Syrjälä 	is_dsi = intel_bios_encoder_supports_dsi(devdata);
2610df0566a6SJani Nikula 
2611f08fbe6aSJani Nikula 	supports_typec_usb = intel_bios_encoder_supports_typec_usb(devdata);
2612f08fbe6aSJani Nikula 	supports_tbt = intel_bios_encoder_supports_tbt(devdata);
2613df0566a6SJani Nikula 
2614dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
26152bea1d7cSVille Syrjälä 		    "Port %c VBT info: CRT:%d DVI:%d HDMI:%d DP:%d eDP:%d DSI:%d DP++:%d LSPCON:%d USB-Type-C:%d TBT:%d DSC:%d\n",
2616ba00eb6aSVille Syrjälä 		    port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp, is_dsi,
26172bea1d7cSVille Syrjälä 		    intel_bios_encoder_supports_dp_dual_mode(devdata),
2618db5d650fSVille Syrjälä 		    intel_bios_encoder_is_lspcon(devdata),
2619f08fbe6aSJani Nikula 		    supports_typec_usb, supports_tbt,
26206e0d46e9SJani Nikula 		    devdata->dsc != NULL);
2621df0566a6SJani Nikula 
262202107ef1SVille Syrjälä 	hdmi_level_shift = intel_bios_hdmi_level_shift(devdata);
2623a9a56e76SJani Nikula 	if (hdmi_level_shift >= 0) {
2624dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
26256ee8d381SJani Nikula 			    "Port %c VBT HDMI level shift: %d\n",
2626a9a56e76SJani Nikula 			    port_name(port), hdmi_level_shift);
2627df0566a6SJani Nikula 	}
2628df0566a6SJani Nikula 
262902107ef1SVille Syrjälä 	max_tmds_clock = intel_bios_hdmi_max_tmds_clock(devdata);
2630df0566a6SJani Nikula 	if (max_tmds_clock)
2631dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
26326ee8d381SJani Nikula 			    "Port %c VBT HDMI max TMDS clock: %d kHz\n",
2633df0566a6SJani Nikula 			    port_name(port), max_tmds_clock);
2634df0566a6SJani Nikula 
2635c0a950d1SJani Nikula 	/* I_boost config for SKL and above */
263602107ef1SVille Syrjälä 	dp_boost_level = intel_bios_dp_boost_level(devdata);
2637c0a950d1SJani Nikula 	if (dp_boost_level)
2638dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
26396ee8d381SJani Nikula 			    "Port %c VBT (e)DP boost level: %d\n",
2640c0a950d1SJani Nikula 			    port_name(port), dp_boost_level);
2641c0a950d1SJani Nikula 
264202107ef1SVille Syrjälä 	hdmi_boost_level = intel_bios_hdmi_boost_level(devdata);
2643c0a950d1SJani Nikula 	if (hdmi_boost_level)
2644dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
26456ee8d381SJani Nikula 			    "Port %c VBT HDMI boost level: %d\n",
2646c0a950d1SJani Nikula 			    port_name(port), hdmi_boost_level);
2647df0566a6SJani Nikula 
264802107ef1SVille Syrjälä 	dp_max_link_rate = intel_bios_dp_max_link_rate(devdata);
264972337aacSJani Nikula 	if (dp_max_link_rate)
2650dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
26516ee8d381SJani Nikula 			    "Port %c VBT DP max link rate: %d\n",
265272337aacSJani Nikula 			    port_name(port), dp_max_link_rate);
2653429a0955SVille Syrjälä 
2654429a0955SVille Syrjälä 	/*
2655429a0955SVille Syrjälä 	 * FIXME need to implement support for VBT
2656429a0955SVille Syrjälä 	 * vswing/preemph tables should this ever trigger.
2657429a0955SVille Syrjälä 	 */
2658429a0955SVille Syrjälä 	drm_WARN(&i915->drm, child->use_vbt_vswing,
2659429a0955SVille Syrjälä 		 "Port %c asks to use VBT vswing/preemph tables\n",
2660429a0955SVille Syrjälä 		 port_name(port));
26618d2ba05bSJani Nikula }
26628d2ba05bSJani Nikula 
26638d2ba05bSJani Nikula static void parse_ddi_port(struct intel_bios_encoder_data *devdata)
26648d2ba05bSJani Nikula {
26658d2ba05bSJani Nikula 	struct drm_i915_private *i915 = devdata->i915;
26668d2ba05bSJani Nikula 	enum port port;
26678d2ba05bSJani Nikula 
2668d84b1945SVille Syrjälä 	port = intel_bios_encoder_port(devdata);
26698d2ba05bSJani Nikula 	if (port == PORT_NONE)
26708d2ba05bSJani Nikula 		return;
26718d2ba05bSJani Nikula 
26728d2ba05bSJani Nikula 	if (!is_port_valid(i915, port)) {
26738d2ba05bSJani Nikula 		drm_dbg_kms(&i915->drm,
26748d2ba05bSJani Nikula 			    "VBT reports port %c as supported, but that can't be true: skipping\n",
26758d2ba05bSJani Nikula 			    port_name(port));
26768d2ba05bSJani Nikula 		return;
26778d2ba05bSJani Nikula 	}
26788d2ba05bSJani Nikula 
26798d2ba05bSJani Nikula 	sanitize_device_type(devdata, port);
26809e372744SVille Syrjälä 	sanitize_hdmi_level_shift(devdata, port);
2681df0566a6SJani Nikula }
2682df0566a6SJani Nikula 
2683b90b6e41SVille Syrjälä static bool has_ddi_port_info(struct drm_i915_private *i915)
2684b90b6e41SVille Syrjälä {
2685594c504dSVille Syrjälä 	return DISPLAY_VER(i915) >= 5 || IS_G4X(i915);
2686b90b6e41SVille Syrjälä }
2687b90b6e41SVille Syrjälä 
2688ef0096e4SJani Nikula static void parse_ddi_ports(struct drm_i915_private *i915)
2689df0566a6SJani Nikula {
26903162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2691df0566a6SJani Nikula 
2692eb9fcf63SVille Syrjälä 	if (!has_ddi_port_info(i915))
2693df0566a6SJani Nikula 		return;
2694df0566a6SJani Nikula 
2695a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
2696c78783f3SJani Nikula 		parse_ddi_port(devdata);
2697e61f294cSJani Nikula 
2698021a62a5SVille Syrjälä 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
2699021a62a5SVille Syrjälä 		print_ddi_port(devdata);
2700df0566a6SJani Nikula }
2701df0566a6SJani Nikula 
2702df0566a6SJani Nikula static void
2703e163cfb4SVille Syrjälä parse_general_definitions(struct drm_i915_private *i915)
2704df0566a6SJani Nikula {
2705df0566a6SJani Nikula 	const struct bdb_general_definitions *defs;
27063162d057SJani Nikula 	struct intel_bios_encoder_data *devdata;
2707df0566a6SJani Nikula 	const struct child_device_config *child;
27080d9ef19bSJani Nikula 	int i, child_device_num;
2709df0566a6SJani Nikula 	u8 expected_size;
2710df0566a6SJani Nikula 	u16 block_size;
2711df0566a6SJani Nikula 	int bus_pin;
2712df0566a6SJani Nikula 
27130a93eeb5SMaarten Lankhorst 	defs = bdb_find_section(i915, BDB_GENERAL_DEFINITIONS);
2714df0566a6SJani Nikula 	if (!defs) {
2715dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2716e92cbf38SWambui Karuga 			    "No general definition block is found, no devices defined.\n");
2717df0566a6SJani Nikula 		return;
2718df0566a6SJani Nikula 	}
2719df0566a6SJani Nikula 
2720df0566a6SJani Nikula 	block_size = get_blocksize(defs);
2721df0566a6SJani Nikula 	if (block_size < sizeof(*defs)) {
2722dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2723e92cbf38SWambui Karuga 			    "General definitions block too small (%u)\n",
2724df0566a6SJani Nikula 			    block_size);
2725df0566a6SJani Nikula 		return;
2726df0566a6SJani Nikula 	}
2727df0566a6SJani Nikula 
2728df0566a6SJani Nikula 	bus_pin = defs->crt_ddc_gmbus_pin;
2729dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "crt_ddc_bus_pin: %d\n", bus_pin);
2730dbd440d8SJani Nikula 	if (intel_gmbus_is_valid_pin(i915, bus_pin))
2731a434689cSJani Nikula 		i915->display.vbt.crt_ddc_pin = bus_pin;
2732df0566a6SJani Nikula 
2733a434689cSJani Nikula 	if (i915->display.vbt.version < 106) {
2734df0566a6SJani Nikula 		expected_size = 22;
2735a434689cSJani Nikula 	} else if (i915->display.vbt.version < 111) {
2736df0566a6SJani Nikula 		expected_size = 27;
2737a434689cSJani Nikula 	} else if (i915->display.vbt.version < 195) {
2738df0566a6SJani Nikula 		expected_size = LEGACY_CHILD_DEVICE_CONFIG_SIZE;
2739a434689cSJani Nikula 	} else if (i915->display.vbt.version == 195) {
2740df0566a6SJani Nikula 		expected_size = 37;
2741a434689cSJani Nikula 	} else if (i915->display.vbt.version <= 215) {
2742df0566a6SJani Nikula 		expected_size = 38;
27430eaca1edSVille Syrjälä 	} else if (i915->display.vbt.version <= 250) {
2744df0566a6SJani Nikula 		expected_size = 39;
2745df0566a6SJani Nikula 	} else {
2746df0566a6SJani Nikula 		expected_size = sizeof(*child);
2747df0566a6SJani Nikula 		BUILD_BUG_ON(sizeof(*child) < 39);
2748dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
2749e92cbf38SWambui Karuga 			"Expected child device config size for VBT version %u not known; assuming %u\n",
2750a434689cSJani Nikula 			i915->display.vbt.version, expected_size);
2751df0566a6SJani Nikula 	}
2752df0566a6SJani Nikula 
2753df0566a6SJani Nikula 	/* Flag an error for unexpected size, but continue anyway. */
2754df0566a6SJani Nikula 	if (defs->child_dev_size != expected_size)
2755dbd440d8SJani Nikula 		drm_err(&i915->drm,
2756e92cbf38SWambui Karuga 			"Unexpected child device config size %u (expected %u for VBT version %u)\n",
2757a434689cSJani Nikula 			defs->child_dev_size, expected_size, i915->display.vbt.version);
2758df0566a6SJani Nikula 
2759df0566a6SJani Nikula 	/* The legacy sized child device config is the minimum we need. */
2760df0566a6SJani Nikula 	if (defs->child_dev_size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
2761dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2762e92cbf38SWambui Karuga 			    "Child device config size %u is too small.\n",
2763df0566a6SJani Nikula 			    defs->child_dev_size);
2764df0566a6SJani Nikula 		return;
2765df0566a6SJani Nikula 	}
2766df0566a6SJani Nikula 
2767df0566a6SJani Nikula 	/* get the number of child device */
2768df0566a6SJani Nikula 	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
2769df0566a6SJani Nikula 
2770df0566a6SJani Nikula 	for (i = 0; i < child_device_num; i++) {
2771df0566a6SJani Nikula 		child = child_device_ptr(defs, i);
2772df0566a6SJani Nikula 		if (!child->device_type)
2773df0566a6SJani Nikula 			continue;
2774df0566a6SJani Nikula 
2775dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2776e92cbf38SWambui Karuga 			    "Found VBT child device with type 0x%x\n",
2777bdeb18dbSMatt Roper 			    child->device_type);
2778bdeb18dbSMatt Roper 
27790d9ef19bSJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
27800d9ef19bSJani Nikula 		if (!devdata)
27810d9ef19bSJani Nikula 			break;
27820d9ef19bSJani Nikula 
27837371fa34SJani Nikula 		devdata->i915 = i915;
27847371fa34SJani Nikula 
2785df0566a6SJani Nikula 		/*
2786df0566a6SJani Nikula 		 * Copy as much as we know (sizeof) and is available
27870d9ef19bSJani Nikula 		 * (child_dev_size) of the child device config. Accessing the
27880d9ef19bSJani Nikula 		 * data must depend on VBT version.
2789df0566a6SJani Nikula 		 */
27900d9ef19bSJani Nikula 		memcpy(&devdata->child, child,
2791df0566a6SJani Nikula 		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
27920d9ef19bSJani Nikula 
2793a434689cSJani Nikula 		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
2794df0566a6SJani Nikula 	}
27950d9ef19bSJani Nikula 
2796a434689cSJani Nikula 	if (list_empty(&i915->display.vbt.display_devices))
2797dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
2798e92cbf38SWambui Karuga 			    "no child dev is parsed from VBT\n");
2799df0566a6SJani Nikula }
2800df0566a6SJani Nikula 
2801df0566a6SJani Nikula /* Common defaults which may be overridden by VBT. */
2802df0566a6SJani Nikula static void
2803dbd440d8SJani Nikula init_vbt_defaults(struct drm_i915_private *i915)
2804df0566a6SJani Nikula {
2805a434689cSJani Nikula 	i915->display.vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
2806df0566a6SJani Nikula 
2807df0566a6SJani Nikula 	/* general features */
2808a434689cSJani Nikula 	i915->display.vbt.int_tv_support = 1;
2809a434689cSJani Nikula 	i915->display.vbt.int_crt_support = 1;
2810df0566a6SJani Nikula 
2811df0566a6SJani Nikula 	/* driver features */
2812a434689cSJani Nikula 	i915->display.vbt.int_lvds_support = 1;
2813df0566a6SJani Nikula 
2814df0566a6SJani Nikula 	/* Default to using SSC */
2815a434689cSJani Nikula 	i915->display.vbt.lvds_use_ssc = 1;
2816df0566a6SJani Nikula 	/*
2817df0566a6SJani Nikula 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
2818df0566a6SJani Nikula 	 * clock for LVDS.
2819df0566a6SJani Nikula 	 */
2820a434689cSJani Nikula 	i915->display.vbt.lvds_ssc_freq = intel_bios_ssc_frequency(i915,
2821dbd440d8SJani Nikula 								   !HAS_PCH_SPLIT(i915));
2822dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm, "Set default to SSC at %d kHz\n",
2823a434689cSJani Nikula 		    i915->display.vbt.lvds_ssc_freq);
2824df0566a6SJani Nikula }
2825df0566a6SJani Nikula 
28263cf05076SVille Syrjälä /* Common defaults which may be overridden by VBT. */
28273cf05076SVille Syrjälä static void
28283cf05076SVille Syrjälä init_vbt_panel_defaults(struct intel_panel *panel)
28293cf05076SVille Syrjälä {
28303cf05076SVille Syrjälä 	/* Default to having backlight */
28313cf05076SVille Syrjälä 	panel->vbt.backlight.present = true;
28323cf05076SVille Syrjälä 
28333cf05076SVille Syrjälä 	/* LFP panel data */
28343cf05076SVille Syrjälä 	panel->vbt.lvds_dither = true;
28353cf05076SVille Syrjälä }
28363cf05076SVille Syrjälä 
2837df0566a6SJani Nikula /* Defaults to initialize only if there is no VBT. */
2838df0566a6SJani Nikula static void
2839dbd440d8SJani Nikula init_vbt_missing_defaults(struct drm_i915_private *i915)
2840df0566a6SJani Nikula {
2841df0566a6SJani Nikula 	enum port port;
28429b52aa72SRodrigo Vivi 	int ports = BIT(PORT_A) | BIT(PORT_B) | BIT(PORT_C) |
28439b52aa72SRodrigo Vivi 		    BIT(PORT_D) | BIT(PORT_E) | BIT(PORT_F);
2844df0566a6SJani Nikula 
2845e20e4037SJani Nikula 	if (!HAS_DDI(i915) && !IS_CHERRYVIEW(i915))
2846e20e4037SJani Nikula 		return;
2847e20e4037SJani Nikula 
28483ae04c0cSJani Nikula 	for_each_port_masked(port, ports) {
28493162d057SJani Nikula 		struct intel_bios_encoder_data *devdata;
285051f57481SJani Nikula 		struct child_device_config *child;
2851dbd440d8SJani Nikula 		enum phy phy = intel_port_to_phy(i915, port);
2852df0566a6SJani Nikula 
2853df0566a6SJani Nikula 		/*
2854df0566a6SJani Nikula 		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
2855df0566a6SJani Nikula 		 * to detect it.
2856df0566a6SJani Nikula 		 */
2857dbd440d8SJani Nikula 		if (intel_phy_is_tc(i915, phy))
2858df0566a6SJani Nikula 			continue;
2859df0566a6SJani Nikula 
286051f57481SJani Nikula 		/* Create fake child device config */
286151f57481SJani Nikula 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
286251f57481SJani Nikula 		if (!devdata)
286351f57481SJani Nikula 			break;
286451f57481SJani Nikula 
28657371fa34SJani Nikula 		devdata->i915 = i915;
286651f57481SJani Nikula 		child = &devdata->child;
286751f57481SJani Nikula 
286851f57481SJani Nikula 		if (port == PORT_F)
286951f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIF;
287051f57481SJani Nikula 		else if (port == PORT_E)
287151f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIE;
287251f57481SJani Nikula 		else
287351f57481SJani Nikula 			child->dvo_port = DVO_PORT_HDMIA + port;
287451f57481SJani Nikula 
287551f57481SJani Nikula 		if (port != PORT_A && port != PORT_E)
287651f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_TMDS_DVI_SIGNALING;
287751f57481SJani Nikula 
287851f57481SJani Nikula 		if (port != PORT_E)
287951f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_DISPLAYPORT_OUTPUT;
288051f57481SJani Nikula 
288151f57481SJani Nikula 		if (port == PORT_A)
288251f57481SJani Nikula 			child->device_type |= DEVICE_TYPE_INTERNAL_CONNECTOR;
288351f57481SJani Nikula 
2884a434689cSJani Nikula 		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
288551f57481SJani Nikula 
288651f57481SJani Nikula 		drm_dbg_kms(&i915->drm,
288751f57481SJani Nikula 			    "Generating default VBT child device with type 0x04%x on port %c\n",
288851f57481SJani Nikula 			    child->device_type, port_name(port));
2889df0566a6SJani Nikula 	}
289051f57481SJani Nikula 
289151f57481SJani Nikula 	/* Bypass some minimum baseline VBT version checks */
2892a434689cSJani Nikula 	i915->display.vbt.version = 155;
2893df0566a6SJani Nikula }
2894df0566a6SJani Nikula 
2895df0566a6SJani Nikula static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
2896df0566a6SJani Nikula {
2897df0566a6SJani Nikula 	const void *_vbt = vbt;
2898df0566a6SJani Nikula 
2899df0566a6SJani Nikula 	return _vbt + vbt->bdb_offset;
2900df0566a6SJani Nikula }
2901df0566a6SJani Nikula 
2902df0566a6SJani Nikula /**
2903df0566a6SJani Nikula  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
2904*ff9bc20cSVille Syrjälä  * @i915:	the device
2905df0566a6SJani Nikula  * @buf:	pointer to a buffer to validate
2906df0566a6SJani Nikula  * @size:	size of the buffer
2907df0566a6SJani Nikula  *
2908df0566a6SJani Nikula  * Returns true on valid VBT.
2909df0566a6SJani Nikula  */
2910*ff9bc20cSVille Syrjälä bool intel_bios_is_valid_vbt(struct drm_i915_private *i915,
2911*ff9bc20cSVille Syrjälä 			     const void *buf, size_t size)
2912df0566a6SJani Nikula {
2913df0566a6SJani Nikula 	const struct vbt_header *vbt = buf;
2914df0566a6SJani Nikula 	const struct bdb_header *bdb;
2915df0566a6SJani Nikula 
2916df0566a6SJani Nikula 	if (!vbt)
2917df0566a6SJani Nikula 		return false;
2918df0566a6SJani Nikula 
2919df0566a6SJani Nikula 	if (sizeof(struct vbt_header) > size) {
2920*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm, "VBT header incomplete\n");
2921df0566a6SJani Nikula 		return false;
2922df0566a6SJani Nikula 	}
2923df0566a6SJani Nikula 
2924df0566a6SJani Nikula 	if (memcmp(vbt->signature, "$VBT", 4)) {
2925*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm, "VBT invalid signature\n");
2926df0566a6SJani Nikula 		return false;
2927df0566a6SJani Nikula 	}
2928df0566a6SJani Nikula 
2929ff00ff96SLucas De Marchi 	if (vbt->vbt_size > size) {
2930*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm, "VBT incomplete (vbt_size overflows)\n");
2931ff00ff96SLucas De Marchi 		return false;
2932ff00ff96SLucas De Marchi 	}
2933ff00ff96SLucas De Marchi 
2934ff00ff96SLucas De Marchi 	size = vbt->vbt_size;
2935ff00ff96SLucas De Marchi 
2936df0566a6SJani Nikula 	if (range_overflows_t(size_t,
2937df0566a6SJani Nikula 			      vbt->bdb_offset,
2938df0566a6SJani Nikula 			      sizeof(struct bdb_header),
2939df0566a6SJani Nikula 			      size)) {
2940*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm, "BDB header incomplete\n");
2941df0566a6SJani Nikula 		return false;
2942df0566a6SJani Nikula 	}
2943df0566a6SJani Nikula 
2944df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
2945df0566a6SJani Nikula 	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
2946*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm, "BDB incomplete\n");
2947df0566a6SJani Nikula 		return false;
2948df0566a6SJani Nikula 	}
2949df0566a6SJani Nikula 
2950df0566a6SJani Nikula 	return vbt;
2951df0566a6SJani Nikula }
2952df0566a6SJani Nikula 
29533631c363SJani Nikula static u32 intel_spi_read(struct intel_uncore *uncore, u32 offset)
29543631c363SJani Nikula {
29553631c363SJani Nikula 	intel_uncore_write(uncore, PRIMARY_SPI_ADDRESS, offset);
29563631c363SJani Nikula 
29573631c363SJani Nikula 	return intel_uncore_read(uncore, PRIMARY_SPI_TRIGGER);
29583631c363SJani Nikula }
29593631c363SJani Nikula 
2960a36e7dc0SClint Taylor static struct vbt_header *spi_oprom_get_vbt(struct drm_i915_private *i915)
2961a36e7dc0SClint Taylor {
2962a36e7dc0SClint Taylor 	u32 count, data, found, store = 0;
2963a36e7dc0SClint Taylor 	u32 static_region, oprom_offset;
2964a36e7dc0SClint Taylor 	u32 oprom_size = 0x200000;
2965a36e7dc0SClint Taylor 	u16 vbt_size;
2966a36e7dc0SClint Taylor 	u32 *vbt;
2967a36e7dc0SClint Taylor 
2968a36e7dc0SClint Taylor 	static_region = intel_uncore_read(&i915->uncore, SPI_STATIC_REGIONS);
2969a36e7dc0SClint Taylor 	static_region &= OPTIONROM_SPI_REGIONID_MASK;
2970a36e7dc0SClint Taylor 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_REGIONID, static_region);
2971a36e7dc0SClint Taylor 
2972a36e7dc0SClint Taylor 	oprom_offset = intel_uncore_read(&i915->uncore, OROM_OFFSET);
2973a36e7dc0SClint Taylor 	oprom_offset &= OROM_OFFSET_MASK;
2974a36e7dc0SClint Taylor 
2975a36e7dc0SClint Taylor 	for (count = 0; count < oprom_size; count += 4) {
29763631c363SJani Nikula 		data = intel_spi_read(&i915->uncore, oprom_offset + count);
2977a36e7dc0SClint Taylor 		if (data == *((const u32 *)"$VBT")) {
2978a36e7dc0SClint Taylor 			found = oprom_offset + count;
2979a36e7dc0SClint Taylor 			break;
2980a36e7dc0SClint Taylor 		}
2981a36e7dc0SClint Taylor 	}
2982a36e7dc0SClint Taylor 
2983a36e7dc0SClint Taylor 	if (count >= oprom_size)
2984a36e7dc0SClint Taylor 		goto err_not_found;
2985a36e7dc0SClint Taylor 
2986a36e7dc0SClint Taylor 	/* Get VBT size and allocate space for the VBT */
29873631c363SJani Nikula 	vbt_size = intel_spi_read(&i915->uncore,
29883631c363SJani Nikula 				  found + offsetof(struct vbt_header, vbt_size));
2989a36e7dc0SClint Taylor 	vbt_size &= 0xffff;
2990a36e7dc0SClint Taylor 
2991980f42e7SJani Nikula 	vbt = kzalloc(round_up(vbt_size, 4), GFP_KERNEL);
2992a36e7dc0SClint Taylor 	if (!vbt)
2993a36e7dc0SClint Taylor 		goto err_not_found;
2994a36e7dc0SClint Taylor 
29953631c363SJani Nikula 	for (count = 0; count < vbt_size; count += 4)
29963631c363SJani Nikula 		*(vbt + store++) = intel_spi_read(&i915->uncore, found + count);
2997a36e7dc0SClint Taylor 
2998*ff9bc20cSVille Syrjälä 	if (!intel_bios_is_valid_vbt(i915, vbt, vbt_size))
2999a36e7dc0SClint Taylor 		goto err_free_vbt;
3000a36e7dc0SClint Taylor 
3001a36e7dc0SClint Taylor 	drm_dbg_kms(&i915->drm, "Found valid VBT in SPI flash\n");
3002a36e7dc0SClint Taylor 
3003a36e7dc0SClint Taylor 	return (struct vbt_header *)vbt;
3004a36e7dc0SClint Taylor 
3005a36e7dc0SClint Taylor err_free_vbt:
3006a36e7dc0SClint Taylor 	kfree(vbt);
3007a36e7dc0SClint Taylor err_not_found:
3008a36e7dc0SClint Taylor 	return NULL;
3009a36e7dc0SClint Taylor }
3010a36e7dc0SClint Taylor 
3011dbd440d8SJani Nikula static struct vbt_header *oprom_get_vbt(struct drm_i915_private *i915)
3012df0566a6SJani Nikula {
3013dbd440d8SJani Nikula 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
30142cded152SLucas De Marchi 	void __iomem *p = NULL, *oprom;
3015fd0186ceSLucas De Marchi 	struct vbt_header *vbt;
3016fd0186ceSLucas De Marchi 	u16 vbt_size;
30172cded152SLucas De Marchi 	size_t i, size;
30182cded152SLucas De Marchi 
30192cded152SLucas De Marchi 	oprom = pci_map_rom(pdev, &size);
30202cded152SLucas De Marchi 	if (!oprom)
30212cded152SLucas De Marchi 		return NULL;
3022df0566a6SJani Nikula 
3023df0566a6SJani Nikula 	/* Scour memory looking for the VBT signature. */
302498cf5c9aSLucas De Marchi 	for (i = 0; i + 4 < size; i += 4) {
3025496f50a6SLucas De Marchi 		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
3026df0566a6SJani Nikula 			continue;
3027df0566a6SJani Nikula 
3028fd0186ceSLucas De Marchi 		p = oprom + i;
3029fd0186ceSLucas De Marchi 		size -= i;
3030df0566a6SJani Nikula 		break;
3031df0566a6SJani Nikula 	}
3032df0566a6SJani Nikula 
3033fd0186ceSLucas De Marchi 	if (!p)
30342cded152SLucas De Marchi 		goto err_unmap_oprom;
3035fd0186ceSLucas De Marchi 
3036fd0186ceSLucas De Marchi 	if (sizeof(struct vbt_header) > size) {
3037dbd440d8SJani Nikula 		drm_dbg(&i915->drm, "VBT header incomplete\n");
30382cded152SLucas De Marchi 		goto err_unmap_oprom;
3039fd0186ceSLucas De Marchi 	}
3040fd0186ceSLucas De Marchi 
3041fd0186ceSLucas De Marchi 	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
3042fd0186ceSLucas De Marchi 	if (vbt_size > size) {
3043dbd440d8SJani Nikula 		drm_dbg(&i915->drm,
3044e92cbf38SWambui Karuga 			"VBT incomplete (vbt_size overflows)\n");
30452cded152SLucas De Marchi 		goto err_unmap_oprom;
3046fd0186ceSLucas De Marchi 	}
3047fd0186ceSLucas De Marchi 
3048fd0186ceSLucas De Marchi 	/* The rest will be validated by intel_bios_is_valid_vbt() */
3049fd0186ceSLucas De Marchi 	vbt = kmalloc(vbt_size, GFP_KERNEL);
3050fd0186ceSLucas De Marchi 	if (!vbt)
30512cded152SLucas De Marchi 		goto err_unmap_oprom;
3052fd0186ceSLucas De Marchi 
3053fd0186ceSLucas De Marchi 	memcpy_fromio(vbt, p, vbt_size);
3054fd0186ceSLucas De Marchi 
3055*ff9bc20cSVille Syrjälä 	if (!intel_bios_is_valid_vbt(i915, vbt, vbt_size))
3056fd0186ceSLucas De Marchi 		goto err_free_vbt;
3057fd0186ceSLucas De Marchi 
30582cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
30592cded152SLucas De Marchi 
3060a36e7dc0SClint Taylor 	drm_dbg_kms(&i915->drm, "Found valid VBT in PCI ROM\n");
3061a36e7dc0SClint Taylor 
3062fd0186ceSLucas De Marchi 	return vbt;
3063fd0186ceSLucas De Marchi 
3064fd0186ceSLucas De Marchi err_free_vbt:
3065fd0186ceSLucas De Marchi 	kfree(vbt);
30662cded152SLucas De Marchi err_unmap_oprom:
30672cded152SLucas De Marchi 	pci_unmap_rom(pdev, oprom);
3068fd0186ceSLucas De Marchi 
3069df0566a6SJani Nikula 	return NULL;
3070df0566a6SJani Nikula }
3071df0566a6SJani Nikula 
3072df0566a6SJani Nikula /**
3073df0566a6SJani Nikula  * intel_bios_init - find VBT and initialize settings from the BIOS
3074dbd440d8SJani Nikula  * @i915: i915 device instance
3075df0566a6SJani Nikula  *
3076df0566a6SJani Nikula  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
3077df0566a6SJani Nikula  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
3078df0566a6SJani Nikula  * initialize some defaults if the VBT is not present at all.
3079df0566a6SJani Nikula  */
3080dbd440d8SJani Nikula void intel_bios_init(struct drm_i915_private *i915)
3081df0566a6SJani Nikula {
308237e21003SJani Nikula 	const struct vbt_header *vbt;
30832cded152SLucas De Marchi 	struct vbt_header *oprom_vbt = NULL;
3084df0566a6SJani Nikula 	const struct bdb_header *bdb;
3085df0566a6SJani Nikula 
3086a434689cSJani Nikula 	INIT_LIST_HEAD(&i915->display.vbt.display_devices);
3087a434689cSJani Nikula 	INIT_LIST_HEAD(&i915->display.vbt.bdb_blocks);
30880d9ef19bSJani Nikula 
3089dbd440d8SJani Nikula 	if (!HAS_DISPLAY(i915)) {
3090dbd440d8SJani Nikula 		drm_dbg_kms(&i915->drm,
3091e92cbf38SWambui Karuga 			    "Skipping VBT init due to disabled display.\n");
3092df0566a6SJani Nikula 		return;
3093df0566a6SJani Nikula 	}
3094df0566a6SJani Nikula 
3095dbd440d8SJani Nikula 	init_vbt_defaults(i915);
3096df0566a6SJani Nikula 
309737e21003SJani Nikula 	vbt = intel_opregion_get_vbt(i915, NULL);
309837e21003SJani Nikula 
3099a36e7dc0SClint Taylor 	/*
3100a36e7dc0SClint Taylor 	 * If the OpRegion does not have VBT, look in SPI flash through MMIO or
3101a36e7dc0SClint Taylor 	 * PCI mapping
3102a36e7dc0SClint Taylor 	 */
3103a36e7dc0SClint Taylor 	if (!vbt && IS_DGFX(i915)) {
3104a36e7dc0SClint Taylor 		oprom_vbt = spi_oprom_get_vbt(i915);
3105a36e7dc0SClint Taylor 		vbt = oprom_vbt;
3106a36e7dc0SClint Taylor 	}
3107a36e7dc0SClint Taylor 
3108df0566a6SJani Nikula 	if (!vbt) {
3109dbd440d8SJani Nikula 		oprom_vbt = oprom_get_vbt(i915);
31102cded152SLucas De Marchi 		vbt = oprom_vbt;
3111df0566a6SJani Nikula 	}
3112df0566a6SJani Nikula 
3113a36e7dc0SClint Taylor 	if (!vbt)
3114a36e7dc0SClint Taylor 		goto out;
3115a36e7dc0SClint Taylor 
3116df0566a6SJani Nikula 	bdb = get_bdb_header(vbt);
3117a434689cSJani Nikula 	i915->display.vbt.version = bdb->version;
3118df0566a6SJani Nikula 
3119dbd440d8SJani Nikula 	drm_dbg_kms(&i915->drm,
3120e92cbf38SWambui Karuga 		    "VBT signature \"%.*s\", BDB version %d\n",
3121a434689cSJani Nikula 		    (int)sizeof(vbt->signature), vbt->signature, i915->display.vbt.version);
3122df0566a6SJani Nikula 
3123e163cfb4SVille Syrjälä 	init_bdb_blocks(i915, bdb);
3124e163cfb4SVille Syrjälä 
3125df0566a6SJani Nikula 	/* Grab useful general definitions */
3126e163cfb4SVille Syrjälä 	parse_general_features(i915);
3127e163cfb4SVille Syrjälä 	parse_general_definitions(i915);
3128e163cfb4SVille Syrjälä 	parse_driver_features(i915);
3129df0566a6SJani Nikula 
31306e0d46e9SJani Nikula 	/* Depends on child device list */
3131e163cfb4SVille Syrjälä 	parse_compression_parameters(i915);
31326e0d46e9SJani Nikula 
3133df0566a6SJani Nikula out:
3134df0566a6SJani Nikula 	if (!vbt) {
3135dbd440d8SJani Nikula 		drm_info(&i915->drm,
3136e92cbf38SWambui Karuga 			 "Failed to find VBIOS tables (VBT)\n");
3137dbd440d8SJani Nikula 		init_vbt_missing_defaults(i915);
3138df0566a6SJani Nikula 	}
3139df0566a6SJani Nikula 
314051f57481SJani Nikula 	/* Further processing on pre-parsed or generated child device data */
314151f57481SJani Nikula 	parse_sdvo_device_mapping(i915);
314251f57481SJani Nikula 	parse_ddi_ports(i915);
314351f57481SJani Nikula 
31442cded152SLucas De Marchi 	kfree(oprom_vbt);
3145df0566a6SJani Nikula }
3146df0566a6SJani Nikula 
31473f9ffce5SVille Syrjälä static void intel_bios_init_panel(struct drm_i915_private *i915,
3148c518a775SVille Syrjälä 				  struct intel_panel *panel,
31496434cf63SAnimesh Manna 				  const struct intel_bios_encoder_data *devdata,
3150c36225a1SJani Nikula 				  const struct drm_edid *drm_edid,
31513f9ffce5SVille Syrjälä 				  bool use_fallback)
3152c2fdb424SVille Syrjälä {
31533f9ffce5SVille Syrjälä 	/* already have it? */
31543f9ffce5SVille Syrjälä 	if (panel->vbt.panel_type >= 0) {
31553f9ffce5SVille Syrjälä 		drm_WARN_ON(&i915->drm, !use_fallback);
31563f9ffce5SVille Syrjälä 		return;
31573f9ffce5SVille Syrjälä 	}
31583cf05076SVille Syrjälä 
31593f9ffce5SVille Syrjälä 	panel->vbt.panel_type = get_panel_type(i915, devdata,
3160c36225a1SJani Nikula 					       drm_edid, use_fallback);
31613f9ffce5SVille Syrjälä 	if (panel->vbt.panel_type < 0) {
31623f9ffce5SVille Syrjälä 		drm_WARN_ON(&i915->drm, use_fallback);
31633f9ffce5SVille Syrjälä 		return;
31643f9ffce5SVille Syrjälä 	}
31653f9ffce5SVille Syrjälä 
31663f9ffce5SVille Syrjälä 	init_vbt_panel_defaults(panel);
31670256ea13SVille Syrjälä 
31680256ea13SVille Syrjälä 	parse_panel_options(i915, panel);
31693cf05076SVille Syrjälä 	parse_generic_dtd(i915, panel);
31703cf05076SVille Syrjälä 	parse_lfp_data(i915, panel);
31713cf05076SVille Syrjälä 	parse_lfp_backlight(i915, panel);
31723cf05076SVille Syrjälä 	parse_sdvo_panel_data(i915, panel);
31733cf05076SVille Syrjälä 	parse_panel_driver_features(i915, panel);
31743cf05076SVille Syrjälä 	parse_power_conservation_features(i915, panel);
31753cf05076SVille Syrjälä 	parse_edp(i915, panel);
31763cf05076SVille Syrjälä 	parse_psr(i915, panel);
31773cf05076SVille Syrjälä 	parse_mipi_config(i915, panel);
31783cf05076SVille Syrjälä 	parse_mipi_sequence(i915, panel);
3179c2fdb424SVille Syrjälä }
3180c2fdb424SVille Syrjälä 
31813f9ffce5SVille Syrjälä void intel_bios_init_panel_early(struct drm_i915_private *i915,
31823f9ffce5SVille Syrjälä 				 struct intel_panel *panel,
31833f9ffce5SVille Syrjälä 				 const struct intel_bios_encoder_data *devdata)
31843f9ffce5SVille Syrjälä {
31853f9ffce5SVille Syrjälä 	intel_bios_init_panel(i915, panel, devdata, NULL, false);
31863f9ffce5SVille Syrjälä }
31873f9ffce5SVille Syrjälä 
31883f9ffce5SVille Syrjälä void intel_bios_init_panel_late(struct drm_i915_private *i915,
31893f9ffce5SVille Syrjälä 				struct intel_panel *panel,
31903f9ffce5SVille Syrjälä 				const struct intel_bios_encoder_data *devdata,
3191c36225a1SJani Nikula 				const struct drm_edid *drm_edid)
31923f9ffce5SVille Syrjälä {
3193c36225a1SJani Nikula 	intel_bios_init_panel(i915, panel, devdata, drm_edid, true);
31943f9ffce5SVille Syrjälä }
31953f9ffce5SVille Syrjälä 
3196df0566a6SJani Nikula /**
319778dae1acSJanusz Krzysztofik  * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
3198dbd440d8SJani Nikula  * @i915: i915 device instance
3199df0566a6SJani Nikula  */
3200dbd440d8SJani Nikula void intel_bios_driver_remove(struct drm_i915_private *i915)
3201df0566a6SJani Nikula {
3202e163cfb4SVille Syrjälä 	struct intel_bios_encoder_data *devdata, *nd;
3203e163cfb4SVille Syrjälä 	struct bdb_block_entry *entry, *ne;
32040d9ef19bSJani Nikula 
3205a434689cSJani Nikula 	list_for_each_entry_safe(devdata, nd, &i915->display.vbt.display_devices, node) {
32060d9ef19bSJani Nikula 		list_del(&devdata->node);
32076e0d46e9SJani Nikula 		kfree(devdata->dsc);
32080d9ef19bSJani Nikula 		kfree(devdata);
32090d9ef19bSJani Nikula 	}
32100d9ef19bSJani Nikula 
3211a434689cSJani Nikula 	list_for_each_entry_safe(entry, ne, &i915->display.vbt.bdb_blocks, node) {
3212e163cfb4SVille Syrjälä 		list_del(&entry->node);
3213e163cfb4SVille Syrjälä 		kfree(entry);
3214e163cfb4SVille Syrjälä 	}
32153cf05076SVille Syrjälä }
3216e163cfb4SVille Syrjälä 
32173cf05076SVille Syrjälä void intel_bios_fini_panel(struct intel_panel *panel)
32183cf05076SVille Syrjälä {
32193cf05076SVille Syrjälä 	kfree(panel->vbt.sdvo_lvds_vbt_mode);
32203cf05076SVille Syrjälä 	panel->vbt.sdvo_lvds_vbt_mode = NULL;
32213cf05076SVille Syrjälä 	kfree(panel->vbt.lfp_lvds_vbt_mode);
32223cf05076SVille Syrjälä 	panel->vbt.lfp_lvds_vbt_mode = NULL;
32233cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.data);
32243cf05076SVille Syrjälä 	panel->vbt.dsi.data = NULL;
32253cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.pps);
32263cf05076SVille Syrjälä 	panel->vbt.dsi.pps = NULL;
32273cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.config);
32283cf05076SVille Syrjälä 	panel->vbt.dsi.config = NULL;
32293cf05076SVille Syrjälä 	kfree(panel->vbt.dsi.deassert_seq);
32303cf05076SVille Syrjälä 	panel->vbt.dsi.deassert_seq = NULL;
3231df0566a6SJani Nikula }
3232df0566a6SJani Nikula 
3233df0566a6SJani Nikula /**
3234df0566a6SJani Nikula  * intel_bios_is_tv_present - is integrated TV present in VBT
3235dbd440d8SJani Nikula  * @i915: i915 device instance
3236df0566a6SJani Nikula  *
3237df0566a6SJani Nikula  * Return true if TV is present. If no child devices were parsed from VBT,
3238df0566a6SJani Nikula  * assume TV is present.
3239df0566a6SJani Nikula  */
3240dbd440d8SJani Nikula bool intel_bios_is_tv_present(struct drm_i915_private *i915)
3241df0566a6SJani Nikula {
32423162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3243df0566a6SJani Nikula 
3244a434689cSJani Nikula 	if (!i915->display.vbt.int_tv_support)
3245df0566a6SJani Nikula 		return false;
3246df0566a6SJani Nikula 
3247a434689cSJani Nikula 	if (list_empty(&i915->display.vbt.display_devices))
3248df0566a6SJani Nikula 		return true;
3249df0566a6SJani Nikula 
3250a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3251d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
32520d9ef19bSJani Nikula 
3253df0566a6SJani Nikula 		/*
3254df0566a6SJani Nikula 		 * If the device type is not TV, continue.
3255df0566a6SJani Nikula 		 */
3256df0566a6SJani Nikula 		switch (child->device_type) {
3257df0566a6SJani Nikula 		case DEVICE_TYPE_INT_TV:
3258df0566a6SJani Nikula 		case DEVICE_TYPE_TV:
3259df0566a6SJani Nikula 		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
3260df0566a6SJani Nikula 			break;
3261df0566a6SJani Nikula 		default:
3262df0566a6SJani Nikula 			continue;
3263df0566a6SJani Nikula 		}
3264df0566a6SJani Nikula 		/* Only when the addin_offset is non-zero, it is regarded
3265df0566a6SJani Nikula 		 * as present.
3266df0566a6SJani Nikula 		 */
3267df0566a6SJani Nikula 		if (child->addin_offset)
3268df0566a6SJani Nikula 			return true;
3269df0566a6SJani Nikula 	}
3270df0566a6SJani Nikula 
3271df0566a6SJani Nikula 	return false;
3272df0566a6SJani Nikula }
3273df0566a6SJani Nikula 
3274df0566a6SJani Nikula /**
3275df0566a6SJani Nikula  * intel_bios_is_lvds_present - is LVDS present in VBT
3276dbd440d8SJani Nikula  * @i915:	i915 device instance
3277df0566a6SJani Nikula  * @i2c_pin:	i2c pin for LVDS if present
3278df0566a6SJani Nikula  *
3279df0566a6SJani Nikula  * Return true if LVDS is present. If no child devices were parsed from VBT,
3280df0566a6SJani Nikula  * assume LVDS is present.
3281df0566a6SJani Nikula  */
3282dbd440d8SJani Nikula bool intel_bios_is_lvds_present(struct drm_i915_private *i915, u8 *i2c_pin)
3283df0566a6SJani Nikula {
32843162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3285df0566a6SJani Nikula 
3286a434689cSJani Nikula 	if (list_empty(&i915->display.vbt.display_devices))
3287df0566a6SJani Nikula 		return true;
3288df0566a6SJani Nikula 
3289a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3290d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3291df0566a6SJani Nikula 
3292df0566a6SJani Nikula 		/* If the device type is not LFP, continue.
3293df0566a6SJani Nikula 		 * We have to check both the new identifiers as well as the
3294df0566a6SJani Nikula 		 * old for compatibility with some BIOSes.
3295df0566a6SJani Nikula 		 */
3296df0566a6SJani Nikula 		if (child->device_type != DEVICE_TYPE_INT_LFP &&
3297df0566a6SJani Nikula 		    child->device_type != DEVICE_TYPE_LFP)
3298df0566a6SJani Nikula 			continue;
3299df0566a6SJani Nikula 
3300dbd440d8SJani Nikula 		if (intel_gmbus_is_valid_pin(i915, child->i2c_pin))
3301df0566a6SJani Nikula 			*i2c_pin = child->i2c_pin;
3302df0566a6SJani Nikula 
3303df0566a6SJani Nikula 		/* However, we cannot trust the BIOS writers to populate
3304df0566a6SJani Nikula 		 * the VBT correctly.  Since LVDS requires additional
3305df0566a6SJani Nikula 		 * information from AIM blocks, a non-zero addin offset is
3306df0566a6SJani Nikula 		 * a good indicator that the LVDS is actually present.
3307df0566a6SJani Nikula 		 */
3308df0566a6SJani Nikula 		if (child->addin_offset)
3309df0566a6SJani Nikula 			return true;
3310df0566a6SJani Nikula 
3311df0566a6SJani Nikula 		/* But even then some BIOS writers perform some black magic
3312df0566a6SJani Nikula 		 * and instantiate the device without reference to any
3313df0566a6SJani Nikula 		 * additional data.  Trust that if the VBT was written into
3314df0566a6SJani Nikula 		 * the OpRegion then they have validated the LVDS's existence.
3315df0566a6SJani Nikula 		 */
331637e21003SJani Nikula 		if (intel_opregion_get_vbt(i915, NULL))
3317df0566a6SJani Nikula 			return true;
3318df0566a6SJani Nikula 	}
3319df0566a6SJani Nikula 
3320df0566a6SJani Nikula 	return false;
3321df0566a6SJani Nikula }
3322df0566a6SJani Nikula 
3323df0566a6SJani Nikula /**
3324df0566a6SJani Nikula  * intel_bios_is_port_present - is the specified digital port present
3325dbd440d8SJani Nikula  * @i915:	i915 device instance
3326df0566a6SJani Nikula  * @port:	port to check
3327df0566a6SJani Nikula  *
3328df0566a6SJani Nikula  * Return true if the device in %port is present.
3329df0566a6SJani Nikula  */
3330dbd440d8SJani Nikula bool intel_bios_is_port_present(struct drm_i915_private *i915, enum port port)
3331df0566a6SJani Nikula {
3332b17a15d6SVille Syrjälä 	const struct intel_bios_encoder_data *devdata;
3333b17a15d6SVille Syrjälä 
3334a868a1e5SVille Syrjälä 	if (WARN_ON(!has_ddi_port_info(i915)))
3335df0566a6SJani Nikula 		return true;
3336df0566a6SJani Nikula 
3337b17a15d6SVille Syrjälä 	if (!is_port_valid(i915, port))
3338b17a15d6SVille Syrjälä 		return false;
3339b17a15d6SVille Syrjälä 
3340b17a15d6SVille Syrjälä 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3341b17a15d6SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3342b17a15d6SVille Syrjälä 
3343b17a15d6SVille Syrjälä 		if (dvo_port_to_port(i915, child->dvo_port) == port)
3344b17a15d6SVille Syrjälä 			return true;
3345b17a15d6SVille Syrjälä 	}
3346b17a15d6SVille Syrjälä 
3347b17a15d6SVille Syrjälä 	return false;
3348df0566a6SJani Nikula }
3349df0566a6SJani Nikula 
33502bea1d7cSVille Syrjälä bool intel_bios_encoder_supports_dp_dual_mode(const struct intel_bios_encoder_data *devdata)
335132c2bc89SVille Syrjälä {
3352044cbc7aSVille Syrjälä 	const struct child_device_config *child = &devdata->child;
3353044cbc7aSVille Syrjälä 
3354044cbc7aSVille Syrjälä 	if (!intel_bios_encoder_supports_dp(devdata) ||
3355044cbc7aSVille Syrjälä 	    !intel_bios_encoder_supports_hdmi(devdata))
335632c2bc89SVille Syrjälä 		return false;
335732c2bc89SVille Syrjälä 
335832c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_DPA)
335932c2bc89SVille Syrjälä 		return true;
336032c2bc89SVille Syrjälä 
336132c2bc89SVille Syrjälä 	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
336232c2bc89SVille Syrjälä 	if (dvo_port_type(child->dvo_port) == DVO_PORT_HDMIA &&
336332c2bc89SVille Syrjälä 	    child->aux_channel != 0)
336432c2bc89SVille Syrjälä 		return true;
336532c2bc89SVille Syrjälä 
336632c2bc89SVille Syrjälä 	return false;
336732c2bc89SVille Syrjälä }
336832c2bc89SVille Syrjälä 
3369df0566a6SJani Nikula /**
3370df0566a6SJani Nikula  * intel_bios_is_dsi_present - is DSI present in VBT
3371dbd440d8SJani Nikula  * @i915:	i915 device instance
3372df0566a6SJani Nikula  * @port:	port for DSI if present
3373df0566a6SJani Nikula  *
3374df0566a6SJani Nikula  * Return true if DSI is present, and return the port in %port.
3375df0566a6SJani Nikula  */
3376dbd440d8SJani Nikula bool intel_bios_is_dsi_present(struct drm_i915_private *i915,
3377df0566a6SJani Nikula 			       enum port *port)
3378df0566a6SJani Nikula {
33793162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
3380df0566a6SJani Nikula 
3381a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3382d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
3383d24b3475SVille Syrjälä 		u8 dvo_port = child->dvo_port;
3384df0566a6SJani Nikula 
3385df0566a6SJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
3386df0566a6SJani Nikula 			continue;
3387df0566a6SJani Nikula 
3388118b5c13SVille Syrjälä 		if (dsi_dvo_port_to_port(i915, dvo_port) == PORT_NONE) {
3389dbd440d8SJani Nikula 			drm_dbg_kms(&i915->drm,
3390e92cbf38SWambui Karuga 				    "VBT has unsupported DSI port %c\n",
3391df0566a6SJani Nikula 				    port_name(dvo_port - DVO_PORT_MIPIA));
3392118b5c13SVille Syrjälä 			continue;
3393df0566a6SJani Nikula 		}
3394118b5c13SVille Syrjälä 
3395118b5c13SVille Syrjälä 		if (port)
3396118b5c13SVille Syrjälä 			*port = dsi_dvo_port_to_port(i915, dvo_port);
3397118b5c13SVille Syrjälä 		return true;
3398df0566a6SJani Nikula 	}
3399df0566a6SJani Nikula 
3400df0566a6SJani Nikula 	return false;
3401df0566a6SJani Nikula }
3402df0566a6SJani Nikula 
34031bf2f3bfSJani Nikula static void fill_dsc(struct intel_crtc_state *crtc_state,
34041bf2f3bfSJani Nikula 		     struct dsc_compression_parameters_entry *dsc,
34051bf2f3bfSJani Nikula 		     int dsc_max_bpc)
34061bf2f3bfSJani Nikula {
3407*ff9bc20cSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
34081bf2f3bfSJani Nikula 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
34091bf2f3bfSJani Nikula 	int bpc = 8;
34101bf2f3bfSJani Nikula 
34111bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_major = dsc->version_major;
34121bf2f3bfSJani Nikula 	vdsc_cfg->dsc_version_minor = dsc->version_minor;
34131bf2f3bfSJani Nikula 
34141bf2f3bfSJani Nikula 	if (dsc->support_12bpc && dsc_max_bpc >= 12)
34151bf2f3bfSJani Nikula 		bpc = 12;
34161bf2f3bfSJani Nikula 	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
34171bf2f3bfSJani Nikula 		bpc = 10;
34181bf2f3bfSJani Nikula 	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
34191bf2f3bfSJani Nikula 		bpc = 8;
34201bf2f3bfSJani Nikula 	else
3421*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm, "VBT: Unsupported BPC %d for DCS\n",
34221bf2f3bfSJani Nikula 			    dsc_max_bpc);
34231bf2f3bfSJani Nikula 
34241bf2f3bfSJani Nikula 	crtc_state->pipe_bpp = bpc * 3;
34251bf2f3bfSJani Nikula 
342659a266f0SAnkit Nautiyal 	crtc_state->dsc.compressed_bpp_x16 = to_bpp_x16(min(crtc_state->pipe_bpp,
342759a266f0SAnkit Nautiyal 							    VBT_DSC_MAX_BPP(dsc->max_bpp)));
34281bf2f3bfSJani Nikula 
34291bf2f3bfSJani Nikula 	/*
34301bf2f3bfSJani Nikula 	 * FIXME: This is ugly, and slice count should take DSC engine
34311bf2f3bfSJani Nikula 	 * throughput etc. into account.
34321bf2f3bfSJani Nikula 	 *
34331bf2f3bfSJani Nikula 	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
34341bf2f3bfSJani Nikula 	 */
34351bf2f3bfSJani Nikula 	if (dsc->slices_per_line & BIT(2)) {
34361bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 4;
34371bf2f3bfSJani Nikula 	} else if (dsc->slices_per_line & BIT(1)) {
34381bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 2;
34391bf2f3bfSJani Nikula 	} else {
34401bf2f3bfSJani Nikula 		/* FIXME */
34411bf2f3bfSJani Nikula 		if (!(dsc->slices_per_line & BIT(0)))
3442*ff9bc20cSVille Syrjälä 			drm_dbg_kms(&i915->drm, "VBT: Unsupported DSC slice count for DSI\n");
34431bf2f3bfSJani Nikula 
34441bf2f3bfSJani Nikula 		crtc_state->dsc.slice_count = 1;
34451bf2f3bfSJani Nikula 	}
34461bf2f3bfSJani Nikula 
34471bf2f3bfSJani Nikula 	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
34481bf2f3bfSJani Nikula 	    crtc_state->dsc.slice_count != 0)
3449*ff9bc20cSVille Syrjälä 		drm_dbg_kms(&i915->drm, "VBT: DSC hdisplay %d not divisible by slice count %d\n",
34501bf2f3bfSJani Nikula 			    crtc_state->hw.adjusted_mode.crtc_hdisplay,
34511bf2f3bfSJani Nikula 			    crtc_state->dsc.slice_count);
34521bf2f3bfSJani Nikula 
34531bf2f3bfSJani Nikula 	/*
34541bf2f3bfSJani Nikula 	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
3455fd8a5b27SJani Nikula 	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63.
34561bf2f3bfSJani Nikula 	 */
3457fd8a5b27SJani Nikula 	vdsc_cfg->rc_model_size = drm_dsc_dp_rc_buffer_size(dsc->rc_buffer_block_size,
3458fd8a5b27SJani Nikula 							    dsc->rc_buffer_size);
34591bf2f3bfSJani Nikula 
34601bf2f3bfSJani Nikula 	/* FIXME: DSI spec says bpc + 1 for this one */
34611bf2f3bfSJani Nikula 	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
34621bf2f3bfSJani Nikula 
34631bf2f3bfSJani Nikula 	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
34641bf2f3bfSJani Nikula 
34651bf2f3bfSJani Nikula 	vdsc_cfg->slice_height = dsc->slice_height;
34661bf2f3bfSJani Nikula }
34671bf2f3bfSJani Nikula 
34681bf2f3bfSJani Nikula /* FIXME: initially DSI specific */
34691bf2f3bfSJani Nikula bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
34701bf2f3bfSJani Nikula 			       struct intel_crtc_state *crtc_state,
34711bf2f3bfSJani Nikula 			       int dsc_max_bpc)
34721bf2f3bfSJani Nikula {
34731bf2f3bfSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
34743162d057SJani Nikula 	const struct intel_bios_encoder_data *devdata;
34751bf2f3bfSJani Nikula 
3476a434689cSJani Nikula 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3477d24b3475SVille Syrjälä 		const struct child_device_config *child = &devdata->child;
34781bf2f3bfSJani Nikula 
34791bf2f3bfSJani Nikula 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
34801bf2f3bfSJani Nikula 			continue;
34811bf2f3bfSJani Nikula 
3482118b5c13SVille Syrjälä 		if (dsi_dvo_port_to_port(i915, child->dvo_port) == encoder->port) {
34831bf2f3bfSJani Nikula 			if (!devdata->dsc)
34841bf2f3bfSJani Nikula 				return false;
34851bf2f3bfSJani Nikula 
34861bf2f3bfSJani Nikula 			fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
34871bf2f3bfSJani Nikula 
34881bf2f3bfSJani Nikula 			return true;
34891bf2f3bfSJani Nikula 		}
34901bf2f3bfSJani Nikula 	}
34911bf2f3bfSJani Nikula 
34921bf2f3bfSJani Nikula 	return false;
34931bf2f3bfSJani Nikula }
34941bf2f3bfSJani Nikula 
34955a0fc7a0SVille Syrjälä static const u8 adlp_aux_ch_map[] = {
34965a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
34975a0fc7a0SVille Syrjälä 	[AUX_CH_B] = DP_AUX_B,
34985a0fc7a0SVille Syrjälä 	[AUX_CH_C] = DP_AUX_C,
34995a0fc7a0SVille Syrjälä 	[AUX_CH_D_XELPD] = DP_AUX_D,
35005a0fc7a0SVille Syrjälä 	[AUX_CH_E_XELPD] = DP_AUX_E,
35015a0fc7a0SVille Syrjälä 	[AUX_CH_USBC1] = DP_AUX_F,
35025a0fc7a0SVille Syrjälä 	[AUX_CH_USBC2] = DP_AUX_G,
35035a0fc7a0SVille Syrjälä 	[AUX_CH_USBC3] = DP_AUX_H,
35045a0fc7a0SVille Syrjälä 	[AUX_CH_USBC4] = DP_AUX_I,
35055a0fc7a0SVille Syrjälä };
35065a0fc7a0SVille Syrjälä 
35075a0fc7a0SVille Syrjälä /*
35085a0fc7a0SVille Syrjälä  * ADL-S VBT uses PHY based mapping. Combo PHYs A,B,C,D,E
35095a0fc7a0SVille Syrjälä  * map to DDI A,TC1,TC2,TC3,TC4 respectively.
35105a0fc7a0SVille Syrjälä  */
35115a0fc7a0SVille Syrjälä static const u8 adls_aux_ch_map[] = {
35125a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
35135a0fc7a0SVille Syrjälä 	[AUX_CH_USBC1] = DP_AUX_B,
35145a0fc7a0SVille Syrjälä 	[AUX_CH_USBC2] = DP_AUX_C,
35155a0fc7a0SVille Syrjälä 	[AUX_CH_USBC3] = DP_AUX_D,
35165a0fc7a0SVille Syrjälä 	[AUX_CH_USBC4] = DP_AUX_E,
35175a0fc7a0SVille Syrjälä };
3518df0566a6SJani Nikula 
351918c283dfSAditya Swarup /*
352018c283dfSAditya Swarup  * RKL/DG1 VBT uses PHY based mapping. Combo PHYs A,B,C,D
352118c283dfSAditya Swarup  * map to DDI A,B,TC1,TC2 respectively.
352218c283dfSAditya Swarup  */
35235a0fc7a0SVille Syrjälä static const u8 rkl_aux_ch_map[] = {
35245a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
35255a0fc7a0SVille Syrjälä 	[AUX_CH_B] = DP_AUX_B,
35265a0fc7a0SVille Syrjälä 	[AUX_CH_USBC1] = DP_AUX_C,
35275a0fc7a0SVille Syrjälä 	[AUX_CH_USBC2] = DP_AUX_D,
35285a0fc7a0SVille Syrjälä };
35295a0fc7a0SVille Syrjälä 
35305a0fc7a0SVille Syrjälä static const u8 direct_aux_ch_map[] = {
35315a0fc7a0SVille Syrjälä 	[AUX_CH_A] = DP_AUX_A,
35325a0fc7a0SVille Syrjälä 	[AUX_CH_B] = DP_AUX_B,
35335a0fc7a0SVille Syrjälä 	[AUX_CH_C] = DP_AUX_C,
35345a0fc7a0SVille Syrjälä 	[AUX_CH_D] = DP_AUX_D, /* aka AUX_CH_USBC1 */
35355a0fc7a0SVille Syrjälä 	[AUX_CH_E] = DP_AUX_E, /* aka AUX_CH_USBC2 */
35365a0fc7a0SVille Syrjälä 	[AUX_CH_F] = DP_AUX_F, /* aka AUX_CH_USBC3 */
35375a0fc7a0SVille Syrjälä 	[AUX_CH_G] = DP_AUX_G, /* aka AUX_CH_USBC4 */
35385a0fc7a0SVille Syrjälä 	[AUX_CH_H] = DP_AUX_H, /* aka AUX_CH_USBC5 */
35395a0fc7a0SVille Syrjälä 	[AUX_CH_I] = DP_AUX_I, /* aka AUX_CH_USBC6 */
35405a0fc7a0SVille Syrjälä };
35415a0fc7a0SVille Syrjälä 
35425a0fc7a0SVille Syrjälä static enum aux_ch map_aux_ch(struct drm_i915_private *i915, u8 aux_channel)
35435a0fc7a0SVille Syrjälä {
35445a0fc7a0SVille Syrjälä 	const u8 *aux_ch_map;
35455a0fc7a0SVille Syrjälä 	int i, n_entries;
35465a0fc7a0SVille Syrjälä 
35475a0fc7a0SVille Syrjälä 	if (DISPLAY_VER(i915) >= 13) {
35485a0fc7a0SVille Syrjälä 		aux_ch_map = adlp_aux_ch_map;
35495a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(adlp_aux_ch_map);
35505a0fc7a0SVille Syrjälä 	} else if (IS_ALDERLAKE_S(i915)) {
35515a0fc7a0SVille Syrjälä 		aux_ch_map = adls_aux_ch_map;
35525a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(adls_aux_ch_map);
35535a0fc7a0SVille Syrjälä 	} else if (IS_DG1(i915) || IS_ROCKETLAKE(i915)) {
35545a0fc7a0SVille Syrjälä 		aux_ch_map = rkl_aux_ch_map;
35555a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(rkl_aux_ch_map);
35565a0fc7a0SVille Syrjälä 	} else {
35575a0fc7a0SVille Syrjälä 		aux_ch_map = direct_aux_ch_map;
35585a0fc7a0SVille Syrjälä 		n_entries = ARRAY_SIZE(direct_aux_ch_map);
3559df0566a6SJani Nikula 	}
3560df0566a6SJani Nikula 
35615a0fc7a0SVille Syrjälä 	for (i = 0; i < n_entries; i++) {
35625a0fc7a0SVille Syrjälä 		if (aux_ch_map[i] == aux_channel)
35635a0fc7a0SVille Syrjälä 			return i;
35645a0fc7a0SVille Syrjälä 	}
35655a0fc7a0SVille Syrjälä 
35665a0fc7a0SVille Syrjälä 	drm_dbg_kms(&i915->drm,
35675a0fc7a0SVille Syrjälä 		    "Ignoring alternate AUX CH: VBT claims AUX 0x%x, which is not valid for this platform\n",
35685a0fc7a0SVille Syrjälä 		    aux_channel);
35695a0fc7a0SVille Syrjälä 
35705a0fc7a0SVille Syrjälä 	return AUX_CH_NONE;
3571df0566a6SJani Nikula }
3572d9ee2111SJani Nikula 
3573bb45217fSVille Syrjälä enum aux_ch intel_bios_dp_aux_ch(const struct intel_bios_encoder_data *devdata)
3574bb45217fSVille Syrjälä {
3575bb45217fSVille Syrjälä 	if (!devdata || !devdata->child.aux_channel)
3576bb45217fSVille Syrjälä 		return AUX_CH_NONE;
3577bb45217fSVille Syrjälä 
3578bb45217fSVille Syrjälä 	return map_aux_ch(devdata->i915, devdata->child.aux_channel);
3579bb45217fSVille Syrjälä }
3580d9ee2111SJani Nikula 
358170052100SVille Syrjälä bool intel_bios_dp_has_shared_aux_ch(const struct intel_bios_encoder_data *devdata)
358270052100SVille Syrjälä {
358370052100SVille Syrjälä 	struct drm_i915_private *i915;
358470052100SVille Syrjälä 	u8 aux_channel;
358570052100SVille Syrjälä 	int count = 0;
358670052100SVille Syrjälä 
358770052100SVille Syrjälä 	if (!devdata || !devdata->child.aux_channel)
358870052100SVille Syrjälä 		return false;
358970052100SVille Syrjälä 
359070052100SVille Syrjälä 	i915 = devdata->i915;
359170052100SVille Syrjälä 	aux_channel = devdata->child.aux_channel;
359270052100SVille Syrjälä 
359370052100SVille Syrjälä 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
359470052100SVille Syrjälä 		if (intel_bios_encoder_supports_dp(devdata) &&
359570052100SVille Syrjälä 		    aux_channel == devdata->child.aux_channel)
359670052100SVille Syrjälä 			count++;
359770052100SVille Syrjälä 	}
359870052100SVille Syrjälä 
359970052100SVille Syrjälä 	return count > 1;
360070052100SVille Syrjälä }
360170052100SVille Syrjälä 
360202107ef1SVille Syrjälä int intel_bios_dp_boost_level(const struct intel_bios_encoder_data *devdata)
3603605a1872SJani Nikula {
3604a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3605c0a950d1SJani Nikula 		return 0;
3606605a1872SJani Nikula 
3607*ff9bc20cSVille Syrjälä 	return translate_iboost(devdata->i915, devdata->child.dp_iboost_level);
3608605a1872SJani Nikula }
360901a60883SJani Nikula 
361002107ef1SVille Syrjälä int intel_bios_hdmi_boost_level(const struct intel_bios_encoder_data *devdata)
361101a60883SJani Nikula {
3612a434689cSJani Nikula 	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3613c0a950d1SJani Nikula 		return 0;
361401a60883SJani Nikula 
3615*ff9bc20cSVille Syrjälä 	return translate_iboost(devdata->i915, devdata->child.hdmi_iboost_level);
361601a60883SJani Nikula }
3617f83acdabSJani Nikula 
361802107ef1SVille Syrjälä int intel_bios_hdmi_ddc_pin(const struct intel_bios_encoder_data *devdata)
3619f83acdabSJani Nikula {
3620dab8477bSJani Nikula 	if (!devdata || !devdata->child.ddc_pin)
3621dab8477bSJani Nikula 		return 0;
3622dab8477bSJani Nikula 
362302107ef1SVille Syrjälä 	return map_ddc_pin(devdata->i915, devdata->child.ddc_pin);
362417004bfbSJani Nikula }
3625c5faae5aSJani Nikula 
3626f08fbe6aSJani Nikula bool intel_bios_encoder_supports_typec_usb(const struct intel_bios_encoder_data *devdata)
3627c5faae5aSJani Nikula {
3628a434689cSJani Nikula 	return devdata->i915->display.vbt.version >= 195 && devdata->child.dp_usb_type_c;
3629c5faae5aSJani Nikula }
3630c5faae5aSJani Nikula 
3631f08fbe6aSJani Nikula bool intel_bios_encoder_supports_tbt(const struct intel_bios_encoder_data *devdata)
3632c5faae5aSJani Nikula {
3633a434689cSJani Nikula 	return devdata->i915->display.vbt.version >= 209 && devdata->child.tbt;
3634c5faae5aSJani Nikula }
363545c0673aSJani Nikula 
36365f42196dSVille Syrjälä bool intel_bios_encoder_lane_reversal(const struct intel_bios_encoder_data *devdata)
36375f42196dSVille Syrjälä {
36385f42196dSVille Syrjälä 	return devdata && devdata->child.lane_reversal;
36395f42196dSVille Syrjälä }
36405f42196dSVille Syrjälä 
36419151c85cSVille Syrjälä bool intel_bios_encoder_hpd_invert(const struct intel_bios_encoder_data *devdata)
36429151c85cSVille Syrjälä {
36439151c85cSVille Syrjälä 	return devdata && devdata->child.hpd_invert;
36449151c85cSVille Syrjälä }
36459151c85cSVille Syrjälä 
364645c0673aSJani Nikula const struct intel_bios_encoder_data *
364745c0673aSJani Nikula intel_bios_encoder_data_lookup(struct drm_i915_private *i915, enum port port)
364845c0673aSJani Nikula {
3649021a62a5SVille Syrjälä 	struct intel_bios_encoder_data *devdata;
3650021a62a5SVille Syrjälä 
3651021a62a5SVille Syrjälä 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3652021a62a5SVille Syrjälä 		if (intel_bios_encoder_port(devdata) == port)
3653021a62a5SVille Syrjälä 			return devdata;
3654021a62a5SVille Syrjälä 	}
3655021a62a5SVille Syrjälä 
3656021a62a5SVille Syrjälä 	return NULL;
3657021a62a5SVille Syrjälä }
3658021a62a5SVille Syrjälä 
3659021a62a5SVille Syrjälä void intel_bios_for_each_encoder(struct drm_i915_private *i915,
3660021a62a5SVille Syrjälä 				 void (*func)(struct drm_i915_private *i915,
3661021a62a5SVille Syrjälä 					      const struct intel_bios_encoder_data *devdata))
3662021a62a5SVille Syrjälä {
3663021a62a5SVille Syrjälä 	struct intel_bios_encoder_data *devdata;
3664021a62a5SVille Syrjälä 
3665021a62a5SVille Syrjälä 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
3666021a62a5SVille Syrjälä 		func(i915, devdata);
366745c0673aSJani Nikula }
366830ef2627SJani Nikula 
366930ef2627SJani Nikula static int intel_bios_vbt_show(struct seq_file *m, void *unused)
367030ef2627SJani Nikula {
367130ef2627SJani Nikula 	struct drm_i915_private *i915 = m->private;
367237e21003SJani Nikula 	const void *vbt;
367337e21003SJani Nikula 	size_t vbt_size;
367430ef2627SJani Nikula 
367530ef2627SJani Nikula 	/*
367630ef2627SJani Nikula 	 * FIXME: VBT might originate from other places than opregion, and then
367730ef2627SJani Nikula 	 * this would be incorrect.
367830ef2627SJani Nikula 	 */
367937e21003SJani Nikula 	vbt = intel_opregion_get_vbt(i915, &vbt_size);
368037e21003SJani Nikula 	if (vbt)
368137e21003SJani Nikula 		seq_write(m, vbt, vbt_size);
368230ef2627SJani Nikula 
368330ef2627SJani Nikula 	return 0;
368430ef2627SJani Nikula }
368530ef2627SJani Nikula 
368630ef2627SJani Nikula DEFINE_SHOW_ATTRIBUTE(intel_bios_vbt);
368730ef2627SJani Nikula 
368830ef2627SJani Nikula void intel_bios_debugfs_register(struct drm_i915_private *i915)
368930ef2627SJani Nikula {
369030ef2627SJani Nikula 	struct drm_minor *minor = i915->drm.primary;
369130ef2627SJani Nikula 
369230ef2627SJani Nikula 	debugfs_create_file("i915_vbt", 0444, minor->debugfs_root,
369330ef2627SJani Nikula 			    i915, &intel_bios_vbt_fops);
369430ef2627SJani Nikula }
3695