Home | History | Annotate | Line # | Download | only in display
      1  1.4  riastrad /*	$NetBSD: intel_bios.c,v 1.4 2021/12/19 12:24:49 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2006 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     22  1.1  riastrad  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     23  1.1  riastrad  * SOFTWARE.
     24  1.1  riastrad  *
     25  1.1  riastrad  * Authors:
     26  1.1  riastrad  *    Eric Anholt <eric (at) anholt.net>
     27  1.1  riastrad  *
     28  1.1  riastrad  */
     29  1.1  riastrad 
     30  1.1  riastrad #include <sys/cdefs.h>
     31  1.4  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_bios.c,v 1.4 2021/12/19 12:24:49 riastradh Exp $");
     32  1.1  riastrad 
     33  1.1  riastrad #include <drm/drm_dp_helper.h>
     34  1.1  riastrad #include <drm/i915_drm.h>
     35  1.1  riastrad 
     36  1.1  riastrad #include "display/intel_display.h"
     37  1.1  riastrad #include "display/intel_display_types.h"
     38  1.1  riastrad #include "display/intel_gmbus.h"
     39  1.1  riastrad 
     40  1.1  riastrad #include "i915_drv.h"
     41  1.1  riastrad 
     42  1.1  riastrad #define _INTEL_BIOS_PRIVATE
     43  1.1  riastrad #include "intel_vbt_defs.h"
     44  1.1  riastrad 
     45  1.1  riastrad /**
     46  1.1  riastrad  * DOC: Video BIOS Table (VBT)
     47  1.1  riastrad  *
     48  1.1  riastrad  * The Video BIOS Table, or VBT, provides platform and board specific
     49  1.1  riastrad  * configuration information to the driver that is not discoverable or available
     50  1.1  riastrad  * through other means. The configuration is mostly related to display
     51  1.1  riastrad  * hardware. The VBT is available via the ACPI OpRegion or, on older systems, in
     52  1.1  riastrad  * the PCI ROM.
     53  1.1  riastrad  *
     54  1.1  riastrad  * The VBT consists of a VBT Header (defined as &struct vbt_header), a BDB
     55  1.1  riastrad  * Header (&struct bdb_header), and a number of BIOS Data Blocks (BDB) that
     56  1.1  riastrad  * contain the actual configuration information. The VBT Header, and thus the
     57  1.1  riastrad  * VBT, begins with "$VBT" signature. The VBT Header contains the offset of the
     58  1.1  riastrad  * BDB Header. The data blocks are concatenated after the BDB Header. The data
     59  1.1  riastrad  * blocks have a 1-byte Block ID, 2-byte Block Size, and Block Size bytes of
     60  1.1  riastrad  * data. (Block 53, the MIPI Sequence Block is an exception.)
     61  1.1  riastrad  *
     62  1.1  riastrad  * The driver parses the VBT during load. The relevant information is stored in
     63  1.1  riastrad  * driver private data for ease of use, and the actual VBT is not read after
     64  1.1  riastrad  * that.
     65  1.1  riastrad  */
     66  1.1  riastrad 
     67  1.1  riastrad /* Wrapper for VBT child device config */
     68  1.1  riastrad struct display_device_data {
     69  1.1  riastrad 	struct child_device_config child;
     70  1.1  riastrad 	struct dsc_compression_parameters_entry *dsc;
     71  1.1  riastrad 	struct list_head node;
     72  1.1  riastrad };
     73  1.1  riastrad 
     74  1.1  riastrad #define	SLAVE_ADDR1	0x70
     75  1.1  riastrad #define	SLAVE_ADDR2	0x72
     76  1.1  riastrad 
     77  1.1  riastrad /* Get BDB block size given a pointer to Block ID. */
     78  1.1  riastrad static u32 _get_blocksize(const u8 *block_base)
     79  1.1  riastrad {
     80  1.1  riastrad 	/* The MIPI Sequence Block v3+ has a separate size field. */
     81  1.1  riastrad 	if (*block_base == BDB_MIPI_SEQUENCE && *(block_base + 3) >= 3)
     82  1.1  riastrad 		return *((const u32 *)(block_base + 4));
     83  1.1  riastrad 	else
     84  1.1  riastrad 		return *((const u16 *)(block_base + 1));
     85  1.1  riastrad }
     86  1.1  riastrad 
     87  1.1  riastrad /* Get BDB block size give a pointer to data after Block ID and Block Size. */
     88  1.1  riastrad static u32 get_blocksize(const void *block_data)
     89  1.1  riastrad {
     90  1.1  riastrad 	return _get_blocksize(block_data - 3);
     91  1.1  riastrad }
     92  1.1  riastrad 
     93  1.1  riastrad static const void *
     94  1.1  riastrad find_section(const void *_bdb, enum bdb_block_id section_id)
     95  1.1  riastrad {
     96  1.1  riastrad 	const struct bdb_header *bdb = _bdb;
     97  1.1  riastrad 	const u8 *base = _bdb;
     98  1.1  riastrad 	int index = 0;
     99  1.1  riastrad 	u32 total, current_size;
    100  1.1  riastrad 	enum bdb_block_id current_id;
    101  1.1  riastrad 
    102  1.1  riastrad 	/* skip to first section */
    103  1.1  riastrad 	index += bdb->header_size;
    104  1.1  riastrad 	total = bdb->bdb_size;
    105  1.1  riastrad 
    106  1.1  riastrad 	/* walk the sections looking for section_id */
    107  1.1  riastrad 	while (index + 3 < total) {
    108  1.1  riastrad 		current_id = *(base + index);
    109  1.1  riastrad 		current_size = _get_blocksize(base + index);
    110  1.1  riastrad 		index += 3;
    111  1.1  riastrad 
    112  1.1  riastrad 		if (index + current_size > total)
    113  1.1  riastrad 			return NULL;
    114  1.1  riastrad 
    115  1.1  riastrad 		if (current_id == section_id)
    116  1.1  riastrad 			return base + index;
    117  1.1  riastrad 
    118  1.1  riastrad 		index += current_size;
    119  1.1  riastrad 	}
    120  1.1  riastrad 
    121  1.1  riastrad 	return NULL;
    122  1.1  riastrad }
    123  1.1  riastrad 
    124  1.1  riastrad static void
    125  1.1  riastrad fill_detail_timing_data(struct drm_display_mode *panel_fixed_mode,
    126  1.1  riastrad 			const struct lvds_dvo_timing *dvo_timing)
    127  1.1  riastrad {
    128  1.1  riastrad 	panel_fixed_mode->hdisplay = (dvo_timing->hactive_hi << 8) |
    129  1.1  riastrad 		dvo_timing->hactive_lo;
    130  1.1  riastrad 	panel_fixed_mode->hsync_start = panel_fixed_mode->hdisplay +
    131  1.1  riastrad 		((dvo_timing->hsync_off_hi << 8) | dvo_timing->hsync_off_lo);
    132  1.1  riastrad 	panel_fixed_mode->hsync_end = panel_fixed_mode->hsync_start +
    133  1.1  riastrad 		((dvo_timing->hsync_pulse_width_hi << 8) |
    134  1.1  riastrad 			dvo_timing->hsync_pulse_width_lo);
    135  1.1  riastrad 	panel_fixed_mode->htotal = panel_fixed_mode->hdisplay +
    136  1.1  riastrad 		((dvo_timing->hblank_hi << 8) | dvo_timing->hblank_lo);
    137  1.1  riastrad 
    138  1.1  riastrad 	panel_fixed_mode->vdisplay = (dvo_timing->vactive_hi << 8) |
    139  1.1  riastrad 		dvo_timing->vactive_lo;
    140  1.1  riastrad 	panel_fixed_mode->vsync_start = panel_fixed_mode->vdisplay +
    141  1.1  riastrad 		((dvo_timing->vsync_off_hi << 4) | dvo_timing->vsync_off_lo);
    142  1.1  riastrad 	panel_fixed_mode->vsync_end = panel_fixed_mode->vsync_start +
    143  1.1  riastrad 		((dvo_timing->vsync_pulse_width_hi << 4) |
    144  1.1  riastrad 			dvo_timing->vsync_pulse_width_lo);
    145  1.1  riastrad 	panel_fixed_mode->vtotal = panel_fixed_mode->vdisplay +
    146  1.1  riastrad 		((dvo_timing->vblank_hi << 8) | dvo_timing->vblank_lo);
    147  1.1  riastrad 	panel_fixed_mode->clock = dvo_timing->clock * 10;
    148  1.1  riastrad 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
    149  1.1  riastrad 
    150  1.1  riastrad 	if (dvo_timing->hsync_positive)
    151  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
    152  1.1  riastrad 	else
    153  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
    154  1.1  riastrad 
    155  1.1  riastrad 	if (dvo_timing->vsync_positive)
    156  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
    157  1.1  riastrad 	else
    158  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
    159  1.1  riastrad 
    160  1.1  riastrad 	panel_fixed_mode->width_mm = (dvo_timing->himage_hi << 8) |
    161  1.1  riastrad 		dvo_timing->himage_lo;
    162  1.1  riastrad 	panel_fixed_mode->height_mm = (dvo_timing->vimage_hi << 8) |
    163  1.1  riastrad 		dvo_timing->vimage_lo;
    164  1.1  riastrad 
    165  1.1  riastrad 	/* Some VBTs have bogus h/vtotal values */
    166  1.1  riastrad 	if (panel_fixed_mode->hsync_end > panel_fixed_mode->htotal)
    167  1.1  riastrad 		panel_fixed_mode->htotal = panel_fixed_mode->hsync_end + 1;
    168  1.1  riastrad 	if (panel_fixed_mode->vsync_end > panel_fixed_mode->vtotal)
    169  1.1  riastrad 		panel_fixed_mode->vtotal = panel_fixed_mode->vsync_end + 1;
    170  1.1  riastrad 
    171  1.1  riastrad 	drm_mode_set_name(panel_fixed_mode);
    172  1.1  riastrad }
    173  1.1  riastrad 
    174  1.1  riastrad static const struct lvds_dvo_timing *
    175  1.1  riastrad get_lvds_dvo_timing(const struct bdb_lvds_lfp_data *lvds_lfp_data,
    176  1.1  riastrad 		    const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs,
    177  1.1  riastrad 		    int index)
    178  1.1  riastrad {
    179  1.1  riastrad 	/*
    180  1.1  riastrad 	 * the size of fp_timing varies on the different platform.
    181  1.1  riastrad 	 * So calculate the DVO timing relative offset in LVDS data
    182  1.1  riastrad 	 * entry to get the DVO timing entry
    183  1.1  riastrad 	 */
    184  1.1  riastrad 
    185  1.1  riastrad 	int lfp_data_size =
    186  1.1  riastrad 		lvds_lfp_data_ptrs->ptr[1].dvo_timing_offset -
    187  1.1  riastrad 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset;
    188  1.1  riastrad 	int dvo_timing_offset =
    189  1.1  riastrad 		lvds_lfp_data_ptrs->ptr[0].dvo_timing_offset -
    190  1.1  riastrad 		lvds_lfp_data_ptrs->ptr[0].fp_timing_offset;
    191  1.2  riastrad 	const char *entry = (const char *)lvds_lfp_data->data +
    192  1.2  riastrad 	    lfp_data_size * index;
    193  1.2  riastrad 
    194  1.2  riastrad 	return (const struct lvds_dvo_timing *)(entry + dvo_timing_offset);
    195  1.1  riastrad }
    196  1.1  riastrad 
    197  1.1  riastrad /* get lvds_fp_timing entry
    198  1.1  riastrad  * this function may return NULL if the corresponding entry is invalid
    199  1.1  riastrad  */
    200  1.1  riastrad static const struct lvds_fp_timing *
    201  1.1  riastrad get_lvds_fp_timing(const struct bdb_header *bdb,
    202  1.1  riastrad 		   const struct bdb_lvds_lfp_data *data,
    203  1.1  riastrad 		   const struct bdb_lvds_lfp_data_ptrs *ptrs,
    204  1.1  riastrad 		   int index)
    205  1.1  riastrad {
    206  1.1  riastrad 	size_t data_ofs = (const u8 *)data - (const u8 *)bdb;
    207  1.1  riastrad 	u16 data_size = ((const u16 *)data)[-1]; /* stored in header */
    208  1.1  riastrad 	size_t ofs;
    209  1.1  riastrad 
    210  1.1  riastrad 	if (index >= ARRAY_SIZE(ptrs->ptr))
    211  1.1  riastrad 		return NULL;
    212  1.1  riastrad 	ofs = ptrs->ptr[index].fp_timing_offset;
    213  1.1  riastrad 	if (ofs < data_ofs ||
    214  1.1  riastrad 	    ofs + sizeof(struct lvds_fp_timing) > data_ofs + data_size)
    215  1.1  riastrad 		return NULL;
    216  1.1  riastrad 	return (const struct lvds_fp_timing *)((const u8 *)bdb + ofs);
    217  1.1  riastrad }
    218  1.1  riastrad 
    219  1.1  riastrad /* Parse general panel options */
    220  1.1  riastrad static void
    221  1.1  riastrad parse_panel_options(struct drm_i915_private *dev_priv,
    222  1.1  riastrad 		    const struct bdb_header *bdb)
    223  1.1  riastrad {
    224  1.1  riastrad 	const struct bdb_lvds_options *lvds_options;
    225  1.1  riastrad 	int panel_type;
    226  1.1  riastrad 	int drrs_mode;
    227  1.1  riastrad 	int ret;
    228  1.1  riastrad 
    229  1.1  riastrad 	lvds_options = find_section(bdb, BDB_LVDS_OPTIONS);
    230  1.1  riastrad 	if (!lvds_options)
    231  1.1  riastrad 		return;
    232  1.1  riastrad 
    233  1.1  riastrad 	dev_priv->vbt.lvds_dither = lvds_options->pixel_dither;
    234  1.1  riastrad 
    235  1.1  riastrad 	ret = intel_opregion_get_panel_type(dev_priv);
    236  1.1  riastrad 	if (ret >= 0) {
    237  1.1  riastrad 		WARN_ON(ret > 0xf);
    238  1.1  riastrad 		panel_type = ret;
    239  1.1  riastrad 		DRM_DEBUG_KMS("Panel type: %d (OpRegion)\n", panel_type);
    240  1.1  riastrad 	} else {
    241  1.1  riastrad 		if (lvds_options->panel_type > 0xf) {
    242  1.1  riastrad 			DRM_DEBUG_KMS("Invalid VBT panel type 0x%x\n",
    243  1.1  riastrad 				      lvds_options->panel_type);
    244  1.1  riastrad 			return;
    245  1.1  riastrad 		}
    246  1.1  riastrad 		panel_type = lvds_options->panel_type;
    247  1.1  riastrad 		DRM_DEBUG_KMS("Panel type: %d (VBT)\n", panel_type);
    248  1.1  riastrad 	}
    249  1.1  riastrad 
    250  1.1  riastrad 	dev_priv->vbt.panel_type = panel_type;
    251  1.1  riastrad 
    252  1.1  riastrad 	drrs_mode = (lvds_options->dps_panel_type_bits
    253  1.1  riastrad 				>> (panel_type * 2)) & MODE_MASK;
    254  1.1  riastrad 	/*
    255  1.1  riastrad 	 * VBT has static DRRS = 0 and seamless DRRS = 2.
    256  1.1  riastrad 	 * The below piece of code is required to adjust vbt.drrs_type
    257  1.1  riastrad 	 * to match the enum drrs_support_type.
    258  1.1  riastrad 	 */
    259  1.1  riastrad 	switch (drrs_mode) {
    260  1.1  riastrad 	case 0:
    261  1.1  riastrad 		dev_priv->vbt.drrs_type = STATIC_DRRS_SUPPORT;
    262  1.1  riastrad 		DRM_DEBUG_KMS("DRRS supported mode is static\n");
    263  1.1  riastrad 		break;
    264  1.1  riastrad 	case 2:
    265  1.1  riastrad 		dev_priv->vbt.drrs_type = SEAMLESS_DRRS_SUPPORT;
    266  1.1  riastrad 		DRM_DEBUG_KMS("DRRS supported mode is seamless\n");
    267  1.1  riastrad 		break;
    268  1.1  riastrad 	default:
    269  1.1  riastrad 		dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
    270  1.1  riastrad 		DRM_DEBUG_KMS("DRRS not supported (VBT input)\n");
    271  1.1  riastrad 		break;
    272  1.1  riastrad 	}
    273  1.1  riastrad }
    274  1.1  riastrad 
    275  1.1  riastrad /* Try to find integrated panel timing data */
    276  1.1  riastrad static void
    277  1.1  riastrad parse_lfp_panel_dtd(struct drm_i915_private *dev_priv,
    278  1.1  riastrad 		    const struct bdb_header *bdb)
    279  1.1  riastrad {
    280  1.1  riastrad 	const struct bdb_lvds_lfp_data *lvds_lfp_data;
    281  1.1  riastrad 	const struct bdb_lvds_lfp_data_ptrs *lvds_lfp_data_ptrs;
    282  1.1  riastrad 	const struct lvds_dvo_timing *panel_dvo_timing;
    283  1.1  riastrad 	const struct lvds_fp_timing *fp_timing;
    284  1.1  riastrad 	struct drm_display_mode *panel_fixed_mode;
    285  1.1  riastrad 	int panel_type = dev_priv->vbt.panel_type;
    286  1.1  riastrad 
    287  1.1  riastrad 	lvds_lfp_data = find_section(bdb, BDB_LVDS_LFP_DATA);
    288  1.1  riastrad 	if (!lvds_lfp_data)
    289  1.1  riastrad 		return;
    290  1.1  riastrad 
    291  1.1  riastrad 	lvds_lfp_data_ptrs = find_section(bdb, BDB_LVDS_LFP_DATA_PTRS);
    292  1.1  riastrad 	if (!lvds_lfp_data_ptrs)
    293  1.1  riastrad 		return;
    294  1.1  riastrad 
    295  1.1  riastrad 	panel_dvo_timing = get_lvds_dvo_timing(lvds_lfp_data,
    296  1.1  riastrad 					       lvds_lfp_data_ptrs,
    297  1.1  riastrad 					       panel_type);
    298  1.1  riastrad 
    299  1.1  riastrad 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
    300  1.1  riastrad 	if (!panel_fixed_mode)
    301  1.1  riastrad 		return;
    302  1.1  riastrad 
    303  1.1  riastrad 	fill_detail_timing_data(panel_fixed_mode, panel_dvo_timing);
    304  1.1  riastrad 
    305  1.1  riastrad 	dev_priv->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
    306  1.1  riastrad 
    307  1.1  riastrad 	DRM_DEBUG_KMS("Found panel mode in BIOS VBT legacy lfp table:\n");
    308  1.1  riastrad 	drm_mode_debug_printmodeline(panel_fixed_mode);
    309  1.1  riastrad 
    310  1.1  riastrad 	fp_timing = get_lvds_fp_timing(bdb, lvds_lfp_data,
    311  1.1  riastrad 				       lvds_lfp_data_ptrs,
    312  1.1  riastrad 				       panel_type);
    313  1.1  riastrad 	if (fp_timing) {
    314  1.1  riastrad 		/* check the resolution, just to be sure */
    315  1.1  riastrad 		if (fp_timing->x_res == panel_fixed_mode->hdisplay &&
    316  1.1  riastrad 		    fp_timing->y_res == panel_fixed_mode->vdisplay) {
    317  1.1  riastrad 			dev_priv->vbt.bios_lvds_val = fp_timing->lvds_reg_val;
    318  1.1  riastrad 			DRM_DEBUG_KMS("VBT initial LVDS value %x\n",
    319  1.1  riastrad 				      dev_priv->vbt.bios_lvds_val);
    320  1.1  riastrad 		}
    321  1.1  riastrad 	}
    322  1.1  riastrad }
    323  1.1  riastrad 
    324  1.1  riastrad static void
    325  1.1  riastrad parse_generic_dtd(struct drm_i915_private *dev_priv,
    326  1.1  riastrad 		  const struct bdb_header *bdb)
    327  1.1  riastrad {
    328  1.1  riastrad 	const struct bdb_generic_dtd *generic_dtd;
    329  1.1  riastrad 	const struct generic_dtd_entry *dtd;
    330  1.1  riastrad 	struct drm_display_mode *panel_fixed_mode;
    331  1.1  riastrad 	int num_dtd;
    332  1.1  riastrad 
    333  1.1  riastrad 	generic_dtd = find_section(bdb, BDB_GENERIC_DTD);
    334  1.1  riastrad 	if (!generic_dtd)
    335  1.1  riastrad 		return;
    336  1.1  riastrad 
    337  1.1  riastrad 	if (generic_dtd->gdtd_size < sizeof(struct generic_dtd_entry)) {
    338  1.1  riastrad 		DRM_ERROR("GDTD size %u is too small.\n",
    339  1.1  riastrad 			  generic_dtd->gdtd_size);
    340  1.1  riastrad 		return;
    341  1.1  riastrad 	} else if (generic_dtd->gdtd_size !=
    342  1.1  riastrad 		   sizeof(struct generic_dtd_entry)) {
    343  1.1  riastrad 		DRM_ERROR("Unexpected GDTD size %u\n", generic_dtd->gdtd_size);
    344  1.1  riastrad 		/* DTD has unknown fields, but keep going */
    345  1.1  riastrad 	}
    346  1.1  riastrad 
    347  1.1  riastrad 	num_dtd = (get_blocksize(generic_dtd) -
    348  1.1  riastrad 		   sizeof(struct bdb_generic_dtd)) / generic_dtd->gdtd_size;
    349  1.1  riastrad 	if (dev_priv->vbt.panel_type >= num_dtd) {
    350  1.1  riastrad 		DRM_ERROR("Panel type %d not found in table of %d DTD's\n",
    351  1.1  riastrad 			  dev_priv->vbt.panel_type, num_dtd);
    352  1.1  riastrad 		return;
    353  1.1  riastrad 	}
    354  1.1  riastrad 
    355  1.1  riastrad 	dtd = &generic_dtd->dtd[dev_priv->vbt.panel_type];
    356  1.1  riastrad 
    357  1.1  riastrad 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
    358  1.1  riastrad 	if (!panel_fixed_mode)
    359  1.1  riastrad 		return;
    360  1.1  riastrad 
    361  1.1  riastrad 	panel_fixed_mode->hdisplay = dtd->hactive;
    362  1.1  riastrad 	panel_fixed_mode->hsync_start =
    363  1.1  riastrad 		panel_fixed_mode->hdisplay + dtd->hfront_porch;
    364  1.1  riastrad 	panel_fixed_mode->hsync_end =
    365  1.1  riastrad 		panel_fixed_mode->hsync_start + dtd->hsync;
    366  1.1  riastrad 	panel_fixed_mode->htotal =
    367  1.1  riastrad 		panel_fixed_mode->hdisplay + dtd->hblank;
    368  1.1  riastrad 
    369  1.1  riastrad 	panel_fixed_mode->vdisplay = dtd->vactive;
    370  1.1  riastrad 	panel_fixed_mode->vsync_start =
    371  1.1  riastrad 		panel_fixed_mode->vdisplay + dtd->vfront_porch;
    372  1.1  riastrad 	panel_fixed_mode->vsync_end =
    373  1.1  riastrad 		panel_fixed_mode->vsync_start + dtd->vsync;
    374  1.1  riastrad 	panel_fixed_mode->vtotal =
    375  1.1  riastrad 		panel_fixed_mode->vdisplay + dtd->vblank;
    376  1.1  riastrad 
    377  1.1  riastrad 	panel_fixed_mode->clock = dtd->pixel_clock;
    378  1.1  riastrad 	panel_fixed_mode->width_mm = dtd->width_mm;
    379  1.1  riastrad 	panel_fixed_mode->height_mm = dtd->height_mm;
    380  1.1  riastrad 
    381  1.1  riastrad 	panel_fixed_mode->type = DRM_MODE_TYPE_PREFERRED;
    382  1.1  riastrad 	drm_mode_set_name(panel_fixed_mode);
    383  1.1  riastrad 
    384  1.1  riastrad 	if (dtd->hsync_positive_polarity)
    385  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PHSYNC;
    386  1.1  riastrad 	else
    387  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NHSYNC;
    388  1.1  riastrad 
    389  1.1  riastrad 	if (dtd->vsync_positive_polarity)
    390  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_PVSYNC;
    391  1.1  riastrad 	else
    392  1.1  riastrad 		panel_fixed_mode->flags |= DRM_MODE_FLAG_NVSYNC;
    393  1.1  riastrad 
    394  1.1  riastrad 	DRM_DEBUG_KMS("Found panel mode in BIOS VBT generic dtd table:\n");
    395  1.1  riastrad 	drm_mode_debug_printmodeline(panel_fixed_mode);
    396  1.1  riastrad 
    397  1.1  riastrad 	dev_priv->vbt.lfp_lvds_vbt_mode = panel_fixed_mode;
    398  1.1  riastrad }
    399  1.1  riastrad 
    400  1.1  riastrad static void
    401  1.1  riastrad parse_panel_dtd(struct drm_i915_private *dev_priv,
    402  1.1  riastrad 		const struct bdb_header *bdb)
    403  1.1  riastrad {
    404  1.1  riastrad 	/*
    405  1.1  riastrad 	 * Older VBTs provided provided DTD information for internal displays
    406  1.1  riastrad 	 * through the "LFP panel DTD" block (42).  As of VBT revision 229,
    407  1.1  riastrad 	 * that block is now deprecated and DTD information should be provided
    408  1.1  riastrad 	 * via a newer "generic DTD" block (58).  Just to be safe, we'll
    409  1.1  riastrad 	 * try the new generic DTD block first on VBT >= 229, but still fall
    410  1.1  riastrad 	 * back to trying the old LFP block if that fails.
    411  1.1  riastrad 	 */
    412  1.1  riastrad 	if (bdb->version >= 229)
    413  1.1  riastrad 		parse_generic_dtd(dev_priv, bdb);
    414  1.1  riastrad 	if (!dev_priv->vbt.lfp_lvds_vbt_mode)
    415  1.1  riastrad 		parse_lfp_panel_dtd(dev_priv, bdb);
    416  1.1  riastrad }
    417  1.1  riastrad 
    418  1.1  riastrad static void
    419  1.1  riastrad parse_lfp_backlight(struct drm_i915_private *dev_priv,
    420  1.1  riastrad 		    const struct bdb_header *bdb)
    421  1.1  riastrad {
    422  1.1  riastrad 	const struct bdb_lfp_backlight_data *backlight_data;
    423  1.1  riastrad 	const struct lfp_backlight_data_entry *entry;
    424  1.1  riastrad 	int panel_type = dev_priv->vbt.panel_type;
    425  1.1  riastrad 
    426  1.1  riastrad 	backlight_data = find_section(bdb, BDB_LVDS_BACKLIGHT);
    427  1.1  riastrad 	if (!backlight_data)
    428  1.1  riastrad 		return;
    429  1.1  riastrad 
    430  1.1  riastrad 	if (backlight_data->entry_size != sizeof(backlight_data->data[0])) {
    431  1.1  riastrad 		DRM_DEBUG_KMS("Unsupported backlight data entry size %u\n",
    432  1.1  riastrad 			      backlight_data->entry_size);
    433  1.1  riastrad 		return;
    434  1.1  riastrad 	}
    435  1.1  riastrad 
    436  1.1  riastrad 	entry = &backlight_data->data[panel_type];
    437  1.1  riastrad 
    438  1.1  riastrad 	dev_priv->vbt.backlight.present = entry->type == BDB_BACKLIGHT_TYPE_PWM;
    439  1.1  riastrad 	if (!dev_priv->vbt.backlight.present) {
    440  1.1  riastrad 		DRM_DEBUG_KMS("PWM backlight not present in VBT (type %u)\n",
    441  1.1  riastrad 			      entry->type);
    442  1.1  riastrad 		return;
    443  1.1  riastrad 	}
    444  1.1  riastrad 
    445  1.1  riastrad 	dev_priv->vbt.backlight.type = INTEL_BACKLIGHT_DISPLAY_DDI;
    446  1.1  riastrad 	if (bdb->version >= 191 &&
    447  1.1  riastrad 	    get_blocksize(backlight_data) >= sizeof(*backlight_data)) {
    448  1.1  riastrad 		const struct lfp_backlight_control_method *method;
    449  1.1  riastrad 
    450  1.1  riastrad 		method = &backlight_data->backlight_control[panel_type];
    451  1.1  riastrad 		dev_priv->vbt.backlight.type = method->type;
    452  1.1  riastrad 		dev_priv->vbt.backlight.controller = method->controller;
    453  1.1  riastrad 	}
    454  1.1  riastrad 
    455  1.1  riastrad 	dev_priv->vbt.backlight.pwm_freq_hz = entry->pwm_freq_hz;
    456  1.1  riastrad 	dev_priv->vbt.backlight.active_low_pwm = entry->active_low_pwm;
    457  1.1  riastrad 	dev_priv->vbt.backlight.min_brightness = entry->min_brightness;
    458  1.1  riastrad 	DRM_DEBUG_KMS("VBT backlight PWM modulation frequency %u Hz, "
    459  1.1  riastrad 		      "active %s, min brightness %u, level %u, controller %u\n",
    460  1.1  riastrad 		      dev_priv->vbt.backlight.pwm_freq_hz,
    461  1.1  riastrad 		      dev_priv->vbt.backlight.active_low_pwm ? "low" : "high",
    462  1.1  riastrad 		      dev_priv->vbt.backlight.min_brightness,
    463  1.1  riastrad 		      backlight_data->level[panel_type],
    464  1.1  riastrad 		      dev_priv->vbt.backlight.controller);
    465  1.1  riastrad }
    466  1.1  riastrad 
    467  1.1  riastrad /* Try to find sdvo panel data */
    468  1.1  riastrad static void
    469  1.1  riastrad parse_sdvo_panel_data(struct drm_i915_private *dev_priv,
    470  1.1  riastrad 		      const struct bdb_header *bdb)
    471  1.1  riastrad {
    472  1.1  riastrad 	const struct bdb_sdvo_panel_dtds *dtds;
    473  1.1  riastrad 	struct drm_display_mode *panel_fixed_mode;
    474  1.1  riastrad 	int index;
    475  1.1  riastrad 
    476  1.1  riastrad 	index = i915_modparams.vbt_sdvo_panel_type;
    477  1.1  riastrad 	if (index == -2) {
    478  1.1  riastrad 		DRM_DEBUG_KMS("Ignore SDVO panel mode from BIOS VBT tables.\n");
    479  1.1  riastrad 		return;
    480  1.1  riastrad 	}
    481  1.1  riastrad 
    482  1.1  riastrad 	if (index == -1) {
    483  1.1  riastrad 		const struct bdb_sdvo_lvds_options *sdvo_lvds_options;
    484  1.1  riastrad 
    485  1.1  riastrad 		sdvo_lvds_options = find_section(bdb, BDB_SDVO_LVDS_OPTIONS);
    486  1.1  riastrad 		if (!sdvo_lvds_options)
    487  1.1  riastrad 			return;
    488  1.1  riastrad 
    489  1.1  riastrad 		index = sdvo_lvds_options->panel_type;
    490  1.1  riastrad 	}
    491  1.1  riastrad 
    492  1.1  riastrad 	dtds = find_section(bdb, BDB_SDVO_PANEL_DTDS);
    493  1.1  riastrad 	if (!dtds)
    494  1.1  riastrad 		return;
    495  1.1  riastrad 
    496  1.1  riastrad 	panel_fixed_mode = kzalloc(sizeof(*panel_fixed_mode), GFP_KERNEL);
    497  1.1  riastrad 	if (!panel_fixed_mode)
    498  1.1  riastrad 		return;
    499  1.1  riastrad 
    500  1.1  riastrad 	fill_detail_timing_data(panel_fixed_mode, &dtds->dtds[index]);
    501  1.1  riastrad 
    502  1.1  riastrad 	dev_priv->vbt.sdvo_lvds_vbt_mode = panel_fixed_mode;
    503  1.1  riastrad 
    504  1.1  riastrad 	DRM_DEBUG_KMS("Found SDVO panel mode in BIOS VBT tables:\n");
    505  1.1  riastrad 	drm_mode_debug_printmodeline(panel_fixed_mode);
    506  1.1  riastrad }
    507  1.1  riastrad 
    508  1.1  riastrad static int intel_bios_ssc_frequency(struct drm_i915_private *dev_priv,
    509  1.1  riastrad 				    bool alternate)
    510  1.1  riastrad {
    511  1.1  riastrad 	switch (INTEL_GEN(dev_priv)) {
    512  1.1  riastrad 	case 2:
    513  1.1  riastrad 		return alternate ? 66667 : 48000;
    514  1.1  riastrad 	case 3:
    515  1.1  riastrad 	case 4:
    516  1.1  riastrad 		return alternate ? 100000 : 96000;
    517  1.1  riastrad 	default:
    518  1.1  riastrad 		return alternate ? 100000 : 120000;
    519  1.1  riastrad 	}
    520  1.1  riastrad }
    521  1.1  riastrad 
    522  1.1  riastrad static void
    523  1.1  riastrad parse_general_features(struct drm_i915_private *dev_priv,
    524  1.1  riastrad 		       const struct bdb_header *bdb)
    525  1.1  riastrad {
    526  1.1  riastrad 	const struct bdb_general_features *general;
    527  1.1  riastrad 
    528  1.1  riastrad 	general = find_section(bdb, BDB_GENERAL_FEATURES);
    529  1.1  riastrad 	if (!general)
    530  1.1  riastrad 		return;
    531  1.1  riastrad 
    532  1.1  riastrad 	dev_priv->vbt.int_tv_support = general->int_tv_support;
    533  1.1  riastrad 	/* int_crt_support can't be trusted on earlier platforms */
    534  1.1  riastrad 	if (bdb->version >= 155 &&
    535  1.1  riastrad 	    (HAS_DDI(dev_priv) || IS_VALLEYVIEW(dev_priv)))
    536  1.1  riastrad 		dev_priv->vbt.int_crt_support = general->int_crt_support;
    537  1.1  riastrad 	dev_priv->vbt.lvds_use_ssc = general->enable_ssc;
    538  1.1  riastrad 	dev_priv->vbt.lvds_ssc_freq =
    539  1.1  riastrad 		intel_bios_ssc_frequency(dev_priv, general->ssc_freq);
    540  1.1  riastrad 	dev_priv->vbt.display_clock_mode = general->display_clock_mode;
    541  1.1  riastrad 	dev_priv->vbt.fdi_rx_polarity_inverted = general->fdi_rx_polarity_inverted;
    542  1.1  riastrad 	if (bdb->version >= 181) {
    543  1.1  riastrad 		dev_priv->vbt.orientation = general->rotate_180 ?
    544  1.1  riastrad 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP :
    545  1.1  riastrad 			DRM_MODE_PANEL_ORIENTATION_NORMAL;
    546  1.1  riastrad 	} else {
    547  1.1  riastrad 		dev_priv->vbt.orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
    548  1.1  riastrad 	}
    549  1.1  riastrad 	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",
    550  1.1  riastrad 		      dev_priv->vbt.int_tv_support,
    551  1.1  riastrad 		      dev_priv->vbt.int_crt_support,
    552  1.1  riastrad 		      dev_priv->vbt.lvds_use_ssc,
    553  1.1  riastrad 		      dev_priv->vbt.lvds_ssc_freq,
    554  1.1  riastrad 		      dev_priv->vbt.display_clock_mode,
    555  1.1  riastrad 		      dev_priv->vbt.fdi_rx_polarity_inverted);
    556  1.1  riastrad }
    557  1.1  riastrad 
    558  1.1  riastrad static const struct child_device_config *
    559  1.1  riastrad child_device_ptr(const struct bdb_general_definitions *defs, int i)
    560  1.1  riastrad {
    561  1.1  riastrad 	return (const void *) &defs->devices[i * defs->child_dev_size];
    562  1.1  riastrad }
    563  1.1  riastrad 
    564  1.1  riastrad static void
    565  1.1  riastrad parse_sdvo_device_mapping(struct drm_i915_private *dev_priv, u8 bdb_version)
    566  1.1  riastrad {
    567  1.1  riastrad 	struct sdvo_device_mapping *mapping;
    568  1.1  riastrad 	const struct display_device_data *devdata;
    569  1.1  riastrad 	const struct child_device_config *child;
    570  1.1  riastrad 	int count = 0;
    571  1.1  riastrad 
    572  1.1  riastrad 	/*
    573  1.1  riastrad 	 * Only parse SDVO mappings on gens that could have SDVO. This isn't
    574  1.1  riastrad 	 * accurate and doesn't have to be, as long as it's not too strict.
    575  1.1  riastrad 	 */
    576  1.1  riastrad 	if (!IS_GEN_RANGE(dev_priv, 3, 7)) {
    577  1.1  riastrad 		DRM_DEBUG_KMS("Skipping SDVO device mapping\n");
    578  1.1  riastrad 		return;
    579  1.1  riastrad 	}
    580  1.1  riastrad 
    581  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node) {
    582  1.1  riastrad 		child = &devdata->child;
    583  1.1  riastrad 
    584  1.1  riastrad 		if (child->slave_addr != SLAVE_ADDR1 &&
    585  1.1  riastrad 		    child->slave_addr != SLAVE_ADDR2) {
    586  1.1  riastrad 			/*
    587  1.1  riastrad 			 * If the slave address is neither 0x70 nor 0x72,
    588  1.1  riastrad 			 * it is not a SDVO device. Skip it.
    589  1.1  riastrad 			 */
    590  1.1  riastrad 			continue;
    591  1.1  riastrad 		}
    592  1.1  riastrad 		if (child->dvo_port != DEVICE_PORT_DVOB &&
    593  1.1  riastrad 		    child->dvo_port != DEVICE_PORT_DVOC) {
    594  1.1  riastrad 			/* skip the incorrect SDVO port */
    595  1.1  riastrad 			DRM_DEBUG_KMS("Incorrect SDVO port. Skip it\n");
    596  1.1  riastrad 			continue;
    597  1.1  riastrad 		}
    598  1.1  riastrad 		DRM_DEBUG_KMS("the SDVO device with slave addr %2x is found on"
    599  1.1  riastrad 			      " %s port\n",
    600  1.1  riastrad 			      child->slave_addr,
    601  1.1  riastrad 			      (child->dvo_port == DEVICE_PORT_DVOB) ?
    602  1.1  riastrad 			      "SDVOB" : "SDVOC");
    603  1.1  riastrad 		mapping = &dev_priv->vbt.sdvo_mappings[child->dvo_port - 1];
    604  1.1  riastrad 		if (!mapping->initialized) {
    605  1.1  riastrad 			mapping->dvo_port = child->dvo_port;
    606  1.1  riastrad 			mapping->slave_addr = child->slave_addr;
    607  1.1  riastrad 			mapping->dvo_wiring = child->dvo_wiring;
    608  1.1  riastrad 			mapping->ddc_pin = child->ddc_pin;
    609  1.1  riastrad 			mapping->i2c_pin = child->i2c_pin;
    610  1.1  riastrad 			mapping->initialized = 1;
    611  1.1  riastrad 			DRM_DEBUG_KMS("SDVO device: dvo=%x, addr=%x, wiring=%d, ddc_pin=%d, i2c_pin=%d\n",
    612  1.1  riastrad 				      mapping->dvo_port,
    613  1.1  riastrad 				      mapping->slave_addr,
    614  1.1  riastrad 				      mapping->dvo_wiring,
    615  1.1  riastrad 				      mapping->ddc_pin,
    616  1.1  riastrad 				      mapping->i2c_pin);
    617  1.1  riastrad 		} else {
    618  1.1  riastrad 			DRM_DEBUG_KMS("Maybe one SDVO port is shared by "
    619  1.1  riastrad 					 "two SDVO device.\n");
    620  1.1  riastrad 		}
    621  1.1  riastrad 		if (child->slave2_addr) {
    622  1.1  riastrad 			/* Maybe this is a SDVO device with multiple inputs */
    623  1.1  riastrad 			/* And the mapping info is not added */
    624  1.1  riastrad 			DRM_DEBUG_KMS("there exists the slave2_addr. Maybe this"
    625  1.1  riastrad 				" is a SDVO device with multiple inputs.\n");
    626  1.1  riastrad 		}
    627  1.1  riastrad 		count++;
    628  1.1  riastrad 	}
    629  1.1  riastrad 
    630  1.1  riastrad 	if (!count) {
    631  1.1  riastrad 		/* No SDVO device info is found */
    632  1.1  riastrad 		DRM_DEBUG_KMS("No SDVO device info is found in VBT\n");
    633  1.1  riastrad 	}
    634  1.1  riastrad }
    635  1.1  riastrad 
    636  1.1  riastrad static void
    637  1.1  riastrad parse_driver_features(struct drm_i915_private *dev_priv,
    638  1.1  riastrad 		      const struct bdb_header *bdb)
    639  1.1  riastrad {
    640  1.1  riastrad 	const struct bdb_driver_features *driver;
    641  1.1  riastrad 
    642  1.1  riastrad 	driver = find_section(bdb, BDB_DRIVER_FEATURES);
    643  1.1  riastrad 	if (!driver)
    644  1.1  riastrad 		return;
    645  1.1  riastrad 
    646  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 5) {
    647  1.1  riastrad 		/*
    648  1.1  riastrad 		 * Note that we consider BDB_DRIVER_FEATURE_INT_SDVO_LVDS
    649  1.1  riastrad 		 * to mean "eDP". The VBT spec doesn't agree with that
    650  1.1  riastrad 		 * interpretation, but real world VBTs seem to.
    651  1.1  riastrad 		 */
    652  1.1  riastrad 		if (driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS)
    653  1.1  riastrad 			dev_priv->vbt.int_lvds_support = 0;
    654  1.1  riastrad 	} else {
    655  1.1  riastrad 		/*
    656  1.1  riastrad 		 * FIXME it's not clear which BDB version has the LVDS config
    657  1.1  riastrad 		 * bits defined. Revision history in the VBT spec says:
    658  1.1  riastrad 		 * "0.92 | Add two definitions for VBT value of LVDS Active
    659  1.1  riastrad 		 *  Config (00b and 11b values defined) | 06/13/2005"
    660  1.1  riastrad 		 * but does not the specify the BDB version.
    661  1.1  riastrad 		 *
    662  1.1  riastrad 		 * So far version 134 (on i945gm) is the oldest VBT observed
    663  1.1  riastrad 		 * in the wild with the bits correctly populated. Version
    664  1.1  riastrad 		 * 108 (on i85x) does not have the bits correctly populated.
    665  1.1  riastrad 		 */
    666  1.1  riastrad 		if (bdb->version >= 134 &&
    667  1.1  riastrad 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_LVDS &&
    668  1.1  riastrad 		    driver->lvds_config != BDB_DRIVER_FEATURE_INT_SDVO_LVDS)
    669  1.1  riastrad 			dev_priv->vbt.int_lvds_support = 0;
    670  1.1  riastrad 	}
    671  1.1  riastrad 
    672  1.1  riastrad 	if (bdb->version < 228) {
    673  1.1  riastrad 		DRM_DEBUG_KMS("DRRS State Enabled:%d\n", driver->drrs_enabled);
    674  1.1  riastrad 		/*
    675  1.1  riastrad 		 * If DRRS is not supported, drrs_type has to be set to 0.
    676  1.1  riastrad 		 * This is because, VBT is configured in such a way that
    677  1.1  riastrad 		 * static DRRS is 0 and DRRS not supported is represented by
    678  1.1  riastrad 		 * driver->drrs_enabled=false
    679  1.1  riastrad 		 */
    680  1.1  riastrad 		if (!driver->drrs_enabled)
    681  1.1  riastrad 			dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
    682  1.1  riastrad 
    683  1.1  riastrad 		dev_priv->vbt.psr.enable = driver->psr_enabled;
    684  1.1  riastrad 	}
    685  1.1  riastrad }
    686  1.1  riastrad 
    687  1.1  riastrad static void
    688  1.1  riastrad parse_power_conservation_features(struct drm_i915_private *dev_priv,
    689  1.1  riastrad 				  const struct bdb_header *bdb)
    690  1.1  riastrad {
    691  1.1  riastrad 	const struct bdb_lfp_power *power;
    692  1.1  riastrad 	u8 panel_type = dev_priv->vbt.panel_type;
    693  1.1  riastrad 
    694  1.1  riastrad 	if (bdb->version < 228)
    695  1.1  riastrad 		return;
    696  1.1  riastrad 
    697  1.1  riastrad 	power = find_section(bdb, BDB_LVDS_POWER);
    698  1.1  riastrad 	if (!power)
    699  1.1  riastrad 		return;
    700  1.1  riastrad 
    701  1.1  riastrad 	dev_priv->vbt.psr.enable = power->psr & BIT(panel_type);
    702  1.1  riastrad 
    703  1.1  riastrad 	/*
    704  1.1  riastrad 	 * If DRRS is not supported, drrs_type has to be set to 0.
    705  1.1  riastrad 	 * This is because, VBT is configured in such a way that
    706  1.1  riastrad 	 * static DRRS is 0 and DRRS not supported is represented by
    707  1.1  riastrad 	 * power->drrs & BIT(panel_type)=false
    708  1.1  riastrad 	 */
    709  1.1  riastrad 	if (!(power->drrs & BIT(panel_type)))
    710  1.1  riastrad 		dev_priv->vbt.drrs_type = DRRS_NOT_SUPPORTED;
    711  1.1  riastrad }
    712  1.1  riastrad 
    713  1.1  riastrad static void
    714  1.1  riastrad parse_edp(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
    715  1.1  riastrad {
    716  1.1  riastrad 	const struct bdb_edp *edp;
    717  1.1  riastrad 	const struct edp_power_seq *edp_pps;
    718  1.1  riastrad 	const struct edp_fast_link_params *edp_link_params;
    719  1.1  riastrad 	int panel_type = dev_priv->vbt.panel_type;
    720  1.1  riastrad 
    721  1.1  riastrad 	edp = find_section(bdb, BDB_EDP);
    722  1.1  riastrad 	if (!edp)
    723  1.1  riastrad 		return;
    724  1.1  riastrad 
    725  1.1  riastrad 	switch ((edp->color_depth >> (panel_type * 2)) & 3) {
    726  1.1  riastrad 	case EDP_18BPP:
    727  1.1  riastrad 		dev_priv->vbt.edp.bpp = 18;
    728  1.1  riastrad 		break;
    729  1.1  riastrad 	case EDP_24BPP:
    730  1.1  riastrad 		dev_priv->vbt.edp.bpp = 24;
    731  1.1  riastrad 		break;
    732  1.1  riastrad 	case EDP_30BPP:
    733  1.1  riastrad 		dev_priv->vbt.edp.bpp = 30;
    734  1.1  riastrad 		break;
    735  1.1  riastrad 	}
    736  1.1  riastrad 
    737  1.1  riastrad 	/* Get the eDP sequencing and link info */
    738  1.1  riastrad 	edp_pps = &edp->power_seqs[panel_type];
    739  1.1  riastrad 	edp_link_params = &edp->fast_link_params[panel_type];
    740  1.1  riastrad 
    741  1.1  riastrad 	dev_priv->vbt.edp.pps = *edp_pps;
    742  1.1  riastrad 
    743  1.1  riastrad 	switch (edp_link_params->rate) {
    744  1.1  riastrad 	case EDP_RATE_1_62:
    745  1.1  riastrad 		dev_priv->vbt.edp.rate = DP_LINK_BW_1_62;
    746  1.1  riastrad 		break;
    747  1.1  riastrad 	case EDP_RATE_2_7:
    748  1.1  riastrad 		dev_priv->vbt.edp.rate = DP_LINK_BW_2_7;
    749  1.1  riastrad 		break;
    750  1.1  riastrad 	default:
    751  1.1  riastrad 		DRM_DEBUG_KMS("VBT has unknown eDP link rate value %u\n",
    752  1.1  riastrad 			      edp_link_params->rate);
    753  1.1  riastrad 		break;
    754  1.1  riastrad 	}
    755  1.1  riastrad 
    756  1.1  riastrad 	switch (edp_link_params->lanes) {
    757  1.1  riastrad 	case EDP_LANE_1:
    758  1.1  riastrad 		dev_priv->vbt.edp.lanes = 1;
    759  1.1  riastrad 		break;
    760  1.1  riastrad 	case EDP_LANE_2:
    761  1.1  riastrad 		dev_priv->vbt.edp.lanes = 2;
    762  1.1  riastrad 		break;
    763  1.1  riastrad 	case EDP_LANE_4:
    764  1.1  riastrad 		dev_priv->vbt.edp.lanes = 4;
    765  1.1  riastrad 		break;
    766  1.1  riastrad 	default:
    767  1.1  riastrad 		DRM_DEBUG_KMS("VBT has unknown eDP lane count value %u\n",
    768  1.1  riastrad 			      edp_link_params->lanes);
    769  1.1  riastrad 		break;
    770  1.1  riastrad 	}
    771  1.1  riastrad 
    772  1.1  riastrad 	switch (edp_link_params->preemphasis) {
    773  1.1  riastrad 	case EDP_PREEMPHASIS_NONE:
    774  1.1  riastrad 		dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_0;
    775  1.1  riastrad 		break;
    776  1.1  riastrad 	case EDP_PREEMPHASIS_3_5dB:
    777  1.1  riastrad 		dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_1;
    778  1.1  riastrad 		break;
    779  1.1  riastrad 	case EDP_PREEMPHASIS_6dB:
    780  1.1  riastrad 		dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_2;
    781  1.1  riastrad 		break;
    782  1.1  riastrad 	case EDP_PREEMPHASIS_9_5dB:
    783  1.1  riastrad 		dev_priv->vbt.edp.preemphasis = DP_TRAIN_PRE_EMPH_LEVEL_3;
    784  1.1  riastrad 		break;
    785  1.1  riastrad 	default:
    786  1.1  riastrad 		DRM_DEBUG_KMS("VBT has unknown eDP pre-emphasis value %u\n",
    787  1.1  riastrad 			      edp_link_params->preemphasis);
    788  1.1  riastrad 		break;
    789  1.1  riastrad 	}
    790  1.1  riastrad 
    791  1.1  riastrad 	switch (edp_link_params->vswing) {
    792  1.1  riastrad 	case EDP_VSWING_0_4V:
    793  1.1  riastrad 		dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_0;
    794  1.1  riastrad 		break;
    795  1.1  riastrad 	case EDP_VSWING_0_6V:
    796  1.1  riastrad 		dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_1;
    797  1.1  riastrad 		break;
    798  1.1  riastrad 	case EDP_VSWING_0_8V:
    799  1.1  riastrad 		dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_2;
    800  1.1  riastrad 		break;
    801  1.1  riastrad 	case EDP_VSWING_1_2V:
    802  1.1  riastrad 		dev_priv->vbt.edp.vswing = DP_TRAIN_VOLTAGE_SWING_LEVEL_3;
    803  1.1  riastrad 		break;
    804  1.1  riastrad 	default:
    805  1.1  riastrad 		DRM_DEBUG_KMS("VBT has unknown eDP voltage swing value %u\n",
    806  1.1  riastrad 			      edp_link_params->vswing);
    807  1.1  riastrad 		break;
    808  1.1  riastrad 	}
    809  1.1  riastrad 
    810  1.1  riastrad 	if (bdb->version >= 173) {
    811  1.1  riastrad 		u8 vswing;
    812  1.1  riastrad 
    813  1.1  riastrad 		/* Don't read from VBT if module parameter has valid value*/
    814  1.1  riastrad 		if (i915_modparams.edp_vswing) {
    815  1.1  riastrad 			dev_priv->vbt.edp.low_vswing =
    816  1.1  riastrad 				i915_modparams.edp_vswing == 1;
    817  1.1  riastrad 		} else {
    818  1.1  riastrad 			vswing = (edp->edp_vswing_preemph >> (panel_type * 4)) & 0xF;
    819  1.1  riastrad 			dev_priv->vbt.edp.low_vswing = vswing == 0;
    820  1.1  riastrad 		}
    821  1.1  riastrad 	}
    822  1.1  riastrad }
    823  1.1  riastrad 
    824  1.1  riastrad static void
    825  1.1  riastrad parse_psr(struct drm_i915_private *dev_priv, const struct bdb_header *bdb)
    826  1.1  riastrad {
    827  1.1  riastrad 	const struct bdb_psr *psr;
    828  1.1  riastrad 	const struct psr_table *psr_table;
    829  1.1  riastrad 	int panel_type = dev_priv->vbt.panel_type;
    830  1.1  riastrad 
    831  1.1  riastrad 	psr = find_section(bdb, BDB_PSR);
    832  1.1  riastrad 	if (!psr) {
    833  1.1  riastrad 		DRM_DEBUG_KMS("No PSR BDB found.\n");
    834  1.1  riastrad 		return;
    835  1.1  riastrad 	}
    836  1.1  riastrad 
    837  1.1  riastrad 	psr_table = &psr->psr_table[panel_type];
    838  1.1  riastrad 
    839  1.1  riastrad 	dev_priv->vbt.psr.full_link = psr_table->full_link;
    840  1.1  riastrad 	dev_priv->vbt.psr.require_aux_wakeup = psr_table->require_aux_to_wakeup;
    841  1.1  riastrad 
    842  1.1  riastrad 	/* Allowed VBT values goes from 0 to 15 */
    843  1.1  riastrad 	dev_priv->vbt.psr.idle_frames = psr_table->idle_frames < 0 ? 0 :
    844  1.1  riastrad 		psr_table->idle_frames > 15 ? 15 : psr_table->idle_frames;
    845  1.1  riastrad 
    846  1.1  riastrad 	switch (psr_table->lines_to_wait) {
    847  1.1  riastrad 	case 0:
    848  1.1  riastrad 		dev_priv->vbt.psr.lines_to_wait = PSR_0_LINES_TO_WAIT;
    849  1.1  riastrad 		break;
    850  1.1  riastrad 	case 1:
    851  1.1  riastrad 		dev_priv->vbt.psr.lines_to_wait = PSR_1_LINE_TO_WAIT;
    852  1.1  riastrad 		break;
    853  1.1  riastrad 	case 2:
    854  1.1  riastrad 		dev_priv->vbt.psr.lines_to_wait = PSR_4_LINES_TO_WAIT;
    855  1.1  riastrad 		break;
    856  1.1  riastrad 	case 3:
    857  1.1  riastrad 		dev_priv->vbt.psr.lines_to_wait = PSR_8_LINES_TO_WAIT;
    858  1.1  riastrad 		break;
    859  1.1  riastrad 	default:
    860  1.1  riastrad 		DRM_DEBUG_KMS("VBT has unknown PSR lines to wait %u\n",
    861  1.1  riastrad 			      psr_table->lines_to_wait);
    862  1.1  riastrad 		break;
    863  1.1  riastrad 	}
    864  1.1  riastrad 
    865  1.1  riastrad 	/*
    866  1.1  riastrad 	 * New psr options 0=500us, 1=100us, 2=2500us, 3=0us
    867  1.1  riastrad 	 * Old decimal value is wake up time in multiples of 100 us.
    868  1.1  riastrad 	 */
    869  1.1  riastrad 	if (bdb->version >= 205 &&
    870  1.1  riastrad 	    (IS_GEN9_BC(dev_priv) || IS_GEMINILAKE(dev_priv) ||
    871  1.1  riastrad 	     INTEL_GEN(dev_priv) >= 10)) {
    872  1.1  riastrad 		switch (psr_table->tp1_wakeup_time) {
    873  1.1  riastrad 		case 0:
    874  1.1  riastrad 			dev_priv->vbt.psr.tp1_wakeup_time_us = 500;
    875  1.1  riastrad 			break;
    876  1.1  riastrad 		case 1:
    877  1.1  riastrad 			dev_priv->vbt.psr.tp1_wakeup_time_us = 100;
    878  1.1  riastrad 			break;
    879  1.1  riastrad 		case 3:
    880  1.1  riastrad 			dev_priv->vbt.psr.tp1_wakeup_time_us = 0;
    881  1.1  riastrad 			break;
    882  1.1  riastrad 		default:
    883  1.1  riastrad 			DRM_DEBUG_KMS("VBT tp1 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
    884  1.1  riastrad 					psr_table->tp1_wakeup_time);
    885  1.1  riastrad 			/* fallthrough */
    886  1.1  riastrad 		case 2:
    887  1.1  riastrad 			dev_priv->vbt.psr.tp1_wakeup_time_us = 2500;
    888  1.1  riastrad 			break;
    889  1.1  riastrad 		}
    890  1.1  riastrad 
    891  1.1  riastrad 		switch (psr_table->tp2_tp3_wakeup_time) {
    892  1.1  riastrad 		case 0:
    893  1.1  riastrad 			dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = 500;
    894  1.1  riastrad 			break;
    895  1.1  riastrad 		case 1:
    896  1.1  riastrad 			dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = 100;
    897  1.1  riastrad 			break;
    898  1.1  riastrad 		case 3:
    899  1.1  riastrad 			dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = 0;
    900  1.1  riastrad 			break;
    901  1.1  riastrad 		default:
    902  1.1  riastrad 			DRM_DEBUG_KMS("VBT tp2_tp3 wakeup time value %d is outside range[0-3], defaulting to max value 2500us\n",
    903  1.1  riastrad 					psr_table->tp2_tp3_wakeup_time);
    904  1.1  riastrad 			/* fallthrough */
    905  1.1  riastrad 		case 2:
    906  1.1  riastrad 			dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = 2500;
    907  1.1  riastrad 		break;
    908  1.1  riastrad 		}
    909  1.1  riastrad 	} else {
    910  1.1  riastrad 		dev_priv->vbt.psr.tp1_wakeup_time_us = psr_table->tp1_wakeup_time * 100;
    911  1.1  riastrad 		dev_priv->vbt.psr.tp2_tp3_wakeup_time_us = psr_table->tp2_tp3_wakeup_time * 100;
    912  1.1  riastrad 	}
    913  1.1  riastrad 
    914  1.1  riastrad 	if (bdb->version >= 226) {
    915  1.1  riastrad 		u32 wakeup_time = psr->psr2_tp2_tp3_wakeup_time;
    916  1.1  riastrad 
    917  1.1  riastrad 		wakeup_time = (wakeup_time >> (2 * panel_type)) & 0x3;
    918  1.1  riastrad 		switch (wakeup_time) {
    919  1.1  riastrad 		case 0:
    920  1.1  riastrad 			wakeup_time = 500;
    921  1.1  riastrad 			break;
    922  1.1  riastrad 		case 1:
    923  1.1  riastrad 			wakeup_time = 100;
    924  1.1  riastrad 			break;
    925  1.1  riastrad 		case 3:
    926  1.1  riastrad 			wakeup_time = 50;
    927  1.1  riastrad 			break;
    928  1.1  riastrad 		default:
    929  1.1  riastrad 		case 2:
    930  1.1  riastrad 			wakeup_time = 2500;
    931  1.1  riastrad 			break;
    932  1.1  riastrad 		}
    933  1.1  riastrad 		dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us = wakeup_time;
    934  1.1  riastrad 	} else {
    935  1.1  riastrad 		/* Reusing PSR1 wakeup time for PSR2 in older VBTs */
    936  1.1  riastrad 		dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us = dev_priv->vbt.psr.tp2_tp3_wakeup_time_us;
    937  1.1  riastrad 	}
    938  1.1  riastrad }
    939  1.1  riastrad 
    940  1.1  riastrad static void parse_dsi_backlight_ports(struct drm_i915_private *dev_priv,
    941  1.1  riastrad 				      u16 version, enum port port)
    942  1.1  riastrad {
    943  1.1  riastrad 	if (!dev_priv->vbt.dsi.config->dual_link || version < 197) {
    944  1.1  riastrad 		dev_priv->vbt.dsi.bl_ports = BIT(port);
    945  1.1  riastrad 		if (dev_priv->vbt.dsi.config->cabc_supported)
    946  1.1  riastrad 			dev_priv->vbt.dsi.cabc_ports = BIT(port);
    947  1.1  riastrad 
    948  1.1  riastrad 		return;
    949  1.1  riastrad 	}
    950  1.1  riastrad 
    951  1.1  riastrad 	switch (dev_priv->vbt.dsi.config->dl_dcs_backlight_ports) {
    952  1.1  riastrad 	case DL_DCS_PORT_A:
    953  1.1  riastrad 		dev_priv->vbt.dsi.bl_ports = BIT(PORT_A);
    954  1.1  riastrad 		break;
    955  1.1  riastrad 	case DL_DCS_PORT_C:
    956  1.1  riastrad 		dev_priv->vbt.dsi.bl_ports = BIT(PORT_C);
    957  1.1  riastrad 		break;
    958  1.1  riastrad 	default:
    959  1.1  riastrad 	case DL_DCS_PORT_A_AND_C:
    960  1.1  riastrad 		dev_priv->vbt.dsi.bl_ports = BIT(PORT_A) | BIT(PORT_C);
    961  1.1  riastrad 		break;
    962  1.1  riastrad 	}
    963  1.1  riastrad 
    964  1.1  riastrad 	if (!dev_priv->vbt.dsi.config->cabc_supported)
    965  1.1  riastrad 		return;
    966  1.1  riastrad 
    967  1.1  riastrad 	switch (dev_priv->vbt.dsi.config->dl_dcs_cabc_ports) {
    968  1.1  riastrad 	case DL_DCS_PORT_A:
    969  1.1  riastrad 		dev_priv->vbt.dsi.cabc_ports = BIT(PORT_A);
    970  1.1  riastrad 		break;
    971  1.1  riastrad 	case DL_DCS_PORT_C:
    972  1.1  riastrad 		dev_priv->vbt.dsi.cabc_ports = BIT(PORT_C);
    973  1.1  riastrad 		break;
    974  1.1  riastrad 	default:
    975  1.1  riastrad 	case DL_DCS_PORT_A_AND_C:
    976  1.1  riastrad 		dev_priv->vbt.dsi.cabc_ports =
    977  1.1  riastrad 					BIT(PORT_A) | BIT(PORT_C);
    978  1.1  riastrad 		break;
    979  1.1  riastrad 	}
    980  1.1  riastrad }
    981  1.1  riastrad 
    982  1.1  riastrad static void
    983  1.1  riastrad parse_mipi_config(struct drm_i915_private *dev_priv,
    984  1.1  riastrad 		  const struct bdb_header *bdb)
    985  1.1  riastrad {
    986  1.1  riastrad 	const struct bdb_mipi_config *start;
    987  1.1  riastrad 	const struct mipi_config *config;
    988  1.1  riastrad 	const struct mipi_pps_data *pps;
    989  1.1  riastrad 	int panel_type = dev_priv->vbt.panel_type;
    990  1.1  riastrad 	enum port port;
    991  1.1  riastrad 
    992  1.1  riastrad 	/* parse MIPI blocks only if LFP type is MIPI */
    993  1.1  riastrad 	if (!intel_bios_is_dsi_present(dev_priv, &port))
    994  1.1  riastrad 		return;
    995  1.1  riastrad 
    996  1.1  riastrad 	/* Initialize this to undefined indicating no generic MIPI support */
    997  1.1  riastrad 	dev_priv->vbt.dsi.panel_id = MIPI_DSI_UNDEFINED_PANEL_ID;
    998  1.1  riastrad 
    999  1.1  riastrad 	/* Block #40 is already parsed and panel_fixed_mode is
   1000  1.1  riastrad 	 * stored in dev_priv->lfp_lvds_vbt_mode
   1001  1.1  riastrad 	 * resuse this when needed
   1002  1.1  riastrad 	 */
   1003  1.1  riastrad 
   1004  1.1  riastrad 	/* Parse #52 for panel index used from panel_type already
   1005  1.1  riastrad 	 * parsed
   1006  1.1  riastrad 	 */
   1007  1.1  riastrad 	start = find_section(bdb, BDB_MIPI_CONFIG);
   1008  1.1  riastrad 	if (!start) {
   1009  1.1  riastrad 		DRM_DEBUG_KMS("No MIPI config BDB found");
   1010  1.1  riastrad 		return;
   1011  1.1  riastrad 	}
   1012  1.1  riastrad 
   1013  1.1  riastrad 	DRM_DEBUG_DRIVER("Found MIPI Config block, panel index = %d\n",
   1014  1.1  riastrad 								panel_type);
   1015  1.1  riastrad 
   1016  1.1  riastrad 	/*
   1017  1.1  riastrad 	 * get hold of the correct configuration block and pps data as per
   1018  1.1  riastrad 	 * the panel_type as index
   1019  1.1  riastrad 	 */
   1020  1.1  riastrad 	config = &start->config[panel_type];
   1021  1.1  riastrad 	pps = &start->pps[panel_type];
   1022  1.1  riastrad 
   1023  1.1  riastrad 	/* store as of now full data. Trim when we realise all is not needed */
   1024  1.1  riastrad 	dev_priv->vbt.dsi.config = kmemdup(config, sizeof(struct mipi_config), GFP_KERNEL);
   1025  1.1  riastrad 	if (!dev_priv->vbt.dsi.config)
   1026  1.1  riastrad 		return;
   1027  1.1  riastrad 
   1028  1.1  riastrad 	dev_priv->vbt.dsi.pps = kmemdup(pps, sizeof(struct mipi_pps_data), GFP_KERNEL);
   1029  1.1  riastrad 	if (!dev_priv->vbt.dsi.pps) {
   1030  1.1  riastrad 		kfree(dev_priv->vbt.dsi.config);
   1031  1.1  riastrad 		return;
   1032  1.1  riastrad 	}
   1033  1.1  riastrad 
   1034  1.1  riastrad 	parse_dsi_backlight_ports(dev_priv, bdb->version, port);
   1035  1.1  riastrad 
   1036  1.1  riastrad 	/* FIXME is the 90 vs. 270 correct? */
   1037  1.1  riastrad 	switch (config->rotation) {
   1038  1.1  riastrad 	case ENABLE_ROTATION_0:
   1039  1.1  riastrad 		/*
   1040  1.1  riastrad 		 * Most (all?) VBTs claim 0 degrees despite having
   1041  1.1  riastrad 		 * an upside down panel, thus we do not trust this.
   1042  1.1  riastrad 		 */
   1043  1.1  riastrad 		dev_priv->vbt.dsi.orientation =
   1044  1.1  riastrad 			DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
   1045  1.1  riastrad 		break;
   1046  1.1  riastrad 	case ENABLE_ROTATION_90:
   1047  1.1  riastrad 		dev_priv->vbt.dsi.orientation =
   1048  1.1  riastrad 			DRM_MODE_PANEL_ORIENTATION_RIGHT_UP;
   1049  1.1  riastrad 		break;
   1050  1.1  riastrad 	case ENABLE_ROTATION_180:
   1051  1.1  riastrad 		dev_priv->vbt.dsi.orientation =
   1052  1.1  riastrad 			DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
   1053  1.1  riastrad 		break;
   1054  1.1  riastrad 	case ENABLE_ROTATION_270:
   1055  1.1  riastrad 		dev_priv->vbt.dsi.orientation =
   1056  1.1  riastrad 			DRM_MODE_PANEL_ORIENTATION_LEFT_UP;
   1057  1.1  riastrad 		break;
   1058  1.1  riastrad 	}
   1059  1.1  riastrad 
   1060  1.1  riastrad 	/* We have mandatory mipi config blocks. Initialize as generic panel */
   1061  1.1  riastrad 	dev_priv->vbt.dsi.panel_id = MIPI_DSI_GENERIC_PANEL_ID;
   1062  1.1  riastrad }
   1063  1.1  riastrad 
   1064  1.1  riastrad /* Find the sequence block and size for the given panel. */
   1065  1.1  riastrad static const u8 *
   1066  1.1  riastrad find_panel_sequence_block(const struct bdb_mipi_sequence *sequence,
   1067  1.1  riastrad 			  u16 panel_id, u32 *seq_size)
   1068  1.1  riastrad {
   1069  1.1  riastrad 	u32 total = get_blocksize(sequence);
   1070  1.1  riastrad 	const u8 *data = &sequence->data[0];
   1071  1.1  riastrad 	u8 current_id;
   1072  1.1  riastrad 	u32 current_size;
   1073  1.1  riastrad 	int header_size = sequence->version >= 3 ? 5 : 3;
   1074  1.1  riastrad 	int index = 0;
   1075  1.1  riastrad 	int i;
   1076  1.1  riastrad 
   1077  1.1  riastrad 	/* skip new block size */
   1078  1.1  riastrad 	if (sequence->version >= 3)
   1079  1.1  riastrad 		data += 4;
   1080  1.1  riastrad 
   1081  1.1  riastrad 	for (i = 0; i < MAX_MIPI_CONFIGURATIONS && index < total; i++) {
   1082  1.1  riastrad 		if (index + header_size > total) {
   1083  1.1  riastrad 			DRM_ERROR("Invalid sequence block (header)\n");
   1084  1.1  riastrad 			return NULL;
   1085  1.1  riastrad 		}
   1086  1.1  riastrad 
   1087  1.1  riastrad 		current_id = *(data + index);
   1088  1.1  riastrad 		if (sequence->version >= 3)
   1089  1.1  riastrad 			current_size = *((const u32 *)(data + index + 1));
   1090  1.1  riastrad 		else
   1091  1.1  riastrad 			current_size = *((const u16 *)(data + index + 1));
   1092  1.1  riastrad 
   1093  1.1  riastrad 		index += header_size;
   1094  1.1  riastrad 
   1095  1.1  riastrad 		if (index + current_size > total) {
   1096  1.1  riastrad 			DRM_ERROR("Invalid sequence block\n");
   1097  1.1  riastrad 			return NULL;
   1098  1.1  riastrad 		}
   1099  1.1  riastrad 
   1100  1.1  riastrad 		if (current_id == panel_id) {
   1101  1.1  riastrad 			*seq_size = current_size;
   1102  1.1  riastrad 			return data + index;
   1103  1.1  riastrad 		}
   1104  1.1  riastrad 
   1105  1.1  riastrad 		index += current_size;
   1106  1.1  riastrad 	}
   1107  1.1  riastrad 
   1108  1.1  riastrad 	DRM_ERROR("Sequence block detected but no valid configuration\n");
   1109  1.1  riastrad 
   1110  1.1  riastrad 	return NULL;
   1111  1.1  riastrad }
   1112  1.1  riastrad 
   1113  1.1  riastrad static int goto_next_sequence(const u8 *data, int index, int total)
   1114  1.1  riastrad {
   1115  1.1  riastrad 	u16 len;
   1116  1.1  riastrad 
   1117  1.1  riastrad 	/* Skip Sequence Byte. */
   1118  1.1  riastrad 	for (index = index + 1; index < total; index += len) {
   1119  1.1  riastrad 		u8 operation_byte = *(data + index);
   1120  1.1  riastrad 		index++;
   1121  1.1  riastrad 
   1122  1.1  riastrad 		switch (operation_byte) {
   1123  1.1  riastrad 		case MIPI_SEQ_ELEM_END:
   1124  1.1  riastrad 			return index;
   1125  1.1  riastrad 		case MIPI_SEQ_ELEM_SEND_PKT:
   1126  1.1  riastrad 			if (index + 4 > total)
   1127  1.1  riastrad 				return 0;
   1128  1.1  riastrad 
   1129  1.1  riastrad 			len = *((const u16 *)(data + index + 2)) + 4;
   1130  1.1  riastrad 			break;
   1131  1.1  riastrad 		case MIPI_SEQ_ELEM_DELAY:
   1132  1.1  riastrad 			len = 4;
   1133  1.1  riastrad 			break;
   1134  1.1  riastrad 		case MIPI_SEQ_ELEM_GPIO:
   1135  1.1  riastrad 			len = 2;
   1136  1.1  riastrad 			break;
   1137  1.1  riastrad 		case MIPI_SEQ_ELEM_I2C:
   1138  1.1  riastrad 			if (index + 7 > total)
   1139  1.1  riastrad 				return 0;
   1140  1.1  riastrad 			len = *(data + index + 6) + 7;
   1141  1.1  riastrad 			break;
   1142  1.1  riastrad 		default:
   1143  1.1  riastrad 			DRM_ERROR("Unknown operation byte\n");
   1144  1.1  riastrad 			return 0;
   1145  1.1  riastrad 		}
   1146  1.1  riastrad 	}
   1147  1.1  riastrad 
   1148  1.1  riastrad 	return 0;
   1149  1.1  riastrad }
   1150  1.1  riastrad 
   1151  1.1  riastrad static int goto_next_sequence_v3(const u8 *data, int index, int total)
   1152  1.1  riastrad {
   1153  1.1  riastrad 	int seq_end;
   1154  1.1  riastrad 	u16 len;
   1155  1.1  riastrad 	u32 size_of_sequence;
   1156  1.1  riastrad 
   1157  1.1  riastrad 	/*
   1158  1.1  riastrad 	 * Could skip sequence based on Size of Sequence alone, but also do some
   1159  1.1  riastrad 	 * checking on the structure.
   1160  1.1  riastrad 	 */
   1161  1.1  riastrad 	if (total < 5) {
   1162  1.1  riastrad 		DRM_ERROR("Too small sequence size\n");
   1163  1.1  riastrad 		return 0;
   1164  1.1  riastrad 	}
   1165  1.1  riastrad 
   1166  1.1  riastrad 	/* Skip Sequence Byte. */
   1167  1.1  riastrad 	index++;
   1168  1.1  riastrad 
   1169  1.1  riastrad 	/*
   1170  1.1  riastrad 	 * Size of Sequence. Excludes the Sequence Byte and the size itself,
   1171  1.1  riastrad 	 * includes MIPI_SEQ_ELEM_END byte, excludes the final MIPI_SEQ_END
   1172  1.1  riastrad 	 * byte.
   1173  1.1  riastrad 	 */
   1174  1.1  riastrad 	size_of_sequence = *((const u32 *)(data + index));
   1175  1.1  riastrad 	index += 4;
   1176  1.1  riastrad 
   1177  1.1  riastrad 	seq_end = index + size_of_sequence;
   1178  1.1  riastrad 	if (seq_end > total) {
   1179  1.1  riastrad 		DRM_ERROR("Invalid sequence size\n");
   1180  1.1  riastrad 		return 0;
   1181  1.1  riastrad 	}
   1182  1.1  riastrad 
   1183  1.1  riastrad 	for (; index < total; index += len) {
   1184  1.1  riastrad 		u8 operation_byte = *(data + index);
   1185  1.1  riastrad 		index++;
   1186  1.1  riastrad 
   1187  1.1  riastrad 		if (operation_byte == MIPI_SEQ_ELEM_END) {
   1188  1.1  riastrad 			if (index != seq_end) {
   1189  1.1  riastrad 				DRM_ERROR("Invalid element structure\n");
   1190  1.1  riastrad 				return 0;
   1191  1.1  riastrad 			}
   1192  1.1  riastrad 			return index;
   1193  1.1  riastrad 		}
   1194  1.1  riastrad 
   1195  1.1  riastrad 		len = *(data + index);
   1196  1.1  riastrad 		index++;
   1197  1.1  riastrad 
   1198  1.1  riastrad 		/*
   1199  1.1  riastrad 		 * FIXME: Would be nice to check elements like for v1/v2 in
   1200  1.1  riastrad 		 * goto_next_sequence() above.
   1201  1.1  riastrad 		 */
   1202  1.1  riastrad 		switch (operation_byte) {
   1203  1.1  riastrad 		case MIPI_SEQ_ELEM_SEND_PKT:
   1204  1.1  riastrad 		case MIPI_SEQ_ELEM_DELAY:
   1205  1.1  riastrad 		case MIPI_SEQ_ELEM_GPIO:
   1206  1.1  riastrad 		case MIPI_SEQ_ELEM_I2C:
   1207  1.1  riastrad 		case MIPI_SEQ_ELEM_SPI:
   1208  1.1  riastrad 		case MIPI_SEQ_ELEM_PMIC:
   1209  1.1  riastrad 			break;
   1210  1.1  riastrad 		default:
   1211  1.1  riastrad 			DRM_ERROR("Unknown operation byte %u\n",
   1212  1.1  riastrad 				  operation_byte);
   1213  1.1  riastrad 			break;
   1214  1.1  riastrad 		}
   1215  1.1  riastrad 	}
   1216  1.1  riastrad 
   1217  1.1  riastrad 	return 0;
   1218  1.1  riastrad }
   1219  1.1  riastrad 
   1220  1.1  riastrad /*
   1221  1.1  riastrad  * Get len of pre-fixed deassert fragment from a v1 init OTP sequence,
   1222  1.1  riastrad  * skip all delay + gpio operands and stop at the first DSI packet op.
   1223  1.1  riastrad  */
   1224  1.1  riastrad static int get_init_otp_deassert_fragment_len(struct drm_i915_private *dev_priv)
   1225  1.1  riastrad {
   1226  1.1  riastrad 	const u8 *data = dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP];
   1227  1.1  riastrad 	int index, len;
   1228  1.1  riastrad 
   1229  1.1  riastrad 	if (WARN_ON(!data || dev_priv->vbt.dsi.seq_version != 1))
   1230  1.1  riastrad 		return 0;
   1231  1.1  riastrad 
   1232  1.1  riastrad 	/* index = 1 to skip sequence byte */
   1233  1.1  riastrad 	for (index = 1; data[index] != MIPI_SEQ_ELEM_END; index += len) {
   1234  1.1  riastrad 		switch (data[index]) {
   1235  1.1  riastrad 		case MIPI_SEQ_ELEM_SEND_PKT:
   1236  1.1  riastrad 			return index == 1 ? 0 : index;
   1237  1.1  riastrad 		case MIPI_SEQ_ELEM_DELAY:
   1238  1.1  riastrad 			len = 5; /* 1 byte for operand + uint32 */
   1239  1.1  riastrad 			break;
   1240  1.1  riastrad 		case MIPI_SEQ_ELEM_GPIO:
   1241  1.1  riastrad 			len = 3; /* 1 byte for op, 1 for gpio_nr, 1 for value */
   1242  1.1  riastrad 			break;
   1243  1.1  riastrad 		default:
   1244  1.1  riastrad 			return 0;
   1245  1.1  riastrad 		}
   1246  1.1  riastrad 	}
   1247  1.1  riastrad 
   1248  1.1  riastrad 	return 0;
   1249  1.1  riastrad }
   1250  1.1  riastrad 
   1251  1.1  riastrad /*
   1252  1.1  riastrad  * Some v1 VBT MIPI sequences do the deassert in the init OTP sequence.
   1253  1.1  riastrad  * The deassert must be done before calling intel_dsi_device_ready, so for
   1254  1.1  riastrad  * these devices we split the init OTP sequence into a deassert sequence and
   1255  1.1  riastrad  * the actual init OTP part.
   1256  1.1  riastrad  */
   1257  1.1  riastrad static void fixup_mipi_sequences(struct drm_i915_private *dev_priv)
   1258  1.1  riastrad {
   1259  1.1  riastrad 	u8 *init_otp;
   1260  1.1  riastrad 	int len;
   1261  1.1  riastrad 
   1262  1.1  riastrad 	/* Limit this to VLV for now. */
   1263  1.1  riastrad 	if (!IS_VALLEYVIEW(dev_priv))
   1264  1.1  riastrad 		return;
   1265  1.1  riastrad 
   1266  1.1  riastrad 	/* Limit this to v1 vid-mode sequences */
   1267  1.1  riastrad 	if (dev_priv->vbt.dsi.config->is_cmd_mode ||
   1268  1.1  riastrad 	    dev_priv->vbt.dsi.seq_version != 1)
   1269  1.1  riastrad 		return;
   1270  1.1  riastrad 
   1271  1.1  riastrad 	/* Only do this if there are otp and assert seqs and no deassert seq */
   1272  1.1  riastrad 	if (!dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] ||
   1273  1.1  riastrad 	    !dev_priv->vbt.dsi.sequence[MIPI_SEQ_ASSERT_RESET] ||
   1274  1.1  riastrad 	    dev_priv->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET])
   1275  1.1  riastrad 		return;
   1276  1.1  riastrad 
   1277  1.1  riastrad 	/* The deassert-sequence ends at the first DSI packet */
   1278  1.1  riastrad 	len = get_init_otp_deassert_fragment_len(dev_priv);
   1279  1.1  riastrad 	if (!len)
   1280  1.1  riastrad 		return;
   1281  1.1  riastrad 
   1282  1.1  riastrad 	DRM_DEBUG_KMS("Using init OTP fragment to deassert reset\n");
   1283  1.1  riastrad 
   1284  1.1  riastrad 	/* Copy the fragment, update seq byte and terminate it */
   1285  1.3  riastrad 	init_otp = (u8 *)__UNCONST(dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP]);
   1286  1.1  riastrad 	dev_priv->vbt.dsi.deassert_seq = kmemdup(init_otp, len + 1, GFP_KERNEL);
   1287  1.1  riastrad 	if (!dev_priv->vbt.dsi.deassert_seq)
   1288  1.1  riastrad 		return;
   1289  1.1  riastrad 	dev_priv->vbt.dsi.deassert_seq[0] = MIPI_SEQ_DEASSERT_RESET;
   1290  1.1  riastrad 	dev_priv->vbt.dsi.deassert_seq[len] = MIPI_SEQ_ELEM_END;
   1291  1.1  riastrad 	/* Use the copy for deassert */
   1292  1.1  riastrad 	dev_priv->vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET] =
   1293  1.1  riastrad 		dev_priv->vbt.dsi.deassert_seq;
   1294  1.1  riastrad 	/* Replace the last byte of the fragment with init OTP seq byte */
   1295  1.1  riastrad 	init_otp[len - 1] = MIPI_SEQ_INIT_OTP;
   1296  1.1  riastrad 	/* And make MIPI_MIPI_SEQ_INIT_OTP point to it */
   1297  1.1  riastrad 	dev_priv->vbt.dsi.sequence[MIPI_SEQ_INIT_OTP] = init_otp + len - 1;
   1298  1.1  riastrad }
   1299  1.1  riastrad 
   1300  1.1  riastrad static void
   1301  1.1  riastrad parse_mipi_sequence(struct drm_i915_private *dev_priv,
   1302  1.1  riastrad 		    const struct bdb_header *bdb)
   1303  1.1  riastrad {
   1304  1.1  riastrad 	int panel_type = dev_priv->vbt.panel_type;
   1305  1.1  riastrad 	const struct bdb_mipi_sequence *sequence;
   1306  1.1  riastrad 	const u8 *seq_data;
   1307  1.1  riastrad 	u32 seq_size;
   1308  1.1  riastrad 	u8 *data;
   1309  1.1  riastrad 	int index = 0;
   1310  1.1  riastrad 
   1311  1.1  riastrad 	/* Only our generic panel driver uses the sequence block. */
   1312  1.1  riastrad 	if (dev_priv->vbt.dsi.panel_id != MIPI_DSI_GENERIC_PANEL_ID)
   1313  1.1  riastrad 		return;
   1314  1.1  riastrad 
   1315  1.1  riastrad 	sequence = find_section(bdb, BDB_MIPI_SEQUENCE);
   1316  1.1  riastrad 	if (!sequence) {
   1317  1.1  riastrad 		DRM_DEBUG_KMS("No MIPI Sequence found, parsing complete\n");
   1318  1.1  riastrad 		return;
   1319  1.1  riastrad 	}
   1320  1.1  riastrad 
   1321  1.1  riastrad 	/* Fail gracefully for forward incompatible sequence block. */
   1322  1.1  riastrad 	if (sequence->version >= 4) {
   1323  1.1  riastrad 		DRM_ERROR("Unable to parse MIPI Sequence Block v%u\n",
   1324  1.1  riastrad 			  sequence->version);
   1325  1.1  riastrad 		return;
   1326  1.1  riastrad 	}
   1327  1.1  riastrad 
   1328  1.1  riastrad 	DRM_DEBUG_DRIVER("Found MIPI sequence block v%u\n", sequence->version);
   1329  1.1  riastrad 
   1330  1.1  riastrad 	seq_data = find_panel_sequence_block(sequence, panel_type, &seq_size);
   1331  1.1  riastrad 	if (!seq_data)
   1332  1.1  riastrad 		return;
   1333  1.1  riastrad 
   1334  1.1  riastrad 	data = kmemdup(seq_data, seq_size, GFP_KERNEL);
   1335  1.1  riastrad 	if (!data)
   1336  1.1  riastrad 		return;
   1337  1.1  riastrad 
   1338  1.1  riastrad 	/* Parse the sequences, store pointers to each sequence. */
   1339  1.1  riastrad 	for (;;) {
   1340  1.1  riastrad 		u8 seq_id = *(data + index);
   1341  1.1  riastrad 		if (seq_id == MIPI_SEQ_END)
   1342  1.1  riastrad 			break;
   1343  1.1  riastrad 
   1344  1.1  riastrad 		if (seq_id >= MIPI_SEQ_MAX) {
   1345  1.1  riastrad 			DRM_ERROR("Unknown sequence %u\n", seq_id);
   1346  1.1  riastrad 			goto err;
   1347  1.1  riastrad 		}
   1348  1.1  riastrad 
   1349  1.1  riastrad 		/* Log about presence of sequences we won't run. */
   1350  1.1  riastrad 		if (seq_id == MIPI_SEQ_TEAR_ON || seq_id == MIPI_SEQ_TEAR_OFF)
   1351  1.1  riastrad 			DRM_DEBUG_KMS("Unsupported sequence %u\n", seq_id);
   1352  1.1  riastrad 
   1353  1.1  riastrad 		dev_priv->vbt.dsi.sequence[seq_id] = data + index;
   1354  1.1  riastrad 
   1355  1.1  riastrad 		if (sequence->version >= 3)
   1356  1.1  riastrad 			index = goto_next_sequence_v3(data, index, seq_size);
   1357  1.1  riastrad 		else
   1358  1.1  riastrad 			index = goto_next_sequence(data, index, seq_size);
   1359  1.1  riastrad 		if (!index) {
   1360  1.1  riastrad 			DRM_ERROR("Invalid sequence %u\n", seq_id);
   1361  1.1  riastrad 			goto err;
   1362  1.1  riastrad 		}
   1363  1.1  riastrad 	}
   1364  1.1  riastrad 
   1365  1.1  riastrad 	dev_priv->vbt.dsi.data = data;
   1366  1.1  riastrad 	dev_priv->vbt.dsi.size = seq_size;
   1367  1.1  riastrad 	dev_priv->vbt.dsi.seq_version = sequence->version;
   1368  1.1  riastrad 
   1369  1.1  riastrad 	fixup_mipi_sequences(dev_priv);
   1370  1.1  riastrad 
   1371  1.1  riastrad 	DRM_DEBUG_DRIVER("MIPI related VBT parsing complete\n");
   1372  1.1  riastrad 	return;
   1373  1.1  riastrad 
   1374  1.1  riastrad err:
   1375  1.1  riastrad 	kfree(data);
   1376  1.1  riastrad 	memset(dev_priv->vbt.dsi.sequence, 0, sizeof(dev_priv->vbt.dsi.sequence));
   1377  1.1  riastrad }
   1378  1.1  riastrad 
   1379  1.1  riastrad static void
   1380  1.1  riastrad parse_compression_parameters(struct drm_i915_private *i915,
   1381  1.1  riastrad 			     const struct bdb_header *bdb)
   1382  1.1  riastrad {
   1383  1.1  riastrad 	const struct bdb_compression_parameters *params;
   1384  1.1  riastrad 	struct display_device_data *devdata;
   1385  1.1  riastrad 	const struct child_device_config *child;
   1386  1.1  riastrad 	u16 block_size;
   1387  1.1  riastrad 	int index;
   1388  1.1  riastrad 
   1389  1.1  riastrad 	if (bdb->version < 198)
   1390  1.1  riastrad 		return;
   1391  1.1  riastrad 
   1392  1.1  riastrad 	params = find_section(bdb, BDB_COMPRESSION_PARAMETERS);
   1393  1.1  riastrad 	if (params) {
   1394  1.1  riastrad 		/* Sanity checks */
   1395  1.1  riastrad 		if (params->entry_size != sizeof(params->data[0])) {
   1396  1.1  riastrad 			DRM_DEBUG_KMS("VBT: unsupported compression param entry size\n");
   1397  1.1  riastrad 			return;
   1398  1.1  riastrad 		}
   1399  1.1  riastrad 
   1400  1.1  riastrad 		block_size = get_blocksize(params);
   1401  1.1  riastrad 		if (block_size < sizeof(*params)) {
   1402  1.1  riastrad 			DRM_DEBUG_KMS("VBT: expected 16 compression param entries\n");
   1403  1.1  riastrad 			return;
   1404  1.1  riastrad 		}
   1405  1.1  riastrad 	}
   1406  1.1  riastrad 
   1407  1.1  riastrad 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
   1408  1.1  riastrad 		child = &devdata->child;
   1409  1.1  riastrad 
   1410  1.1  riastrad 		if (!child->compression_enable)
   1411  1.1  riastrad 			continue;
   1412  1.1  riastrad 
   1413  1.1  riastrad 		if (!params) {
   1414  1.1  riastrad 			DRM_DEBUG_KMS("VBT: compression params not available\n");
   1415  1.1  riastrad 			continue;
   1416  1.1  riastrad 		}
   1417  1.1  riastrad 
   1418  1.1  riastrad 		if (child->compression_method_cps) {
   1419  1.1  riastrad 			DRM_DEBUG_KMS("VBT: CPS compression not supported\n");
   1420  1.1  riastrad 			continue;
   1421  1.1  riastrad 		}
   1422  1.1  riastrad 
   1423  1.1  riastrad 		index = child->compression_structure_index;
   1424  1.1  riastrad 
   1425  1.1  riastrad 		devdata->dsc = kmemdup(&params->data[index],
   1426  1.1  riastrad 				       sizeof(*devdata->dsc), GFP_KERNEL);
   1427  1.1  riastrad 	}
   1428  1.1  riastrad }
   1429  1.1  riastrad 
   1430  1.1  riastrad static u8 translate_iboost(u8 val)
   1431  1.1  riastrad {
   1432  1.1  riastrad 	static const u8 mapping[] = { 1, 3, 7 }; /* See VBT spec */
   1433  1.1  riastrad 
   1434  1.1  riastrad 	if (val >= ARRAY_SIZE(mapping)) {
   1435  1.1  riastrad 		DRM_DEBUG_KMS("Unsupported I_boost value found in VBT (%d), display may not work properly\n", val);
   1436  1.1  riastrad 		return 0;
   1437  1.1  riastrad 	}
   1438  1.1  riastrad 	return mapping[val];
   1439  1.1  riastrad }
   1440  1.1  riastrad 
   1441  1.1  riastrad static enum port get_port_by_ddc_pin(struct drm_i915_private *i915, u8 ddc_pin)
   1442  1.1  riastrad {
   1443  1.1  riastrad 	const struct ddi_vbt_port_info *info;
   1444  1.1  riastrad 	enum port port;
   1445  1.1  riastrad 
   1446  1.1  riastrad 	for_each_port(port) {
   1447  1.1  riastrad 		info = &i915->vbt.ddi_port_info[port];
   1448  1.1  riastrad 
   1449  1.1  riastrad 		if (info->child && ddc_pin == info->alternate_ddc_pin)
   1450  1.1  riastrad 			return port;
   1451  1.1  riastrad 	}
   1452  1.1  riastrad 
   1453  1.1  riastrad 	return PORT_NONE;
   1454  1.1  riastrad }
   1455  1.1  riastrad 
   1456  1.1  riastrad static void sanitize_ddc_pin(struct drm_i915_private *dev_priv,
   1457  1.1  riastrad 			     enum port port)
   1458  1.1  riastrad {
   1459  1.1  riastrad 	struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
   1460  1.1  riastrad 	enum port p;
   1461  1.1  riastrad 
   1462  1.1  riastrad 	if (!info->alternate_ddc_pin)
   1463  1.1  riastrad 		return;
   1464  1.1  riastrad 
   1465  1.1  riastrad 	p = get_port_by_ddc_pin(dev_priv, info->alternate_ddc_pin);
   1466  1.1  riastrad 	if (p != PORT_NONE) {
   1467  1.1  riastrad 		DRM_DEBUG_KMS("port %c trying to use the same DDC pin (0x%x) as port %c, "
   1468  1.1  riastrad 			      "disabling port %c DVI/HDMI support\n",
   1469  1.1  riastrad 			      port_name(port), info->alternate_ddc_pin,
   1470  1.1  riastrad 			      port_name(p), port_name(p));
   1471  1.1  riastrad 
   1472  1.1  riastrad 		/*
   1473  1.1  riastrad 		 * If we have multiple ports supposedly sharing the
   1474  1.1  riastrad 		 * pin, then dvi/hdmi couldn't exist on the shared
   1475  1.1  riastrad 		 * port. Otherwise they share the same ddc bin and
   1476  1.1  riastrad 		 * system couldn't communicate with them separately.
   1477  1.1  riastrad 		 *
   1478  1.1  riastrad 		 * Give inverse child device order the priority,
   1479  1.1  riastrad 		 * last one wins. Yes, there are real machines
   1480  1.1  riastrad 		 * (eg. Asrock B250M-HDV) where VBT has both
   1481  1.1  riastrad 		 * port A and port E with the same AUX ch and
   1482  1.1  riastrad 		 * we must pick port E :(
   1483  1.1  riastrad 		 */
   1484  1.1  riastrad 		info = &dev_priv->vbt.ddi_port_info[p];
   1485  1.1  riastrad 
   1486  1.1  riastrad 		info->supports_dvi = false;
   1487  1.1  riastrad 		info->supports_hdmi = false;
   1488  1.1  riastrad 		info->alternate_ddc_pin = 0;
   1489  1.1  riastrad 	}
   1490  1.1  riastrad }
   1491  1.1  riastrad 
   1492  1.1  riastrad static enum port get_port_by_aux_ch(struct drm_i915_private *i915, u8 aux_ch)
   1493  1.1  riastrad {
   1494  1.1  riastrad 	const struct ddi_vbt_port_info *info;
   1495  1.1  riastrad 	enum port port;
   1496  1.1  riastrad 
   1497  1.1  riastrad 	for_each_port(port) {
   1498  1.1  riastrad 		info = &i915->vbt.ddi_port_info[port];
   1499  1.1  riastrad 
   1500  1.1  riastrad 		if (info->child && aux_ch == info->alternate_aux_channel)
   1501  1.1  riastrad 			return port;
   1502  1.1  riastrad 	}
   1503  1.1  riastrad 
   1504  1.1  riastrad 	return PORT_NONE;
   1505  1.1  riastrad }
   1506  1.1  riastrad 
   1507  1.1  riastrad static void sanitize_aux_ch(struct drm_i915_private *dev_priv,
   1508  1.1  riastrad 			    enum port port)
   1509  1.1  riastrad {
   1510  1.1  riastrad 	struct ddi_vbt_port_info *info = &dev_priv->vbt.ddi_port_info[port];
   1511  1.1  riastrad 	enum port p;
   1512  1.1  riastrad 
   1513  1.1  riastrad 	if (!info->alternate_aux_channel)
   1514  1.1  riastrad 		return;
   1515  1.1  riastrad 
   1516  1.1  riastrad 	p = get_port_by_aux_ch(dev_priv, info->alternate_aux_channel);
   1517  1.1  riastrad 	if (p != PORT_NONE) {
   1518  1.1  riastrad 		DRM_DEBUG_KMS("port %c trying to use the same AUX CH (0x%x) as port %c, "
   1519  1.1  riastrad 			      "disabling port %c DP support\n",
   1520  1.1  riastrad 			      port_name(port), info->alternate_aux_channel,
   1521  1.1  riastrad 			      port_name(p), port_name(p));
   1522  1.1  riastrad 
   1523  1.1  riastrad 		/*
   1524  1.1  riastrad 		 * If we have multiple ports supposedlt sharing the
   1525  1.1  riastrad 		 * aux channel, then DP couldn't exist on the shared
   1526  1.1  riastrad 		 * port. Otherwise they share the same aux channel
   1527  1.1  riastrad 		 * and system couldn't communicate with them separately.
   1528  1.1  riastrad 		 *
   1529  1.1  riastrad 		 * Give inverse child device order the priority,
   1530  1.1  riastrad 		 * last one wins. Yes, there are real machines
   1531  1.1  riastrad 		 * (eg. Asrock B250M-HDV) where VBT has both
   1532  1.1  riastrad 		 * port A and port E with the same AUX ch and
   1533  1.1  riastrad 		 * we must pick port E :(
   1534  1.1  riastrad 		 */
   1535  1.1  riastrad 		info = &dev_priv->vbt.ddi_port_info[p];
   1536  1.1  riastrad 
   1537  1.1  riastrad 		info->supports_dp = false;
   1538  1.1  riastrad 		info->alternate_aux_channel = 0;
   1539  1.1  riastrad 	}
   1540  1.1  riastrad }
   1541  1.1  riastrad 
   1542  1.1  riastrad static const u8 cnp_ddc_pin_map[] = {
   1543  1.1  riastrad 	[0] = 0, /* N/A */
   1544  1.1  riastrad 	[DDC_BUS_DDI_B] = GMBUS_PIN_1_BXT,
   1545  1.1  riastrad 	[DDC_BUS_DDI_C] = GMBUS_PIN_2_BXT,
   1546  1.1  riastrad 	[DDC_BUS_DDI_D] = GMBUS_PIN_4_CNP, /* sic */
   1547  1.1  riastrad 	[DDC_BUS_DDI_F] = GMBUS_PIN_3_BXT, /* sic */
   1548  1.1  riastrad };
   1549  1.1  riastrad 
   1550  1.1  riastrad static const u8 icp_ddc_pin_map[] = {
   1551  1.1  riastrad 	[ICL_DDC_BUS_DDI_A] = GMBUS_PIN_1_BXT,
   1552  1.1  riastrad 	[ICL_DDC_BUS_DDI_B] = GMBUS_PIN_2_BXT,
   1553  1.1  riastrad 	[TGL_DDC_BUS_DDI_C] = GMBUS_PIN_3_BXT,
   1554  1.1  riastrad 	[ICL_DDC_BUS_PORT_1] = GMBUS_PIN_9_TC1_ICP,
   1555  1.1  riastrad 	[ICL_DDC_BUS_PORT_2] = GMBUS_PIN_10_TC2_ICP,
   1556  1.1  riastrad 	[ICL_DDC_BUS_PORT_3] = GMBUS_PIN_11_TC3_ICP,
   1557  1.1  riastrad 	[ICL_DDC_BUS_PORT_4] = GMBUS_PIN_12_TC4_ICP,
   1558  1.1  riastrad 	[TGL_DDC_BUS_PORT_5] = GMBUS_PIN_13_TC5_TGP,
   1559  1.1  riastrad 	[TGL_DDC_BUS_PORT_6] = GMBUS_PIN_14_TC6_TGP,
   1560  1.1  riastrad };
   1561  1.1  riastrad 
   1562  1.1  riastrad static u8 map_ddc_pin(struct drm_i915_private *dev_priv, u8 vbt_pin)
   1563  1.1  riastrad {
   1564  1.1  riastrad 	const u8 *ddc_pin_map;
   1565  1.1  riastrad 	int n_entries;
   1566  1.1  riastrad 
   1567  1.1  riastrad 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP) {
   1568  1.1  riastrad 		ddc_pin_map = icp_ddc_pin_map;
   1569  1.1  riastrad 		n_entries = ARRAY_SIZE(icp_ddc_pin_map);
   1570  1.1  riastrad 	} else if (HAS_PCH_CNP(dev_priv)) {
   1571  1.1  riastrad 		ddc_pin_map = cnp_ddc_pin_map;
   1572  1.1  riastrad 		n_entries = ARRAY_SIZE(cnp_ddc_pin_map);
   1573  1.1  riastrad 	} else {
   1574  1.1  riastrad 		/* Assuming direct map */
   1575  1.1  riastrad 		return vbt_pin;
   1576  1.1  riastrad 	}
   1577  1.1  riastrad 
   1578  1.1  riastrad 	if (vbt_pin < n_entries && ddc_pin_map[vbt_pin] != 0)
   1579  1.1  riastrad 		return ddc_pin_map[vbt_pin];
   1580  1.1  riastrad 
   1581  1.1  riastrad 	DRM_DEBUG_KMS("Ignoring alternate pin: VBT claims DDC pin %d, which is not valid for this platform\n",
   1582  1.1  riastrad 		      vbt_pin);
   1583  1.1  riastrad 	return 0;
   1584  1.1  riastrad }
   1585  1.1  riastrad 
   1586  1.1  riastrad static enum port dvo_port_to_port(u8 dvo_port)
   1587  1.1  riastrad {
   1588  1.1  riastrad 	/*
   1589  1.1  riastrad 	 * Each DDI port can have more than one value on the "DVO Port" field,
   1590  1.1  riastrad 	 * so look for all the possible values for each port.
   1591  1.1  riastrad 	 */
   1592  1.1  riastrad 	static const int dvo_ports[][3] = {
   1593  1.1  riastrad 		[PORT_A] = { DVO_PORT_HDMIA, DVO_PORT_DPA, -1},
   1594  1.1  riastrad 		[PORT_B] = { DVO_PORT_HDMIB, DVO_PORT_DPB, -1},
   1595  1.1  riastrad 		[PORT_C] = { DVO_PORT_HDMIC, DVO_PORT_DPC, -1},
   1596  1.1  riastrad 		[PORT_D] = { DVO_PORT_HDMID, DVO_PORT_DPD, -1},
   1597  1.1  riastrad 		[PORT_E] = { DVO_PORT_CRT, DVO_PORT_HDMIE, DVO_PORT_DPE},
   1598  1.1  riastrad 		[PORT_F] = { DVO_PORT_HDMIF, DVO_PORT_DPF, -1},
   1599  1.1  riastrad 		[PORT_G] = { DVO_PORT_HDMIG, DVO_PORT_DPG, -1},
   1600  1.1  riastrad 	};
   1601  1.1  riastrad 	enum port port;
   1602  1.1  riastrad 	int i;
   1603  1.1  riastrad 
   1604  1.1  riastrad 	for (port = PORT_A; port < ARRAY_SIZE(dvo_ports); port++) {
   1605  1.1  riastrad 		for (i = 0; i < ARRAY_SIZE(dvo_ports[port]); i++) {
   1606  1.1  riastrad 			if (dvo_ports[port][i] == -1)
   1607  1.1  riastrad 				break;
   1608  1.1  riastrad 
   1609  1.1  riastrad 			if (dvo_port == dvo_ports[port][i])
   1610  1.1  riastrad 				return port;
   1611  1.1  riastrad 		}
   1612  1.1  riastrad 	}
   1613  1.1  riastrad 
   1614  1.1  riastrad 	return PORT_NONE;
   1615  1.1  riastrad }
   1616  1.1  riastrad 
   1617  1.1  riastrad static void parse_ddi_port(struct drm_i915_private *dev_priv,
   1618  1.1  riastrad 			   struct display_device_data *devdata,
   1619  1.1  riastrad 			   u8 bdb_version)
   1620  1.1  riastrad {
   1621  1.1  riastrad 	const struct child_device_config *child = &devdata->child;
   1622  1.1  riastrad 	struct ddi_vbt_port_info *info;
   1623  1.1  riastrad 	bool is_dvi, is_hdmi, is_dp, is_edp, is_crt;
   1624  1.1  riastrad 	enum port port;
   1625  1.1  riastrad 
   1626  1.1  riastrad 	port = dvo_port_to_port(child->dvo_port);
   1627  1.1  riastrad 	if (port == PORT_NONE)
   1628  1.1  riastrad 		return;
   1629  1.1  riastrad 
   1630  1.1  riastrad 	info = &dev_priv->vbt.ddi_port_info[port];
   1631  1.1  riastrad 
   1632  1.1  riastrad 	if (info->child) {
   1633  1.1  riastrad 		DRM_DEBUG_KMS("More than one child device for port %c in VBT, using the first.\n",
   1634  1.1  riastrad 			      port_name(port));
   1635  1.1  riastrad 		return;
   1636  1.1  riastrad 	}
   1637  1.1  riastrad 
   1638  1.1  riastrad 	is_dvi = child->device_type & DEVICE_TYPE_TMDS_DVI_SIGNALING;
   1639  1.1  riastrad 	is_dp = child->device_type & DEVICE_TYPE_DISPLAYPORT_OUTPUT;
   1640  1.1  riastrad 	is_crt = child->device_type & DEVICE_TYPE_ANALOG_OUTPUT;
   1641  1.1  riastrad 	is_hdmi = is_dvi && (child->device_type & DEVICE_TYPE_NOT_HDMI_OUTPUT) == 0;
   1642  1.1  riastrad 	is_edp = is_dp && (child->device_type & DEVICE_TYPE_INTERNAL_CONNECTOR);
   1643  1.1  riastrad 
   1644  1.1  riastrad 	if (port == PORT_A && is_dvi && INTEL_GEN(dev_priv) < 12) {
   1645  1.1  riastrad 		DRM_DEBUG_KMS("VBT claims port A supports DVI%s, ignoring\n",
   1646  1.1  riastrad 			      is_hdmi ? "/HDMI" : "");
   1647  1.1  riastrad 		is_dvi = false;
   1648  1.1  riastrad 		is_hdmi = false;
   1649  1.1  riastrad 	}
   1650  1.1  riastrad 
   1651  1.1  riastrad 	info->supports_dvi = is_dvi;
   1652  1.1  riastrad 	info->supports_hdmi = is_hdmi;
   1653  1.1  riastrad 	info->supports_dp = is_dp;
   1654  1.1  riastrad 	info->supports_edp = is_edp;
   1655  1.1  riastrad 
   1656  1.1  riastrad 	if (bdb_version >= 195)
   1657  1.1  riastrad 		info->supports_typec_usb = child->dp_usb_type_c;
   1658  1.1  riastrad 
   1659  1.1  riastrad 	if (bdb_version >= 209)
   1660  1.1  riastrad 		info->supports_tbt = child->tbt;
   1661  1.1  riastrad 
   1662  1.1  riastrad 	DRM_DEBUG_KMS("Port %c VBT info: CRT:%d DVI:%d HDMI:%d DP:%d eDP:%d LSPCON:%d USB-Type-C:%d TBT:%d DSC:%d\n",
   1663  1.1  riastrad 		      port_name(port), is_crt, is_dvi, is_hdmi, is_dp, is_edp,
   1664  1.1  riastrad 		      HAS_LSPCON(dev_priv) && child->lspcon,
   1665  1.1  riastrad 		      info->supports_typec_usb, info->supports_tbt,
   1666  1.1  riastrad 		      devdata->dsc != NULL);
   1667  1.1  riastrad 
   1668  1.1  riastrad 	if (is_dvi) {
   1669  1.1  riastrad 		u8 ddc_pin;
   1670  1.1  riastrad 
   1671  1.1  riastrad 		ddc_pin = map_ddc_pin(dev_priv, child->ddc_pin);
   1672  1.1  riastrad 		if (intel_gmbus_is_valid_pin(dev_priv, ddc_pin)) {
   1673  1.1  riastrad 			info->alternate_ddc_pin = ddc_pin;
   1674  1.1  riastrad 			sanitize_ddc_pin(dev_priv, port);
   1675  1.1  riastrad 		} else {
   1676  1.1  riastrad 			DRM_DEBUG_KMS("Port %c has invalid DDC pin %d, "
   1677  1.1  riastrad 				      "sticking to defaults\n",
   1678  1.1  riastrad 				      port_name(port), ddc_pin);
   1679  1.1  riastrad 		}
   1680  1.1  riastrad 	}
   1681  1.1  riastrad 
   1682  1.1  riastrad 	if (is_dp) {
   1683  1.1  riastrad 		info->alternate_aux_channel = child->aux_channel;
   1684  1.1  riastrad 
   1685  1.1  riastrad 		sanitize_aux_ch(dev_priv, port);
   1686  1.1  riastrad 	}
   1687  1.1  riastrad 
   1688  1.1  riastrad 	if (bdb_version >= 158) {
   1689  1.1  riastrad 		/* The VBT HDMI level shift values match the table we have. */
   1690  1.1  riastrad 		u8 hdmi_level_shift = child->hdmi_level_shifter_value;
   1691  1.1  riastrad 		DRM_DEBUG_KMS("VBT HDMI level shift for port %c: %d\n",
   1692  1.1  riastrad 			      port_name(port),
   1693  1.1  riastrad 			      hdmi_level_shift);
   1694  1.1  riastrad 		info->hdmi_level_shift = hdmi_level_shift;
   1695  1.1  riastrad 		info->hdmi_level_shift_set = true;
   1696  1.1  riastrad 	}
   1697  1.1  riastrad 
   1698  1.1  riastrad 	if (bdb_version >= 204) {
   1699  1.1  riastrad 		int max_tmds_clock;
   1700  1.1  riastrad 
   1701  1.1  riastrad 		switch (child->hdmi_max_data_rate) {
   1702  1.1  riastrad 		default:
   1703  1.1  riastrad 			MISSING_CASE(child->hdmi_max_data_rate);
   1704  1.1  riastrad 			/* fall through */
   1705  1.1  riastrad 		case HDMI_MAX_DATA_RATE_PLATFORM:
   1706  1.1  riastrad 			max_tmds_clock = 0;
   1707  1.1  riastrad 			break;
   1708  1.1  riastrad 		case HDMI_MAX_DATA_RATE_297:
   1709  1.1  riastrad 			max_tmds_clock = 297000;
   1710  1.1  riastrad 			break;
   1711  1.1  riastrad 		case HDMI_MAX_DATA_RATE_165:
   1712  1.1  riastrad 			max_tmds_clock = 165000;
   1713  1.1  riastrad 			break;
   1714  1.1  riastrad 		}
   1715  1.1  riastrad 
   1716  1.1  riastrad 		if (max_tmds_clock)
   1717  1.1  riastrad 			DRM_DEBUG_KMS("VBT HDMI max TMDS clock for port %c: %d kHz\n",
   1718  1.1  riastrad 				      port_name(port), max_tmds_clock);
   1719  1.1  riastrad 		info->max_tmds_clock = max_tmds_clock;
   1720  1.1  riastrad 	}
   1721  1.1  riastrad 
   1722  1.1  riastrad 	/* Parse the I_boost config for SKL and above */
   1723  1.1  riastrad 	if (bdb_version >= 196 && child->iboost) {
   1724  1.1  riastrad 		info->dp_boost_level = translate_iboost(child->dp_iboost_level);
   1725  1.1  riastrad 		DRM_DEBUG_KMS("VBT (e)DP boost level for port %c: %d\n",
   1726  1.1  riastrad 			      port_name(port), info->dp_boost_level);
   1727  1.1  riastrad 		info->hdmi_boost_level = translate_iboost(child->hdmi_iboost_level);
   1728  1.1  riastrad 		DRM_DEBUG_KMS("VBT HDMI boost level for port %c: %d\n",
   1729  1.1  riastrad 			      port_name(port), info->hdmi_boost_level);
   1730  1.1  riastrad 	}
   1731  1.1  riastrad 
   1732  1.1  riastrad 	/* DP max link rate for CNL+ */
   1733  1.1  riastrad 	if (bdb_version >= 216) {
   1734  1.1  riastrad 		switch (child->dp_max_link_rate) {
   1735  1.1  riastrad 		default:
   1736  1.1  riastrad 		case VBT_DP_MAX_LINK_RATE_HBR3:
   1737  1.1  riastrad 			info->dp_max_link_rate = 810000;
   1738  1.1  riastrad 			break;
   1739  1.1  riastrad 		case VBT_DP_MAX_LINK_RATE_HBR2:
   1740  1.1  riastrad 			info->dp_max_link_rate = 540000;
   1741  1.1  riastrad 			break;
   1742  1.1  riastrad 		case VBT_DP_MAX_LINK_RATE_HBR:
   1743  1.1  riastrad 			info->dp_max_link_rate = 270000;
   1744  1.1  riastrad 			break;
   1745  1.1  riastrad 		case VBT_DP_MAX_LINK_RATE_LBR:
   1746  1.1  riastrad 			info->dp_max_link_rate = 162000;
   1747  1.1  riastrad 			break;
   1748  1.1  riastrad 		}
   1749  1.1  riastrad 		DRM_DEBUG_KMS("VBT DP max link rate for port %c: %d\n",
   1750  1.1  riastrad 			      port_name(port), info->dp_max_link_rate);
   1751  1.1  riastrad 	}
   1752  1.1  riastrad 
   1753  1.1  riastrad 	info->child = child;
   1754  1.1  riastrad }
   1755  1.1  riastrad 
   1756  1.1  riastrad static void parse_ddi_ports(struct drm_i915_private *dev_priv, u8 bdb_version)
   1757  1.1  riastrad {
   1758  1.1  riastrad 	struct display_device_data *devdata;
   1759  1.1  riastrad 
   1760  1.1  riastrad 	if (!HAS_DDI(dev_priv) && !IS_CHERRYVIEW(dev_priv))
   1761  1.1  riastrad 		return;
   1762  1.1  riastrad 
   1763  1.1  riastrad 	if (bdb_version < 155)
   1764  1.1  riastrad 		return;
   1765  1.1  riastrad 
   1766  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node)
   1767  1.1  riastrad 		parse_ddi_port(dev_priv, devdata, bdb_version);
   1768  1.1  riastrad }
   1769  1.1  riastrad 
   1770  1.1  riastrad static void
   1771  1.1  riastrad parse_general_definitions(struct drm_i915_private *dev_priv,
   1772  1.1  riastrad 			  const struct bdb_header *bdb)
   1773  1.1  riastrad {
   1774  1.1  riastrad 	const struct bdb_general_definitions *defs;
   1775  1.1  riastrad 	struct display_device_data *devdata;
   1776  1.1  riastrad 	const struct child_device_config *child;
   1777  1.1  riastrad 	int i, child_device_num;
   1778  1.1  riastrad 	u8 expected_size;
   1779  1.1  riastrad 	u16 block_size;
   1780  1.1  riastrad 	int bus_pin;
   1781  1.1  riastrad 
   1782  1.1  riastrad 	defs = find_section(bdb, BDB_GENERAL_DEFINITIONS);
   1783  1.1  riastrad 	if (!defs) {
   1784  1.1  riastrad 		DRM_DEBUG_KMS("No general definition block is found, no devices defined.\n");
   1785  1.1  riastrad 		return;
   1786  1.1  riastrad 	}
   1787  1.1  riastrad 
   1788  1.1  riastrad 	block_size = get_blocksize(defs);
   1789  1.1  riastrad 	if (block_size < sizeof(*defs)) {
   1790  1.1  riastrad 		DRM_DEBUG_KMS("General definitions block too small (%u)\n",
   1791  1.1  riastrad 			      block_size);
   1792  1.1  riastrad 		return;
   1793  1.1  riastrad 	}
   1794  1.1  riastrad 
   1795  1.1  riastrad 	bus_pin = defs->crt_ddc_gmbus_pin;
   1796  1.1  riastrad 	DRM_DEBUG_KMS("crt_ddc_bus_pin: %d\n", bus_pin);
   1797  1.1  riastrad 	if (intel_gmbus_is_valid_pin(dev_priv, bus_pin))
   1798  1.1  riastrad 		dev_priv->vbt.crt_ddc_pin = bus_pin;
   1799  1.1  riastrad 
   1800  1.1  riastrad 	if (bdb->version < 106) {
   1801  1.1  riastrad 		expected_size = 22;
   1802  1.1  riastrad 	} else if (bdb->version < 111) {
   1803  1.1  riastrad 		expected_size = 27;
   1804  1.1  riastrad 	} else if (bdb->version < 195) {
   1805  1.1  riastrad 		expected_size = LEGACY_CHILD_DEVICE_CONFIG_SIZE;
   1806  1.1  riastrad 	} else if (bdb->version == 195) {
   1807  1.1  riastrad 		expected_size = 37;
   1808  1.1  riastrad 	} else if (bdb->version <= 215) {
   1809  1.1  riastrad 		expected_size = 38;
   1810  1.1  riastrad 	} else if (bdb->version <= 229) {
   1811  1.1  riastrad 		expected_size = 39;
   1812  1.1  riastrad 	} else {
   1813  1.1  riastrad 		expected_size = sizeof(*child);
   1814  1.1  riastrad 		BUILD_BUG_ON(sizeof(*child) < 39);
   1815  1.1  riastrad 		DRM_DEBUG_DRIVER("Expected child device config size for VBT version %u not known; assuming %u\n",
   1816  1.1  riastrad 				 bdb->version, expected_size);
   1817  1.1  riastrad 	}
   1818  1.1  riastrad 
   1819  1.1  riastrad 	/* Flag an error for unexpected size, but continue anyway. */
   1820  1.1  riastrad 	if (defs->child_dev_size != expected_size)
   1821  1.1  riastrad 		DRM_ERROR("Unexpected child device config size %u (expected %u for VBT version %u)\n",
   1822  1.1  riastrad 			  defs->child_dev_size, expected_size, bdb->version);
   1823  1.1  riastrad 
   1824  1.1  riastrad 	/* The legacy sized child device config is the minimum we need. */
   1825  1.1  riastrad 	if (defs->child_dev_size < LEGACY_CHILD_DEVICE_CONFIG_SIZE) {
   1826  1.1  riastrad 		DRM_DEBUG_KMS("Child device config size %u is too small.\n",
   1827  1.1  riastrad 			      defs->child_dev_size);
   1828  1.1  riastrad 		return;
   1829  1.1  riastrad 	}
   1830  1.1  riastrad 
   1831  1.1  riastrad 	/* get the number of child device */
   1832  1.1  riastrad 	child_device_num = (block_size - sizeof(*defs)) / defs->child_dev_size;
   1833  1.1  riastrad 
   1834  1.1  riastrad 	for (i = 0; i < child_device_num; i++) {
   1835  1.1  riastrad 		child = child_device_ptr(defs, i);
   1836  1.1  riastrad 		if (!child->device_type)
   1837  1.1  riastrad 			continue;
   1838  1.1  riastrad 
   1839  1.1  riastrad 		DRM_DEBUG_KMS("Found VBT child device with type 0x%x\n",
   1840  1.1  riastrad 			      child->device_type);
   1841  1.1  riastrad 
   1842  1.1  riastrad 		devdata = kzalloc(sizeof(*devdata), GFP_KERNEL);
   1843  1.1  riastrad 		if (!devdata)
   1844  1.1  riastrad 			break;
   1845  1.1  riastrad 
   1846  1.1  riastrad 		/*
   1847  1.1  riastrad 		 * Copy as much as we know (sizeof) and is available
   1848  1.1  riastrad 		 * (child_dev_size) of the child device config. Accessing the
   1849  1.1  riastrad 		 * data must depend on VBT version.
   1850  1.1  riastrad 		 */
   1851  1.1  riastrad 		memcpy(&devdata->child, child,
   1852  1.1  riastrad 		       min_t(size_t, defs->child_dev_size, sizeof(*child)));
   1853  1.1  riastrad 
   1854  1.1  riastrad 		list_add_tail(&devdata->node, &dev_priv->vbt.display_devices);
   1855  1.1  riastrad 	}
   1856  1.1  riastrad 
   1857  1.1  riastrad 	if (list_empty(&dev_priv->vbt.display_devices))
   1858  1.1  riastrad 		DRM_DEBUG_KMS("no child dev is parsed from VBT\n");
   1859  1.1  riastrad }
   1860  1.1  riastrad 
   1861  1.1  riastrad /* Common defaults which may be overridden by VBT. */
   1862  1.1  riastrad static void
   1863  1.1  riastrad init_vbt_defaults(struct drm_i915_private *dev_priv)
   1864  1.1  riastrad {
   1865  1.1  riastrad 	dev_priv->vbt.crt_ddc_pin = GMBUS_PIN_VGADDC;
   1866  1.1  riastrad 
   1867  1.1  riastrad 	/* Default to having backlight */
   1868  1.1  riastrad 	dev_priv->vbt.backlight.present = true;
   1869  1.1  riastrad 
   1870  1.1  riastrad 	/* LFP panel data */
   1871  1.1  riastrad 	dev_priv->vbt.lvds_dither = 1;
   1872  1.1  riastrad 
   1873  1.1  riastrad 	/* SDVO panel data */
   1874  1.1  riastrad 	dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
   1875  1.1  riastrad 
   1876  1.1  riastrad 	/* general features */
   1877  1.1  riastrad 	dev_priv->vbt.int_tv_support = 1;
   1878  1.1  riastrad 	dev_priv->vbt.int_crt_support = 1;
   1879  1.1  riastrad 
   1880  1.1  riastrad 	/* driver features */
   1881  1.1  riastrad 	dev_priv->vbt.int_lvds_support = 1;
   1882  1.1  riastrad 
   1883  1.1  riastrad 	/* Default to using SSC */
   1884  1.1  riastrad 	dev_priv->vbt.lvds_use_ssc = 1;
   1885  1.1  riastrad 	/*
   1886  1.1  riastrad 	 * Core/SandyBridge/IvyBridge use alternative (120MHz) reference
   1887  1.1  riastrad 	 * clock for LVDS.
   1888  1.1  riastrad 	 */
   1889  1.1  riastrad 	dev_priv->vbt.lvds_ssc_freq = intel_bios_ssc_frequency(dev_priv,
   1890  1.1  riastrad 			!HAS_PCH_SPLIT(dev_priv));
   1891  1.1  riastrad 	DRM_DEBUG_KMS("Set default to SSC at %d kHz\n", dev_priv->vbt.lvds_ssc_freq);
   1892  1.1  riastrad }
   1893  1.1  riastrad 
   1894  1.1  riastrad /* Defaults to initialize only if there is no VBT. */
   1895  1.1  riastrad static void
   1896  1.1  riastrad init_vbt_missing_defaults(struct drm_i915_private *dev_priv)
   1897  1.1  riastrad {
   1898  1.1  riastrad 	enum port port;
   1899  1.1  riastrad 
   1900  1.1  riastrad 	for_each_port(port) {
   1901  1.1  riastrad 		struct ddi_vbt_port_info *info =
   1902  1.1  riastrad 			&dev_priv->vbt.ddi_port_info[port];
   1903  1.1  riastrad 		enum phy phy = intel_port_to_phy(dev_priv, port);
   1904  1.1  riastrad 
   1905  1.1  riastrad 		/*
   1906  1.1  riastrad 		 * VBT has the TypeC mode (native,TBT/USB) and we don't want
   1907  1.1  riastrad 		 * to detect it.
   1908  1.1  riastrad 		 */
   1909  1.1  riastrad 		if (intel_phy_is_tc(dev_priv, phy))
   1910  1.1  riastrad 			continue;
   1911  1.1  riastrad 
   1912  1.1  riastrad 		info->supports_dvi = (port != PORT_A && port != PORT_E);
   1913  1.1  riastrad 		info->supports_hdmi = info->supports_dvi;
   1914  1.1  riastrad 		info->supports_dp = (port != PORT_E);
   1915  1.1  riastrad 		info->supports_edp = (port == PORT_A);
   1916  1.1  riastrad 	}
   1917  1.1  riastrad }
   1918  1.1  riastrad 
   1919  1.1  riastrad static const struct bdb_header *get_bdb_header(const struct vbt_header *vbt)
   1920  1.1  riastrad {
   1921  1.1  riastrad 	const void *_vbt = vbt;
   1922  1.1  riastrad 
   1923  1.1  riastrad 	return _vbt + vbt->bdb_offset;
   1924  1.1  riastrad }
   1925  1.1  riastrad 
   1926  1.1  riastrad /**
   1927  1.1  riastrad  * intel_bios_is_valid_vbt - does the given buffer contain a valid VBT
   1928  1.1  riastrad  * @buf:	pointer to a buffer to validate
   1929  1.1  riastrad  * @size:	size of the buffer
   1930  1.1  riastrad  *
   1931  1.1  riastrad  * Returns true on valid VBT.
   1932  1.1  riastrad  */
   1933  1.1  riastrad bool intel_bios_is_valid_vbt(const void *buf, size_t size)
   1934  1.1  riastrad {
   1935  1.1  riastrad 	const struct vbt_header *vbt = buf;
   1936  1.1  riastrad 	const struct bdb_header *bdb;
   1937  1.1  riastrad 
   1938  1.1  riastrad 	if (!vbt)
   1939  1.1  riastrad 		return false;
   1940  1.1  riastrad 
   1941  1.1  riastrad 	if (sizeof(struct vbt_header) > size) {
   1942  1.1  riastrad 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
   1943  1.1  riastrad 		return false;
   1944  1.1  riastrad 	}
   1945  1.1  riastrad 
   1946  1.1  riastrad 	if (memcmp(vbt->signature, "$VBT", 4)) {
   1947  1.1  riastrad 		DRM_DEBUG_DRIVER("VBT invalid signature\n");
   1948  1.1  riastrad 		return false;
   1949  1.1  riastrad 	}
   1950  1.1  riastrad 
   1951  1.1  riastrad 	if (vbt->vbt_size > size) {
   1952  1.1  riastrad 		DRM_DEBUG_DRIVER("VBT incomplete (vbt_size overflows)\n");
   1953  1.1  riastrad 		return false;
   1954  1.1  riastrad 	}
   1955  1.1  riastrad 
   1956  1.1  riastrad 	size = vbt->vbt_size;
   1957  1.1  riastrad 
   1958  1.1  riastrad 	if (range_overflows_t(size_t,
   1959  1.1  riastrad 			      vbt->bdb_offset,
   1960  1.1  riastrad 			      sizeof(struct bdb_header),
   1961  1.1  riastrad 			      size)) {
   1962  1.1  riastrad 		DRM_DEBUG_DRIVER("BDB header incomplete\n");
   1963  1.1  riastrad 		return false;
   1964  1.1  riastrad 	}
   1965  1.1  riastrad 
   1966  1.1  riastrad 	bdb = get_bdb_header(vbt);
   1967  1.1  riastrad 	if (range_overflows_t(size_t, vbt->bdb_offset, bdb->bdb_size, size)) {
   1968  1.1  riastrad 		DRM_DEBUG_DRIVER("BDB incomplete\n");
   1969  1.1  riastrad 		return false;
   1970  1.1  riastrad 	}
   1971  1.1  riastrad 
   1972  1.1  riastrad 	return vbt;
   1973  1.1  riastrad }
   1974  1.1  riastrad 
   1975  1.2  riastrad #ifdef __NetBSD__
   1976  1.2  riastrad #  define	__iomem	__pci_rom_iomem
   1977  1.3  riastrad #  define	ioread16	fake_ioread16
   1978  1.2  riastrad #  define	ioread32	fake_ioread32
   1979  1.3  riastrad static inline uint16_t
   1980  1.3  riastrad fake_ioread16(const void __iomem *p)
   1981  1.3  riastrad {
   1982  1.3  riastrad 	uint16_t v;
   1983  1.3  riastrad 
   1984  1.3  riastrad 	v = *(const uint16_t __iomem *)p;
   1985  1.3  riastrad 	__insn_barrier();
   1986  1.3  riastrad 
   1987  1.3  riastrad 	return v;
   1988  1.3  riastrad }
   1989  1.2  riastrad static inline uint32_t
   1990  1.2  riastrad fake_ioread32(const void __iomem *p)
   1991  1.2  riastrad {
   1992  1.2  riastrad 	uint32_t v;
   1993  1.2  riastrad 
   1994  1.2  riastrad 	v = *(const uint32_t __iomem *)p;
   1995  1.2  riastrad 	__insn_barrier();
   1996  1.2  riastrad 
   1997  1.2  riastrad 	return v;
   1998  1.2  riastrad }
   1999  1.2  riastrad #endif
   2000  1.2  riastrad 
   2001  1.1  riastrad static struct vbt_header *oprom_get_vbt(struct drm_i915_private *dev_priv)
   2002  1.1  riastrad {
   2003  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
   2004  1.1  riastrad 	void __iomem *p = NULL, *oprom;
   2005  1.1  riastrad 	struct vbt_header *vbt;
   2006  1.1  riastrad 	u16 vbt_size;
   2007  1.1  riastrad 	size_t i, size;
   2008  1.1  riastrad 
   2009  1.1  riastrad 	oprom = pci_map_rom(pdev, &size);
   2010  1.1  riastrad 	if (!oprom)
   2011  1.1  riastrad 		return NULL;
   2012  1.1  riastrad 
   2013  1.1  riastrad 	/* Scour memory looking for the VBT signature. */
   2014  1.1  riastrad 	for (i = 0; i + 4 < size; i += 4) {
   2015  1.1  riastrad 		if (ioread32(oprom + i) != *((const u32 *)"$VBT"))
   2016  1.1  riastrad 			continue;
   2017  1.1  riastrad 
   2018  1.1  riastrad 		p = oprom + i;
   2019  1.1  riastrad 		size -= i;
   2020  1.1  riastrad 		break;
   2021  1.1  riastrad 	}
   2022  1.1  riastrad 
   2023  1.1  riastrad 	if (!p)
   2024  1.1  riastrad 		goto err_unmap_oprom;
   2025  1.1  riastrad 
   2026  1.1  riastrad 	if (sizeof(struct vbt_header) > size) {
   2027  1.1  riastrad 		DRM_DEBUG_DRIVER("VBT header incomplete\n");
   2028  1.1  riastrad 		goto err_unmap_oprom;
   2029  1.1  riastrad 	}
   2030  1.1  riastrad 
   2031  1.1  riastrad 	vbt_size = ioread16(p + offsetof(struct vbt_header, vbt_size));
   2032  1.1  riastrad 	if (vbt_size > size) {
   2033  1.1  riastrad 		DRM_DEBUG_DRIVER("VBT incomplete (vbt_size overflows)\n");
   2034  1.1  riastrad 		goto err_unmap_oprom;
   2035  1.1  riastrad 	}
   2036  1.1  riastrad 
   2037  1.1  riastrad 	/* The rest will be validated by intel_bios_is_valid_vbt() */
   2038  1.1  riastrad 	vbt = kmalloc(vbt_size, GFP_KERNEL);
   2039  1.1  riastrad 	if (!vbt)
   2040  1.1  riastrad 		goto err_unmap_oprom;
   2041  1.1  riastrad 
   2042  1.1  riastrad 	memcpy_fromio(vbt, p, vbt_size);
   2043  1.1  riastrad 
   2044  1.1  riastrad 	if (!intel_bios_is_valid_vbt(vbt, vbt_size))
   2045  1.1  riastrad 		goto err_free_vbt;
   2046  1.1  riastrad 
   2047  1.1  riastrad 	pci_unmap_rom(pdev, oprom);
   2048  1.1  riastrad 
   2049  1.1  riastrad 	return vbt;
   2050  1.1  riastrad 
   2051  1.1  riastrad err_free_vbt:
   2052  1.1  riastrad 	kfree(vbt);
   2053  1.1  riastrad err_unmap_oprom:
   2054  1.1  riastrad 	pci_unmap_rom(pdev, oprom);
   2055  1.1  riastrad 
   2056  1.1  riastrad 	return NULL;
   2057  1.1  riastrad }
   2058  1.1  riastrad 
   2059  1.2  riastrad #ifdef __NetBSD__
   2060  1.2  riastrad #  undef	__iomem
   2061  1.2  riastrad #  undef	ioread32
   2062  1.2  riastrad #endif
   2063  1.2  riastrad 
   2064  1.1  riastrad /**
   2065  1.1  riastrad  * intel_bios_init - find VBT and initialize settings from the BIOS
   2066  1.1  riastrad  * @dev_priv: i915 device instance
   2067  1.1  riastrad  *
   2068  1.1  riastrad  * Parse and initialize settings from the Video BIOS Tables (VBT). If the VBT
   2069  1.1  riastrad  * was not found in ACPI OpRegion, try to find it in PCI ROM first. Also
   2070  1.1  riastrad  * initialize some defaults if the VBT is not present at all.
   2071  1.1  riastrad  */
   2072  1.1  riastrad void intel_bios_init(struct drm_i915_private *dev_priv)
   2073  1.1  riastrad {
   2074  1.1  riastrad 	const struct vbt_header *vbt = dev_priv->opregion.vbt;
   2075  1.1  riastrad 	struct vbt_header *oprom_vbt = NULL;
   2076  1.1  riastrad 	const struct bdb_header *bdb;
   2077  1.1  riastrad 
   2078  1.1  riastrad 	INIT_LIST_HEAD(&dev_priv->vbt.display_devices);
   2079  1.1  riastrad 
   2080  1.1  riastrad 	if (!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv)) {
   2081  1.1  riastrad 		DRM_DEBUG_KMS("Skipping VBT init due to disabled display.\n");
   2082  1.1  riastrad 		return;
   2083  1.1  riastrad 	}
   2084  1.1  riastrad 
   2085  1.1  riastrad 	init_vbt_defaults(dev_priv);
   2086  1.1  riastrad 
   2087  1.1  riastrad 	/* If the OpRegion does not have VBT, look in PCI ROM. */
   2088  1.1  riastrad 	if (!vbt) {
   2089  1.1  riastrad 		oprom_vbt = oprom_get_vbt(dev_priv);
   2090  1.1  riastrad 		if (!oprom_vbt)
   2091  1.1  riastrad 			goto out;
   2092  1.1  riastrad 
   2093  1.1  riastrad 		vbt = oprom_vbt;
   2094  1.1  riastrad 
   2095  1.1  riastrad 		DRM_DEBUG_KMS("Found valid VBT in PCI ROM\n");
   2096  1.1  riastrad 	}
   2097  1.1  riastrad 
   2098  1.1  riastrad 	bdb = get_bdb_header(vbt);
   2099  1.1  riastrad 
   2100  1.1  riastrad 	DRM_DEBUG_KMS("VBT signature \"%.*s\", BDB version %d\n",
   2101  1.1  riastrad 		      (int)sizeof(vbt->signature), vbt->signature, bdb->version);
   2102  1.1  riastrad 
   2103  1.1  riastrad 	/* Grab useful general definitions */
   2104  1.1  riastrad 	parse_general_features(dev_priv, bdb);
   2105  1.1  riastrad 	parse_general_definitions(dev_priv, bdb);
   2106  1.1  riastrad 	parse_panel_options(dev_priv, bdb);
   2107  1.1  riastrad 	parse_panel_dtd(dev_priv, bdb);
   2108  1.1  riastrad 	parse_lfp_backlight(dev_priv, bdb);
   2109  1.1  riastrad 	parse_sdvo_panel_data(dev_priv, bdb);
   2110  1.1  riastrad 	parse_driver_features(dev_priv, bdb);
   2111  1.1  riastrad 	parse_power_conservation_features(dev_priv, bdb);
   2112  1.1  riastrad 	parse_edp(dev_priv, bdb);
   2113  1.1  riastrad 	parse_psr(dev_priv, bdb);
   2114  1.1  riastrad 	parse_mipi_config(dev_priv, bdb);
   2115  1.1  riastrad 	parse_mipi_sequence(dev_priv, bdb);
   2116  1.1  riastrad 
   2117  1.1  riastrad 	/* Depends on child device list */
   2118  1.1  riastrad 	parse_compression_parameters(dev_priv, bdb);
   2119  1.1  riastrad 
   2120  1.1  riastrad 	/* Further processing on pre-parsed data */
   2121  1.1  riastrad 	parse_sdvo_device_mapping(dev_priv, bdb->version);
   2122  1.1  riastrad 	parse_ddi_ports(dev_priv, bdb->version);
   2123  1.1  riastrad 
   2124  1.1  riastrad out:
   2125  1.1  riastrad 	if (!vbt) {
   2126  1.1  riastrad 		DRM_INFO("Failed to find VBIOS tables (VBT)\n");
   2127  1.1  riastrad 		init_vbt_missing_defaults(dev_priv);
   2128  1.1  riastrad 	}
   2129  1.1  riastrad 
   2130  1.1  riastrad 	kfree(oprom_vbt);
   2131  1.1  riastrad }
   2132  1.1  riastrad 
   2133  1.1  riastrad /**
   2134  1.1  riastrad  * intel_bios_driver_remove - Free any resources allocated by intel_bios_init()
   2135  1.1  riastrad  * @dev_priv: i915 device instance
   2136  1.1  riastrad  */
   2137  1.1  riastrad void intel_bios_driver_remove(struct drm_i915_private *dev_priv)
   2138  1.1  riastrad {
   2139  1.1  riastrad 	struct display_device_data *devdata, *n;
   2140  1.1  riastrad 
   2141  1.1  riastrad 	list_for_each_entry_safe(devdata, n, &dev_priv->vbt.display_devices, node) {
   2142  1.1  riastrad 		list_del(&devdata->node);
   2143  1.1  riastrad 		kfree(devdata->dsc);
   2144  1.1  riastrad 		kfree(devdata);
   2145  1.1  riastrad 	}
   2146  1.1  riastrad 
   2147  1.1  riastrad 	kfree(dev_priv->vbt.sdvo_lvds_vbt_mode);
   2148  1.1  riastrad 	dev_priv->vbt.sdvo_lvds_vbt_mode = NULL;
   2149  1.1  riastrad 	kfree(dev_priv->vbt.lfp_lvds_vbt_mode);
   2150  1.1  riastrad 	dev_priv->vbt.lfp_lvds_vbt_mode = NULL;
   2151  1.1  riastrad 	kfree(dev_priv->vbt.dsi.data);
   2152  1.1  riastrad 	dev_priv->vbt.dsi.data = NULL;
   2153  1.1  riastrad 	kfree(dev_priv->vbt.dsi.pps);
   2154  1.1  riastrad 	dev_priv->vbt.dsi.pps = NULL;
   2155  1.1  riastrad 	kfree(dev_priv->vbt.dsi.config);
   2156  1.1  riastrad 	dev_priv->vbt.dsi.config = NULL;
   2157  1.1  riastrad 	kfree(dev_priv->vbt.dsi.deassert_seq);
   2158  1.1  riastrad 	dev_priv->vbt.dsi.deassert_seq = NULL;
   2159  1.1  riastrad }
   2160  1.1  riastrad 
   2161  1.1  riastrad /**
   2162  1.1  riastrad  * intel_bios_is_tv_present - is integrated TV present in VBT
   2163  1.1  riastrad  * @dev_priv:	i915 device instance
   2164  1.1  riastrad  *
   2165  1.1  riastrad  * Return true if TV is present. If no child devices were parsed from VBT,
   2166  1.1  riastrad  * assume TV is present.
   2167  1.1  riastrad  */
   2168  1.1  riastrad bool intel_bios_is_tv_present(struct drm_i915_private *dev_priv)
   2169  1.1  riastrad {
   2170  1.1  riastrad 	const struct display_device_data *devdata;
   2171  1.1  riastrad 	const struct child_device_config *child;
   2172  1.1  riastrad 
   2173  1.1  riastrad 	if (!dev_priv->vbt.int_tv_support)
   2174  1.1  riastrad 		return false;
   2175  1.1  riastrad 
   2176  1.1  riastrad 	if (list_empty(&dev_priv->vbt.display_devices))
   2177  1.1  riastrad 		return true;
   2178  1.1  riastrad 
   2179  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node) {
   2180  1.1  riastrad 		child = &devdata->child;
   2181  1.1  riastrad 
   2182  1.1  riastrad 		/*
   2183  1.1  riastrad 		 * If the device type is not TV, continue.
   2184  1.1  riastrad 		 */
   2185  1.1  riastrad 		switch (child->device_type) {
   2186  1.1  riastrad 		case DEVICE_TYPE_INT_TV:
   2187  1.1  riastrad 		case DEVICE_TYPE_TV:
   2188  1.1  riastrad 		case DEVICE_TYPE_TV_SVIDEO_COMPOSITE:
   2189  1.1  riastrad 			break;
   2190  1.1  riastrad 		default:
   2191  1.1  riastrad 			continue;
   2192  1.1  riastrad 		}
   2193  1.1  riastrad 		/* Only when the addin_offset is non-zero, it is regarded
   2194  1.1  riastrad 		 * as present.
   2195  1.1  riastrad 		 */
   2196  1.1  riastrad 		if (child->addin_offset)
   2197  1.1  riastrad 			return true;
   2198  1.1  riastrad 	}
   2199  1.1  riastrad 
   2200  1.1  riastrad 	return false;
   2201  1.1  riastrad }
   2202  1.1  riastrad 
   2203  1.1  riastrad /**
   2204  1.1  riastrad  * intel_bios_is_lvds_present - is LVDS present in VBT
   2205  1.1  riastrad  * @dev_priv:	i915 device instance
   2206  1.1  riastrad  * @i2c_pin:	i2c pin for LVDS if present
   2207  1.1  riastrad  *
   2208  1.1  riastrad  * Return true if LVDS is present. If no child devices were parsed from VBT,
   2209  1.1  riastrad  * assume LVDS is present.
   2210  1.1  riastrad  */
   2211  1.1  riastrad bool intel_bios_is_lvds_present(struct drm_i915_private *dev_priv, u8 *i2c_pin)
   2212  1.1  riastrad {
   2213  1.1  riastrad 	const struct display_device_data *devdata;
   2214  1.1  riastrad 	const struct child_device_config *child;
   2215  1.1  riastrad 
   2216  1.1  riastrad 	if (list_empty(&dev_priv->vbt.display_devices))
   2217  1.1  riastrad 		return true;
   2218  1.1  riastrad 
   2219  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node) {
   2220  1.1  riastrad 		child = &devdata->child;
   2221  1.1  riastrad 
   2222  1.1  riastrad 		/* If the device type is not LFP, continue.
   2223  1.1  riastrad 		 * We have to check both the new identifiers as well as the
   2224  1.1  riastrad 		 * old for compatibility with some BIOSes.
   2225  1.1  riastrad 		 */
   2226  1.1  riastrad 		if (child->device_type != DEVICE_TYPE_INT_LFP &&
   2227  1.1  riastrad 		    child->device_type != DEVICE_TYPE_LFP)
   2228  1.1  riastrad 			continue;
   2229  1.1  riastrad 
   2230  1.1  riastrad 		if (intel_gmbus_is_valid_pin(dev_priv, child->i2c_pin))
   2231  1.1  riastrad 			*i2c_pin = child->i2c_pin;
   2232  1.1  riastrad 
   2233  1.1  riastrad 		/* However, we cannot trust the BIOS writers to populate
   2234  1.1  riastrad 		 * the VBT correctly.  Since LVDS requires additional
   2235  1.1  riastrad 		 * information from AIM blocks, a non-zero addin offset is
   2236  1.1  riastrad 		 * a good indicator that the LVDS is actually present.
   2237  1.1  riastrad 		 */
   2238  1.1  riastrad 		if (child->addin_offset)
   2239  1.1  riastrad 			return true;
   2240  1.1  riastrad 
   2241  1.1  riastrad 		/* But even then some BIOS writers perform some black magic
   2242  1.1  riastrad 		 * and instantiate the device without reference to any
   2243  1.1  riastrad 		 * additional data.  Trust that if the VBT was written into
   2244  1.1  riastrad 		 * the OpRegion then they have validated the LVDS's existence.
   2245  1.1  riastrad 		 */
   2246  1.1  riastrad 		if (dev_priv->opregion.vbt)
   2247  1.1  riastrad 			return true;
   2248  1.1  riastrad 	}
   2249  1.1  riastrad 
   2250  1.1  riastrad 	return false;
   2251  1.1  riastrad }
   2252  1.1  riastrad 
   2253  1.1  riastrad /**
   2254  1.1  riastrad  * intel_bios_is_port_present - is the specified digital port present
   2255  1.1  riastrad  * @dev_priv:	i915 device instance
   2256  1.1  riastrad  * @port:	port to check
   2257  1.1  riastrad  *
   2258  1.1  riastrad  * Return true if the device in %port is present.
   2259  1.1  riastrad  */
   2260  1.1  riastrad bool intel_bios_is_port_present(struct drm_i915_private *dev_priv, enum port port)
   2261  1.1  riastrad {
   2262  1.1  riastrad 	const struct display_device_data *devdata;
   2263  1.1  riastrad 	const struct child_device_config *child;
   2264  1.1  riastrad 	static const struct {
   2265  1.1  riastrad 		u16 dp, hdmi;
   2266  1.1  riastrad 	} port_mapping[] = {
   2267  1.1  riastrad 		[PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
   2268  1.1  riastrad 		[PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
   2269  1.1  riastrad 		[PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
   2270  1.1  riastrad 		[PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
   2271  1.1  riastrad 		[PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
   2272  1.1  riastrad 	};
   2273  1.1  riastrad 
   2274  1.1  riastrad 	if (HAS_DDI(dev_priv)) {
   2275  1.1  riastrad 		const struct ddi_vbt_port_info *port_info =
   2276  1.1  riastrad 			&dev_priv->vbt.ddi_port_info[port];
   2277  1.1  riastrad 
   2278  1.1  riastrad 		return port_info->supports_dp ||
   2279  1.1  riastrad 		       port_info->supports_dvi ||
   2280  1.1  riastrad 		       port_info->supports_hdmi;
   2281  1.1  riastrad 	}
   2282  1.1  riastrad 
   2283  1.1  riastrad 	/* FIXME maybe deal with port A as well? */
   2284  1.1  riastrad 	if (WARN_ON(port == PORT_A) || port >= ARRAY_SIZE(port_mapping))
   2285  1.1  riastrad 		return false;
   2286  1.1  riastrad 
   2287  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node) {
   2288  1.1  riastrad 		child = &devdata->child;
   2289  1.1  riastrad 
   2290  1.1  riastrad 		if ((child->dvo_port == port_mapping[port].dp ||
   2291  1.1  riastrad 		     child->dvo_port == port_mapping[port].hdmi) &&
   2292  1.1  riastrad 		    (child->device_type & (DEVICE_TYPE_TMDS_DVI_SIGNALING |
   2293  1.1  riastrad 					   DEVICE_TYPE_DISPLAYPORT_OUTPUT)))
   2294  1.1  riastrad 			return true;
   2295  1.1  riastrad 	}
   2296  1.1  riastrad 
   2297  1.1  riastrad 	return false;
   2298  1.1  riastrad }
   2299  1.1  riastrad 
   2300  1.1  riastrad /**
   2301  1.1  riastrad  * intel_bios_is_port_edp - is the device in given port eDP
   2302  1.1  riastrad  * @dev_priv:	i915 device instance
   2303  1.1  riastrad  * @port:	port to check
   2304  1.1  riastrad  *
   2305  1.1  riastrad  * Return true if the device in %port is eDP.
   2306  1.1  riastrad  */
   2307  1.1  riastrad bool intel_bios_is_port_edp(struct drm_i915_private *dev_priv, enum port port)
   2308  1.1  riastrad {
   2309  1.1  riastrad 	const struct display_device_data *devdata;
   2310  1.1  riastrad 	const struct child_device_config *child;
   2311  1.1  riastrad 	static const short port_mapping[] = {
   2312  1.1  riastrad 		[PORT_B] = DVO_PORT_DPB,
   2313  1.1  riastrad 		[PORT_C] = DVO_PORT_DPC,
   2314  1.1  riastrad 		[PORT_D] = DVO_PORT_DPD,
   2315  1.1  riastrad 		[PORT_E] = DVO_PORT_DPE,
   2316  1.1  riastrad 		[PORT_F] = DVO_PORT_DPF,
   2317  1.1  riastrad 	};
   2318  1.1  riastrad 
   2319  1.1  riastrad 	if (HAS_DDI(dev_priv))
   2320  1.1  riastrad 		return dev_priv->vbt.ddi_port_info[port].supports_edp;
   2321  1.1  riastrad 
   2322  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node) {
   2323  1.1  riastrad 		child = &devdata->child;
   2324  1.1  riastrad 
   2325  1.1  riastrad 		if (child->dvo_port == port_mapping[port] &&
   2326  1.1  riastrad 		    (child->device_type & DEVICE_TYPE_eDP_BITS) ==
   2327  1.1  riastrad 		    (DEVICE_TYPE_eDP & DEVICE_TYPE_eDP_BITS))
   2328  1.1  riastrad 			return true;
   2329  1.1  riastrad 	}
   2330  1.1  riastrad 
   2331  1.1  riastrad 	return false;
   2332  1.1  riastrad }
   2333  1.1  riastrad 
   2334  1.1  riastrad static bool child_dev_is_dp_dual_mode(const struct child_device_config *child,
   2335  1.1  riastrad 				      enum port port)
   2336  1.1  riastrad {
   2337  1.1  riastrad 	static const struct {
   2338  1.1  riastrad 		u16 dp, hdmi;
   2339  1.1  riastrad 	} port_mapping[] = {
   2340  1.1  riastrad 		/*
   2341  1.1  riastrad 		 * Buggy VBTs may declare DP ports as having
   2342  1.1  riastrad 		 * HDMI type dvo_port :( So let's check both.
   2343  1.1  riastrad 		 */
   2344  1.1  riastrad 		[PORT_B] = { DVO_PORT_DPB, DVO_PORT_HDMIB, },
   2345  1.1  riastrad 		[PORT_C] = { DVO_PORT_DPC, DVO_PORT_HDMIC, },
   2346  1.1  riastrad 		[PORT_D] = { DVO_PORT_DPD, DVO_PORT_HDMID, },
   2347  1.1  riastrad 		[PORT_E] = { DVO_PORT_DPE, DVO_PORT_HDMIE, },
   2348  1.1  riastrad 		[PORT_F] = { DVO_PORT_DPF, DVO_PORT_HDMIF, },
   2349  1.1  riastrad 	};
   2350  1.1  riastrad 
   2351  1.1  riastrad 	if (port == PORT_A || port >= ARRAY_SIZE(port_mapping))
   2352  1.1  riastrad 		return false;
   2353  1.1  riastrad 
   2354  1.1  riastrad 	if ((child->device_type & DEVICE_TYPE_DP_DUAL_MODE_BITS) !=
   2355  1.1  riastrad 	    (DEVICE_TYPE_DP_DUAL_MODE & DEVICE_TYPE_DP_DUAL_MODE_BITS))
   2356  1.1  riastrad 		return false;
   2357  1.1  riastrad 
   2358  1.1  riastrad 	if (child->dvo_port == port_mapping[port].dp)
   2359  1.1  riastrad 		return true;
   2360  1.1  riastrad 
   2361  1.1  riastrad 	/* Only accept a HDMI dvo_port as DP++ if it has an AUX channel */
   2362  1.1  riastrad 	if (child->dvo_port == port_mapping[port].hdmi &&
   2363  1.1  riastrad 	    child->aux_channel != 0)
   2364  1.1  riastrad 		return true;
   2365  1.1  riastrad 
   2366  1.1  riastrad 	return false;
   2367  1.1  riastrad }
   2368  1.1  riastrad 
   2369  1.1  riastrad bool intel_bios_is_port_dp_dual_mode(struct drm_i915_private *dev_priv,
   2370  1.1  riastrad 				     enum port port)
   2371  1.1  riastrad {
   2372  1.1  riastrad 	const struct display_device_data *devdata;
   2373  1.1  riastrad 
   2374  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node) {
   2375  1.1  riastrad 		if (child_dev_is_dp_dual_mode(&devdata->child, port))
   2376  1.1  riastrad 			return true;
   2377  1.1  riastrad 	}
   2378  1.1  riastrad 
   2379  1.1  riastrad 	return false;
   2380  1.1  riastrad }
   2381  1.1  riastrad 
   2382  1.1  riastrad /**
   2383  1.1  riastrad  * intel_bios_is_dsi_present - is DSI present in VBT
   2384  1.1  riastrad  * @dev_priv:	i915 device instance
   2385  1.1  riastrad  * @port:	port for DSI if present
   2386  1.1  riastrad  *
   2387  1.1  riastrad  * Return true if DSI is present, and return the port in %port.
   2388  1.1  riastrad  */
   2389  1.1  riastrad bool intel_bios_is_dsi_present(struct drm_i915_private *dev_priv,
   2390  1.1  riastrad 			       enum port *port)
   2391  1.1  riastrad {
   2392  1.1  riastrad 	const struct display_device_data *devdata;
   2393  1.1  riastrad 	const struct child_device_config *child;
   2394  1.1  riastrad 	u8 dvo_port;
   2395  1.1  riastrad 
   2396  1.1  riastrad 	list_for_each_entry(devdata, &dev_priv->vbt.display_devices, node) {
   2397  1.1  riastrad 		child = &devdata->child;
   2398  1.1  riastrad 
   2399  1.1  riastrad 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
   2400  1.1  riastrad 			continue;
   2401  1.1  riastrad 
   2402  1.1  riastrad 		dvo_port = child->dvo_port;
   2403  1.1  riastrad 
   2404  1.1  riastrad 		if (dvo_port == DVO_PORT_MIPIA ||
   2405  1.1  riastrad 		    (dvo_port == DVO_PORT_MIPIB && INTEL_GEN(dev_priv) >= 11) ||
   2406  1.1  riastrad 		    (dvo_port == DVO_PORT_MIPIC && INTEL_GEN(dev_priv) < 11)) {
   2407  1.1  riastrad 			if (port)
   2408  1.1  riastrad 				*port = dvo_port - DVO_PORT_MIPIA;
   2409  1.1  riastrad 			return true;
   2410  1.1  riastrad 		} else if (dvo_port == DVO_PORT_MIPIB ||
   2411  1.1  riastrad 			   dvo_port == DVO_PORT_MIPIC ||
   2412  1.1  riastrad 			   dvo_port == DVO_PORT_MIPID) {
   2413  1.1  riastrad 			DRM_DEBUG_KMS("VBT has unsupported DSI port %c\n",
   2414  1.1  riastrad 				      port_name(dvo_port - DVO_PORT_MIPIA));
   2415  1.1  riastrad 		}
   2416  1.1  riastrad 	}
   2417  1.1  riastrad 
   2418  1.1  riastrad 	return false;
   2419  1.1  riastrad }
   2420  1.1  riastrad 
   2421  1.1  riastrad static void fill_dsc(struct intel_crtc_state *crtc_state,
   2422  1.1  riastrad 		     struct dsc_compression_parameters_entry *dsc,
   2423  1.1  riastrad 		     int dsc_max_bpc)
   2424  1.1  riastrad {
   2425  1.1  riastrad 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
   2426  1.1  riastrad 	int bpc = 8;
   2427  1.1  riastrad 
   2428  1.1  riastrad 	vdsc_cfg->dsc_version_major = dsc->version_major;
   2429  1.1  riastrad 	vdsc_cfg->dsc_version_minor = dsc->version_minor;
   2430  1.1  riastrad 
   2431  1.1  riastrad 	if (dsc->support_12bpc && dsc_max_bpc >= 12)
   2432  1.1  riastrad 		bpc = 12;
   2433  1.1  riastrad 	else if (dsc->support_10bpc && dsc_max_bpc >= 10)
   2434  1.1  riastrad 		bpc = 10;
   2435  1.1  riastrad 	else if (dsc->support_8bpc && dsc_max_bpc >= 8)
   2436  1.1  riastrad 		bpc = 8;
   2437  1.1  riastrad 	else
   2438  1.1  riastrad 		DRM_DEBUG_KMS("VBT: Unsupported BPC %d for DCS\n",
   2439  1.1  riastrad 			      dsc_max_bpc);
   2440  1.1  riastrad 
   2441  1.1  riastrad 	crtc_state->pipe_bpp = bpc * 3;
   2442  1.1  riastrad 
   2443  1.1  riastrad 	crtc_state->dsc.compressed_bpp = min(crtc_state->pipe_bpp,
   2444  1.1  riastrad 					     VBT_DSC_MAX_BPP(dsc->max_bpp));
   2445  1.1  riastrad 
   2446  1.1  riastrad 	/*
   2447  1.1  riastrad 	 * FIXME: This is ugly, and slice count should take DSC engine
   2448  1.1  riastrad 	 * throughput etc. into account.
   2449  1.1  riastrad 	 *
   2450  1.1  riastrad 	 * Also, per spec DSI supports 1, 2, 3 or 4 horizontal slices.
   2451  1.1  riastrad 	 */
   2452  1.1  riastrad 	if (dsc->slices_per_line & BIT(2)) {
   2453  1.1  riastrad 		crtc_state->dsc.slice_count = 4;
   2454  1.1  riastrad 	} else if (dsc->slices_per_line & BIT(1)) {
   2455  1.1  riastrad 		crtc_state->dsc.slice_count = 2;
   2456  1.1  riastrad 	} else {
   2457  1.1  riastrad 		/* FIXME */
   2458  1.1  riastrad 		if (!(dsc->slices_per_line & BIT(0)))
   2459  1.1  riastrad 			DRM_DEBUG_KMS("VBT: Unsupported DSC slice count for DSI\n");
   2460  1.1  riastrad 
   2461  1.1  riastrad 		crtc_state->dsc.slice_count = 1;
   2462  1.1  riastrad 	}
   2463  1.1  riastrad 
   2464  1.1  riastrad 	if (crtc_state->hw.adjusted_mode.crtc_hdisplay %
   2465  1.1  riastrad 	    crtc_state->dsc.slice_count != 0)
   2466  1.1  riastrad 		DRM_DEBUG_KMS("VBT: DSC hdisplay %d not divisible by slice count %d\n",
   2467  1.1  riastrad 			      crtc_state->hw.adjusted_mode.crtc_hdisplay,
   2468  1.1  riastrad 			      crtc_state->dsc.slice_count);
   2469  1.1  riastrad 
   2470  1.1  riastrad 	/*
   2471  1.1  riastrad 	 * FIXME: Use VBT rc_buffer_block_size and rc_buffer_size for the
   2472  1.1  riastrad 	 * implementation specific physical rate buffer size. Currently we use
   2473  1.1  riastrad 	 * the required rate buffer model size calculated in
   2474  1.1  riastrad 	 * drm_dsc_compute_rc_parameters() according to VESA DSC Annex E.
   2475  1.1  riastrad 	 *
   2476  1.1  riastrad 	 * The VBT rc_buffer_block_size and rc_buffer_size definitions
   2477  1.1  riastrad 	 * correspond to DP 1.4 DPCD offsets 0x62 and 0x63. The DP DSC
   2478  1.1  riastrad 	 * implementation should also use the DPCD (or perhaps VBT for eDP)
   2479  1.1  riastrad 	 * provided value for the buffer size.
   2480  1.1  riastrad 	 */
   2481  1.1  riastrad 
   2482  1.1  riastrad 	/* FIXME: DSI spec says bpc + 1 for this one */
   2483  1.1  riastrad 	vdsc_cfg->line_buf_depth = VBT_DSC_LINE_BUFFER_DEPTH(dsc->line_buffer_depth);
   2484  1.1  riastrad 
   2485  1.1  riastrad 	vdsc_cfg->block_pred_enable = dsc->block_prediction_enable;
   2486  1.1  riastrad 
   2487  1.1  riastrad 	vdsc_cfg->slice_height = dsc->slice_height;
   2488  1.1  riastrad }
   2489  1.1  riastrad 
   2490  1.1  riastrad /* FIXME: initially DSI specific */
   2491  1.1  riastrad bool intel_bios_get_dsc_params(struct intel_encoder *encoder,
   2492  1.1  riastrad 			       struct intel_crtc_state *crtc_state,
   2493  1.1  riastrad 			       int dsc_max_bpc)
   2494  1.1  riastrad {
   2495  1.1  riastrad 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
   2496  1.1  riastrad 	const struct display_device_data *devdata;
   2497  1.1  riastrad 	const struct child_device_config *child;
   2498  1.1  riastrad 
   2499  1.1  riastrad 	list_for_each_entry(devdata, &i915->vbt.display_devices, node) {
   2500  1.1  riastrad 		child = &devdata->child;
   2501  1.1  riastrad 
   2502  1.1  riastrad 		if (!(child->device_type & DEVICE_TYPE_MIPI_OUTPUT))
   2503  1.1  riastrad 			continue;
   2504  1.1  riastrad 
   2505  1.1  riastrad 		if (child->dvo_port - DVO_PORT_MIPIA == encoder->port) {
   2506  1.1  riastrad 			if (!devdata->dsc)
   2507  1.1  riastrad 				return false;
   2508  1.1  riastrad 
   2509  1.1  riastrad 			if (crtc_state)
   2510  1.1  riastrad 				fill_dsc(crtc_state, devdata->dsc, dsc_max_bpc);
   2511  1.1  riastrad 
   2512  1.1  riastrad 			return true;
   2513  1.1  riastrad 		}
   2514  1.1  riastrad 	}
   2515  1.1  riastrad 
   2516  1.1  riastrad 	return false;
   2517  1.1  riastrad }
   2518  1.1  riastrad 
   2519  1.1  riastrad /**
   2520  1.1  riastrad  * intel_bios_is_port_hpd_inverted - is HPD inverted for %port
   2521  1.1  riastrad  * @i915:	i915 device instance
   2522  1.1  riastrad  * @port:	port to check
   2523  1.1  riastrad  *
   2524  1.1  riastrad  * Return true if HPD should be inverted for %port.
   2525  1.1  riastrad  */
   2526  1.1  riastrad bool
   2527  1.1  riastrad intel_bios_is_port_hpd_inverted(const struct drm_i915_private *i915,
   2528  1.1  riastrad 				enum port port)
   2529  1.1  riastrad {
   2530  1.1  riastrad 	const struct child_device_config *child =
   2531  1.1  riastrad 		i915->vbt.ddi_port_info[port].child;
   2532  1.1  riastrad 
   2533  1.1  riastrad 	if (WARN_ON_ONCE(!IS_GEN9_LP(i915)))
   2534  1.1  riastrad 		return false;
   2535  1.1  riastrad 
   2536  1.1  riastrad 	return child && child->hpd_invert;
   2537  1.1  riastrad }
   2538  1.1  riastrad 
   2539  1.1  riastrad /**
   2540  1.1  riastrad  * intel_bios_is_lspcon_present - if LSPCON is attached on %port
   2541  1.1  riastrad  * @i915:	i915 device instance
   2542  1.1  riastrad  * @port:	port to check
   2543  1.1  riastrad  *
   2544  1.1  riastrad  * Return true if LSPCON is present on this port
   2545  1.1  riastrad  */
   2546  1.1  riastrad bool
   2547  1.1  riastrad intel_bios_is_lspcon_present(const struct drm_i915_private *i915,
   2548  1.1  riastrad 			     enum port port)
   2549  1.1  riastrad {
   2550  1.1  riastrad 	const struct child_device_config *child =
   2551  1.1  riastrad 		i915->vbt.ddi_port_info[port].child;
   2552  1.1  riastrad 
   2553  1.1  riastrad 	return HAS_LSPCON(i915) && child && child->lspcon;
   2554  1.1  riastrad }
   2555  1.1  riastrad 
   2556  1.1  riastrad enum aux_ch intel_bios_port_aux_ch(struct drm_i915_private *dev_priv,
   2557  1.1  riastrad 				   enum port port)
   2558  1.1  riastrad {
   2559  1.1  riastrad 	const struct ddi_vbt_port_info *info =
   2560  1.1  riastrad 		&dev_priv->vbt.ddi_port_info[port];
   2561  1.1  riastrad 	enum aux_ch aux_ch;
   2562  1.1  riastrad 
   2563  1.1  riastrad 	if (!info->alternate_aux_channel) {
   2564  1.1  riastrad 		aux_ch = (enum aux_ch)port;
   2565  1.1  riastrad 
   2566  1.1  riastrad 		DRM_DEBUG_KMS("using AUX %c for port %c (platform default)\n",
   2567  1.1  riastrad 			      aux_ch_name(aux_ch), port_name(port));
   2568  1.1  riastrad 		return aux_ch;
   2569  1.1  riastrad 	}
   2570  1.1  riastrad 
   2571  1.1  riastrad 	switch (info->alternate_aux_channel) {
   2572  1.1  riastrad 	case DP_AUX_A:
   2573  1.1  riastrad 		aux_ch = AUX_CH_A;
   2574  1.1  riastrad 		break;
   2575  1.1  riastrad 	case DP_AUX_B:
   2576  1.1  riastrad 		aux_ch = AUX_CH_B;
   2577  1.1  riastrad 		break;
   2578  1.1  riastrad 	case DP_AUX_C:
   2579  1.1  riastrad 		aux_ch = AUX_CH_C;
   2580  1.1  riastrad 		break;
   2581  1.1  riastrad 	case DP_AUX_D:
   2582  1.1  riastrad 		aux_ch = AUX_CH_D;
   2583  1.1  riastrad 		break;
   2584  1.1  riastrad 	case DP_AUX_E:
   2585  1.1  riastrad 		aux_ch = AUX_CH_E;
   2586  1.1  riastrad 		break;
   2587  1.1  riastrad 	case DP_AUX_F:
   2588  1.1  riastrad 		aux_ch = AUX_CH_F;
   2589  1.1  riastrad 		break;
   2590  1.1  riastrad 	case DP_AUX_G:
   2591  1.1  riastrad 		aux_ch = AUX_CH_G;
   2592  1.1  riastrad 		break;
   2593  1.1  riastrad 	default:
   2594  1.1  riastrad 		MISSING_CASE(info->alternate_aux_channel);
   2595  1.1  riastrad 		aux_ch = AUX_CH_A;
   2596  1.1  riastrad 		break;
   2597  1.1  riastrad 	}
   2598  1.1  riastrad 
   2599  1.1  riastrad 	DRM_DEBUG_KMS("using AUX %c for port %c (VBT)\n",
   2600  1.1  riastrad 		      aux_ch_name(aux_ch), port_name(port));
   2601  1.1  riastrad 
   2602  1.1  riastrad 	return aux_ch;
   2603  1.1  riastrad }
   2604