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