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