drm/i915/psr: vbt change for psr
[linux-2.6-microblaze.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_hi << 8) |
118                         dvo_timing->hsync_pulse_width_lo);
119         panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
120                 ((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
121
122         panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
123                 dvo_timing->vactive_lo;
124         panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
125                 ((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
126         panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
127                 ((dvo_timing->vsync_pulse_width_hi << 4) |
128                         dvo_timing->vsync_pulse_width_lo);
129         panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
130                 ((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
131         panel_fixed_mode->clock = dvo_timing->clock * 10;
132         panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
133
134         if (dvo_timing->hsync_positive)
135                 panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
136         else
137                 panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
138
139         if (dvo_timing->vsync_positive)
140                 panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
141         else
142                 panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
143
144         panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
145                 dvo_timing->himage_lo;
146         panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
147                 dvo_timing->vimage_lo;
148
149         /* Some VBTs have bogus h/vtotal values */
150         if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
151                 panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
152         if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
153                 panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
154
155         drm_mode_set_name(panel_fixed_mode);
156 }
157
158 static const struct lvds_dvo_timing *
159 get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *lvds_lfp_data,
160                     const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs,
161                     int index)
162 {
163         /*
164          * the size of fp_timing varies on the different platform.
165          * So calculate the DVO timing relative offset in LVDS data
166          * entry to get the DVO timing entry
167          */
168
169         int lfp_data_size =
170                 lvds_lfp_data_ptrs->ptr[1].dvo_timing_offset -
171                 lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset;
172         int dvo_timing_offset =
173                 lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset -
174                 lvds_lfp_data_ptrs->ptr[0].fp_timing_offset;
175         char *entry = (char *)lvds_lfp_data->data + lfp_data_size * index;
176
177         return (struct lvds_dvo_timing *)(entry + dvo_timing_offset);
178 }
179
180 /* get lvds_fp_timing entry
181  * this function may return NULL if the corresponding entry is invalid
182  */
183 static const struct lvds_fp_timing *
184 get_lvds_fp_timing(const struct bdb_header *bdb,
185                    const struct bdb_lvds_lfp_data *data,
186                    const struct bdb_lvds_lfp_data_ptrs *ptrs,
187                    int index)
188 {
189         size_t data_ofs = (const u8 *)data - (const u8 *)bdb;
190         u16 data_size = ((const u16 *)data)[-1]; /* stored in header */
191         size_t ofs;
192
193         if (index >= ARRAY_SIZE(ptrs->ptr))
194                 return NULL;
195         ofs = ptrs->ptr[index].fp_timing_offset;
196         if (ofs < data_ofs ||
197             ofs + sizeof(struct lvds_fp_timing) > data_ofs + data_size)
198                 return NULL;
199         return (const struct lvds_fp_timing *)((const u8 *)bdb + ofs);
200 }
201
202 /* Try to find integrated panel data */
203 static void
204 parse_lfp_panel_data(struct drm_i915_private *dev_priv,
205                      const struct bdb_header *bdb)
206 {
207         const struct bdb_lvds_options *lvds_options;
208         const struct bdb_lvds_lfp_data *lvds_lfp_data;
209         const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs;
210         const struct lvds_dvo_timing *panel_dvo_timing;
211         const struct lvds_fp_timing *fp_timing;
212         struct drm_display_mode *panel_fixed_mode;
213         int panel_type;
214         int drrs_mode;
215         int ret;
216
217         lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
218         if (!lvds_options)
219                 return;
220
221         dev_priv->vbt.lvds_dither = lvds_options->pixel_dither;
222
223         ret = intel_opregion_get_panel_type(dev_priv);
224         if (ret >= 0) {
225                 WARN_ON(ret > 0xf);
226                 panel_type = ret;
227                 DRM_DEBUG_KMS("Panel type: %d (OpRegion)\n", panel_type);
228         } else {
229                 if (lvds_options->panel_type > 0xf) {
230                         DRM_DEBUG_KMS("Invalid VBT panel type 0x%x\n",
231                                       lvds_options->panel_type);
232                         return;
233                 }
234                 panel_type = lvds_options->panel_type;
235                 DRM_DEBUG_KMS("Panel type: %d (VBT)\n", panel_type);
236         }
237
238         dev_priv->vbt.panel_type = panel_type;
239
240         drrs_mode = (lvds_options->dps_panel_type_bits
241                                 >> (panel_type * 2)) & MODE_MASK;
242         /*
243          * VBT has static DRRS = 0 and seamless DRRS = 2.
244          * The below piece of code is required to adjust vbt.drrs_type
245          * to match the enum drrs_support_type.
246          */
247         switch (drrs_mode) {
248         case 0:
249                 dev_priv->vbt.drrs_type = STATIC_DRRS_SUPPORT;
250                 DRM_DEBUG_KMS("DRRS supported mode is static\n");
251                 break;
252         case 2:
253                 dev_priv->vbt.drrs_type = SEAMLESS_DRRS_SUPPORT;
254                 DRM_DEBUG_KMS("DRRS supported mode is seamless\n");
255                 break;
256         default:
257                 dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
258                 DRM_DEBUG_KMS("DRRS not supported (VBT input)\n");
259                 break;
260         }
261
262         lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
263         if (!lvds_lfp_data)
264                 return;
265
266         lvds_lfp_data_ptrs = find_section(bdb, BDB_LVDS_LFP_DATA_PTRS);
267         if (!lvds_lfp_data_ptrs)
268                 return;
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                 dev_priv->vbt.backlight.controller = method->controller;
334         }
335
336         dev_priv->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
337         dev_priv->vbt.backlight.active_low_pwm = entry->active_low_pwm;
338         dev_priv->vbt.backlight.min_brightness = entry->min_brightness;
339         DRM_DEBUG_KMS("VBT backlight PWM modulation frequency %u Hz, "
340                       "active %s, min brightness %u, level %u, controller %u\n",
341                       dev_priv->vbt.backlight.pwm_freq_hz,
342                       dev_priv->vbt.backlight.active_low_pwm ? "low" : "high",
343                       dev_priv->vbt.backlight.min_brightness,
344                       backlight_data->level[panel_type],
345                       dev_priv->vbt.backlight.controller);
346 }
347
348 /* Try to find sdvo panel data */
349 static void
350 parse_sdvo_panel_data(struct drm_i915_private *dev_priv,
351                       const struct bdb_header *bdb)
352 {
353         const struct lvds_dvo_timing *dvo_timing;
354         struct drm_display_mode *panel_fixed_mode;
355         int index;
356
357         index = i915_modparams.vbt_sdvo_panel_type;
358         if (index == -2) {
359                 DRM_DEBUG_KMS("Ignore SDVO panel mode from BIOS VBT tables.\n");
360                 return;
361         }
362
363         if (index == -1) {
364                 const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
365
366                 sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
367                 if (!sdvo_lvds_options)
368                         return;
369
370                 index = sdvo_lvds_options->panel_type;
371         }
372
373         dvo_timing = find_section(bdb, BDB_SDVO_PANEL_DTDS);
374         if (!dvo_timing)
375                 return;
376
377         panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
378         if (!panel_fixed_mode)
379                 return;
380
381         fill_detail_timing_data(panel_fixed_mode, dvo_timing + index);
382
383         dev_priv->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
384
385         DRM_DEBUG_KMS("Found SDVO panel mode in BIOS VBT tables:\n");
386         drm_mode_debug_printmodeline(panel_fixed_mode);
387 }
388
389 static int intel_bios_ssc_frequency(struct drm_i915_private *dev_priv,
390                                     bool alternate)
391 {
392         switch (INTEL_GEN(dev_priv)) {
393         case 2:
394                 return alternate ? 66667 : 48000;
395         case 3:
396         case 4:
397                 return alternate ? 100000 : 96000;
398         default:
399                 return alternate ? 100000 : 120000;
400         }
401 }
402
403 static void
404 parse_general_features(struct drm_i915_private *dev_priv,
405                        const struct bdb_header *bdb)
406 {
407         const struct bdb_general_features *general;
408
409         general = find_section(bdb, BDB_GENERAL_FEATURES);
410         if (!general)
411                 return;
412
413         dev_priv->vbt.int_tv_support = general->int_tv_support;
414         /* int_crt_support can't be trusted on earlier platforms */
415         if (bdb->version >= 155 &&
416             (HAS_DDI(dev_priv) || IS_VALLEYVIEW(dev_priv)))
417                 dev_priv->vbt.int_crt_support = general->int_crt_support;
418         dev_priv->vbt.lvds_use_ssc = general->enable_ssc;
419         dev_priv->vbt.lvds_ssc_freq =
420                 intel_bios_ssc_frequency(dev_priv, general->ssc_freq);
421         dev_priv->vbt.display_clock_mode = general->display_clock_mode;
422         dev_priv->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
423         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",
424                       dev_priv->vbt.int_tv_support,
425                       dev_priv->vbt.int_crt_support,
426                       dev_priv->vbt.lvds_use_ssc,
427                       dev_priv->vbt.lvds_ssc_freq,
428                       dev_priv->vbt.display_clock_mode,
429                       dev_priv->vbt.fdi_rx_polarity_inverted);
430 }
431
432 static const struct child_device_config *
433 child_device_ptr(const struct bdb_general_definitions *defs, int i)
434 {
435         return (const void *) &defs->devices[i * defs->child_dev_size];
436 }
437
438 static void
439 parse_sdvo_device_mapping(struct drm_i915_private *dev_priv, u8 bdb_version)
440 {
441         struct sdvo_device_mapping *mapping;
442         const struct child_device_config *child;
443         int i, count = 0;
444
445         /*
446          * Only parse SDVO mappings on gens that could have SDVO. This isn't
447          * accurate and doesn't have to be, as long as it's not too strict.
448          */
449         if (!IS_GEN(dev_priv, 3, 7)) {
450                 DRM_DEBUG_KMS("Skipping SDVO device mapping\n");
451                 return;
452         }
453
454         for (i = 0, count = 0; i < dev_priv->vbt.child_dev_num; i++) {
455                 child = dev_priv->vbt.child_dev + i;
456
457                 if (child->slave_addr != SLAVE_ADDR1 &&
458                     child->slave_addr != SLAVE_ADDR2) {
459                         /*
460                          * If the slave address is neither 0x70 nor 0x72,
461                          * it is not a SDVO device. Skip it.
462                          */
463                         continue;
464                 }
465                 if (child->dvo_port != DEVICE_PORT_DVOB &&
466                     child->dvo_port != DEVICE_PORT_DVOC) {
467                         /* skip the incorrect SDVO port */
468                         DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
469                         continue;
470                 }
471                 DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
472                               " %s port\n",
473                               child->slave_addr,
474                               (child->dvo_port == DEVICE_PORT_DVOB) ?
475                               "SDVOB" : "SDVOC");
476                 mapping = &dev_priv->vbt.sdvo_mappings[child->dvo_port - 1];
477                 if (!mapping->initialized) {
478                         mapping->dvo_port = child->dvo_port;
479                         mapping->slave_addr = child->slave_addr;
480                         mapping->dvo_wiring = child->dvo_wiring;
481                         mapping->ddc_pin = child->ddc_pin;
482                         mapping->i2c_pin = child->i2c_pin;
483                         mapping->initialized = 1;
484                         DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
485                                       mapping->dvo_port,
486                                       mapping->slave_addr,
487                                       mapping->dvo_wiring,
488                                       mapping->ddc_pin,
489                                       mapping->i2c_pin);
490                 } else {
491                         DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
492                                          "two SDVO device.\n");
493                 }
494                 if (child->slave2_addr) {
495                         /* Maybe this is a SDVO device with multiple inputs */
496                         /* And the mapping info is not added */
497                         DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
498                                 " is a SDVO device with multiple inputs.\n");
499                 }
500                 count++;
501         }
502
503         if (!count) {
504                 /* No SDVO device info is found */
505                 DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
506         }
507 }
508
509 static void
510 parse_driver_features(struct drm_i915_private *dev_priv,
511                       const struct bdb_header *bdb)
512 {
513         const struct bdb_driver_features *driver;
514
515         driver = find_section(bdb, BDB_DRIVER_FEATURES);
516         if (!driver)
517                 return;
518
519         if (INTEL_GEN(dev_priv) >= 5 &&
520             driver->lvds_config == BDB_DRIVER_FEATURE_EDP)
521                 dev_priv->vbt.int_lvds_support = 0;
522
523         DRM_DEBUG_KMS("DRRS State Enabled:%d\n", driver->drrs_enabled);
524         /*
525          * If DRRS is not supported, drrs_type has to be set to 0.
526          * This is because, VBT is configured in such a way that
527          * static DRRS is 0 and DRRS not supported is represented by
528          * driver->drrs_enabled=false
529          */
530         if (!driver->drrs_enabled)
531                 dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
532         dev_priv->vbt.psr.enable = driver->psr_enabled;
533 }
534
535 static void
536 parse_edp(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
537 {
538         const struct bdb_edp *edp;
539         const struct edp_power_seq *edp_pps;
540         const struct edp_fast_link_params *edp_link_params;
541         int panel_type = dev_priv->vbt.panel_type;
542
543         edp = find_section(bdb, BDB_EDP);
544         if (!edp)
545                 return;
546
547         switch ((edp->color_depth >> (panel_type * 2)) & 3) {
548         case EDP_18BPP:
549                 dev_priv->vbt.edp.bpp = 18;
550                 break;
551         case EDP_24BPP:
552                 dev_priv->vbt.edp.bpp = 24;
553                 break;
554         case EDP_30BPP:
555                 dev_priv->vbt.edp.bpp = 30;
556                 break;
557         }
558
559         /* Get the eDP sequencing and link info */
560         edp_pps = &edp->power_seqs[panel_type];
561         edp_link_params = &edp->fast_link_params[panel_type];
562
563         dev_priv->vbt.edp.pps = *edp_pps;
564
565         switch (edp_link_params->rate) {
566         case EDP_RATE_1_62:
567                 dev_priv->vbt.edp.rate = DP_LINK_BW_1_62;
568                 break;
569         case EDP_RATE_2_7:
570                 dev_priv->vbt.edp.rate = DP_LINK_BW_2_7;
571                 break;
572         default:
573                 DRM_DEBUG_KMS("VBT has unknown eDP link rate value %u\n",
574                               edp_link_params->rate);
575                 break;
576         }
577
578         switch (edp_link_params->lanes) {
579         case EDP_LANE_1:
580                 dev_priv->vbt.edp.lanes = 1;
581                 break;
582         case EDP_LANE_2:
583                 dev_priv->vbt.edp.lanes = 2;
584                 break;
585         case EDP_LANE_4:
586                 dev_priv->vbt.edp.lanes = 4;
587                 break;
588         default:
589                 DRM_DEBUG_KMS("VBT has unknown eDP lane count value %u\n",
590                               edp_link_params->lanes);
591                 break;
592         }
593
594         switch (edp_link_params->preemphasis) {
595         case EDP_PREEMPHASIS_NONE:
596                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
597                 break;
598         case EDP_PREEMPHASIS_3_5dB:
599                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
600                 break;
601         case EDP_PREEMPHASIS_6dB:
602                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
603                 break;
604         case EDP_PREEMPHASIS_9_5dB:
605                 dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
606                 break;
607         default:
608                 DRM_DEBUG_KMS("VBT has unknown eDP pre-emphasis value %u\n",
609                               edp_link_params->preemphasis);
610                 break;
611         }
612
613         switch (edp_link_params->vswing) {
614         case EDP_VSWING_0_4V:
615                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
616                 break;
617         case EDP_VSWING_0_6V:
618                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
619                 break;
620         case EDP_VSWING_0_8V:
621                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
622                 break;
623         case EDP_VSWING_1_2V:
624                 dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
625                 break;
626         default:
627                 DRM_DEBUG_KMS("VBT has unknown eDP voltage swing value %u\n",
628                               edp_link_params->vswing);
629                 break;
630         }
631
632         if (bdb->version >= 173) {
633                 uint8_t vswing;
634
635                 /* Don't read from VBT if module parameter has valid value*/
636                 if (i915_modparams.edp_vswing) {
637                         dev_priv->vbt.edp.low_vswing =
638                                 i915_modparams.edp_vswing == 1;
639                 } else {
640                         vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
641                         dev_priv->vbt.edp.low_vswing = vswing == 0;
642                 }
643         }
644 }
645
646 static void
647 parse_psr(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
648 {
649         const struct bdb_psr *psr;
650         const struct psr_table *psr_table;
651         int panel_type = dev_priv->vbt.panel_type;
652
653         psr = find_section(bdb, BDB_PSR);
654         if (!psr) {
655                 DRM_DEBUG_KMS("No PSR BDB found.\n");
656                 return;
657         }
658
659         psr_table = &psr->psr_table[panel_type];
660
661         dev_priv->vbt.psr.full_link = psr_table->full_link;
662         dev_priv->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
663
664         /* Allowed VBT values goes from 0 to 15 */
665         dev_priv->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
666                 psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
667
668         switch (psr_table->lines_to_wait) {
669         case 0:
670                 dev_priv->vbt.psr.lines_to_wait = PSR_0_LINES_TO_WAIT;
671                 break;
672         case 1:
673                 dev_priv->vbt.psr.lines_to_wait = PSR_1_LINE_TO_WAIT;
674                 break;
675         case 2:
676                 dev_priv->vbt.psr.lines_to_wait = PSR_4_LINES_TO_WAIT;
677                 break;
678         case 3:
679                 dev_priv->vbt.psr.lines_to_wait = PSR_8_LINES_TO_WAIT;
680                 break;
681         default:
682                 DRM_DEBUG_KMS("VBT has unknown PSR lines to wait %u\n",
683                               psr_table->lines_to_wait);
684                 break;
685         }
686
687         /*
688          * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
689          * Old decimal value is wake up time in multiples of 100 us.
690          */
691         if (bdb->version >= 209 && IS_GEN9_BC(dev_priv)) {
692                 switch (psr_table->tp1_wakeup_time) {
693                 case 0:
694                         dev_priv->vbt.psr.tp1_wakeup_time_us = 500;
695                         break;
696                 case 1:
697                         dev_priv->vbt.psr.tp1_wakeup_time_us = 100;
698                         break;
699                 case 3:
700                         dev_priv->vbt.psr.tp1_wakeup_time_us = 0;
701                         break;
702                 default:
703                         DRM_DEBUG_KMS("VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
704                                         psr_table->tp1_wakeup_time);
705                         /* fallthrough */
706                 case 2:
707                         dev_priv->vbt.psr.tp1_wakeup_time_us = 2500;
708                         break;
709                 }
710
711                 switch (psr_table->tp2_tp3_wakeup_time) {
712                 case 0:
713                         dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = 500;
714                         break;
715                 case 1:
716                         dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = 100;
717                         break;
718                 case 3:
719                         dev_priv->vbt.psr.tp1_wakeup_time_us = 0;
720                         break;
721                 default:
722                         DRM_DEBUG_KMS("VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
723                                         psr_table->tp2_tp3_wakeup_time);
724                         /* fallthrough */
725                 case 2:
726                         dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
727                 break;
728                 }
729         } else {
730                 dev_priv->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
731                 dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
732         }
733 }
734
735 static void parse_dsi_backlight_ports(struct drm_i915_private *dev_priv,
736                                       u16 version, enum port port)
737 {
738         if (!dev_priv->vbt.dsi.config->dual_link || version < 197) {
739                 dev_priv->vbt.dsi.bl_ports = BIT(port);
740                 if (dev_priv->vbt.dsi.config->cabc_supported)
741                         dev_priv->vbt.dsi.cabc_ports = BIT(port);
742
743                 return;
744         }
745
746         switch (dev_priv->vbt.dsi.config->dl_dcs_backlight_ports) {
747         case DL_DCS_PORT_A:
748                 dev_priv->vbt.dsi.bl_ports = BIT(PORT_A);
749                 break;
750         case DL_DCS_PORT_C:
751                 dev_priv->vbt.dsi.bl_ports = BIT(PORT_C);
752                 break;
753         default:
754         case DL_DCS_PORT_A_AND_C:
755                 dev_priv->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(PORT_C);
756                 break;
757         }
758
759         if (!dev_priv->vbt.dsi.config->cabc_supported)
760                 return;
761
762         switch (dev_priv->vbt.dsi.config->dl_dcs_cabc_ports) {
763         case DL_DCS_PORT_A:
764                 dev_priv->vbt.dsi.cabc_ports = BIT(PORT_A);
765                 break;
766         case DL_DCS_PORT_C:
767                 dev_priv->vbt.dsi.cabc_ports = BIT(PORT_C);
768                 break;
769         default:
770         case DL_DCS_PORT_A_AND_C:
771                 dev_priv->vbt.dsi.cabc_ports =
772                                         BIT(PORT_A) | BIT(PORT_C);
773                 break;
774         }
775 }
776
777 static void
778 parse_mipi_config(struct drm_i915_private *dev_priv,
779                   const struct bdb_header *bdb)
780 {
781         const struct bdb_mipi_config *start;
782         const struct mipi_config *config;
783         const struct mipi_pps_data *pps;
784         int panel_type = dev_priv->vbt.panel_type;
785         enum port port;
786
787         /* parse MIPI blocks only if LFP type is MIPI */
788         if (!intel_bios_is_dsi_present(dev_priv, &port))
789                 return;
790
791         /* Initialize this to undefined indicating no generic MIPI support */
792         dev_priv->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
793
794         /* Block #40 is already parsed and panel_fixed_mode is
795          * stored in dev_priv->lfp_lvds_vbt_mode
796          * resuse this when needed
797          */
798
799         /* Parse #52 for panel index used from panel_type already
800          * parsed
801          */
802         start = find_section(bdb, BDB_MIPI_CONFIG);
803         if (!start) {
804                 DRM_DEBUG_KMS("No MIPI config BDB found");
805                 return;
806         }
807
808         DRM_DEBUG_DRIVER("Found MIPI Config block, panel index = %d\n",
809                                                                 panel_type);
810
811         /*
812          * get hold of the correct configuration block and pps data as per
813          * the panel_type as index
814          */
815         config = &start->config[panel_type];
816         pps = &start->pps[panel_type];
817
818         /* store as of now full data. Trim when we realise all is not needed */
819         dev_priv->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
820         if (!dev_priv->vbt.dsi.config)
821                 return;
822
823         dev_priv->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
824         if (!dev_priv->vbt.dsi.pps) {
825                 kfree(dev_priv->vbt.dsi.config);
826                 return;
827         }
828
829         parse_dsi_backlight_ports(dev_priv, bdb->version, port);
830
831         /* We have mandatory mipi config blocks. Initialize as generic panel */
832         dev_priv->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
833 }
834
835 /* Find the sequence block and size for the given panel. */
836 static const u8 *
837 find_panel_sequence_block(const struct bdb_mipi_sequence *sequence,
838                           u16 panel_id, u32 *seq_size)
839 {
840         u32 total = get_blocksize(sequence);
841         const u8 *data = &sequence->data[0];
842         u8 current_id;
843         u32 current_size;
844         int header_size = sequence->version >= 3 ? 5 : 3;
845         int index = 0;
846         int i;
847
848         /* skip new block size */
849         if (sequence->version >= 3)
850                 data += 4;
851
852         for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
853                 if (index + header_size > total) {
854                         DRM_ERROR("Invalid sequence block (header)\n");
855                         return NULL;
856                 }
857
858                 current_id = *(data + index);
859                 if (sequence->version >= 3)
860                         current_size = *((const u32 *)(data + index + 1));
861                 else
862                         current_size = *((const u16 *)(data + index + 1));
863
864                 index += header_size;
865
866                 if (index + current_size > total) {
867                         DRM_ERROR("Invalid sequence block\n");
868                         return NULL;
869                 }
870
871                 if (current_id == panel_id) {
872                         *seq_size = current_size;
873                         return data + index;
874                 }
875
876                 index += current_size;
877         }
878
879         DRM_ERROR("Sequence block detected but no valid configuration\n");
880
881         return NULL;
882 }
883
884 static int goto_next_sequence(const u8 *data, int index, int total)
885 {
886         u16 len;
887
888         /* Skip Sequence Byte. */
889         for (index = index + 1; index < total; index += len) {
890                 u8 operation_byte = *(data + index);
891                 index++;
892
893                 switch (operation_byte) {
894                 case MIPI_SEQ_ELEM_END:
895                         return index;
896                 case MIPI_SEQ_ELEM_SEND_PKT:
897                         if (index + 4 > total)
898                                 return 0;
899
900                         len = *((const u16 *)(data + index + 2)) + 4;
901                         break;
902                 case MIPI_SEQ_ELEM_DELAY:
903                         len = 4;
904                         break;
905                 case MIPI_SEQ_ELEM_GPIO:
906                         len = 2;
907                         break;
908                 case MIPI_SEQ_ELEM_I2C:
909                         if (index + 7 > total)
910                                 return 0;
911                         len = *(data + index + 6) + 7;
912                         break;
913                 default:
914                         DRM_ERROR("Unknown operation byte\n");
915                         return 0;
916                 }
917         }
918
919         return 0;
920 }
921
922 static int goto_next_sequence_v3(const u8 *data, int index, int total)
923 {
924         int seq_end;
925         u16 len;
926         u32 size_of_sequence;
927
928         /*
929          * Could skip sequence based on Size of Sequence alone, but also do some
930          * checking on the structure.
931          */
932         if (total < 5) {
933                 DRM_ERROR("Too small sequence size\n");
934                 return 0;
935         }
936
937         /* Skip Sequence Byte. */
938         index++;
939
940         /*
941          * Size of Sequence. Excludes the Sequence Byte and the size itself,
942          * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
943          * byte.
944          */
945         size_of_sequence = *((const uint32_t *)(data + index));
946         index += 4;
947
948         seq_end = index + size_of_sequence;
949         if (seq_end > total) {
950                 DRM_ERROR("Invalid sequence size\n");
951                 return 0;
952         }
953
954         for (; index < total; index += len) {
955                 u8 operation_byte = *(data + index);
956                 index++;
957
958                 if (operation_byte == MIPI_SEQ_ELEM_END) {
959                         if (index != seq_end) {
960                                 DRM_ERROR("Invalid element structure\n");
961                                 return 0;
962                         }
963                         return index;
964                 }
965
966                 len = *(data + index);
967                 index++;
968
969                 /*
970                  * FIXME: Would be nice to check elements like for v1/v2 in
971                  * goto_next_sequence() above.
972                  */
973                 switch (operation_byte) {
974                 case MIPI_SEQ_ELEM_SEND_PKT:
975                 case MIPI_SEQ_ELEM_DELAY:
976                 case MIPI_SEQ_ELEM_GPIO:
977                 case MIPI_SEQ_ELEM_I2C:
978                 case MIPI_SEQ_ELEM_SPI:
979                 case MIPI_SEQ_ELEM_PMIC:
980                         break;
981                 default:
982                         DRM_ERROR("Unknown operation byte %u\n",
983                                   operation_byte);
984                         break;
985                 }
986         }
987
988         return 0;
989 }
990
991 /*
992  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
993  * skip all delay + gpio operands and stop at the first DSI packet op.
994  */
995 static int get_init_otp_deassert_fragment_len(struct drm_i915_private *dev_priv)
996 {
997         const u8 *data = dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
998         int index, len;
999
1000         if (WARN_ON(!data || dev_priv->vbt.dsi.seq_version != 1))
1001                 return 0;
1002
1003         /* index = 1 to skip sequence byte */
1004         for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
1005                 switch (data[index]) {
1006                 case MIPI_SEQ_ELEM_SEND_PKT:
1007                         return index == 1 ? 0 : index;
1008                 case MIPI_SEQ_ELEM_DELAY:
1009                         len = 5; /* 1 byte for operand + uint32 */
1010                         break;
1011                 case MIPI_SEQ_ELEM_GPIO:
1012                         len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
1013                         break;
1014                 default:
1015                         return 0;
1016                 }
1017         }
1018
1019         return 0;
1020 }
1021
1022 /*
1023  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
1024  * The deassert must be done before calling intel_dsi_device_ready, so for
1025  * these devices we split the init OTP sequence into a deassert sequence and
1026  * the actual init OTP part.
1027  */
1028 static void fixup_mipi_sequences(struct drm_i915_private *dev_priv)
1029 {
1030         u8 *init_otp;
1031         int len;
1032
1033         /* Limit this to VLV for now. */
1034         if (!IS_VALLEYVIEW(dev_priv))
1035                 return;
1036
1037         /* Limit this to v1 vid-mode sequences */
1038         if (dev_priv->vbt.dsi.config->is_cmd_mode ||
1039             dev_priv->vbt.dsi.seq_version != 1)
1040                 return;
1041
1042         /* Only do this if there are otp and assert seqs and no deassert seq */
1043         if (!dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
1044             !dev_priv->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
1045             dev_priv->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
1046                 return;
1047
1048         /* The deassert-sequence ends at the first DSI packet */
1049         len = get_init_otp_deassert_fragment_len(dev_priv);
1050         if (!len)
1051                 return;
1052
1053         DRM_DEBUG_KMS("Using init OTP fragment to deassert reset\n");
1054
1055         /* Copy the fragment, update seq byte and terminate it */
1056         init_otp = (u8 *)dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
1057         dev_priv->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
1058         if (!dev_priv->vbt.dsi.deassert_seq)
1059                 return;
1060         dev_priv->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
1061         dev_priv->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
1062         /* Use the copy for deassert */
1063         dev_priv->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
1064                 dev_priv->vbt.dsi.deassert_seq;
1065         /* Replace the last byte of the fragment with init OTP seq byte */
1066         init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
1067         /* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
1068         dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
1069 }
1070
1071 static void
1072 parse_mipi_sequence(struct drm_i915_private *dev_priv,
1073                     const struct bdb_header *bdb)
1074 {
1075         int panel_type = dev_priv->vbt.panel_type;
1076         const struct bdb_mipi_sequence *sequence;
1077         const u8 *seq_data;
1078         u32 seq_size;
1079         u8 *data;
1080         int index = 0;
1081
1082         /* Only our generic panel driver uses the sequence block. */
1083         if (dev_priv->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
1084                 return;
1085
1086         sequence = find_section(bdb, BDB_MIPI_SEQUENCE);
1087         if (!sequence) {
1088                 DRM_DEBUG_KMS("No MIPI Sequence found, parsing complete\n");
1089                 return;
1090         }
1091
1092         /* Fail gracefully for forward incompatible sequence block. */
1093         if (sequence->version >= 4) {
1094                 DRM_ERROR("Unable to parse MIPI Sequence Block v%u\n",
1095                           sequence->version);
1096                 return;
1097         }
1098
1099         DRM_DEBUG_DRIVER("Found MIPI sequence block v%u\n", sequence->version);
1100
1101         seq_data = find_panel_sequence_block(sequence, panel_type, &seq_size);
1102         if (!seq_data)
1103                 return;
1104
1105         data = kmemdup(seq_data, seq_size, GFP_KERNEL);
1106         if (!data)
1107                 return;
1108
1109         /* Parse the sequences, store pointers to each sequence. */
1110         for (;;) {
1111                 u8 seq_id = *(data + index);
1112                 if (seq_id == MIPI_SEQ_END)
1113                         break;
1114
1115                 if (seq_id >= MIPI_SEQ_MAX) {
1116                         DRM_ERROR("Unknown sequence %u\n", seq_id);
1117                         goto err;
1118                 }
1119
1120                 /* Log about presence of sequences we won't run. */
1121                 if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
1122                         DRM_DEBUG_KMS("Unsupported sequence %u\n", seq_id);
1123
1124                 dev_priv->vbt.dsi.sequence[seq_id] = data + index;
1125
1126                 if (sequence->version >= 3)
1127                         index = goto_next_sequence_v3(data, index, seq_size);
1128                 else
1129                         index = goto_next_sequence(data, index, seq_size);
1130                 if (!index) {
1131                         DRM_ERROR("Invalid sequence %u\n", seq_id);
1132                         goto err;
1133                 }
1134         }
1135
1136         dev_priv->vbt.dsi.data = data;
1137         dev_priv->vbt.dsi.size = seq_size;
1138         dev_priv->vbt.dsi.seq_version = sequence->version;
1139
1140         fixup_mipi_sequences(dev_priv);
1141
1142         DRM_DEBUG_DRIVER("MIPI related VBT parsing complete\n");
1143         return;
1144
1145 err:
1146         kfree(data);
1147         memset(dev_priv->vbt.dsi.sequence, 0, sizeof(dev_priv->vbt.dsi.sequence));
1148 }
1149
1150 static u8 translate_iboost(u8 val)
1151 {
1152         static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
1153
1154         if (val >= ARRAY_SIZE(mapping)) {
1155                 DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
1156                 return 0;
1157         }
1158         return mapping[val];
1159 }
1160
1161 static void sanitize_ddc_pin(struct drm_i915_private *dev_priv,
1162                              enum port port)
1163 {
1164         const struct ddi_vbt_port_info *info =
1165                 &dev_priv->vbt.ddi_port_info[port];
1166         enum port p;
1167
1168         if (!info->alternate_ddc_pin)
1169                 return;
1170
1171         for_each_port_masked(p, (1 << port) - 1) {
1172                 struct ddi_vbt_port_info *i = &dev_priv->vbt.ddi_port_info[p];
1173
1174                 if (info->alternate_ddc_pin != i->alternate_ddc_pin)
1175                         continue;
1176
1177                 DRM_DEBUG_KMS("port %c trying to use the same DDC pin (0x%x) as port %c, "
1178                               "disabling port %c DVI/HDMI support\n",
1179                               port_name(p), i->alternate_ddc_pin,
1180                               port_name(port), port_name(p));
1181
1182                 /*
1183                  * If we have multiple ports supposedly sharing the
1184                  * pin, then dvi/hdmi couldn't exist on the shared
1185                  * port. Otherwise they share the same ddc bin and
1186                  * system couldn't communicate with them separately.
1187                  *
1188                  * Due to parsing the ports in alphabetical order,
1189                  * a higher port will always clobber a lower one.
1190                  */
1191                 i->supports_dvi = false;
1192                 i->supports_hdmi = false;
1193                 i->alternate_ddc_pin = 0;
1194         }
1195 }
1196
1197 static void sanitize_aux_ch(struct drm_i915_private *dev_priv,
1198                             enum port port)
1199 {
1200         const struct ddi_vbt_port_info *info =
1201                 &dev_priv->vbt.ddi_port_info[port];
1202         enum port p;
1203
1204         if (!info->alternate_aux_channel)
1205                 return;
1206
1207         for_each_port_masked(p, (1 << port) - 1) {
1208                 struct ddi_vbt_port_info *i = &dev_priv->vbt.ddi_port_info[p];
1209
1210                 if (info->alternate_aux_channel != i->alternate_aux_channel)
1211                         continue;
1212
1213                 DRM_DEBUG_KMS("port %c trying to use the same AUX CH (0x%x) as port %c, "
1214                               "disabling port %c DP support\n",
1215                               port_name(p), i->alternate_aux_channel,
1216                               port_name(port), port_name(p));
1217
1218                 /*
1219                  * If we have multiple ports supposedlt sharing the
1220                  * aux channel, then DP couldn't exist on the shared
1221                  * port. Otherwise they share the same aux channel
1222                  * and system couldn't communicate with them separately.
1223                  *
1224                  * Due to parsing the ports in alphabetical order,
1225                  * a higher port will always clobber a lower one.
1226                  */
1227                 i->supports_dp = false;
1228                 i->alternate_aux_channel = 0;
1229         }
1230 }
1231
1232 static const u8 cnp_ddc_pin_map[] = {
1233         [0] = 0, /* N/A */
1234         [DDC_BUS_DDI_B] = GMBUS_PIN_1_BXT,
1235         [DDC_BUS_DDI_C] = GMBUS_PIN_2_BXT,
1236         [DDC_BUS_DDI_D] = GMBUS_PIN_4_CNP, /* sic */
1237         [DDC_BUS_DDI_F] = GMBUS_PIN_3_BXT, /* sic */
1238 };
1239
1240 static u8 map_ddc_pin(struct drm_i915_private *dev_priv, u8 vbt_pin)
1241 {
1242         if (HAS_PCH_CNP(dev_priv)) {
1243                 if (vbt_pin < ARRAY_SIZE(cnp_ddc_pin_map)) {
1244                         return cnp_ddc_pin_map[vbt_pin];
1245                 } else {
1246                         DRM_DEBUG_KMS("Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n", vbt_pin);
1247                         return 0;
1248                 }
1249         }
1250
1251         return vbt_pin;
1252 }
1253
1254 static void parse_ddi_port(struct drm_i915_private *dev_priv, enum port port,
1255                            u8 bdb_version)
1256 {
1257         struct child_device_config *it, *child = NULL;
1258         struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
1259         int i, j;
1260         bool is_dvi, is_hdmi, is_dp, is_edp, is_crt;
1261         /* Each DDI port can have more than one value on the "DVO Port" field,
1262          * so look for all the possible values for each port.
1263          */
1264         int dvo_ports[][3] = {
1265                 {DVO_PORT_HDMIA, DVO_PORT_DPA, -1},
1266                 {DVO_PORT_HDMIB, DVO_PORT_DPB, -1},
1267                 {DVO_PORT_HDMIC, DVO_PORT_DPC, -1},
1268                 {DVO_PORT_HDMID, DVO_PORT_DPD, -1},
1269                 {DVO_PORT_CRT, DVO_PORT_HDMIE, DVO_PORT_DPE},
1270                 {DVO_PORT_HDMIF, DVO_PORT_DPF, -1},
1271         };
1272
1273         /*
1274          * Find the first child device to reference the port, report if more
1275          * than one found.
1276          */
1277         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1278                 it = dev_priv->vbt.child_dev + i;
1279
1280                 for (j = 0; j < 3; j++) {
1281                         if (dvo_ports[port][j] == -1)
1282                                 break;
1283
1284                         if (it->dvo_port == dvo_ports[port][j]) {
1285                                 if (child) {
1286                                         DRM_DEBUG_KMS("More than one child device for port %c in VBT, using the first.\n",
1287                                                       port_name(port));
1288                                 } else {
1289                                         child = it;
1290                                 }
1291                         }
1292                 }
1293         }
1294         if (!child)
1295                 return;
1296
1297         is_dvi = child->device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
1298         is_dp = child->device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
1299         is_crt = child->device_type & DEVICE_TYPE_ANALOG_OUTPUT;
1300         is_hdmi = is_dvi && (child->device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
1301         is_edp = is_dp && (child->device_type & DEVICE_TYPE_INTERNAL_CONNECTOR);
1302
1303         if (port == PORT_A && is_dvi) {
1304                 DRM_DEBUG_KMS("VBT claims port A supports DVI%s, ignoring\n",
1305                               is_hdmi ? "/HDMI" : "");
1306                 is_dvi = false;
1307                 is_hdmi = false;
1308         }
1309
1310         info->supports_dvi = is_dvi;
1311         info->supports_hdmi = is_hdmi;
1312         info->supports_dp = is_dp;
1313         info->supports_edp = is_edp;
1314
1315         DRM_DEBUG_KMS("Port %c VBT info: DP:%d HDMI:%d DVI:%d EDP:%d CRT:%d\n",
1316                       port_name(port), is_dp, is_hdmi, is_dvi, is_edp, is_crt);
1317
1318         if (is_edp && is_dvi)
1319                 DRM_DEBUG_KMS("Internal DP port %c is TMDS compatible\n",
1320                               port_name(port));
1321         if (is_crt && port != PORT_E)
1322                 DRM_DEBUG_KMS("Port %c is analog\n", port_name(port));
1323         if (is_crt && (is_dvi || is_dp))
1324                 DRM_DEBUG_KMS("Analog port %c is also DP or TMDS compatible\n",
1325                               port_name(port));
1326         if (is_dvi && (port == PORT_A || port == PORT_E))
1327                 DRM_DEBUG_KMS("Port %c is TMDS compatible\n", port_name(port));
1328         if (!is_dvi && !is_dp && !is_crt)
1329                 DRM_DEBUG_KMS("Port %c is not DP/TMDS/CRT compatible\n",
1330                               port_name(port));
1331         if (is_edp && (port == PORT_B || port == PORT_C || port == PORT_E))
1332                 DRM_DEBUG_KMS("Port %c is internal DP\n", port_name(port));
1333
1334         if (is_dvi) {
1335                 u8 ddc_pin;
1336
1337                 ddc_pin = map_ddc_pin(dev_priv, child->ddc_pin);
1338                 if (intel_gmbus_is_valid_pin(dev_priv, ddc_pin)) {
1339                         info->alternate_ddc_pin = ddc_pin;
1340                         sanitize_ddc_pin(dev_priv, port);
1341                 } else {
1342                         DRM_DEBUG_KMS("Port %c has invalid DDC pin %d, "
1343                                       "sticking to defaults\n",
1344                                       port_name(port), ddc_pin);
1345                 }
1346         }
1347
1348         if (is_dp) {
1349                 info->alternate_aux_channel = child->aux_channel;
1350
1351                 sanitize_aux_ch(dev_priv, port);
1352         }
1353
1354         if (bdb_version >= 158) {
1355                 /* The VBT HDMI level shift values match the table we have. */
1356                 u8 hdmi_level_shift = child->hdmi_level_shifter_value;
1357                 DRM_DEBUG_KMS("VBT HDMI level shift for port %c: %d\n",
1358                               port_name(port),
1359                               hdmi_level_shift);
1360                 info->hdmi_level_shift = hdmi_level_shift;
1361         }
1362
1363         if (bdb_version >= 204) {
1364                 int max_tmds_clock;
1365
1366                 switch (child->hdmi_max_data_rate) {
1367                 default:
1368                         MISSING_CASE(child->hdmi_max_data_rate);
1369                         /* fall through */
1370                 case HDMI_MAX_DATA_RATE_PLATFORM:
1371                         max_tmds_clock = 0;
1372                         break;
1373                 case HDMI_MAX_DATA_RATE_297:
1374                         max_tmds_clock = 297000;
1375                         break;
1376                 case HDMI_MAX_DATA_RATE_165:
1377                         max_tmds_clock = 165000;
1378                         break;
1379                 }
1380
1381                 if (max_tmds_clock)
1382                         DRM_DEBUG_KMS("VBT HDMI max TMDS clock for port %c: %d kHz\n",
1383                                       port_name(port), max_tmds_clock);
1384                 info->max_tmds_clock = max_tmds_clock;
1385         }
1386
1387         /* Parse the I_boost config for SKL and above */
1388         if (bdb_version >= 196 && child->iboost) {
1389                 info->dp_boost_level = translate_iboost(child->dp_iboost_level);
1390                 DRM_DEBUG_KMS("VBT (e)DP boost level for port %c: %d\n",
1391                               port_name(port), info->dp_boost_level);
1392                 info->hdmi_boost_level = translate_iboost(child->hdmi_iboost_level);
1393                 DRM_DEBUG_KMS("VBT HDMI boost level for port %c: %d\n",
1394                               port_name(port), info->hdmi_boost_level);
1395         }
1396
1397         /* DP max link rate for CNL+ */
1398         if (bdb_version >= 216) {
1399                 switch (child->dp_max_link_rate) {
1400                 default:
1401                 case VBT_DP_MAX_LINK_RATE_HBR3:
1402                         info->dp_max_link_rate = 810000;
1403                         break;
1404                 case VBT_DP_MAX_LINK_RATE_HBR2:
1405                         info->dp_max_link_rate = 540000;
1406                         break;
1407                 case VBT_DP_MAX_LINK_RATE_HBR:
1408                         info->dp_max_link_rate = 270000;
1409                         break;
1410                 case VBT_DP_MAX_LINK_RATE_LBR:
1411                         info->dp_max_link_rate = 162000;
1412                         break;
1413                 }
1414                 DRM_DEBUG_KMS("VBT DP max link rate for port %c: %d\n",
1415                               port_name(port), info->dp_max_link_rate);
1416         }
1417 }
1418
1419 static void parse_ddi_ports(struct drm_i915_private *dev_priv, u8 bdb_version)
1420 {
1421         enum port port;
1422
1423         if (!HAS_DDI(dev_priv) && !IS_CHERRYVIEW(dev_priv))
1424                 return;
1425
1426         if (!dev_priv->vbt.child_dev_num)
1427                 return;
1428
1429         if (bdb_version < 155)
1430                 return;
1431
1432         for (port = PORT_A; port < I915_MAX_PORTS; port++)
1433                 parse_ddi_port(dev_priv, port, bdb_version);
1434 }
1435
1436 static void
1437 parse_general_definitions(struct drm_i915_private *dev_priv,
1438                           const struct bdb_header *bdb)
1439 {
1440         const struct bdb_general_definitions *defs;
1441         const struct child_device_config *child;
1442         int i, child_device_num, count;
1443         u8 expected_size;
1444         u16 block_size;
1445         int bus_pin;
1446
1447         defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
1448         if (!defs) {
1449                 DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
1450                 return;
1451         }
1452
1453         block_size = get_blocksize(defs);
1454         if (block_size < sizeof(*defs)) {
1455                 DRM_DEBUG_KMS("General definitions block too small (%u)\n",
1456                               block_size);
1457                 return;
1458         }
1459
1460         bus_pin = defs->crt_ddc_gmbus_pin;
1461         DRM_DEBUG_KMS("crt_ddc_bus_pin: %d\n", bus_pin);
1462         if (intel_gmbus_is_valid_pin(dev_priv, bus_pin))
1463                 dev_priv->vbt.crt_ddc_pin = bus_pin;
1464
1465         if (bdb->version < 106) {
1466                 expected_size = 22;
1467         } else if (bdb->version < 111) {
1468                 expected_size = 27;
1469         } else if (bdb->version < 195) {
1470                 expected_size = LEGACY_CHILD_DEVICE_CONFIG_SIZE;
1471         } else if (bdb->version == 195) {
1472                 expected_size = 37;
1473         } else if (bdb->version <= 215) {
1474                 expected_size = 38;
1475         } else if (bdb->version <= 216) {
1476                 expected_size = 39;
1477         } else {
1478                 expected_size = sizeof(*child);
1479                 BUILD_BUG_ON(sizeof(*child) < 39);
1480                 DRM_DEBUG_DRIVER("Expected child device config size for VBT version %u not known; assuming %u\n",
1481                                  bdb->version, expected_size);
1482         }
1483
1484         /* Flag an error for unexpected size, but continue anyway. */
1485         if (defs->child_dev_size != expected_size)
1486                 DRM_ERROR("Unexpected child device config size %u (expected %u for VBT version %u)\n",
1487                           defs->child_dev_size, expected_size, bdb->version);
1488
1489         /* The legacy sized child device config is the minimum we need. */
1490         if (defs->child_dev_size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
1491                 DRM_DEBUG_KMS("Child device config size %u is too small.\n",
1492                               defs->child_dev_size);
1493                 return;
1494         }
1495
1496         /* get the number of child device */
1497         child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
1498         count = 0;
1499         /* get the number of child device that is present */
1500         for (i = 0; i < child_device_num; i++) {
1501                 child = child_device_ptr(defs, i);
1502                 if (!child->device_type)
1503                         continue;
1504                 count++;
1505         }
1506         if (!count) {
1507                 DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
1508                 return;
1509         }
1510         dev_priv->vbt.child_dev = kcalloc(count, sizeof(*child), GFP_KERNEL);
1511         if (!dev_priv->vbt.child_dev) {
1512                 DRM_DEBUG_KMS("No memory space for child device\n");
1513                 return;
1514         }
1515
1516         dev_priv->vbt.child_dev_num = count;
1517         count = 0;
1518         for (i = 0; i < child_device_num; i++) {
1519                 child = child_device_ptr(defs, i);
1520                 if (!child->device_type)
1521                         continue;
1522
1523                 /*
1524                  * Copy as much as we know (sizeof) and is available
1525                  * (child_dev_size) of the child device. Accessing the data must
1526                  * depend on VBT version.
1527                  */
1528                 memcpy(dev_priv->vbt.child_dev + count, child,
1529                        min_t(size_t, defs->child_dev_size, sizeof(*child)));
1530                 count++;
1531         }
1532 }
1533
1534 /* Common defaults which may be overridden by VBT. */
1535 static void
1536 init_vbt_defaults(struct drm_i915_private *dev_priv)
1537 {
1538         enum port port;
1539
1540         dev_priv->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
1541
1542         /* Default to having backlight */
1543         dev_priv->vbt.backlight.present = true;
1544
1545         /* LFP panel data */
1546         dev_priv->vbt.lvds_dither = 1;
1547
1548         /* SDVO panel data */
1549         dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1550
1551         /* general features */
1552         dev_priv->vbt.int_tv_support = 1;
1553         dev_priv->vbt.int_crt_support = 1;
1554
1555         /* driver features */
1556         dev_priv->vbt.int_lvds_support = 1;
1557
1558         /* Default to using SSC */
1559         dev_priv->vbt.lvds_use_ssc = 1;
1560         /*
1561          * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
1562          * clock for LVDS.
1563          */
1564         dev_priv->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(dev_priv,
1565                         !HAS_PCH_SPLIT(dev_priv));
1566         DRM_DEBUG_KMS("Set default to SSC at %d kHz\n", dev_priv->vbt.lvds_ssc_freq);
1567
1568         for (port = PORT_A; port < I915_MAX_PORTS; port++) {
1569                 struct ddi_vbt_port_info *info =
1570                         &dev_priv->vbt.ddi_port_info[port];
1571
1572                 info->hdmi_level_shift = HDMI_LEVEL_SHIFT_UNKNOWN;
1573         }
1574 }
1575
1576 /* Defaults to initialize only if there is no VBT. */
1577 static void
1578 init_vbt_missing_defaults(struct drm_i915_private *dev_priv)
1579 {
1580         enum port port;
1581
1582         for (port = PORT_A; port < I915_MAX_PORTS; port++) {
1583                 struct ddi_vbt_port_info *info =
1584                         &dev_priv->vbt.ddi_port_info[port];
1585
1586                 info->supports_dvi = (port != PORT_A && port != PORT_E);
1587                 info->supports_hdmi = info->supports_dvi;
1588                 info->supports_dp = (port != PORT_E);
1589         }
1590 }
1591
1592 static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
1593 {
1594         const void *_vbt = vbt;
1595
1596         return _vbt + vbt->bdb_offset;
1597 }
1598
1599 /**
1600  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
1601  * @buf:        pointer to a buffer to validate
1602  * @size:       size of the buffer
1603  *
1604  * Returns true on valid VBT.
1605  */
1606 bool intel_bios_is_valid_vbt(const void *buf, size_t size)
1607 {
1608         const struct vbt_header *vbt = buf;
1609         const struct bdb_header *bdb;
1610
1611         if (!vbt)
1612                 return false;
1613
1614         if (sizeof(struct vbt_header) > size) {
1615                 DRM_DEBUG_DRIVER("VBT header incomplete\n");
1616                 return false;
1617         }
1618
1619         if (memcmp(vbt->signature, "$VBT", 4)) {
1620                 DRM_DEBUG_DRIVER("VBT invalid signature\n");
1621                 return false;
1622         }
1623
1624         if (range_overflows_t(size_t,
1625                               vbt->bdb_offset,
1626                               sizeof(struct bdb_header),
1627                               size)) {
1628                 DRM_DEBUG_DRIVER("BDB header incomplete\n");
1629                 return false;
1630         }
1631
1632         bdb = get_bdb_header(vbt);
1633         if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
1634                 DRM_DEBUG_DRIVER("BDB incomplete\n");
1635                 return false;
1636         }
1637
1638         return vbt;
1639 }
1640
1641 static const struct vbt_header *find_vbt(void __iomem *bios, size_t size)
1642 {
1643         size_t i;
1644
1645         /* Scour memory looking for the VBT signature. */
1646         for (i = 0; i + 4 < size; i++) {
1647                 void *vbt;
1648
1649                 if (ioread32(bios + i) != *((const u32 *) "$VBT"))
1650                         continue;
1651
1652                 /*
1653                  * This is the one place where we explicitly discard the address
1654                  * space (__iomem) of the BIOS/VBT.
1655                  */
1656                 vbt = (void __force *) bios + i;
1657                 if (intel_bios_is_valid_vbt(vbt, size - i))
1658                         return vbt;
1659
1660                 break;
1661         }
1662
1663         return NULL;
1664 }
1665
1666 /**
1667  * intel_bios_init - find VBT and initialize settings from the BIOS
1668  * @dev_priv: i915 device instance
1669  *
1670  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
1671  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
1672  * initialize some defaults if the VBT is not present at all.
1673  */
1674 void intel_bios_init(struct drm_i915_private *dev_priv)
1675 {
1676         struct pci_dev *pdev = dev_priv->drm.pdev;
1677         const struct vbt_header *vbt = dev_priv->opregion.vbt;
1678         const struct bdb_header *bdb;
1679         u8 __iomem *bios = NULL;
1680
1681         if (HAS_PCH_NOP(dev_priv)) {
1682                 DRM_DEBUG_KMS("Skipping VBT init due to disabled display.\n");
1683                 return;
1684         }
1685
1686         init_vbt_defaults(dev_priv);
1687
1688         /* If the OpRegion does not have VBT, look in PCI ROM. */
1689         if (!vbt) {
1690                 size_t size;
1691
1692                 bios = pci_map_rom(pdev, &size);
1693                 if (!bios)
1694                         goto out;
1695
1696                 vbt = find_vbt(bios, size);
1697                 if (!vbt)
1698                         goto out;
1699
1700                 DRM_DEBUG_KMS("Found valid VBT in PCI ROM\n");
1701         }
1702
1703         bdb = get_bdb_header(vbt);
1704
1705         DRM_DEBUG_KMS("VBT signature \"%.*s\", BDB version %d\n",
1706                       (int)sizeof(vbt->signature), vbt->signature, bdb->version);
1707
1708         /* Grab useful general definitions */
1709         parse_general_features(dev_priv, bdb);
1710         parse_general_definitions(dev_priv, bdb);
1711         parse_lfp_panel_data(dev_priv, bdb);
1712         parse_lfp_backlight(dev_priv, bdb);
1713         parse_sdvo_panel_data(dev_priv, bdb);
1714         parse_driver_features(dev_priv, bdb);
1715         parse_edp(dev_priv, bdb);
1716         parse_psr(dev_priv, bdb);
1717         parse_mipi_config(dev_priv, bdb);
1718         parse_mipi_sequence(dev_priv, bdb);
1719
1720         /* Further processing on pre-parsed data */
1721         parse_sdvo_device_mapping(dev_priv, bdb->version);
1722         parse_ddi_ports(dev_priv, bdb->version);
1723
1724 out:
1725         if (!vbt) {
1726                 DRM_INFO("Failed to find VBIOS tables (VBT)\n");
1727                 init_vbt_missing_defaults(dev_priv);
1728         }
1729
1730         if (bios)
1731                 pci_unmap_rom(pdev, bios);
1732 }
1733
1734 /**
1735  * intel_bios_cleanup - Free any resources allocated by intel_bios_init()
1736  * @dev_priv: i915 device instance
1737  */
1738 void intel_bios_cleanup(struct drm_i915_private *dev_priv)
1739 {
1740         kfree(dev_priv->vbt.child_dev);
1741         dev_priv->vbt.child_dev = NULL;
1742         dev_priv->vbt.child_dev_num = 0;
1743         kfree(dev_priv->vbt.sdvo_lvds_vbt_mode);
1744         dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
1745         kfree(dev_priv->vbt.lfp_lvds_vbt_mode);
1746         dev_priv->vbt.lfp_lvds_vbt_mode = NULL;
1747         kfree(dev_priv->vbt.dsi.data);
1748         dev_priv->vbt.dsi.data = NULL;
1749         kfree(dev_priv->vbt.dsi.pps);
1750         dev_priv->vbt.dsi.pps = NULL;
1751         kfree(dev_priv->vbt.dsi.config);
1752         dev_priv->vbt.dsi.config = NULL;
1753         kfree(dev_priv->vbt.dsi.deassert_seq);
1754         dev_priv->vbt.dsi.deassert_seq = NULL;
1755 }
1756
1757 /**
1758  * intel_bios_is_tv_present - is integrated TV present in VBT
1759  * @dev_priv:   i915 device instance
1760  *
1761  * Return true if TV is present. If no child devices were parsed from VBT,
1762  * assume TV is present.
1763  */
1764 bool intel_bios_is_tv_present(struct drm_i915_private *dev_priv)
1765 {
1766         const struct child_device_config *child;
1767         int i;
1768
1769         if (!dev_priv->vbt.int_tv_support)
1770                 return false;
1771
1772         if (!dev_priv->vbt.child_dev_num)
1773                 return true;
1774
1775         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1776                 child = dev_priv->vbt.child_dev + i;
1777                 /*
1778                  * If the device type is not TV, continue.
1779                  */
1780                 switch (child->device_type) {
1781                 case DEVICE_TYPE_INT_TV:
1782                 case DEVICE_TYPE_TV:
1783                 case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
1784                         break;
1785                 default:
1786                         continue;
1787                 }
1788                 /* Only when the addin_offset is non-zero, it is regarded
1789                  * as present.
1790                  */
1791                 if (child->addin_offset)
1792                         return true;
1793         }
1794
1795         return false;
1796 }
1797
1798 /**
1799  * intel_bios_is_lvds_present - is LVDS present in VBT
1800  * @dev_priv:   i915 device instance
1801  * @i2c_pin:    i2c pin for LVDS if present
1802  *
1803  * Return true if LVDS is present. If no child devices were parsed from VBT,
1804  * assume LVDS is present.
1805  */
1806 bool intel_bios_is_lvds_present(struct drm_i915_private *dev_priv, u8 *i2c_pin)
1807 {
1808         const struct child_device_config *child;
1809         int i;
1810
1811         if (!dev_priv->vbt.child_dev_num)
1812                 return true;
1813
1814         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1815                 child = dev_priv->vbt.child_dev + i;
1816
1817                 /* If the device type is not LFP, continue.
1818                  * We have to check both the new identifiers as well as the
1819                  * old for compatibility with some BIOSes.
1820                  */
1821                 if (child->device_type != DEVICE_TYPE_INT_LFP &&
1822                     child->device_type != DEVICE_TYPE_LFP)
1823                         continue;
1824
1825                 if (intel_gmbus_is_valid_pin(dev_priv, child->i2c_pin))
1826                         *i2c_pin = child->i2c_pin;
1827
1828                 /* However, we cannot trust the BIOS writers to populate
1829                  * the VBT correctly.  Since LVDS requires additional
1830                  * information from AIM blocks, a non-zero addin offset is
1831                  * a good indicator that the LVDS is actually present.
1832                  */
1833                 if (child->addin_offset)
1834                         return true;
1835
1836                 /* But even then some BIOS writers perform some black magic
1837                  * and instantiate the device without reference to any
1838                  * additional data.  Trust that if the VBT was written into
1839                  * the OpRegion then they have validated the LVDS's existence.
1840                  */
1841                 if (dev_priv->opregion.vbt)
1842                         return true;
1843         }
1844
1845         return false;
1846 }
1847
1848 /**
1849  * intel_bios_is_port_present - is the specified digital port present
1850  * @dev_priv:   i915 device instance
1851  * @port:       port to check
1852  *
1853  * Return true if the device in %port is present.
1854  */
1855 bool intel_bios_is_port_present(struct drm_i915_private *dev_priv, enum port port)
1856 {
1857         const struct child_device_config *child;
1858         static const struct {
1859                 u16 dp, hdmi;
1860         } port_mapping[] = {
1861                 [PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
1862                 [PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
1863                 [PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
1864                 [PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
1865                 [PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
1866         };
1867         int i;
1868
1869         /* FIXME maybe deal with port A as well? */
1870         if (WARN_ON(port == PORT_A) || port >= ARRAY_SIZE(port_mapping))
1871                 return false;
1872
1873         if (!dev_priv->vbt.child_dev_num)
1874                 return false;
1875
1876         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1877                 child = dev_priv->vbt.child_dev + i;
1878
1879                 if ((child->dvo_port == port_mapping[port].dp ||
1880                      child->dvo_port == port_mapping[port].hdmi) &&
1881                     (child->device_type & (DEVICE_TYPE_TMDS_DVI_SIGNALING |
1882                                            DEVICE_TYPE_DISPLAYPORT_OUTPUT)))
1883                         return true;
1884         }
1885
1886         return false;
1887 }
1888
1889 /**
1890  * intel_bios_is_port_edp - is the device in given port eDP
1891  * @dev_priv:   i915 device instance
1892  * @port:       port to check
1893  *
1894  * Return true if the device in %port is eDP.
1895  */
1896 bool intel_bios_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
1897 {
1898         const struct child_device_config *child;
1899         static const short port_mapping[] = {
1900                 [PORT_B] = DVO_PORT_DPB,
1901                 [PORT_C] = DVO_PORT_DPC,
1902                 [PORT_D] = DVO_PORT_DPD,
1903                 [PORT_E] = DVO_PORT_DPE,
1904                 [PORT_F] = DVO_PORT_DPF,
1905         };
1906         int i;
1907
1908         if (HAS_DDI(dev_priv))
1909                 return dev_priv->vbt.ddi_port_info[port].supports_edp;
1910
1911         if (!dev_priv->vbt.child_dev_num)
1912                 return false;
1913
1914         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1915                 child = dev_priv->vbt.child_dev + i;
1916
1917                 if (child->dvo_port == port_mapping[port] &&
1918                     (child->device_type & DEVICE_TYPE_eDP_BITS) ==
1919                     (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
1920                         return true;
1921         }
1922
1923         return false;
1924 }
1925
1926 static bool child_dev_is_dp_dual_mode(const struct child_device_config *child,
1927                                       enum port port)
1928 {
1929         static const struct {
1930                 u16 dp, hdmi;
1931         } port_mapping[] = {
1932                 /*
1933                  * Buggy VBTs may declare DP ports as having
1934                  * HDMI type dvo_port :( So let's check both.
1935                  */
1936                 [PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
1937                 [PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
1938                 [PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
1939                 [PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
1940                 [PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
1941         };
1942
1943         if (port == PORT_A || port >= ARRAY_SIZE(port_mapping))
1944                 return false;
1945
1946         if ((child->device_type & DEVICE_TYPE_DP_DUAL_MODE_BITS) !=
1947             (DEVICE_TYPE_DP_DUAL_MODE & DEVICE_TYPE_DP_DUAL_MODE_BITS))
1948                 return false;
1949
1950         if (child->dvo_port == port_mapping[port].dp)
1951                 return true;
1952
1953         /* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
1954         if (child->dvo_port == port_mapping[port].hdmi &&
1955             child->aux_channel != 0)
1956                 return true;
1957
1958         return false;
1959 }
1960
1961 bool intel_bios_is_port_dp_dual_mode(struct drm_i915_private *dev_priv,
1962                                      enum port port)
1963 {
1964         const struct child_device_config *child;
1965         int i;
1966
1967         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1968                 child = dev_priv->vbt.child_dev + i;
1969
1970                 if (child_dev_is_dp_dual_mode(child, port))
1971                         return true;
1972         }
1973
1974         return false;
1975 }
1976
1977 /**
1978  * intel_bios_is_dsi_present - is DSI present in VBT
1979  * @dev_priv:   i915 device instance
1980  * @port:       port for DSI if present
1981  *
1982  * Return true if DSI is present, and return the port in %port.
1983  */
1984 bool intel_bios_is_dsi_present(struct drm_i915_private *dev_priv,
1985                                enum port *port)
1986 {
1987         const struct child_device_config *child;
1988         u8 dvo_port;
1989         int i;
1990
1991         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
1992                 child = dev_priv->vbt.child_dev + i;
1993
1994                 if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
1995                         continue;
1996
1997                 dvo_port = child->dvo_port;
1998
1999                 switch (dvo_port) {
2000                 case DVO_PORT_MIPIA:
2001                 case DVO_PORT_MIPIC:
2002                         if (port)
2003                                 *port = dvo_port - DVO_PORT_MIPIA;
2004                         return true;
2005                 case DVO_PORT_MIPIB:
2006                 case DVO_PORT_MIPID:
2007                         DRM_DEBUG_KMS("VBT has unsupported DSI port %c\n",
2008                                       port_name(dvo_port - DVO_PORT_MIPIA));
2009                         break;
2010                 }
2011         }
2012
2013         return false;
2014 }
2015
2016 /**
2017  * intel_bios_is_port_hpd_inverted - is HPD inverted for %port
2018  * @dev_priv:   i915 device instance
2019  * @port:       port to check
2020  *
2021  * Return true if HPD should be inverted for %port.
2022  */
2023 bool
2024 intel_bios_is_port_hpd_inverted(struct drm_i915_private *dev_priv,
2025                                 enum port port)
2026 {
2027         const struct child_device_config *child;
2028         int i;
2029
2030         if (WARN_ON_ONCE(!IS_GEN9_LP(dev_priv)))
2031                 return false;
2032
2033         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
2034                 child = dev_priv->vbt.child_dev + i;
2035
2036                 if (!child->hpd_invert)
2037                         continue;
2038
2039                 switch (child->dvo_port) {
2040                 case DVO_PORT_DPA:
2041                 case DVO_PORT_HDMIA:
2042                         if (port == PORT_A)
2043                                 return true;
2044                         break;
2045                 case DVO_PORT_DPB:
2046                 case DVO_PORT_HDMIB:
2047                         if (port == PORT_B)
2048                                 return true;
2049                         break;
2050                 case DVO_PORT_DPC:
2051                 case DVO_PORT_HDMIC:
2052                         if (port == PORT_C)
2053                                 return true;
2054                         break;
2055                 default:
2056                         break;
2057                 }
2058         }
2059
2060         return false;
2061 }
2062
2063 /**
2064  * intel_bios_is_lspcon_present - if LSPCON is attached on %port
2065  * @dev_priv:   i915 device instance
2066  * @port:       port to check
2067  *
2068  * Return true if LSPCON is present on this port
2069  */
2070 bool
2071 intel_bios_is_lspcon_present(struct drm_i915_private *dev_priv,
2072                                 enum port port)
2073 {
2074         const struct child_device_config *child;
2075         int i;
2076
2077         if (!HAS_LSPCON(dev_priv))
2078                 return false;
2079
2080         for (i = 0; i < dev_priv->vbt.child_dev_num; i++) {
2081                 child = dev_priv->vbt.child_dev + i;
2082
2083                 if (!child->lspcon)
2084                         continue;
2085
2086                 switch (child->dvo_port) {
2087                 case DVO_PORT_DPA:
2088                 case DVO_PORT_HDMIA:
2089                         if (port == PORT_A)
2090                                 return true;
2091                         break;
2092                 case DVO_PORT_DPB:
2093                 case DVO_PORT_HDMIB:
2094                         if (port == PORT_B)
2095                                 return true;
2096                         break;
2097                 case DVO_PORT_DPC:
2098                 case DVO_PORT_HDMIC:
2099                         if (port == PORT_C)
2100                                 return true;
2101                         break;
2102                 case DVO_PORT_DPD:
2103                 case DVO_PORT_HDMID:
2104                         if (port == PORT_D)
2105                                 return true;
2106                         break;
2107                 case DVO_PORT_DPF:
2108                 case DVO_PORT_HDMIF:
2109                         if (port == PORT_F)
2110                                 return true;
2111                         break;
2112                 default:
2113                         break;
2114                 }
2115         }
2116
2117         return false;
2118 }