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