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