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 */
66struct 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. */
78static 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. */
88static u32 get_blocksize(const void *block_data)
89{
90	return _get_blocksize(block_data - 3);
91}
92
93static const void *
94find_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 */
128static 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
139struct bdb_block_entry {
140	struct list_head node;
141	enum bdb_block_id section_id;
142	u8 data[];
143};
144
145static const void *
146bdb_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
159static 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
201static 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
218static 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 */
312static 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
341static 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
353static 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
361static 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
451static void
452init_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
509static 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
525static void
526fill_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
582static const struct lvds_dvo_timing *
583get_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
590static const struct lvds_fp_timing *
591get_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
598static const struct drm_edid_product_id *
599get_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
609static const struct bdb_lvds_lfp_data_tail *
610get_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
619static 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
626static 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
651static 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
701static 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
708enum panel_type {
709	PANEL_TYPE_OPREGION,
710	PANEL_TYPE_VBT,
711	PANEL_TYPE_PNPID,
712	PANEL_TYPE_FALLBACK,
713};
714
715static 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
774static 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
779static 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 */
785static void
786parse_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
832static void
833parse_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
873static void
874parse_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
917static void
918parse_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
1007static void
1008parse_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		const struct lfp_backlight_control_method *method;
1041
1042		method = &backlight_data->backlight_control[panel_type];
1043		panel->vbt.backlight.type = method->type;
1044		panel->vbt.backlight.controller = method->controller;
1045	}
1046
1047	panel->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
1048	panel->vbt.backlight.active_low_pwm = entry->active_low_pwm;
1049
1050	if (i915->display.vbt.version >= 234) {
1051		u16 min_level;
1052		bool scale;
1053
1054		level = backlight_data->brightness_level[panel_type].level;
1055		min_level = backlight_data->brightness_min_level[panel_type].level;
1056
1057		if (i915->display.vbt.version >= 236)
1058			scale = backlight_data->brightness_precision_bits[panel_type] == 16;
1059		else
1060			scale = level > 255;
1061
1062		if (scale)
1063			min_level = min_level / 255;
1064
1065		if (min_level > 255) {
1066			drm_warn(&i915->drm, "Brightness min level > 255\n");
1067			level = 255;
1068		}
1069		panel->vbt.backlight.min_brightness = min_level;
1070
1071		panel->vbt.backlight.brightness_precision_bits =
1072			backlight_data->brightness_precision_bits[panel_type];
1073	} else {
1074		level = backlight_data->level[panel_type];
1075		panel->vbt.backlight.min_brightness = entry->min_brightness;
1076	}
1077
1078	if (i915->display.vbt.version >= 239)
1079		panel->vbt.backlight.hdr_dpcd_refresh_timeout =
1080			DIV_ROUND_UP(backlight_data->hdr_dpcd_refresh_timeout[panel_type], 100);
1081	else
1082		panel->vbt.backlight.hdr_dpcd_refresh_timeout = 30;
1083
1084	drm_dbg_kms(&i915->drm,
1085		    "VBT backlight PWM modulation frequency %u Hz, "
1086		    "active %s, min brightness %u, level %u, controller %u\n",
1087		    panel->vbt.backlight.pwm_freq_hz,
1088		    panel->vbt.backlight.active_low_pwm ? "low" : "high",
1089		    panel->vbt.backlight.min_brightness,
1090		    level,
1091		    panel->vbt.backlight.controller);
1092}
1093
1094/* Try to find sdvo panel data */
1095static void
1096parse_sdvo_panel_data(struct drm_i915_private *i915,
1097		      struct intel_panel *panel)
1098{
1099	const struct bdb_sdvo_panel_dtds *dtds;
1100	struct drm_display_mode *panel_fixed_mode;
1101	int index;
1102
1103	index = i915->display.params.vbt_sdvo_panel_type;
1104	if (index == -2) {
1105		drm_dbg_kms(&i915->drm,
1106			    "Ignore SDVO panel mode from BIOS VBT tables.\n");
1107		return;
1108	}
1109
1110	if (index == -1) {
1111		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
1112
1113		sdvo_lvds_options = bdb_find_section(i915, BDB_SDVO_LVDS_OPTIONS);
1114		if (!sdvo_lvds_options)
1115			return;
1116
1117		index = sdvo_lvds_options->panel_type;
1118	}
1119
1120	dtds = bdb_find_section(i915, BDB_SDVO_PANEL_DTDS);
1121	if (!dtds)
1122		return;
1123
1124	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
1125	if (!panel_fixed_mode)
1126		return;
1127
1128	fill_detail_timing_data(i915, panel_fixed_mode, &dtds->dtds[index]);
1129
1130	panel->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
1131
1132	drm_dbg_kms(&i915->drm,
1133		    "Found SDVO panel mode in BIOS VBT tables: " DRM_MODE_FMT "\n",
1134		    DRM_MODE_ARG(panel_fixed_mode));
1135}
1136
1137static int intel_bios_ssc_frequency(struct drm_i915_private *i915,
1138				    bool alternate)
1139{
1140	switch (DISPLAY_VER(i915)) {
1141	case 2:
1142		return alternate ? 66667 : 48000;
1143	case 3:
1144	case 4:
1145		return alternate ? 100000 : 96000;
1146	default:
1147		return alternate ? 100000 : 120000;
1148	}
1149}
1150
1151static void
1152parse_general_features(struct drm_i915_private *i915)
1153{
1154	const struct bdb_general_features *general;
1155
1156	general = bdb_find_section(i915, BDB_GENERAL_FEATURES);
1157	if (!general)
1158		return;
1159
1160	i915->display.vbt.int_tv_support = general->int_tv_support;
1161	/* int_crt_support can't be trusted on earlier platforms */
1162	if (i915->display.vbt.version >= 155 &&
1163	    (HAS_DDI(i915) || IS_VALLEYVIEW(i915)))
1164		i915->display.vbt.int_crt_support = general->int_crt_support;
1165	i915->display.vbt.lvds_use_ssc = general->enable_ssc;
1166	i915->display.vbt.lvds_ssc_freq =
1167		intel_bios_ssc_frequency(i915, general->ssc_freq);
1168	i915->display.vbt.display_clock_mode = general->display_clock_mode;
1169	i915->display.vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
1170	if (i915->display.vbt.version >= 181) {
1171		i915->display.vbt.orientation = general->rotate_180 ?
1172			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
1173			DRM_MODE_PANEL_ORIENTATION_NORMAL;
1174	} else {
1175		i915->display.vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1176	}
1177
1178	if (i915->display.vbt.version >= 249 && general->afc_startup_config) {
1179		i915->display.vbt.override_afc_startup = true;
1180		i915->display.vbt.override_afc_startup_val = general->afc_startup_config == 0x1 ? 0x0 : 0x7;
1181	}
1182
1183	drm_dbg_kms(&i915->drm,
1184		    "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",
1185		    i915->display.vbt.int_tv_support,
1186		    i915->display.vbt.int_crt_support,
1187		    i915->display.vbt.lvds_use_ssc,
1188		    i915->display.vbt.lvds_ssc_freq,
1189		    i915->display.vbt.display_clock_mode,
1190		    i915->display.vbt.fdi_rx_polarity_inverted);
1191}
1192
1193static const struct child_device_config *
1194child_device_ptr(const struct bdb_general_definitions *defs, int i)
1195{
1196	return (const void *) &defs->devices[i * defs->child_dev_size];
1197}
1198
1199static void
1200parse_sdvo_device_mapping(struct drm_i915_private *i915)
1201{
1202	const struct intel_bios_encoder_data *devdata;
1203	int count = 0;
1204
1205	/*
1206	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
1207	 * accurate and doesn't have to be, as long as it's not too strict.
1208	 */
1209	if (!IS_DISPLAY_VER(i915, 3, 7)) {
1210		drm_dbg_kms(&i915->drm, "Skipping SDVO device mapping\n");
1211		return;
1212	}
1213
1214	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
1215		const struct child_device_config *child = &devdata->child;
1216		struct sdvo_device_mapping *mapping;
1217
1218		if (child->slave_addr != SLAVE_ADDR1 &&
1219		    child->slave_addr != SLAVE_ADDR2) {
1220			/*
1221			 * If the slave address is neither 0x70 nor 0x72,
1222			 * it is not a SDVO device. Skip it.
1223			 */
1224			continue;
1225		}
1226		if (child->dvo_port != DEVICE_PORT_DVOB &&
1227		    child->dvo_port != DEVICE_PORT_DVOC) {
1228			/* skip the incorrect SDVO port */
1229			drm_dbg_kms(&i915->drm,
1230				    "Incorrect SDVO port. Skip it\n");
1231			continue;
1232		}
1233		drm_dbg_kms(&i915->drm,
1234			    "the SDVO device with slave addr %2x is found on"
1235			    " %s port\n",
1236			    child->slave_addr,
1237			    (child->dvo_port == DEVICE_PORT_DVOB) ?
1238			    "SDVOB" : "SDVOC");
1239		mapping = &i915->display.vbt.sdvo_mappings[child->dvo_port - 1];
1240		if (!mapping->initialized) {
1241			mapping->dvo_port = child->dvo_port;
1242			mapping->slave_addr = child->slave_addr;
1243			mapping->dvo_wiring = child->dvo_wiring;
1244			mapping->ddc_pin = child->ddc_pin;
1245			mapping->i2c_pin = child->i2c_pin;
1246			mapping->initialized = 1;
1247			drm_dbg_kms(&i915->drm,
1248				    "SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
1249				    mapping->dvo_port, mapping->slave_addr,
1250				    mapping->dvo_wiring, mapping->ddc_pin,
1251				    mapping->i2c_pin);
1252		} else {
1253			drm_dbg_kms(&i915->drm,
1254				    "Maybe one SDVO port is shared by "
1255				    "two SDVO device.\n");
1256		}
1257		if (child->slave2_addr) {
1258			/* Maybe this is a SDVO device with multiple inputs */
1259			/* And the mapping info is not added */
1260			drm_dbg_kms(&i915->drm,
1261				    "there exists the slave2_addr. Maybe this"
1262				    " is a SDVO device with multiple inputs.\n");
1263		}
1264		count++;
1265	}
1266
1267	if (!count) {
1268		/* No SDVO device info is found */
1269		drm_dbg_kms(&i915->drm,
1270			    "No SDVO device info is found in VBT\n");
1271	}
1272}
1273
1274static void
1275parse_driver_features(struct drm_i915_private *i915)
1276{
1277	const struct bdb_driver_features *driver;
1278
1279	driver = bdb_find_section(i915, BDB_DRIVER_FEATURES);
1280	if (!driver)
1281		return;
1282
1283	if (DISPLAY_VER(i915) >= 5) {
1284		/*
1285		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
1286		 * to mean "eDP". The VBT spec doesn't agree with that
1287		 * interpretation, but real world VBTs seem to.
1288		 */
1289		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
1290			i915->display.vbt.int_lvds_support = 0;
1291	} else {
1292		/*
1293		 * FIXME it's not clear which BDB version has the LVDS config
1294		 * bits defined. Revision history in the VBT spec says:
1295		 * "0.92 | Add two definitions for VBT value of LVDS Active
1296		 *  Config (00b and 11b values defined) | 06/13/2005"
1297		 * but does not the specify the BDB version.
1298		 *
1299		 * So far version 134 (on i945gm) is the oldest VBT observed
1300		 * in the wild with the bits correctly populated. Version
1301		 * 108 (on i85x) does not have the bits correctly populated.
1302		 */
1303		if (i915->display.vbt.version >= 134 &&
1304		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
1305		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
1306			i915->display.vbt.int_lvds_support = 0;
1307	}
1308}
1309
1310static void
1311parse_panel_driver_features(struct drm_i915_private *i915,
1312			    struct intel_panel *panel)
1313{
1314	const struct bdb_driver_features *driver;
1315
1316	driver = bdb_find_section(i915, BDB_DRIVER_FEATURES);
1317	if (!driver)
1318		return;
1319
1320	if (i915->display.vbt.version < 228) {
1321		drm_dbg_kms(&i915->drm, "DRRS State Enabled:%d\n",
1322			    driver->drrs_enabled);
1323		/*
1324		 * If DRRS is not supported, drrs_type has to be set to 0.
1325		 * This is because, VBT is configured in such a way that
1326		 * static DRRS is 0 and DRRS not supported is represented by
1327		 * driver->drrs_enabled=false
1328		 */
1329		if (!driver->drrs_enabled && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
1330			/*
1331			 * FIXME Should DMRRS perhaps be treated as seamless
1332			 * but without the automatic downclocking?
1333			 */
1334			if (driver->dmrrs_enabled)
1335				panel->vbt.drrs_type = DRRS_TYPE_STATIC;
1336			else
1337				panel->vbt.drrs_type = DRRS_TYPE_NONE;
1338		}
1339
1340		panel->vbt.psr.enable = driver->psr_enabled;
1341	}
1342}
1343
1344static void
1345parse_power_conservation_features(struct drm_i915_private *i915,
1346				  struct intel_panel *panel)
1347{
1348	const struct bdb_lfp_power *power;
1349	u8 panel_type = panel->vbt.panel_type;
1350
1351	panel->vbt.vrr = true; /* matches Windows behaviour */
1352
1353	if (i915->display.vbt.version < 228)
1354		return;
1355
1356	power = bdb_find_section(i915, BDB_LFP_POWER);
1357	if (!power)
1358		return;
1359
1360	panel->vbt.psr.enable = panel_bool(power->psr, panel_type);
1361
1362	/*
1363	 * If DRRS is not supported, drrs_type has to be set to 0.
1364	 * This is because, VBT is configured in such a way that
1365	 * static DRRS is 0 and DRRS not supported is represented by
1366	 * power->drrs & BIT(panel_type)=false
1367	 */
1368	if (!panel_bool(power->drrs, panel_type) && panel->vbt.drrs_type != DRRS_TYPE_NONE) {
1369		/*
1370		 * FIXME Should DMRRS perhaps be treated as seamless
1371		 * but without the automatic downclocking?
1372		 */
1373		if (panel_bool(power->dmrrs, panel_type))
1374			panel->vbt.drrs_type = DRRS_TYPE_STATIC;
1375		else
1376			panel->vbt.drrs_type = DRRS_TYPE_NONE;
1377	}
1378
1379	if (i915->display.vbt.version >= 232)
1380		panel->vbt.edp.hobl = panel_bool(power->hobl, panel_type);
1381
1382	if (i915->display.vbt.version >= 233)
1383		panel->vbt.vrr = panel_bool(power->vrr_feature_enabled,
1384					    panel_type);
1385}
1386
1387static void
1388parse_edp(struct drm_i915_private *i915,
1389	  struct intel_panel *panel)
1390{
1391	const struct bdb_edp *edp;
1392	const struct edp_power_seq *edp_pps;
1393	const struct edp_fast_link_params *edp_link_params;
1394	int panel_type = panel->vbt.panel_type;
1395
1396	edp = bdb_find_section(i915, BDB_EDP);
1397	if (!edp)
1398		return;
1399
1400	switch (panel_bits(edp->color_depth, panel_type, 2)) {
1401	case EDP_18BPP:
1402		panel->vbt.edp.bpp = 18;
1403		break;
1404	case EDP_24BPP:
1405		panel->vbt.edp.bpp = 24;
1406		break;
1407	case EDP_30BPP:
1408		panel->vbt.edp.bpp = 30;
1409		break;
1410	}
1411
1412	/* Get the eDP sequencing and link info */
1413	edp_pps = &edp->power_seqs[panel_type];
1414	edp_link_params = &edp->fast_link_params[panel_type];
1415
1416	panel->vbt.edp.pps = *edp_pps;
1417
1418	if (i915->display.vbt.version >= 224) {
1419		panel->vbt.edp.rate =
1420			edp->edp_fast_link_training_rate[panel_type] * 20;
1421	} else {
1422		switch (edp_link_params->rate) {
1423		case EDP_RATE_1_62:
1424			panel->vbt.edp.rate = 162000;
1425			break;
1426		case EDP_RATE_2_7:
1427			panel->vbt.edp.rate = 270000;
1428			break;
1429		case EDP_RATE_5_4:
1430			panel->vbt.edp.rate = 540000;
1431			break;
1432		default:
1433			drm_dbg_kms(&i915->drm,
1434				    "VBT has unknown eDP link rate value %u\n",
1435				    edp_link_params->rate);
1436			break;
1437		}
1438	}
1439
1440	switch (edp_link_params->lanes) {
1441	case EDP_LANE_1:
1442		panel->vbt.edp.lanes = 1;
1443		break;
1444	case EDP_LANE_2:
1445		panel->vbt.edp.lanes = 2;
1446		break;
1447	case EDP_LANE_4:
1448		panel->vbt.edp.lanes = 4;
1449		break;
1450	default:
1451		drm_dbg_kms(&i915->drm,
1452			    "VBT has unknown eDP lane count value %u\n",
1453			    edp_link_params->lanes);
1454		break;
1455	}
1456
1457	switch (edp_link_params->preemphasis) {
1458	case EDP_PREEMPHASIS_NONE:
1459		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
1460		break;
1461	case EDP_PREEMPHASIS_3_5dB:
1462		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
1463		break;
1464	case EDP_PREEMPHASIS_6dB:
1465		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
1466		break;
1467	case EDP_PREEMPHASIS_9_5dB:
1468		panel->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
1469		break;
1470	default:
1471		drm_dbg_kms(&i915->drm,
1472			    "VBT has unknown eDP pre-emphasis value %u\n",
1473			    edp_link_params->preemphasis);
1474		break;
1475	}
1476
1477	switch (edp_link_params->vswing) {
1478	case EDP_VSWING_0_4V:
1479		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
1480		break;
1481	case EDP_VSWING_0_6V:
1482		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
1483		break;
1484	case EDP_VSWING_0_8V:
1485		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
1486		break;
1487	case EDP_VSWING_1_2V:
1488		panel->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
1489		break;
1490	default:
1491		drm_dbg_kms(&i915->drm,
1492			    "VBT has unknown eDP voltage swing value %u\n",
1493			    edp_link_params->vswing);
1494		break;
1495	}
1496
1497	if (i915->display.vbt.version >= 173) {
1498		u8 vswing;
1499
1500		/* Don't read from VBT if module parameter has valid value*/
1501		if (i915->display.params.edp_vswing) {
1502			panel->vbt.edp.low_vswing =
1503				i915->display.params.edp_vswing == 1;
1504		} else {
1505			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
1506			panel->vbt.edp.low_vswing = vswing == 0;
1507		}
1508	}
1509
1510	panel->vbt.edp.drrs_msa_timing_delay =
1511		panel_bits(edp->sdrrs_msa_timing_delay, panel_type, 2);
1512
1513	if (i915->display.vbt.version >= 244)
1514		panel->vbt.edp.max_link_rate =
1515			edp->edp_max_port_link_rate[panel_type] * 20;
1516}
1517
1518static void
1519parse_psr(struct drm_i915_private *i915,
1520	  struct intel_panel *panel)
1521{
1522	const struct bdb_psr *psr;
1523	const struct psr_table *psr_table;
1524	int panel_type = panel->vbt.panel_type;
1525
1526	psr = bdb_find_section(i915, BDB_PSR);
1527	if (!psr) {
1528		drm_dbg_kms(&i915->drm, "No PSR BDB found.\n");
1529		return;
1530	}
1531
1532	psr_table = &psr->psr_table[panel_type];
1533
1534	panel->vbt.psr.full_link = psr_table->full_link;
1535	panel->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
1536
1537	/* Allowed VBT values goes from 0 to 15 */
1538	panel->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
1539		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
1540
1541	/*
1542	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
1543	 * Old decimal value is wake up time in multiples of 100 us.
1544	 */
1545	if (i915->display.vbt.version >= 205 &&
1546	    (DISPLAY_VER(i915) >= 9 && !IS_BROXTON(i915))) {
1547		switch (psr_table->tp1_wakeup_time) {
1548		case 0:
1549			panel->vbt.psr.tp1_wakeup_time_us = 500;
1550			break;
1551		case 1:
1552			panel->vbt.psr.tp1_wakeup_time_us = 100;
1553			break;
1554		case 3:
1555			panel->vbt.psr.tp1_wakeup_time_us = 0;
1556			break;
1557		default:
1558			drm_dbg_kms(&i915->drm,
1559				    "VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1560				    psr_table->tp1_wakeup_time);
1561			fallthrough;
1562		case 2:
1563			panel->vbt.psr.tp1_wakeup_time_us = 2500;
1564			break;
1565		}
1566
1567		switch (psr_table->tp2_tp3_wakeup_time) {
1568		case 0:
1569			panel->vbt.psr.tp2_tp3_wakeup_time_us = 500;
1570			break;
1571		case 1:
1572			panel->vbt.psr.tp2_tp3_wakeup_time_us = 100;
1573			break;
1574		case 3:
1575			panel->vbt.psr.tp2_tp3_wakeup_time_us = 0;
1576			break;
1577		default:
1578			drm_dbg_kms(&i915->drm,
1579				    "VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
1580				    psr_table->tp2_tp3_wakeup_time);
1581			fallthrough;
1582		case 2:
1583			panel->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
1584		break;
1585		}
1586	} else {
1587		panel->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
1588		panel->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
1589	}
1590
1591	if (i915->display.vbt.version >= 226) {
1592		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
1593
1594		wakeup_time = panel_bits(wakeup_time, panel_type, 2);
1595		switch (wakeup_time) {
1596		case 0:
1597			wakeup_time = 500;
1598			break;
1599		case 1:
1600			wakeup_time = 100;
1601			break;
1602		case 3:
1603			wakeup_time = 50;
1604			break;
1605		default:
1606		case 2:
1607			wakeup_time = 2500;
1608			break;
1609		}
1610		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
1611	} else {
1612		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
1613		panel->vbt.psr.psr2_tp2_tp3_wakeup_time_us = panel->vbt.psr.tp2_tp3_wakeup_time_us;
1614	}
1615}
1616
1617static void parse_dsi_backlight_ports(struct drm_i915_private *i915,
1618				      struct intel_panel *panel,
1619				      enum port port)
1620{
1621	enum port port_bc = DISPLAY_VER(i915) >= 11 ? PORT_B : PORT_C;
1622
1623	if (!panel->vbt.dsi.config->dual_link || i915->display.vbt.version < 197) {
1624		panel->vbt.dsi.bl_ports = BIT(port);
1625		if (panel->vbt.dsi.config->cabc_supported)
1626			panel->vbt.dsi.cabc_ports = BIT(port);
1627
1628		return;
1629	}
1630
1631	switch (panel->vbt.dsi.config->dl_dcs_backlight_ports) {
1632	case DL_DCS_PORT_A:
1633		panel->vbt.dsi.bl_ports = BIT(PORT_A);
1634		break;
1635	case DL_DCS_PORT_C:
1636		panel->vbt.dsi.bl_ports = BIT(port_bc);
1637		break;
1638	default:
1639	case DL_DCS_PORT_A_AND_C:
1640		panel->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(port_bc);
1641		break;
1642	}
1643
1644	if (!panel->vbt.dsi.config->cabc_supported)
1645		return;
1646
1647	switch (panel->vbt.dsi.config->dl_dcs_cabc_ports) {
1648	case DL_DCS_PORT_A:
1649		panel->vbt.dsi.cabc_ports = BIT(PORT_A);
1650		break;
1651	case DL_DCS_PORT_C:
1652		panel->vbt.dsi.cabc_ports = BIT(port_bc);
1653		break;
1654	default:
1655	case DL_DCS_PORT_A_AND_C:
1656		panel->vbt.dsi.cabc_ports =
1657					BIT(PORT_A) | BIT(port_bc);
1658		break;
1659	}
1660}
1661
1662static void
1663parse_mipi_config(struct drm_i915_private *i915,
1664		  struct intel_panel *panel)
1665{
1666	const struct bdb_mipi_config *start;
1667	const struct mipi_config *config;
1668	const struct mipi_pps_data *pps;
1669	int panel_type = panel->vbt.panel_type;
1670	enum port port;
1671
1672	/* parse MIPI blocks only if LFP type is MIPI */
1673	if (!intel_bios_is_dsi_present(i915, &port))
1674		return;
1675
1676	/* Initialize this to undefined indicating no generic MIPI support */
1677	panel->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
1678
1679	/* Block #40 is already parsed and panel_fixed_mode is
1680	 * stored in i915->lfp_lvds_vbt_mode
1681	 * resuse this when needed
1682	 */
1683
1684	/* Parse #52 for panel index used from panel_type already
1685	 * parsed
1686	 */
1687	start = bdb_find_section(i915, BDB_MIPI_CONFIG);
1688	if (!start) {
1689		drm_dbg_kms(&i915->drm, "No MIPI config BDB found");
1690		return;
1691	}
1692
1693	drm_dbg(&i915->drm, "Found MIPI Config block, panel index = %d\n",
1694		panel_type);
1695
1696	/*
1697	 * get hold of the correct configuration block and pps data as per
1698	 * the panel_type as index
1699	 */
1700	config = &start->config[panel_type];
1701	pps = &start->pps[panel_type];
1702
1703	/* store as of now full data. Trim when we realise all is not needed */
1704	panel->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
1705	if (!panel->vbt.dsi.config)
1706		return;
1707
1708	panel->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
1709	if (!panel->vbt.dsi.pps) {
1710		kfree(panel->vbt.dsi.config);
1711		return;
1712	}
1713
1714	parse_dsi_backlight_ports(i915, panel, port);
1715
1716	/* FIXME is the 90 vs. 270 correct? */
1717	switch (config->rotation) {
1718	case ENABLE_ROTATION_0:
1719		/*
1720		 * Most (all?) VBTs claim 0 degrees despite having
1721		 * an upside down panel, thus we do not trust this.
1722		 */
1723		panel->vbt.dsi.orientation =
1724			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1725		break;
1726	case ENABLE_ROTATION_90:
1727		panel->vbt.dsi.orientation =
1728			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
1729		break;
1730	case ENABLE_ROTATION_180:
1731		panel->vbt.dsi.orientation =
1732			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1733		break;
1734	case ENABLE_ROTATION_270:
1735		panel->vbt.dsi.orientation =
1736			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
1737		break;
1738	}
1739
1740	/* We have mandatory mipi config blocks. Initialize as generic panel */
1741	panel->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
1742}
1743
1744/* Find the sequence block and size for the given panel. */
1745static const u8 *
1746find_panel_sequence_block(struct drm_i915_private *i915,
1747			  const struct bdb_mipi_sequence *sequence,
1748			  u16 panel_id, u32 *seq_size)
1749{
1750	u32 total = get_blocksize(sequence);
1751	const u8 *data = &sequence->data[0];
1752	u8 current_id;
1753	u32 current_size;
1754	int header_size = sequence->version >= 3 ? 5 : 3;
1755	int index = 0;
1756	int i;
1757
1758	/* skip new block size */
1759	if (sequence->version >= 3)
1760		data += 4;
1761
1762	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
1763		if (index + header_size > total) {
1764			drm_err(&i915->drm, "Invalid sequence block (header)\n");
1765			return NULL;
1766		}
1767
1768		current_id = *(data + index);
1769		if (sequence->version >= 3)
1770			current_size = *((const u32 *)(data + index + 1));
1771		else
1772			current_size = *((const u16 *)(data + index + 1));
1773
1774		index += header_size;
1775
1776		if (index + current_size > total) {
1777			drm_err(&i915->drm, "Invalid sequence block\n");
1778			return NULL;
1779		}
1780
1781		if (current_id == panel_id) {
1782			*seq_size = current_size;
1783			return data + index;
1784		}
1785
1786		index += current_size;
1787	}
1788
1789	drm_err(&i915->drm, "Sequence block detected but no valid configuration\n");
1790
1791	return NULL;
1792}
1793
1794static int goto_next_sequence(struct drm_i915_private *i915,
1795			      const u8 *data, int index, int total)
1796{
1797	u16 len;
1798
1799	/* Skip Sequence Byte. */
1800	for (index = index + 1; index < total; index += len) {
1801		u8 operation_byte = *(data + index);
1802		index++;
1803
1804		switch (operation_byte) {
1805		case MIPI_SEQ_ELEM_END:
1806			return index;
1807		case MIPI_SEQ_ELEM_SEND_PKT:
1808			if (index + 4 > total)
1809				return 0;
1810
1811			len = *((const u16 *)(data + index + 2)) + 4;
1812			break;
1813		case MIPI_SEQ_ELEM_DELAY:
1814			len = 4;
1815			break;
1816		case MIPI_SEQ_ELEM_GPIO:
1817			len = 2;
1818			break;
1819		case MIPI_SEQ_ELEM_I2C:
1820			if (index + 7 > total)
1821				return 0;
1822			len = *(data + index + 6) + 7;
1823			break;
1824		default:
1825			drm_err(&i915->drm, "Unknown operation byte\n");
1826			return 0;
1827		}
1828	}
1829
1830	return 0;
1831}
1832
1833static int goto_next_sequence_v3(struct drm_i915_private *i915,
1834				 const u8 *data, int index, int total)
1835{
1836	int seq_end;
1837	u16 len;
1838	u32 size_of_sequence;
1839
1840	/*
1841	 * Could skip sequence based on Size of Sequence alone, but also do some
1842	 * checking on the structure.
1843	 */
1844	if (total < 5) {
1845		drm_err(&i915->drm, "Too small sequence size\n");
1846		return 0;
1847	}
1848
1849	/* Skip Sequence Byte. */
1850	index++;
1851
1852	/*
1853	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
1854	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
1855	 * byte.
1856	 */
1857	size_of_sequence = *((const u32 *)(data + index));
1858	index += 4;
1859
1860	seq_end = index + size_of_sequence;
1861	if (seq_end > total) {
1862		drm_err(&i915->drm, "Invalid sequence size\n");
1863		return 0;
1864	}
1865
1866	for (; index < total; index += len) {
1867		u8 operation_byte = *(data + index);
1868		index++;
1869
1870		if (operation_byte == MIPI_SEQ_ELEM_END) {
1871			if (index != seq_end) {
1872				drm_err(&i915->drm, "Invalid element structure\n");
1873				return 0;
1874			}
1875			return index;
1876		}
1877
1878		len = *(data + index);
1879		index++;
1880
1881		/*
1882		 * FIXME: Would be nice to check elements like for v1/v2 in
1883		 * goto_next_sequence() above.
1884		 */
1885		switch (operation_byte) {
1886		case MIPI_SEQ_ELEM_SEND_PKT:
1887		case MIPI_SEQ_ELEM_DELAY:
1888		case MIPI_SEQ_ELEM_GPIO:
1889		case MIPI_SEQ_ELEM_I2C:
1890		case MIPI_SEQ_ELEM_SPI:
1891		case MIPI_SEQ_ELEM_PMIC:
1892			break;
1893		default:
1894			drm_err(&i915->drm, "Unknown operation byte %u\n",
1895				operation_byte);
1896			break;
1897		}
1898	}
1899
1900	return 0;
1901}
1902
1903/*
1904 * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
1905 * skip all delay + gpio operands and stop at the first DSI packet op.
1906 */
1907static int get_init_otp_deassert_fragment_len(struct drm_i915_private *i915,
1908					      struct intel_panel *panel)
1909{
1910	const u8 *data = panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1911	int index, len;
1912
1913	if (drm_WARN_ON(&i915->drm,
1914			!data || panel->vbt.dsi.seq_version != 1))
1915		return 0;
1916
1917	/* index = 1 to skip sequence byte */
1918	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1919		switch (data[index]) {
1920		case MIPI_SEQ_ELEM_SEND_PKT:
1921			return index == 1 ? 0 : index;
1922		case MIPI_SEQ_ELEM_DELAY:
1923			len = 5; /* 1 byte for operand + uint32 */
1924			break;
1925		case MIPI_SEQ_ELEM_GPIO:
1926			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1927			break;
1928		default:
1929			return 0;
1930		}
1931	}
1932
1933	return 0;
1934}
1935
1936/*
1937 * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1938 * The deassert must be done before calling intel_dsi_device_ready, so for
1939 * these devices we split the init OTP sequence into a deassert sequence and
1940 * the actual init OTP part.
1941 */
1942static void vlv_fixup_mipi_sequences(struct drm_i915_private *i915,
1943				     struct intel_panel *panel)
1944{
1945	u8 *init_otp;
1946	int len;
1947
1948	/* Limit this to v1 vid-mode sequences */
1949	if (panel->vbt.dsi.config->is_cmd_mode ||
1950	    panel->vbt.dsi.seq_version != 1)
1951		return;
1952
1953	/* Only do this if there are otp and assert seqs and no deassert seq */
1954	if (!panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
1955	    !panel->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
1956	    panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1957		return;
1958
1959	/* The deassert-sequence ends at the first DSI packet */
1960	len = get_init_otp_deassert_fragment_len(i915, panel);
1961	if (!len)
1962		return;
1963
1964	drm_dbg_kms(&i915->drm,
1965		    "Using init OTP fragment to deassert reset\n");
1966
1967	/* Copy the fragment, update seq byte and terminate it */
1968	init_otp = (u8 *)panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1969	panel->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
1970	if (!panel->vbt.dsi.deassert_seq)
1971		return;
1972	panel->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
1973	panel->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1974	/* Use the copy for deassert */
1975	panel->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
1976		panel->vbt.dsi.deassert_seq;
1977	/* Replace the last byte of the fragment with init OTP seq byte */
1978	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1979	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
1980	panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
1981}
1982
1983/*
1984 * Some machines (eg. Lenovo 82TQ) appear to have broken
1985 * VBT sequences:
1986 * - INIT_OTP is not present at all
1987 * - what should be in INIT_OTP is in DISPLAY_ON
1988 * - what should be in DISPLAY_ON is in BACKLIGHT_ON
1989 *   (along with the actual backlight stuff)
1990 *
1991 * To make those work we simply swap DISPLAY_ON and INIT_OTP.
1992 *
1993 * TODO: Do we need to limit this to specific machines,
1994 *       or examine the contents of the sequences to
1995 *       avoid false positives?
1996 */
1997static void icl_fixup_mipi_sequences(struct drm_i915_private *i915,
1998				     struct intel_panel *panel)
1999{
2000	if (!panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] &&
2001	    panel->vbt.dsi.sequence[MIPI_SEQ_DISPLAY_ON]) {
2002		drm_dbg_kms(&i915->drm, "Broken VBT: Swapping INIT_OTP and DISPLAY_ON sequences\n");
2003
2004		swap(panel->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP],
2005		     panel->vbt.dsi.sequence[MIPI_SEQ_DISPLAY_ON]);
2006	}
2007}
2008
2009static void fixup_mipi_sequences(struct drm_i915_private *i915,
2010				 struct intel_panel *panel)
2011{
2012	if (DISPLAY_VER(i915) >= 11)
2013		icl_fixup_mipi_sequences(i915, panel);
2014	else if (IS_VALLEYVIEW(i915))
2015		vlv_fixup_mipi_sequences(i915, panel);
2016}
2017
2018static void
2019parse_mipi_sequence(struct drm_i915_private *i915,
2020		    struct intel_panel *panel)
2021{
2022	int panel_type = panel->vbt.panel_type;
2023	const struct bdb_mipi_sequence *sequence;
2024	const u8 *seq_data;
2025	u32 seq_size;
2026	u8 *data;
2027	int index = 0;
2028
2029	/* Only our generic panel driver uses the sequence block. */
2030	if (panel->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
2031		return;
2032
2033	sequence = bdb_find_section(i915, BDB_MIPI_SEQUENCE);
2034	if (!sequence) {
2035		drm_dbg_kms(&i915->drm,
2036			    "No MIPI Sequence found, parsing complete\n");
2037		return;
2038	}
2039
2040	/* Fail gracefully for forward incompatible sequence block. */
2041	if (sequence->version >= 4) {
2042		drm_err(&i915->drm,
2043			"Unable to parse MIPI Sequence Block v%u\n",
2044			sequence->version);
2045		return;
2046	}
2047
2048	drm_dbg(&i915->drm, "Found MIPI sequence block v%u\n",
2049		sequence->version);
2050
2051	seq_data = find_panel_sequence_block(i915, sequence, panel_type, &seq_size);
2052	if (!seq_data)
2053		return;
2054
2055	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
2056	if (!data)
2057		return;
2058
2059	/* Parse the sequences, store pointers to each sequence. */
2060	for (;;) {
2061		u8 seq_id = *(data + index);
2062		if (seq_id == MIPI_SEQ_END)
2063			break;
2064
2065		if (seq_id >= MIPI_SEQ_MAX) {
2066			drm_err(&i915->drm, "Unknown sequence %u\n",
2067				seq_id);
2068			goto err;
2069		}
2070
2071		/* Log about presence of sequences we won't run. */
2072		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
2073			drm_dbg_kms(&i915->drm,
2074				    "Unsupported sequence %u\n", seq_id);
2075
2076		panel->vbt.dsi.sequence[seq_id] = data + index;
2077
2078		if (sequence->version >= 3)
2079			index = goto_next_sequence_v3(i915, data, index, seq_size);
2080		else
2081			index = goto_next_sequence(i915, data, index, seq_size);
2082		if (!index) {
2083			drm_err(&i915->drm, "Invalid sequence %u\n",
2084				seq_id);
2085			goto err;
2086		}
2087	}
2088
2089	panel->vbt.dsi.data = data;
2090	panel->vbt.dsi.size = seq_size;
2091	panel->vbt.dsi.seq_version = sequence->version;
2092
2093	fixup_mipi_sequences(i915, panel);
2094
2095	drm_dbg(&i915->drm, "MIPI related VBT parsing complete\n");
2096	return;
2097
2098err:
2099	kfree(data);
2100	memset(panel->vbt.dsi.sequence, 0, sizeof(panel->vbt.dsi.sequence));
2101}
2102
2103static void
2104parse_compression_parameters(struct drm_i915_private *i915)
2105{
2106	const struct bdb_compression_parameters *params;
2107	struct intel_bios_encoder_data *devdata;
2108	u16 block_size;
2109	int index;
2110
2111	if (i915->display.vbt.version < 198)
2112		return;
2113
2114	params = bdb_find_section(i915, BDB_COMPRESSION_PARAMETERS);
2115	if (params) {
2116		/* Sanity checks */
2117		if (params->entry_size != sizeof(params->data[0])) {
2118			drm_dbg_kms(&i915->drm,
2119				    "VBT: unsupported compression param entry size\n");
2120			return;
2121		}
2122
2123		block_size = get_blocksize(params);
2124		if (block_size < sizeof(*params)) {
2125			drm_dbg_kms(&i915->drm,
2126				    "VBT: expected 16 compression param entries\n");
2127			return;
2128		}
2129	}
2130
2131	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
2132		const struct child_device_config *child = &devdata->child;
2133
2134		if (!child->compression_enable)
2135			continue;
2136
2137		if (!params) {
2138			drm_dbg_kms(&i915->drm,
2139				    "VBT: compression params not available\n");
2140			continue;
2141		}
2142
2143		if (child->compression_method_cps) {
2144			drm_dbg_kms(&i915->drm,
2145				    "VBT: CPS compression not supported\n");
2146			continue;
2147		}
2148
2149		index = child->compression_structure_index;
2150
2151		devdata->dsc = kmemdup(&params->data[index],
2152				       sizeof(*devdata->dsc), GFP_KERNEL);
2153	}
2154}
2155
2156static u8 translate_iboost(struct drm_i915_private *i915, u8 val)
2157{
2158	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
2159
2160	if (val >= ARRAY_SIZE(mapping)) {
2161		drm_dbg_kms(&i915->drm,
2162			    "Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
2163		return 0;
2164	}
2165	return mapping[val];
2166}
2167
2168static const u8 cnp_ddc_pin_map[] = {
2169	[0] = 0, /* N/A */
2170	[GMBUS_PIN_1_BXT] = DDC_BUS_DDI_B,
2171	[GMBUS_PIN_2_BXT] = DDC_BUS_DDI_C,
2172	[GMBUS_PIN_4_CNP] = DDC_BUS_DDI_D, /* sic */
2173	[GMBUS_PIN_3_BXT] = DDC_BUS_DDI_F, /* sic */
2174};
2175
2176static const u8 icp_ddc_pin_map[] = {
2177	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
2178	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
2179	[GMBUS_PIN_3_BXT] = TGL_DDC_BUS_DDI_C,
2180	[GMBUS_PIN_9_TC1_ICP] = ICL_DDC_BUS_PORT_1,
2181	[GMBUS_PIN_10_TC2_ICP] = ICL_DDC_BUS_PORT_2,
2182	[GMBUS_PIN_11_TC3_ICP] = ICL_DDC_BUS_PORT_3,
2183	[GMBUS_PIN_12_TC4_ICP] = ICL_DDC_BUS_PORT_4,
2184	[GMBUS_PIN_13_TC5_TGP] = TGL_DDC_BUS_PORT_5,
2185	[GMBUS_PIN_14_TC6_TGP] = TGL_DDC_BUS_PORT_6,
2186};
2187
2188static const u8 rkl_pch_tgp_ddc_pin_map[] = {
2189	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
2190	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
2191	[GMBUS_PIN_9_TC1_ICP] = RKL_DDC_BUS_DDI_D,
2192	[GMBUS_PIN_10_TC2_ICP] = RKL_DDC_BUS_DDI_E,
2193};
2194
2195static const u8 adls_ddc_pin_map[] = {
2196	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
2197	[GMBUS_PIN_9_TC1_ICP] = ADLS_DDC_BUS_PORT_TC1,
2198	[GMBUS_PIN_10_TC2_ICP] = ADLS_DDC_BUS_PORT_TC2,
2199	[GMBUS_PIN_11_TC3_ICP] = ADLS_DDC_BUS_PORT_TC3,
2200	[GMBUS_PIN_12_TC4_ICP] = ADLS_DDC_BUS_PORT_TC4,
2201};
2202
2203static const u8 gen9bc_tgp_ddc_pin_map[] = {
2204	[GMBUS_PIN_2_BXT] = DDC_BUS_DDI_B,
2205	[GMBUS_PIN_9_TC1_ICP] = DDC_BUS_DDI_C,
2206	[GMBUS_PIN_10_TC2_ICP] = DDC_BUS_DDI_D,
2207};
2208
2209static const u8 adlp_ddc_pin_map[] = {
2210	[GMBUS_PIN_1_BXT] = ICL_DDC_BUS_DDI_A,
2211	[GMBUS_PIN_2_BXT] = ICL_DDC_BUS_DDI_B,
2212	[GMBUS_PIN_9_TC1_ICP] = ADLP_DDC_BUS_PORT_TC1,
2213	[GMBUS_PIN_10_TC2_ICP] = ADLP_DDC_BUS_PORT_TC2,
2214	[GMBUS_PIN_11_TC3_ICP] = ADLP_DDC_BUS_PORT_TC3,
2215	[GMBUS_PIN_12_TC4_ICP] = ADLP_DDC_BUS_PORT_TC4,
2216};
2217
2218static u8 map_ddc_pin(struct drm_i915_private *i915, u8 vbt_pin)
2219{
2220	const u8 *ddc_pin_map;
2221	int i, n_entries;
2222
2223	if (IS_DGFX(i915))
2224		return vbt_pin;
2225
2226	if (INTEL_PCH_TYPE(i915) >= PCH_MTL || IS_ALDERLAKE_P(i915)) {
2227		ddc_pin_map = adlp_ddc_pin_map;
2228		n_entries = ARRAY_SIZE(adlp_ddc_pin_map);
2229	} else if (IS_ALDERLAKE_S(i915)) {
2230		ddc_pin_map = adls_ddc_pin_map;
2231		n_entries = ARRAY_SIZE(adls_ddc_pin_map);
2232	} else if (IS_ROCKETLAKE(i915) && INTEL_PCH_TYPE(i915) == PCH_TGP) {
2233		ddc_pin_map = rkl_pch_tgp_ddc_pin_map;
2234		n_entries = ARRAY_SIZE(rkl_pch_tgp_ddc_pin_map);
2235	} else if (HAS_PCH_TGP(i915) && DISPLAY_VER(i915) == 9) {
2236		ddc_pin_map = gen9bc_tgp_ddc_pin_map;
2237		n_entries = ARRAY_SIZE(gen9bc_tgp_ddc_pin_map);
2238	} else if (INTEL_PCH_TYPE(i915) >= PCH_ICP) {
2239		ddc_pin_map = icp_ddc_pin_map;
2240		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
2241	} else if (HAS_PCH_CNP(i915)) {
2242		ddc_pin_map = cnp_ddc_pin_map;
2243		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
2244	} else {
2245		/* Assuming direct map */
2246		return vbt_pin;
2247	}
2248
2249	for (i = 0; i < n_entries; i++) {
2250		if (ddc_pin_map[i] == vbt_pin)
2251			return i;
2252	}
2253
2254	drm_dbg_kms(&i915->drm,
2255		    "Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
2256		    vbt_pin);
2257	return 0;
2258}
2259
2260static u8 dvo_port_type(u8 dvo_port)
2261{
2262	switch (dvo_port) {
2263	case DVO_PORT_HDMIA:
2264	case DVO_PORT_HDMIB:
2265	case DVO_PORT_HDMIC:
2266	case DVO_PORT_HDMID:
2267	case DVO_PORT_HDMIE:
2268	case DVO_PORT_HDMIF:
2269	case DVO_PORT_HDMIG:
2270	case DVO_PORT_HDMIH:
2271	case DVO_PORT_HDMII:
2272		return DVO_PORT_HDMIA;
2273	case DVO_PORT_DPA:
2274	case DVO_PORT_DPB:
2275	case DVO_PORT_DPC:
2276	case DVO_PORT_DPD:
2277	case DVO_PORT_DPE:
2278	case DVO_PORT_DPF:
2279	case DVO_PORT_DPG:
2280	case DVO_PORT_DPH:
2281	case DVO_PORT_DPI:
2282		return DVO_PORT_DPA;
2283	case DVO_PORT_MIPIA:
2284	case DVO_PORT_MIPIB:
2285	case DVO_PORT_MIPIC:
2286	case DVO_PORT_MIPID:
2287		return DVO_PORT_MIPIA;
2288	default:
2289		return dvo_port;
2290	}
2291}
2292
2293static enum port __dvo_port_to_port(int n_ports, int n_dvo,
2294				    const int port_mapping[][3], u8 dvo_port)
2295{
2296	enum port port;
2297	int i;
2298
2299	for (port = PORT_A; port < n_ports; port++) {
2300		for (i = 0; i < n_dvo; i++) {
2301			if (port_mapping[port][i] == -1)
2302				break;
2303
2304			if (dvo_port == port_mapping[port][i])
2305				return port;
2306		}
2307	}
2308
2309	return PORT_NONE;
2310}
2311
2312static enum port dvo_port_to_port(struct drm_i915_private *i915,
2313				  u8 dvo_port)
2314{
2315	/*
2316	 * Each DDI port can have more than one value on the "DVO Port" field,
2317	 * so look for all the possible values for each port.
2318	 */
2319	static const int port_mapping[][3] = {
2320		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2321		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2322		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2323		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2324		[PORT_E] = { DVO_PORT_HDMIE, DVO_PORT_DPE, DVO_PORT_CRT },
2325		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
2326		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2327		[PORT_H] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2328		[PORT_I] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
2329	};
2330	/*
2331	 * RKL VBT uses PHY based mapping. Combo PHYs A,B,C,D
2332	 * map to DDI A,B,TC1,TC2 respectively.
2333	 */
2334	static const int rkl_port_mapping[][3] = {
2335		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2336		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2337		[PORT_C] = { -1 },
2338		[PORT_TC1] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2339		[PORT_TC2] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2340	};
2341	/*
2342	 * Alderlake S ports used in the driver are PORT_A, PORT_D, PORT_E,
2343	 * PORT_F and PORT_G, we need to map that to correct VBT sections.
2344	 */
2345	static const int adls_port_mapping[][3] = {
2346		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2347		[PORT_B] = { -1 },
2348		[PORT_C] = { -1 },
2349		[PORT_TC1] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2350		[PORT_TC2] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2351		[PORT_TC3] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2352		[PORT_TC4] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
2353	};
2354	static const int xelpd_port_mapping[][3] = {
2355		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1 },
2356		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1 },
2357		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1 },
2358		[PORT_D_XELPD] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1 },
2359		[PORT_E_XELPD] = { DVO_PORT_HDMIE, DVO_PORT_DPE, -1 },
2360		[PORT_TC1] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1 },
2361		[PORT_TC2] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1 },
2362		[PORT_TC3] = { DVO_PORT_HDMIH, DVO_PORT_DPH, -1 },
2363		[PORT_TC4] = { DVO_PORT_HDMII, DVO_PORT_DPI, -1 },
2364	};
2365
2366	if (DISPLAY_VER(i915) >= 13)
2367		return __dvo_port_to_port(ARRAY_SIZE(xelpd_port_mapping),
2368					  ARRAY_SIZE(xelpd_port_mapping[0]),
2369					  xelpd_port_mapping,
2370					  dvo_port);
2371	else if (IS_ALDERLAKE_S(i915))
2372		return __dvo_port_to_port(ARRAY_SIZE(adls_port_mapping),
2373					  ARRAY_SIZE(adls_port_mapping[0]),
2374					  adls_port_mapping,
2375					  dvo_port);
2376	else if (IS_DG1(i915) || IS_ROCKETLAKE(i915))
2377		return __dvo_port_to_port(ARRAY_SIZE(rkl_port_mapping),
2378					  ARRAY_SIZE(rkl_port_mapping[0]),
2379					  rkl_port_mapping,
2380					  dvo_port);
2381	else
2382		return __dvo_port_to_port(ARRAY_SIZE(port_mapping),
2383					  ARRAY_SIZE(port_mapping[0]),
2384					  port_mapping,
2385					  dvo_port);
2386}
2387
2388static enum port
2389dsi_dvo_port_to_port(struct drm_i915_private *i915, u8 dvo_port)
2390{
2391	switch (dvo_port) {
2392	case DVO_PORT_MIPIA:
2393		return PORT_A;
2394	case DVO_PORT_MIPIC:
2395		if (DISPLAY_VER(i915) >= 11)
2396			return PORT_B;
2397		else
2398			return PORT_C;
2399	default:
2400		return PORT_NONE;
2401	}
2402}
2403
2404enum port intel_bios_encoder_port(const struct intel_bios_encoder_data *devdata)
2405{
2406	struct drm_i915_private *i915 = devdata->i915;
2407	const struct child_device_config *child = &devdata->child;
2408	enum port port;
2409
2410	port = dvo_port_to_port(i915, child->dvo_port);
2411	if (port == PORT_NONE && DISPLAY_VER(i915) >= 11)
2412		port = dsi_dvo_port_to_port(i915, child->dvo_port);
2413
2414	return port;
2415}
2416
2417static int parse_bdb_230_dp_max_link_rate(const int vbt_max_link_rate)
2418{
2419	switch (vbt_max_link_rate) {
2420	default:
2421	case BDB_230_VBT_DP_MAX_LINK_RATE_DEF:
2422		return 0;
2423	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR20:
2424		return 2000000;
2425	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR13P5:
2426		return 1350000;
2427	case BDB_230_VBT_DP_MAX_LINK_RATE_UHBR10:
2428		return 1000000;
2429	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR3:
2430		return 810000;
2431	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR2:
2432		return 540000;
2433	case BDB_230_VBT_DP_MAX_LINK_RATE_HBR:
2434		return 270000;
2435	case BDB_230_VBT_DP_MAX_LINK_RATE_LBR:
2436		return 162000;
2437	}
2438}
2439
2440static int parse_bdb_216_dp_max_link_rate(const int vbt_max_link_rate)
2441{
2442	switch (vbt_max_link_rate) {
2443	default:
2444	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR3:
2445		return 810000;
2446	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR2:
2447		return 540000;
2448	case BDB_216_VBT_DP_MAX_LINK_RATE_HBR:
2449		return 270000;
2450	case BDB_216_VBT_DP_MAX_LINK_RATE_LBR:
2451		return 162000;
2452	}
2453}
2454
2455int intel_bios_dp_max_link_rate(const struct intel_bios_encoder_data *devdata)
2456{
2457	if (!devdata || devdata->i915->display.vbt.version < 216)
2458		return 0;
2459
2460	if (devdata->i915->display.vbt.version >= 230)
2461		return parse_bdb_230_dp_max_link_rate(devdata->child.dp_max_link_rate);
2462	else
2463		return parse_bdb_216_dp_max_link_rate(devdata->child.dp_max_link_rate);
2464}
2465
2466int intel_bios_dp_max_lane_count(const struct intel_bios_encoder_data *devdata)
2467{
2468	if (!devdata || devdata->i915->display.vbt.version < 244)
2469		return 0;
2470
2471	return devdata->child.dp_max_lane_count + 1;
2472}
2473
2474static void sanitize_device_type(struct intel_bios_encoder_data *devdata,
2475				 enum port port)
2476{
2477	struct drm_i915_private *i915 = devdata->i915;
2478	bool is_hdmi;
2479
2480	if (port != PORT_A || DISPLAY_VER(i915) >= 12)
2481		return;
2482
2483	if (!intel_bios_encoder_supports_dvi(devdata))
2484		return;
2485
2486	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2487
2488	drm_dbg_kms(&i915->drm, "VBT claims port A supports DVI%s, ignoring\n",
2489		    is_hdmi ? "/HDMI" : "");
2490
2491	devdata->child.device_type &= ~DEVICE_TYPE_TMDS_DVI_SIGNALING;
2492	devdata->child.device_type |= DEVICE_TYPE_NOT_HDMI_OUTPUT;
2493}
2494
2495static void sanitize_hdmi_level_shift(struct intel_bios_encoder_data *devdata,
2496				      enum port port)
2497{
2498	struct drm_i915_private *i915 = devdata->i915;
2499
2500	if (!intel_bios_encoder_supports_dvi(devdata))
2501		return;
2502
2503	/*
2504	 * Some BDW machines (eg. HP Pavilion 15-ab) shipped
2505	 * with a HSW VBT where the level shifter value goes
2506	 * up to 11, whereas the BDW max is 9.
2507	 */
2508	if (IS_BROADWELL(i915) && devdata->child.hdmi_level_shifter_value > 9) {
2509		drm_dbg_kms(&i915->drm, "Bogus port %c VBT HDMI level shift %d, adjusting to %d\n",
2510			    port_name(port), devdata->child.hdmi_level_shifter_value, 9);
2511
2512		devdata->child.hdmi_level_shifter_value = 9;
2513	}
2514}
2515
2516static bool
2517intel_bios_encoder_supports_crt(const struct intel_bios_encoder_data *devdata)
2518{
2519	return devdata->child.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
2520}
2521
2522bool
2523intel_bios_encoder_supports_dvi(const struct intel_bios_encoder_data *devdata)
2524{
2525	return devdata->child.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
2526}
2527
2528bool
2529intel_bios_encoder_supports_hdmi(const struct intel_bios_encoder_data *devdata)
2530{
2531	return intel_bios_encoder_supports_dvi(devdata) &&
2532		(devdata->child.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
2533}
2534
2535bool
2536intel_bios_encoder_supports_dp(const struct intel_bios_encoder_data *devdata)
2537{
2538	return devdata->child.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2539}
2540
2541bool
2542intel_bios_encoder_supports_edp(const struct intel_bios_encoder_data *devdata)
2543{
2544	return intel_bios_encoder_supports_dp(devdata) &&
2545		devdata->child.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR;
2546}
2547
2548bool
2549intel_bios_encoder_supports_dsi(const struct intel_bios_encoder_data *devdata)
2550{
2551	return devdata->child.device_type & DEVICE_TYPE_MIPI_OUTPUT;
2552}
2553
2554bool
2555intel_bios_encoder_is_lspcon(const struct intel_bios_encoder_data *devdata)
2556{
2557	return devdata && HAS_LSPCON(devdata->i915) && devdata->child.lspcon;
2558}
2559
2560/* This is an index in the HDMI/DVI DDI buffer translation table, or -1 */
2561int intel_bios_hdmi_level_shift(const struct intel_bios_encoder_data *devdata)
2562{
2563	if (!devdata || devdata->i915->display.vbt.version < 158 ||
2564	    DISPLAY_VER(devdata->i915) >= 14)
2565		return -1;
2566
2567	return devdata->child.hdmi_level_shifter_value;
2568}
2569
2570int intel_bios_hdmi_max_tmds_clock(const struct intel_bios_encoder_data *devdata)
2571{
2572	if (!devdata || devdata->i915->display.vbt.version < 204)
2573		return 0;
2574
2575	switch (devdata->child.hdmi_max_data_rate) {
2576	default:
2577		MISSING_CASE(devdata->child.hdmi_max_data_rate);
2578		fallthrough;
2579	case HDMI_MAX_DATA_RATE_PLATFORM:
2580		return 0;
2581	case HDMI_MAX_DATA_RATE_594:
2582		return 594000;
2583	case HDMI_MAX_DATA_RATE_340:
2584		return 340000;
2585	case HDMI_MAX_DATA_RATE_300:
2586		return 300000;
2587	case HDMI_MAX_DATA_RATE_297:
2588		return 297000;
2589	case HDMI_MAX_DATA_RATE_165:
2590		return 165000;
2591	}
2592}
2593
2594static bool is_port_valid(struct drm_i915_private *i915, enum port port)
2595{
2596	/*
2597	 * On some ICL SKUs port F is not present, but broken VBTs mark
2598	 * the port as present. Only try to initialize port F for the
2599	 * SKUs that may actually have it.
2600	 */
2601	if (port == PORT_F && IS_ICELAKE(i915))
2602		return IS_ICL_WITH_PORT_F(i915);
2603
2604	return true;
2605}
2606
2607static void print_ddi_port(const struct intel_bios_encoder_data *devdata)
2608{
2609	struct drm_i915_private *i915 = devdata->i915;
2610	const struct child_device_config *child = &devdata->child;
2611	bool is_dvi, is_hdmi, is_dp, is_edp, is_dsi, is_crt, supports_typec_usb, supports_tbt;
2612	int dp_boost_level, dp_max_link_rate, hdmi_boost_level, hdmi_level_shift, max_tmds_clock;
2613	enum port port;
2614
2615	port = intel_bios_encoder_port(devdata);
2616	if (port == PORT_NONE)
2617		return;
2618
2619	is_dvi = intel_bios_encoder_supports_dvi(devdata);
2620	is_dp = intel_bios_encoder_supports_dp(devdata);
2621	is_crt = intel_bios_encoder_supports_crt(devdata);
2622	is_hdmi = intel_bios_encoder_supports_hdmi(devdata);
2623	is_edp = intel_bios_encoder_supports_edp(devdata);
2624	is_dsi = intel_bios_encoder_supports_dsi(devdata);
2625
2626	supports_typec_usb = intel_bios_encoder_supports_typec_usb(devdata);
2627	supports_tbt = intel_bios_encoder_supports_tbt(devdata);
2628
2629	drm_dbg_kms(&i915->drm,
2630		    "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",
2631		    port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp, is_dsi,
2632		    intel_bios_encoder_supports_dp_dual_mode(devdata),
2633		    intel_bios_encoder_is_lspcon(devdata),
2634		    supports_typec_usb, supports_tbt,
2635		    devdata->dsc != NULL);
2636
2637	hdmi_level_shift = intel_bios_hdmi_level_shift(devdata);
2638	if (hdmi_level_shift >= 0) {
2639		drm_dbg_kms(&i915->drm,
2640			    "Port %c VBT HDMI level shift: %d\n",
2641			    port_name(port), hdmi_level_shift);
2642	}
2643
2644	max_tmds_clock = intel_bios_hdmi_max_tmds_clock(devdata);
2645	if (max_tmds_clock)
2646		drm_dbg_kms(&i915->drm,
2647			    "Port %c VBT HDMI max TMDS clock: %d kHz\n",
2648			    port_name(port), max_tmds_clock);
2649
2650	/* I_boost config for SKL and above */
2651	dp_boost_level = intel_bios_dp_boost_level(devdata);
2652	if (dp_boost_level)
2653		drm_dbg_kms(&i915->drm,
2654			    "Port %c VBT (e)DP boost level: %d\n",
2655			    port_name(port), dp_boost_level);
2656
2657	hdmi_boost_level = intel_bios_hdmi_boost_level(devdata);
2658	if (hdmi_boost_level)
2659		drm_dbg_kms(&i915->drm,
2660			    "Port %c VBT HDMI boost level: %d\n",
2661			    port_name(port), hdmi_boost_level);
2662
2663	dp_max_link_rate = intel_bios_dp_max_link_rate(devdata);
2664	if (dp_max_link_rate)
2665		drm_dbg_kms(&i915->drm,
2666			    "Port %c VBT DP max link rate: %d\n",
2667			    port_name(port), dp_max_link_rate);
2668
2669	/*
2670	 * FIXME need to implement support for VBT
2671	 * vswing/preemph tables should this ever trigger.
2672	 */
2673	drm_WARN(&i915->drm, child->use_vbt_vswing,
2674		 "Port %c asks to use VBT vswing/preemph tables\n",
2675		 port_name(port));
2676}
2677
2678static void parse_ddi_port(struct intel_bios_encoder_data *devdata)
2679{
2680	struct drm_i915_private *i915 = devdata->i915;
2681	enum port port;
2682
2683	port = intel_bios_encoder_port(devdata);
2684	if (port == PORT_NONE)
2685		return;
2686
2687	if (!is_port_valid(i915, port)) {
2688		drm_dbg_kms(&i915->drm,
2689			    "VBT reports port %c as supported, but that can't be true: skipping\n",
2690			    port_name(port));
2691		return;
2692	}
2693
2694	sanitize_device_type(devdata, port);
2695	sanitize_hdmi_level_shift(devdata, port);
2696}
2697
2698static bool has_ddi_port_info(struct drm_i915_private *i915)
2699{
2700	return DISPLAY_VER(i915) >= 5 || IS_G4X(i915);
2701}
2702
2703static void parse_ddi_ports(struct drm_i915_private *i915)
2704{
2705	struct intel_bios_encoder_data *devdata;
2706
2707	if (!has_ddi_port_info(i915))
2708		return;
2709
2710	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
2711		parse_ddi_port(devdata);
2712
2713	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
2714		print_ddi_port(devdata);
2715}
2716
2717static int child_device_expected_size(u16 version)
2718{
2719	BUILD_BUG_ON(sizeof(struct child_device_config) < 40);
2720
2721	if (version > 256)
2722		return -ENOENT;
2723	else if (version >= 256)
2724		return 40;
2725	else if (version >= 216)
2726		return 39;
2727	else if (version >= 196)
2728		return 38;
2729	else if (version >= 195)
2730		return 37;
2731	else if (version >= 111)
2732		return LEGACY_CHILD_DEVICE_CONFIG_SIZE;
2733	else if (version >= 106)
2734		return 27;
2735	else
2736		return 22;
2737}
2738
2739static bool child_device_size_valid(struct drm_i915_private *i915, int size)
2740{
2741	int expected_size;
2742
2743	expected_size = child_device_expected_size(i915->display.vbt.version);
2744	if (expected_size < 0) {
2745		expected_size = sizeof(struct child_device_config);
2746		drm_dbg(&i915->drm,
2747			"Expected child device config size for VBT version %u not known; assuming %d\n",
2748			i915->display.vbt.version, expected_size);
2749	}
2750
2751	/* Flag an error for unexpected size, but continue anyway. */
2752	if (size != expected_size)
2753		drm_err(&i915->drm,
2754			"Unexpected child device config size %d (expected %d for VBT version %u)\n",
2755			size, expected_size, i915->display.vbt.version);
2756
2757	/* The legacy sized child device config is the minimum we need. */
2758	if (size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
2759		drm_dbg_kms(&i915->drm,
2760			    "Child device config size %d is too small.\n",
2761			    size);
2762		return false;
2763	}
2764
2765	return true;
2766}
2767
2768static void
2769parse_general_definitions(struct drm_i915_private *i915)
2770{
2771	const struct bdb_general_definitions *defs;
2772	struct intel_bios_encoder_data *devdata;
2773	const struct child_device_config *child;
2774	int i, child_device_num;
2775	u16 block_size;
2776	int bus_pin;
2777
2778	defs = bdb_find_section(i915, BDB_GENERAL_DEFINITIONS);
2779	if (!defs) {
2780		drm_dbg_kms(&i915->drm,
2781			    "No general definition block is found, no devices defined.\n");
2782		return;
2783	}
2784
2785	block_size = get_blocksize(defs);
2786	if (block_size < sizeof(*defs)) {
2787		drm_dbg_kms(&i915->drm,
2788			    "General definitions block too small (%u)\n",
2789			    block_size);
2790		return;
2791	}
2792
2793	bus_pin = defs->crt_ddc_gmbus_pin;
2794	drm_dbg_kms(&i915->drm, "crt_ddc_bus_pin: %d\n", bus_pin);
2795	if (intel_gmbus_is_valid_pin(i915, bus_pin))
2796		i915->display.vbt.crt_ddc_pin = bus_pin;
2797
2798	if (!child_device_size_valid(i915, defs->child_dev_size))
2799		return;
2800
2801	/* get the number of child device */
2802	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
2803
2804	for (i = 0; i < child_device_num; i++) {
2805		child = child_device_ptr(defs, i);
2806		if (!child->device_type)
2807			continue;
2808
2809		drm_dbg_kms(&i915->drm,
2810			    "Found VBT child device with type 0x%x\n",
2811			    child->device_type);
2812
2813		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
2814		if (!devdata)
2815			break;
2816
2817		devdata->i915 = i915;
2818
2819		/*
2820		 * Copy as much as we know (sizeof) and is available
2821		 * (child_dev_size) of the child device config. Accessing the
2822		 * data must depend on VBT version.
2823		 */
2824		memcpy(&devdata->child, child,
2825		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
2826
2827		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
2828	}
2829
2830	if (list_empty(&i915->display.vbt.display_devices))
2831		drm_dbg_kms(&i915->drm,
2832			    "no child dev is parsed from VBT\n");
2833}
2834
2835/* Common defaults which may be overridden by VBT. */
2836static void
2837init_vbt_defaults(struct drm_i915_private *i915)
2838{
2839	i915->display.vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
2840
2841	/* general features */
2842	i915->display.vbt.int_tv_support = 1;
2843	i915->display.vbt.int_crt_support = 1;
2844
2845	/* driver features */
2846	i915->display.vbt.int_lvds_support = 1;
2847
2848	/* Default to using SSC */
2849	i915->display.vbt.lvds_use_ssc = 1;
2850	/*
2851	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
2852	 * clock for LVDS.
2853	 */
2854	i915->display.vbt.lvds_ssc_freq = intel_bios_ssc_frequency(i915,
2855								   !HAS_PCH_SPLIT(i915));
2856	drm_dbg_kms(&i915->drm, "Set default to SSC at %d kHz\n",
2857		    i915->display.vbt.lvds_ssc_freq);
2858}
2859
2860/* Common defaults which may be overridden by VBT. */
2861static void
2862init_vbt_panel_defaults(struct intel_panel *panel)
2863{
2864	/* Default to having backlight */
2865	panel->vbt.backlight.present = true;
2866
2867	/* LFP panel data */
2868	panel->vbt.lvds_dither = true;
2869}
2870
2871/* Defaults to initialize only if there is no VBT. */
2872static void
2873init_vbt_missing_defaults(struct drm_i915_private *i915)
2874{
2875	unsigned int ports = DISPLAY_RUNTIME_INFO(i915)->port_mask;
2876	enum port port;
2877
2878	if (!HAS_DDI(i915) && !IS_CHERRYVIEW(i915))
2879		return;
2880
2881	for_each_port_masked(port, ports) {
2882		struct intel_bios_encoder_data *devdata;
2883		struct child_device_config *child;
2884		enum phy phy = intel_port_to_phy(i915, port);
2885
2886		/*
2887		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
2888		 * to detect it.
2889		 */
2890		if (intel_phy_is_tc(i915, phy))
2891			continue;
2892
2893		/* Create fake child device config */
2894		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
2895		if (!devdata)
2896			break;
2897
2898		devdata->i915 = i915;
2899		child = &devdata->child;
2900
2901		if (port == PORT_F)
2902			child->dvo_port = DVO_PORT_HDMIF;
2903		else if (port == PORT_E)
2904			child->dvo_port = DVO_PORT_HDMIE;
2905		else
2906			child->dvo_port = DVO_PORT_HDMIA + port;
2907
2908		if (port != PORT_A && port != PORT_E)
2909			child->device_type |= DEVICE_TYPE_TMDS_DVI_SIGNALING;
2910
2911		if (port != PORT_E)
2912			child->device_type |= DEVICE_TYPE_DISPLAYPORT_OUTPUT;
2913
2914		if (port == PORT_A)
2915			child->device_type |= DEVICE_TYPE_INTERNAL_CONNECTOR;
2916
2917		list_add_tail(&devdata->node, &i915->display.vbt.display_devices);
2918
2919		drm_dbg_kms(&i915->drm,
2920			    "Generating default VBT child device with type 0x04%x on port %c\n",
2921			    child->device_type, port_name(port));
2922	}
2923
2924	/* Bypass some minimum baseline VBT version checks */
2925	i915->display.vbt.version = 155;
2926}
2927
2928static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
2929{
2930	const void *_vbt = vbt;
2931
2932	return _vbt + vbt->bdb_offset;
2933}
2934
2935/**
2936 * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
2937 * @i915:	the device
2938 * @buf:	pointer to a buffer to validate
2939 * @size:	size of the buffer
2940 *
2941 * Returns true on valid VBT.
2942 */
2943bool intel_bios_is_valid_vbt(struct drm_i915_private *i915,
2944			     const void *buf, size_t size)
2945{
2946	const struct vbt_header *vbt = buf;
2947	const struct bdb_header *bdb;
2948
2949	if (!vbt)
2950		return false;
2951
2952	if (sizeof(struct vbt_header) > size) {
2953		drm_dbg_kms(&i915->drm, "VBT header incomplete\n");
2954		return false;
2955	}
2956
2957	if (memcmp(vbt->signature, "$VBT", 4)) {
2958		drm_dbg_kms(&i915->drm, "VBT invalid signature\n");
2959		return false;
2960	}
2961
2962	if (vbt->vbt_size > size) {
2963		drm_dbg_kms(&i915->drm, "VBT incomplete (vbt_size overflows)\n");
2964		return false;
2965	}
2966
2967	size = vbt->vbt_size;
2968
2969	if (range_overflows_t(size_t,
2970			      vbt->bdb_offset,
2971			      sizeof(struct bdb_header),
2972			      size)) {
2973		drm_dbg_kms(&i915->drm, "BDB header incomplete\n");
2974		return false;
2975	}
2976
2977	bdb = get_bdb_header(vbt);
2978	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
2979		drm_dbg_kms(&i915->drm, "BDB incomplete\n");
2980		return false;
2981	}
2982
2983	return vbt;
2984}
2985
2986static struct vbt_header *firmware_get_vbt(struct drm_i915_private *i915,
2987					   size_t *size)
2988{
2989	struct vbt_header *vbt = NULL;
2990	const struct firmware *fw = NULL;
2991	const char *name = i915->display.params.vbt_firmware;
2992	int ret;
2993
2994	if (!name || !*name)
2995		return NULL;
2996
2997	ret = request_firmware(&fw, name, i915->drm.dev);
2998	if (ret) {
2999		drm_err(&i915->drm,
3000			"Requesting VBT firmware \"%s\" failed (%d)\n",
3001			name, ret);
3002		return NULL;
3003	}
3004
3005	if (intel_bios_is_valid_vbt(i915, fw->data, fw->size)) {
3006		vbt = kmemdup(fw->data, fw->size, GFP_KERNEL);
3007		if (vbt) {
3008			drm_dbg_kms(&i915->drm,
3009				    "Found valid VBT firmware \"%s\"\n", name);
3010			if (size)
3011				*size = fw->size;
3012		}
3013	} else {
3014		drm_dbg_kms(&i915->drm, "Invalid VBT firmware \"%s\"\n",
3015			    name);
3016	}
3017
3018	release_firmware(fw);
3019
3020	return vbt;
3021}
3022
3023static u32 intel_spi_read(struct intel_uncore *uncore, u32 offset)
3024{
3025	intel_uncore_write(uncore, PRIMARY_SPI_ADDRESS, offset);
3026
3027	return intel_uncore_read(uncore, PRIMARY_SPI_TRIGGER);
3028}
3029
3030static struct vbt_header *spi_oprom_get_vbt(struct drm_i915_private *i915,
3031					    size_t *size)
3032{
3033	u32 count, data, found, store = 0;
3034	u32 static_region, oprom_offset;
3035	u32 oprom_size = 0x200000;
3036	u16 vbt_size;
3037	u32 *vbt;
3038
3039	static_region = intel_uncore_read(&i915->uncore, SPI_STATIC_REGIONS);
3040	static_region &= OPTIONROM_SPI_REGIONID_MASK;
3041	intel_uncore_write(&i915->uncore, PRIMARY_SPI_REGIONID, static_region);
3042
3043	oprom_offset = intel_uncore_read(&i915->uncore, OROM_OFFSET);
3044	oprom_offset &= OROM_OFFSET_MASK;
3045
3046	for (count = 0; count < oprom_size; count += 4) {
3047		data = intel_spi_read(&i915->uncore, oprom_offset + count);
3048		if (data == *((const u32 *)"$VBT")) {
3049			found = oprom_offset + count;
3050			break;
3051		}
3052	}
3053
3054	if (count >= oprom_size)
3055		goto err_not_found;
3056
3057	/* Get VBT size and allocate space for the VBT */
3058	vbt_size = intel_spi_read(&i915->uncore,
3059				  found + offsetof(struct vbt_header, vbt_size));
3060	vbt_size &= 0xffff;
3061
3062	vbt = kzalloc(round_up(vbt_size, 4), GFP_KERNEL);
3063	if (!vbt)
3064		goto err_not_found;
3065
3066	for (count = 0; count < vbt_size; count += 4)
3067		*(vbt + store++) = intel_spi_read(&i915->uncore, found + count);
3068
3069	if (!intel_bios_is_valid_vbt(i915, vbt, vbt_size))
3070		goto err_free_vbt;
3071
3072	drm_dbg_kms(&i915->drm, "Found valid VBT in SPI flash\n");
3073
3074	if (size)
3075		*size = vbt_size;
3076
3077	return (struct vbt_header *)vbt;
3078
3079err_free_vbt:
3080	kfree(vbt);
3081err_not_found:
3082	return NULL;
3083}
3084
3085static struct vbt_header *oprom_get_vbt(struct drm_i915_private *i915,
3086					size_t *sizep)
3087{
3088	struct pci_dev *pdev = to_pci_dev(i915->drm.dev);
3089	void __iomem *p = NULL, *oprom;
3090	struct vbt_header *vbt;
3091	u16 vbt_size;
3092	size_t i, size;
3093
3094	oprom = pci_map_rom(pdev, &size);
3095	if (!oprom)
3096		return NULL;
3097
3098	/* Scour memory looking for the VBT signature. */
3099	for (i = 0; i + 4 < size; i += 4) {
3100		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
3101			continue;
3102
3103		p = oprom + i;
3104		size -= i;
3105		break;
3106	}
3107
3108	if (!p)
3109		goto err_unmap_oprom;
3110
3111	if (sizeof(struct vbt_header) > size) {
3112		drm_dbg(&i915->drm, "VBT header incomplete\n");
3113		goto err_unmap_oprom;
3114	}
3115
3116	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
3117	if (vbt_size > size) {
3118		drm_dbg(&i915->drm,
3119			"VBT incomplete (vbt_size overflows)\n");
3120		goto err_unmap_oprom;
3121	}
3122
3123	/* The rest will be validated by intel_bios_is_valid_vbt() */
3124	vbt = kmalloc(vbt_size, GFP_KERNEL);
3125	if (!vbt)
3126		goto err_unmap_oprom;
3127
3128	memcpy_fromio(vbt, p, vbt_size);
3129
3130	if (!intel_bios_is_valid_vbt(i915, vbt, vbt_size))
3131		goto err_free_vbt;
3132
3133	pci_unmap_rom(pdev, oprom);
3134
3135	if (sizep)
3136		*sizep = vbt_size;
3137
3138	drm_dbg_kms(&i915->drm, "Found valid VBT in PCI ROM\n");
3139
3140	return vbt;
3141
3142err_free_vbt:
3143	kfree(vbt);
3144err_unmap_oprom:
3145	pci_unmap_rom(pdev, oprom);
3146
3147	return NULL;
3148}
3149
3150static const struct vbt_header *intel_bios_get_vbt(struct drm_i915_private *i915,
3151						   size_t *sizep)
3152{
3153	const struct vbt_header *vbt = NULL;
3154	intel_wakeref_t wakeref;
3155
3156	vbt = firmware_get_vbt(i915, sizep);
3157
3158	if (!vbt)
3159		vbt = intel_opregion_get_vbt(i915, sizep);
3160
3161	/*
3162	 * If the OpRegion does not have VBT, look in SPI flash
3163	 * through MMIO or PCI mapping
3164	 */
3165	if (!vbt && IS_DGFX(i915))
3166		with_intel_runtime_pm(&i915->runtime_pm, wakeref)
3167			vbt = spi_oprom_get_vbt(i915, sizep);
3168
3169	if (!vbt)
3170		with_intel_runtime_pm(&i915->runtime_pm, wakeref)
3171			vbt = oprom_get_vbt(i915, sizep);
3172
3173	return vbt;
3174}
3175
3176/**
3177 * intel_bios_init - find VBT and initialize settings from the BIOS
3178 * @i915: i915 device instance
3179 *
3180 * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
3181 * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
3182 * initialize some defaults if the VBT is not present at all.
3183 */
3184void intel_bios_init(struct drm_i915_private *i915)
3185{
3186	const struct vbt_header *vbt;
3187	const struct bdb_header *bdb;
3188
3189	INIT_LIST_HEAD(&i915->display.vbt.display_devices);
3190	INIT_LIST_HEAD(&i915->display.vbt.bdb_blocks);
3191
3192	if (!HAS_DISPLAY(i915)) {
3193		drm_dbg_kms(&i915->drm,
3194			    "Skipping VBT init due to disabled display.\n");
3195		return;
3196	}
3197
3198	init_vbt_defaults(i915);
3199
3200	vbt = intel_bios_get_vbt(i915, NULL);
3201
3202	if (!vbt)
3203		goto out;
3204
3205	bdb = get_bdb_header(vbt);
3206	i915->display.vbt.version = bdb->version;
3207
3208	drm_dbg_kms(&i915->drm,
3209		    "VBT signature \"%.*s\", BDB version %d\n",
3210		    (int)sizeof(vbt->signature), vbt->signature, i915->display.vbt.version);
3211
3212	init_bdb_blocks(i915, bdb);
3213
3214	/* Grab useful general definitions */
3215	parse_general_features(i915);
3216	parse_general_definitions(i915);
3217	parse_driver_features(i915);
3218
3219	/* Depends on child device list */
3220	parse_compression_parameters(i915);
3221
3222out:
3223	if (!vbt) {
3224		drm_info(&i915->drm,
3225			 "Failed to find VBIOS tables (VBT)\n");
3226		init_vbt_missing_defaults(i915);
3227	}
3228
3229	/* Further processing on pre-parsed or generated child device data */
3230	parse_sdvo_device_mapping(i915);
3231	parse_ddi_ports(i915);
3232
3233	kfree(vbt);
3234}
3235
3236static void intel_bios_init_panel(struct drm_i915_private *i915,
3237				  struct intel_panel *panel,
3238				  const struct intel_bios_encoder_data *devdata,
3239				  const struct drm_edid *drm_edid,
3240				  bool use_fallback)
3241{
3242	/* already have it? */
3243	if (panel->vbt.panel_type >= 0) {
3244		drm_WARN_ON(&i915->drm, !use_fallback);
3245		return;
3246	}
3247
3248	panel->vbt.panel_type = get_panel_type(i915, devdata,
3249					       drm_edid, use_fallback);
3250	if (panel->vbt.panel_type < 0) {
3251		drm_WARN_ON(&i915->drm, use_fallback);
3252		return;
3253	}
3254
3255	init_vbt_panel_defaults(panel);
3256
3257	parse_panel_options(i915, panel);
3258	parse_generic_dtd(i915, panel);
3259	parse_lfp_data(i915, panel);
3260	parse_lfp_backlight(i915, panel);
3261	parse_sdvo_panel_data(i915, panel);
3262	parse_panel_driver_features(i915, panel);
3263	parse_power_conservation_features(i915, panel);
3264	parse_edp(i915, panel);
3265	parse_psr(i915, panel);
3266	parse_mipi_config(i915, panel);
3267	parse_mipi_sequence(i915, panel);
3268}
3269
3270void intel_bios_init_panel_early(struct drm_i915_private *i915,
3271				 struct intel_panel *panel,
3272				 const struct intel_bios_encoder_data *devdata)
3273{
3274	intel_bios_init_panel(i915, panel, devdata, NULL, false);
3275}
3276
3277void intel_bios_init_panel_late(struct drm_i915_private *i915,
3278				struct intel_panel *panel,
3279				const struct intel_bios_encoder_data *devdata,
3280				const struct drm_edid *drm_edid)
3281{
3282	intel_bios_init_panel(i915, panel, devdata, drm_edid, true);
3283}
3284
3285/**
3286 * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
3287 * @i915: i915 device instance
3288 */
3289void intel_bios_driver_remove(struct drm_i915_private *i915)
3290{
3291	struct intel_bios_encoder_data *devdata, *nd;
3292	struct bdb_block_entry *entry, *ne;
3293
3294	list_for_each_entry_safe(devdata, nd, &i915->display.vbt.display_devices, node) {
3295		list_del(&devdata->node);
3296		kfree(devdata->dsc);
3297		kfree(devdata);
3298	}
3299
3300	list_for_each_entry_safe(entry, ne, &i915->display.vbt.bdb_blocks, node) {
3301		list_del(&entry->node);
3302		kfree(entry);
3303	}
3304}
3305
3306void intel_bios_fini_panel(struct intel_panel *panel)
3307{
3308	kfree(panel->vbt.sdvo_lvds_vbt_mode);
3309	panel->vbt.sdvo_lvds_vbt_mode = NULL;
3310	kfree(panel->vbt.lfp_lvds_vbt_mode);
3311	panel->vbt.lfp_lvds_vbt_mode = NULL;
3312	kfree(panel->vbt.dsi.data);
3313	panel->vbt.dsi.data = NULL;
3314	kfree(panel->vbt.dsi.pps);
3315	panel->vbt.dsi.pps = NULL;
3316	kfree(panel->vbt.dsi.config);
3317	panel->vbt.dsi.config = NULL;
3318	kfree(panel->vbt.dsi.deassert_seq);
3319	panel->vbt.dsi.deassert_seq = NULL;
3320}
3321
3322/**
3323 * intel_bios_is_tv_present - is integrated TV present in VBT
3324 * @i915: i915 device instance
3325 *
3326 * Return true if TV is present. If no child devices were parsed from VBT,
3327 * assume TV is present.
3328 */
3329bool intel_bios_is_tv_present(struct drm_i915_private *i915)
3330{
3331	const struct intel_bios_encoder_data *devdata;
3332
3333	if (!i915->display.vbt.int_tv_support)
3334		return false;
3335
3336	if (list_empty(&i915->display.vbt.display_devices))
3337		return true;
3338
3339	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3340		const struct child_device_config *child = &devdata->child;
3341
3342		/*
3343		 * If the device type is not TV, continue.
3344		 */
3345		switch (child->device_type) {
3346		case DEVICE_TYPE_INT_TV:
3347		case DEVICE_TYPE_TV:
3348		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
3349			break;
3350		default:
3351			continue;
3352		}
3353		/* Only when the addin_offset is non-zero, it is regarded
3354		 * as present.
3355		 */
3356		if (child->addin_offset)
3357			return true;
3358	}
3359
3360	return false;
3361}
3362
3363/**
3364 * intel_bios_is_lvds_present - is LVDS present in VBT
3365 * @i915:	i915 device instance
3366 * @i2c_pin:	i2c pin for LVDS if present
3367 *
3368 * Return true if LVDS is present. If no child devices were parsed from VBT,
3369 * assume LVDS is present.
3370 */
3371bool intel_bios_is_lvds_present(struct drm_i915_private *i915, u8 *i2c_pin)
3372{
3373	const struct intel_bios_encoder_data *devdata;
3374
3375	if (list_empty(&i915->display.vbt.display_devices))
3376		return true;
3377
3378	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3379		const struct child_device_config *child = &devdata->child;
3380
3381		/* If the device type is not LFP, continue.
3382		 * We have to check both the new identifiers as well as the
3383		 * old for compatibility with some BIOSes.
3384		 */
3385		if (child->device_type != DEVICE_TYPE_INT_LFP &&
3386		    child->device_type != DEVICE_TYPE_LFP)
3387			continue;
3388
3389		if (intel_gmbus_is_valid_pin(i915, child->i2c_pin))
3390			*i2c_pin = child->i2c_pin;
3391
3392		/* However, we cannot trust the BIOS writers to populate
3393		 * the VBT correctly.  Since LVDS requires additional
3394		 * information from AIM blocks, a non-zero addin offset is
3395		 * a good indicator that the LVDS is actually present.
3396		 */
3397		if (child->addin_offset)
3398			return true;
3399
3400		/* But even then some BIOS writers perform some black magic
3401		 * and instantiate the device without reference to any
3402		 * additional data.  Trust that if the VBT was written into
3403		 * the OpRegion then they have validated the LVDS's existence.
3404		 */
3405		return intel_opregion_vbt_present(i915);
3406	}
3407
3408	return false;
3409}
3410
3411/**
3412 * intel_bios_is_port_present - is the specified digital port present
3413 * @i915:	i915 device instance
3414 * @port:	port to check
3415 *
3416 * Return true if the device in %port is present.
3417 */
3418bool intel_bios_is_port_present(struct drm_i915_private *i915, enum port port)
3419{
3420	const struct intel_bios_encoder_data *devdata;
3421
3422	if (WARN_ON(!has_ddi_port_info(i915)))
3423		return true;
3424
3425	if (!is_port_valid(i915, port))
3426		return false;
3427
3428	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3429		const struct child_device_config *child = &devdata->child;
3430
3431		if (dvo_port_to_port(i915, child->dvo_port) == port)
3432			return true;
3433	}
3434
3435	return false;
3436}
3437
3438bool intel_bios_encoder_supports_dp_dual_mode(const struct intel_bios_encoder_data *devdata)
3439{
3440	const struct child_device_config *child = &devdata->child;
3441
3442	if (!devdata)
3443		return false;
3444
3445	if (!intel_bios_encoder_supports_dp(devdata) ||
3446	    !intel_bios_encoder_supports_hdmi(devdata))
3447		return false;
3448
3449	if (dvo_port_type(child->dvo_port) == DVO_PORT_DPA)
3450		return true;
3451
3452	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
3453	if (dvo_port_type(child->dvo_port) == DVO_PORT_HDMIA &&
3454	    child->aux_channel != 0)
3455		return true;
3456
3457	return false;
3458}
3459
3460/**
3461 * intel_bios_is_dsi_present - is DSI present in VBT
3462 * @i915:	i915 device instance
3463 * @port:	port for DSI if present
3464 *
3465 * Return true if DSI is present, and return the port in %port.
3466 */
3467bool intel_bios_is_dsi_present(struct drm_i915_private *i915,
3468			       enum port *port)
3469{
3470	const struct intel_bios_encoder_data *devdata;
3471
3472	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3473		const struct child_device_config *child = &devdata->child;
3474		u8 dvo_port = child->dvo_port;
3475
3476		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
3477			continue;
3478
3479		if (dsi_dvo_port_to_port(i915, dvo_port) == PORT_NONE) {
3480			drm_dbg_kms(&i915->drm,
3481				    "VBT has unsupported DSI port %c\n",
3482				    port_name(dvo_port - DVO_PORT_MIPIA));
3483			continue;
3484		}
3485
3486		if (port)
3487			*port = dsi_dvo_port_to_port(i915, dvo_port);
3488		return true;
3489	}
3490
3491	return false;
3492}
3493
3494static void fill_dsc(struct intel_crtc_state *crtc_state,
3495		     struct dsc_compression_parameters_entry *dsc,
3496		     int dsc_max_bpc)
3497{
3498	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
3499	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
3500	int bpc = 8;
3501
3502	vdsc_cfg->dsc_version_major = dsc->version_major;
3503	vdsc_cfg->dsc_version_minor = dsc->version_minor;
3504
3505	if (dsc->support_12bpc && dsc_max_bpc >= 12)
3506		bpc = 12;
3507	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
3508		bpc = 10;
3509	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
3510		bpc = 8;
3511	else
3512		drm_dbg_kms(&i915->drm, "VBT: Unsupported BPC %d for DCS\n",
3513			    dsc_max_bpc);
3514
3515	crtc_state->pipe_bpp = bpc * 3;
3516
3517	crtc_state->dsc.compressed_bpp_x16 = to_bpp_x16(min(crtc_state->pipe_bpp,
3518							    VBT_DSC_MAX_BPP(dsc->max_bpp)));
3519
3520	/*
3521	 * FIXME: This is ugly, and slice count should take DSC engine
3522	 * throughput etc. into account.
3523	 *
3524	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
3525	 */
3526	if (dsc->slices_per_line & BIT(2)) {
3527		crtc_state->dsc.slice_count = 4;
3528	} else if (dsc->slices_per_line & BIT(1)) {
3529		crtc_state->dsc.slice_count = 2;
3530	} else {
3531		/* FIXME */
3532		if (!(dsc->slices_per_line & BIT(0)))
3533			drm_dbg_kms(&i915->drm, "VBT: Unsupported DSC slice count for DSI\n");
3534
3535		crtc_state->dsc.slice_count = 1;
3536	}
3537
3538	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
3539	    crtc_state->dsc.slice_count != 0)
3540		drm_dbg_kms(&i915->drm, "VBT: DSC hdisplay %d not divisible by slice count %d\n",
3541			    crtc_state->hw.adjusted_mode.crtc_hdisplay,
3542			    crtc_state->dsc.slice_count);
3543
3544	/*
3545	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
3546	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63.
3547	 */
3548	vdsc_cfg->rc_model_size = drm_dsc_dp_rc_buffer_size(dsc->rc_buffer_block_size,
3549							    dsc->rc_buffer_size);
3550
3551	/* FIXME: DSI spec says bpc + 1 for this one */
3552	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
3553
3554	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
3555
3556	vdsc_cfg->slice_height = dsc->slice_height;
3557}
3558
3559/* FIXME: initially DSI specific */
3560bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
3561			       struct intel_crtc_state *crtc_state,
3562			       int dsc_max_bpc)
3563{
3564	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3565	const struct intel_bios_encoder_data *devdata;
3566
3567	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3568		const struct child_device_config *child = &devdata->child;
3569
3570		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
3571			continue;
3572
3573		if (dsi_dvo_port_to_port(i915, child->dvo_port) == encoder->port) {
3574			if (!devdata->dsc)
3575				return false;
3576
3577			fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
3578
3579			return true;
3580		}
3581	}
3582
3583	return false;
3584}
3585
3586static const u8 adlp_aux_ch_map[] = {
3587	[AUX_CH_A] = DP_AUX_A,
3588	[AUX_CH_B] = DP_AUX_B,
3589	[AUX_CH_C] = DP_AUX_C,
3590	[AUX_CH_D_XELPD] = DP_AUX_D,
3591	[AUX_CH_E_XELPD] = DP_AUX_E,
3592	[AUX_CH_USBC1] = DP_AUX_F,
3593	[AUX_CH_USBC2] = DP_AUX_G,
3594	[AUX_CH_USBC3] = DP_AUX_H,
3595	[AUX_CH_USBC4] = DP_AUX_I,
3596};
3597
3598/*
3599 * ADL-S VBT uses PHY based mapping. Combo PHYs A,B,C,D,E
3600 * map to DDI A,TC1,TC2,TC3,TC4 respectively.
3601 */
3602static const u8 adls_aux_ch_map[] = {
3603	[AUX_CH_A] = DP_AUX_A,
3604	[AUX_CH_USBC1] = DP_AUX_B,
3605	[AUX_CH_USBC2] = DP_AUX_C,
3606	[AUX_CH_USBC3] = DP_AUX_D,
3607	[AUX_CH_USBC4] = DP_AUX_E,
3608};
3609
3610/*
3611 * RKL/DG1 VBT uses PHY based mapping. Combo PHYs A,B,C,D
3612 * map to DDI A,B,TC1,TC2 respectively.
3613 */
3614static const u8 rkl_aux_ch_map[] = {
3615	[AUX_CH_A] = DP_AUX_A,
3616	[AUX_CH_B] = DP_AUX_B,
3617	[AUX_CH_USBC1] = DP_AUX_C,
3618	[AUX_CH_USBC2] = DP_AUX_D,
3619};
3620
3621static const u8 direct_aux_ch_map[] = {
3622	[AUX_CH_A] = DP_AUX_A,
3623	[AUX_CH_B] = DP_AUX_B,
3624	[AUX_CH_C] = DP_AUX_C,
3625	[AUX_CH_D] = DP_AUX_D, /* aka AUX_CH_USBC1 */
3626	[AUX_CH_E] = DP_AUX_E, /* aka AUX_CH_USBC2 */
3627	[AUX_CH_F] = DP_AUX_F, /* aka AUX_CH_USBC3 */
3628	[AUX_CH_G] = DP_AUX_G, /* aka AUX_CH_USBC4 */
3629	[AUX_CH_H] = DP_AUX_H, /* aka AUX_CH_USBC5 */
3630	[AUX_CH_I] = DP_AUX_I, /* aka AUX_CH_USBC6 */
3631};
3632
3633static enum aux_ch map_aux_ch(struct drm_i915_private *i915, u8 aux_channel)
3634{
3635	const u8 *aux_ch_map;
3636	int i, n_entries;
3637
3638	if (DISPLAY_VER(i915) >= 13) {
3639		aux_ch_map = adlp_aux_ch_map;
3640		n_entries = ARRAY_SIZE(adlp_aux_ch_map);
3641	} else if (IS_ALDERLAKE_S(i915)) {
3642		aux_ch_map = adls_aux_ch_map;
3643		n_entries = ARRAY_SIZE(adls_aux_ch_map);
3644	} else if (IS_DG1(i915) || IS_ROCKETLAKE(i915)) {
3645		aux_ch_map = rkl_aux_ch_map;
3646		n_entries = ARRAY_SIZE(rkl_aux_ch_map);
3647	} else {
3648		aux_ch_map = direct_aux_ch_map;
3649		n_entries = ARRAY_SIZE(direct_aux_ch_map);
3650	}
3651
3652	for (i = 0; i < n_entries; i++) {
3653		if (aux_ch_map[i] == aux_channel)
3654			return i;
3655	}
3656
3657	drm_dbg_kms(&i915->drm,
3658		    "Ignoring alternate AUX CH: VBT claims AUX 0x%x, which is not valid for this platform\n",
3659		    aux_channel);
3660
3661	return AUX_CH_NONE;
3662}
3663
3664enum aux_ch intel_bios_dp_aux_ch(const struct intel_bios_encoder_data *devdata)
3665{
3666	if (!devdata || !devdata->child.aux_channel)
3667		return AUX_CH_NONE;
3668
3669	return map_aux_ch(devdata->i915, devdata->child.aux_channel);
3670}
3671
3672bool intel_bios_dp_has_shared_aux_ch(const struct intel_bios_encoder_data *devdata)
3673{
3674	struct drm_i915_private *i915;
3675	u8 aux_channel;
3676	int count = 0;
3677
3678	if (!devdata || !devdata->child.aux_channel)
3679		return false;
3680
3681	i915 = devdata->i915;
3682	aux_channel = devdata->child.aux_channel;
3683
3684	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3685		if (intel_bios_encoder_supports_dp(devdata) &&
3686		    aux_channel == devdata->child.aux_channel)
3687			count++;
3688	}
3689
3690	return count > 1;
3691}
3692
3693int intel_bios_dp_boost_level(const struct intel_bios_encoder_data *devdata)
3694{
3695	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3696		return 0;
3697
3698	return translate_iboost(devdata->i915, devdata->child.dp_iboost_level);
3699}
3700
3701int intel_bios_hdmi_boost_level(const struct intel_bios_encoder_data *devdata)
3702{
3703	if (!devdata || devdata->i915->display.vbt.version < 196 || !devdata->child.iboost)
3704		return 0;
3705
3706	return translate_iboost(devdata->i915, devdata->child.hdmi_iboost_level);
3707}
3708
3709int intel_bios_hdmi_ddc_pin(const struct intel_bios_encoder_data *devdata)
3710{
3711	if (!devdata || !devdata->child.ddc_pin)
3712		return 0;
3713
3714	return map_ddc_pin(devdata->i915, devdata->child.ddc_pin);
3715}
3716
3717bool intel_bios_encoder_supports_typec_usb(const struct intel_bios_encoder_data *devdata)
3718{
3719	return devdata->i915->display.vbt.version >= 195 && devdata->child.dp_usb_type_c;
3720}
3721
3722bool intel_bios_encoder_supports_tbt(const struct intel_bios_encoder_data *devdata)
3723{
3724	return devdata->i915->display.vbt.version >= 209 && devdata->child.tbt;
3725}
3726
3727bool intel_bios_encoder_lane_reversal(const struct intel_bios_encoder_data *devdata)
3728{
3729	return devdata && devdata->child.lane_reversal;
3730}
3731
3732bool intel_bios_encoder_hpd_invert(const struct intel_bios_encoder_data *devdata)
3733{
3734	return devdata && devdata->child.hpd_invert;
3735}
3736
3737const struct intel_bios_encoder_data *
3738intel_bios_encoder_data_lookup(struct drm_i915_private *i915, enum port port)
3739{
3740	struct intel_bios_encoder_data *devdata;
3741
3742	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node) {
3743		if (intel_bios_encoder_port(devdata) == port)
3744			return devdata;
3745	}
3746
3747	return NULL;
3748}
3749
3750void intel_bios_for_each_encoder(struct drm_i915_private *i915,
3751				 void (*func)(struct drm_i915_private *i915,
3752					      const struct intel_bios_encoder_data *devdata))
3753{
3754	struct intel_bios_encoder_data *devdata;
3755
3756	list_for_each_entry(devdata, &i915->display.vbt.display_devices, node)
3757		func(i915, devdata);
3758}
3759
3760static int intel_bios_vbt_show(struct seq_file *m, void *unused)
3761{
3762	struct drm_i915_private *i915 = m->private;
3763	const void *vbt;
3764	size_t vbt_size;
3765
3766	vbt = intel_bios_get_vbt(i915, &vbt_size);
3767
3768	if (vbt) {
3769		seq_write(m, vbt, vbt_size);
3770		kfree(vbt);
3771	}
3772
3773	return 0;
3774}
3775
3776DEFINE_SHOW_ATTRIBUTE(intel_bios_vbt);
3777
3778void intel_bios_debugfs_register(struct drm_i915_private *i915)
3779{
3780	struct drm_minor *minor = i915->drm.primary;
3781
3782	debugfs_create_file("i915_vbt", 0444, minor->debugfs_root,
3783			    i915, &intel_bios_vbt_fops);
3784}
3785