xref: /dragonfly/sys/dev/drm/i915/intel_bios.c (revision 0de090e1)
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 #include <linux/dmi.h>
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_device *dev,
338 				    bool alternate)
339 {
340 	switch (INTEL_INFO(dev)->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 	struct drm_device *dev = dev_priv->dev;
356 	const struct bdb_general_features *general;
357 
358 	general = find_section(bdb, BDB_GENERAL_FEATURES);
359 	if (general) {
360 		dev_priv->vbt.int_tv_support = general->int_tv_support;
361 		dev_priv->vbt.int_crt_support = general->int_crt_support;
362 		dev_priv->vbt.lvds_use_ssc = general->enable_ssc;
363 		dev_priv->vbt.lvds_ssc_freq =
364 			intel_bios_ssc_frequency(dev, general->ssc_freq);
365 		dev_priv->vbt.display_clock_mode = general->display_clock_mode;
366 		dev_priv->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
367 		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",
368 			      dev_priv->vbt.int_tv_support,
369 			      dev_priv->vbt.int_crt_support,
370 			      dev_priv->vbt.lvds_use_ssc,
371 			      dev_priv->vbt.lvds_ssc_freq,
372 			      dev_priv->vbt.display_clock_mode,
373 			      dev_priv->vbt.fdi_rx_polarity_inverted);
374 	}
375 }
376 
377 static void
378 parse_general_definitions(struct drm_i915_private *dev_priv,
379 			  const struct bdb_header *bdb)
380 {
381 	const struct bdb_general_definitions *general;
382 
383 	general = find_section(bdb, BDB_GENERAL_DEFINITIONS);
384 	if (general) {
385 		u16 block_size = get_blocksize(general);
386 		if (block_size >= sizeof(*general)) {
387 			int bus_pin = general->crt_ddc_gmbus_pin;
388 			DRM_DEBUG_KMS("crt_ddc_bus_pin: %d\n", bus_pin);
389 			if (intel_gmbus_is_valid_pin(dev_priv, bus_pin))
390 				dev_priv->vbt.crt_ddc_pin = bus_pin;
391 		} else {
392 			DRM_DEBUG_KMS("BDB_GD too small (%d). Invalid.\n",
393 				      block_size);
394 		}
395 	}
396 }
397 
398 static const union child_device_config *
399 child_device_ptr(const struct bdb_general_definitions *p_defs, int i)
400 {
401 	return (const void *) &p_defs->devices[i * p_defs->child_dev_size];
402 }
403 
404 static void
405 parse_sdvo_device_mapping(struct drm_i915_private *dev_priv,
406 			  const struct bdb_header *bdb)
407 {
408 	struct sdvo_device_mapping *p_mapping;
409 	const struct bdb_general_definitions *p_defs;
410 	const struct old_child_dev_config *child; /* legacy */
411 	int i, child_device_num, count;
412 	u16	block_size;
413 
414 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
415 	if (!p_defs) {
416 		DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
417 		return;
418 	}
419 
420 	/*
421 	 * Only parse SDVO mappings when the general definitions block child
422 	 * device size matches that of the *legacy* child device config
423 	 * struct. Thus, SDVO mapping will be skipped for newer VBT.
424 	 */
425 	if (p_defs->child_dev_size != sizeof(*child)) {
426 		DRM_DEBUG_KMS("Unsupported child device size for SDVO mapping.\n");
427 		return;
428 	}
429 	/* get the block size of general definitions */
430 	block_size = get_blocksize(p_defs);
431 	/* get the number of child device */
432 	child_device_num = (block_size - sizeof(*p_defs)) /
433 		p_defs->child_dev_size;
434 	count = 0;
435 	for (i = 0; i < child_device_num; i++) {
436 		child = &child_device_ptr(p_defs, i)->old;
437 		if (!child->device_type) {
438 			/* skip the device block if device type is invalid */
439 			continue;
440 		}
441 		if (child->slave_addr != SLAVE_ADDR1 &&
442 		    child->slave_addr != SLAVE_ADDR2) {
443 			/*
444 			 * If the slave address is neither 0x70 nor 0x72,
445 			 * it is not a SDVO device. Skip it.
446 			 */
447 			continue;
448 		}
449 		if (child->dvo_port != DEVICE_PORT_DVOB &&
450 		    child->dvo_port != DEVICE_PORT_DVOC) {
451 			/* skip the incorrect SDVO port */
452 			DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
453 			continue;
454 		}
455 		DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
456 			      " %s port\n",
457 			      child->slave_addr,
458 			      (child->dvo_port == DEVICE_PORT_DVOB) ?
459 			      "SDVOB" : "SDVOC");
460 		p_mapping = &(dev_priv->sdvo_mappings[child->dvo_port - 1]);
461 		if (!p_mapping->initialized) {
462 			p_mapping->dvo_port = child->dvo_port;
463 			p_mapping->slave_addr = child->slave_addr;
464 			p_mapping->dvo_wiring = child->dvo_wiring;
465 			p_mapping->ddc_pin = child->ddc_pin;
466 			p_mapping->i2c_pin = child->i2c_pin;
467 			p_mapping->initialized = 1;
468 			DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
469 				      p_mapping->dvo_port,
470 				      p_mapping->slave_addr,
471 				      p_mapping->dvo_wiring,
472 				      p_mapping->ddc_pin,
473 				      p_mapping->i2c_pin);
474 		} else {
475 			DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
476 					 "two SDVO device.\n");
477 		}
478 		if (child->slave2_addr) {
479 			/* Maybe this is a SDVO device with multiple inputs */
480 			/* And the mapping info is not added */
481 			DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
482 				" is a SDVO device with multiple inputs.\n");
483 		}
484 		count++;
485 	}
486 
487 	if (!count) {
488 		/* No SDVO device info is found */
489 		DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
490 	}
491 	return;
492 }
493 
494 static void
495 parse_driver_features(struct drm_i915_private *dev_priv,
496 		      const struct bdb_header *bdb)
497 {
498 	const struct bdb_driver_features *driver;
499 
500 	driver = find_section(bdb, BDB_DRIVER_FEATURES);
501 	if (!driver)
502 		return;
503 
504 	if (driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
505 		dev_priv->vbt.edp_support = 1;
506 
507 	if (driver->dual_frequency)
508 		dev_priv->render_reclock_avail = true;
509 
510 	DRM_DEBUG_KMS("DRRS State Enabled:%d\n", driver->drrs_enabled);
511 	/*
512 	 * If DRRS is not supported, drrs_type has to be set to 0.
513 	 * This is because, VBT is configured in such a way that
514 	 * static DRRS is 0 and DRRS not supported is represented by
515 	 * driver->drrs_enabled=false
516 	 */
517 	if (!driver->drrs_enabled)
518 		dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
519 }
520 
521 static void
522 parse_edp(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
523 {
524 	const struct bdb_edp *edp;
525 	const struct edp_power_seq *edp_pps;
526 	const struct edp_link_params *edp_link_params;
527 
528 	edp = find_section(bdb, BDB_EDP);
529 	if (!edp) {
530 		if (dev_priv->vbt.edp_support)
531 			DRM_DEBUG_KMS("No eDP BDB found but eDP panel supported.\n");
532 		return;
533 	}
534 
535 	switch ((edp->color_depth >> (panel_type * 2)) & 3) {
536 	case EDP_18BPP:
537 		dev_priv->vbt.edp_bpp = 18;
538 		break;
539 	case EDP_24BPP:
540 		dev_priv->vbt.edp_bpp = 24;
541 		break;
542 	case EDP_30BPP:
543 		dev_priv->vbt.edp_bpp = 30;
544 		break;
545 	}
546 
547 	/* Get the eDP sequencing and link info */
548 	edp_pps = &edp->power_seqs[panel_type];
549 	edp_link_params = &edp->link_params[panel_type];
550 
551 	dev_priv->vbt.edp_pps = *edp_pps;
552 
553 	switch (edp_link_params->rate) {
554 	case EDP_RATE_1_62:
555 		dev_priv->vbt.edp_rate = DP_LINK_BW_1_62;
556 		break;
557 	case EDP_RATE_2_7:
558 		dev_priv->vbt.edp_rate = DP_LINK_BW_2_7;
559 		break;
560 	default:
561 		DRM_DEBUG_KMS("VBT has unknown eDP link rate value %u\n",
562 			      edp_link_params->rate);
563 		break;
564 	}
565 
566 	switch (edp_link_params->lanes) {
567 	case EDP_LANE_1:
568 		dev_priv->vbt.edp_lanes = 1;
569 		break;
570 	case EDP_LANE_2:
571 		dev_priv->vbt.edp_lanes = 2;
572 		break;
573 	case EDP_LANE_4:
574 		dev_priv->vbt.edp_lanes = 4;
575 		break;
576 	default:
577 		DRM_DEBUG_KMS("VBT has unknown eDP lane count value %u\n",
578 			      edp_link_params->lanes);
579 		break;
580 	}
581 
582 	switch (edp_link_params->preemphasis) {
583 	case EDP_PREEMPHASIS_NONE:
584 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
585 		break;
586 	case EDP_PREEMPHASIS_3_5dB:
587 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
588 		break;
589 	case EDP_PREEMPHASIS_6dB:
590 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
591 		break;
592 	case EDP_PREEMPHASIS_9_5dB:
593 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
594 		break;
595 	default:
596 		DRM_DEBUG_KMS("VBT has unknown eDP pre-emphasis value %u\n",
597 			      edp_link_params->preemphasis);
598 		break;
599 	}
600 
601 	switch (edp_link_params->vswing) {
602 	case EDP_VSWING_0_4V:
603 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
604 		break;
605 	case EDP_VSWING_0_6V:
606 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
607 		break;
608 	case EDP_VSWING_0_8V:
609 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
610 		break;
611 	case EDP_VSWING_1_2V:
612 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
613 		break;
614 	default:
615 		DRM_DEBUG_KMS("VBT has unknown eDP voltage swing value %u\n",
616 			      edp_link_params->vswing);
617 		break;
618 	}
619 
620 	if (bdb->version >= 173) {
621 		uint8_t vswing;
622 
623 		/* Don't read from VBT if module parameter has valid value*/
624 		if (i915.edp_vswing) {
625 			dev_priv->edp_low_vswing = i915.edp_vswing == 1;
626 		} else {
627 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
628 			dev_priv->edp_low_vswing = vswing == 0;
629 		}
630 	}
631 }
632 
633 static void
634 parse_psr(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
635 {
636 	const struct bdb_psr *psr;
637 	const struct psr_table *psr_table;
638 
639 	psr = find_section(bdb, BDB_PSR);
640 	if (!psr) {
641 		DRM_DEBUG_KMS("No PSR BDB found.\n");
642 		return;
643 	}
644 
645 	psr_table = &psr->psr_table[panel_type];
646 
647 	dev_priv->vbt.psr.full_link = psr_table->full_link;
648 	dev_priv->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
649 
650 	/* Allowed VBT values goes from 0 to 15 */
651 	dev_priv->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
652 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
653 
654 	switch (psr_table->lines_to_wait) {
655 	case 0:
656 		dev_priv->vbt.psr.lines_to_wait = PSR_0_LINES_TO_WAIT;
657 		break;
658 	case 1:
659 		dev_priv->vbt.psr.lines_to_wait = PSR_1_LINE_TO_WAIT;
660 		break;
661 	case 2:
662 		dev_priv->vbt.psr.lines_to_wait = PSR_4_LINES_TO_WAIT;
663 		break;
664 	case 3:
665 		dev_priv->vbt.psr.lines_to_wait = PSR_8_LINES_TO_WAIT;
666 		break;
667 	default:
668 		DRM_DEBUG_KMS("VBT has unknown PSR lines to wait %u\n",
669 			      psr_table->lines_to_wait);
670 		break;
671 	}
672 
673 	dev_priv->vbt.psr.tp1_wakeup_time = psr_table->tp1_wakeup_time;
674 	dev_priv->vbt.psr.tp2_tp3_wakeup_time = psr_table->tp2_tp3_wakeup_time;
675 }
676 
677 static u8 *goto_next_sequence(u8 *data, int *size)
678 {
679 	u16 len;
680 	int tmp = *size;
681 
682 	if (--tmp < 0)
683 		return NULL;
684 
685 	/* goto first element */
686 	data++;
687 	while (1) {
688 		switch (*data) {
689 		case MIPI_SEQ_ELEM_SEND_PKT:
690 			/*
691 			 * skip by this element payload size
692 			 * skip elem id, command flag and data type
693 			 */
694 			tmp -= 5;
695 			if (tmp < 0)
696 				return NULL;
697 
698 			data += 3;
699 			len = *((u16 *)data);
700 
701 			tmp -= len;
702 			if (tmp < 0)
703 				return NULL;
704 
705 			/* skip by len */
706 			data = data + 2 + len;
707 			break;
708 		case MIPI_SEQ_ELEM_DELAY:
709 			/* skip by elem id, and delay is 4 bytes */
710 			tmp -= 5;
711 			if (tmp < 0)
712 				return NULL;
713 
714 			data += 5;
715 			break;
716 		case MIPI_SEQ_ELEM_GPIO:
717 			tmp -= 3;
718 			if (tmp < 0)
719 				return NULL;
720 
721 			data += 3;
722 			break;
723 		default:
724 			DRM_ERROR("Unknown element\n");
725 			return NULL;
726 		}
727 
728 		/* end of sequence ? */
729 		if (*data == 0)
730 			break;
731 	}
732 
733 	/* goto next sequence or end of block byte */
734 	if (--tmp < 0)
735 		return NULL;
736 
737 	data++;
738 
739 	/* update amount of data left for the sequence block to be parsed */
740 	*size = tmp;
741 	return data;
742 }
743 
744 static void
745 parse_mipi(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
746 {
747 	const struct bdb_mipi_config *start;
748 	const struct bdb_mipi_sequence *sequence;
749 	const struct mipi_config *config;
750 	const struct mipi_pps_data *pps;
751 	u8 *data;
752 	const u8 *seq_data;
753 	int i, panel_id, seq_size;
754 	u16 block_size;
755 
756 	/* parse MIPI blocks only if LFP type is MIPI */
757 	if (!dev_priv->vbt.has_mipi)
758 		return;
759 
760 	/* Initialize this to undefined indicating no generic MIPI support */
761 	dev_priv->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
762 
763 	/* Block #40 is already parsed and panel_fixed_mode is
764 	 * stored in dev_priv->lfp_lvds_vbt_mode
765 	 * resuse this when needed
766 	 */
767 
768 	/* Parse #52 for panel index used from panel_type already
769 	 * parsed
770 	 */
771 	start = find_section(bdb, BDB_MIPI_CONFIG);
772 	if (!start) {
773 		DRM_DEBUG_KMS("No MIPI config BDB found");
774 		return;
775 	}
776 
777 	DRM_DEBUG_DRIVER("Found MIPI Config block, panel index = %d\n",
778 								panel_type);
779 
780 	/*
781 	 * get hold of the correct configuration block and pps data as per
782 	 * the panel_type as index
783 	 */
784 	config = &start->config[panel_type];
785 	pps = &start->pps[panel_type];
786 
787 	/* store as of now full data. Trim when we realise all is not needed */
788 	dev_priv->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
789 	if (!dev_priv->vbt.dsi.config)
790 		return;
791 
792 	dev_priv->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
793 	if (!dev_priv->vbt.dsi.pps) {
794 		kfree(dev_priv->vbt.dsi.config);
795 		return;
796 	}
797 
798 	/* We have mandatory mipi config blocks. Initialize as generic panel */
799 	dev_priv->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
800 
801 	/* Check if we have sequence block as well */
802 	sequence = find_section(bdb, BDB_MIPI_SEQUENCE);
803 	if (!sequence) {
804 		DRM_DEBUG_KMS("No MIPI Sequence found, parsing complete\n");
805 		return;
806 	}
807 
808 	/* Fail gracefully for forward incompatible sequence block. */
809 	if (sequence->version >= 3) {
810 		DRM_ERROR("Unable to parse MIPI Sequence Block v3+\n");
811 		return;
812 	}
813 
814 	DRM_DEBUG_DRIVER("Found MIPI sequence block\n");
815 
816 	block_size = get_blocksize(sequence);
817 
818 	/*
819 	 * parse the sequence block for individual sequences
820 	 */
821 	dev_priv->vbt.dsi.seq_version = sequence->version;
822 
823 	seq_data = &sequence->data[0];
824 
825 	/*
826 	 * sequence block is variable length and hence we need to parse and
827 	 * get the sequence data for specific panel id
828 	 */
829 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS; i++) {
830 		panel_id = *seq_data;
831 #pragma GCC diagnostic ignored "-Wcast-qual"
832 		seq_size = *((u16 *) (seq_data + 1));
833 #pragma GCC diagnostic pop
834 		if (panel_id == panel_type)
835 			break;
836 
837 		/* skip the sequence including seq header of 3 bytes */
838 		seq_data = seq_data + 3 + seq_size;
839 		if ((seq_data - &sequence->data[0]) > block_size) {
840 			DRM_ERROR("Sequence start is beyond sequence block size, corrupted sequence block\n");
841 			return;
842 		}
843 	}
844 
845 	if (i == MAX_MIPI_CONFIGURATIONS) {
846 		DRM_ERROR("Sequence block detected but no valid configuration\n");
847 		return;
848 	}
849 
850 	/* check if found sequence is completely within the sequence block
851 	 * just being paranoid */
852 	if (seq_size > block_size) {
853 		DRM_ERROR("Corrupted sequence/size, bailing out\n");
854 		return;
855 	}
856 
857 	/* skip the panel id(1 byte) and seq size(2 bytes) */
858 	dev_priv->vbt.dsi.data = kmemdup(seq_data + 3, seq_size, GFP_KERNEL);
859 	if (!dev_priv->vbt.dsi.data)
860 		return;
861 
862 	/*
863 	 * loop into the sequence data and split into multiple sequneces
864 	 * There are only 5 types of sequences as of now
865 	 */
866 	data = dev_priv->vbt.dsi.data;
867 	dev_priv->vbt.dsi.size = seq_size;
868 
869 	/* two consecutive 0x00 indicate end of all sequences */
870 	while (1) {
871 		int seq_id = *data;
872 		if (MIPI_SEQ_MAX > seq_id && seq_id > MIPI_SEQ_UNDEFINED) {
873 			dev_priv->vbt.dsi.sequence[seq_id] = data;
874 			DRM_DEBUG_DRIVER("Found mipi sequence - %d\n", seq_id);
875 		} else {
876 			DRM_ERROR("undefined sequence\n");
877 			goto err;
878 		}
879 
880 		/* partial parsing to skip elements */
881 		data = goto_next_sequence(data, &seq_size);
882 
883 		if (data == NULL) {
884 			DRM_ERROR("Sequence elements going beyond block itself. Sequence block parsing failed\n");
885 			goto err;
886 		}
887 
888 		if (*data == 0)
889 			break; /* end of sequence reached */
890 	}
891 
892 	DRM_DEBUG_DRIVER("MIPI related vbt parsing complete\n");
893 	return;
894 err:
895 	kfree(dev_priv->vbt.dsi.data);
896 	dev_priv->vbt.dsi.data = NULL;
897 
898 	/* error during parsing so set all pointers to null
899 	 * because of partial parsing */
900 	memset(dev_priv->vbt.dsi.sequence, 0, sizeof(dev_priv->vbt.dsi.sequence));
901 }
902 
903 static u8 translate_iboost(u8 val)
904 {
905 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
906 
907 	if (val >= ARRAY_SIZE(mapping)) {
908 		DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
909 		return 0;
910 	}
911 	return mapping[val];
912 }
913 
914 static void parse_ddi_port(struct drm_i915_private *dev_priv, enum port port,
915 			   const struct bdb_header *bdb)
916 {
917 	union child_device_config *it, *child = NULL;
918 	struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
919 	uint8_t hdmi_level_shift;
920 	int i, j;
921 	bool is_dvi, is_hdmi, is_dp, is_edp, is_crt;
922 	uint8_t aux_channel, ddc_pin;
923 	/* Each DDI port can have more than one value on the "DVO Port" field,
924 	 * so look for all the possible values for each port and abort if more
925 	 * than one is found. */
926 	int dvo_ports[][3] = {
927 		{DVO_PORT_HDMIA, DVO_PORT_DPA, -1},
928 		{DVO_PORT_HDMIB, DVO_PORT_DPB, -1},
929 		{DVO_PORT_HDMIC, DVO_PORT_DPC, -1},
930 		{DVO_PORT_HDMID, DVO_PORT_DPD, -1},
931 		{DVO_PORT_CRT, DVO_PORT_HDMIE, DVO_PORT_DPE},
932 	};
933 
934 	/* Find the child device to use, abort if more than one found. */
935 	for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
936 		it = dev_priv->vbt.child_dev + i;
937 
938 		for (j = 0; j < 3; j++) {
939 			if (dvo_ports[port][j] == -1)
940 				break;
941 
942 			if (it->common.dvo_port == dvo_ports[port][j]) {
943 				if (child) {
944 					DRM_DEBUG_KMS("More than one child device for port %c in VBT.\n",
945 						      port_name(port));
946 					return;
947 				}
948 				child = it;
949 			}
950 		}
951 	}
952 	if (!child)
953 		return;
954 
955 	aux_channel = child->raw[25];
956 	ddc_pin = child->common.ddc_pin;
957 
958 	is_dvi = child->common.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
959 	is_dp = child->common.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
960 	is_crt = child->common.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
961 	is_hdmi = is_dvi && (child->common.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
962 	is_edp = is_dp && (child->common.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR);
963 
964 	info->supports_dvi = is_dvi;
965 	info->supports_hdmi = is_hdmi;
966 	info->supports_dp = is_dp;
967 
968 	DRM_DEBUG_KMS("Port %c VBT info: DP:%d HDMI:%d DVI:%d EDP:%d CRT:%d\n",
969 		      port_name(port), is_dp, is_hdmi, is_dvi, is_edp, is_crt);
970 
971 	if (is_edp && is_dvi)
972 		DRM_DEBUG_KMS("Internal DP port %c is TMDS compatible\n",
973 			      port_name(port));
974 	if (is_crt && port != PORT_E)
975 		DRM_DEBUG_KMS("Port %c is analog\n", port_name(port));
976 	if (is_crt && (is_dvi || is_dp))
977 		DRM_DEBUG_KMS("Analog port %c is also DP or TMDS compatible\n",
978 			      port_name(port));
979 	if (is_dvi && (port == PORT_A || port == PORT_E))
980 		DRM_DEBUG_KMS("Port %c is TMDS compatible\n", port_name(port));
981 	if (!is_dvi && !is_dp && !is_crt)
982 		DRM_DEBUG_KMS("Port %c is not DP/TMDS/CRT compatible\n",
983 			      port_name(port));
984 	if (is_edp && (port == PORT_B || port == PORT_C || port == PORT_E))
985 		DRM_DEBUG_KMS("Port %c is internal DP\n", port_name(port));
986 
987 	if (is_dvi) {
988 		if (port == PORT_E) {
989 			info->alternate_ddc_pin = ddc_pin;
990 			/* if DDIE share ddc pin with other port, then
991 			 * dvi/hdmi couldn't exist on the shared port.
992 			 * Otherwise they share the same ddc bin and system
993 			 * couldn't communicate with them seperately. */
994 			if (ddc_pin == DDC_PIN_B) {
995 				dev_priv->vbt.ddi_port_info[PORT_B].supports_dvi = 0;
996 				dev_priv->vbt.ddi_port_info[PORT_B].supports_hdmi = 0;
997 			} else if (ddc_pin == DDC_PIN_C) {
998 				dev_priv->vbt.ddi_port_info[PORT_C].supports_dvi = 0;
999 				dev_priv->vbt.ddi_port_info[PORT_C].supports_hdmi = 0;
1000 			} else if (ddc_pin == DDC_PIN_D) {
1001 				dev_priv->vbt.ddi_port_info[PORT_D].supports_dvi = 0;
1002 				dev_priv->vbt.ddi_port_info[PORT_D].supports_hdmi = 0;
1003 			}
1004 		} else if (ddc_pin == DDC_PIN_B && port != PORT_B)
1005 			DRM_DEBUG_KMS("Unexpected DDC pin for port B\n");
1006 		else if (ddc_pin == DDC_PIN_C && port != PORT_C)
1007 			DRM_DEBUG_KMS("Unexpected DDC pin for port C\n");
1008 		else if (ddc_pin == DDC_PIN_D && port != PORT_D)
1009 			DRM_DEBUG_KMS("Unexpected DDC pin for port D\n");
1010 	}
1011 
1012 	if (is_dp) {
1013 		if (port == PORT_E) {
1014 			info->alternate_aux_channel = aux_channel;
1015 			/* if DDIE share aux channel with other port, then
1016 			 * DP couldn't exist on the shared port. Otherwise
1017 			 * they share the same aux channel and system
1018 			 * couldn't communicate with them seperately. */
1019 			if (aux_channel == DP_AUX_A)
1020 				dev_priv->vbt.ddi_port_info[PORT_A].supports_dp = 0;
1021 			else if (aux_channel == DP_AUX_B)
1022 				dev_priv->vbt.ddi_port_info[PORT_B].supports_dp = 0;
1023 			else if (aux_channel == DP_AUX_C)
1024 				dev_priv->vbt.ddi_port_info[PORT_C].supports_dp = 0;
1025 			else if (aux_channel == DP_AUX_D)
1026 				dev_priv->vbt.ddi_port_info[PORT_D].supports_dp = 0;
1027 		}
1028 		else if (aux_channel == DP_AUX_A && port != PORT_A)
1029 			DRM_DEBUG_KMS("Unexpected AUX channel for port A\n");
1030 		else if (aux_channel == DP_AUX_B && port != PORT_B)
1031 			DRM_DEBUG_KMS("Unexpected AUX channel for port B\n");
1032 		else if (aux_channel == DP_AUX_C && port != PORT_C)
1033 			DRM_DEBUG_KMS("Unexpected AUX channel for port C\n");
1034 		else if (aux_channel == DP_AUX_D && port != PORT_D)
1035 			DRM_DEBUG_KMS("Unexpected AUX channel for port D\n");
1036 	}
1037 
1038 	if (bdb->version >= 158) {
1039 		/* The VBT HDMI level shift values match the table we have. */
1040 		hdmi_level_shift = child->raw[7] & 0xF;
1041 		DRM_DEBUG_KMS("VBT HDMI level shift for port %c: %d\n",
1042 			      port_name(port),
1043 			      hdmi_level_shift);
1044 		info->hdmi_level_shift = hdmi_level_shift;
1045 	}
1046 
1047 	/* Parse the I_boost config for SKL and above */
1048 	if (bdb->version >= 196 && (child->common.flags_1 & IBOOST_ENABLE)) {
1049 		info->dp_boost_level = translate_iboost(child->common.iboost_level & 0xF);
1050 		DRM_DEBUG_KMS("VBT (e)DP boost level for port %c: %d\n",
1051 			      port_name(port), info->dp_boost_level);
1052 		info->hdmi_boost_level = translate_iboost(child->common.iboost_level >> 4);
1053 		DRM_DEBUG_KMS("VBT HDMI boost level for port %c: %d\n",
1054 			      port_name(port), info->hdmi_boost_level);
1055 	}
1056 }
1057 
1058 static void parse_ddi_ports(struct drm_i915_private *dev_priv,
1059 			    const struct bdb_header *bdb)
1060 {
1061 	struct drm_device *dev = dev_priv->dev;
1062 	enum port port;
1063 
1064 	if (!HAS_DDI(dev))
1065 		return;
1066 
1067 	if (!dev_priv->vbt.child_dev_num)
1068 		return;
1069 
1070 	if (bdb->version < 155)
1071 		return;
1072 
1073 	for (port = PORT_A; port < I915_MAX_PORTS; port++)
1074 		parse_ddi_port(dev_priv, port, bdb);
1075 }
1076 
1077 static void
1078 parse_device_mapping(struct drm_i915_private *dev_priv,
1079 		     const struct bdb_header *bdb)
1080 {
1081 	const struct bdb_general_definitions *p_defs;
1082 	const union child_device_config *p_child;
1083 	union child_device_config *child_dev_ptr;
1084 	int i, child_device_num, count;
1085 	u8 expected_size;
1086 	u16 block_size;
1087 
1088 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
1089 	if (!p_defs) {
1090 		DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
1091 		return;
1092 	}
1093 	if (bdb->version < 195) {
1094 		expected_size = sizeof(struct old_child_dev_config);
1095 	} else if (bdb->version == 195) {
1096 		expected_size = 37;
1097 	} else if (bdb->version <= 197) {
1098 		expected_size = 38;
1099 	} else {
1100 		expected_size = 38;
1101 		BUILD_BUG_ON(sizeof(*p_child) < 38);
1102 		DRM_DEBUG_DRIVER("Expected child device config size for VBT version %u not known; assuming %u\n",
1103 				 bdb->version, expected_size);
1104 	}
1105 
1106 	/* The legacy sized child device config is the minimum we need. */
1107 	if (p_defs->child_dev_size < sizeof(struct old_child_dev_config)) {
1108 		DRM_ERROR("Child device config size %u is too small.\n",
1109 			  p_defs->child_dev_size);
1110 		return;
1111 	}
1112 
1113 	/* Flag an error for unexpected size, but continue anyway. */
1114 	if (p_defs->child_dev_size != expected_size)
1115 		DRM_ERROR("Unexpected child device config size %u (expected %u for VBT version %u)\n",
1116 			  p_defs->child_dev_size, expected_size, bdb->version);
1117 
1118 	/* get the block size of general definitions */
1119 	block_size = get_blocksize(p_defs);
1120 	/* get the number of child device */
1121 	child_device_num = (block_size - sizeof(*p_defs)) /
1122 				p_defs->child_dev_size;
1123 	count = 0;
1124 	/* get the number of child device that is present */
1125 	for (i = 0; i < child_device_num; i++) {
1126 		p_child = child_device_ptr(p_defs, i);
1127 		if (!p_child->common.device_type) {
1128 			/* skip the device block if device type is invalid */
1129 			continue;
1130 		}
1131 		count++;
1132 	}
1133 	if (!count) {
1134 		DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
1135 		return;
1136 	}
1137 	dev_priv->vbt.child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
1138 	if (!dev_priv->vbt.child_dev) {
1139 		DRM_DEBUG_KMS("No memory space for child device\n");
1140 		return;
1141 	}
1142 
1143 	dev_priv->vbt.child_dev_num = count;
1144 	count = 0;
1145 	for (i = 0; i < child_device_num; i++) {
1146 		p_child = child_device_ptr(p_defs, i);
1147 		if (!p_child->common.device_type) {
1148 			/* skip the device block if device type is invalid */
1149 			continue;
1150 		}
1151 
1152 		if (p_child->common.dvo_port >= DVO_PORT_MIPIA
1153 		    && p_child->common.dvo_port <= DVO_PORT_MIPID
1154 		    &&p_child->common.device_type & DEVICE_TYPE_MIPI_OUTPUT) {
1155 			DRM_DEBUG_KMS("Found MIPI as LFP\n");
1156 			dev_priv->vbt.has_mipi = 1;
1157 			dev_priv->vbt.dsi.port = p_child->common.dvo_port;
1158 		}
1159 
1160 		child_dev_ptr = dev_priv->vbt.child_dev + count;
1161 		count++;
1162 
1163 		/*
1164 		 * Copy as much as we know (sizeof) and is available
1165 		 * (child_dev_size) of the child device. Accessing the data must
1166 		 * depend on VBT version.
1167 		 */
1168 		memcpy(child_dev_ptr, p_child,
1169 		       min_t(size_t, p_defs->child_dev_size, sizeof(*p_child)));
1170 	}
1171 	return;
1172 }
1173 
1174 static void
1175 init_vbt_defaults(struct drm_i915_private *dev_priv)
1176 {
1177 	struct drm_device *dev = dev_priv->dev;
1178 	enum port port;
1179 
1180 	dev_priv->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
1181 
1182 	/* Default to having backlight */
1183 	dev_priv->vbt.backlight.present = true;
1184 
1185 	/* LFP panel data */
1186 	dev_priv->vbt.lvds_dither = 1;
1187 	dev_priv->vbt.lvds_vbt = 0;
1188 
1189 	/* SDVO panel data */
1190 	dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1191 
1192 	/* general features */
1193 	dev_priv->vbt.int_tv_support = 1;
1194 	dev_priv->vbt.int_crt_support = 1;
1195 
1196 	/* Default to using SSC */
1197 	dev_priv->vbt.lvds_use_ssc = 1;
1198 	/*
1199 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
1200 	 * clock for LVDS.
1201 	 */
1202 	dev_priv->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(dev,
1203 			!HAS_PCH_SPLIT(dev));
1204 	DRM_DEBUG_KMS("Set default to SSC at %d kHz\n", dev_priv->vbt.lvds_ssc_freq);
1205 
1206 	for (port = PORT_A; port < I915_MAX_PORTS; port++) {
1207 		struct ddi_vbt_port_info *info =
1208 			&dev_priv->vbt.ddi_port_info[port];
1209 
1210 		info->hdmi_level_shift = HDMI_LEVEL_SHIFT_UNKNOWN;
1211 
1212 		info->supports_dvi = (port != PORT_A && port != PORT_E);
1213 		info->supports_hdmi = info->supports_dvi;
1214 		info->supports_dp = (port != PORT_E);
1215 	}
1216 }
1217 
1218 static int intel_no_opregion_vbt_callback(const struct dmi_system_id *id)
1219 {
1220 	DRM_DEBUG_KMS("Falling back to manually reading VBT from "
1221 		      "VBIOS ROM for %s\n",
1222 		      id->ident);
1223 	return 1;
1224 }
1225 
1226 static const struct dmi_system_id intel_no_opregion_vbt[] = {
1227 	{
1228 		.callback = intel_no_opregion_vbt_callback,
1229 		.ident = "ThinkCentre A57",
1230 		.matches = {
1231 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1232 			DMI_MATCH(DMI_PRODUCT_NAME, "97027RG"),
1233 		},
1234 	},
1235 	{ }
1236 };
1237 
1238 static const struct bdb_header *validate_vbt(const void *base,
1239 					     size_t size,
1240 					     const void *_vbt,
1241 					     const char *source)
1242 {
1243 	size_t offset = (const char *)_vbt - (const char *)base;
1244 	const struct vbt_header *vbt = _vbt;
1245 	const struct bdb_header *bdb;
1246 
1247 	if (offset + sizeof(struct vbt_header) > size) {
1248 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
1249 		return NULL;
1250 	}
1251 
1252 	if (memcmp(vbt->signature, "$VBT", 4)) {
1253 		DRM_DEBUG_DRIVER("VBT invalid signature\n");
1254 		return NULL;
1255 	}
1256 
1257 	offset += vbt->bdb_offset;
1258 	if (offset + sizeof(struct bdb_header) > size) {
1259 		DRM_DEBUG_DRIVER("BDB header incomplete\n");
1260 		return NULL;
1261 	}
1262 
1263 	bdb = (const struct bdb_header *)((const char *)base + offset);
1264 	if (offset + bdb->bdb_size > size) {
1265 		DRM_DEBUG_DRIVER("BDB incomplete\n");
1266 		return NULL;
1267 	}
1268 
1269 	DRM_DEBUG_KMS("Using VBT from %s: %20s\n",
1270 		      source, vbt->signature);
1271 	return bdb;
1272 }
1273 
1274 static const struct bdb_header *find_vbt(void __iomem *bios, size_t size)
1275 {
1276 	const struct bdb_header *bdb = NULL;
1277 	size_t i;
1278 
1279 	/* Scour memory looking for the VBT signature. */
1280 	for (i = 0; i + 4 < size; i++) {
1281 		if (ioread32(bios + i) == *((const u32 *) "$VBT")) {
1282 			/*
1283 			 * This is the one place where we explicitly discard the
1284 			 * address space (__iomem) of the BIOS/VBT. From now on
1285 			 * everything is based on 'base', and treated as regular
1286 			 * memory.
1287 			 */
1288 			void *_bios = (void __force *) bios;
1289 
1290 			bdb = validate_vbt(_bios, size, (const char *)_bios + i, "PCI ROM");
1291 			break;
1292 		}
1293 	}
1294 
1295 	return bdb;
1296 }
1297 
1298 /**
1299  * intel_parse_bios - find VBT and initialize settings from the BIOS
1300  * @dev: DRM device
1301  *
1302  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
1303  * to appropriate values.
1304  *
1305  * Returns 0 on success, nonzero on failure.
1306  */
1307 int
1308 intel_parse_bios(struct drm_device *dev)
1309 {
1310 	struct drm_i915_private *dev_priv = dev->dev_private;
1311 #if 0
1312 	struct pci_dev *pdev = dev->pdev;
1313 #endif
1314 	const struct bdb_header *bdb = NULL;
1315 	u8 __iomem *bios = NULL;
1316 
1317 	if (HAS_PCH_NOP(dev))
1318 		return -ENODEV;
1319 
1320 	init_vbt_defaults(dev_priv);
1321 
1322 	/* XXX Should this validation be moved to intel_opregion.c? */
1323 	if (!dmi_check_system(intel_no_opregion_vbt) && dev_priv->opregion.vbt)
1324 		bdb = validate_vbt(dev_priv->opregion.header, OPREGION_SIZE,
1325 				   dev_priv->opregion.vbt, "OpRegion");
1326 
1327 	if (bdb == NULL) {
1328 		size_t size;
1329 
1330 #if 0
1331 		bios = pci_map_rom(pdev, &size);
1332 		if (!bios)
1333 #endif
1334 			return -1;
1335 
1336 		bdb = find_vbt(bios, size);
1337 		if (!bdb) {
1338 #if 0
1339 			pci_unmap_rom(pdev, bios);
1340 #endif
1341 			return -1;
1342 		}
1343 	}
1344 
1345 	/* Grab useful general definitions */
1346 	parse_general_features(dev_priv, bdb);
1347 	parse_general_definitions(dev_priv, bdb);
1348 	parse_lfp_panel_data(dev_priv, bdb);
1349 	parse_lfp_backlight(dev_priv, bdb);
1350 	parse_sdvo_panel_data(dev_priv, bdb);
1351 	parse_sdvo_device_mapping(dev_priv, bdb);
1352 	parse_device_mapping(dev_priv, bdb);
1353 	parse_driver_features(dev_priv, bdb);
1354 	parse_edp(dev_priv, bdb);
1355 	parse_psr(dev_priv, bdb);
1356 	parse_mipi(dev_priv, bdb);
1357 	parse_ddi_ports(dev_priv, bdb);
1358 
1359 #if 0
1360 	if (bios)
1361 		pci_unmap_rom(pdev, bios);
1362 #endif
1363 
1364 	return 0;
1365 }
1366