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