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