GNU Linux-libre 4.9.337-gnu1
[releases.git] / drivers / gpu / drm / i915 / intel_bios.c
1 /*
2  * Copyright © 2006 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 #include <drm/drm_dp_helper.h>
29 #include <drm/drmP.h>
30 #include <drm/i915_drm.h>
31 #include "i915_drv.h"
32
33 #define _INTEL_BIOS_PRIVATE
34 #include "intel_vbt_defs.h"
35
36 /**
37  * DOC: Video BIOS Table (VBT)
38  *
39  * The Video BIOS Table, or VBT, provides platform and board specific
40  * configuration information to the driver that is not discoverable or available
41  * through other means. The configuration is mostly related to display
42  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
43  * the PCI ROM.
44  *
45  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
46  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
47  * contain the actual configuration information. The VBT Header, and thus the
48  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
49  * BDB Header. The data blocks are concatenated after the BDB Header. The data
50  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
51  * data. (Block 53, the MIPI Sequence Block is an exception.)
52  *
53  * The driver parses the VBT during load. The relevant information is stored in
54  * driver private data for ease of use, and the actual VBT is not read after
55  * that.
56  */
57
58 #define SLAVE_ADDR1     0x70
59 #define SLAVE_ADDR2     0x72
60
61 /* Get BDB block size given a pointer to Block ID. */
62 static u32 _get_blocksize(const u8 *block_base)
63 {
64         /* The MIPI Sequence Block v3+ has a separate size field. */
65         if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
66                 return *((const u32 *)(block_base + 4));
67         else
68                 return *((const u16 *)(block_base + 1));
69 }
70
71 /* Get BDB block size give a pointer to data after Block ID and Block Size. */
72 static u32 get_blocksize(const void *block_data)
73 {
74         return _get_blocksize(block_data - 3);
75 }
76
77 static const void *
78 find_section(const void *_bdb, int section_id)
79 {
80         const struct bdb_header *bdb = _bdb;
81         const u8 *base = _bdb;
82         int index = 0;
83         u32 total, current_size;
84         u8 current_id;
85
86         /* skip to first section */
87         index += bdb->header_size;
88         total = bdb->bdb_size;
89
90         /* walk the sections looking for section_id */
91         while (index + 3 < total) {
92                 current_id = *(base + index);
93                 current_size = _get_blocksize(base + index);
94                 index += 3;
95
96                 if (index + current_size > total)
97                         return NULL;
98
99                 if (current_id == section_id)
100                         return base + index;
101
102                 index += current_size;
103         }
104
105         return NULL;
106 }
107
108 static void
109 fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
110                         const struct lvds_dvo_timing *dvo_timing)
111 {
112         panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
113                 dvo_timing->hactive_lo;
114         panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
115                 ((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
116         panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
117                 dvo_timing->hsync_pulse_width;
118         panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
119                 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
120
121         panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
122                 dvo_timing->vactive_lo;
123         panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
124                 dvo_timing->vsync_off;
125         panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
126                 dvo_timing->vsync_pulse_width;
127         panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
128                 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
129         panel_fixed_mode->clock = dvo_timing->clock * 10;
130         panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
131
132         if (dvo_timing->hsync_positive)
133                 panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
134         else
135                 panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
136
137         if (dvo_timing->vsync_positive)
138                 panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
139         else
140                 panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
141
142         panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
143                 dvo_timing->himage_lo;
144         panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
145                 dvo_timing->vimage_lo;
146
147         /* Some VBTs have bogus h/vtotal values */
148         if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
149                 panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
150         if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
151                 panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
152
153         drm_mode_set_name(panel_fixed_mode);
154 }
155
156 static const struct lvds_dvo_timing *
157 get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *lvds_lfp_data,
158                     const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs,
159                     int index)
160 {
161         /*
162          * the size of fp_timing varies on the different platform.
163          * So calculate the DVO timing relative offset in LVDS data
164          * entry to get the DVO timing entry
165          */
166
167         int lfp_data_size =
168                 lvds_lfp_data_ptrs->ptr[1].dvo_timing_offset -
169                 lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset;
170         int dvo_timing_offset =
171                 lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset -
172                 lvds_lfp_data_ptrs->ptr[0].fp_timing_offset;
173         char *entry = (char *)lvds_lfp_data->data + lfp_data_size * index;
174
175         return (struct lvds_dvo_timing *)(entry + dvo_timing_offset);
176 }
177
178 /* get lvds_fp_timing entry
179  * this function may return NULL if the corresponding entry is invalid
180  */
181 static const struct lvds_fp_timing *
182 get_lvds_fp_timing(const struct bdb_header *bdb,
183                    const struct bdb_lvds_lfp_data *data,
184                    const struct bdb_lvds_lfp_data_ptrs *ptrs,
185                    int index)
186 {
187         size_t data_ofs = (const u8 *)data - (const u8 *)bdb;
188         u16 data_size = ((const u16 *)data)[-1]; /* stored in header */
189         size_t ofs;
190
191         if (index >= ARRAY_SIZE(ptrs->ptr))
192                 return NULL;
193         ofs = ptrs->ptr[index].fp_timing_offset;
194         if (ofs < data_ofs ||
195             ofs + sizeof(struct lvds_fp_timing) > data_ofs + data_size)
196                 return NULL;
197         return (const struct lvds_fp_timing *)((const u8 *)bdb + ofs);
198 }
199
200 /* Try to find integrated panel data */
201 static void
202 parse_lfp_panel_data(struct drm_i915_private *dev_priv,
203                      const struct bdb_header *bdb)
204 {
205         const struct bdb_lvds_options *lvds_options;
206         const struct bdb_lvds_lfp_data *lvds_lfp_data;
207         const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs;
208         const struct lvds_dvo_timing *panel_dvo_timing;
209         const struct lvds_fp_timing *fp_timing;
210         struct drm_display_mode *panel_fixed_mode;
211         int panel_type;
212         int drrs_mode;
213         int ret;
214
215         lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
216         if (!lvds_options)
217                 return;
218
219         dev_priv->vbt.lvds_dither = lvds_options->pixel_dither;
220
221         ret = intel_opregion_get_panel_type(dev_priv);
222         if (ret >= 0) {
223                 WARN_ON(ret > 0xf);
224                 panel_type = ret;
225                 DRM_DEBUG_KMS("Panel type: %d (OpRegion)\n", panel_type);
226         } else {
227                 if (lvds_options->panel_type > 0xf) {
228                         DRM_DEBUG_KMS("Invalid VBT panel type 0x%x\n",
229                                       lvds_options->panel_type);
230                         return;
231                 }
232                 panel_type = lvds_options->panel_type;
233                 DRM_DEBUG_KMS("Panel type: %d (VBT)\n", panel_type);
234         }
235
236         dev_priv->vbt.panel_type = panel_type;
237
238         drrs_mode = (lvds_options->dps_panel_type_bits
239                                 >> (panel_type * 2)) & MODE_MASK;
240         /*
241          * VBT has static DRRS = 0 and seamless DRRS = 2.
242          * The below piece of code is required to adjust vbt.drrs_type
243          * to match the enum drrs_support_type.
244          */
245         switch (drrs_mode) {
246         case 0:
247                 dev_priv->vbt.drrs_type = STATIC_DRRS_SUPPORT;
248                 DRM_DEBUG_KMS("DRRS supported mode is static\n");
249                 break;
250         case 2:
251                 dev_priv->vbt.drrs_type = SEAMLESS_DRRS_SUPPORT;
252                 DRM_DEBUG_KMS("DRRS supported mode is seamless\n");
253                 break;
254         default:
255                 dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
256                 DRM_DEBUG_KMS("DRRS not supported (VBT input)\n");
257                 break;
258         }
259
260         lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
261         if (!lvds_lfp_data)
262                 return;
263
264         lvds_lfp_data_ptrs = find_section(bdb, BDB_LVDS_LFP_DATA_PTRS);
265         if (!lvds_lfp_data_ptrs)
266                 return;
267
268         dev_priv->vbt.lvds_vbt = 1;
269
270         panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
271                                                lvds_lfp_data_ptrs,
272                                                panel_type);
273
274         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
275         if (!panel_fixed_mode)
276                 return;
277
278         fill_detail_timing_data(panel_fixed_mode, panel_dvo_timing);
279
280         dev_priv->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
281
282         DRM_DEBUG_KMS("Found panel mode in BIOS VBT tables:\n");
283         drm_mode_debug_printmodeline(panel_fixed_mode);
284
285         fp_timing = get_lvds_fp_timing(bdb, lvds_lfp_data,
286                                        lvds_lfp_data_ptrs,
287                                        panel_type);
288         if (fp_timing) {
289                 /* check the resolution, just to be sure */
290                 if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
291                     fp_timing->y_res == panel_fixed_mode->vdisplay) {
292                         dev_priv->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
293                         DRM_DEBUG_KMS("VBT initial LVDS value %x\n",
294                                       dev_priv->vbt.bios_lvds_val);
295                 }
296         }
297 }
298
299 static void
300 parse_lfp_backlight(struct drm_i915_private *dev_priv,
301                     const struct bdb_header *bdb)
302 {
303         const struct bdb_lfp_backlight_data *backlight_data;
304         const struct bdb_lfp_backlight_data_entry *entry;
305         int panel_type = dev_priv->vbt.panel_type;
306
307         backlight_data = find_section(bdb, BDB_LVDS_BACKLIGHT);
308         if (!backlight_data)
309                 return;
310
311         if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
312                 DRM_DEBUG_KMS("Unsupported backlight data entry size %u\n",
313                               backlight_data->entry_size);
314                 return;
315         }
316
317         entry = &backlight_data->data[panel_type];
318
319         dev_priv->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
320         if (!dev_priv->vbt.backlight.present) {
321                 DRM_DEBUG_KMS("PWM backlight not present in VBT (type %u)\n",
322                               entry->type);
323                 return;
324         }
325
326         dev_priv->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
327         if (bdb->version >= 191 &&
328             get_blocksize(backlight_data) >= sizeof(*backlight_data)) {
329                 const struct bdb_lfp_backlight_control_method *method;
330
331                 method = &backlight_data->backlight_control[panel_type];
332                 dev_priv->vbt.backlight.type = method->type;
333         }
334
335         dev_priv->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
336         dev_priv->vbt.backlight.active_low_pwm = entry->active_low_pwm;
337         dev_priv->vbt.backlight.min_brightness = entry->min_brightness;
338         DRM_DEBUG_KMS("VBT backlight PWM modulation frequency %u Hz, "
339                       "active %s, min brightness %u, level %u\n",
340                       dev_priv->vbt.backlight.pwm_freq_hz,
341                       dev_priv->vbt.backlight.active_low_pwm ? "low" : "high",
342                       dev_priv->vbt.backlight.min_brightness,
343                       backlight_data->level[panel_type]);
344 }
345
346 /* Try to find sdvo panel data */
347 static void
348 parse_sdvo_panel_data(struct drm_i915_private *dev_priv,
349                       const struct bdb_header *bdb)
350 {
351         const struct lvds_dvo_timing *dvo_timing;
352         struct drm_display_mode *panel_fixed_mode;
353         int index;
354
355         index = i915.vbt_sdvo_panel_type;
356         if (index == -2) {
357                 DRM_DEBUG_KMS("Ignore SDVO panel mode from BIOS VBT tables.\n");
358                 return;
359         }
360
361         if (index == -1) {
362                 const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
363
364                 sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
365                 if (!sdvo_lvds_options)
366                         return;
367
368                 index = sdvo_lvds_options->panel_type;
369         }
370
371         dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
372         if (!dvo_timing)
373                 return;
374
375         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
376         if (!panel_fixed_mode)
377                 return;
378
379         fill_detail_timing_data(panel_fixed_mode, dvo_timing + index);
380
381         dev_priv->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
382
383         DRM_DEBUG_KMS("Found SDVO panel mode in BIOS VBT tables:\n");
384         drm_mode_debug_printmodeline(panel_fixed_mode);
385 }
386
387 static int intel_bios_ssc_frequency(struct drm_i915_private *dev_priv,
388                                     bool alternate)
389 {
390         switch (INTEL_INFO(dev_priv)->gen) {
391         case 2:
392                 return alternate ? 66667 : 48000;
393         case 3:
394         case 4:
395                 return alternate ? 100000 : 96000;
396         default:
397                 return alternate ? 100000 : 120000;
398         }
399 }
400
401 static void
402 parse_general_features(struct drm_i915_private *dev_priv,
403                        const struct bdb_header *bdb)
404 {
405         const struct bdb_general_features *general;
406
407         general = find_section(bdb, BDB_GENERAL_FEATURES);
408         if (!general)
409                 return;
410
411         dev_priv->vbt.int_tv_support = general->int_tv_support;
412         /* int_crt_support can't be trusted on earlier platforms */
413         if (bdb->version >= 155 &&
414             (HAS_DDI(dev_priv) || IS_VALLEYVIEW(dev_priv)))
415                 dev_priv->vbt.int_crt_support = general->int_crt_support;
416         dev_priv->vbt.lvds_use_ssc = general->enable_ssc;
417         dev_priv->vbt.lvds_ssc_freq =
418                 intel_bios_ssc_frequency(dev_priv, general->ssc_freq);
419         dev_priv->vbt.display_clock_mode = general->display_clock_mode;
420         dev_priv->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
421         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",
422                       dev_priv->vbt.int_tv_support,
423                       dev_priv->vbt.int_crt_support,
424                       dev_priv->vbt.lvds_use_ssc,
425                       dev_priv->vbt.lvds_ssc_freq,
426                       dev_priv->vbt.display_clock_mode,
427                       dev_priv->vbt.fdi_rx_polarity_inverted);
428 }
429
430 static void
431 parse_general_definitions(struct drm_i915_private *dev_priv,
432                           const struct bdb_header *bdb)
433 {
434         const struct bdb_general_definitions *general;
435
436         general = find_section(bdb, BDB_GENERAL_DEFINITIONS);
437         if (general) {
438                 u16 block_size = get_blocksize(general);
439                 if (block_size >= sizeof(*general)) {
440                         int bus_pin = general->crt_ddc_gmbus_pin;
441                         DRM_DEBUG_KMS("crt_ddc_bus_pin: %d\n", bus_pin);
442                         if (intel_gmbus_is_valid_pin(dev_priv, bus_pin))
443                                 dev_priv->vbt.crt_ddc_pin = bus_pin;
444                 } else {
445                         DRM_DEBUG_KMS("BDB_GD too small (%d). Invalid.\n",
446                                       block_size);
447                 }
448         }
449 }
450
451 static const union child_device_config *
452 child_device_ptr(const struct bdb_general_definitions *p_defs, int i)
453 {
454         return (const void *) &p_defs->devices[i * p_defs->child_dev_size];
455 }
456
457 static void
458 parse_sdvo_device_mapping(struct drm_i915_private *dev_priv,
459                           const struct bdb_header *bdb)
460 {
461         struct sdvo_device_mapping *p_mapping;
462         const struct bdb_general_definitions *p_defs;
463         const struct old_child_dev_config *child; /* legacy */
464         int i, child_device_num, count;
465         u16     block_size;
466
467         p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
468         if (!p_defs) {
469                 DRM_DEBUG_KMS("No general definition block is found, unable to construct sdvo mapping.\n");
470                 return;
471         }
472
473         /*
474          * Only parse SDVO mappings when the general definitions block child
475          * device size matches that of the *legacy* child device config
476          * struct. Thus, SDVO mapping will be skipped for newer VBT.
477          */
478         if (p_defs->child_dev_size != sizeof(*child)) {
479                 DRM_DEBUG_KMS("Unsupported child device size for SDVO mapping.\n");
480                 return;
481         }
482         /* get the block size of general definitions */
483         block_size = get_blocksize(p_defs);
484         /* get the number of child device */
485         child_device_num = (block_size - sizeof(*p_defs)) /
486                 p_defs->child_dev_size;
487         count = 0;
488         for (i = 0; i < child_device_num; i++) {
489                 child = &child_device_ptr(p_defs, i)->old;
490                 if (!child->device_type) {
491                         /* skip the device block if device type is invalid */
492                         continue;
493                 }
494                 if (child->slave_addr != SLAVE_ADDR1 &&
495                     child->slave_addr != SLAVE_ADDR2) {
496                         /*
497                          * If the slave address is neither 0x70 nor 0x72,
498                          * it is not a SDVO device. Skip it.
499                          */
500                         continue;
501                 }
502                 if (child->dvo_port != DEVICE_PORT_DVOB &&
503                     child->dvo_port != DEVICE_PORT_DVOC) {
504                         /* skip the incorrect SDVO port */
505                         DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
506                         continue;
507                 }
508                 DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
509                               " %s port\n",
510                               child->slave_addr,
511                               (child->dvo_port == DEVICE_PORT_DVOB) ?
512                               "SDVOB" : "SDVOC");
513                 p_mapping = &dev_priv->vbt.sdvo_mappings[child->dvo_port - 1];
514                 if (!p_mapping->initialized) {
515                         p_mapping->dvo_port = child->dvo_port;
516                         p_mapping->slave_addr = child->slave_addr;
517                         p_mapping->dvo_wiring = child->dvo_wiring;
518                         p_mapping->ddc_pin = child->ddc_pin;
519                         p_mapping->i2c_pin = child->i2c_pin;
520                         p_mapping->initialized = 1;
521                         DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
522                                       p_mapping->dvo_port,
523                                       p_mapping->slave_addr,
524                                       p_mapping->dvo_wiring,
525                                       p_mapping->ddc_pin,
526                                       p_mapping->i2c_pin);
527                 } else {
528                         DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
529                                          "two SDVO device.\n");
530                 }
531                 if (child->slave2_addr) {
532                         /* Maybe this is a SDVO device with multiple inputs */
533                         /* And the mapping info is not added */
534                         DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
535                                 " is a SDVO device with multiple inputs.\n");
536                 }
537                 count++;
538         }
539
540         if (!count) {
541                 /* No SDVO device info is found */
542                 DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
543         }
544         return;
545 }
546
547 static void
548 parse_driver_features(struct drm_i915_private *dev_priv,
549                       const struct bdb_header *bdb)
550 {
551         const struct bdb_driver_features *driver;
552
553         driver = find_section(bdb, BDB_DRIVER_FEATURES);
554         if (!driver)
555                 return;
556
557         if (driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
558                 dev_priv->vbt.edp.support = 1;
559
560         DRM_DEBUG_KMS("DRRS State Enabled:%d\n", driver->drrs_enabled);
561         /*
562          * If DRRS is not supported, drrs_type has to be set to 0.
563          * This is because, VBT is configured in such a way that
564          * static DRRS is 0 and DRRS not supported is represented by
565          * driver->drrs_enabled=false
566          */
567         if (!driver->drrs_enabled)
568                 dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
569 }
570
571 static void
572 parse_edp(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
573 {
574         const struct bdb_edp *edp;
575         const struct edp_power_seq *edp_pps;
576         const struct edp_link_params *edp_link_params;
577         int panel_type = dev_priv->vbt.panel_type;
578
579         edp = find_section(bdb, BDB_EDP);
580         if (!edp) {
581                 if (dev_priv->vbt.edp.support)
582                         DRM_DEBUG_KMS("No eDP BDB found but eDP panel supported.\n");
583                 return;
584         }
585
586         switch ((edp->color_depth >> (panel_type * 2)) & 3) {
587         case EDP_18BPP:
588                 dev_priv->vbt.edp.bpp = 18;
589                 break;
590         case EDP_24BPP:
591                 dev_priv->vbt.edp.bpp = 24;
592                 break;
593         case EDP_30BPP:
594                 dev_priv->vbt.edp.bpp = 30;
595                 break;
596         }
597
598         /* Get the eDP sequencing and link info */
599         edp_pps = &edp->power_seqs[panel_type];
600         edp_link_params = &edp->link_params[panel_type];
601
602         dev_priv->vbt.edp.pps = *edp_pps;
603
604         switch (edp_link_params->rate) {
605         case EDP_RATE_1_62:
606                 dev_priv->vbt.edp.rate = DP_LINK_BW_1_62;
607                 break;
608         case EDP_RATE_2_7:
609                 dev_priv->vbt.edp.rate = DP_LINK_BW_2_7;
610                 break;
611         default:
612                 DRM_DEBUG_KMS("VBT has unknown eDP link rate value %u\n",
613                               edp_link_params->rate);
614                 break;
615         }
616
617         switch (edp_link_params->lanes) {
618         case EDP_LANE_1:
619                 dev_priv->vbt.edp.lanes = 1;
620                 break;
621         case EDP_LANE_2:
622                 dev_priv->vbt.edp.lanes = 2;
623                 break;
624         case EDP_LANE_4:
625                 dev_priv->vbt.edp.lanes = 4;
626                 break;
627         default:
628                 DRM_DEBUG_KMS("VBT has unknown eDP lane count value %u\n",
629                               edp_link_params->lanes);
630                 break;
631         }
632
633         switch (edp_link_params->preemphasis) {
634         case EDP_PREEMPHASIS_NONE:
635                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
636                 break;
637         case EDP_PREEMPHASIS_3_5dB:
638                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
639                 break;
640         case EDP_PREEMPHASIS_6dB:
641                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
642                 break;
643         case EDP_PREEMPHASIS_9_5dB:
644                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
645                 break;
646         default:
647                 DRM_DEBUG_KMS("VBT has unknown eDP pre-emphasis value %u\n",
648                               edp_link_params->preemphasis);
649                 break;
650         }
651
652         switch (edp_link_params->vswing) {
653         case EDP_VSWING_0_4V:
654                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
655                 break;
656         case EDP_VSWING_0_6V:
657                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
658                 break;
659         case EDP_VSWING_0_8V:
660                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
661                 break;
662         case EDP_VSWING_1_2V:
663                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
664                 break;
665         default:
666                 DRM_DEBUG_KMS("VBT has unknown eDP voltage swing value %u\n",
667                               edp_link_params->vswing);
668                 break;
669         }
670
671         if (bdb->version >= 173) {
672                 uint8_t vswing;
673
674                 /* Don't read from VBT if module parameter has valid value*/
675                 if (i915.edp_vswing) {
676                         dev_priv->vbt.edp.low_vswing = i915.edp_vswing == 1;
677                 } else {
678                         vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
679                         dev_priv->vbt.edp.low_vswing = vswing == 0;
680                 }
681         }
682 }
683
684 static void
685 parse_psr(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
686 {
687         const struct bdb_psr *psr;
688         const struct psr_table *psr_table;
689         int panel_type = dev_priv->vbt.panel_type;
690
691         psr = find_section(bdb, BDB_PSR);
692         if (!psr) {
693                 DRM_DEBUG_KMS("No PSR BDB found.\n");
694                 return;
695         }
696
697         psr_table = &psr->psr_table[panel_type];
698
699         dev_priv->vbt.psr.full_link = psr_table->full_link;
700         dev_priv->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
701
702         /* Allowed VBT values goes from 0 to 15 */
703         dev_priv->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
704                 psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
705
706         switch (psr_table->lines_to_wait) {
707         case 0:
708                 dev_priv->vbt.psr.lines_to_wait = PSR_0_LINES_TO_WAIT;
709                 break;
710         case 1:
711                 dev_priv->vbt.psr.lines_to_wait = PSR_1_LINE_TO_WAIT;
712                 break;
713         case 2:
714                 dev_priv->vbt.psr.lines_to_wait = PSR_4_LINES_TO_WAIT;
715                 break;
716         case 3:
717                 dev_priv->vbt.psr.lines_to_wait = PSR_8_LINES_TO_WAIT;
718                 break;
719         default:
720                 DRM_DEBUG_KMS("VBT has unknown PSR lines to wait %u\n",
721                               psr_table->lines_to_wait);
722                 break;
723         }
724
725         dev_priv->vbt.psr.tp1_wakeup_time = psr_table->tp1_wakeup_time;
726         dev_priv->vbt.psr.tp2_tp3_wakeup_time = psr_table->tp2_tp3_wakeup_time;
727 }
728
729 static void
730 parse_mipi_config(struct drm_i915_private *dev_priv,
731                   const struct bdb_header *bdb)
732 {
733         const struct bdb_mipi_config *start;
734         const struct mipi_config *config;
735         const struct mipi_pps_data *pps;
736         int panel_type = dev_priv->vbt.panel_type;
737
738         /* parse MIPI blocks only if LFP type is MIPI */
739         if (!intel_bios_is_dsi_present(dev_priv, NULL))
740                 return;
741
742         /* Initialize this to undefined indicating no generic MIPI support */
743         dev_priv->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
744
745         /* Block #40 is already parsed and panel_fixed_mode is
746          * stored in dev_priv->lfp_lvds_vbt_mode
747          * resuse this when needed
748          */
749
750         /* Parse #52 for panel index used from panel_type already
751          * parsed
752          */
753         start = find_section(bdb, BDB_MIPI_CONFIG);
754         if (!start) {
755                 DRM_DEBUG_KMS("No MIPI config BDB found");
756                 return;
757         }
758
759         DRM_DEBUG_DRIVER("Found MIPI Config block, panel index = %d\n",
760                                                                 panel_type);
761
762         /*
763          * get hold of the correct configuration block and pps data as per
764          * the panel_type as index
765          */
766         config = &start->config[panel_type];
767         pps = &start->pps[panel_type];
768
769         /* store as of now full data. Trim when we realise all is not needed */
770         dev_priv->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
771         if (!dev_priv->vbt.dsi.config)
772                 return;
773
774         dev_priv->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
775         if (!dev_priv->vbt.dsi.pps) {
776                 kfree(dev_priv->vbt.dsi.config);
777                 return;
778         }
779
780         /*
781          * These fields are introduced from the VBT version 197 onwards,
782          * so making sure that these bits are set zero in the previous
783          * versions.
784          */
785         if (dev_priv->vbt.dsi.config->dual_link && bdb->version < 197) {
786                 dev_priv->vbt.dsi.config->dl_dcs_cabc_ports = 0;
787                 dev_priv->vbt.dsi.config->dl_dcs_backlight_ports = 0;
788         }
789
790         /* We have mandatory mipi config blocks. Initialize as generic panel */
791         dev_priv->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
792 }
793
794 /* Find the sequence block and size for the given panel. */
795 static const u8 *
796 find_panel_sequence_block(const struct bdb_mipi_sequence *sequence,
797                           u16 panel_id, u32 *seq_size)
798 {
799         u32 total = get_blocksize(sequence);
800         const u8 *data = &sequence->data[0];
801         u8 current_id;
802         u32 current_size;
803         int header_size = sequence->version >= 3 ? 5 : 3;
804         int index = 0;
805         int i;
806
807         /* skip new block size */
808         if (sequence->version >= 3)
809                 data += 4;
810
811         for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
812                 if (index + header_size > total) {
813                         DRM_ERROR("Invalid sequence block (header)\n");
814                         return NULL;
815                 }
816
817                 current_id = *(data + index);
818                 if (sequence->version >= 3)
819                         current_size = *((const u32 *)(data + index + 1));
820                 else
821                         current_size = *((const u16 *)(data + index + 1));
822
823                 index += header_size;
824
825                 if (index + current_size > total) {
826                         DRM_ERROR("Invalid sequence block\n");
827                         return NULL;
828                 }
829
830                 if (current_id == panel_id) {
831                         *seq_size = current_size;
832                         return data + index;
833                 }
834
835                 index += current_size;
836         }
837
838         DRM_ERROR("Sequence block detected but no valid configuration\n");
839
840         return NULL;
841 }
842
843 static int goto_next_sequence(const u8 *data, int index, int total)
844 {
845         u16 len;
846
847         /* Skip Sequence Byte. */
848         for (index = index + 1; index < total; index += len) {
849                 u8 operation_byte = *(data + index);
850                 index++;
851
852                 switch (operation_byte) {
853                 case MIPI_SEQ_ELEM_END:
854                         return index;
855                 case MIPI_SEQ_ELEM_SEND_PKT:
856                         if (index + 4 > total)
857                                 return 0;
858
859                         len = *((const u16 *)(data + index + 2)) + 4;
860                         break;
861                 case MIPI_SEQ_ELEM_DELAY:
862                         len = 4;
863                         break;
864                 case MIPI_SEQ_ELEM_GPIO:
865                         len = 2;
866                         break;
867                 case MIPI_SEQ_ELEM_I2C:
868                         if (index + 7 > total)
869                                 return 0;
870                         len = *(data + index + 6) + 7;
871                         break;
872                 default:
873                         DRM_ERROR("Unknown operation byte\n");
874                         return 0;
875                 }
876         }
877
878         return 0;
879 }
880
881 static int goto_next_sequence_v3(const u8 *data, int index, int total)
882 {
883         int seq_end;
884         u16 len;
885         u32 size_of_sequence;
886
887         /*
888          * Could skip sequence based on Size of Sequence alone, but also do some
889          * checking on the structure.
890          */
891         if (total < 5) {
892                 DRM_ERROR("Too small sequence size\n");
893                 return 0;
894         }
895
896         /* Skip Sequence Byte. */
897         index++;
898
899         /*
900          * Size of Sequence. Excludes the Sequence Byte and the size itself,
901          * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
902          * byte.
903          */
904         size_of_sequence = *((const uint32_t *)(data + index));
905         index += 4;
906
907         seq_end = index + size_of_sequence;
908         if (seq_end > total) {
909                 DRM_ERROR("Invalid sequence size\n");
910                 return 0;
911         }
912
913         for (; index < total; index += len) {
914                 u8 operation_byte = *(data + index);
915                 index++;
916
917                 if (operation_byte == MIPI_SEQ_ELEM_END) {
918                         if (index != seq_end) {
919                                 DRM_ERROR("Invalid element structure\n");
920                                 return 0;
921                         }
922                         return index;
923                 }
924
925                 len = *(data + index);
926                 index++;
927
928                 /*
929                  * FIXME: Would be nice to check elements like for v1/v2 in
930                  * goto_next_sequence() above.
931                  */
932                 switch (operation_byte) {
933                 case MIPI_SEQ_ELEM_SEND_PKT:
934                 case MIPI_SEQ_ELEM_DELAY:
935                 case MIPI_SEQ_ELEM_GPIO:
936                 case MIPI_SEQ_ELEM_I2C:
937                 case MIPI_SEQ_ELEM_SPI:
938                 case MIPI_SEQ_ELEM_PMIC:
939                         break;
940                 default:
941                         DRM_ERROR("Unknown operation byte %u\n",
942                                   operation_byte);
943                         break;
944                 }
945         }
946
947         return 0;
948 }
949
950 static void
951 parse_mipi_sequence(struct drm_i915_private *dev_priv,
952                     const struct bdb_header *bdb)
953 {
954         int panel_type = dev_priv->vbt.panel_type;
955         const struct bdb_mipi_sequence *sequence;
956         const u8 *seq_data;
957         u32 seq_size;
958         u8 *data;
959         int index = 0;
960
961         /* Only our generic panel driver uses the sequence block. */
962         if (dev_priv->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
963                 return;
964
965         sequence = find_section(bdb, BDB_MIPI_SEQUENCE);
966         if (!sequence) {
967                 DRM_DEBUG_KMS("No MIPI Sequence found, parsing complete\n");
968                 return;
969         }
970
971         /* Fail gracefully for forward incompatible sequence block. */
972         if (sequence->version >= 4) {
973                 DRM_ERROR("Unable to parse MIPI Sequence Block v%u\n",
974                           sequence->version);
975                 return;
976         }
977
978         DRM_DEBUG_DRIVER("Found MIPI sequence block v%u\n", sequence->version);
979
980         seq_data = find_panel_sequence_block(sequence, panel_type, &seq_size);
981         if (!seq_data)
982                 return;
983
984         data = kmemdup(seq_data, seq_size, GFP_KERNEL);
985         if (!data)
986                 return;
987
988         /* Parse the sequences, store pointers to each sequence. */
989         for (;;) {
990                 u8 seq_id = *(data + index);
991                 if (seq_id == MIPI_SEQ_END)
992                         break;
993
994                 if (seq_id >= MIPI_SEQ_MAX) {
995                         DRM_ERROR("Unknown sequence %u\n", seq_id);
996                         goto err;
997                 }
998
999                 dev_priv->vbt.dsi.sequence[seq_id] = data + index;
1000
1001                 if (sequence->version >= 3)
1002                         index = goto_next_sequence_v3(data, index, seq_size);
1003                 else
1004                         index = goto_next_sequence(data, index, seq_size);
1005                 if (!index) {
1006                         DRM_ERROR("Invalid sequence %u\n", seq_id);
1007                         goto err;
1008                 }
1009         }
1010
1011         dev_priv->vbt.dsi.data = data;
1012         dev_priv->vbt.dsi.size = seq_size;
1013         dev_priv->vbt.dsi.seq_version = sequence->version;
1014
1015         DRM_DEBUG_DRIVER("MIPI related VBT parsing complete\n");
1016         return;
1017
1018 err:
1019         kfree(data);
1020         memset(dev_priv->vbt.dsi.sequence, 0, sizeof(dev_priv->vbt.dsi.sequence));
1021 }
1022
1023 static u8 translate_iboost(u8 val)
1024 {
1025         static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
1026
1027         if (val >= ARRAY_SIZE(mapping)) {
1028                 DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
1029                 return 0;
1030         }
1031         return mapping[val];
1032 }
1033
1034 static void sanitize_ddc_pin(struct drm_i915_private *dev_priv,
1035                              enum port port)
1036 {
1037         const struct ddi_vbt_port_info *info =
1038                 &dev_priv->vbt.ddi_port_info[port];
1039         enum port p;
1040
1041         if (!info->alternate_ddc_pin)
1042                 return;
1043
1044         for_each_port_masked(p, (1 << port) - 1) {
1045                 struct ddi_vbt_port_info *i = &dev_priv->vbt.ddi_port_info[p];
1046
1047                 if (info->alternate_ddc_pin != i->alternate_ddc_pin)
1048                         continue;
1049
1050                 DRM_DEBUG_KMS("port %c trying to use the same DDC pin (0x%x) as port %c, "
1051                               "disabling port %c DVI/HDMI support\n",
1052                               port_name(p), i->alternate_ddc_pin,
1053                               port_name(port), port_name(p));
1054
1055                 /*
1056                  * If we have multiple ports supposedly sharing the
1057                  * pin, then dvi/hdmi couldn't exist on the shared
1058                  * port. Otherwise they share the same ddc bin and
1059                  * system couldn't communicate with them separately.
1060                  *
1061                  * Due to parsing the ports in alphabetical order,
1062                  * a higher port will always clobber a lower one.
1063                  */
1064                 i->supports_dvi = false;
1065                 i->supports_hdmi = false;
1066                 i->alternate_ddc_pin = 0;
1067         }
1068 }
1069
1070 static void sanitize_aux_ch(struct drm_i915_private *dev_priv,
1071                             enum port port)
1072 {
1073         const struct ddi_vbt_port_info *info =
1074                 &dev_priv->vbt.ddi_port_info[port];
1075         enum port p;
1076
1077         if (!info->alternate_aux_channel)
1078                 return;
1079
1080         for_each_port_masked(p, (1 << port) - 1) {
1081                 struct ddi_vbt_port_info *i = &dev_priv->vbt.ddi_port_info[p];
1082
1083                 if (info->alternate_aux_channel != i->alternate_aux_channel)
1084                         continue;
1085
1086                 DRM_DEBUG_KMS("port %c trying to use the same AUX CH (0x%x) as port %c, "
1087                               "disabling port %c DP support\n",
1088                               port_name(p), i->alternate_aux_channel,
1089                               port_name(port), port_name(p));
1090
1091                 /*
1092                  * If we have multiple ports supposedlt sharing the
1093                  * aux channel, then DP couldn't exist on the shared
1094                  * port. Otherwise they share the same aux channel
1095                  * and system couldn't communicate with them separately.
1096                  *
1097                  * Due to parsing the ports in alphabetical order,
1098                  * a higher port will always clobber a lower one.
1099                  */
1100                 i->supports_dp = false;
1101                 i->alternate_aux_channel = 0;
1102         }
1103 }
1104
1105 static void parse_ddi_port(struct drm_i915_private *dev_priv, enum port port,
1106                            const struct bdb_header *bdb)
1107 {
1108         union child_device_config *it, *child = NULL;
1109         struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
1110         uint8_t hdmi_level_shift;
1111         int i, j;
1112         bool is_dvi, is_hdmi, is_dp, is_edp, is_crt;
1113         uint8_t aux_channel, ddc_pin;
1114         /* Each DDI port can have more than one value on the "DVO Port" field,
1115          * so look for all the possible values for each port and abort if more
1116          * than one is found. */
1117         int dvo_ports[][3] = {
1118                 {DVO_PORT_HDMIA, DVO_PORT_DPA, -1},
1119                 {DVO_PORT_HDMIB, DVO_PORT_DPB, -1},
1120                 {DVO_PORT_HDMIC, DVO_PORT_DPC, -1},
1121                 {DVO_PORT_HDMID, DVO_PORT_DPD, -1},
1122                 {DVO_PORT_CRT, DVO_PORT_HDMIE, DVO_PORT_DPE},
1123         };
1124
1125         /* Find the child device to use, abort if more than one found. */
1126         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1127                 it = dev_priv->vbt.child_dev + i;
1128
1129                 for (j = 0; j < 3; j++) {
1130                         if (dvo_ports[port][j] == -1)
1131                                 break;
1132
1133                         if (it->common.dvo_port == dvo_ports[port][j]) {
1134                                 if (child) {
1135                                         DRM_DEBUG_KMS("More than one child device for port %c in VBT.\n",
1136                                                       port_name(port));
1137                                         return;
1138                                 }
1139                                 child = it;
1140                         }
1141                 }
1142         }
1143         if (!child)
1144                 return;
1145
1146         aux_channel = child->common.aux_channel;
1147         ddc_pin = child->common.ddc_pin;
1148
1149         is_dvi = child->common.device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
1150         is_dp = child->common.device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
1151         is_crt = child->common.device_type & DEVICE_TYPE_ANALOG_OUTPUT;
1152         is_hdmi = is_dvi && (child->common.device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
1153         is_edp = is_dp && (child->common.device_type & DEVICE_TYPE_INTERNAL_CONNECTOR);
1154
1155         if (port == PORT_A && is_dvi) {
1156                 DRM_DEBUG_KMS("VBT claims port A supports DVI%s, ignoring\n",
1157                               is_hdmi ? "/HDMI" : "");
1158                 is_dvi = false;
1159                 is_hdmi = false;
1160         }
1161
1162         info->supports_dvi = is_dvi;
1163         info->supports_hdmi = is_hdmi;
1164         info->supports_dp = is_dp;
1165
1166         DRM_DEBUG_KMS("Port %c VBT info: DP:%d HDMI:%d DVI:%d EDP:%d CRT:%d\n",
1167                       port_name(port), is_dp, is_hdmi, is_dvi, is_edp, is_crt);
1168
1169         if (is_edp && is_dvi)
1170                 DRM_DEBUG_KMS("Internal DP port %c is TMDS compatible\n",
1171                               port_name(port));
1172         if (is_crt && port != PORT_E)
1173                 DRM_DEBUG_KMS("Port %c is analog\n", port_name(port));
1174         if (is_crt && (is_dvi || is_dp))
1175                 DRM_DEBUG_KMS("Analog port %c is also DP or TMDS compatible\n",
1176                               port_name(port));
1177         if (is_dvi && (port == PORT_A || port == PORT_E))
1178                 DRM_DEBUG_KMS("Port %c is TMDS compatible\n", port_name(port));
1179         if (!is_dvi && !is_dp && !is_crt)
1180                 DRM_DEBUG_KMS("Port %c is not DP/TMDS/CRT compatible\n",
1181                               port_name(port));
1182         if (is_edp && (port == PORT_B || port == PORT_C || port == PORT_E))
1183                 DRM_DEBUG_KMS("Port %c is internal DP\n", port_name(port));
1184
1185         if (is_dvi) {
1186                 info->alternate_ddc_pin = ddc_pin;
1187
1188                 sanitize_ddc_pin(dev_priv, port);
1189         }
1190
1191         if (is_dp) {
1192                 info->alternate_aux_channel = aux_channel;
1193
1194                 sanitize_aux_ch(dev_priv, port);
1195         }
1196
1197         if (bdb->version >= 158) {
1198                 /* The VBT HDMI level shift values match the table we have. */
1199                 hdmi_level_shift = child->raw[7] & 0xF;
1200                 DRM_DEBUG_KMS("VBT HDMI level shift for port %c: %d\n",
1201                               port_name(port),
1202                               hdmi_level_shift);
1203                 info->hdmi_level_shift = hdmi_level_shift;
1204         }
1205
1206         /* Parse the I_boost config for SKL and above */
1207         if (bdb->version >= 196 && child->common.iboost) {
1208                 info->dp_boost_level = translate_iboost(child->common.iboost_level & 0xF);
1209                 DRM_DEBUG_KMS("VBT (e)DP boost level for port %c: %d\n",
1210                               port_name(port), info->dp_boost_level);
1211                 info->hdmi_boost_level = translate_iboost(child->common.iboost_level >> 4);
1212                 DRM_DEBUG_KMS("VBT HDMI boost level for port %c: %d\n",
1213                               port_name(port), info->hdmi_boost_level);
1214         }
1215 }
1216
1217 static void parse_ddi_ports(struct drm_i915_private *dev_priv,
1218                             const struct bdb_header *bdb)
1219 {
1220         enum port port;
1221
1222         if (!HAS_DDI(dev_priv) && !IS_CHERRYVIEW(dev_priv))
1223                 return;
1224
1225         if (!dev_priv->vbt.child_dev_num)
1226                 return;
1227
1228         if (bdb->version < 155)
1229                 return;
1230
1231         for (port = PORT_A; port < I915_MAX_PORTS; port++)
1232                 parse_ddi_port(dev_priv, port, bdb);
1233 }
1234
1235 static void
1236 parse_device_mapping(struct drm_i915_private *dev_priv,
1237                      const struct bdb_header *bdb)
1238 {
1239         const struct bdb_general_definitions *p_defs;
1240         const union child_device_config *p_child;
1241         union child_device_config *child_dev_ptr;
1242         int i, child_device_num, count;
1243         u8 expected_size;
1244         u16 block_size;
1245
1246         p_defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
1247         if (!p_defs) {
1248                 DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
1249                 return;
1250         }
1251         if (bdb->version < 106) {
1252                 expected_size = 22;
1253         } else if (bdb->version < 111) {
1254                 expected_size = 27;
1255         } else if (bdb->version < 195) {
1256                 BUILD_BUG_ON(sizeof(struct old_child_dev_config) != 33);
1257                 expected_size = sizeof(struct old_child_dev_config);
1258         } else if (bdb->version == 195) {
1259                 expected_size = 37;
1260         } else if (bdb->version <= 197) {
1261                 expected_size = 38;
1262         } else {
1263                 expected_size = 38;
1264                 BUILD_BUG_ON(sizeof(*p_child) < 38);
1265                 DRM_DEBUG_DRIVER("Expected child device config size for VBT version %u not known; assuming %u\n",
1266                                  bdb->version, expected_size);
1267         }
1268
1269         /* Flag an error for unexpected size, but continue anyway. */
1270         if (p_defs->child_dev_size != expected_size)
1271                 DRM_ERROR("Unexpected child device config size %u (expected %u for VBT version %u)\n",
1272                           p_defs->child_dev_size, expected_size, bdb->version);
1273
1274         /* The legacy sized child device config is the minimum we need. */
1275         if (p_defs->child_dev_size < sizeof(struct old_child_dev_config)) {
1276                 DRM_DEBUG_KMS("Child device config size %u is too small.\n",
1277                               p_defs->child_dev_size);
1278                 return;
1279         }
1280
1281         /* get the block size of general definitions */
1282         block_size = get_blocksize(p_defs);
1283         /* get the number of child device */
1284         child_device_num = (block_size - sizeof(*p_defs)) /
1285                                 p_defs->child_dev_size;
1286         count = 0;
1287         /* get the number of child device that is present */
1288         for (i = 0; i < child_device_num; i++) {
1289                 p_child = child_device_ptr(p_defs, i);
1290                 if (!p_child->common.device_type) {
1291                         /* skip the device block if device type is invalid */
1292                         continue;
1293                 }
1294                 count++;
1295         }
1296         if (!count) {
1297                 DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
1298                 return;
1299         }
1300         dev_priv->vbt.child_dev = kcalloc(count, sizeof(*p_child), GFP_KERNEL);
1301         if (!dev_priv->vbt.child_dev) {
1302                 DRM_DEBUG_KMS("No memory space for child device\n");
1303                 return;
1304         }
1305
1306         dev_priv->vbt.child_dev_num = count;
1307         count = 0;
1308         for (i = 0; i < child_device_num; i++) {
1309                 p_child = child_device_ptr(p_defs, i);
1310                 if (!p_child->common.device_type) {
1311                         /* skip the device block if device type is invalid */
1312                         continue;
1313                 }
1314
1315                 child_dev_ptr = dev_priv->vbt.child_dev + count;
1316                 count++;
1317
1318                 /*
1319                  * Copy as much as we know (sizeof) and is available
1320                  * (child_dev_size) of the child device. Accessing the data must
1321                  * depend on VBT version.
1322                  */
1323                 memcpy(child_dev_ptr, p_child,
1324                        min_t(size_t, p_defs->child_dev_size, sizeof(*p_child)));
1325
1326                 /*
1327                  * copied full block, now init values when they are not
1328                  * available in current version
1329                  */
1330                 if (bdb->version < 196) {
1331                         /* Set default values for bits added from v196 */
1332                         child_dev_ptr->common.iboost = 0;
1333                         child_dev_ptr->common.hpd_invert = 0;
1334                 }
1335
1336                 if (bdb->version < 192)
1337                         child_dev_ptr->common.lspcon = 0;
1338         }
1339         return;
1340 }
1341
1342 /* Common defaults which may be overridden by VBT. */
1343 static void
1344 init_vbt_defaults(struct drm_i915_private *dev_priv)
1345 {
1346         enum port port;
1347
1348         dev_priv->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
1349
1350         /* Default to having backlight */
1351         dev_priv->vbt.backlight.present = true;
1352
1353         /* LFP panel data */
1354         dev_priv->vbt.lvds_dither = 1;
1355         dev_priv->vbt.lvds_vbt = 0;
1356
1357         /* SDVO panel data */
1358         dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1359
1360         /* general features */
1361         dev_priv->vbt.int_tv_support = 1;
1362         dev_priv->vbt.int_crt_support = 1;
1363
1364         /* Default to using SSC */
1365         dev_priv->vbt.lvds_use_ssc = 1;
1366         /*
1367          * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
1368          * clock for LVDS.
1369          */
1370         dev_priv->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(dev_priv,
1371                         !HAS_PCH_SPLIT(dev_priv));
1372         DRM_DEBUG_KMS("Set default to SSC at %d kHz\n", dev_priv->vbt.lvds_ssc_freq);
1373
1374         for (port = PORT_A; port < I915_MAX_PORTS; port++) {
1375                 struct ddi_vbt_port_info *info =
1376                         &dev_priv->vbt.ddi_port_info[port];
1377
1378                 info->hdmi_level_shift = HDMI_LEVEL_SHIFT_UNKNOWN;
1379         }
1380 }
1381
1382 /* Defaults to initialize only if there is no VBT. */
1383 static void
1384 init_vbt_missing_defaults(struct drm_i915_private *dev_priv)
1385 {
1386         enum port port;
1387
1388         for (port = PORT_A; port < I915_MAX_PORTS; port++) {
1389                 struct ddi_vbt_port_info *info =
1390                         &dev_priv->vbt.ddi_port_info[port];
1391
1392                 info->supports_dvi = (port != PORT_A && port != PORT_E);
1393                 info->supports_hdmi = info->supports_dvi;
1394                 info->supports_dp = (port != PORT_E);
1395         }
1396 }
1397
1398 static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
1399 {
1400         const void *_vbt = vbt;
1401
1402         return _vbt + vbt->bdb_offset;
1403 }
1404
1405 /**
1406  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
1407  * @buf:        pointer to a buffer to validate
1408  * @size:       size of the buffer
1409  *
1410  * Returns true on valid VBT.
1411  */
1412 bool intel_bios_is_valid_vbt(const void *buf, size_t size)
1413 {
1414         const struct vbt_header *vbt = buf;
1415         const struct bdb_header *bdb;
1416
1417         if (!vbt)
1418                 return false;
1419
1420         if (sizeof(struct vbt_header) > size) {
1421                 DRM_DEBUG_DRIVER("VBT header incomplete\n");
1422                 return false;
1423         }
1424
1425         if (memcmp(vbt->signature, "$VBT", 4)) {
1426                 DRM_DEBUG_DRIVER("VBT invalid signature\n");
1427                 return false;
1428         }
1429
1430         if (vbt->bdb_offset + sizeof(struct bdb_header) > size) {
1431                 DRM_DEBUG_DRIVER("BDB header incomplete\n");
1432                 return false;
1433         }
1434
1435         bdb = get_bdb_header(vbt);
1436         if (vbt->bdb_offset + bdb->bdb_size > size) {
1437                 DRM_DEBUG_DRIVER("BDB incomplete\n");
1438                 return false;
1439         }
1440
1441         return vbt;
1442 }
1443
1444 static const struct vbt_header *find_vbt(void __iomem *bios, size_t size)
1445 {
1446         size_t i;
1447
1448         /* Scour memory looking for the VBT signature. */
1449         for (i = 0; i + 4 < size; i++) {
1450                 void *vbt;
1451
1452                 if (ioread32(bios + i) != *((const u32 *) "$VBT"))
1453                         continue;
1454
1455                 /*
1456                  * This is the one place where we explicitly discard the address
1457                  * space (__iomem) of the BIOS/VBT.
1458                  */
1459                 vbt = (void __force *) bios + i;
1460                 if (intel_bios_is_valid_vbt(vbt, size - i))
1461                         return vbt;
1462
1463                 break;
1464         }
1465
1466         return NULL;
1467 }
1468
1469 /**
1470  * intel_bios_init - find VBT and initialize settings from the BIOS
1471  * @dev_priv: i915 device instance
1472  *
1473  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
1474  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
1475  * initialize some defaults if the VBT is not present at all.
1476  */
1477 void intel_bios_init(struct drm_i915_private *dev_priv)
1478 {
1479         struct pci_dev *pdev = dev_priv->drm.pdev;
1480         const struct vbt_header *vbt = dev_priv->opregion.vbt;
1481         const struct bdb_header *bdb;
1482         u8 __iomem *bios = NULL;
1483
1484         if (HAS_PCH_NOP(dev_priv)) {
1485                 DRM_DEBUG_KMS("Skipping VBT init due to disabled display.\n");
1486                 return;
1487         }
1488
1489         init_vbt_defaults(dev_priv);
1490
1491         /* If the OpRegion does not have VBT, look in PCI ROM. */
1492         if (!vbt) {
1493                 size_t size;
1494
1495                 bios = pci_map_rom(pdev, &size);
1496                 if (!bios)
1497                         goto out;
1498
1499                 vbt = find_vbt(bios, size);
1500                 if (!vbt)
1501                         goto out;
1502
1503                 DRM_DEBUG_KMS("Found valid VBT in PCI ROM\n");
1504         }
1505
1506         bdb = get_bdb_header(vbt);
1507
1508         DRM_DEBUG_KMS("VBT signature \"%.*s\", BDB version %d\n",
1509                       (int)sizeof(vbt->signature), vbt->signature, bdb->version);
1510
1511         /* Grab useful general definitions */
1512         parse_general_features(dev_priv, bdb);
1513         parse_general_definitions(dev_priv, bdb);
1514         parse_lfp_panel_data(dev_priv, bdb);
1515         parse_lfp_backlight(dev_priv, bdb);
1516         parse_sdvo_panel_data(dev_priv, bdb);
1517         parse_sdvo_device_mapping(dev_priv, bdb);
1518         parse_device_mapping(dev_priv, bdb);
1519         parse_driver_features(dev_priv, bdb);
1520         parse_edp(dev_priv, bdb);
1521         parse_psr(dev_priv, bdb);
1522         parse_mipi_config(dev_priv, bdb);
1523         parse_mipi_sequence(dev_priv, bdb);
1524         parse_ddi_ports(dev_priv, bdb);
1525
1526 out:
1527         if (!vbt) {
1528                 DRM_INFO("Failed to find VBIOS tables (VBT)\n");
1529                 init_vbt_missing_defaults(dev_priv);
1530         }
1531
1532         if (bios)
1533                 pci_unmap_rom(pdev, bios);
1534 }
1535
1536 /**
1537  * intel_bios_is_tv_present - is integrated TV present in VBT
1538  * @dev_priv:   i915 device instance
1539  *
1540  * Return true if TV is present. If no child devices were parsed from VBT,
1541  * assume TV is present.
1542  */
1543 bool intel_bios_is_tv_present(struct drm_i915_private *dev_priv)
1544 {
1545         union child_device_config *p_child;
1546         int i;
1547
1548         if (!dev_priv->vbt.int_tv_support)
1549                 return false;
1550
1551         if (!dev_priv->vbt.child_dev_num)
1552                 return true;
1553
1554         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1555                 p_child = dev_priv->vbt.child_dev + i;
1556                 /*
1557                  * If the device type is not TV, continue.
1558                  */
1559                 switch (p_child->old.device_type) {
1560                 case DEVICE_TYPE_INT_TV:
1561                 case DEVICE_TYPE_TV:
1562                 case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
1563                         break;
1564                 default:
1565                         continue;
1566                 }
1567                 /* Only when the addin_offset is non-zero, it is regarded
1568                  * as present.
1569                  */
1570                 if (p_child->old.addin_offset)
1571                         return true;
1572         }
1573
1574         return false;
1575 }
1576
1577 /**
1578  * intel_bios_is_lvds_present - is LVDS present in VBT
1579  * @dev_priv:   i915 device instance
1580  * @i2c_pin:    i2c pin for LVDS if present
1581  *
1582  * Return true if LVDS is present. If no child devices were parsed from VBT,
1583  * assume LVDS is present.
1584  */
1585 bool intel_bios_is_lvds_present(struct drm_i915_private *dev_priv, u8 *i2c_pin)
1586 {
1587         int i;
1588
1589         if (!dev_priv->vbt.child_dev_num)
1590                 return true;
1591
1592         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1593                 union child_device_config *uchild = dev_priv->vbt.child_dev + i;
1594                 struct old_child_dev_config *child = &uchild->old;
1595
1596                 /* If the device type is not LFP, continue.
1597                  * We have to check both the new identifiers as well as the
1598                  * old for compatibility with some BIOSes.
1599                  */
1600                 if (child->device_type != DEVICE_TYPE_INT_LFP &&
1601                     child->device_type != DEVICE_TYPE_LFP)
1602                         continue;
1603
1604                 if (intel_gmbus_is_valid_pin(dev_priv, child->i2c_pin))
1605                         *i2c_pin = child->i2c_pin;
1606
1607                 /* However, we cannot trust the BIOS writers to populate
1608                  * the VBT correctly.  Since LVDS requires additional
1609                  * information from AIM blocks, a non-zero addin offset is
1610                  * a good indicator that the LVDS is actually present.
1611                  */
1612                 if (child->addin_offset)
1613                         return true;
1614
1615                 /* But even then some BIOS writers perform some black magic
1616                  * and instantiate the device without reference to any
1617                  * additional data.  Trust that if the VBT was written into
1618                  * the OpRegion then they have validated the LVDS's existence.
1619                  */
1620                 if (dev_priv->opregion.vbt)
1621                         return true;
1622         }
1623
1624         return false;
1625 }
1626
1627 /**
1628  * intel_bios_is_port_present - is the specified digital port present
1629  * @dev_priv:   i915 device instance
1630  * @port:       port to check
1631  *
1632  * Return true if the device in %port is present.
1633  */
1634 bool intel_bios_is_port_present(struct drm_i915_private *dev_priv, enum port port)
1635 {
1636         static const struct {
1637                 u16 dp, hdmi;
1638         } port_mapping[] = {
1639                 [PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
1640                 [PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
1641                 [PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
1642                 [PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
1643         };
1644         int i;
1645
1646         /* FIXME maybe deal with port A as well? */
1647         if (WARN_ON(port == PORT_A) || port >= ARRAY_SIZE(port_mapping))
1648                 return false;
1649
1650         if (!dev_priv->vbt.child_dev_num)
1651                 return false;
1652
1653         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1654                 const union child_device_config *p_child =
1655                         &dev_priv->vbt.child_dev[i];
1656                 if ((p_child->common.dvo_port == port_mapping[port].dp ||
1657                      p_child->common.dvo_port == port_mapping[port].hdmi) &&
1658                     (p_child->common.device_type & (DEVICE_TYPE_TMDS_DVI_SIGNALING |
1659                                                     DEVICE_TYPE_DISPLAYPORT_OUTPUT)))
1660                         return true;
1661         }
1662
1663         return false;
1664 }
1665
1666 /**
1667  * intel_bios_is_port_edp - is the device in given port eDP
1668  * @dev_priv:   i915 device instance
1669  * @port:       port to check
1670  *
1671  * Return true if the device in %port is eDP.
1672  */
1673 bool intel_bios_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
1674 {
1675         union child_device_config *p_child;
1676         static const short port_mapping[] = {
1677                 [PORT_B] = DVO_PORT_DPB,
1678                 [PORT_C] = DVO_PORT_DPC,
1679                 [PORT_D] = DVO_PORT_DPD,
1680                 [PORT_E] = DVO_PORT_DPE,
1681         };
1682         int i;
1683
1684         if (!dev_priv->vbt.child_dev_num)
1685                 return false;
1686
1687         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1688                 p_child = dev_priv->vbt.child_dev + i;
1689
1690                 if (p_child->common.dvo_port == port_mapping[port] &&
1691                     (p_child->common.device_type & DEVICE_TYPE_eDP_BITS) ==
1692                     (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
1693                         return true;
1694         }
1695
1696         return false;
1697 }
1698
1699 static bool child_dev_is_dp_dual_mode(const union child_device_config *p_child,
1700                                       enum port port)
1701 {
1702         static const struct {
1703                 u16 dp, hdmi;
1704         } port_mapping[] = {
1705                 /*
1706                  * Buggy VBTs may declare DP ports as having
1707                  * HDMI type dvo_port :( So let's check both.
1708                  */
1709                 [PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
1710                 [PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
1711                 [PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
1712                 [PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
1713         };
1714
1715         if (port == PORT_A || port >= ARRAY_SIZE(port_mapping))
1716                 return false;
1717
1718         if ((p_child->common.device_type & DEVICE_TYPE_DP_DUAL_MODE_BITS) !=
1719             (DEVICE_TYPE_DP_DUAL_MODE & DEVICE_TYPE_DP_DUAL_MODE_BITS))
1720                 return false;
1721
1722         if (p_child->common.dvo_port == port_mapping[port].dp)
1723                 return true;
1724
1725         /* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
1726         if (p_child->common.dvo_port == port_mapping[port].hdmi &&
1727             p_child->common.aux_channel != 0)
1728                 return true;
1729
1730         return false;
1731 }
1732
1733 bool intel_bios_is_port_dp_dual_mode(struct drm_i915_private *dev_priv,
1734                                      enum port port)
1735 {
1736         int i;
1737
1738         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1739                 const union child_device_config *p_child =
1740                         &dev_priv->vbt.child_dev[i];
1741
1742                 if (child_dev_is_dp_dual_mode(p_child, port))
1743                         return true;
1744         }
1745
1746         return false;
1747 }
1748
1749 /**
1750  * intel_bios_is_dsi_present - is DSI present in VBT
1751  * @dev_priv:   i915 device instance
1752  * @port:       port for DSI if present
1753  *
1754  * Return true if DSI is present, and return the port in %port.
1755  */
1756 bool intel_bios_is_dsi_present(struct drm_i915_private *dev_priv,
1757                                enum port *port)
1758 {
1759         union child_device_config *p_child;
1760         u8 dvo_port;
1761         int i;
1762
1763         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1764                 p_child = dev_priv->vbt.child_dev + i;
1765
1766                 if (!(p_child->common.device_type & DEVICE_TYPE_MIPI_OUTPUT))
1767                         continue;
1768
1769                 dvo_port = p_child->common.dvo_port;
1770
1771                 switch (dvo_port) {
1772                 case DVO_PORT_MIPIA:
1773                 case DVO_PORT_MIPIC:
1774                         if (port)
1775                                 *port = dvo_port - DVO_PORT_MIPIA;
1776                         return true;
1777                 case DVO_PORT_MIPIB:
1778                 case DVO_PORT_MIPID:
1779                         DRM_DEBUG_KMS("VBT has unsupported DSI port %c\n",
1780                                       port_name(dvo_port - DVO_PORT_MIPIA));
1781                         break;
1782                 }
1783         }
1784
1785         return false;
1786 }
1787
1788 /**
1789  * intel_bios_is_port_hpd_inverted - is HPD inverted for %port
1790  * @dev_priv:   i915 device instance
1791  * @port:       port to check
1792  *
1793  * Return true if HPD should be inverted for %port.
1794  */
1795 bool
1796 intel_bios_is_port_hpd_inverted(struct drm_i915_private *dev_priv,
1797                                 enum port port)
1798 {
1799         int i;
1800
1801         if (WARN_ON_ONCE(!IS_BROXTON(dev_priv)))
1802                 return false;
1803
1804         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1805                 if (!dev_priv->vbt.child_dev[i].common.hpd_invert)
1806                         continue;
1807
1808                 switch (dev_priv->vbt.child_dev[i].common.dvo_port) {
1809                 case DVO_PORT_DPA:
1810                 case DVO_PORT_HDMIA:
1811                         if (port == PORT_A)
1812                                 return true;
1813                         break;
1814                 case DVO_PORT_DPB:
1815                 case DVO_PORT_HDMIB:
1816                         if (port == PORT_B)
1817                                 return true;
1818                         break;
1819                 case DVO_PORT_DPC:
1820                 case DVO_PORT_HDMIC:
1821                         if (port == PORT_C)
1822                                 return true;
1823                         break;
1824                 default:
1825                         break;
1826                 }
1827         }
1828
1829         return false;
1830 }