1 /*
2  * Copyright © 2006 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 #include <drm/display/drm_dp_helper.h>
29 #include <drm/display/drm_dsc_helper.h>
30 #include <drm/drm_edid.h>
31 
32 #include "i915_drv.h"
33 #include "i915_reg.h"
34 #include "intel_display.h"
35 #include "intel_display_types.h"
36 #include "intel_gmbus.h"
37 
38 #define _INTEL_BIOS_PRIVATE
39 #include "intel_vbt_defs.h"
40 
41 /**
42  * DOC: Video BIOS Table (VBT)
43  *
44  * The Video BIOS Table, or VBT, provides platform and board specific
45  * configuration information to the driver that is not discoverable or available
46  * through other means. The configuration is mostly related to display
47  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
48  * the PCI ROM.
49  *
50  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
51  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
52  * contain the actual configuration information. The VBT Header, and thus the
53  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
54  * BDB Header. The data blocks are concatenated after the BDB Header. The data
55  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
56  * data. (Block 53, the MIPI Sequence Block is an exception.)
57  *
58  * The driver parses the VBT during load. The relevant information is stored in
59  * driver private data for ease of use, and the actual VBT is not read after
60  * that.
61  */
62 
63 /* Wrapper for VBT child device config */
64 struct intel_bios_encoder_data {
65 	struct drm_i915_private *i915;
66 
67 	struct child_device_config child;
68 	struct dsc_compression_parameters_entry *dsc;
69 	struct list_head node;
70 };
71 
72 #define	SLAVE_ADDR1	0x70
73 #define	SLAVE_ADDR2	0x72
74 
75 /* Get BDB block size given a pointer to Block ID. */
76 static u32 _get_blocksize(const u8 *block_base)
77 {
78 	/* The MIPI Sequence Block v3+ has a separate size field. */
79 	if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
80 		return *((const u32 *)(block_base + 4));
81 	else
82 		return *((const u16 *)(block_base + 1));
83 }
84 
85 /* Get BDB block size give a pointer to data after Block ID and Block Size. */
86 static u32 get_blocksize(const void *block_data)
87 {
88 	return _get_blocksize(block_data - 3);
89 }
90 
91 static const void *
92 find_raw_section(const void *_bdb, enum bdb_block_id section_id)
93 {
94 	const struct bdb_header *bdb = _bdb;
95 	const u8 *base = _bdb;
96 	int index = 0;
97 	u32 total, current_size;
98 	enum bdb_block_id current_id;
99 
100 	/* skip to first section */
101 	index += bdb->header_size;
102 	total = bdb->bdb_size;
103 
104 	/* walk the sections looking for section_id */
105 	while (index + 3 < total) {
106 		current_id = *(base + index);
107 		current_size = _get_blocksize(base + index);
108 		index += 3;
109 
110 		if (index + current_size > total)
111 			return NULL;
112 
113 		if (current_id == section_id)
114 			return base + index;
115 
116 		index += current_size;
117 	}
118 
119 	return NULL;
120 }
121 
122 /*
123  * Offset from the start of BDB to the start of the
124  * block data (just past the block header).
125  */
126 static u32 raw_block_offset(const void *bdb, enum bdb_block_id section_id)
127 {
128 	const void *block;
129 
130 	block = find_raw_section(bdb, section_id);
131 	if (!block)
132 		return 0;
133 
134 	return block - bdb;
135 }
136 
137 struct bdb_block_entry {
138 	struct list_head node;
139 	enum bdb_block_id section_id;
140 	u8 data[];
141 };
142 
143 static const void *
144 find_section(struct drm_i915_private *i915,
145 	     enum bdb_block_id section_id)
146 {
147 	struct bdb_block_entry *entry;
148 
149 	list_for_each_entry(entry, &i915->display.vbt.bdb_blocks, node) {
150 		if (entry->section_id == section_id)
151 			return entry->data + 3;
152 	}
153 
154 	return NULL;
155 }
156 
157 static const struct {
158 	enum bdb_block_id section_id;
159 	size_t min_size;
160 } bdb_blocks[] = {
161 	{ .section_id = BDB_GENERAL_FEATURES,
162 	  .min_size = sizeof(struct bdb_general_features), },
163 	{ .section_id = BDB_GENERAL_DEFINITIONS,
164 	  .min_size = sizeof(struct bdb_general_definitions), },
165 	{ .section_id = BDB_PSR,
166 	  .min_size = sizeof(struct bdb_psr), },
167 	{ .section_id = BDB_DRIVER_FEATURES,
168 	  .min_size = sizeof(struct bdb_driver_features), },
169 	{ .section_id = BDB_SDVO_LVDS_OPTIONS,
170 	  .min_size = sizeof(struct bdb_sdvo_lvds_options), },
171 	{ .section_id = BDB_SDVO_PANEL_DTDS,
172 	  .min_size = sizeof(struct bdb_sdvo_panel_dtds), },
173 	{ .section_id = BDB_EDP,
174 	  .min_size = sizeof(struct bdb_edp), },
175 	{ .section_id = BDB_LVDS_OPTIONS,
176 	  .min_size = sizeof(struct bdb_lvds_options), },
177 	/*
178 	 * BDB_LVDS_LFP_DATA depends on BDB_LVDS_LFP_DATA_PTRS,
179 	 * so keep the two ordered.
180 	 */
181 	{ .section_id = BDB_LVDS_LFP_DATA_PTRS,
182 	  .min_size = sizeof(struct bdb_lvds_lfp_data_ptrs), },
183 	{ .section_id = BDB_LVDS_LFP_DATA,
184 	  .min_size = 0, /* special case */ },
185 	{ .section_id = BDB_LVDS_BACKLIGHT,
186 	  .min_size = sizeof(struct bdb_lfp_backlight_data), },
187 	{ .section_id = BDB_LFP_POWER,
188 	  .min_size = sizeof(struct bdb_lfp_power), },
189 	{ .section_id = BDB_MIPI_CONFIG,
190 	  .min_size = sizeof(struct bdb_mipi_config), },
191 	{ .section_id = BDB_MIPI_SEQUENCE,
192 	  .min_size = sizeof(struct bdb_mipi_sequence) },
193 	{ .section_id = BDB_COMPRESSION_PARAMETERS,
194 	  .min_size = sizeof(struct bdb_compression_parameters), },
195 	{ .section_id = BDB_GENERIC_DTD,
196 	  .min_size = sizeof(struct bdb_generic_dtd), },
197 };
198 
199 static size_t lfp_data_min_size(struct drm_i915_private *i915)
200 {
201 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
202 	size_t size;
203 
204 	ptrs = find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
205 	if (!ptrs)
206 		return 0;
207 
208 	size = sizeof(struct bdb_lvds_lfp_data);
209 	if (ptrs->panel_name.table_size)
210 		size = max(size, ptrs->panel_name.offset +
211 			   sizeof(struct bdb_lvds_lfp_data_tail));
212 
213 	return size;
214 }
215 
216 static bool validate_lfp_data_ptrs(const void *bdb,
217 				   const struct bdb_lvds_lfp_data_ptrs *ptrs)
218 {
219 	int fp_timing_size, dvo_timing_size, panel_pnp_id_size, panel_name_size;
220 	int data_block_size, lfp_data_size;
221 	const void *data_block;
222 	int i;
223 
224 	data_block = find_raw_section(bdb, BDB_LVDS_LFP_DATA);
225 	if (!data_block)
226 		return false;
227 
228 	data_block_size = get_blocksize(data_block);
229 	if (data_block_size == 0)
230 		return false;
231 
232 	/* always 3 indicating the presence of fp_timing+dvo_timing+panel_pnp_id */
233 	if (ptrs->lvds_entries != 3)
234 		return false;
235 
236 	fp_timing_size = ptrs->ptr[0].fp_timing.table_size;
237 	dvo_timing_size = ptrs->ptr[0].dvo_timing.table_size;
238 	panel_pnp_id_size = ptrs->ptr[0].panel_pnp_id.table_size;
239 	panel_name_size = ptrs->panel_name.table_size;
240 
241 	/* fp_timing has variable size */
242 	if (fp_timing_size < 32 ||
243 	    dvo_timing_size != sizeof(struct lvds_dvo_timing) ||
244 	    panel_pnp_id_size != sizeof(struct lvds_pnp_id))
245 		return false;
246 
247 	/* panel_name is not present in old VBTs */
248 	if (panel_name_size != 0 &&
249 	    panel_name_size != sizeof(struct lvds_lfp_panel_name))
250 		return false;
251 
252 	lfp_data_size = ptrs->ptr[1].fp_timing.offset - ptrs->ptr[0].fp_timing.offset;
253 	if (16 * lfp_data_size > data_block_size)
254 		return false;
255 
256 	/* make sure the table entries have uniform size */
257 	for (i = 1; i < 16; i++) {
258 		if (ptrs->ptr[i].fp_timing.table_size != fp_timing_size ||
259 		    ptrs->ptr[i].dvo_timing.table_size != dvo_timing_size ||
260 		    ptrs->ptr[i].panel_pnp_id.table_size != panel_pnp_id_size)
261 			return false;
262 
263 		if (ptrs->ptr[i].fp_timing.offset - ptrs->ptr[i-1].fp_timing.offset != lfp_data_size ||
264 		    ptrs->ptr[i].dvo_timing.offset - ptrs->ptr[i-1].dvo_timing.offset != lfp_data_size ||
265 		    ptrs->ptr[i].panel_pnp_id.offset - ptrs->ptr[i-1].panel_pnp_id.offset != lfp_data_size)
266 			return false;
267 	}
268 
269 	/*
270 	 * Except for vlv/chv machines all real VBTs seem to have 6
271 	 * unaccounted bytes in the fp_timing table. And it doesn't
272 	 * appear to be a really intentional hole as the fp_timing
273 	 * 0xffff terminator is always within those 6 missing bytes.
274 	 */
275 	if (fp_timing_size + 6 + dvo_timing_size + panel_pnp_id_size == lfp_data_size)
276 		fp_timing_size += 6;
277 
278 	if (fp_timing_size + dvo_timing_size + panel_pnp_id_size != lfp_data_size)
279 		return false;
280 
281 	if (ptrs->ptr[0].fp_timing.offset + fp_timing_size != ptrs->ptr[0].dvo_timing.offset ||
282 	    ptrs->ptr[0].dvo_timing.offset + dvo_timing_size != ptrs->ptr[0].panel_pnp_id.offset ||
283 	    ptrs->ptr[0].panel_pnp_id.offset + panel_pnp_id_size != lfp_data_size)
284 		return false;
285 
286 	/* make sure the tables fit inside the data block */
287 	for (i = 0; i < 16; i++) {
288 		if (ptrs->ptr[i].fp_timing.offset + fp_timing_size > data_block_size ||
289 		    ptrs->ptr[i].dvo_timing.offset + dvo_timing_size > data_block_size ||
290 		    ptrs->ptr[i].panel_pnp_id.offset + panel_pnp_id_size > data_block_size)
291 			return false;
292 	}
293 
294 	if (ptrs->panel_name.offset + 16 * panel_name_size > data_block_size)
295 		return false;
296 
297 	/* make sure fp_timing terminators are present at expected locations */
298 	for (i = 0; i < 16; i++) {
299 		const u16 *t = data_block + ptrs->ptr[i].fp_timing.offset +
300 			fp_timing_size - 2;
301 
302 		if (*t != 0xffff)
303 			return false;
304 	}
305 
306 	return true;
307 }
308 
309 /* make the data table offsets relative to the data block */
310 static bool fixup_lfp_data_ptrs(const void *bdb, void *ptrs_block)
311 {
312 	struct bdb_lvds_lfp_data_ptrs *ptrs = ptrs_block;
313 	u32 offset;
314 	int i;
315 
316 	offset = raw_block_offset(bdb, BDB_LVDS_LFP_DATA);
317 
318 	for (i = 0; i < 16; i++) {
319 		if (ptrs->ptr[i].fp_timing.offset < offset ||
320 		    ptrs->ptr[i].dvo_timing.offset < offset ||
321 		    ptrs->ptr[i].panel_pnp_id.offset < offset)
322 			return false;
323 
324 		ptrs->ptr[i].fp_timing.offset -= offset;
325 		ptrs->ptr[i].dvo_timing.offset -= offset;
326 		ptrs->ptr[i].panel_pnp_id.offset -= offset;
327 	}
328 
329 	if (ptrs->panel_name.table_size) {
330 		if (ptrs->panel_name.offset < offset)
331 			return false;
332 
333 		ptrs->panel_name.offset -= offset;
334 	}
335 
336 	return validate_lfp_data_ptrs(bdb, ptrs);
337 }
338 
339 static int make_lfp_data_ptr(struct lvds_lfp_data_ptr_table *table,
340 			     int table_size, int total_size)
341 {
342 	if (total_size < table_size)
343 		return total_size;
344 
345 	table->table_size = table_size;
346 	table->offset = total_size - table_size;
347 
348 	return total_size - table_size;
349 }
350 
351 static void next_lfp_data_ptr(struct lvds_lfp_data_ptr_table *next,
352 			      const struct lvds_lfp_data_ptr_table *prev,
353 			      int size)
354 {
355 	next->table_size = prev->table_size;
356 	next->offset = prev->offset + size;
357 }
358 
359 static void *generate_lfp_data_ptrs(struct drm_i915_private *i915,
360 				    const void *bdb)
361 {
362 	int i, size, table_size, block_size, offset, fp_timing_size;
363 	struct bdb_lvds_lfp_data_ptrs *ptrs;
364 	const void *block;
365 	void *ptrs_block;
366 
367 	/*
368 	 * The hardcoded fp_timing_size is only valid for
369 	 * modernish VBTs. All older VBTs definitely should
370 	 * include block 41 and thus we don't need to
371 	 * generate one.
372 	 */
373 	if (i915->display.vbt.version < 155)
374 		return NULL;
375 
376 	fp_timing_size = 38;
377 
378 	block = find_raw_section(bdb, BDB_LVDS_LFP_DATA);
379 	if (!block)
380 		return NULL;
381 
382 	drm_dbg_kms(&i915->drm, "Generating LFP data table pointers\n");
383 
384 	block_size = get_blocksize(block);
385 
386 	size = fp_timing_size + sizeof(struct lvds_dvo_timing) +
387 		sizeof(struct lvds_pnp_id);
388 	if (size * 16 > block_size)
389 		return NULL;
390 
391 	ptrs_block = kzalloc(sizeof(*ptrs) + 3, GFP_KERNEL);
392 	if (!ptrs_block)
393 		return NULL;
394 
395 	*(u8 *)(ptrs_block + 0) = BDB_LVDS_LFP_DATA_PTRS;
396 	*(u16 *)(ptrs_block + 1) = sizeof(*ptrs);
397 	ptrs = ptrs_block + 3;
398 
399 	table_size = sizeof(struct lvds_pnp_id);
400 	size = make_lfp_data_ptr(&ptrs->ptr[0].panel_pnp_id, table_size, size);
401 
402 	table_size = sizeof(struct lvds_dvo_timing);
403 	size = make_lfp_data_ptr(&ptrs->ptr[0].dvo_timing, table_size, size);
404 
405 	table_size = fp_timing_size;
406 	size = make_lfp_data_ptr(&ptrs->ptr[0].fp_timing, table_size, size);
407 
408 	if (ptrs->ptr[0].fp_timing.table_size)
409 		ptrs->lvds_entries++;
410 	if (ptrs->ptr[0].dvo_timing.table_size)
411 		ptrs->lvds_entries++;
412 	if (ptrs->ptr[0].panel_pnp_id.table_size)
413 		ptrs->lvds_entries++;
414 
415 	if (size != 0 || ptrs->lvds_entries != 3) {
416 		kfree(ptrs_block);
417 		return NULL;
418 	}
419 
420 	size = fp_timing_size + sizeof(struct lvds_dvo_timing) +
421 		sizeof(struct lvds_pnp_id);
422 	for (i = 1; i < 16; i++) {
423 		next_lfp_data_ptr(&ptrs->ptr[i].fp_timing, &ptrs->ptr[i-1].fp_timing, size);
424 		next_lfp_data_ptr(&ptrs->ptr[i].dvo_timing, &ptrs->ptr[i-1].dvo_timing, size);
425 		next_lfp_data_ptr(&ptrs->ptr[i].panel_pnp_id, &ptrs->ptr[i-1].panel_pnp_id, size);
426 	}
427 
428 	table_size = sizeof(struct lvds_lfp_panel_name);
429 
430 	if (16 * (size + table_size) <= block_size) {
431 		ptrs->panel_name.table_size = table_size;
432 		ptrs->panel_name.offset = size * 16;
433 	}
434 
435 	offset = block - bdb;
436 
437 	for (i = 0; i < 16; i++) {
438 		ptrs->ptr[i].fp_timing.offset += offset;
439 		ptrs->ptr[i].dvo_timing.offset += offset;
440 		ptrs->ptr[i].panel_pnp_id.offset += offset;
441 	}
442 
443 	if (ptrs->panel_name.table_size)
444 		ptrs->panel_name.offset += offset;
445 
446 	return ptrs_block;
447 }
448 
449 static void
450 init_bdb_block(struct drm_i915_private *i915,
451 	       const void *bdb, enum bdb_block_id section_id,
452 	       size_t min_size)
453 {
454 	struct bdb_block_entry *entry;
455 	void *temp_block = NULL;
456 	const void *block;
457 	size_t block_size;
458 
459 	block = find_raw_section(bdb, section_id);
460 
461 	/* Modern VBTs lack the LFP data table pointers block, make one up */
462 	if (!block && section_id == BDB_LVDS_LFP_DATA_PTRS) {
463 		temp_block = generate_lfp_data_ptrs(i915, bdb);
464 		if (temp_block)
465 			block = temp_block + 3;
466 	}
467 	if (!block)
468 		return;
469 
470 	drm_WARN(&i915->drm, min_size == 0,
471 		 "Block %d min_size is zero\n", section_id);
472 
473 	block_size = get_blocksize(block);
474 
475 	/*
476 	 * Version number and new block size are considered
477 	 * part of the header for MIPI sequenece block v3+.
478 	 */
479 	if (section_id == BDB_MIPI_SEQUENCE && *(const u8 *)block >= 3)
480 		block_size += 5;
481 
482 	entry = kzalloc(struct_size(entry, data, max(min_size, block_size) + 3),
483 			GFP_KERNEL);
484 	if (!entry) {
485 		kfree(temp_block);
486 		return;
487 	}
488 
489 	entry->section_id = section_id;
490 	memcpy(entry->data, block - 3, block_size + 3);
491 
492 	kfree(temp_block);
493 
494 	drm_dbg_kms(&i915->drm, "Found BDB block %d (size %zu, min size %zu)\n",
495 		    section_id, block_size, min_size);
496 
497 	if (section_id == BDB_LVDS_LFP_DATA_PTRS &&
498 	    !fixup_lfp_data_ptrs(bdb, entry->data + 3)) {
499 		drm_err(&i915->drm, "VBT has malformed LFP data table pointers\n");
500 		kfree(entry);
501 		return;
502 	}
503 
504 	list_add_tail(&entry->node, &i915->display.vbt.bdb_blocks);
505 }
506 
507 static void init_bdb_blocks(struct drm_i915_private *i915,
508 			    const void *bdb)
509 {
510 	int i;
511 
512 	for (i = 0; i < ARRAY_SIZE(bdb_blocks); i++) {
513 		enum bdb_block_id section_id = bdb_blocks[i].section_id;
514 		size_t min_size = bdb_blocks[i].min_size;
515 
516 		if (section_id == BDB_LVDS_LFP_DATA)
517 			min_size = lfp_data_min_size(i915);
518 
519 		init_bdb_block(i915, bdb, section_id, min_size);
520 	}
521 }
522 
523 static void
524 fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
525 			const struct lvds_dvo_timing *dvo_timing)
526 {
527 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
528 		dvo_timing->hactive_lo;
529 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
530 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
531 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
532 		((dvo_timing->hsync_pulse_width_hi << 8) |
533 			dvo_timing->hsync_pulse_width_lo);
534 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
535 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
536 
537 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
538 		dvo_timing->vactive_lo;
539 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
540 		((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
541 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
542 		((dvo_timing->vsync_pulse_width_hi << 4) |
543 			dvo_timing->vsync_pulse_width_lo);
544 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
545 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
546 	panel_fixed_mode->clock = dvo_timing->clock * 10;
547 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
548 
549 	if (dvo_timing->hsync_positive)
550 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
551 	else
552 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
553 
554 	if (dvo_timing->vsync_positive)
555 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
556 	else
557 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
558 
559 	panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
560 		dvo_timing->himage_lo;
561 	panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
562 		dvo_timing->vimage_lo;
563 
564 	/* Some VBTs have bogus h/vtotal values */
565 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
566 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
567 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
568 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
569 
570 	drm_mode_set_name(panel_fixed_mode);
571 }
572 
573 static const struct lvds_dvo_timing *
574 get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *data,
575 		    const struct bdb_lvds_lfp_data_ptrs *ptrs,
576 		    int index)
577 {
578 	return (const void *)data + ptrs->ptr[index].dvo_timing.offset;
579 }
580 
581 static const struct lvds_fp_timing *
582 get_lvds_fp_timing(const struct bdb_lvds_lfp_data *data,
583 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
584 		   int index)
585 {
586 	return (const void *)data + ptrs->ptr[index].fp_timing.offset;
587 }
588 
589 static const struct lvds_pnp_id *
590 get_lvds_pnp_id(const struct bdb_lvds_lfp_data *data,
591 		const struct bdb_lvds_lfp_data_ptrs *ptrs,
592 		int index)
593 {
594 	return (const void *)data + ptrs->ptr[index].panel_pnp_id.offset;
595 }
596 
597 static const struct bdb_lvds_lfp_data_tail *
598 get_lfp_data_tail(const struct bdb_lvds_lfp_data *data,
599 		  const struct bdb_lvds_lfp_data_ptrs *ptrs)
600 {
601 	if (ptrs->panel_name.table_size)
602 		return (const void *)data + ptrs->panel_name.offset;
603 	else
604 		return NULL;
605 }
606 
607 static void dump_pnp_id(struct drm_i915_private *i915,
608 			const struct lvds_pnp_id *pnp_id,
609 			const char *name)
610 {
611 	u16 mfg_name = be16_to_cpu((__force __be16)pnp_id->mfg_name);
612 	char vend[4];
613 
614 	drm_dbg_kms(&i915->drm, "%s PNPID mfg: %s (0x%x), prod: %u, serial: %u, week: %d, year: %d\n",
615 		    name, drm_edid_decode_mfg_id(mfg_name, vend),
616 		    pnp_id->mfg_name, pnp_id->product_code, pnp_id->serial,
617 		    pnp_id->mfg_week, pnp_id->mfg_year + 1990);
618 }
619 
620 static int opregion_get_panel_type(struct drm_i915_private *i915,
621 				   const struct intel_bios_encoder_data *devdata,
622 				   const struct drm_edid *drm_edid, bool use_fallback)
623 {
624 	return intel_opregion_get_panel_type(i915);
625 }
626 
627 static int vbt_get_panel_type(struct drm_i915_private *i915,
628 			      const struct intel_bios_encoder_data *devdata,
629 			      const struct drm_edid *drm_edid, bool use_fallback)
630 {
631 	const struct bdb_lvds_options *lvds_options;
632 
633 	lvds_options = find_section(i915, BDB_LVDS_OPTIONS);
634 	if (!lvds_options)
635 		return -1;
636 
637 	if (lvds_options->panel_type > 0xf &&
638 	    lvds_options->panel_type != 0xff) {
639 		drm_dbg_kms(&i915->drm, "Invalid VBT panel type 0x%x\n",
640 			    lvds_options->panel_type);
641 		return -1;
642 	}
643 
644 	if (devdata && devdata->child.handle == DEVICE_HANDLE_LFP2)
645 		return lvds_options->panel_type2;
646 
647 	drm_WARN_ON(&i915->drm, devdata && devdata->child.handle != DEVICE_HANDLE_LFP1);
648 
649 	return lvds_options->panel_type;
650 }
651 
652 static int pnpid_get_panel_type(struct drm_i915_private *i915,
653 				const struct intel_bios_encoder_data *devdata,
654 				const struct drm_edid *drm_edid, bool use_fallback)
655 {
656 	const struct bdb_lvds_lfp_data *data;
657 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
658 	const struct lvds_pnp_id *edid_id;
659 	struct lvds_pnp_id edid_id_nodate;
660 	const struct edid *edid = drm_edid_raw(drm_edid); /* FIXME */
661 	int i, best = -1;
662 
663 	if (!edid)
664 		return -1;
665 
666 	edid_id = (const void *)&edid->mfg_id[0];
667 
668 	edid_id_nodate = *edid_id;
669 	edid_id_nodate.mfg_week = 0;
670 	edid_id_nodate.mfg_year = 0;
671 
672 	dump_pnp_id(i915, edid_id, "EDID");
673 
674 	ptrs = find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
675 	if (!ptrs)
676 		return -1;
677 
678 	data = find_section(i915, BDB_LVDS_LFP_DATA);
679 	if (!data)
680 		return -1;
681 
682 	for (i = 0; i < 16; i++) {
683 		const struct lvds_pnp_id *vbt_id =
684 			get_lvds_pnp_id(data, ptrs, i);
685 
686 		/* full match? */
687 		if (!memcmp(vbt_id, edid_id, sizeof(*vbt_id)))
688 			return i;
689 
690 		/*
691 		 * Accept a match w/o date if no full match is found,
692 		 * and the VBT entry does not specify a date.
693 		 */
694 		if (best < 0 &&
695 		    !memcmp(vbt_id, &edid_id_nodate, sizeof(*vbt_id)))
696 			best = i;
697 	}
698 
699 	return best;
700 }
701 
702 static int fallback_get_panel_type(struct drm_i915_private *i915,
703 				   const struct intel_bios_encoder_data *devdata,
704 				   const struct drm_edid *drm_edid, bool use_fallback)
705 {
706 	return use_fallback ? 0 : -1;
707 }
708 
709 enum panel_type {
710 	PANEL_TYPE_OPREGION,
711 	PANEL_TYPE_VBT,
712 	PANEL_TYPE_PNPID,
713 	PANEL_TYPE_FALLBACK,
714 };
715 
716 static int get_panel_type(struct drm_i915_private *i915,
717 			  const struct intel_bios_encoder_data *devdata,
718 			  const struct drm_edid *drm_edid, bool use_fallback)
719 {
720 	struct {
721 		const char *name;
722 		int (*get_panel_type)(struct drm_i915_private *i915,
723 				      const struct intel_bios_encoder_data *devdata,
724 				      const struct drm_edid *drm_edid, bool use_fallback);
725 		int panel_type;
726 	} panel_types[] = {
727 		[PANEL_TYPE_OPREGION] = {
728 			.name = "OpRegion",
729 			.get_panel_type = opregion_get_panel_type,
730 		},
731 		[PANEL_TYPE_VBT] = {
732 			.name = "VBT",
733 			.get_panel_type = vbt_get_panel_type,
734 		},
735 		[PANEL_TYPE_PNPID] = {
736 			.name = "PNPID",
737 			.get_panel_type = pnpid_get_panel_type,
738 		},
739 		[PANEL_TYPE_FALLBACK] = {
740 			.name = "fallback",
741 			.get_panel_type = fallback_get_panel_type,
742 		},
743 	};
744 	int i;
745 
746 	for (i = 0; i < ARRAY_SIZE(panel_types); i++) {
747 		panel_types[i].panel_type = panel_types[i].get_panel_type(i915, devdata,
748 									  drm_edid, use_fallback);
749 
750 		drm_WARN_ON(&i915->drm, panel_types[i].panel_type > 0xf &&
751 			    panel_types[i].panel_type != 0xff);
752 
753 		if (panel_types[i].panel_type >= 0)
754 			drm_dbg_kms(&i915->drm, "Panel type (%s): %d\n",
755 				    panel_types[i].name, panel_types[i].panel_type);
756 	}
757 
758 	if (panel_types[PANEL_TYPE_OPREGION].panel_type >= 0)
759 		i = PANEL_TYPE_OPREGION;
760 	else if (panel_types[PANEL_TYPE_VBT].panel_type == 0xff &&
761 		 panel_types[PANEL_TYPE_PNPID].panel_type >= 0)
762 		i = PANEL_TYPE_PNPID;
763 	else if (panel_types[PANEL_TYPE_VBT].panel_type != 0xff &&
764 		 panel_types[PANEL_TYPE_VBT].panel_type >= 0)
765 		i = PANEL_TYPE_VBT;
766 	else
767 		i = PANEL_TYPE_FALLBACK;
768 
769 	drm_dbg_kms(&i915->drm, "Selected panel type (%s): %d\n",
770 		    panel_types[i].name, panel_types[i].panel_type);
771 
772 	return panel_types[i].panel_type;
773 }
774 
775 static unsigned int panel_bits(unsigned int value, int panel_type, int num_bits)
776 {
777 	return (value >> (panel_type * num_bits)) & (BIT(num_bits) - 1);
778 }
779 
780 static bool panel_bool(unsigned int value, int panel_type)
781 {
782 	return panel_bits(value, panel_type, 1);
783 }
784 
785 /* Parse general panel options */
786 static void
787 parse_panel_options(struct drm_i915_private *i915,
788 		    struct intel_panel *panel)
789 {
790 	const struct bdb_lvds_options *lvds_options;
791 	int panel_type = panel->vbt.panel_type;
792 	int drrs_mode;
793 
794 	lvds_options = find_section(i915, BDB_LVDS_OPTIONS);
795 	if (!lvds_options)
796 		return;
797 
798 	panel->vbt.lvds_dither = lvds_options->pixel_dither;
799 
800 	/*
801 	 * Empirical evidence indicates the block size can be
802 	 * either 4,14,16,24+ bytes. For older VBTs no clear
803 	 * relationship between the block size vs. BDB version.
804 	 */
805 	if (get_blocksize(lvds_options) < 16)
806 		return;
807 
808 	drrs_mode = panel_bits(lvds_options->dps_panel_type_bits,
809 			       panel_type, 2);
810 	/*
811 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
812 	 * The below piece of code is required to adjust vbt.drrs_type
813 	 * to match the enum drrs_support_type.
814 	 */
815 	switch (drrs_mode) {
816 	case 0:
817 		panel->vbt.drrs_type = DRRS_TYPE_STATIC;
818 		drm_dbg_kms(&i915->drm, "DRRS supported mode is static\n");
819 		break;
820 	case 2:
821 		panel->vbt.drrs_type = DRRS_TYPE_SEAMLESS;
822 		drm_dbg_kms(&i915->drm,
823 			    "DRRS supported mode is seamless\n");
824 		break;
825 	default:
826 		panel->vbt.drrs_type = DRRS_TYPE_NONE;
827 		drm_dbg_kms(&i915->drm,
828 			    "DRRS not supported (VBT input)\n");
829 		break;
830 	}
831 }
832 
833 static void
834 parse_lfp_panel_dtd(struct drm_i915_private *i915,
835 		    struct intel_panel *panel,
836 		    const struct bdb_lvds_lfp_data *lvds_lfp_data,
837 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs)
838 {
839 	const struct lvds_dvo_timing *panel_dvo_timing;
840 	const struct lvds_fp_timing *fp_timing;
841 	struct drm_display_mode *panel_fixed_mode;
842 	int panel_type = panel->vbt.panel_type;
843 
844 	panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
845 					       lvds_lfp_data_ptrs,
846 					       panel_type);
847 
848 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
849 	if (!panel_fixed_mode)
850 		return;
851 
852 	fill_detail_timing_data(panel_fixed_mode, panel_dvo_timing);
853 
854 	panel->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
855 
856 	drm_dbg_kms(&i915->drm,
857 		    "Found panel mode in BIOS VBT legacy lfp table: " DRM_MODE_FMT "\n",
858 		    DRM_MODE_ARG(panel_fixed_mode));
859 
860 	fp_timing = get_lvds_fp_timing(lvds_lfp_data,
861 				       lvds_lfp_data_ptrs,
862 				       panel_type);
863 
864 	/* check the resolution, just to be sure */
865 	if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
866 	    fp_timing->y_res == panel_fixed_mode->vdisplay) {
867 		panel->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
868 		drm_dbg_kms(&i915->drm,
869 			    "VBT initial LVDS value %x\n",
870 			    panel->vbt.bios_lvds_val);
871 	}
872 }
873 
874 static void
875 parse_lfp_data(struct drm_i915_private *i915,
876 	       struct intel_panel *panel)
877 {
878 	const struct bdb_lvds_lfp_data *data;
879 	const struct bdb_lvds_lfp_data_tail *tail;
880 	const struct bdb_lvds_lfp_data_ptrs *ptrs;
881 	const struct lvds_pnp_id *pnp_id;
882 	int panel_type = panel->vbt.panel_type;
883 
884 	ptrs = find_section(i915, BDB_LVDS_LFP_DATA_PTRS);
885 	if (!ptrs)
886 		return;
887 
888 	data = find_section(i915, BDB_LVDS_LFP_DATA);
889 	if (!data)
890 		return;
891 
892 	if (!panel->vbt.lfp_lvds_vbt_mode)
893 		parse_lfp_panel_dtd(i915, panel, data, ptrs);
894 
895 	pnp_id = get_lvds_pnp_id(data, ptrs, panel_type);
896 	dump_pnp_id(i915, pnp_id, "Panel");
897 
898 	tail = get_lfp_data_tail(data, ptrs);
899 	if (!tail)
900 		return;
901 
902 	drm_dbg_kms(&i915->drm, "Panel name: %.*s\n",
903 		    (int)sizeof(tail->panel_name[0].name),
904 		    tail->panel_name[panel_type].name);
905 
906 	if (i915->display.vbt.version >= 188) {
907 		panel->vbt.seamless_drrs_min_refresh_rate =
908 			tail->seamless_drrs_min_refresh_rate[panel_type];
909 		drm_dbg_kms(&i915->drm,
910 			    "Seamless DRRS min refresh rate: %d Hz\n",
911 			    panel->vbt.seamless_drrs_min_refresh_rate);
912 	}
913 }
914 
915 static void
916 parse_generic_dtd(struct drm_i915_private *i915,
917 		  struct intel_panel *panel)
918 {
919 	const struct bdb_generic_dtd *generic_dtd;
920 	const struct generic_dtd_entry *dtd;
921 	struct drm_display_mode *panel_fixed_mode;
922 	int num_dtd;
923 
924 	/*
925 	 * Older VBTs provided DTD information for internal displays through
926 	 * the "LFP panel tables" block (42).  As of VBT revision 229 the
927 	 * DTD information should be provided via a newer "generic DTD"
928 	 * block (58).  Just to be safe, we'll try the new generic DTD block
929 	 * first on VBT >= 229, but still fall back to trying the old LFP
930 	 * block if that fails.
931 	 */
932 	if (i915->display.vbt.version < 229)
933 		return;
934 
935 	generic_dtd = find_section(i915, BDB_GENERIC_DTD);
936 	if (!generic_dtd)
937 		return;
938 
939 	if (generic_dtd->gdtd_size < sizeof(struct generic_dtd_entry)) {
940 		drm_err(&i915->drm, "GDTD size %u is too small.\n",
941 			generic_dtd->gdtd_size);
942 		return;
943 	} else if (generic_dtd->gdtd_size !=
944 		   sizeof(struct generic_dtd_entry)) {
945 		drm_err(&i915->drm, "Unexpected GDTD size %u\n",
946 			generic_dtd->gdtd_size);
947 		/* DTD has unknown fields, but keep going */
948 	}
949 
950 	num_dtd = (get_blocksize(generic_dtd) -
951 		   sizeof(struct bdb_generic_dtd)) / generic_dtd->gdtd_size;
952 	if (panel->vbt.panel_type >= num_dtd) {
953 		drm_err(&i915->drm,
954 			"Panel type %d not found in table of %d DTD's\n",
955 			panel->vbt.panel_type, num_dtd);
956 		return;
957 	}
958 
959 	dtd = &generic_dtd->dtd[panel->vbt.panel_type];
960 
961 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
962 	if (!panel_fixed_mode)
963 		return;
964 
965 	panel_fixed_mode->hdisplay = dtd->hactive;
966 	panel_fixed_mode->hsync_start =
967 		panel_fixed_mode->hdisplay + dtd->hfront_porch;
968 	panel_fixed_mode->hsync_end =
969 		panel_fixed_mode->hsync_start + dtd->hsync;
970 	panel_fixed_mode->htotal =
971 		panel_fixed_mode->hdisplay + dtd->hblank;
972 
973 	panel_fixed_mode->vdisplay = dtd->vactive;
974 	panel_fixed_mode->vsync_start =
975 		panel_fixed_mode->vdisplay + dtd->vfront_porch;
976 	panel_fixed_mode->vsync_end =
977 		panel_fixed_mode->vsync_start + dtd->vsync;
978 	panel_fixed_mode->vtotal =
979 		panel_fixed_mode->vdisplay + dtd->vblank;
980 
981 	panel_fixed_mode->clock = dtd->pixel_clock;
982 	panel_fixed_mode->width_mm = dtd->width_mm;
983 	panel_fixed_mode->height_mm = dtd->height_mm;
984 
985 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
986 	drm_mode_set_name(panel_fixed_mode);
987 
988 	if (dtd->hsync_positive_polarity)
989 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
990 	else
991 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
992 
993 	if (dtd->vsync_positive_polarity)
994 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
995 	else
996 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
997 
998 	drm_dbg_kms(&i915->drm,
999 		    "Found panel mode in BIOS VBT generic dtd table: " DRM_MODE_FMT "\n",
1000 		    DRM_MODE_ARG(panel_fixed_mode));
1001 
1002 	panel->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
1003 }
1004 
1005 static void
1006 parse_lfp_backlight(struct drm_i915_private *i915,
1007 		    struct intel_panel *panel)
1008 {
1009 	const struct bdb_lfp_backlight_data *backlight_data;
1010 	const struct lfp_backlight_data_entry *entry;
1011 	int panel_type = panel->vbt.panel_type;
1012 	u16 level;
1013 
1014 	backlight_data = find_section(i915, BDB_LVDS_BACKLIGHT);
1015 	if (!backlight_data)
1016 		return;
1017 
1018 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
1019 		drm_dbg_kms(&i915->drm,
1020 			    "Unsupported backlight data entry size %u\n",
1021 			    backlight_data->entry_size);
1022 		return;
1023 	}
1024 
1025 	entry = &backlight_data->data[panel_type];
1026 
1027 	panel->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
1028 	if (!panel->vbt.backlight.present) {
1029 		drm_dbg_kms(&i915->drm,
1030 			    "PWM backlight not present in VBT (type %u)\n",
1031 			    entry->type);
1032 		return;
1033 	}
1034 
1035 	panel->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
1036 	panel->vbt.backlight.controller = 0;
1037 	if (i915->display.vbt.version >= 191) {
1038 		size_t exp_size;
1039 
1040 		if (i915->display.vbt.version >= 236)
1041 			exp_size = sizeof(struct bdb_lfp_backlight_data);
1042 		else if (i915->display.vbt.version >= 234)
1043 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_234;
1044 		else
1045 			exp_size = EXP_BDB_LFP_BL_DATA_SIZE_REV_191;
1046 
1047 		if (get_blocksize(backlight_data) >= exp_size) {
1048 			const struct lfp_backlight_control_method *method;
1049 
1050 			method = &backlight_data->backlight_control[panel_type];
1051 			panel->vbt.backlight.type = method->type;
1052 			panel->vbt.backlight.controller = method->controller;
1053 		}
1054 	}
1055 
1056 	panel->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
1057 	panel->vbt.backlight.active_low_pwm = entry->active_low_pwm;
1058 
1059 	if (i915->display.vbt.version >= 234) {
1060 		u16 min_level;
1061 		bool scale;
1062 
1063 		level = backlight_data->brightness_level[panel_type].level;
1064 		min_level = backlight_data->brightness_min_level[panel_type].level;
1065 
1066 		if (i915->display.vbt.version >= 236)
1067 			scale = backlight_data->brightness_precision_bits[panel_type] == 16;
1068 		else
1069 			scale = level > 255;
1070 
1071 		if (scale)
1072 			min_level = min_level / 255;
1073 
1074 		if (min_level > 255) {
1075 			drm_warn(&i915->drm, "Brightness min level > 255\n");
1076 			level = 255;
1077 		}
1078 		panel->vbt.backlight.min_brightness = min_level;
1079 
1080 		panel->vbt.backlight.brightness_precision_bits =
1081 			backlight_data->brightness_precision_bits[panel_type];
1082 	} else {
1083 		level = backlight_data->level[panel_type];
1084 		panel->vbt.backlight.min_brightness = entry->min_brightness;
1085 	}
1086 
1087 	drm_dbg_kms(&i915->drm,
1088 		    "VBT backlight PWM modulation frequency %u Hz, "
1089 		    "active %s, min brightness %u, level %u, controller %u\n",
1090 		    panel->vbt.backlight.pwm_freq_hz,
1091 		    panel->vbt.backlight.active_low_pwm ? "low" : "high",
1092 		    panel->vbt.backlight.min_brightness,
1093 		    level,
1094 		    panel->vbt.backlight.controller);
1095 }
1096 
1097 /* Try to find sdvo panel data */
1098 static void
1099 parse_sdvo_panel_data(struct drm_i915_private *i915,
1100 		      struct intel_panel *panel)
1101 {
1102 	const struct bdb_sdvo_panel_dtds *dtds;
1103 	struct drm_display_mode *panel_fixed_mode;
1104 	int index;
1105 
1106 	index = i915->params.vbt_sdvo_panel_type;
1107 	if (index == -2) {
1108 		drm_dbg_kms(&i915->drm,
1109 			    "Ignore SDVO panel mode from BIOS VBT tables.\n");
1110 		return;
1111 	}
1112 
1113 	if (index == -1) {
1114 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
1115 
1116 		sdvo_lvds_options = find_section(i915, BDB_SDVO_LVDS_OPTIONS);
1117 		if (!sdvo_lvds_options)
1118 			return;
1119 
1120 		index = sdvo_lvds_options->panel_type;
1121 	}
1122 
1123 	dtds = find_section(i915, BDB_SDVO_PANEL_DTDS);
1124 	if (!dtds)
1125 		return;
1126 
1127 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
1128 	if (!panel_fixed_mode)
1129 		return;
1130 
1131 	fill_detail_timing_data(panel_fixed_mode, &dtds->dtds[index]);
1132 
1133 	panel->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
1134 
1135 	drm_dbg_kms(&i915->drm,
1136 		    "Found SDVO panel mode in BIOS VBT tables: " DRM_MODE_FMT "\n",
1137 		    DRM_MODE_ARG(panel_fixed_mode));
1138 }
1139 
1140 static int intel_bios_ssc_frequency(struct drm_i915_private *i915,
1141 				    bool alternate)
1142 {
1143 	switch (DISPLAY_VER(i915)) {
1144 	case 2:
1145 		return alternate ? 66667 : 48000;
1146 	case 3:
1147 	case 4:
1148 		return alternate ? 100000 : 96000;
1149 	default:
1150 		return alternate ? 100000 : 120000;
1151 	}
1152 }
1153 
1154 static void
1155 parse_general_features(struct drm_i915_private *i915)
1156 {
1157 	const struct bdb_general_features *general;
1158 
1159 	general = find_section(i915, BDB_GENERAL_FEATURES);
1160 	if (!general)
1161 		return;
1162 
1163 	i915->display.vbt.int_tv_support = general->int_tv_support;
1164 	/* int_crt_support can't be trusted on earlier platforms */
1165 	if (i915->display.vbt.version >= 155 &&
1166 	    (HAS_DDI(i915) || IS_VALLEYVIEW(i915)))
1167 		i915->display.vbt.int_crt_support = general->int_crt_support;
1168 	i915->display.vbt.lvds_use_ssc = general->enable_ssc;
1169 	i915->display.vbt.lvds_ssc_freq =
1170 		intel_bios_ssc_frequency(i915, general->ssc_freq);
1171 	i915->display.vbt.display_clock_mode = general->display_clock_mode;
1172 	i915->display.vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
1173 	if (i915->display.vbt.version >= 181) {
1174 		i915->display.vbt.orientation = general->rotate_180 ?
1175 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
1176 			DRM_MODE_PANEL_ORIENTATION_NORMAL;
1177 	} else {
1178 		i915->display.vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1179 	}
1180 
1181 	if (i915->display.vbt.version >= 249 && general->afc_startup_config) {
1182 		i915->display.vbt.override_afc_startup = true;
1183 		i915->display.vbt.override_afc_startup_val = general->afc_startup_config == 0x1 ? 0x0 : 0x7;
1184 	}
1185 
1186 	drm_dbg_kms(&i915->drm,
1187 		    "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",
1188 		    i915->display.vbt.int_tv_support,
1189 		    i915->display.vbt.int_crt_support,
1190 		    i915->display.vbt.lvds_use_ssc,
1191 		    i915->display.vbt.lvds_ssc_freq,
1192 		    i915->display.vbt.display_clock_mode,
1193 		    i915->display.vbt.fdi_rx_polarity_inverted);
1194 }
1195 
1196 static const struct child_device_config *
1197 child_device_ptr(const struct bdb_general_definitions *defs, int i)
1198 {
1199 	return (const void *) &defs->devices[i * defs->child_dev_size];
1200 }
1201 
1202 static void
1203 parse_sdvo_device_mapping(struct drm_i915_private *i915)
1204 {
1205 	struct sdvo_device_mapping *mapping;
1206 	const struct intel_bios_encoder_data *devdata;
1207 	const struct child_device_config *child;
1208 	int count = 0;
1209 
1210 	/*
1211 	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
1212 	 * accurate and doesn't have to be, as long as it's not too strict.
1213 	 */
1214 	if (!IS_DISPLAY_VER(i915, 3, 7)) {
1215 		drm_dbg_kms(&i915->drm, "Skipping SDVO device mapping\n");
1216 		return;
1217 	}
1218 
1219 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
1220 		child = &devdata->child;
1221 
1222 		if (child->slave_addr != SLAVE_ADDR1 &&
1223 		    child->slave_addr != SLAVE_ADDR2) {
1224 			/*
1225 			 * If the slave address is neither 0x70 nor 0x72,
1226 			 * it is not a SDVO device. Skip it.
1227 			 */
1228 			continue;
1229 		}
1230 		if (child->dvo_port != DEVICE_PORT_DVOB &&
1231 		    child->dvo_port != DEVICE_PORT_DVOC) {
1232 			/* skip the incorrect SDVO port */
1233 			drm_dbg_kms(&i915->drm,
1234 				    "Incorrect SDVO port. Skip it\n");
1235 			continue;
1236 		}
1237 		drm_dbg_kms(&i915->drm,
1238 			    "the SDVO device with slave addr %2x is found on"
1239 			    " %s port\n",
1240 			    child->slave_addr,
1241 			    (child->dvo_port == DEVICE_PORT_DVOB) ?
1242 			    "SDVOB" : "SDVOC");
1243 		mapping = &i915->display.vbt.sdvo_mappings[child->dvo_port - 1];
1244 		if (!mapping->initialized) {
1245 			mapping->dvo_port = child->dvo_port;
1246 			mapping->slave_addr = child->slave_addr;
1247 			mapping->dvo_wiring = child->dvo_wiring;
1248 			mapping->ddc_pin = child->ddc_pin;
1249 			mapping->i2c_pin = child->i2c_pin;
1250 			mapping->initialized = 1;
1251 			drm_dbg_kms(&i915->drm,
1252 				    "SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
1253 				    mapping->dvo_port, mapping->slave_addr,
1254 				    mapping->dvo_wiring, mapping->ddc_pin,
1255 				    mapping->i2c_pin);
1256 		} else {
1257 			drm_dbg_kms(&i915->drm,
1258 				    "Maybe one SDVO port is shared by "
1259 				    "two SDVO device.\n");
1260 		}
1261 		if (child->slave2_addr) {
1262 			/* Maybe this is a SDVO device with multiple inputs */
1263 			/* And the mapping info is not added */
1264 			drm_dbg_kms(&i915->drm,
1265 				    "there exists the slave2_addr. Maybe this"
1266 				    " is a SDVO device with multiple inputs.\n");
1267 		}
1268 		count++;
1269 	}
1270 
1271 	if (!count) {
1272 		/* No SDVO device info is found */
1273 		drm_dbg_kms(&i915->drm,
1274 			    "No SDVO device info is found in VBT\n");
1275 	}
1276 }
1277 
1278 static void
1279 parse_driver_features(struct drm_i915_private *i915)
1280 {
1281 	const struct bdb_driver_features *driver;
1282 
1283 	driver = find_section(i915, BDB_DRIVER_FEATURES);
1284 	if (!driver)
1285 		return;
1286 
1287 	if (DISPLAY_VER(i915) >= 5) {
1288 		/*
1289 		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
1290 		 * to mean "eDP". The VBT spec doesn't agree with that
1291 		 * interpretation, but real world VBTs seem to.
1292 		 */
1293 		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
1294 			i915->display.vbt.int_lvds_support = 0;
1295 	} else {
1296 		/*
1297 		 * FIXME it's not clear which BDB version has the LVDS config
1298 		 * bits defined. Revision history in the VBT spec says:
1299 		 * "0.92 | Add two definitions for VBT value of LVDS Active
1300 		 *  Config (00b and 11b values defined) | 06/13/2005"
1301 		 * but does not the specify the BDB version.
1302 		 *
1303 		 * So far version 134 (on i945gm) is the oldest VBT observed
1304 		 * in the wild with the bits correctly populated. Version
1305 		 * 108 (on i85x) does not have the bits correctly populated.
1306 		 */
1307 		if (i915->display.vbt.version >= 134 &&
1308 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
1309 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
1310 			i915->display.vbt.int_lvds_support = 0;
1311 	}
1312 }
1313 
1314 static void
1315 parse_panel_driver_features(struct drm_i915_private *i915,
1316 			    struct intel_panel *panel)
1317 {
1318 	const struct bdb_driver_features *driver;
1319 
1320 	driver = find_section(i915, BDB_DRIVER_FEATURES);
1321 	if (!driver)
1322 		return;
1323 
1324 	if (i915->display.vbt.version < 228) {
1325 		drm_dbg_kms(&i915->drm, "DRRS State Enabled:%d\n",
1326 			    driver->drrs_enabled);
1327 		/*
1328 		 * If DRRS is not supported, drrs_type has to be set to 0.
1329 		 * This is because, VBT is configured in such a way that
1330 		 * static DRRS is 0 and DRRS not supported is represented by
1331 		 * driver->drrs_enabled=false
1332 		 */
1333 		if (!driver->drrs_enabled && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
1334 			/*
1335 			 * FIXME Should DMRRS perhaps be treated as seamless
1336 			 * but without the automatic downclocking?
1337 			 */
1338 			if (driver->dmrrs_enabled)
1339 				panel->vbt.drrs_type = DRRS_TYPE_STATIC;
1340 			else
1341 				panel->vbt.drrs_type = DRRS_TYPE_NONE;
1342 		}
1343 
1344 		panel->vbt.psr.enable = driver->psr_enabled;
1345 	}
1346 }
1347 
1348 static void
1349 parse_power_conservation_features(struct drm_i915_private *i915,
1350 				  struct intel_panel *panel)
1351 {
1352 	const struct bdb_lfp_power *power;
1353 	u8 panel_type = panel->vbt.panel_type;
1354 
1355 	panel->vbt.vrr = true; /* matches Windows behaviour */
1356 
1357 	if (i915->display.vbt.version < 228)
1358 		return;
1359 
1360 	power = find_section(i915, BDB_LFP_POWER);
1361 	if (!power)
1362 		return;
1363 
1364 	panel->vbt.psr.enable = panel_bool(power->psr, panel_type);
1365 
1366 	/*
1367 	 * If DRRS is not supported, drrs_type has to be set to 0.
1368 	 * This is because, VBT is configured in such a way that
1369 	 * static DRRS is 0 and DRRS not supported is represented by
1370 	 * power->drrs & BIT(panel_type)=false
1371 	 */
1372 	if (!panel_bool(power->drrs, panel_type) && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
1373 		/*
1374 		 * FIXME Should DMRRS perhaps be treated as seamless
1375 		 * but without the automatic downclocking?
1376 		 */
1377 		if (panel_bool(power->dmrrs, panel_type))
1378 			panel->vbt.drrs_type = DRRS_TYPE_STATIC;
1379 		else
1380 			panel->vbt.drrs_type = DRRS_TYPE_NONE;
1381 	}
1382 
1383 	if (i915->display.vbt.version >= 232)
1384 		panel->vbt.edp.hobl = panel_bool(power->hobl, panel_type);
1385 
1386 	if (i915->display.vbt.version >= 233)
1387 		panel->vbt.vrr = panel_bool(power->vrr_feature_enabled,
1388 					    panel_type);
1389 }
1390 
1391 static void
1392 parse_edp(struct drm_i915_private *i915,
1393 	  struct intel_panel *panel)
1394 {
1395 	const struct bdb_edp *edp;
1396 	const struct edp_power_seq *edp_pps;
1397 	const struct edp_fast_link_params *edp_link_params;
1398 	int panel_type = panel->vbt.panel_type;
1399 
1400 	edp = find_section(i915, BDB_EDP);
1401 	if (!edp)
1402 		return;
1403 
1404 	switch (panel_bits(edp->color_depth, panel_type, 2)) {
1405 	case EDP_18BPP:
1406 		panel->vbt.edp.bpp = 18;
1407 		break;
1408 	case EDP_24BPP:
1409 		panel->vbt.edp.bpp = 24;
1410 		break;
1411 	case EDP_30BPP:
1412 		panel->vbt.edp.bpp = 30;
1413 		break;
1414 	}
1415 
1416 	/* Get the eDP sequencing and link info */
1417 	edp_pps = &edp->power_seqs[panel_type];
1418 	edp_link_params = &edp->fast_link_params[panel_type];
1419 
1420 	panel->vbt.edp.pps = *edp_pps;
1421 
1422 	if (i915->display.vbt.version >= 224) {
1423 		panel->vbt.edp.rate =
1424 			edp->edp_fast_link_training_rate[panel_type] * 20;
1425 	} else {
1426 		switch (edp_link_params->rate) {
1427 		case EDP_RATE_1_62:
1428 			panel->vbt.edp.rate = 162000;
1429 			break;
1430 		case EDP_RATE_2_7:
1431 			panel->vbt.edp.rate = 270000;
1432 			break;
1433 		case EDP_RATE_5_4:
1434 			panel->vbt.edp.rate = 540000;
1435 			break;
1436 		default:
1437 			drm_dbg_kms(&i915->drm,
1438 				    "VBT has unknown eDP link rate value %u\n",
1439 				    edp_link_params->rate);
1440 			break;
1441 		}
1442 	}
1443 
1444 	switch (edp_link_params->lanes) {
1445 	case EDP_LANE_1:
1446 		panel->vbt.edp.lanes = 1;
1447 		break;
1448 	case EDP_LANE_2:
1449 		panel->vbt.edp.lanes = 2;
1450 		break;
1451 	case EDP_LANE_4:
1452 		panel->vbt.edp.lanes = 4;
1453 		break;
1454 	default:
1455 		drm_dbg_kms(&i915->drm,
1456 			    "VBT has unknown eDP lane count value %u\n",
1457 			    edp_link_params->lanes);
1458 		break;
1459 	}
1460 
1461 	switch (edp_link_params->preemphasis) {
1462 	case EDP_PREEMPHASIS_NONE:
1463 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
1464 		break;
1465 	case EDP_PREEMPHASIS_3_5dB:
1466 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
1467 		break;
1468 	case EDP_PREEMPHASIS_6dB:
1469 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
1470 		break;
1471 	case EDP_PREEMPHASIS_9_5dB:
1472 		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
1473 		break;
1474 	default:
1475 		drm_dbg_kms(&i915->drm,
1476 			    "VBT has unknown eDP pre-emphasis value %u\n",
1477 			    edp_link_params->preemphasis);
1478 		break;
1479 	}
1480 
1481 	switch (edp_link_params->vswing) {
1482 	case EDP_VSWING_0_4V:
1483 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
1484 		break;
1485 	case EDP_VSWING_0_6V:
1486 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
1487 		break;
1488 	case EDP_VSWING_0_8V:
1489 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
1490 		break;
1491 	case EDP_VSWING_1_2V:
1492 		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
1493 		break;
1494 	default:
1495 		drm_dbg_kms(&i915->drm,
1496 			    "VBT has unknown eDP voltage swing value %u\n",
1497 			    edp_link_params->vswing);
1498 		break;
1499 	}
1500 
1501 	if (i915->display.vbt.version >= 173) {
1502 		u8 vswing;
1503 
1504 		/* Don't read from VBT if module parameter has valid value*/
1505 		if (i915->params.edp_vswing) {
1506 			panel->vbt.edp.low_vswing =
1507 				i915->params.edp_vswing == 1;
1508 		} else {
1509 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
1510 			panel->vbt.edp.low_vswing = vswing == 0;
1511 		}
1512 	}
1513 
1514 	panel->vbt.edp.drrs_msa_timing_delay =
1515 		panel_bits(edp->sdrrs_msa_timing_delay, panel_type, 2);
1516 
1517 	if (i915->display.vbt.version >= 244)
1518 		panel->vbt.edp.max_link_rate =
1519 			edp->edp_max_port_link_rate[panel_type] * 20;
1520 }
1521 
1522 static void
1523 parse_psr(struct drm_i915_private *i915,
1524 	  struct intel_panel *panel)
1525 {
1526 	const struct bdb_psr *psr;
1527 	const struct psr_table *psr_table;
1528 	int panel_type = panel->vbt.panel_type;
1529 
1530 	psr = find_section(i915, BDB_PSR);
1531 	if (!psr) {
1532 		drm_dbg_kms(&i915->drm, "No PSR BDB found.\n");
1533 		return;
1534 	}
1535 
1536 	psr_table = &psr->psr_table[panel_type];
1537 
1538 	panel->vbt.psr.full_link = psr_table->full_link;
1539 	panel->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
1540 
1541 	/* Allowed VBT values goes from 0 to 15 */
1542 	panel->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
1543 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
1544 
1545 	/*
1546 	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
1547 	 * Old decimal value is wake up time in multiples of 100 us.
1548 	 */
1549 	if (i915->display.vbt.version >= 205 &&
1550 	    (DISPLAY_VER(i915) >= 9 && !IS_BROXTON(i915))) {
1551 		switch (psr_table->tp1_wakeup_time) {
1552 		case 0:
1553 			panel->vbt.psr.tp1_wakeup_time_us = 500;
1554 			break;
1555 		case 1:
1556 			panel->vbt.psr.tp1_wakeup_time_us = 100;
1557 			break;
1558 		case 3:
1559 			panel->vbt.psr.tp1_wakeup_time_us = 0;
1560 			break;
1561 		default:
1562 			drm_dbg_kms(&i915->drm,
1563 				    "VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1564 				    psr_table->tp1_wakeup_time);
1565 			fallthrough;
1566 		case 2:
1567 			panel->vbt.psr.tp1_wakeup_time_us = 2500;
1568 			break;
1569 		}
1570 
1571 		switch (psr_table->tp2_tp3_wakeup_time) {
1572 		case 0:
1573 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 500;
1574 			break;
1575 		case 1:
1576 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 100;
1577 			break;
1578 		case 3:
1579 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 0;
1580 			break;
1581 		default:
1582 			drm_dbg_kms(&i915->drm,
1583 				    "VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1584 				    psr_table->tp2_tp3_wakeup_time);
1585 			fallthrough;
1586 		case 2:
1587 			panel->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
1588 		break;
1589 		}
1590 	} else {
1591 		panel->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
1592 		panel->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
1593 	}
1594 
1595 	if (i915->display.vbt.version >= 226) {
1596 		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
1597 
1598 		wakeup_time = panel_bits(wakeup_time, panel_type, 2);
1599 		switch (wakeup_time) {
1600 		case 0:
1601 			wakeup_time = 500;
1602 			break;
1603 		case 1:
1604 			wakeup_time = 100;
1605 			break;
1606 		case 3:
1607 			wakeup_time = 50;
1608 			break;
1609 		default:
1610 		case 2:
1611 			wakeup_time = 2500;
1612 			break;
1613 		}
1614 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
1615 	} else {
1616 		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
1617 		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = panel->vbt.psr.tp2_tp3_wakeup_time_us;
1618 	}
1619 }
1620 
1621 static void parse_dsi_backlight_ports(struct drm_i915_private *i915,
1622 				      struct intel_panel *panel,
1623 				      enum port port)
1624 {
1625 	enum port port_bc = DISPLAY_VER(i915) >= 11 ? PORT_B : PORT_C;
1626 
1627 	if (!panel->vbt.dsi.config->dual_link || i915->display.vbt.version < 197) {
1628 		panel->vbt.dsi.bl_ports = BIT(port);
1629 		if (panel->vbt.dsi.config->cabc_supported)
1630 			panel->vbt.dsi.cabc_ports = BIT(port);
1631 
1632 		return;
1633 	}
1634 
1635 	switch (panel->vbt.dsi.config->dl_dcs_backlight_ports) {
1636 	case DL_DCS_PORT_A:
1637 		panel->vbt.dsi.bl_ports = BIT(PORT_A);
1638 		break;
1639 	case DL_DCS_PORT_C:
1640 		panel->vbt.dsi.bl_ports = BIT(port_bc);
1641 		break;
1642 	default:
1643 	case DL_DCS_PORT_A_AND_C:
1644 		panel->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(port_bc);
1645 		break;
1646 	}
1647 
1648 	if (!panel->vbt.dsi.config->cabc_supported)
1649 		return;
1650 
1651 	switch (panel->vbt.dsi.config->dl_dcs_cabc_ports) {
1652 	case DL_DCS_PORT_A:
1653 		panel->vbt.dsi.cabc_ports = BIT(PORT_A);
1654 		break;
1655 	case DL_DCS_PORT_C:
1656 		panel->vbt.dsi.cabc_ports = BIT(port_bc);
1657 		break;
1658 	default:
1659 	case DL_DCS_PORT_A_AND_C:
1660 		panel->vbt.dsi.cabc_ports =
1661 					BIT(PORT_A) | BIT(port_bc);
1662 		break;
1663 	}
1664 }
1665 
1666 static void
1667 parse_mipi_config(struct drm_i915_private *i915,
1668 		  struct intel_panel *panel)
1669 {
1670 	const struct bdb_mipi_config *start;
1671 	const struct mipi_config *config;
1672 	const struct mipi_pps_data *pps;
1673 	int panel_type = panel->vbt.panel_type;
1674 	enum port port;
1675 
1676 	/* parse MIPI blocks only if LFP type is MIPI */
1677 	if (!intel_bios_is_dsi_present(i915, &port))
1678 		return;
1679 
1680 	/* Initialize this to undefined indicating no generic MIPI support */
1681 	panel->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
1682 
1683 	/* Block #40 is already parsed and panel_fixed_mode is
1684 	 * stored in i915->lfp_lvds_vbt_mode
1685 	 * resuse this when needed
1686 	 */
1687 
1688 	/* Parse #52 for panel index used from panel_type already
1689 	 * parsed
1690 	 */
1691 	start = find_section(i915, BDB_MIPI_CONFIG);
1692 	if (!start) {
1693 		drm_dbg_kms(&i915->drm, "No MIPI config BDB found");
1694 		return;
1695 	}
1696 
1697 	drm_dbg(&i915->drm, "Found MIPI Config block, panel index = %d\n",
1698 		panel_type);
1699 
1700 	/*
1701 	 * get hold of the correct configuration block and pps data as per
1702 	 * the panel_type as index
1703 	 */
1704 	config = &start->config[panel_type];
1705 	pps = &start->pps[panel_type];
1706 
1707 	/* store as of now full data. Trim when we realise all is not needed */
1708 	panel->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
1709 	if (!panel->vbt.dsi.config)
1710 		return;
1711 
1712 	panel->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
1713 	if (!panel->vbt.dsi.pps) {
1714 		kfree(panel->vbt.dsi.config);
1715 		return;
1716 	}
1717 
1718 	parse_dsi_backlight_ports(i915, panel, port);
1719 
1720 	/* FIXME is the 90 vs. 270 correct? */
1721 	switch (config->rotation) {
1722 	case ENABLE_ROTATION_0:
1723 		/*
1724 		 * Most (all?) VBTs claim 0 degrees despite having
1725 		 * an upside down panel, thus we do not trust this.
1726 		 */
1727 		panel->vbt.dsi.orientation =
1728 			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1729 		break;
1730 	case ENABLE_ROTATION_90:
1731 		panel->vbt.dsi.orientation =
1732 			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1733 		break;
1734 	case ENABLE_ROTATION_180:
1735 		panel->vbt.dsi.orientation =
1736 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1737 		break;
1738 	case ENABLE_ROTATION_270:
1739 		panel->vbt.dsi.orientation =
1740 			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1741 		break;
1742 	}
1743 
1744 	/* We have mandatory mipi config blocks. Initialize as generic panel */
1745 	panel->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
1746 }
1747 
1748 /* Find the sequence block and size for the given panel. */
1749 static const u8 *
1750 find_panel_sequence_block(const struct bdb_mipi_sequence *sequence,
1751 			  u16 panel_id, u32 *seq_size)
1752 {
1753 	u32 total = get_blocksize(sequence);
1754 	const u8 *data = &sequence->data[0];
1755 	u8 current_id;
1756 	u32 current_size;
1757 	int header_size = sequence->version >= 3 ? 5 : 3;
1758 	int index = 0;
1759 	int i;
1760 
1761 	/* skip new block size */
1762 	if (sequence->version >= 3)
1763 		data += 4;
1764 
1765 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
1766 		if (index + header_size > total) {
1767 			DRM_ERROR("Invalid sequence block (header)\n");
1768 			return NULL;
1769 		}
1770 
1771 		current_id = *(data + index);
1772 		if (sequence->version >= 3)
1773 			current_size = *((const u32 *)(data + index + 1));
1774 		else
1775 			current_size = *((const u16 *)(data + index + 1));
1776 
1777 		index += header_size;
1778 
1779 		if (index + current_size > total) {
1780 			DRM_ERROR("Invalid sequence block\n");
1781 			return NULL;
1782 		}
1783 
1784 		if (current_id == panel_id) {
1785 			*seq_size = current_size;
1786 			return data + index;
1787 		}
1788 
1789 		index += current_size;
1790 	}
1791 
1792 	DRM_ERROR("Sequence block detected but no valid configuration\n");
1793 
1794 	return NULL;
1795 }
1796 
1797 static int goto_next_sequence(const u8 *data, int index, int total)
1798 {
1799 	u16 len;
1800 
1801 	/* Skip Sequence Byte. */
1802 	for (index = index + 1; index < total; index += len) {
1803 		u8 operation_byte = *(data + index);
1804 		index++;
1805 
1806 		switch (operation_byte) {
1807 		case MIPI_SEQ_ELEM_END:
1808 			return index;
1809 		case MIPI_SEQ_ELEM_SEND_PKT:
1810 			if (index + 4 > total)
1811 				return 0;
1812 
1813 			len = *((const u16 *)(data + index + 2)) + 4;
1814 			break;
1815 		case MIPI_SEQ_ELEM_DELAY:
1816 			len = 4;
1817 			break;
1818 		case MIPI_SEQ_ELEM_GPIO:
1819 			len = 2;
1820 			break;
1821 		case MIPI_SEQ_ELEM_I2C:
1822 			if (index + 7 > total)
1823 				return 0;
1824 			len = *(data + index + 6) + 7;
1825 			break;
1826 		default:
1827 			DRM_ERROR("Unknown operation byte\n");
1828 			return 0;
1829 		}
1830 	}
1831 
1832 	return 0;
1833 }
1834 
1835 static int goto_next_sequence_v3(const u8 *data, int index, int total)
1836 {
1837 	int seq_end;
1838 	u16 len;
1839 	u32 size_of_sequence;
1840 
1841 	/*
1842 	 * Could skip sequence based on Size of Sequence alone, but also do some
1843 	 * checking on the structure.
1844 	 */
1845 	if (total < 5) {
1846 		DRM_ERROR("Too small sequence size\n");
1847 		return 0;
1848 	}
1849 
1850 	/* Skip Sequence Byte. */
1851 	index++;
1852 
1853 	/*
1854 	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
1855 	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
1856 	 * byte.
1857 	 */
1858 	size_of_sequence = *((const u32 *)(data + index));
1859 	index += 4;
1860 
1861 	seq_end = index + size_of_sequence;
1862 	if (seq_end > total) {
1863 		DRM_ERROR("Invalid sequence size\n");
1864 		return 0;
1865 	}
1866 
1867 	for (; index < total; index += len) {
1868 		u8 operation_byte = *(data + index);
1869 		index++;
1870 
1871 		if (operation_byte == MIPI_SEQ_ELEM_END) {
1872 			if (index != seq_end) {
1873 				DRM_ERROR("Invalid element structure\n");
1874 				return 0;
1875 			}
1876 			return index;
1877 		}
1878 
1879 		len = *(data + index);
1880 		index++;
1881 
1882 		/*
1883 		 * FIXME: Would be nice to check elements like for v1/v2 in
1884 		 * goto_next_sequence() above.
1885 		 */
1886 		switch (operation_byte) {
1887 		case MIPI_SEQ_ELEM_SEND_PKT:
1888 		case MIPI_SEQ_ELEM_DELAY:
1889 		case MIPI_SEQ_ELEM_GPIO:
1890 		case MIPI_SEQ_ELEM_I2C:
1891 		case MIPI_SEQ_ELEM_SPI:
1892 		case MIPI_SEQ_ELEM_PMIC:
1893 			break;
1894 		default:
1895 			DRM_ERROR("Unknown operation byte %u\n",
1896 				  operation_byte);
1897 			break;
1898 		}
1899 	}
1900 
1901 	return 0;
1902 }
1903 
1904 /*
1905  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
1906  * skip all delay + gpio operands and stop at the first DSI packet op.
1907  */
1908 static int get_init_otp_deassert_fragment_len(struct drm_i915_private *i915,
1909 					      struct intel_panel *panel)
1910 {
1911 	const u8 *data = panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1912 	int index, len;
1913 
1914 	if (drm_WARN_ON(&i915->drm,
1915 			!data || panel->vbt.dsi.seq_version != 1))
1916 		return 0;
1917 
1918 	/* index = 1 to skip sequence byte */
1919 	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1920 		switch (data[index]) {
1921 		case MIPI_SEQ_ELEM_SEND_PKT:
1922 			return index == 1 ? 0 : index;
1923 		case MIPI_SEQ_ELEM_DELAY:
1924 			len = 5; /* 1 byte for operand + uint32 */
1925 			break;
1926 		case MIPI_SEQ_ELEM_GPIO:
1927 			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1928 			break;
1929 		default:
1930 			return 0;
1931 		}
1932 	}
1933 
1934 	return 0;
1935 }
1936 
1937 /*
1938  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1939  * The deassert must be done before calling intel_dsi_device_ready, so for
1940  * these devices we split the init OTP sequence into a deassert sequence and
1941  * the actual init OTP part.
1942  */
1943 static void fixup_mipi_sequences(struct drm_i915_private *i915,
1944 				 struct intel_panel *panel)
1945 {
1946 	u8 *init_otp;
1947 	int len;
1948 
1949 	/* Limit this to VLV for now. */
1950 	if (!IS_VALLEYVIEW(i915))
1951 		return;
1952 
1953 	/* Limit this to v1 vid-mode sequences */
1954 	if (panel->vbt.dsi.config->is_cmd_mode ||
1955 	    panel->vbt.dsi.seq_version != 1)
1956 		return;
1957 
1958 	/* Only do this if there are otp and assert seqs and no deassert seq */
1959 	if (!panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
1960 	    !panel->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
1961 	    panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1962 		return;
1963 
1964 	/* The deassert-sequence ends at the first DSI packet */
1965 	len = get_init_otp_deassert_fragment_len(i915, panel);
1966 	if (!len)
1967 		return;
1968 
1969 	drm_dbg_kms(&i915->drm,
1970 		    "Using init OTP fragment to deassert reset\n");
1971 
1972 	/* Copy the fragment, update seq byte and terminate it */
1973 	init_otp = (u8 *)panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1974 	panel->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
1975 	if (!panel->vbt.dsi.deassert_seq)
1976 		return;
1977 	panel->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
1978 	panel->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1979 	/* Use the copy for deassert */
1980 	panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
1981 		panel->vbt.dsi.deassert_seq;
1982 	/* Replace the last byte of the fragment with init OTP seq byte */
1983 	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1984 	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
1985 	panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
1986 }
1987 
1988 static void
1989 parse_mipi_sequence(struct drm_i915_private *i915,
1990 		    struct intel_panel *panel)
1991 {
1992 	int panel_type = panel->vbt.panel_type;
1993 	const struct bdb_mipi_sequence *sequence;
1994 	const u8 *seq_data;
1995 	u32 seq_size;
1996 	u8 *data;
1997 	int index = 0;
1998 
1999 	/* Only our generic panel driver uses the sequence block. */
2000 	if (panel->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
2001 		return;
2002 
2003 	sequence = find_section(i915, BDB_MIPI_SEQUENCE);
2004 	if (!sequence) {
2005 		drm_dbg_kms(&i915->drm,
2006 			    "No MIPI Sequence found, parsing complete\n");
2007 		return;
2008 	}
2009 
2010 	/* Fail gracefully for forward incompatible sequence block. */
2011 	if (sequence->version >= 4) {
2012 		drm_err(&i915->drm,
2013 			"Unable to parse MIPI Sequence Block v%u\n",
2014 			sequence->version);
2015 		return;
2016 	}
2017 
2018 	drm_dbg(&i915->drm, "Found MIPI sequence block v%u\n",
2019 		sequence->version);
2020 
2021 	seq_data = find_panel_sequence_block(sequence, panel_type, &seq_size);
2022 	if (!seq_data)
2023 		return;
2024 
2025 	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
2026 	if (!data)
2027 		return;
2028 
2029 	/* Parse the sequences, store pointers to each sequence. */
2030 	for (;;) {
2031 		u8 seq_id = *(data + index);
2032 		if (seq_id == MIPI_SEQ_END)
2033 			break;
2034 
2035 		if (seq_id >= MIPI_SEQ_MAX) {
2036 			drm_err(&i915->drm, "Unknown sequence %u\n",
2037 				seq_id);
2038 			goto err;
2039 		}
2040 
2041 		/* Log about presence of sequences we won't run. */
2042 		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
2043 			drm_dbg_kms(&i915->drm,
2044 				    "Unsupported sequence %u\n", seq_id);
2045 
2046 		panel->vbt.dsi.sequence[seq_id] = data + index;
2047 
2048 		if (sequence->version >= 3)
2049 			index = goto_next_sequence_v3(data, index, seq_size);
2050 		else
2051 			index = goto_next_sequence(data, index, seq_size);
2052 		if (!index) {
2053 			drm_err(&i915->drm, "Invalid sequence %u\n",
2054 				seq_id);
2055 			goto err;
2056 		}
2057 	}
2058 
2059 	panel->vbt.dsi.data = data;
2060 	panel->vbt.dsi.size = seq_size;
2061 	panel->vbt.dsi.seq_version = sequence->version;
2062 
2063 	fixup_mipi_sequences(i915, panel);
2064 
2065 	drm_dbg(&i915->drm, "MIPI related VBT parsing complete\n");
2066 	return;
2067 
2068 err:
2069 	kfree(data);
2070 	memset(panel->vbt.dsi.sequence, 0, sizeof(panel->vbt.dsi.sequence));
2071 }
2072 
2073 static void
2074 parse_compression_parameters(struct drm_i915_private *i915)
2075 {
2076 	const struct bdb_compression_parameters *params;
2077 	struct intel_bios_encoder_data *devdata;
2078 	const struct child_device_config *child;
2079 	u16 block_size;
2080 	int index;
2081 
2082 	if (i915->display.vbt.version < 198)
2083 		return;
2084 
2085 	params = find_section(i915, BDB_COMPRESSION_PARAMETERS);
2086 	if (params) {
2087 		/* Sanity checks */
2088 		if (params->entry_size != sizeof(params->data[0])) {
2089 			drm_dbg_kms(&i915->drm,
2090 				    "VBT: unsupported compression param entry size\n");
2091 			return;
2092 		}
2093 
2094 		block_size = get_blocksize(params);
2095 		if (block_size < sizeof(*params)) {
2096 			drm_dbg_kms(&i915->drm,
2097 				    "VBT: expected 16 compression param entries\n");
2098 			return;
2099 		}
2100 	}
2101 
2102 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
2103 		child = &devdata->child;
2104 
2105 		if (!child->compression_enable)
2106 			continue;
2107 
2108 		if (!params) {
2109 			drm_dbg_kms(&i915->drm,
2110 				    "VBT: compression params not available\n");
2111 			continue;
2112 		}
2113 
2114 		if (child->compression_method_cps) {
2115 			drm_dbg_kms(&i915->drm,
2116 				    "VBT: CPS compression not supported\n");
2117 			continue;
2118 		}
2119 
2120 		index = child->compression_structure_index;
2121 
2122 		devdata->dsc = kmemdup(&params->data[index],
2123 				       sizeof(*devdata->dsc), GFP_KERNEL);
2124 	}
2125 }
2126 
2127 static u8 translate_iboost(u8 val)
2128 {
2129 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
2130 
2131 	if (val >= ARRAY_SIZE(mapping)) {
2132 		DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
2133 		return 0;
2134 	}
2135 	return mapping[val];
2136 }
2137 
2138 static const u8 cnp_ddc_pin_map[] = {
2139 	[0] = 0, /* N/A */
2140 	[DDC_BUS_DDI_B] = GMBUS_PIN_1_BXT,
2141 	[DDC_BUS_DDI_C] = GMBUS_PIN_2_BXT,
2142 	[DDC_BUS_DDI_D] = GMBUS_PIN_4_CNP, /* sic */
2143 	[DDC_BUS_DDI_F] = GMBUS_PIN_3_BXT, /* sic */
2144 };
2145 
2146 static const u8 icp_ddc_pin_map[] = {
2147 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
2148 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
2149 	[TGL_DDC_BUS_DDI_C] = GMBUS_PIN_3_BXT,
2150 	[ICL_DDC_BUS_PORT_1] = GMBUS_PIN_9_TC1_ICP,
2151 	[ICL_DDC_BUS_PORT_2] = GMBUS_PIN_10_TC2_ICP,
2152 	[ICL_DDC_BUS_PORT_3] = GMBUS_PIN_11_TC3_ICP,
2153 	[ICL_DDC_BUS_PORT_4] = GMBUS_PIN_12_TC4_ICP,
2154 	[TGL_DDC_BUS_PORT_5] = GMBUS_PIN_13_TC5_TGP,
2155 	[TGL_DDC_BUS_PORT_6] = GMBUS_PIN_14_TC6_TGP,
2156 };
2157 
2158 static const u8 rkl_pch_tgp_ddc_pin_map[] = {
2159 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
2160 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
2161 	[RKL_DDC_BUS_DDI_D] = GMBUS_PIN_9_TC1_ICP,
2162 	[RKL_DDC_BUS_DDI_E] = GMBUS_PIN_10_TC2_ICP,
2163 };
2164 
2165 static const u8 adls_ddc_pin_map[] = {
2166 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
2167 	[ADLS_DDC_BUS_PORT_TC1] = GMBUS_PIN_9_TC1_ICP,
2168 	[ADLS_DDC_BUS_PORT_TC2] = GMBUS_PIN_10_TC2_ICP,
2169 	[ADLS_DDC_BUS_PORT_TC3] = GMBUS_PIN_11_TC3_ICP,
2170 	[ADLS_DDC_BUS_PORT_TC4] = GMBUS_PIN_12_TC4_ICP,
2171 };
2172 
2173 static const u8 gen9bc_tgp_ddc_pin_map[] = {
2174 	[DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
2175 	[DDC_BUS_DDI_C] = GMBUS_PIN_9_TC1_ICP,
2176 	[DDC_BUS_DDI_D] = GMBUS_PIN_10_TC2_ICP,
2177 };
2178 
2179 static const u8 adlp_ddc_pin_map[] = {
2180 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
2181 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
2182 	[ADLP_DDC_BUS_PORT_TC1] = GMBUS_PIN_9_TC1_ICP,
2183 	[ADLP_DDC_BUS_PORT_TC2] = GMBUS_PIN_10_TC2_ICP,
2184 	[ADLP_DDC_BUS_PORT_TC3] = GMBUS_PIN_11_TC3_ICP,
2185 	[ADLP_DDC_BUS_PORT_TC4] = GMBUS_PIN_12_TC4_ICP,
2186 };
2187 
2188 static u8 map_ddc_pin(struct drm_i915_private *i915, u8 vbt_pin)
2189 {
2190 	const u8 *ddc_pin_map;
2191 	int n_entries;
2192 
2193 	if (HAS_PCH_MTP(i915) || IS_ALDERLAKE_P(i915)) {
2194 		ddc_pin_map = adlp_ddc_pin_map;
2195 		n_entries = ARRAY_SIZE(adlp_ddc_pin_map);
2196 	} else if (IS_ALDERLAKE_S(i915)) {
2197 		ddc_pin_map = adls_ddc_pin_map;
2198 		n_entries = ARRAY_SIZE(adls_ddc_pin_map);
2199 	} else if (INTEL_PCH_TYPE(i915) >= PCH_DG1) {
2200 		return vbt_pin;
2201 	} else if (IS_ROCKETLAKE(i915) && INTEL_PCH_TYPE(i915) == PCH_TGP) {
2202 		ddc_pin_map = rkl_pch_tgp_ddc_pin_map;
2203 		n_entries = ARRAY_SIZE(rkl_pch_tgp_ddc_pin_map);
2204 	} else if (HAS_PCH_TGP(i915) && DISPLAY_VER(i915) == 9) {
2205 		ddc_pin_map = gen9bc_tgp_ddc_pin_map;
2206 		n_entries = ARRAY_SIZE(gen9bc_tgp_ddc_pin_map);
2207 	} else if (INTEL_PCH_TYPE(i915) >= PCH_ICP) {
2208 		ddc_pin_map = icp_ddc_pin_map;
2209 		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
2210 	} else if (HAS_PCH_CNP(i915)) {
2211 		ddc_pin_map = cnp_ddc_pin_map;
2212 		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
2213 	} else {
2214 		/* Assuming direct map */
2215 		return vbt_pin;
2216 	}
2217 
2218 	if (vbt_pin < n_entries && ddc_pin_map[vbt_pin] != 0)
2219 		return ddc_pin_map[vbt_pin];
2220 
2221 	drm_dbg_kms(&i915->drm,
2222 		    "Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
2223 		    vbt_pin);
2224 	return 0;
2225 }
2226 
2227 static enum port get_port_by_ddc_pin(struct drm_i915_private *i915, u8 ddc_pin)
2228 {
2229 	const struct intel_bios_encoder_data *devdata;
2230 	enum port port;
2231 
2232 	if (!ddc_pin)
2233 		return PORT_NONE;
2234 
2235 	for_each_port(port) {
2236 		devdata = i915->display.vbt.ports[port];
2237 
2238 		if (devdata && ddc_pin == devdata->child.ddc_pin)
2239 			return port;
2240 	}
2241 
2242 	return PORT_NONE;
2243 }
2244 
2245 static void sanitize_ddc_pin(struct intel_bios_encoder_data *devdata,
2246 			     enum port port)
2247 {
2248 	struct drm_i915_private *i915 = devdata->i915;
2249 	struct child_device_config *child;
2250 	u8 mapped_ddc_pin;
2251 	enum port p;
2252 
2253 	if (!devdata->child.ddc_pin)
2254 		return;
2255 
2256 	mapped_ddc_pin = map_ddc_pin(i915, devdata->child.ddc_pin);
2257 	if (!intel_gmbus_is_valid_pin(i915, mapped_ddc_pin)) {
2258 		drm_dbg_kms(&i915->drm,
2259 			    "Port %c has invalid DDC pin %d, "
2260 			    "sticking to defaults\n",
2261 			    port_name(port), mapped_ddc_pin);
2262 		devdata->child.ddc_pin = 0;
2263 		return;
2264 	}
2265 
2266 	p = get_port_by_ddc_pin(i915, devdata->child.ddc_pin);
2267 	if (p == PORT_NONE)
2268 		return;
2269 
2270 	drm_dbg_kms(&i915->drm,
2271 		    "port %c trying to use the same DDC pin (0x%x) as port %c, "
2272 		    "disabling port %c DVI/HDMI support\n",
2273 		    port_name(port), mapped_ddc_pin,
2274 		    port_name(p), port_name(p));
2275 
2276 	/*
2277 	 * If we have multiple ports supposedly sharing the pin, then dvi/hdmi
2278 	 * couldn't exist on the shared port. Otherwise they share the same ddc
2279 	 * pin and system couldn't communicate with them separately.
2280 	 *
2281 	 * Give inverse child device order the priority, last one wins. Yes,
2282 	 * there are real machines (eg. Asrock B250M-HDV) where VBT has both
2283 	 * port A and port E with the same AUX ch and we must pick port E :(
2284 	 */
2285 	child = &i915->display.vbt.ports[p]->child;
2286 
2287 	child->device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
2288 	child->device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
2289 
2290 	child->ddc_pin = 0;
2291 }
2292 
2293 static enum port get_port_by_aux_ch(struct drm_i915_private *i915, u8 aux_ch)
2294 {
2295 	const struct intel_bios_encoder_data *devdata;
2296 	enum port port;
2297 
2298 	if (!aux_ch)
2299 		return PORT_NONE;
2300 
2301 	for_each_port(port) {
2302 		devdata = i915->display.vbt.ports[port];
2303 
2304 		if (devdata && aux_ch == devdata->child.aux_channel)
2305 			return port;
2306 	}
2307 
2308 	return PORT_NONE;
2309 }
2310 
2311 static void sanitize_aux_ch(struct intel_bios_encoder_data *devdata,
2312 			    enum port port)
2313 {
2314 	struct drm_i915_private *i915 = devdata->i915;
2315 	struct child_device_config *child;
2316 	enum port p;
2317 
2318 	p = get_port_by_aux_ch(i915, devdata->child.aux_channel);
2319 	if (p == PORT_NONE)
2320 		return;
2321 
2322 	drm_dbg_kms(&i915->drm,
2323 		    "port %c trying to use the same AUX CH (0x%x) as port %c, "
2324 		    "disabling port %c DP support\n",
2325 		    port_name(port), devdata->child.aux_channel,
2326 		    port_name(p), port_name(p));
2327 
2328 	/*
2329 	 * If we have multiple ports supposedly sharing the aux channel, then DP
2330 	 * couldn't exist on the shared port. Otherwise they share the same aux
2331 	 * channel and system couldn't communicate with them separately.
2332 	 *
2333 	 * Give inverse child device order the priority, last one wins. Yes,
2334 	 * there are real machines (eg. Asrock B250M-HDV) where VBT has both
2335 	 * port A and port E with the same AUX ch and we must pick port E :(
2336 	 */
2337 	child = &i915->display.vbt.ports[p]->child;
2338 
2339 	child->device_type &= ~DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2340 	child->aux_channel = 0;
2341 }
2342 
2343 static u8 dvo_port_type(u8 dvo_port)
2344 {
2345 	switch (dvo_port) {
2346 	case DVO_PORT_HDMIA:
2347 	case DVO_PORT_HDMIB:
2348 	case DVO_PORT_HDMIC:
2349 	case DVO_PORT_HDMID:
2350 	case DVO_PORT_HDMIE:
2351 	case DVO_PORT_HDMIF:
2352 	case DVO_PORT_HDMIG:
2353 	case DVO_PORT_HDMIH:
2354 	case DVO_PORT_HDMII:
2355 		return DVO_PORT_HDMIA;
2356 	case DVO_PORT_DPA:
2357 	case DVO_PORT_DPB:
2358 	case DVO_PORT_DPC:
2359 	case DVO_PORT_DPD:
2360 	case DVO_PORT_DPE:
2361 	case DVO_PORT_DPF:
2362 	case DVO_PORT_DPG:
2363 	case DVO_PORT_DPH:
2364 	case DVO_PORT_DPI:
2365 		return DVO_PORT_DPA;
2366 	case DVO_PORT_MIPIA:
2367 	case DVO_PORT_MIPIB:
2368 	case DVO_PORT_MIPIC:
2369 	case DVO_PORT_MIPID:
2370 		return DVO_PORT_MIPIA;
2371 	default:
2372 		return dvo_port;
2373 	}
2374 }
2375 
2376 static enum port __dvo_port_to_port(int n_ports, int n_dvo,
2377 				    const int port_mapping[][3], u8 dvo_port)
2378 {
2379 	enum port port;
2380 	int i;
2381 
2382 	for (port = PORT_A; port < n_ports; port++) {
2383 		for (i = 0; i < n_dvo; i++) {
2384 			if (port_mapping[port][i] == -1)
2385 				break;
2386 
2387 			if (dvo_port == port_mapping[port][i])
2388 				return port;
2389 		}
2390 	}
2391 
2392 	return PORT_NONE;
2393 }
2394 
2395 static enum port dvo_port_to_port(struct drm_i915_private *i915,
2396 				  u8 dvo_port)
2397 {
2398 	/*
2399 	 * Each DDI port can have more than one value on the "DVO Port" field,
2400 	 * so look for all the possible values for each port.
2401 	 */
2402 	static const int port_mapping[][3] = {
2403 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2404 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2405 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2406 		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2407 		[PORT_E] = { DVO_PORT_HDMIE, DVO_PORT_DPE, DVO_PORT_CRT },
2408 		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
2409 		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2410 		[PORT_H] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2411 		[PORT_I] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
2412 	};
2413 	/*
2414 	 * RKL VBT uses PHY based mapping. Combo PHYs A,B,C,D
2415 	 * map to DDI A,B,TC1,TC2 respectively.
2416 	 */
2417 	static const int rkl_port_mapping[][3] = {
2418 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2419 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2420 		[PORT_C] = { -1 },
2421 		[PORT_TC1] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2422 		[PORT_TC2] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2423 	};
2424 	/*
2425 	 * Alderlake S ports used in the driver are PORT_A, PORT_D, PORT_E,
2426 	 * PORT_F and PORT_G, we need to map that to correct VBT sections.
2427 	 */
2428 	static const int adls_port_mapping[][3] = {
2429 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2430 		[PORT_B] = { -1 },
2431 		[PORT_C] = { -1 },
2432 		[PORT_TC1] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2433 		[PORT_TC2] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2434 		[PORT_TC3] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2435 		[PORT_TC4] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
2436 	};
2437 	static const int xelpd_port_mapping[][3] = {
2438 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2439 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2440 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2441 		[PORT_D_XELPD] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2442 		[PORT_E_XELPD] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
2443 		[PORT_TC1] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
2444 		[PORT_TC2] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2445 		[PORT_TC3] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2446 		[PORT_TC4] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
2447 	};
2448 
2449 	if (DISPLAY_VER(i915) >= 13)
2450 		return __dvo_port_to_port(ARRAY_SIZE(xelpd_port_mapping),
2451 					  ARRAY_SIZE(xelpd_port_mapping[0]),
2452 					  xelpd_port_mapping,
2453 					  dvo_port);
2454 	else if (IS_ALDERLAKE_S(i915))
2455 		return __dvo_port_to_port(ARRAY_SIZE(adls_port_mapping),
2456 					  ARRAY_SIZE(adls_port_mapping[0]),
2457 					  adls_port_mapping,
2458 					  dvo_port);
2459 	else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
2460 		return __dvo_port_to_port(ARRAY_SIZE(rkl_port_mapping),
2461 					  ARRAY_SIZE(rkl_port_mapping[0]),
2462 					  rkl_port_mapping,
2463 					  dvo_port);
2464 	else
2465 		return __dvo_port_to_port(ARRAY_SIZE(port_mapping),
2466 					  ARRAY_SIZE(port_mapping[0]),
2467 					  port_mapping,
2468 					  dvo_port);
2469 }
2470 
2471 static enum port
2472 dsi_dvo_port_to_port(struct drm_i915_private *i915, u8 dvo_port)
2473 {
2474 	switch (dvo_port) {
2475 	case DVO_PORT_MIPIA:
2476 		return PORT_A;
2477 	case DVO_PORT_MIPIC:
2478 		if (DISPLAY_VER(i915) >= 11)
2479 			return PORT_B;
2480 		else
2481 			return PORT_C;
2482 	default:
2483 		return PORT_NONE;
2484 	}
2485 }
2486 
2487 static int parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)
2488 {
2489 	switch (vbt_max_link_rate) {
2490 	default:
2491 	case BDB_230_VBT_DP_MAX_LINK_RATE_DEF:
2492 		return 0;
2493 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR20:
2494 		return 2000000;
2495 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR13P5:
2496 		return 1350000;
2497 	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR10:
2498 		return 1000000;
2499 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR3:
2500 		return 810000;
2501 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR2:
2502 		return 540000;
2503 	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR:
2504 		return 270000;
2505 	case BDB_230_VBT_DP_MAX_LINK_RATE_LBR:
2506 		return 162000;
2507 	}
2508 }
2509 
2510 static int parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)
2511 {
2512 	switch (vbt_max_link_rate) {
2513 	default:
2514 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR3:
2515 		return 810000;
2516 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR2:
2517 		return 540000;
2518 	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR:
2519 		return 270000;
2520 	case BDB_216_VBT_DP_MAX_LINK_RATE_LBR:
2521 		return 162000;
2522 	}
2523 }
2524 
2525 static int _intel_bios_dp_max_link_rate(const struct intel_bios_encoder_data *devdata)
2526 {
2527 	if (!devdata || devdata->i915->display.vbt.version < 216)
2528 		return 0;
2529 
2530 	if (devdata->i915->display.vbt.version >= 230)
2531 		return parse_bdb_230_dp_max_link_rate(devdata->child.dp_max_link_rate);
2532 	else
2533 		return parse_bdb_216_dp_max_link_rate(devdata->child.dp_max_link_rate);
2534 }
2535 
2536 static int _intel_bios_dp_max_lane_count(const struct intel_bios_encoder_data *devdata)
2537 {
2538 	if (!devdata || devdata->i915->display.vbt.version < 244)
2539 		return 0;
2540 
2541 	return devdata->child.dp_max_lane_count + 1;
2542 }
2543 
2544 static void sanitize_device_type(struct intel_bios_encoder_data *devdata,
2545 				 enum port port)
2546 {
2547 	struct drm_i915_private *i915 = devdata->i915;
2548 	bool is_hdmi;
2549 
2550 	if (port != PORT_A || DISPLAY_VER(i915) >= 12)
2551 		return;
2552 
2553 	if (!intel_bios_encoder_supports_dvi(devdata))
2554 		return;
2555 
2556 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2557 
2558 	drm_dbg_kms(&i915->drm, "VBT claims port A supports DVI%s, ignoring\n",
2559 		    is_hdmi ? "/HDMI" : "");
2560 
2561 	devdata->child.device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
2562 	devdata->child.device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
2563 }
2564 
2565 static bool
2566 intel_bios_encoder_supports_crt(const struct intel_bios_encoder_data *devdata)
2567 {
2568 	return devdata->child.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
2569 }
2570 
2571 bool
2572 intel_bios_encoder_supports_dvi(const struct intel_bios_encoder_data *devdata)
2573 {
2574 	return devdata->child.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
2575 }
2576 
2577 bool
2578 intel_bios_encoder_supports_hdmi(const struct intel_bios_encoder_data *devdata)
2579 {
2580 	return intel_bios_encoder_supports_dvi(devdata) &&
2581 		(devdata->child.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
2582 }
2583 
2584 bool
2585 intel_bios_encoder_supports_dp(const struct intel_bios_encoder_data *devdata)
2586 {
2587 	return devdata->child.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2588 }
2589 
2590 static bool
2591 intel_bios_encoder_supports_edp(const struct intel_bios_encoder_data *devdata)
2592 {
2593 	return intel_bios_encoder_supports_dp(devdata) &&
2594 		devdata->child.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR;
2595 }
2596 
2597 static bool
2598 intel_bios_encoder_supports_dsi(const struct intel_bios_encoder_data *devdata)
2599 {
2600 	return devdata->child.device_type & DEVICE_TYPE_MIPI_OUTPUT;
2601 }
2602 
2603 static int _intel_bios_hdmi_level_shift(const struct intel_bios_encoder_data *devdata)
2604 {
2605 	if (!devdata || devdata->i915->display.vbt.version < 158)
2606 		return -1;
2607 
2608 	return devdata->child.hdmi_level_shifter_value;
2609 }
2610 
2611 static int _intel_bios_max_tmds_clock(const struct intel_bios_encoder_data *devdata)
2612 {
2613 	if (!devdata || devdata->i915->display.vbt.version < 204)
2614 		return 0;
2615 
2616 	switch (devdata->child.hdmi_max_data_rate) {
2617 	default:
2618 		MISSING_CASE(devdata->child.hdmi_max_data_rate);
2619 		fallthrough;
2620 	case HDMI_MAX_DATA_RATE_PLATFORM:
2621 		return 0;
2622 	case HDMI_MAX_DATA_RATE_594:
2623 		return 594000;
2624 	case HDMI_MAX_DATA_RATE_340:
2625 		return 340000;
2626 	case HDMI_MAX_DATA_RATE_300:
2627 		return 300000;
2628 	case HDMI_MAX_DATA_RATE_297:
2629 		return 297000;
2630 	case HDMI_MAX_DATA_RATE_165:
2631 		return 165000;
2632 	}
2633 }
2634 
2635 static bool is_port_valid(struct drm_i915_private *i915, enum port port)
2636 {
2637 	/*
2638 	 * On some ICL SKUs port F is not present, but broken VBTs mark
2639 	 * the port as present. Only try to initialize port F for the
2640 	 * SKUs that may actually have it.
2641 	 */
2642 	if (port == PORT_F && IS_ICELAKE(i915))
2643 		return IS_ICL_WITH_PORT_F(i915);
2644 
2645 	return true;
2646 }
2647 
2648 static void print_ddi_port(const struct intel_bios_encoder_data *devdata,
2649 			   enum port port)
2650 {
2651 	struct drm_i915_private *i915 = devdata->i915;
2652 	const struct child_device_config *child = &devdata->child;
2653 	bool is_dvi, is_hdmi, is_dp, is_edp, is_dsi, is_crt, supports_typec_usb, supports_tbt;
2654 	int dp_boost_level, dp_max_link_rate, hdmi_boost_level, hdmi_level_shift, max_tmds_clock;
2655 
2656 	is_dvi = intel_bios_encoder_supports_dvi(devdata);
2657 	is_dp = intel_bios_encoder_supports_dp(devdata);
2658 	is_crt = intel_bios_encoder_supports_crt(devdata);
2659 	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2660 	is_edp = intel_bios_encoder_supports_edp(devdata);
2661 	is_dsi = intel_bios_encoder_supports_dsi(devdata);
2662 
2663 	supports_typec_usb = intel_bios_encoder_supports_typec_usb(devdata);
2664 	supports_tbt = intel_bios_encoder_supports_tbt(devdata);
2665 
2666 	drm_dbg_kms(&i915->drm,
2667 		    "Port %c VBT info: CRT:%d DVI:%d HDMI:%d DP:%d eDP:%d DSI:%d LSPCON:%d USB-Type-C:%d TBT:%d DSC:%d\n",
2668 		    port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp, is_dsi,
2669 		    HAS_LSPCON(i915) && child->lspcon,
2670 		    supports_typec_usb, supports_tbt,
2671 		    devdata->dsc != NULL);
2672 
2673 	hdmi_level_shift = _intel_bios_hdmi_level_shift(devdata);
2674 	if (hdmi_level_shift >= 0) {
2675 		drm_dbg_kms(&i915->drm,
2676 			    "Port %c VBT HDMI level shift: %d\n",
2677 			    port_name(port), hdmi_level_shift);
2678 	}
2679 
2680 	max_tmds_clock = _intel_bios_max_tmds_clock(devdata);
2681 	if (max_tmds_clock)
2682 		drm_dbg_kms(&i915->drm,
2683 			    "Port %c VBT HDMI max TMDS clock: %d kHz\n",
2684 			    port_name(port), max_tmds_clock);
2685 
2686 	/* I_boost config for SKL and above */
2687 	dp_boost_level = intel_bios_encoder_dp_boost_level(devdata);
2688 	if (dp_boost_level)
2689 		drm_dbg_kms(&i915->drm,
2690 			    "Port %c VBT (e)DP boost level: %d\n",
2691 			    port_name(port), dp_boost_level);
2692 
2693 	hdmi_boost_level = intel_bios_encoder_hdmi_boost_level(devdata);
2694 	if (hdmi_boost_level)
2695 		drm_dbg_kms(&i915->drm,
2696 			    "Port %c VBT HDMI boost level: %d\n",
2697 			    port_name(port), hdmi_boost_level);
2698 
2699 	dp_max_link_rate = _intel_bios_dp_max_link_rate(devdata);
2700 	if (dp_max_link_rate)
2701 		drm_dbg_kms(&i915->drm,
2702 			    "Port %c VBT DP max link rate: %d\n",
2703 			    port_name(port), dp_max_link_rate);
2704 
2705 	/*
2706 	 * FIXME need to implement support for VBT
2707 	 * vswing/preemph tables should this ever trigger.
2708 	 */
2709 	drm_WARN(&i915->drm, child->use_vbt_vswing,
2710 		 "Port %c asks to use VBT vswing/preemph tables\n",
2711 		 port_name(port));
2712 }
2713 
2714 static void parse_ddi_port(struct intel_bios_encoder_data *devdata)
2715 {
2716 	struct drm_i915_private *i915 = devdata->i915;
2717 	const struct child_device_config *child = &devdata->child;
2718 	enum port port;
2719 
2720 	port = dvo_port_to_port(i915, child->dvo_port);
2721 	if (port == PORT_NONE && DISPLAY_VER(i915) >= 11)
2722 		port = dsi_dvo_port_to_port(i915, child->dvo_port);
2723 	if (port == PORT_NONE)
2724 		return;
2725 
2726 	if (!is_port_valid(i915, port)) {
2727 		drm_dbg_kms(&i915->drm,
2728 			    "VBT reports port %c as supported, but that can't be true: skipping\n",
2729 			    port_name(port));
2730 		return;
2731 	}
2732 
2733 	if (i915->display.vbt.ports[port]) {
2734 		drm_dbg_kms(&i915->drm,
2735 			    "More than one child device for port %c in VBT, using the first.\n",
2736 			    port_name(port));
2737 		return;
2738 	}
2739 
2740 	sanitize_device_type(devdata, port);
2741 
2742 	if (intel_bios_encoder_supports_dvi(devdata))
2743 		sanitize_ddc_pin(devdata, port);
2744 
2745 	if (intel_bios_encoder_supports_dp(devdata))
2746 		sanitize_aux_ch(devdata, port);
2747 
2748 	i915->display.vbt.ports[port] = devdata;
2749 }
2750 
2751 static bool has_ddi_port_info(struct drm_i915_private *i915)
2752 {
2753 	return DISPLAY_VER(i915) >= 5 || IS_G4X(i915);
2754 }
2755 
2756 static void parse_ddi_ports(struct drm_i915_private *i915)
2757 {
2758 	struct intel_bios_encoder_data *devdata;
2759 	enum port port;
2760 
2761 	if (!has_ddi_port_info(i915))
2762 		return;
2763 
2764 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
2765 		parse_ddi_port(devdata);
2766 
2767 	for_each_port(port) {
2768 		if (i915->display.vbt.ports[port])
2769 			print_ddi_port(i915->display.vbt.ports[port], port);
2770 	}
2771 }
2772 
2773 static void
2774 parse_general_definitions(struct drm_i915_private *i915)
2775 {
2776 	const struct bdb_general_definitions *defs;
2777 	struct intel_bios_encoder_data *devdata;
2778 	const struct child_device_config *child;
2779 	int i, child_device_num;
2780 	u8 expected_size;
2781 	u16 block_size;
2782 	int bus_pin;
2783 
2784 	defs = find_section(i915, BDB_GENERAL_DEFINITIONS);
2785 	if (!defs) {
2786 		drm_dbg_kms(&i915->drm,
2787 			    "No general definition block is found, no devices defined.\n");
2788 		return;
2789 	}
2790 
2791 	block_size = get_blocksize(defs);
2792 	if (block_size < sizeof(*defs)) {
2793 		drm_dbg_kms(&i915->drm,
2794 			    "General definitions block too small (%u)\n",
2795 			    block_size);
2796 		return;
2797 	}
2798 
2799 	bus_pin = defs->crt_ddc_gmbus_pin;
2800 	drm_dbg_kms(&i915->drm, "crt_ddc_bus_pin: %d\n", bus_pin);
2801 	if (intel_gmbus_is_valid_pin(i915, bus_pin))
2802 		i915->display.vbt.crt_ddc_pin = bus_pin;
2803 
2804 	if (i915->display.vbt.version < 106) {
2805 		expected_size = 22;
2806 	} else if (i915->display.vbt.version < 111) {
2807 		expected_size = 27;
2808 	} else if (i915->display.vbt.version < 195) {
2809 		expected_size = LEGACY_CHILD_DEVICE_CONFIG_SIZE;
2810 	} else if (i915->display.vbt.version == 195) {
2811 		expected_size = 37;
2812 	} else if (i915->display.vbt.version <= 215) {
2813 		expected_size = 38;
2814 	} else if (i915->display.vbt.version <= 237) {
2815 		expected_size = 39;
2816 	} else {
2817 		expected_size = sizeof(*child);
2818 		BUILD_BUG_ON(sizeof(*child) < 39);
2819 		drm_dbg(&i915->drm,
2820 			"Expected child device config size for VBT version %u not known; assuming %u\n",
2821 			i915->display.vbt.version, expected_size);
2822 	}
2823 
2824 	/* Flag an error for unexpected size, but continue anyway. */
2825 	if (defs->child_dev_size != expected_size)
2826 		drm_err(&i915->drm,
2827 			"Unexpected child device config size %u (expected %u for VBT version %u)\n",
2828 			defs->child_dev_size, expected_size, i915->display.vbt.version);
2829 
2830 	/* The legacy sized child device config is the minimum we need. */
2831 	if (defs->child_dev_size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
2832 		drm_dbg_kms(&i915->drm,
2833 			    "Child device config size %u is too small.\n",
2834 			    defs->child_dev_size);
2835 		return;
2836 	}
2837 
2838 	/* get the number of child device */
2839 	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
2840 
2841 	for (i = 0; i < child_device_num; i++) {
2842 		child = child_device_ptr(defs, i);
2843 		if (!child->device_type)
2844 			continue;
2845 
2846 		drm_dbg_kms(&i915->drm,
2847 			    "Found VBT child device with type 0x%x\n",
2848 			    child->device_type);
2849 
2850 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
2851 		if (!devdata)
2852 			break;
2853 
2854 		devdata->i915 = i915;
2855 
2856 		/*
2857 		 * Copy as much as we know (sizeof) and is available
2858 		 * (child_dev_size) of the child device config. Accessing the
2859 		 * data must depend on VBT version.
2860 		 */
2861 		memcpy(&devdata->child, child,
2862 		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
2863 
2864 		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
2865 	}
2866 
2867 	if (list_empty(&i915->display.vbt.display_devices))
2868 		drm_dbg_kms(&i915->drm,
2869 			    "no child dev is parsed from VBT\n");
2870 }
2871 
2872 /* Common defaults which may be overridden by VBT. */
2873 static void
2874 init_vbt_defaults(struct drm_i915_private *i915)
2875 {
2876 	i915->display.vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
2877 
2878 	/* general features */
2879 	i915->display.vbt.int_tv_support = 1;
2880 	i915->display.vbt.int_crt_support = 1;
2881 
2882 	/* driver features */
2883 	i915->display.vbt.int_lvds_support = 1;
2884 
2885 	/* Default to using SSC */
2886 	i915->display.vbt.lvds_use_ssc = 1;
2887 	/*
2888 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
2889 	 * clock for LVDS.
2890 	 */
2891 	i915->display.vbt.lvds_ssc_freq = intel_bios_ssc_frequency(i915,
2892 								   !HAS_PCH_SPLIT(i915));
2893 	drm_dbg_kms(&i915->drm, "Set default to SSC at %d kHz\n",
2894 		    i915->display.vbt.lvds_ssc_freq);
2895 }
2896 
2897 /* Common defaults which may be overridden by VBT. */
2898 static void
2899 init_vbt_panel_defaults(struct intel_panel *panel)
2900 {
2901 	/* Default to having backlight */
2902 	panel->vbt.backlight.present = true;
2903 
2904 	/* LFP panel data */
2905 	panel->vbt.lvds_dither = true;
2906 }
2907 
2908 /* Defaults to initialize only if there is no VBT. */
2909 static void
2910 init_vbt_missing_defaults(struct drm_i915_private *i915)
2911 {
2912 	enum port port;
2913 	int ports = BIT(PORT_A) | BIT(PORT_B) | BIT(PORT_C) |
2914 		    BIT(PORT_D) | BIT(PORT_E) | BIT(PORT_F);
2915 
2916 	if (!HAS_DDI(i915) && !IS_CHERRYVIEW(i915))
2917 		return;
2918 
2919 	for_each_port_masked(port, ports) {
2920 		struct intel_bios_encoder_data *devdata;
2921 		struct child_device_config *child;
2922 		enum phy phy = intel_port_to_phy(i915, port);
2923 
2924 		/*
2925 		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
2926 		 * to detect it.
2927 		 */
2928 		if (intel_phy_is_tc(i915, phy))
2929 			continue;
2930 
2931 		/* Create fake child device config */
2932 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
2933 		if (!devdata)
2934 			break;
2935 
2936 		devdata->i915 = i915;
2937 		child = &devdata->child;
2938 
2939 		if (port == PORT_F)
2940 			child->dvo_port = DVO_PORT_HDMIF;
2941 		else if (port == PORT_E)
2942 			child->dvo_port = DVO_PORT_HDMIE;
2943 		else
2944 			child->dvo_port = DVO_PORT_HDMIA + port;
2945 
2946 		if (port != PORT_A && port != PORT_E)
2947 			child->device_type |= DEVICE_TYPE_TMDS_DVI_SIGNALING;
2948 
2949 		if (port != PORT_E)
2950 			child->device_type |= DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2951 
2952 		if (port == PORT_A)
2953 			child->device_type |= DEVICE_TYPE_INTERNAL_CONNECTOR;
2954 
2955 		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
2956 
2957 		drm_dbg_kms(&i915->drm,
2958 			    "Generating default VBT child device with type 0x04%x on port %c\n",
2959 			    child->device_type, port_name(port));
2960 	}
2961 
2962 	/* Bypass some minimum baseline VBT version checks */
2963 	i915->display.vbt.version = 155;
2964 }
2965 
2966 static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
2967 {
2968 	const void *_vbt = vbt;
2969 
2970 	return _vbt + vbt->bdb_offset;
2971 }
2972 
2973 /**
2974  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
2975  * @buf:	pointer to a buffer to validate
2976  * @size:	size of the buffer
2977  *
2978  * Returns true on valid VBT.
2979  */
2980 bool intel_bios_is_valid_vbt(const void *buf, size_t size)
2981 {
2982 	const struct vbt_header *vbt = buf;
2983 	const struct bdb_header *bdb;
2984 
2985 	if (!vbt)
2986 		return false;
2987 
2988 	if (sizeof(struct vbt_header) > size) {
2989 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
2990 		return false;
2991 	}
2992 
2993 	if (memcmp(vbt->signature, "$VBT", 4)) {
2994 		DRM_DEBUG_DRIVER("VBT invalid signature\n");
2995 		return false;
2996 	}
2997 
2998 	if (vbt->vbt_size > size) {
2999 		DRM_DEBUG_DRIVER("VBT incomplete (vbt_size overflows)\n");
3000 		return false;
3001 	}
3002 
3003 	size = vbt->vbt_size;
3004 
3005 	if (range_overflows_t(size_t,
3006 			      vbt->bdb_offset,
3007 			      sizeof(struct bdb_header),
3008 			      size)) {
3009 		DRM_DEBUG_DRIVER("BDB header incomplete\n");
3010 		return false;
3011 	}
3012 
3013 	bdb = get_bdb_header(vbt);
3014 	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
3015 		DRM_DEBUG_DRIVER("BDB incomplete\n");
3016 		return false;
3017 	}
3018 
3019 	return vbt;
3020 }
3021 
3022 static struct vbt_header *spi_oprom_get_vbt(struct drm_i915_private *i915)
3023 {
3024 	u32 count, data, found, store = 0;
3025 	u32 static_region, oprom_offset;
3026 	u32 oprom_size = 0x200000;
3027 	u16 vbt_size;
3028 	u32 *vbt;
3029 
3030 	static_region = intel_uncore_read(&i915->uncore, SPI_STATIC_REGIONS);
3031 	static_region &= OPTIONROM_SPI_REGIONID_MASK;
3032 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_REGIONID, static_region);
3033 
3034 	oprom_offset = intel_uncore_read(&i915->uncore, OROM_OFFSET);
3035 	oprom_offset &= OROM_OFFSET_MASK;
3036 
3037 	for (count = 0; count < oprom_size; count += 4) {
3038 		intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, oprom_offset + count);
3039 		data = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
3040 
3041 		if (data == *((const u32 *)"$VBT")) {
3042 			found = oprom_offset + count;
3043 			break;
3044 		}
3045 	}
3046 
3047 	if (count >= oprom_size)
3048 		goto err_not_found;
3049 
3050 	/* Get VBT size and allocate space for the VBT */
3051 	intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, found +
3052 		   offsetof(struct vbt_header, vbt_size));
3053 	vbt_size = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
3054 	vbt_size &= 0xffff;
3055 
3056 	vbt = kzalloc(round_up(vbt_size, 4), GFP_KERNEL);
3057 	if (!vbt)
3058 		goto err_not_found;
3059 
3060 	for (count = 0; count < vbt_size; count += 4) {
3061 		intel_uncore_write(&i915->uncore, PRIMARY_SPI_ADDRESS, found + count);
3062 		data = intel_uncore_read(&i915->uncore, PRIMARY_SPI_TRIGGER);
3063 		*(vbt + store++) = data;
3064 	}
3065 
3066 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
3067 		goto err_free_vbt;
3068 
3069 	drm_dbg_kms(&i915->drm, "Found valid VBT in SPI flash\n");
3070 
3071 	return (struct vbt_header *)vbt;
3072 
3073 err_free_vbt:
3074 	kfree(vbt);
3075 err_not_found:
3076 	return NULL;
3077 }
3078 
3079 static struct vbt_header *oprom_get_vbt(struct drm_i915_private *i915)
3080 {
3081 	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
3082 	void __iomem *p = NULL, *oprom;
3083 	struct vbt_header *vbt;
3084 	u16 vbt_size;
3085 	size_t i, size;
3086 
3087 	oprom = pci_map_rom(pdev, &size);
3088 	if (!oprom)
3089 		return NULL;
3090 
3091 	/* Scour memory looking for the VBT signature. */
3092 	for (i = 0; i + 4 < size; i += 4) {
3093 		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
3094 			continue;
3095 
3096 		p = oprom + i;
3097 		size -= i;
3098 		break;
3099 	}
3100 
3101 	if (!p)
3102 		goto err_unmap_oprom;
3103 
3104 	if (sizeof(struct vbt_header) > size) {
3105 		drm_dbg(&i915->drm, "VBT header incomplete\n");
3106 		goto err_unmap_oprom;
3107 	}
3108 
3109 	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
3110 	if (vbt_size > size) {
3111 		drm_dbg(&i915->drm,
3112 			"VBT incomplete (vbt_size overflows)\n");
3113 		goto err_unmap_oprom;
3114 	}
3115 
3116 	/* The rest will be validated by intel_bios_is_valid_vbt() */
3117 	vbt = kmalloc(vbt_size, GFP_KERNEL);
3118 	if (!vbt)
3119 		goto err_unmap_oprom;
3120 
3121 	memcpy_fromio(vbt, p, vbt_size);
3122 
3123 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
3124 		goto err_free_vbt;
3125 
3126 	pci_unmap_rom(pdev, oprom);
3127 
3128 	drm_dbg_kms(&i915->drm, "Found valid VBT in PCI ROM\n");
3129 
3130 	return vbt;
3131 
3132 err_free_vbt:
3133 	kfree(vbt);
3134 err_unmap_oprom:
3135 	pci_unmap_rom(pdev, oprom);
3136 
3137 	return NULL;
3138 }
3139 
3140 /**
3141  * intel_bios_init - find VBT and initialize settings from the BIOS
3142  * @i915: i915 device instance
3143  *
3144  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
3145  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
3146  * initialize some defaults if the VBT is not present at all.
3147  */
3148 void intel_bios_init(struct drm_i915_private *i915)
3149 {
3150 	const struct vbt_header *vbt = i915->display.opregion.vbt;
3151 	struct vbt_header *oprom_vbt = NULL;
3152 	const struct bdb_header *bdb;
3153 
3154 	INIT_LIST_HEAD(&i915->display.vbt.display_devices);
3155 	INIT_LIST_HEAD(&i915->display.vbt.bdb_blocks);
3156 
3157 	if (!HAS_DISPLAY(i915)) {
3158 		drm_dbg_kms(&i915->drm,
3159 			    "Skipping VBT init due to disabled display.\n");
3160 		return;
3161 	}
3162 
3163 	init_vbt_defaults(i915);
3164 
3165 	/*
3166 	 * If the OpRegion does not have VBT, look in SPI flash through MMIO or
3167 	 * PCI mapping
3168 	 */
3169 	if (!vbt && IS_DGFX(i915)) {
3170 		oprom_vbt = spi_oprom_get_vbt(i915);
3171 		vbt = oprom_vbt;
3172 	}
3173 
3174 	if (!vbt) {
3175 		oprom_vbt = oprom_get_vbt(i915);
3176 		vbt = oprom_vbt;
3177 	}
3178 
3179 	if (!vbt)
3180 		goto out;
3181 
3182 	bdb = get_bdb_header(vbt);
3183 	i915->display.vbt.version = bdb->version;
3184 
3185 	drm_dbg_kms(&i915->drm,
3186 		    "VBT signature \"%.*s\", BDB version %d\n",
3187 		    (int)sizeof(vbt->signature), vbt->signature, i915->display.vbt.version);
3188 
3189 	init_bdb_blocks(i915, bdb);
3190 
3191 	/* Grab useful general definitions */
3192 	parse_general_features(i915);
3193 	parse_general_definitions(i915);
3194 	parse_driver_features(i915);
3195 
3196 	/* Depends on child device list */
3197 	parse_compression_parameters(i915);
3198 
3199 out:
3200 	if (!vbt) {
3201 		drm_info(&i915->drm,
3202 			 "Failed to find VBIOS tables (VBT)\n");
3203 		init_vbt_missing_defaults(i915);
3204 	}
3205 
3206 	/* Further processing on pre-parsed or generated child device data */
3207 	parse_sdvo_device_mapping(i915);
3208 	parse_ddi_ports(i915);
3209 
3210 	kfree(oprom_vbt);
3211 }
3212 
3213 static void intel_bios_init_panel(struct drm_i915_private *i915,
3214 				  struct intel_panel *panel,
3215 				  const struct intel_bios_encoder_data *devdata,
3216 				  const struct drm_edid *drm_edid,
3217 				  bool use_fallback)
3218 {
3219 	/* already have it? */
3220 	if (panel->vbt.panel_type >= 0) {
3221 		drm_WARN_ON(&i915->drm, !use_fallback);
3222 		return;
3223 	}
3224 
3225 	panel->vbt.panel_type = get_panel_type(i915, devdata,
3226 					       drm_edid, use_fallback);
3227 	if (panel->vbt.panel_type < 0) {
3228 		drm_WARN_ON(&i915->drm, use_fallback);
3229 		return;
3230 	}
3231 
3232 	init_vbt_panel_defaults(panel);
3233 
3234 	parse_panel_options(i915, panel);
3235 	parse_generic_dtd(i915, panel);
3236 	parse_lfp_data(i915, panel);
3237 	parse_lfp_backlight(i915, panel);
3238 	parse_sdvo_panel_data(i915, panel);
3239 	parse_panel_driver_features(i915, panel);
3240 	parse_power_conservation_features(i915, panel);
3241 	parse_edp(i915, panel);
3242 	parse_psr(i915, panel);
3243 	parse_mipi_config(i915, panel);
3244 	parse_mipi_sequence(i915, panel);
3245 }
3246 
3247 void intel_bios_init_panel_early(struct drm_i915_private *i915,
3248 				 struct intel_panel *panel,
3249 				 const struct intel_bios_encoder_data *devdata)
3250 {
3251 	intel_bios_init_panel(i915, panel, devdata, NULL, false);
3252 }
3253 
3254 void intel_bios_init_panel_late(struct drm_i915_private *i915,
3255 				struct intel_panel *panel,
3256 				const struct intel_bios_encoder_data *devdata,
3257 				const struct drm_edid *drm_edid)
3258 {
3259 	intel_bios_init_panel(i915, panel, devdata, drm_edid, true);
3260 }
3261 
3262 /**
3263  * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
3264  * @i915: i915 device instance
3265  */
3266 void intel_bios_driver_remove(struct drm_i915_private *i915)
3267 {
3268 	struct intel_bios_encoder_data *devdata, *nd;
3269 	struct bdb_block_entry *entry, *ne;
3270 
3271 	list_for_each_entry_safe(devdata, nd, &i915->display.vbt.display_devices, node) {
3272 		list_del(&devdata->node);
3273 		kfree(devdata->dsc);
3274 		kfree(devdata);
3275 	}
3276 
3277 	list_for_each_entry_safe(entry, ne, &i915->display.vbt.bdb_blocks, node) {
3278 		list_del(&entry->node);
3279 		kfree(entry);
3280 	}
3281 }
3282 
3283 void intel_bios_fini_panel(struct intel_panel *panel)
3284 {
3285 	kfree(panel->vbt.sdvo_lvds_vbt_mode);
3286 	panel->vbt.sdvo_lvds_vbt_mode = NULL;
3287 	kfree(panel->vbt.lfp_lvds_vbt_mode);
3288 	panel->vbt.lfp_lvds_vbt_mode = NULL;
3289 	kfree(panel->vbt.dsi.data);
3290 	panel->vbt.dsi.data = NULL;
3291 	kfree(panel->vbt.dsi.pps);
3292 	panel->vbt.dsi.pps = NULL;
3293 	kfree(panel->vbt.dsi.config);
3294 	panel->vbt.dsi.config = NULL;
3295 	kfree(panel->vbt.dsi.deassert_seq);
3296 	panel->vbt.dsi.deassert_seq = NULL;
3297 }
3298 
3299 /**
3300  * intel_bios_is_tv_present - is integrated TV present in VBT
3301  * @i915: i915 device instance
3302  *
3303  * Return true if TV is present. If no child devices were parsed from VBT,
3304  * assume TV is present.
3305  */
3306 bool intel_bios_is_tv_present(struct drm_i915_private *i915)
3307 {
3308 	const struct intel_bios_encoder_data *devdata;
3309 	const struct child_device_config *child;
3310 
3311 	if (!i915->display.vbt.int_tv_support)
3312 		return false;
3313 
3314 	if (list_empty(&i915->display.vbt.display_devices))
3315 		return true;
3316 
3317 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3318 		child = &devdata->child;
3319 
3320 		/*
3321 		 * If the device type is not TV, continue.
3322 		 */
3323 		switch (child->device_type) {
3324 		case DEVICE_TYPE_INT_TV:
3325 		case DEVICE_TYPE_TV:
3326 		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
3327 			break;
3328 		default:
3329 			continue;
3330 		}
3331 		/* Only when the addin_offset is non-zero, it is regarded
3332 		 * as present.
3333 		 */
3334 		if (child->addin_offset)
3335 			return true;
3336 	}
3337 
3338 	return false;
3339 }
3340 
3341 /**
3342  * intel_bios_is_lvds_present - is LVDS present in VBT
3343  * @i915:	i915 device instance
3344  * @i2c_pin:	i2c pin for LVDS if present
3345  *
3346  * Return true if LVDS is present. If no child devices were parsed from VBT,
3347  * assume LVDS is present.
3348  */
3349 bool intel_bios_is_lvds_present(struct drm_i915_private *i915, u8 *i2c_pin)
3350 {
3351 	const struct intel_bios_encoder_data *devdata;
3352 	const struct child_device_config *child;
3353 
3354 	if (list_empty(&i915->display.vbt.display_devices))
3355 		return true;
3356 
3357 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3358 		child = &devdata->child;
3359 
3360 		/* If the device type is not LFP, continue.
3361 		 * We have to check both the new identifiers as well as the
3362 		 * old for compatibility with some BIOSes.
3363 		 */
3364 		if (child->device_type != DEVICE_TYPE_INT_LFP &&
3365 		    child->device_type != DEVICE_TYPE_LFP)
3366 			continue;
3367 
3368 		if (intel_gmbus_is_valid_pin(i915, child->i2c_pin))
3369 			*i2c_pin = child->i2c_pin;
3370 
3371 		/* However, we cannot trust the BIOS writers to populate
3372 		 * the VBT correctly.  Since LVDS requires additional
3373 		 * information from AIM blocks, a non-zero addin offset is
3374 		 * a good indicator that the LVDS is actually present.
3375 		 */
3376 		if (child->addin_offset)
3377 			return true;
3378 
3379 		/* But even then some BIOS writers perform some black magic
3380 		 * and instantiate the device without reference to any
3381 		 * additional data.  Trust that if the VBT was written into
3382 		 * the OpRegion then they have validated the LVDS's existence.
3383 		 */
3384 		if (i915->display.opregion.vbt)
3385 			return true;
3386 	}
3387 
3388 	return false;
3389 }
3390 
3391 /**
3392  * intel_bios_is_port_present - is the specified digital port present
3393  * @i915:	i915 device instance
3394  * @port:	port to check
3395  *
3396  * Return true if the device in %port is present.
3397  */
3398 bool intel_bios_is_port_present(struct drm_i915_private *i915, enum port port)
3399 {
3400 	if (WARN_ON(!has_ddi_port_info(i915)))
3401 		return true;
3402 
3403 	return i915->display.vbt.ports[port];
3404 }
3405 
3406 /**
3407  * intel_bios_is_port_edp - is the device in given port eDP
3408  * @i915:	i915 device instance
3409  * @port:	port to check
3410  *
3411  * Return true if the device in %port is eDP.
3412  */
3413 bool intel_bios_is_port_edp(struct drm_i915_private *i915, enum port port)
3414 {
3415 	const struct intel_bios_encoder_data *devdata =
3416 		intel_bios_encoder_data_lookup(i915, port);
3417 
3418 	return devdata && intel_bios_encoder_supports_edp(devdata);
3419 }
3420 
3421 static bool intel_bios_encoder_supports_dp_dual_mode(const struct intel_bios_encoder_data *devdata)
3422 {
3423 	const struct child_device_config *child = &devdata->child;
3424 
3425 	if (!intel_bios_encoder_supports_dp(devdata) ||
3426 	    !intel_bios_encoder_supports_hdmi(devdata))
3427 		return false;
3428 
3429 	if (dvo_port_type(child->dvo_port) == DVO_PORT_DPA)
3430 		return true;
3431 
3432 	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
3433 	if (dvo_port_type(child->dvo_port) == DVO_PORT_HDMIA &&
3434 	    child->aux_channel != 0)
3435 		return true;
3436 
3437 	return false;
3438 }
3439 
3440 bool intel_bios_is_port_dp_dual_mode(struct drm_i915_private *i915,
3441 				     enum port port)
3442 {
3443 	const struct intel_bios_encoder_data *devdata =
3444 		intel_bios_encoder_data_lookup(i915, port);
3445 
3446 	return devdata && intel_bios_encoder_supports_dp_dual_mode(devdata);
3447 }
3448 
3449 /**
3450  * intel_bios_is_dsi_present - is DSI present in VBT
3451  * @i915:	i915 device instance
3452  * @port:	port for DSI if present
3453  *
3454  * Return true if DSI is present, and return the port in %port.
3455  */
3456 bool intel_bios_is_dsi_present(struct drm_i915_private *i915,
3457 			       enum port *port)
3458 {
3459 	const struct intel_bios_encoder_data *devdata;
3460 	const struct child_device_config *child;
3461 	u8 dvo_port;
3462 
3463 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3464 		child = &devdata->child;
3465 
3466 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
3467 			continue;
3468 
3469 		dvo_port = child->dvo_port;
3470 
3471 		if (dsi_dvo_port_to_port(i915, dvo_port) == PORT_NONE) {
3472 			drm_dbg_kms(&i915->drm,
3473 				    "VBT has unsupported DSI port %c\n",
3474 				    port_name(dvo_port - DVO_PORT_MIPIA));
3475 			continue;
3476 		}
3477 
3478 		if (port)
3479 			*port = dsi_dvo_port_to_port(i915, dvo_port);
3480 		return true;
3481 	}
3482 
3483 	return false;
3484 }
3485 
3486 static void fill_dsc(struct intel_crtc_state *crtc_state,
3487 		     struct dsc_compression_parameters_entry *dsc,
3488 		     int dsc_max_bpc)
3489 {
3490 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
3491 	int bpc = 8;
3492 
3493 	vdsc_cfg->dsc_version_major = dsc->version_major;
3494 	vdsc_cfg->dsc_version_minor = dsc->version_minor;
3495 
3496 	if (dsc->support_12bpc && dsc_max_bpc >= 12)
3497 		bpc = 12;
3498 	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
3499 		bpc = 10;
3500 	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
3501 		bpc = 8;
3502 	else
3503 		DRM_DEBUG_KMS("VBT: Unsupported BPC %d for DCS\n",
3504 			      dsc_max_bpc);
3505 
3506 	crtc_state->pipe_bpp = bpc * 3;
3507 
3508 	crtc_state->dsc.compressed_bpp = min(crtc_state->pipe_bpp,
3509 					     VBT_DSC_MAX_BPP(dsc->max_bpp));
3510 
3511 	/*
3512 	 * FIXME: This is ugly, and slice count should take DSC engine
3513 	 * throughput etc. into account.
3514 	 *
3515 	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
3516 	 */
3517 	if (dsc->slices_per_line & BIT(2)) {
3518 		crtc_state->dsc.slice_count = 4;
3519 	} else if (dsc->slices_per_line & BIT(1)) {
3520 		crtc_state->dsc.slice_count = 2;
3521 	} else {
3522 		/* FIXME */
3523 		if (!(dsc->slices_per_line & BIT(0)))
3524 			DRM_DEBUG_KMS("VBT: Unsupported DSC slice count for DSI\n");
3525 
3526 		crtc_state->dsc.slice_count = 1;
3527 	}
3528 
3529 	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
3530 	    crtc_state->dsc.slice_count != 0)
3531 		DRM_DEBUG_KMS("VBT: DSC hdisplay %d not divisible by slice count %d\n",
3532 			      crtc_state->hw.adjusted_mode.crtc_hdisplay,
3533 			      crtc_state->dsc.slice_count);
3534 
3535 	/*
3536 	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
3537 	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63.
3538 	 */
3539 	vdsc_cfg->rc_model_size = drm_dsc_dp_rc_buffer_size(dsc->rc_buffer_block_size,
3540 							    dsc->rc_buffer_size);
3541 
3542 	/* FIXME: DSI spec says bpc + 1 for this one */
3543 	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
3544 
3545 	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
3546 
3547 	vdsc_cfg->slice_height = dsc->slice_height;
3548 }
3549 
3550 /* FIXME: initially DSI specific */
3551 bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
3552 			       struct intel_crtc_state *crtc_state,
3553 			       int dsc_max_bpc)
3554 {
3555 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3556 	const struct intel_bios_encoder_data *devdata;
3557 	const struct child_device_config *child;
3558 
3559 	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3560 		child = &devdata->child;
3561 
3562 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
3563 			continue;
3564 
3565 		if (dsi_dvo_port_to_port(i915, child->dvo_port) == encoder->port) {
3566 			if (!devdata->dsc)
3567 				return false;
3568 
3569 			if (crtc_state)
3570 				fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
3571 
3572 			return true;
3573 		}
3574 	}
3575 
3576 	return false;
3577 }
3578 
3579 /**
3580  * intel_bios_is_port_hpd_inverted - is HPD inverted for %port
3581  * @i915:	i915 device instance
3582  * @port:	port to check
3583  *
3584  * Return true if HPD should be inverted for %port.
3585  */
3586 bool
3587 intel_bios_is_port_hpd_inverted(const struct drm_i915_private *i915,
3588 				enum port port)
3589 {
3590 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[port];
3591 
3592 	if (drm_WARN_ON_ONCE(&i915->drm,
3593 			     !IS_GEMINILAKE(i915) && !IS_BROXTON(i915)))
3594 		return false;
3595 
3596 	return devdata && devdata->child.hpd_invert;
3597 }
3598 
3599 /**
3600  * intel_bios_is_lspcon_present - if LSPCON is attached on %port
3601  * @i915:	i915 device instance
3602  * @port:	port to check
3603  *
3604  * Return true if LSPCON is present on this port
3605  */
3606 bool
3607 intel_bios_is_lspcon_present(const struct drm_i915_private *i915,
3608 			     enum port port)
3609 {
3610 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[port];
3611 
3612 	return HAS_LSPCON(i915) && devdata && devdata->child.lspcon;
3613 }
3614 
3615 /**
3616  * intel_bios_is_lane_reversal_needed - if lane reversal needed on port
3617  * @i915:       i915 device instance
3618  * @port:       port to check
3619  *
3620  * Return true if port requires lane reversal
3621  */
3622 bool
3623 intel_bios_is_lane_reversal_needed(const struct drm_i915_private *i915,
3624 				   enum port port)
3625 {
3626 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[port];
3627 
3628 	return devdata && devdata->child.lane_reversal;
3629 }
3630 
3631 enum aux_ch intel_bios_port_aux_ch(struct drm_i915_private *i915,
3632 				   enum port port)
3633 {
3634 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[port];
3635 	enum aux_ch aux_ch;
3636 
3637 	if (!devdata || !devdata->child.aux_channel) {
3638 		aux_ch = (enum aux_ch)port;
3639 
3640 		drm_dbg_kms(&i915->drm,
3641 			    "using AUX %c for port %c (platform default)\n",
3642 			    aux_ch_name(aux_ch), port_name(port));
3643 		return aux_ch;
3644 	}
3645 
3646 	/*
3647 	 * RKL/DG1 VBT uses PHY based mapping. Combo PHYs A,B,C,D
3648 	 * map to DDI A,B,TC1,TC2 respectively.
3649 	 *
3650 	 * ADL-S VBT uses PHY based mapping. Combo PHYs A,B,C,D,E
3651 	 * map to DDI A,TC1,TC2,TC3,TC4 respectively.
3652 	 */
3653 	switch (devdata->child.aux_channel) {
3654 	case DP_AUX_A:
3655 		aux_ch = AUX_CH_A;
3656 		break;
3657 	case DP_AUX_B:
3658 		if (IS_ALDERLAKE_S(i915))
3659 			aux_ch = AUX_CH_USBC1;
3660 		else
3661 			aux_ch = AUX_CH_B;
3662 		break;
3663 	case DP_AUX_C:
3664 		if (IS_ALDERLAKE_S(i915))
3665 			aux_ch = AUX_CH_USBC2;
3666 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
3667 			aux_ch = AUX_CH_USBC1;
3668 		else
3669 			aux_ch = AUX_CH_C;
3670 		break;
3671 	case DP_AUX_D:
3672 		if (DISPLAY_VER(i915) >= 13)
3673 			aux_ch = AUX_CH_D_XELPD;
3674 		else if (IS_ALDERLAKE_S(i915))
3675 			aux_ch = AUX_CH_USBC3;
3676 		else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
3677 			aux_ch = AUX_CH_USBC2;
3678 		else
3679 			aux_ch = AUX_CH_D;
3680 		break;
3681 	case DP_AUX_E:
3682 		if (DISPLAY_VER(i915) >= 13)
3683 			aux_ch = AUX_CH_E_XELPD;
3684 		else if (IS_ALDERLAKE_S(i915))
3685 			aux_ch = AUX_CH_USBC4;
3686 		else
3687 			aux_ch = AUX_CH_E;
3688 		break;
3689 	case DP_AUX_F:
3690 		if (DISPLAY_VER(i915) >= 13)
3691 			aux_ch = AUX_CH_USBC1;
3692 		else
3693 			aux_ch = AUX_CH_F;
3694 		break;
3695 	case DP_AUX_G:
3696 		if (DISPLAY_VER(i915) >= 13)
3697 			aux_ch = AUX_CH_USBC2;
3698 		else
3699 			aux_ch = AUX_CH_G;
3700 		break;
3701 	case DP_AUX_H:
3702 		if (DISPLAY_VER(i915) >= 13)
3703 			aux_ch = AUX_CH_USBC3;
3704 		else
3705 			aux_ch = AUX_CH_H;
3706 		break;
3707 	case DP_AUX_I:
3708 		if (DISPLAY_VER(i915) >= 13)
3709 			aux_ch = AUX_CH_USBC4;
3710 		else
3711 			aux_ch = AUX_CH_I;
3712 		break;
3713 	default:
3714 		MISSING_CASE(devdata->child.aux_channel);
3715 		aux_ch = AUX_CH_A;
3716 		break;
3717 	}
3718 
3719 	drm_dbg_kms(&i915->drm, "using AUX %c for port %c (VBT)\n",
3720 		    aux_ch_name(aux_ch), port_name(port));
3721 
3722 	return aux_ch;
3723 }
3724 
3725 int intel_bios_max_tmds_clock(struct intel_encoder *encoder)
3726 {
3727 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3728 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[encoder->port];
3729 
3730 	return _intel_bios_max_tmds_clock(devdata);
3731 }
3732 
3733 /* This is an index in the HDMI/DVI DDI buffer translation table, or -1 */
3734 int intel_bios_hdmi_level_shift(struct intel_encoder *encoder)
3735 {
3736 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3737 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[encoder->port];
3738 
3739 	return _intel_bios_hdmi_level_shift(devdata);
3740 }
3741 
3742 int intel_bios_encoder_dp_boost_level(const struct intel_bios_encoder_data *devdata)
3743 {
3744 	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3745 		return 0;
3746 
3747 	return translate_iboost(devdata->child.dp_iboost_level);
3748 }
3749 
3750 int intel_bios_encoder_hdmi_boost_level(const struct intel_bios_encoder_data *devdata)
3751 {
3752 	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3753 		return 0;
3754 
3755 	return translate_iboost(devdata->child.hdmi_iboost_level);
3756 }
3757 
3758 int intel_bios_dp_max_link_rate(struct intel_encoder *encoder)
3759 {
3760 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3761 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[encoder->port];
3762 
3763 	return _intel_bios_dp_max_link_rate(devdata);
3764 }
3765 
3766 int intel_bios_dp_max_lane_count(struct intel_encoder *encoder)
3767 {
3768 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3769 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[encoder->port];
3770 
3771 	return _intel_bios_dp_max_lane_count(devdata);
3772 }
3773 
3774 int intel_bios_alternate_ddc_pin(struct intel_encoder *encoder)
3775 {
3776 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3777 	const struct intel_bios_encoder_data *devdata = i915->display.vbt.ports[encoder->port];
3778 
3779 	if (!devdata || !devdata->child.ddc_pin)
3780 		return 0;
3781 
3782 	return map_ddc_pin(i915, devdata->child.ddc_pin);
3783 }
3784 
3785 bool intel_bios_encoder_supports_typec_usb(const struct intel_bios_encoder_data *devdata)
3786 {
3787 	return devdata->i915->display.vbt.version >= 195 && devdata->child.dp_usb_type_c;
3788 }
3789 
3790 bool intel_bios_encoder_supports_tbt(const struct intel_bios_encoder_data *devdata)
3791 {
3792 	return devdata->i915->display.vbt.version >= 209 && devdata->child.tbt;
3793 }
3794 
3795 const struct intel_bios_encoder_data *
3796 intel_bios_encoder_data_lookup(struct drm_i915_private *i915, enum port port)
3797 {
3798 	return i915->display.vbt.ports[port];
3799 }
3800