xref: /dragonfly/sys/dev/drm/i915/intel_bios.c (revision 896f2e3a)
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 <drm/drmP.h>
28 #include <drm/drm_dp_helper.h>
29 #include <drm/i915_drm.h>
30 #include "i915_drv.h"
31 #include "intel_bios.h"
32 
33 #define	SLAVE_ADDR1	0x70
34 #define	SLAVE_ADDR2	0x72
35 
36 static int panel_type;
37 
38 static void *
39 find_section(struct bdb_header *bdb, int section_id)
40 {
41 	u8 *base = (u8 *)bdb;
42 	int index = 0;
43 	u16 total, current_size;
44 	u8 current_id;
45 
46 	/* skip to first section */
47 	index += bdb->header_size;
48 	total = bdb->bdb_size;
49 
50 	/* walk the sections looking for section_id */
51 	while (index < total) {
52 		current_id = *(base + index);
53 		index++;
54 		current_size = *((u16 *)(base + index));
55 		index += 2;
56 		if (current_id == section_id)
57 			return base + index;
58 		index += current_size;
59 	}
60 
61 	return NULL;
62 }
63 
64 static u16
65 get_blocksize(void *p)
66 {
67 	u16 *block_ptr, block_size;
68 
69 	block_ptr = (u16 *)((char *)p - 2);
70 	block_size = *block_ptr;
71 	return block_size;
72 }
73 
74 static void
75 fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
76 			const struct lvds_dvo_timing *dvo_timing)
77 {
78 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
79 		dvo_timing->hactive_lo;
80 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
81 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
82 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
83 		dvo_timing->hsync_pulse_width;
84 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
85 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
86 
87 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
88 		dvo_timing->vactive_lo;
89 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
90 		dvo_timing->vsync_off;
91 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
92 		dvo_timing->vsync_pulse_width;
93 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
94 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
95 	panel_fixed_mode->clock = dvo_timing->clock * 10;
96 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
97 
98 	if (dvo_timing->hsync_positive)
99 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
100 	else
101 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
102 
103 	if (dvo_timing->vsync_positive)
104 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
105 	else
106 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
107 
108 	/* Some VBTs have bogus h/vtotal values */
109 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
110 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
111 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
112 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
113 
114 	drm_mode_set_name(panel_fixed_mode);
115 }
116 
117 static bool
118 lvds_dvo_timing_equal_size(const struct lvds_dvo_timing *a,
119 			   const struct lvds_dvo_timing *b)
120 {
121 	if (a->hactive_hi != b->hactive_hi ||
122 	    a->hactive_lo != b->hactive_lo)
123 		return false;
124 
125 	if (a->hsync_off_hi != b->hsync_off_hi ||
126 	    a->hsync_off_lo != b->hsync_off_lo)
127 		return false;
128 
129 	if (a->hsync_pulse_width != b->hsync_pulse_width)
130 		return false;
131 
132 	if (a->hblank_hi != b->hblank_hi ||
133 	    a->hblank_lo != b->hblank_lo)
134 		return false;
135 
136 	if (a->vactive_hi != b->vactive_hi ||
137 	    a->vactive_lo != b->vactive_lo)
138 		return false;
139 
140 	if (a->vsync_off != b->vsync_off)
141 		return false;
142 
143 	if (a->vsync_pulse_width != b->vsync_pulse_width)
144 		return false;
145 
146 	if (a->vblank_hi != b->vblank_hi ||
147 	    a->vblank_lo != b->vblank_lo)
148 		return false;
149 
150 	return true;
151 }
152 
153 static const struct lvds_dvo_timing *
154 get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *lvds_lfp_data,
155 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs,
156 		    int index)
157 {
158 	/*
159 	 * the size of fp_timing varies on the different platform.
160 	 * So calculate the DVO timing relative offset in LVDS data
161 	 * entry to get the DVO timing entry
162 	 */
163 
164 	int lfp_data_size =
165 		lvds_lfp_data_ptrs->ptr[1].dvo_timing_offset -
166 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset;
167 	int dvo_timing_offset =
168 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset -
169 		lvds_lfp_data_ptrs->ptr[0].fp_timing_offset;
170 	const char *entry = (const char *)lvds_lfp_data->data + lfp_data_size * index;
171 
172 	return (const struct lvds_dvo_timing *)(entry + dvo_timing_offset);
173 }
174 
175 /* get lvds_fp_timing entry
176  * this function may return NULL if the corresponding entry is invalid
177  */
178 static const struct lvds_fp_timing *
179 get_lvds_fp_timing(const struct bdb_header *bdb,
180 		   const struct bdb_lvds_lfp_data *data,
181 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
182 		   int index)
183 {
184 	size_t data_ofs = (const u8 *)data - (const u8 *)bdb;
185 	u16 data_size = ((const u16 *)data)[-1]; /* stored in header */
186 	size_t ofs;
187 
188 	if (index >= ARRAY_SIZE(ptrs->ptr))
189 		return NULL;
190 	ofs = ptrs->ptr[index].fp_timing_offset;
191 	if (ofs < data_ofs ||
192 	    ofs + sizeof(struct lvds_fp_timing) > data_ofs + data_size)
193 		return NULL;
194 	return (const struct lvds_fp_timing *)((const u8 *)bdb + ofs);
195 }
196 
197 /* Try to find integrated panel data */
198 static void
199 parse_lfp_panel_data(struct drm_i915_private *dev_priv,
200 			    struct bdb_header *bdb)
201 {
202 	const struct bdb_lvds_options *lvds_options;
203 	const struct bdb_lvds_lfp_data *lvds_lfp_data;
204 	const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs;
205 	const struct lvds_dvo_timing *panel_dvo_timing;
206 	const struct lvds_fp_timing *fp_timing;
207 	struct drm_display_mode *panel_fixed_mode;
208 	int i, downclock;
209 
210 	lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
211 	if (!lvds_options)
212 		return;
213 
214 	dev_priv->vbt.lvds_dither = lvds_options->pixel_dither;
215 	if (lvds_options->panel_type == 0xff)
216 		return;
217 
218 	panel_type = lvds_options->panel_type;
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 	/*
246 	 * Iterate over the LVDS panel timing info to find the lowest clock
247 	 * for the native resolution.
248 	 */
249 	downclock = panel_dvo_timing->clock;
250 	for (i = 0; i < 16; i++) {
251 		const struct lvds_dvo_timing *dvo_timing;
252 
253 		dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
254 						 lvds_lfp_data_ptrs,
255 						 i);
256 		if (lvds_dvo_timing_equal_size(dvo_timing, panel_dvo_timing) &&
257 		    dvo_timing->clock < downclock)
258 			downclock = dvo_timing->clock;
259 	}
260 
261 	if (downclock < panel_dvo_timing->clock && i915_lvds_downclock) {
262 		dev_priv->lvds_downclock_avail = 1;
263 		dev_priv->lvds_downclock = downclock * 10;
264 		DRM_DEBUG_KMS("LVDS downclock is found in VBT. "
265 			      "Normal Clock %dKHz, downclock %dKHz\n",
266 			      panel_fixed_mode->clock, 10*downclock);
267 	}
268 
269 	fp_timing = get_lvds_fp_timing(bdb, lvds_lfp_data,
270 				       lvds_lfp_data_ptrs,
271 				       lvds_options->panel_type);
272 	if (fp_timing) {
273 		/* check the resolution, just to be sure */
274 		if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
275 		    fp_timing->y_res == panel_fixed_mode->vdisplay) {
276 			dev_priv->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
277 			DRM_DEBUG_KMS("VBT initial LVDS value %x\n",
278 				      dev_priv->vbt.bios_lvds_val);
279 		}
280 	}
281 }
282 
283 static void
284 parse_lfp_backlight(struct drm_i915_private *dev_priv, struct bdb_header *bdb)
285 {
286 	const struct bdb_lfp_backlight_data *backlight_data;
287 	const struct bdb_lfp_backlight_data_entry *entry;
288 
289 	backlight_data = find_section(bdb, BDB_LVDS_BACKLIGHT);
290 	if (!backlight_data)
291 		return;
292 
293 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
294 		DRM_DEBUG_KMS("Unsupported backlight data entry size %u\n",
295 			      backlight_data->entry_size);
296 		return;
297 	}
298 
299 	entry = &backlight_data->data[panel_type];
300 
301 	dev_priv->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
302 	dev_priv->vbt.backlight.active_low_pwm = entry->active_low_pwm;
303 	DRM_DEBUG_KMS("VBT backlight PWM modulation frequency %u Hz, "
304 		      "active %s, min brightness %u, level %u\n",
305 		      dev_priv->vbt.backlight.pwm_freq_hz,
306 		      dev_priv->vbt.backlight.active_low_pwm ? "low" : "high",
307 		      entry->min_brightness,
308 		      backlight_data->level[panel_type]);
309 }
310 
311 /* Try to find sdvo panel data */
312 static void
313 parse_sdvo_panel_data(struct drm_i915_private *dev_priv,
314 		      struct bdb_header *bdb)
315 {
316 	struct lvds_dvo_timing *dvo_timing;
317 	struct drm_display_mode *panel_fixed_mode;
318 	int index;
319 
320 	index = i915_vbt_sdvo_panel_type;
321 	if (index == -2) {
322 		DRM_DEBUG_KMS("Ignore SDVO panel mode from BIOS VBT tables.\n");
323 		return;
324 	}
325 
326 	if (index == -1) {
327 		struct bdb_sdvo_lvds_options *sdvo_lvds_options;
328 
329 		sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
330 		if (!sdvo_lvds_options)
331 			return;
332 
333 		index = sdvo_lvds_options->panel_type;
334 	}
335 
336 	dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
337 	if (!dvo_timing)
338 		return;
339 
340 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
341 	if (!panel_fixed_mode)
342 		return;
343 
344 	fill_detail_timing_data(panel_fixed_mode, dvo_timing + index);
345 
346 	dev_priv->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
347 
348 	DRM_DEBUG_KMS("Found SDVO panel mode in BIOS VBT tables:\n");
349 	drm_mode_debug_printmodeline(panel_fixed_mode);
350 }
351 
352 static int intel_bios_ssc_frequency(struct drm_device *dev,
353 				    bool alternate)
354 {
355 	switch (INTEL_INFO(dev)->gen) {
356 	case 2:
357 		return alternate ? 66667 : 48000;
358 	case 3:
359 	case 4:
360 		return alternate ? 100000 : 96000;
361 	default:
362 		return alternate ? 100000 : 120000;
363 	}
364 }
365 
366 static void
367 parse_general_features(struct drm_i915_private *dev_priv,
368 		       struct bdb_header *bdb)
369 {
370 	struct drm_device *dev = dev_priv->dev;
371 	struct bdb_general_features *general;
372 
373 	general = find_section(bdb, BDB_GENERAL_FEATURES);
374 	if (general) {
375 		dev_priv->vbt.int_tv_support = general->int_tv_support;
376 		dev_priv->vbt.int_crt_support = general->int_crt_support;
377 		dev_priv->vbt.lvds_use_ssc = general->enable_ssc;
378 		dev_priv->vbt.lvds_ssc_freq =
379 			intel_bios_ssc_frequency(dev, general->ssc_freq);
380 		dev_priv->vbt.display_clock_mode = general->display_clock_mode;
381 		dev_priv->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
382 		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",
383 			      dev_priv->vbt.int_tv_support,
384 			      dev_priv->vbt.int_crt_support,
385 			      dev_priv->vbt.lvds_use_ssc,
386 			      dev_priv->vbt.lvds_ssc_freq,
387 			      dev_priv->vbt.display_clock_mode,
388 			      dev_priv->vbt.fdi_rx_polarity_inverted);
389 	}
390 }
391 
392 static void
393 parse_general_definitions(struct drm_i915_private *dev_priv,
394 			  struct bdb_header *bdb)
395 {
396 	struct bdb_general_definitions *general;
397 
398 	general = find_section(bdb, BDB_GENERAL_DEFINITIONS);
399 	if (general) {
400 		u16 block_size = get_blocksize(general);
401 		if (block_size >= sizeof(*general)) {
402 			int bus_pin = general->crt_ddc_gmbus_pin;
403 			DRM_DEBUG_KMS("crt_ddc_bus_pin: %d\n", bus_pin);
404 			if (intel_gmbus_is_port_valid(bus_pin))
405 				dev_priv->vbt.crt_ddc_pin = bus_pin;
406 		} else {
407 			DRM_DEBUG_KMS("BDB_GD too small (%d). Invalid.\n",
408 				      block_size);
409 		}
410 	}
411 }
412 
413 static void
414 parse_sdvo_device_mapping(struct drm_i915_private *dev_priv,
415 			  struct bdb_header *bdb)
416 {
417 	struct sdvo_device_mapping *p_mapping;
418 	struct bdb_general_definitions *p_defs;
419 	union child_device_config *p_child;
420 	int i, child_device_num, count;
421 	u16	block_size;
422 
423 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
424 	if (!p_defs) {
425 		DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
426 		return;
427 	}
428 	/* judge whether the size of child device meets the requirements.
429 	 * If the child device size obtained from general definition block
430 	 * is different with sizeof(struct child_device_config), skip the
431 	 * parsing of sdvo device info
432 	 */
433 	if (p_defs->child_dev_size != sizeof(*p_child)) {
434 		/* different child dev size . Ignore it */
435 		DRM_DEBUG_KMS("different child size is found. Invalid.\n");
436 		return;
437 	}
438 	/* get the block size of general definitions */
439 	block_size = get_blocksize(p_defs);
440 	/* get the number of child device */
441 	child_device_num = (block_size - sizeof(*p_defs)) /
442 				sizeof(*p_child);
443 	count = 0;
444 	for (i = 0; i < child_device_num; i++) {
445 		p_child = &(p_defs->devices[i]);
446 		if (!p_child->old.device_type) {
447 			/* skip the device block if device type is invalid */
448 			continue;
449 		}
450 		if (p_child->old.slave_addr != SLAVE_ADDR1 &&
451 			p_child->old.slave_addr != SLAVE_ADDR2) {
452 			/*
453 			 * If the slave address is neither 0x70 nor 0x72,
454 			 * it is not a SDVO device. Skip it.
455 			 */
456 			continue;
457 		}
458 		if (p_child->old.dvo_port != DEVICE_PORT_DVOB &&
459 			p_child->old.dvo_port != DEVICE_PORT_DVOC) {
460 			/* skip the incorrect SDVO port */
461 			DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
462 			continue;
463 		}
464 		DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
465 				" %s port\n",
466 				p_child->old.slave_addr,
467 				(p_child->old.dvo_port == DEVICE_PORT_DVOB) ?
468 					"SDVOB" : "SDVOC");
469 		p_mapping = &(dev_priv->sdvo_mappings[p_child->old.dvo_port - 1]);
470 		if (!p_mapping->initialized) {
471 			p_mapping->dvo_port = p_child->old.dvo_port;
472 			p_mapping->slave_addr = p_child->old.slave_addr;
473 			p_mapping->dvo_wiring = p_child->old.dvo_wiring;
474 			p_mapping->ddc_pin = p_child->old.ddc_pin;
475 			p_mapping->i2c_pin = p_child->old.i2c_pin;
476 			p_mapping->initialized = 1;
477 			DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
478 				      p_mapping->dvo_port,
479 				      p_mapping->slave_addr,
480 				      p_mapping->dvo_wiring,
481 				      p_mapping->ddc_pin,
482 				      p_mapping->i2c_pin);
483 		} else {
484 			DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
485 					 "two SDVO device.\n");
486 		}
487 		if (p_child->old.slave2_addr) {
488 			/* Maybe this is a SDVO device with multiple inputs */
489 			/* And the mapping info is not added */
490 			DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
491 				" is a SDVO device with multiple inputs.\n");
492 		}
493 		count++;
494 	}
495 
496 	if (!count) {
497 		/* No SDVO device info is found */
498 		DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
499 	}
500 	return;
501 }
502 
503 static void
504 parse_driver_features(struct drm_i915_private *dev_priv,
505 		       struct bdb_header *bdb)
506 {
507 	struct bdb_driver_features *driver;
508 
509 	driver = find_section(bdb, BDB_DRIVER_FEATURES);
510 	if (!driver)
511 		return;
512 
513 	if (driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
514 		dev_priv->vbt.edp_support = 1;
515 
516 	if (driver->dual_frequency)
517 		dev_priv->render_reclock_avail = true;
518 }
519 
520 static void
521 parse_edp(struct drm_i915_private *dev_priv, struct bdb_header *bdb)
522 {
523 	struct bdb_edp *edp;
524 	struct edp_power_seq *edp_pps;
525 	struct edp_link_params *edp_link_params;
526 
527 	edp = find_section(bdb, BDB_EDP);
528 	if (!edp) {
529 		if (dev_priv->vbt.edp_support)
530 			DRM_DEBUG_KMS("No eDP BDB found but eDP panel supported.\n");
531 		return;
532 	}
533 
534 	switch ((edp->color_depth >> (panel_type * 2)) & 3) {
535 	case EDP_18BPP:
536 		dev_priv->vbt.edp_bpp = 18;
537 		break;
538 	case EDP_24BPP:
539 		dev_priv->vbt.edp_bpp = 24;
540 		break;
541 	case EDP_30BPP:
542 		dev_priv->vbt.edp_bpp = 30;
543 		break;
544 	}
545 
546 	/* Get the eDP sequencing and link info */
547 	edp_pps = &edp->power_seqs[panel_type];
548 	edp_link_params = &edp->link_params[panel_type];
549 
550 	dev_priv->vbt.edp_pps = *edp_pps;
551 
552 	dev_priv->vbt.edp_rate = edp_link_params->rate ? DP_LINK_BW_2_7 :
553 		DP_LINK_BW_1_62;
554 	switch (edp_link_params->lanes) {
555 	case 0:
556 		dev_priv->vbt.edp_lanes = 1;
557 		break;
558 	case 1:
559 		dev_priv->vbt.edp_lanes = 2;
560 		break;
561 	case 3:
562 	default:
563 		dev_priv->vbt.edp_lanes = 4;
564 		break;
565 	}
566 	switch (edp_link_params->preemphasis) {
567 	case 0:
568 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPHASIS_0;
569 		break;
570 	case 1:
571 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPHASIS_3_5;
572 		break;
573 	case 2:
574 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPHASIS_6;
575 		break;
576 	case 3:
577 		dev_priv->vbt.edp_preemphasis = DP_TRAIN_PRE_EMPHASIS_9_5;
578 		break;
579 	}
580 	switch (edp_link_params->vswing) {
581 	case 0:
582 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_400;
583 		break;
584 	case 1:
585 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_600;
586 		break;
587 	case 2:
588 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_800;
589 		break;
590 	case 3:
591 		dev_priv->vbt.edp_vswing = DP_TRAIN_VOLTAGE_SWING_1200;
592 		break;
593 	}
594 }
595 
596 static void
597 parse_mipi(struct drm_i915_private *dev_priv, struct bdb_header *bdb)
598 {
599 	struct bdb_mipi *mipi;
600 
601 	mipi = find_section(bdb, BDB_MIPI);
602 	if (!mipi) {
603 		DRM_DEBUG_KMS("No MIPI BDB found");
604 		return;
605 	}
606 
607 	/* XXX: add more info */
608 	dev_priv->vbt.dsi.panel_id = mipi->panel_id;
609 }
610 
611 static void parse_ddi_port(struct drm_i915_private *dev_priv, enum port port,
612 			   struct bdb_header *bdb)
613 {
614 	union child_device_config *it, *child = NULL;
615 	struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
616 	uint8_t hdmi_level_shift;
617 	int i, j;
618 	bool is_dvi, is_hdmi, is_dp, is_edp, is_crt;
619 	uint8_t aux_channel;
620 	/* Each DDI port can have more than one value on the "DVO Port" field,
621 	 * so look for all the possible values for each port and abort if more
622 	 * than one is found. */
623 	int dvo_ports[][2] = {
624 		{DVO_PORT_HDMIA, DVO_PORT_DPA},
625 		{DVO_PORT_HDMIB, DVO_PORT_DPB},
626 		{DVO_PORT_HDMIC, DVO_PORT_DPC},
627 		{DVO_PORT_HDMID, DVO_PORT_DPD},
628 		{DVO_PORT_CRT, -1 /* Port E can only be DVO_PORT_CRT */ },
629 	};
630 
631 	/* Find the child device to use, abort if more than one found. */
632 	for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
633 		it = dev_priv->vbt.child_dev + i;
634 
635 		for (j = 0; j < 2; j++) {
636 			if (dvo_ports[port][j] == -1)
637 				break;
638 
639 			if (it->common.dvo_port == dvo_ports[port][j]) {
640 				if (child) {
641 					DRM_DEBUG_KMS("More than one child device for port %c in VBT.\n",
642 						      port_name(port));
643 					return;
644 				}
645 				child = it;
646 			}
647 		}
648 	}
649 	if (!child)
650 		return;
651 
652 	aux_channel = child->raw[25];
653 
654 	is_dvi = child->common.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
655 	is_dp = child->common.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
656 	is_crt = child->common.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
657 	is_hdmi = is_dvi && (child->common.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
658 	is_edp = is_dp && (child->common.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR);
659 
660 	info->supports_dvi = is_dvi;
661 	info->supports_hdmi = is_hdmi;
662 	info->supports_dp = is_dp;
663 
664 	DRM_DEBUG_KMS("Port %c VBT info: DP:%d HDMI:%d DVI:%d EDP:%d CRT:%d\n",
665 		      port_name(port), is_dp, is_hdmi, is_dvi, is_edp, is_crt);
666 
667 	if (is_edp && is_dvi)
668 		DRM_DEBUG_KMS("Internal DP port %c is TMDS compatible\n",
669 			      port_name(port));
670 	if (is_crt && port != PORT_E)
671 		DRM_DEBUG_KMS("Port %c is analog\n", port_name(port));
672 	if (is_crt && (is_dvi || is_dp))
673 		DRM_DEBUG_KMS("Analog port %c is also DP or TMDS compatible\n",
674 			      port_name(port));
675 	if (is_dvi && (port == PORT_A || port == PORT_E))
676 		DRM_DEBUG_KMS("Port %c is TMDS compabile\n", port_name(port));
677 	if (!is_dvi && !is_dp && !is_crt)
678 		DRM_DEBUG_KMS("Port %c is not DP/TMDS/CRT compatible\n",
679 			      port_name(port));
680 	if (is_edp && (port == PORT_B || port == PORT_C || port == PORT_E))
681 		DRM_DEBUG_KMS("Port %c is internal DP\n", port_name(port));
682 
683 	if (is_dvi) {
684 		if (child->common.ddc_pin == 0x05 && port != PORT_B)
685 			DRM_DEBUG_KMS("Unexpected DDC pin for port B\n");
686 		if (child->common.ddc_pin == 0x04 && port != PORT_C)
687 			DRM_DEBUG_KMS("Unexpected DDC pin for port C\n");
688 		if (child->common.ddc_pin == 0x06 && port != PORT_D)
689 			DRM_DEBUG_KMS("Unexpected DDC pin for port D\n");
690 	}
691 
692 	if (is_dp) {
693 		if (aux_channel == 0x40 && port != PORT_A)
694 			DRM_DEBUG_KMS("Unexpected AUX channel for port A\n");
695 		if (aux_channel == 0x10 && port != PORT_B)
696 			DRM_DEBUG_KMS("Unexpected AUX channel for port B\n");
697 		if (aux_channel == 0x20 && port != PORT_C)
698 			DRM_DEBUG_KMS("Unexpected AUX channel for port C\n");
699 		if (aux_channel == 0x30 && port != PORT_D)
700 			DRM_DEBUG_KMS("Unexpected AUX channel for port D\n");
701 	}
702 
703 	if (bdb->version >= 158) {
704 		/* The VBT HDMI level shift values match the table we have. */
705 		hdmi_level_shift = child->raw[7] & 0xF;
706 		if (hdmi_level_shift < 0xC) {
707 			DRM_DEBUG_KMS("VBT HDMI level shift for port %c: %d\n",
708 				      port_name(port),
709 				      hdmi_level_shift);
710 			info->hdmi_level_shift = hdmi_level_shift;
711 		}
712 	}
713 }
714 
715 static void parse_ddi_ports(struct drm_i915_private *dev_priv,
716 			    struct bdb_header *bdb)
717 {
718 	struct drm_device *dev = dev_priv->dev;
719 	enum port port;
720 
721 	if (!HAS_DDI(dev))
722 		return;
723 
724 	if (!dev_priv->vbt.child_dev_num)
725 		return;
726 
727 	if (bdb->version < 155)
728 		return;
729 
730 	for (port = PORT_A; port < I915_MAX_PORTS; port++)
731 		parse_ddi_port(dev_priv, port, bdb);
732 }
733 
734 static void
735 parse_device_mapping(struct drm_i915_private *dev_priv,
736 		       struct bdb_header *bdb)
737 {
738 	struct bdb_general_definitions *p_defs;
739 	union child_device_config *p_child, *child_dev_ptr;
740 	int i, child_device_num, count;
741 	u16	block_size;
742 
743 	p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
744 	if (!p_defs) {
745 		DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
746 		return;
747 	}
748 	/* judge whether the size of child device meets the requirements.
749 	 * If the child device size obtained from general definition block
750 	 * is different with sizeof(struct child_device_config), skip the
751 	 * parsing of sdvo device info
752 	 */
753 	if (p_defs->child_dev_size != sizeof(*p_child)) {
754 		/* different child dev size . Ignore it */
755 		DRM_DEBUG_KMS("different child size is found. Invalid.\n");
756 		return;
757 	}
758 	/* get the block size of general definitions */
759 	block_size = get_blocksize(p_defs);
760 	/* get the number of child device */
761 	child_device_num = (block_size - sizeof(*p_defs)) /
762 				sizeof(*p_child);
763 	count = 0;
764 	/* get the number of child device that is present */
765 	for (i = 0; i < child_device_num; i++) {
766 		p_child = &(p_defs->devices[i]);
767 		if (!p_child->common.device_type) {
768 			/* skip the device block if device type is invalid */
769 			continue;
770 		}
771 		count++;
772 	}
773 	if (!count) {
774 		DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
775 		return;
776 	}
777 	dev_priv->vbt.child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
778 	if (!dev_priv->vbt.child_dev) {
779 		DRM_DEBUG_KMS("No memory space for child device\n");
780 		return;
781 	}
782 
783 	dev_priv->vbt.child_dev_num = count;
784 	count = 0;
785 	for (i = 0; i < child_device_num; i++) {
786 		p_child = &(p_defs->devices[i]);
787 		if (!p_child->common.device_type) {
788 			/* skip the device block if device type is invalid */
789 			continue;
790 		}
791 		child_dev_ptr = dev_priv->vbt.child_dev + count;
792 		count++;
793 		memcpy((void *)child_dev_ptr, (void *)p_child,
794 					sizeof(*p_child));
795 	}
796 	return;
797 }
798 
799 static void
800 init_vbt_defaults(struct drm_i915_private *dev_priv)
801 {
802 	struct drm_device *dev = dev_priv->dev;
803 	enum port port;
804 
805 	dev_priv->vbt.crt_ddc_pin = GMBUS_PORT_VGADDC;
806 
807 	/* LFP panel data */
808 	dev_priv->vbt.lvds_dither = 1;
809 	dev_priv->vbt.lvds_vbt = 0;
810 
811 	/* SDVO panel data */
812 	dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
813 
814 	/* general features */
815 	dev_priv->vbt.int_tv_support = 1;
816 	dev_priv->vbt.int_crt_support = 1;
817 
818 	/* Default to using SSC */
819 	dev_priv->vbt.lvds_use_ssc = 1;
820 	/*
821 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
822 	 * clock for LVDS.
823 	 */
824 	dev_priv->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(dev,
825 			!HAS_PCH_SPLIT(dev));
826 	DRM_DEBUG_KMS("Set default to SSC at %d kHz\n", dev_priv->vbt.lvds_ssc_freq);
827 
828 	for (port = PORT_A; port < I915_MAX_PORTS; port++) {
829 		struct ddi_vbt_port_info *info =
830 			&dev_priv->vbt.ddi_port_info[port];
831 
832 		/* Recommended BSpec default: 800mV 0dB. */
833 		info->hdmi_level_shift = 6;
834 
835 		info->supports_dvi = (port != PORT_A && port != PORT_E);
836 		info->supports_hdmi = info->supports_dvi;
837 		info->supports_dp = (port != PORT_E);
838 	}
839 }
840 
841 static int __init intel_no_opregion_vbt_callback(const struct dmi_system_id *id)
842 {
843 	DRM_DEBUG_KMS("Falling back to manually reading VBT from "
844 		      "VBIOS ROM for %s\n",
845 		      id->ident);
846 	return 1;
847 }
848 
849 static const struct dmi_system_id intel_no_opregion_vbt[] = {
850 	{
851 		.callback = intel_no_opregion_vbt_callback,
852 		.ident = "ThinkCentre A57",
853 		.matches = {
854 			DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),
855 			DMI_MATCH(DMI_PRODUCT_NAME, "97027RG"),
856 		},
857 	},
858 	{ }
859 };
860 
861 /**
862  * intel_parse_bios - find VBT and initialize settings from the BIOS
863  * @dev: DRM device
864  *
865  * Loads the Video BIOS and checks that the VBT exists.  Sets scratch registers
866  * to appropriate values.
867  *
868  * Returns 0 on success, nonzero on failure.
869  */
870 int
871 intel_parse_bios(struct drm_device *dev)
872 {
873 	struct drm_i915_private *dev_priv = dev->dev_private;
874 	struct bdb_header *bdb = NULL;
875 	u8 __iomem *bios = NULL;
876 
877 	if (HAS_PCH_NOP(dev))
878 		return -ENODEV;
879 
880 	init_vbt_defaults(dev_priv);
881 
882 	/* XXX Should this validation be moved to intel_opregion.c? */
883 	if (!dmi_check_system(intel_no_opregion_vbt) && dev_priv->opregion.vbt) {
884 		struct vbt_header *vbt = dev_priv->opregion.vbt;
885 		if (memcmp(vbt->signature, "$VBT", 4) == 0) {
886 			DRM_DEBUG_KMS("Using VBT from OpRegion: %20s\n",
887 					 vbt->signature);
888 			bdb = (struct bdb_header *)((char *)vbt + vbt->bdb_offset);
889 		} else
890 			dev_priv->opregion.vbt = NULL;
891 	}
892 	bios = NULL;
893 
894 #if 1
895 	if (bdb == NULL) {
896 		KIB_NOTYET();
897 		return (-1);
898 	}
899 #else
900 	if (bdb == NULL) {
901 		struct vbt_header *vbt = NULL;
902 		size_t size;
903 		int i;
904 
905 		bios = pci_map_rom(pdev, &size);
906 		if (!bios)
907 			return -1;
908 
909 		/* Scour memory looking for the VBT signature */
910 		for (i = 0; i + 4 < size; i++) {
911 			if (!memcmp(bios + i, "$VBT", 4)) {
912 				vbt = (struct vbt_header *)(bios + i);
913 				break;
914 			}
915 		}
916 
917 		if (!vbt) {
918 			DRM_DEBUG_DRIVER("VBT signature missing\n");
919 			pci_unmap_rom(pdev, bios);
920 			return -1;
921 		}
922 
923 		bdb = (struct bdb_header *)(bios + i + vbt->bdb_offset);
924 	}
925 #endif
926 
927 	/* Grab useful general definitions */
928 	parse_general_features(dev_priv, bdb);
929 	parse_general_definitions(dev_priv, bdb);
930 	parse_lfp_panel_data(dev_priv, bdb);
931 	parse_lfp_backlight(dev_priv, bdb);
932 	parse_sdvo_panel_data(dev_priv, bdb);
933 	parse_sdvo_device_mapping(dev_priv, bdb);
934 	parse_device_mapping(dev_priv, bdb);
935 	parse_driver_features(dev_priv, bdb);
936 	parse_edp(dev_priv, bdb);
937 	parse_mipi(dev_priv, bdb);
938 	parse_ddi_ports(dev_priv, bdb);
939 
940 #if 0
941 	if (bios)
942 		pci_unmap_rom(pdev, bios);
943 #endif
944 
945 	return 0;
946 }
947 
948 /* Ensure that vital registers have been initialised, even if the BIOS
949  * is absent or just failing to do its job.
950  */
951 void intel_setup_bios(struct drm_device *dev)
952 {
953 	struct drm_i915_private *dev_priv = dev->dev_private;
954 
955 	 /* Set the Panel Power On/Off timings if uninitialized. */
956 	if (!HAS_PCH_SPLIT(dev) &&
957 	    I915_READ(PP_ON_DELAYS) == 0 && I915_READ(PP_OFF_DELAYS) == 0) {
958 		/* Set T2 to 40ms and T5 to 200ms */
959 		I915_WRITE(PP_ON_DELAYS, 0x019007d0);
960 
961 		/* Set T3 to 35ms and Tx to 200ms */
962 		I915_WRITE(PP_OFF_DELAYS, 0x015e07d0);
963 	}
964 }
965