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