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