xref: /dragonfly/sys/dev/drm/i915/intel_bios.c (revision 38b5d46c)
1 /*
2  * Copyright © 2006 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 #include <drm/drm_dp_helper.h>
29 #include <drm/drmP.h>
30 #include <drm/i915_drm.h>
31 #include "i915_drv.h"
32 #include "intel_bios.h"
33 
34 #define	SLAVE_ADDR1	0x70
35 #define	SLAVE_ADDR2	0x72
36 
37 static int panel_type;
38 
39 static const void *
40 find_section(const void *_bdb, int section_id)
41 {
42 	const struct bdb_header *bdb = _bdb;
43 	const u8 *base = _bdb;
44 	int index = 0;
45 	u32 total, current_size;
46 	u8 current_id;
47 
48 	/* skip to first section */
49 	index += bdb->header_size;
50 	total = bdb->bdb_size;
51 
52 	/* walk the sections looking for section_id */
53 	while (index + 3 < total) {
54 		current_id = *(base + index);
55 		index++;
56 
57 		current_size = *((const u16 *)(base + index));
58 		index += 2;
59 
60 		/* The MIPI Sequence Block v3+ has a separate size field. */
61 		if (current_id == BDB_MIPI_SEQUENCE && *(base + index) >= 3)
62 			current_size = *((const u32 *)(base + index + 1));
63 
64 		if (index + current_size > total)
65 			return NULL;
66 
67 		if (current_id == section_id)
68 			return base + index;
69 
70 		index += current_size;
71 	}
72 
73 	return NULL;
74 }
75 
76 #pragma GCC diagnostic ignored "-Wcast-qual"
77 static u16
78 get_blocksize(const void *p)
79 {
80 	u16 *block_ptr, block_size;
81 
82 	block_ptr = (u16 *)((char *)p - 2);
83 	block_size = *block_ptr;
84 	return block_size;
85 }
86 #pragma GCC diagnostic pop
87 
88 static void
89 fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
90 			const struct lvds_dvo_timing *dvo_timing)
91 {
92 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
93 		dvo_timing->hactive_lo;
94 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
95 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
96 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
97 		dvo_timing->hsync_pulse_width;
98 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
99 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
100 
101 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
102 		dvo_timing->vactive_lo;
103 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
104 		dvo_timing->vsync_off;
105 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
106 		dvo_timing->vsync_pulse_width;
107 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
108 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
109 	panel_fixed_mode->clock = dvo_timing->clock * 10;
110 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
111 
112 	if (dvo_timing->hsync_positive)
113 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
114 	else
115 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
116 
117 	if (dvo_timing->vsync_positive)
118 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
119 	else
120 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
121 
122 	/* Some VBTs have bogus h/vtotal values */
123 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
124 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
125 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
126 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
127 
128 	drm_mode_set_name(panel_fixed_mode);
129 }
130 
131 static const struct lvds_dvo_timing *
132 get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *lvds_lfp_data,
133 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs,
134 		    int index)
135 {
136 	/*
137 	 * the size of fp_timing varies on the different platform.
138 	 * So calculate the DVO timing relative offset in LVDS data
139 	 * entry to get the DVO timing entry
140 	 */
141 
142 	int lfp_data_size =
143 		lvds_lfp_data_ptrs->ptr[1].dvo_timing_offset -
144 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset;
145 	int dvo_timing_offset =
146 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset -
147 		lvds_lfp_data_ptrs->ptr[0].fp_timing_offset;
148 	const char *entry = (const char *)lvds_lfp_data->data + lfp_data_size * index;
149 
150 	return (const struct lvds_dvo_timing *)(entry + dvo_timing_offset);
151 }
152 
153 /* get lvds_fp_timing entry
154  * this function may return NULL if the corresponding entry is invalid
155  */
156 static const struct lvds_fp_timing *
157 get_lvds_fp_timing(const struct bdb_header *bdb,
158 		   const struct bdb_lvds_lfp_data *data,
159 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
160 		   int index)
161 {
162 	size_t data_ofs = (const u8 *)data - (const u8 *)bdb;
163 	u16 data_size = ((const u16 *)data)[-1]; /* stored in header */
164 	size_t ofs;
165 
166 	if (index >= ARRAY_SIZE(ptrs->ptr))
167 		return NULL;
168 	ofs = ptrs->ptr[index].fp_timing_offset;
169 	if (ofs < data_ofs ||
170 	    ofs + sizeof(struct lvds_fp_timing) > data_ofs + data_size)
171 		return NULL;
172 	return (const struct lvds_fp_timing *)((const u8 *)bdb + ofs);
173 }
174 
175 /* Try to find integrated panel data */
176 static void
177 parse_lfp_panel_data(struct drm_i915_private *dev_priv,
178 		     const struct bdb_header *bdb)
179 {
180 	const struct bdb_lvds_options *lvds_options;
181 	const struct bdb_lvds_lfp_data *lvds_lfp_data;
182 	const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs;
183 	const struct lvds_dvo_timing *panel_dvo_timing;
184 	const struct lvds_fp_timing *fp_timing;
185 	struct drm_display_mode *panel_fixed_mode;
186 	int drrs_mode;
187 
188 	lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
189 	if (!lvds_options)
190 		return;
191 
192 	dev_priv->vbt.lvds_dither = lvds_options->pixel_dither;
193 	if (lvds_options->panel_type == 0xff)
194 		return;
195 
196 	panel_type = lvds_options->panel_type;
197 
198 	drrs_mode = (lvds_options->dps_panel_type_bits
199 				>> (panel_type * 2)) & MODE_MASK;
200 	/*
201 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
202 	 * The below piece of code is required to adjust vbt.drrs_type
203 	 * to match the enum drrs_support_type.
204 	 */
205 	switch (drrs_mode) {
206 	case 0:
207 		dev_priv->vbt.drrs_type = STATIC_DRRS_SUPPORT;
208 		DRM_DEBUG_KMS("DRRS supported mode is static\n");
209 		break;
210 	case 2:
211 		dev_priv->vbt.drrs_type = SEAMLESS_DRRS_SUPPORT;
212 		DRM_DEBUG_KMS("DRRS supported mode is seamless\n");
213 		break;
214 	default:
215 		dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
216 		DRM_DEBUG_KMS("DRRS not supported (VBT input)\n");
217 		break;
218 	}
219 
220 	lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
221 	if (!lvds_lfp_data)
222 		return;
223 
224 	lvds_lfp_data_ptrs = find_section(bdb, BDB_LVDS_LFP_DATA_PTRS);
225 	if (!lvds_lfp_data_ptrs)
226 		return;
227 
228 	dev_priv->vbt.lvds_vbt = 1;
229 
230 	panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
231 					       lvds_lfp_data_ptrs,
232 					       lvds_options->panel_type);
233 
234 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
235 	if (!panel_fixed_mode)
236 		return;
237 
238 	fill_detail_timing_data(panel_fixed_mode, panel_dvo_timing);
239 
240 	dev_priv->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
241 
242 	DRM_DEBUG_KMS("Found panel mode in BIOS VBT tables:\n");
243 	drm_mode_debug_printmodeline(panel_fixed_mode);
244 
245 	fp_timing = get_lvds_fp_timing(bdb, lvds_lfp_data,
246 				       lvds_lfp_data_ptrs,
247 				       lvds_options->panel_type);
248 	if (fp_timing) {
249 		/* check the resolution, just to be sure */
250 		if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
251 		    fp_timing->y_res == panel_fixed_mode->vdisplay) {
252 			dev_priv->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
253 			DRM_DEBUG_KMS("VBT initial LVDS value %x\n",
254 				      dev_priv->vbt.bios_lvds_val);
255 		}
256 	}
257 }
258 
259 static void
260 parse_lfp_backlight(struct drm_i915_private *dev_priv,
261 		    const struct bdb_header *bdb)
262 {
263 	const struct bdb_lfp_backlight_data *backlight_data;
264 	const struct bdb_lfp_backlight_data_entry *entry;
265 
266 	backlight_data = find_section(bdb, BDB_LVDS_BACKLIGHT);
267 	if (!backlight_data)
268 		return;
269 
270 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
271 		DRM_DEBUG_KMS("Unsupported backlight data entry size %u\n",
272 			      backlight_data->entry_size);
273 		return;
274 	}
275 
276 	entry = &backlight_data->data[panel_type];
277 
278 	dev_priv->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
279 	if (!dev_priv->vbt.backlight.present) {
280 		DRM_DEBUG_KMS("PWM backlight not present in VBT (type %u)\n",
281 			      entry->type);
282 		return;
283 	}
284 
285 	dev_priv->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
286 	dev_priv->vbt.backlight.active_low_pwm = entry->active_low_pwm;
287 	dev_priv->vbt.backlight.min_brightness = entry->min_brightness;
288 	DRM_DEBUG_KMS("VBT backlight PWM modulation frequency %u Hz, "
289 		      "active %s, min brightness %u, level %u\n",
290 		      dev_priv->vbt.backlight.pwm_freq_hz,
291 		      dev_priv->vbt.backlight.active_low_pwm ? "low" : "high",
292 		      dev_priv->vbt.backlight.min_brightness,
293 		      backlight_data->level[panel_type]);
294 }
295 
296 /* Try to find sdvo panel data */
297 static void
298 parse_sdvo_panel_data(struct drm_i915_private *dev_priv,
299 		      const struct bdb_header *bdb)
300 {
301 	const struct lvds_dvo_timing *dvo_timing;
302 	struct drm_display_mode *panel_fixed_mode;
303 	int index;
304 
305 	index = i915.vbt_sdvo_panel_type;
306 	if (index == -2) {
307 		DRM_DEBUG_KMS("Ignore SDVO panel mode from BIOS VBT tables.\n");
308 		return;
309 	}
310 
311 	if (index == -1) {
312 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
313 
314 		sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
315 		if (!sdvo_lvds_options)
316 			return;
317 
318 		index = sdvo_lvds_options->panel_type;
319 	}
320 
321 	dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
322 	if (!dvo_timing)
323 		return;
324 
325 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
326 	if (!panel_fixed_mode)
327 		return;
328 
329 	fill_detail_timing_data(panel_fixed_mode, dvo_timing + index);
330 
331 	dev_priv->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
332 
333 	DRM_DEBUG_KMS("Found SDVO panel mode in BIOS VBT tables:\n");
334 	drm_mode_debug_printmodeline(panel_fixed_mode);
335 }
336 
337 static int intel_bios_ssc_frequency(struct drm_i915_private *dev_priv,
338 				    bool alternate)
339 {
340 	switch (INTEL_INFO(dev_priv)->gen) {
341 	case 2:
342 		return alternate ? 66667 : 48000;
343 	case 3:
344 	case 4:
345 		return alternate ? 100000 : 96000;
346 	default:
347 		return alternate ? 100000 : 120000;
348 	}
349 }
350 
351 static void
352 parse_general_features(struct drm_i915_private *dev_priv,
353 		       const struct bdb_header *bdb)
354 {
355 	const struct bdb_general_features *general;
356 
357 	general = find_section(bdb, BDB_GENERAL_FEATURES);
358 	if (!general)
359 		return;
360 
361 	dev_priv->vbt.int_tv_support = general->int_tv_support;
362 	/* int_crt_support can't be trusted on earlier platforms */
363 	if (bdb->version >= 155 &&
364 	    (HAS_DDI(dev_priv) || IS_VALLEYVIEW(dev_priv)))
365 		dev_priv->vbt.int_crt_support = general->int_crt_support;
366 	dev_priv->vbt.lvds_use_ssc = general->enable_ssc;
367 	dev_priv->vbt.lvds_ssc_freq =
368 		intel_bios_ssc_frequency(dev_priv, general->ssc_freq);
369 	dev_priv->vbt.display_clock_mode = general->display_clock_mode;
370 	dev_priv->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
371 	DRM_DEBUG_KMS("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",
372 		      dev_priv->vbt.int_tv_support,
373 		      dev_priv->vbt.int_crt_support,
374 		      dev_priv->vbt.lvds_use_ssc,
375 		      dev_priv->vbt.lvds_ssc_freq,
376 		      dev_priv->vbt.display_clock_mode,
377 		      dev_priv->vbt.fdi_rx_polarity_inverted);
378 }
379 
380 static void
381 parse_general_definitions(struct drm_i915_private *dev_priv,
382 			  const struct bdb_header *bdb)
383 {
384 	const struct bdb_general_definitions *general;
385 
386 	general = find_section(bdb, BDB_GENERAL_DEFINITIONS);
387 	if (general) {
388 		u16 block_size = get_blocksize(general);
389 		if (block_size >= sizeof(*general)) {
390 			int bus_pin = general->crt_ddc_gmbus_pin;
391 			DRM_DEBUG_KMS("crt_ddc_bus_pin: %d\n", bus_pin);
392 			if (intel_gmbus_is_valid_pin(dev_priv, bus_pin))
393 				dev_priv->vbt.crt_ddc_pin = bus_pin;
394 		} else {
395 			DRM_DEBUG_KMS("BDB_GD too small (%d). Invalid.\n",
396 				      block_size);
397 		}
398 	}
399 }
400 
401 static const union child_device_config *
402 child_device_ptr(const struct bdb_general_definitions *p_defs, int i)
403 {
404 	return (const void *) &p_defs->devices[i * p_defs->child_dev_size];
405 }
406 
407 static void
408 parse_sdvo_device_mapping(struct drm_i915_private *dev_priv,
409 			  const struct bdb_header *bdb)
410 {
411 	struct sdvo_device_mapping *p_mapping;
412 	const struct bdb_general_definitions *p_defs;
413 	const struct old_child_dev_config *child; /* legacy */
414 	int i, child_device_num, count;
415 	u16	block_size;
416 
417 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
418 	if (!p_defs) {
419 		DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
420 		return;
421 	}
422 
423 	/*
424 	 * Only parse SDVO mappings when the general definitions block child
425 	 * device size matches that of the *legacy* child device config
426 	 * struct. Thus, SDVO mapping will be skipped for newer VBT.
427 	 */
428 	if (p_defs->child_dev_size != sizeof(*child)) {
429 		DRM_DEBUG_KMS("Unsupported child device size for SDVO mapping.\n");
430 		return;
431 	}
432 	/* get the block size of general definitions */
433 	block_size = get_blocksize(p_defs);
434 	/* get the number of child device */
435 	child_device_num = (block_size - sizeof(*p_defs)) /
436 		p_defs->child_dev_size;
437 	count = 0;
438 	for (i = 0; i < child_device_num; i++) {
439 		child = &child_device_ptr(p_defs, i)->old;
440 		if (!child->device_type) {
441 			/* skip the device block if device type is invalid */
442 			continue;
443 		}
444 		if (child->slave_addr != SLAVE_ADDR1 &&
445 		    child->slave_addr != SLAVE_ADDR2) {
446 			/*
447 			 * If the slave address is neither 0x70 nor 0x72,
448 			 * it is not a SDVO device. Skip it.
449 			 */
450 			continue;
451 		}
452 		if (child->dvo_port != DEVICE_PORT_DVOB &&
453 		    child->dvo_port != DEVICE_PORT_DVOC) {
454 			/* skip the incorrect SDVO port */
455 			DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
456 			continue;
457 		}
458 		DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
459 			      " %s port\n",
460 			      child->slave_addr,
461 			      (child->dvo_port == DEVICE_PORT_DVOB) ?
462 			      "SDVOB" : "SDVOC");
463 		p_mapping = &(dev_priv->sdvo_mappings[child->dvo_port - 1]);
464 		if (!p_mapping->initialized) {
465 			p_mapping->dvo_port = child->dvo_port;
466 			p_mapping->slave_addr = child->slave_addr;
467 			p_mapping->dvo_wiring = child->dvo_wiring;
468 			p_mapping->ddc_pin = child->ddc_pin;
469 			p_mapping->i2c_pin = child->i2c_pin;
470 			p_mapping->initialized = 1;
471 			DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
472 				      p_mapping->dvo_port,
473 				      p_mapping->slave_addr,
474 				      p_mapping->dvo_wiring,
475 				      p_mapping->ddc_pin,
476 				      p_mapping->i2c_pin);
477 		} else {
478 			DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
479 					 "two SDVO device.\n");
480 		}
481 		if (child->slave2_addr) {
482 			/* Maybe this is a SDVO device with multiple inputs */
483 			/* And the mapping info is not added */
484 			DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
485 				" is a SDVO device with multiple inputs.\n");
486 		}
487 		count++;
488 	}
489 
490 	if (!count) {
491 		/* No SDVO device info is found */
492 		DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
493 	}
494 	return;
495 }
496 
497 static void
498 parse_driver_features(struct drm_i915_private *dev_priv,
499 		      const struct bdb_header *bdb)
500 {
501 	const struct bdb_driver_features *driver;
502 
503 	driver = find_section(bdb, BDB_DRIVER_FEATURES);
504 	if (!driver)
505 		return;
506 
507 	if (driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
508 		dev_priv->vbt.edp_support = 1;
509 
510 	if (driver->dual_frequency)
511 		dev_priv->render_reclock_avail = true;
512 
513 	DRM_DEBUG_KMS("DRRS State Enabled:%d\n", driver->drrs_enabled);
514 	/*
515 	 * If DRRS is not supported, drrs_type has to be set to 0.
516 	 * This is because, VBT is configured in such a way that
517 	 * static DRRS is 0 and DRRS not supported is represented by
518 	 * driver->drrs_enabled=false
519 	 */
520 	if (!driver->drrs_enabled)
521 		dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
522 }
523 
524 static void
525 parse_edp(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
526 {
527 	const struct bdb_edp *edp;
528 	const struct edp_power_seq *edp_pps;
529 	const struct edp_link_params *edp_link_params;
530 
531 	edp = find_section(bdb, BDB_EDP);
532 	if (!edp) {
533 		if (dev_priv->vbt.edp_support)
534 			DRM_DEBUG_KMS("No eDP BDB found but eDP panel supported.\n");
535 		return;
536 	}
537 
538 	switch ((edp->color_depth >> (panel_type * 2)) & 3) {
539 	case EDP_18BPP:
540 		dev_priv->vbt.edp_bpp = 18;
541 		break;
542 	case EDP_24BPP:
543 		dev_priv->vbt.edp_bpp = 24;
544 		break;
545 	case EDP_30BPP:
546 		dev_priv->vbt.edp_bpp = 30;
547 		break;
548 	}
549 
550 	/* Get the eDP sequencing and link info */
551 	edp_pps = &edp->power_seqs[panel_type];
552 	edp_link_params = &edp->link_params[panel_type];
553 
554 	dev_priv->vbt.edp_pps = *edp_pps;
555 
556 	switch (edp_link_params->rate) {
557 	case EDP_RATE_1_62:
558 		dev_priv->vbt.edp_rate = DP_LINK_BW_1_62;
559 		break;
560 	case EDP_RATE_2_7:
561 		dev_priv->vbt.edp_rate = DP_LINK_BW_2_7;
562 		break;
563 	default:
564 		DRM_DEBUG_KMS("VBT has unknown eDP link rate value %u\n",
565 			      edp_link_params->rate);
566 		break;
567 	}
568 
569 	switch (edp_link_params->lanes) {
570 	case EDP_LANE_1:
571 		dev_priv->vbt.edp_lanes = 1;
572 		break;
573 	case EDP_LANE_2:
574 		dev_priv->vbt.edp_lanes = 2;
575 		break;
576 	case EDP_LANE_4:
577 		dev_priv->vbt.edp_lanes = 4;
578 		break;
579 	default:
580 		DRM_DEBUG_KMS("VBT has unknown eDP lane count value %u\n",
581 			      edp_link_params->lanes);
582 		break;
583 	}
584 
585 	switch (edp_link_params->preemphasis) {
586 	case EDP_PREEMPHASIS_NONE:
587 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
588 		break;
589 	case EDP_PREEMPHASIS_3_5dB:
590 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
591 		break;
592 	case EDP_PREEMPHASIS_6dB:
593 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
594 		break;
595 	case EDP_PREEMPHASIS_9_5dB:
596 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
597 		break;
598 	default:
599 		DRM_DEBUG_KMS("VBT has unknown eDP pre-emphasis value %u\n",
600 			      edp_link_params->preemphasis);
601 		break;
602 	}
603 
604 	switch (edp_link_params->vswing) {
605 	case EDP_VSWING_0_4V:
606 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
607 		break;
608 	case EDP_VSWING_0_6V:
609 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
610 		break;
611 	case EDP_VSWING_0_8V:
612 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
613 		break;
614 	case EDP_VSWING_1_2V:
615 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
616 		break;
617 	default:
618 		DRM_DEBUG_KMS("VBT has unknown eDP voltage swing value %u\n",
619 			      edp_link_params->vswing);
620 		break;
621 	}
622 
623 	if (bdb->version >= 173) {
624 		uint8_t vswing;
625 
626 		/* Don't read from VBT if module parameter has valid value*/
627 		if (i915.edp_vswing) {
628 			dev_priv->edp_low_vswing = i915.edp_vswing == 1;
629 		} else {
630 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
631 			dev_priv->edp_low_vswing = vswing == 0;
632 		}
633 	}
634 }
635 
636 static void
637 parse_psr(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
638 {
639 	const struct bdb_psr *psr;
640 	const struct psr_table *psr_table;
641 
642 	psr = find_section(bdb, BDB_PSR);
643 	if (!psr) {
644 		DRM_DEBUG_KMS("No PSR BDB found.\n");
645 		return;
646 	}
647 
648 	psr_table = &psr->psr_table[panel_type];
649 
650 	dev_priv->vbt.psr.full_link = psr_table->full_link;
651 	dev_priv->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
652 
653 	/* Allowed VBT values goes from 0 to 15 */
654 	dev_priv->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
655 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
656 
657 	switch (psr_table->lines_to_wait) {
658 	case 0:
659 		dev_priv->vbt.psr.lines_to_wait = PSR_0_LINES_TO_WAIT;
660 		break;
661 	case 1:
662 		dev_priv->vbt.psr.lines_to_wait = PSR_1_LINE_TO_WAIT;
663 		break;
664 	case 2:
665 		dev_priv->vbt.psr.lines_to_wait = PSR_4_LINES_TO_WAIT;
666 		break;
667 	case 3:
668 		dev_priv->vbt.psr.lines_to_wait = PSR_8_LINES_TO_WAIT;
669 		break;
670 	default:
671 		DRM_DEBUG_KMS("VBT has unknown PSR lines to wait %u\n",
672 			      psr_table->lines_to_wait);
673 		break;
674 	}
675 
676 	dev_priv->vbt.psr.tp1_wakeup_time = psr_table->tp1_wakeup_time;
677 	dev_priv->vbt.psr.tp2_tp3_wakeup_time = psr_table->tp2_tp3_wakeup_time;
678 }
679 
680 static u8 *goto_next_sequence(u8 *data, int *size)
681 {
682 	u16 len;
683 	int tmp = *size;
684 
685 	if (--tmp < 0)
686 		return NULL;
687 
688 	/* goto first element */
689 	data++;
690 	while (1) {
691 		switch (*data) {
692 		case MIPI_SEQ_ELEM_SEND_PKT:
693 			/*
694 			 * skip by this element payload size
695 			 * skip elem id, command flag and data type
696 			 */
697 			tmp -= 5;
698 			if (tmp < 0)
699 				return NULL;
700 
701 			data += 3;
702 			len = *((u16 *)data);
703 
704 			tmp -= len;
705 			if (tmp < 0)
706 				return NULL;
707 
708 			/* skip by len */
709 			data = data + 2 + len;
710 			break;
711 		case MIPI_SEQ_ELEM_DELAY:
712 			/* skip by elem id, and delay is 4 bytes */
713 			tmp -= 5;
714 			if (tmp < 0)
715 				return NULL;
716 
717 			data += 5;
718 			break;
719 		case MIPI_SEQ_ELEM_GPIO:
720 			tmp -= 3;
721 			if (tmp < 0)
722 				return NULL;
723 
724 			data += 3;
725 			break;
726 		default:
727 			DRM_ERROR("Unknown element\n");
728 			return NULL;
729 		}
730 
731 		/* end of sequence ? */
732 		if (*data == 0)
733 			break;
734 	}
735 
736 	/* goto next sequence or end of block byte */
737 	if (--tmp < 0)
738 		return NULL;
739 
740 	data++;
741 
742 	/* update amount of data left for the sequence block to be parsed */
743 	*size = tmp;
744 	return data;
745 }
746 
747 static void
748 parse_mipi(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
749 {
750 	const struct bdb_mipi_config *start;
751 	const struct bdb_mipi_sequence *sequence;
752 	const struct mipi_config *config;
753 	const struct mipi_pps_data *pps;
754 	u8 *data;
755 	const u8 *seq_data;
756 	int i, panel_id, seq_size;
757 	u16 block_size;
758 
759 	/* parse MIPI blocks only if LFP type is MIPI */
760 	if (!dev_priv->vbt.has_mipi)
761 		return;
762 
763 	/* Initialize this to undefined indicating no generic MIPI support */
764 	dev_priv->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
765 
766 	/* Block #40 is already parsed and panel_fixed_mode is
767 	 * stored in dev_priv->lfp_lvds_vbt_mode
768 	 * resuse this when needed
769 	 */
770 
771 	/* Parse #52 for panel index used from panel_type already
772 	 * parsed
773 	 */
774 	start = find_section(bdb, BDB_MIPI_CONFIG);
775 	if (!start) {
776 		DRM_DEBUG_KMS("No MIPI config BDB found");
777 		return;
778 	}
779 
780 	DRM_DEBUG_DRIVER("Found MIPI Config block, panel index = %d\n",
781 								panel_type);
782 
783 	/*
784 	 * get hold of the correct configuration block and pps data as per
785 	 * the panel_type as index
786 	 */
787 	config = &start->config[panel_type];
788 	pps = &start->pps[panel_type];
789 
790 	/* store as of now full data. Trim when we realise all is not needed */
791 	dev_priv->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
792 	if (!dev_priv->vbt.dsi.config)
793 		return;
794 
795 	dev_priv->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
796 	if (!dev_priv->vbt.dsi.pps) {
797 		kfree(dev_priv->vbt.dsi.config);
798 		return;
799 	}
800 
801 	/* We have mandatory mipi config blocks. Initialize as generic panel */
802 	dev_priv->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
803 
804 	/* Check if we have sequence block as well */
805 	sequence = find_section(bdb, BDB_MIPI_SEQUENCE);
806 	if (!sequence) {
807 		DRM_DEBUG_KMS("No MIPI Sequence found, parsing complete\n");
808 		return;
809 	}
810 
811 	/* Fail gracefully for forward incompatible sequence block. */
812 	if (sequence->version >= 3) {
813 		DRM_ERROR("Unable to parse MIPI Sequence Block v3+\n");
814 		return;
815 	}
816 
817 	DRM_DEBUG_DRIVER("Found MIPI sequence block\n");
818 
819 	block_size = get_blocksize(sequence);
820 
821 	/*
822 	 * parse the sequence block for individual sequences
823 	 */
824 	dev_priv->vbt.dsi.seq_version = sequence->version;
825 
826 	seq_data = &sequence->data[0];
827 
828 	/*
829 	 * sequence block is variable length and hence we need to parse and
830 	 * get the sequence data for specific panel id
831 	 */
832 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS; i++) {
833 		panel_id = *seq_data;
834 #pragma GCC diagnostic ignored "-Wcast-qual"
835 		seq_size = *((u16 *) (seq_data + 1));
836 #pragma GCC diagnostic pop
837 		if (panel_id == panel_type)
838 			break;
839 
840 		/* skip the sequence including seq header of 3 bytes */
841 		seq_data = seq_data + 3 + seq_size;
842 		if ((seq_data - &sequence->data[0]) > block_size) {
843 			DRM_ERROR("Sequence start is beyond sequence block size, corrupted sequence block\n");
844 			return;
845 		}
846 	}
847 
848 	if (i == MAX_MIPI_CONFIGURATIONS) {
849 		DRM_ERROR("Sequence block detected but no valid configuration\n");
850 		return;
851 	}
852 
853 	/* check if found sequence is completely within the sequence block
854 	 * just being paranoid */
855 	if (seq_size > block_size) {
856 		DRM_ERROR("Corrupted sequence/size, bailing out\n");
857 		return;
858 	}
859 
860 	/* skip the panel id(1 byte) and seq size(2 bytes) */
861 	dev_priv->vbt.dsi.data = kmemdup(seq_data + 3, seq_size, GFP_KERNEL);
862 	if (!dev_priv->vbt.dsi.data)
863 		return;
864 
865 	/*
866 	 * loop into the sequence data and split into multiple sequneces
867 	 * There are only 5 types of sequences as of now
868 	 */
869 	data = dev_priv->vbt.dsi.data;
870 	dev_priv->vbt.dsi.size = seq_size;
871 
872 	/* two consecutive 0x00 indicate end of all sequences */
873 	while (1) {
874 		int seq_id = *data;
875 		if (MIPI_SEQ_MAX > seq_id && seq_id > MIPI_SEQ_UNDEFINED) {
876 			dev_priv->vbt.dsi.sequence[seq_id] = data;
877 			DRM_DEBUG_DRIVER("Found mipi sequence - %d\n", seq_id);
878 		} else {
879 			DRM_ERROR("undefined sequence\n");
880 			goto err;
881 		}
882 
883 		/* partial parsing to skip elements */
884 		data = goto_next_sequence(data, &seq_size);
885 
886 		if (data == NULL) {
887 			DRM_ERROR("Sequence elements going beyond block itself. Sequence block parsing failed\n");
888 			goto err;
889 		}
890 
891 		if (*data == 0)
892 			break; /* end of sequence reached */
893 	}
894 
895 	DRM_DEBUG_DRIVER("MIPI related vbt parsing complete\n");
896 	return;
897 err:
898 	kfree(dev_priv->vbt.dsi.data);
899 	dev_priv->vbt.dsi.data = NULL;
900 
901 	/* error during parsing so set all pointers to null
902 	 * because of partial parsing */
903 	memset(dev_priv->vbt.dsi.sequence, 0, sizeof(dev_priv->vbt.dsi.sequence));
904 }
905 
906 static u8 translate_iboost(u8 val)
907 {
908 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
909 
910 	if (val >= ARRAY_SIZE(mapping)) {
911 		DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
912 		return 0;
913 	}
914 	return mapping[val];
915 }
916 
917 static void parse_ddi_port(struct drm_i915_private *dev_priv, enum port port,
918 			   const struct bdb_header *bdb)
919 {
920 	union child_device_config *it, *child = NULL;
921 	struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
922 	uint8_t hdmi_level_shift;
923 	int i, j;
924 	bool is_dvi, is_hdmi, is_dp, is_edp, is_crt;
925 	uint8_t aux_channel, ddc_pin;
926 	/* Each DDI port can have more than one value on the "DVO Port" field,
927 	 * so look for all the possible values for each port and abort if more
928 	 * than one is found. */
929 	int dvo_ports[][3] = {
930 		{DVO_PORT_HDMIA, DVO_PORT_DPA, -1},
931 		{DVO_PORT_HDMIB, DVO_PORT_DPB, -1},
932 		{DVO_PORT_HDMIC, DVO_PORT_DPC, -1},
933 		{DVO_PORT_HDMID, DVO_PORT_DPD, -1},
934 		{DVO_PORT_CRT, DVO_PORT_HDMIE, DVO_PORT_DPE},
935 	};
936 
937 	/* Find the child device to use, abort if more than one found. */
938 	for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
939 		it = dev_priv->vbt.child_dev + i;
940 
941 		for (j = 0; j < 3; j++) {
942 			if (dvo_ports[port][j] == -1)
943 				break;
944 
945 			if (it->common.dvo_port == dvo_ports[port][j]) {
946 				if (child) {
947 					DRM_DEBUG_KMS("More than one child device for port %c in VBT.\n",
948 						      port_name(port));
949 					return;
950 				}
951 				child = it;
952 			}
953 		}
954 	}
955 	if (!child)
956 		return;
957 
958 	aux_channel = child->raw[25];
959 	ddc_pin = child->common.ddc_pin;
960 
961 	is_dvi = child->common.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
962 	is_dp = child->common.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
963 	is_crt = child->common.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
964 	is_hdmi = is_dvi && (child->common.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
965 	is_edp = is_dp && (child->common.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR);
966 
967 	info->supports_dvi = is_dvi;
968 	info->supports_hdmi = is_hdmi;
969 	info->supports_dp = is_dp;
970 
971 	DRM_DEBUG_KMS("Port %c VBT info: DP:%d HDMI:%d DVI:%d EDP:%d CRT:%d\n",
972 		      port_name(port), is_dp, is_hdmi, is_dvi, is_edp, is_crt);
973 
974 	if (is_edp && is_dvi)
975 		DRM_DEBUG_KMS("Internal DP port %c is TMDS compatible\n",
976 			      port_name(port));
977 	if (is_crt && port != PORT_E)
978 		DRM_DEBUG_KMS("Port %c is analog\n", port_name(port));
979 	if (is_crt && (is_dvi || is_dp))
980 		DRM_DEBUG_KMS("Analog port %c is also DP or TMDS compatible\n",
981 			      port_name(port));
982 	if (is_dvi && (port == PORT_A || port == PORT_E))
983 		DRM_DEBUG_KMS("Port %c is TMDS compatible\n", port_name(port));
984 	if (!is_dvi && !is_dp && !is_crt)
985 		DRM_DEBUG_KMS("Port %c is not DP/TMDS/CRT compatible\n",
986 			      port_name(port));
987 	if (is_edp && (port == PORT_B || port == PORT_C || port == PORT_E))
988 		DRM_DEBUG_KMS("Port %c is internal DP\n", port_name(port));
989 
990 	if (is_dvi) {
991 		if (port == PORT_E) {
992 			info->alternate_ddc_pin = ddc_pin;
993 			/* if DDIE share ddc pin with other port, then
994 			 * dvi/hdmi couldn't exist on the shared port.
995 			 * Otherwise they share the same ddc bin and system
996 			 * couldn't communicate with them seperately. */
997 			if (ddc_pin == DDC_PIN_B) {
998 				dev_priv->vbt.ddi_port_info[PORT_B].supports_dvi = 0;
999 				dev_priv->vbt.ddi_port_info[PORT_B].supports_hdmi = 0;
1000 			} else if (ddc_pin == DDC_PIN_C) {
1001 				dev_priv->vbt.ddi_port_info[PORT_C].supports_dvi = 0;
1002 				dev_priv->vbt.ddi_port_info[PORT_C].supports_hdmi = 0;
1003 			} else if (ddc_pin == DDC_PIN_D) {
1004 				dev_priv->vbt.ddi_port_info[PORT_D].supports_dvi = 0;
1005 				dev_priv->vbt.ddi_port_info[PORT_D].supports_hdmi = 0;
1006 			}
1007 		} else if (ddc_pin == DDC_PIN_B && port != PORT_B)
1008 			DRM_DEBUG_KMS("Unexpected DDC pin for port B\n");
1009 		else if (ddc_pin == DDC_PIN_C && port != PORT_C)
1010 			DRM_DEBUG_KMS("Unexpected DDC pin for port C\n");
1011 		else if (ddc_pin == DDC_PIN_D && port != PORT_D)
1012 			DRM_DEBUG_KMS("Unexpected DDC pin for port D\n");
1013 	}
1014 
1015 	if (is_dp) {
1016 		if (port == PORT_E) {
1017 			info->alternate_aux_channel = aux_channel;
1018 			/* if DDIE share aux channel with other port, then
1019 			 * DP couldn't exist on the shared port. Otherwise
1020 			 * they share the same aux channel and system
1021 			 * couldn't communicate with them seperately. */
1022 			if (aux_channel == DP_AUX_A)
1023 				dev_priv->vbt.ddi_port_info[PORT_A].supports_dp = 0;
1024 			else if (aux_channel == DP_AUX_B)
1025 				dev_priv->vbt.ddi_port_info[PORT_B].supports_dp = 0;
1026 			else if (aux_channel == DP_AUX_C)
1027 				dev_priv->vbt.ddi_port_info[PORT_C].supports_dp = 0;
1028 			else if (aux_channel == DP_AUX_D)
1029 				dev_priv->vbt.ddi_port_info[PORT_D].supports_dp = 0;
1030 		}
1031 		else if (aux_channel == DP_AUX_A && port != PORT_A)
1032 			DRM_DEBUG_KMS("Unexpected AUX channel for port A\n");
1033 		else if (aux_channel == DP_AUX_B && port != PORT_B)
1034 			DRM_DEBUG_KMS("Unexpected AUX channel for port B\n");
1035 		else if (aux_channel == DP_AUX_C && port != PORT_C)
1036 			DRM_DEBUG_KMS("Unexpected AUX channel for port C\n");
1037 		else if (aux_channel == DP_AUX_D && port != PORT_D)
1038 			DRM_DEBUG_KMS("Unexpected AUX channel for port D\n");
1039 	}
1040 
1041 	if (bdb->version >= 158) {
1042 		/* The VBT HDMI level shift values match the table we have. */
1043 		hdmi_level_shift = child->raw[7] & 0xF;
1044 		DRM_DEBUG_KMS("VBT HDMI level shift for port %c: %d\n",
1045 			      port_name(port),
1046 			      hdmi_level_shift);
1047 		info->hdmi_level_shift = hdmi_level_shift;
1048 	}
1049 
1050 	/* Parse the I_boost config for SKL and above */
1051 	if (bdb->version >= 196 && (child->common.flags_1 & IBOOST_ENABLE)) {
1052 		info->dp_boost_level = translate_iboost(child->common.iboost_level & 0xF);
1053 		DRM_DEBUG_KMS("VBT (e)DP boost level for port %c: %d\n",
1054 			      port_name(port), info->dp_boost_level);
1055 		info->hdmi_boost_level = translate_iboost(child->common.iboost_level >> 4);
1056 		DRM_DEBUG_KMS("VBT HDMI boost level for port %c: %d\n",
1057 			      port_name(port), info->hdmi_boost_level);
1058 	}
1059 }
1060 
1061 static void parse_ddi_ports(struct drm_i915_private *dev_priv,
1062 			    const struct bdb_header *bdb)
1063 {
1064 	enum port port;
1065 
1066 	if (!HAS_DDI(dev_priv))
1067 		return;
1068 
1069 	if (!dev_priv->vbt.child_dev_num)
1070 		return;
1071 
1072 	if (bdb->version < 155)
1073 		return;
1074 
1075 	for (port = PORT_A; port < I915_MAX_PORTS; port++)
1076 		parse_ddi_port(dev_priv, port, bdb);
1077 }
1078 
1079 static void
1080 parse_device_mapping(struct drm_i915_private *dev_priv,
1081 		     const struct bdb_header *bdb)
1082 {
1083 	const struct bdb_general_definitions *p_defs;
1084 	const union child_device_config *p_child;
1085 	union child_device_config *child_dev_ptr;
1086 	int i, child_device_num, count;
1087 	u8 expected_size;
1088 	u16 block_size;
1089 
1090 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
1091 	if (!p_defs) {
1092 		DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
1093 		return;
1094 	}
1095 	if (bdb->version < 195) {
1096 		expected_size = sizeof(struct old_child_dev_config);
1097 	} else if (bdb->version == 195) {
1098 		expected_size = 37;
1099 	} else if (bdb->version <= 197) {
1100 		expected_size = 38;
1101 	} else {
1102 		expected_size = 38;
1103 		BUILD_BUG_ON(sizeof(*p_child) < 38);
1104 		DRM_DEBUG_DRIVER("Expected child device config size for VBT version %u not known; assuming %u\n",
1105 				 bdb->version, expected_size);
1106 	}
1107 
1108 	/* The legacy sized child device config is the minimum we need. */
1109 	if (p_defs->child_dev_size < sizeof(struct old_child_dev_config)) {
1110 		DRM_ERROR("Child device config size %u is too small.\n",
1111 			  p_defs->child_dev_size);
1112 		return;
1113 	}
1114 
1115 	/* Flag an error for unexpected size, but continue anyway. */
1116 	if (p_defs->child_dev_size != expected_size)
1117 		DRM_ERROR("Unexpected child device config size %u (expected %u for VBT version %u)\n",
1118 			  p_defs->child_dev_size, expected_size, bdb->version);
1119 
1120 	/* get the block size of general definitions */
1121 	block_size = get_blocksize(p_defs);
1122 	/* get the number of child device */
1123 	child_device_num = (block_size - sizeof(*p_defs)) /
1124 				p_defs->child_dev_size;
1125 	count = 0;
1126 	/* get the number of child device that is present */
1127 	for (i = 0; i < child_device_num; i++) {
1128 		p_child = child_device_ptr(p_defs, i);
1129 		if (!p_child->common.device_type) {
1130 			/* skip the device block if device type is invalid */
1131 			continue;
1132 		}
1133 		count++;
1134 	}
1135 	if (!count) {
1136 		DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
1137 		return;
1138 	}
1139 	dev_priv->vbt.child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
1140 	if (!dev_priv->vbt.child_dev) {
1141 		DRM_DEBUG_KMS("No memory space for child device\n");
1142 		return;
1143 	}
1144 
1145 	dev_priv->vbt.child_dev_num = count;
1146 	count = 0;
1147 	for (i = 0; i < child_device_num; i++) {
1148 		p_child = child_device_ptr(p_defs, i);
1149 		if (!p_child->common.device_type) {
1150 			/* skip the device block if device type is invalid */
1151 			continue;
1152 		}
1153 
1154 		if (p_child->common.dvo_port >= DVO_PORT_MIPIA
1155 		    && p_child->common.dvo_port <= DVO_PORT_MIPID
1156 		    &&p_child->common.device_type & DEVICE_TYPE_MIPI_OUTPUT) {
1157 			DRM_DEBUG_KMS("Found MIPI as LFP\n");
1158 			dev_priv->vbt.has_mipi = 1;
1159 			dev_priv->vbt.dsi.port = p_child->common.dvo_port;
1160 		}
1161 
1162 		child_dev_ptr = dev_priv->vbt.child_dev + count;
1163 		count++;
1164 
1165 		/*
1166 		 * Copy as much as we know (sizeof) and is available
1167 		 * (child_dev_size) of the child device. Accessing the data must
1168 		 * depend on VBT version.
1169 		 */
1170 		memcpy(child_dev_ptr, p_child,
1171 		       min_t(size_t, p_defs->child_dev_size, sizeof(*p_child)));
1172 	}
1173 	return;
1174 }
1175 
1176 static void
1177 init_vbt_defaults(struct drm_i915_private *dev_priv)
1178 {
1179 	enum port port;
1180 
1181 	dev_priv->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
1182 
1183 	/* Default to having backlight */
1184 	dev_priv->vbt.backlight.present = true;
1185 
1186 	/* LFP panel data */
1187 	dev_priv->vbt.lvds_dither = 1;
1188 	dev_priv->vbt.lvds_vbt = 0;
1189 
1190 	/* SDVO panel data */
1191 	dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1192 
1193 	/* general features */
1194 	dev_priv->vbt.int_tv_support = 1;
1195 	dev_priv->vbt.int_crt_support = 1;
1196 
1197 	/* Default to using SSC */
1198 	dev_priv->vbt.lvds_use_ssc = 1;
1199 	/*
1200 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
1201 	 * clock for LVDS.
1202 	 */
1203 	dev_priv->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(dev_priv,
1204 			!HAS_PCH_SPLIT(dev_priv));
1205 	DRM_DEBUG_KMS("Set default to SSC at %d kHz\n", dev_priv->vbt.lvds_ssc_freq);
1206 
1207 	for (port = PORT_A; port < I915_MAX_PORTS; port++) {
1208 		struct ddi_vbt_port_info *info =
1209 			&dev_priv->vbt.ddi_port_info[port];
1210 
1211 		info->hdmi_level_shift = HDMI_LEVEL_SHIFT_UNKNOWN;
1212 
1213 		info->supports_dvi = (port != PORT_A && port != PORT_E);
1214 		info->supports_hdmi = info->supports_dvi;
1215 		info->supports_dp = (port != PORT_E);
1216 	}
1217 }
1218 
1219 static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
1220 {
1221 	const char *_vbt = (const char *)vbt;
1222 
1223 	return (const struct bdb_header *)(_vbt + vbt->bdb_offset);
1224 }
1225 
1226 /**
1227  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
1228  * @buf:	pointer to a buffer to validate
1229  * @size:	size of the buffer
1230  *
1231  * Returns true on valid VBT.
1232  */
1233 bool intel_bios_is_valid_vbt(const void *buf, size_t size)
1234 {
1235 	const struct vbt_header *vbt = buf;
1236 	const struct bdb_header *bdb;
1237 
1238 	if (!vbt)
1239 		return false;
1240 
1241 	if (sizeof(struct vbt_header) > size) {
1242 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
1243 		return false;
1244 	}
1245 
1246 	if (memcmp(vbt->signature, "$VBT", 4)) {
1247 		DRM_DEBUG_DRIVER("VBT invalid signature\n");
1248 		return false;
1249 	}
1250 
1251 	if (vbt->bdb_offset + sizeof(struct bdb_header) > size) {
1252 		DRM_DEBUG_DRIVER("BDB header incomplete\n");
1253 		return false;
1254 	}
1255 
1256 	bdb = get_bdb_header(vbt);
1257 	if (vbt->bdb_offset + bdb->bdb_size > size) {
1258 		DRM_DEBUG_DRIVER("BDB incomplete\n");
1259 		return false;
1260 	}
1261 
1262 	return vbt;
1263 }
1264 
1265 static const struct vbt_header *find_vbt(void __iomem *bios, size_t size)
1266 {
1267 	size_t i;
1268 
1269 	/* Scour memory looking for the VBT signature. */
1270 	for (i = 0; i + 4 < size; i++) {
1271 		void *vbt;
1272 
1273 		if (ioread32(bios + i) != *((const u32 *) "$VBT"))
1274 			continue;
1275 
1276 		/*
1277 		 * This is the one place where we explicitly discard the address
1278 		 * space (__iomem) of the BIOS/VBT.
1279 		 */
1280 		vbt = (char __force *) bios + i;
1281 		if (intel_bios_is_valid_vbt(vbt, size - i))
1282 			return vbt;
1283 
1284 		break;
1285 	}
1286 
1287 	return NULL;
1288 }
1289 
1290 /**
1291  * intel_bios_init - find VBT and initialize settings from the BIOS
1292  * @dev: DRM device
1293  *
1294  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
1295  * to appropriate values.
1296  *
1297  * Returns 0 on success, nonzero on failure.
1298  */
1299 int
1300 intel_bios_init(struct drm_i915_private *dev_priv)
1301 {
1302 #if 0
1303 	struct pci_dev *pdev = dev_priv->dev->pdev;
1304 #endif
1305 	const struct vbt_header *vbt = dev_priv->opregion.vbt;
1306 	const struct bdb_header *bdb;
1307 	u8 __iomem *bios = NULL;
1308 
1309 	if (HAS_PCH_NOP(dev_priv))
1310 		return -ENODEV;
1311 
1312 	init_vbt_defaults(dev_priv);
1313 
1314 	if (!vbt) {
1315 		size_t size;
1316 
1317 #if 0
1318 		bios = pci_map_rom(pdev, &size);
1319 		if (!bios)
1320 #endif
1321 			return -1;
1322 
1323 		vbt = find_vbt(bios, size);
1324 		if (!vbt) {
1325 #if 0
1326 			pci_unmap_rom(pdev, bios);
1327 #endif
1328 			return -1;
1329 		}
1330 
1331 		DRM_DEBUG_KMS("Found valid VBT in PCI ROM\n");
1332 	}
1333 
1334 	bdb = get_bdb_header(vbt);
1335 
1336 	DRM_DEBUG_KMS("VBT signature \"%.*s\", BDB version %d\n",
1337 		      (int)sizeof(vbt->signature), vbt->signature, bdb->version);
1338 
1339 	/* Grab useful general definitions */
1340 	parse_general_features(dev_priv, bdb);
1341 	parse_general_definitions(dev_priv, bdb);
1342 	parse_lfp_panel_data(dev_priv, bdb);
1343 	parse_lfp_backlight(dev_priv, bdb);
1344 	parse_sdvo_panel_data(dev_priv, bdb);
1345 	parse_sdvo_device_mapping(dev_priv, bdb);
1346 	parse_device_mapping(dev_priv, bdb);
1347 	parse_driver_features(dev_priv, bdb);
1348 	parse_edp(dev_priv, bdb);
1349 	parse_psr(dev_priv, bdb);
1350 	parse_mipi(dev_priv, bdb);
1351 	parse_ddi_ports(dev_priv, bdb);
1352 
1353 #if 0
1354 	if (bios)
1355 		pci_unmap_rom(pdev, bios);
1356 #endif
1357 
1358 	return 0;
1359 }
1360