Home | History | Annotate | Line # | Download | only in nouveau
      1  1.6  riastrad /*	$NetBSD: nouveau_bios.c,v 1.6 2021/12/19 11:34:44 riastradh Exp $	*/
      2  1.2  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright 2005-2006 Erik Waling
      5  1.1  riastrad  * Copyright 2006 Stephane Marchesin
      6  1.1  riastrad  * Copyright 2007-2009 Stuart Bennett
      7  1.1  riastrad  *
      8  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      9  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     10  1.1  riastrad  * to deal in the Software without restriction, including without limitation
     11  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     12  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     13  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     14  1.1  riastrad  *
     15  1.1  riastrad  * The above copyright notice and this permission notice shall be included in
     16  1.1  riastrad  * all copies or substantial portions of the Software.
     17  1.1  riastrad  *
     18  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  1.1  riastrad  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
     22  1.1  riastrad  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
     23  1.1  riastrad  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     24  1.1  riastrad  * SOFTWARE.
     25  1.1  riastrad  */
     26  1.1  riastrad 
     27  1.2  riastrad #include <sys/cdefs.h>
     28  1.6  riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_bios.c,v 1.6 2021/12/19 11:34:44 riastradh Exp $");
     29  1.1  riastrad 
     30  1.5  riastrad #include "nouveau_drv.h"
     31  1.1  riastrad #include "nouveau_reg.h"
     32  1.1  riastrad #include "dispnv04/hw.h"
     33  1.1  riastrad #include "nouveau_encoder.h"
     34  1.1  riastrad 
     35  1.1  riastrad #include <linux/io-mapping.h>
     36  1.1  riastrad #include <linux/firmware.h>
     37  1.1  riastrad 
     38  1.1  riastrad /* these defines are made up */
     39  1.1  riastrad #define NV_CIO_CRE_44_HEADA 0x0
     40  1.1  riastrad #define NV_CIO_CRE_44_HEADB 0x3
     41  1.1  riastrad #define FEATURE_MOBILE 0x10	/* also FEATURE_QUADRO for BMP */
     42  1.1  riastrad 
     43  1.1  riastrad #define EDID1_LEN 128
     44  1.1  riastrad 
     45  1.1  riastrad #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
     46  1.1  riastrad #define LOG_OLD_VALUE(x)
     47  1.1  riastrad 
     48  1.1  riastrad struct init_exec {
     49  1.1  riastrad 	bool execute;
     50  1.1  riastrad 	bool repeat;
     51  1.1  riastrad };
     52  1.1  riastrad 
     53  1.1  riastrad static bool nv_cksum(const uint8_t *data, unsigned int length)
     54  1.1  riastrad {
     55  1.1  riastrad 	/*
     56  1.1  riastrad 	 * There's a few checksums in the BIOS, so here's a generic checking
     57  1.1  riastrad 	 * function.
     58  1.1  riastrad 	 */
     59  1.1  riastrad 	int i;
     60  1.1  riastrad 	uint8_t sum = 0;
     61  1.1  riastrad 
     62  1.1  riastrad 	for (i = 0; i < length; i++)
     63  1.1  riastrad 		sum += data[i];
     64  1.1  riastrad 
     65  1.1  riastrad 	if (sum)
     66  1.1  riastrad 		return true;
     67  1.1  riastrad 
     68  1.1  riastrad 	return false;
     69  1.1  riastrad }
     70  1.1  riastrad 
     71  1.1  riastrad static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
     72  1.1  riastrad {
     73  1.1  riastrad 	int compare_record_len, i = 0;
     74  1.1  riastrad 	uint16_t compareclk, scriptptr = 0;
     75  1.1  riastrad 
     76  1.1  riastrad 	if (bios->major_version < 5) /* pre BIT */
     77  1.1  riastrad 		compare_record_len = 3;
     78  1.1  riastrad 	else
     79  1.1  riastrad 		compare_record_len = 4;
     80  1.1  riastrad 
     81  1.1  riastrad 	do {
     82  1.1  riastrad 		compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
     83  1.1  riastrad 		if (pxclk >= compareclk * 10) {
     84  1.1  riastrad 			if (bios->major_version < 5) {
     85  1.1  riastrad 				uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
     86  1.1  riastrad 				scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
     87  1.1  riastrad 			} else
     88  1.1  riastrad 				scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
     89  1.1  riastrad 			break;
     90  1.1  riastrad 		}
     91  1.1  riastrad 		i++;
     92  1.1  riastrad 	} while (compareclk);
     93  1.1  riastrad 
     94  1.1  riastrad 	return scriptptr;
     95  1.1  riastrad }
     96  1.1  riastrad 
     97  1.1  riastrad static void
     98  1.1  riastrad run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
     99  1.1  riastrad 		      struct dcb_output *dcbent, int head, bool dl)
    100  1.1  riastrad {
    101  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    102  1.1  riastrad 
    103  1.1  riastrad 	NV_INFO(drm, "0x%04X: Parsing digital output script table\n",
    104  1.1  riastrad 		 scriptptr);
    105  1.1  riastrad 	NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, head ? NV_CIO_CRE_44_HEADB :
    106  1.1  riastrad 					         NV_CIO_CRE_44_HEADA);
    107  1.1  riastrad 	nouveau_bios_run_init_table(dev, scriptptr, dcbent, head);
    108  1.1  riastrad 
    109  1.1  riastrad 	nv04_dfp_bind_head(dev, dcbent, head, dl);
    110  1.1  riastrad }
    111  1.1  riastrad 
    112  1.1  riastrad static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script)
    113  1.1  riastrad {
    114  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    115  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
    116  1.1  riastrad 	uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & DCB_OUTPUT_C ? 1 : 0);
    117  1.1  riastrad 	uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
    118  1.1  riastrad 
    119  1.1  riastrad 	if (!bios->fp.xlated_entry || !sub || !scriptofs)
    120  1.1  riastrad 		return -EINVAL;
    121  1.1  riastrad 
    122  1.1  riastrad 	run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
    123  1.1  riastrad 
    124  1.1  riastrad 	if (script == LVDS_PANEL_OFF) {
    125  1.1  riastrad 		/* off-on delay in ms */
    126  1.1  riastrad 		mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
    127  1.1  riastrad 	}
    128  1.1  riastrad #ifdef __powerpc__
    129  1.1  riastrad 	/* Powerbook specific quirks */
    130  1.1  riastrad 	if (script == LVDS_RESET &&
    131  1.1  riastrad 	    (dev->pdev->device == 0x0179 || dev->pdev->device == 0x0189 ||
    132  1.1  riastrad 	     dev->pdev->device == 0x0329))
    133  1.1  riastrad 		nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
    134  1.1  riastrad #endif
    135  1.1  riastrad 
    136  1.1  riastrad 	return 0;
    137  1.1  riastrad }
    138  1.1  riastrad 
    139  1.1  riastrad static int run_lvds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
    140  1.1  riastrad {
    141  1.1  riastrad 	/*
    142  1.1  riastrad 	 * The BIT LVDS table's header has the information to setup the
    143  1.1  riastrad 	 * necessary registers. Following the standard 4 byte header are:
    144  1.1  riastrad 	 * A bitmask byte and a dual-link transition pxclk value for use in
    145  1.1  riastrad 	 * selecting the init script when not using straps; 4 script pointers
    146  1.1  riastrad 	 * for panel power, selected by output and on/off; and 8 table pointers
    147  1.1  riastrad 	 * for panel init, the needed one determined by output, and bits in the
    148  1.1  riastrad 	 * conf byte. These tables are similar to the TMDS tables, consisting
    149  1.1  riastrad 	 * of a list of pxclks and script pointers.
    150  1.1  riastrad 	 */
    151  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    152  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
    153  1.1  riastrad 	unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
    154  1.1  riastrad 	uint16_t scriptptr = 0, clktable;
    155  1.1  riastrad 
    156  1.1  riastrad 	/*
    157  1.1  riastrad 	 * For now we assume version 3.0 table - g80 support will need some
    158  1.1  riastrad 	 * changes
    159  1.1  riastrad 	 */
    160  1.1  riastrad 
    161  1.1  riastrad 	switch (script) {
    162  1.1  riastrad 	case LVDS_INIT:
    163  1.1  riastrad 		return -ENOSYS;
    164  1.1  riastrad 	case LVDS_BACKLIGHT_ON:
    165  1.1  riastrad 	case LVDS_PANEL_ON:
    166  1.1  riastrad 		scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
    167  1.1  riastrad 		break;
    168  1.1  riastrad 	case LVDS_BACKLIGHT_OFF:
    169  1.1  riastrad 	case LVDS_PANEL_OFF:
    170  1.1  riastrad 		scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
    171  1.1  riastrad 		break;
    172  1.1  riastrad 	case LVDS_RESET:
    173  1.1  riastrad 		clktable = bios->fp.lvdsmanufacturerpointer + 15;
    174  1.1  riastrad 		if (dcbent->or == 4)
    175  1.1  riastrad 			clktable += 8;
    176  1.1  riastrad 
    177  1.1  riastrad 		if (dcbent->lvdsconf.use_straps_for_mode) {
    178  1.1  riastrad 			if (bios->fp.dual_link)
    179  1.1  riastrad 				clktable += 4;
    180  1.1  riastrad 			if (bios->fp.if_is_24bit)
    181  1.1  riastrad 				clktable += 2;
    182  1.1  riastrad 		} else {
    183  1.1  riastrad 			/* using EDID */
    184  1.1  riastrad 			int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
    185  1.1  riastrad 
    186  1.1  riastrad 			if (bios->fp.dual_link) {
    187  1.1  riastrad 				clktable += 4;
    188  1.1  riastrad 				cmpval_24bit <<= 1;
    189  1.1  riastrad 			}
    190  1.1  riastrad 
    191  1.1  riastrad 			if (bios->fp.strapless_is_24bit & cmpval_24bit)
    192  1.1  riastrad 				clktable += 2;
    193  1.1  riastrad 		}
    194  1.1  riastrad 
    195  1.1  riastrad 		clktable = ROM16(bios->data[clktable]);
    196  1.1  riastrad 		if (!clktable) {
    197  1.1  riastrad 			NV_ERROR(drm, "Pixel clock comparison table not found\n");
    198  1.1  riastrad 			return -ENOENT;
    199  1.1  riastrad 		}
    200  1.1  riastrad 		scriptptr = clkcmptable(bios, clktable, pxclk);
    201  1.1  riastrad 	}
    202  1.1  riastrad 
    203  1.1  riastrad 	if (!scriptptr) {
    204  1.1  riastrad 		NV_ERROR(drm, "LVDS output init script not found\n");
    205  1.1  riastrad 		return -ENOENT;
    206  1.1  riastrad 	}
    207  1.1  riastrad 	run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
    208  1.1  riastrad 
    209  1.1  riastrad 	return 0;
    210  1.1  riastrad }
    211  1.1  riastrad 
    212  1.1  riastrad int call_lvds_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
    213  1.1  riastrad {
    214  1.1  riastrad 	/*
    215  1.1  riastrad 	 * LVDS operations are multiplexed in an effort to present a single API
    216  1.1  riastrad 	 * which works with two vastly differing underlying structures.
    217  1.1  riastrad 	 * This acts as the demux
    218  1.1  riastrad 	 */
    219  1.1  riastrad 
    220  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    221  1.5  riastrad 	struct nvif_object *device = &drm->client.device.object;
    222  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
    223  1.1  riastrad 	uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
    224  1.1  riastrad 	uint32_t sel_clk_binding, sel_clk;
    225  1.1  riastrad 	int ret;
    226  1.1  riastrad 
    227  1.1  riastrad 	if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
    228  1.1  riastrad 	    (lvds_ver >= 0x30 && script == LVDS_INIT))
    229  1.1  riastrad 		return 0;
    230  1.1  riastrad 
    231  1.1  riastrad 	if (!bios->fp.lvds_init_run) {
    232  1.1  riastrad 		bios->fp.lvds_init_run = true;
    233  1.1  riastrad 		call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
    234  1.1  riastrad 	}
    235  1.1  riastrad 
    236  1.1  riastrad 	if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
    237  1.1  riastrad 		call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
    238  1.1  riastrad 	if (script == LVDS_RESET && bios->fp.power_off_for_reset)
    239  1.1  riastrad 		call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
    240  1.1  riastrad 
    241  1.1  riastrad 	NV_INFO(drm, "Calling LVDS script %d:\n", script);
    242  1.1  riastrad 
    243  1.1  riastrad 	/* don't let script change pll->head binding */
    244  1.3  riastrad 	sel_clk_binding = nvif_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
    245  1.1  riastrad 
    246  1.1  riastrad 	if (lvds_ver < 0x30)
    247  1.1  riastrad 		ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
    248  1.1  riastrad 	else
    249  1.1  riastrad 		ret = run_lvds_table(dev, dcbent, head, script, pxclk);
    250  1.1  riastrad 
    251  1.1  riastrad 	bios->fp.last_script_invoc = (script << 1 | head);
    252  1.1  riastrad 
    253  1.1  riastrad 	sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
    254  1.1  riastrad 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
    255  1.1  riastrad 	/* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
    256  1.3  riastrad 	nvif_wr32(device, NV_PBUS_POWERCTRL_2, 0);
    257  1.1  riastrad 
    258  1.1  riastrad 	return ret;
    259  1.1  riastrad }
    260  1.1  riastrad 
    261  1.1  riastrad struct lvdstableheader {
    262  1.1  riastrad 	uint8_t lvds_ver, headerlen, recordlen;
    263  1.1  riastrad };
    264  1.1  riastrad 
    265  1.1  riastrad static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
    266  1.1  riastrad {
    267  1.1  riastrad 	/*
    268  1.1  riastrad 	 * BMP version (0xa) LVDS table has a simple header of version and
    269  1.1  riastrad 	 * record length. The BIT LVDS table has the typical BIT table header:
    270  1.1  riastrad 	 * version byte, header length byte, record length byte, and a byte for
    271  1.1  riastrad 	 * the maximum number of records that can be held in the table.
    272  1.1  riastrad 	 */
    273  1.1  riastrad 
    274  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    275  1.1  riastrad 	uint8_t lvds_ver, headerlen, recordlen;
    276  1.1  riastrad 
    277  1.1  riastrad 	memset(lth, 0, sizeof(struct lvdstableheader));
    278  1.1  riastrad 
    279  1.1  riastrad 	if (bios->fp.lvdsmanufacturerpointer == 0x0) {
    280  1.1  riastrad 		NV_ERROR(drm, "Pointer to LVDS manufacturer table invalid\n");
    281  1.1  riastrad 		return -EINVAL;
    282  1.1  riastrad 	}
    283  1.1  riastrad 
    284  1.1  riastrad 	lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
    285  1.1  riastrad 
    286  1.1  riastrad 	switch (lvds_ver) {
    287  1.1  riastrad 	case 0x0a:	/* pre NV40 */
    288  1.1  riastrad 		headerlen = 2;
    289  1.1  riastrad 		recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
    290  1.1  riastrad 		break;
    291  1.1  riastrad 	case 0x30:	/* NV4x */
    292  1.1  riastrad 		headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
    293  1.1  riastrad 		if (headerlen < 0x1f) {
    294  1.1  riastrad 			NV_ERROR(drm, "LVDS table header not understood\n");
    295  1.1  riastrad 			return -EINVAL;
    296  1.1  riastrad 		}
    297  1.1  riastrad 		recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
    298  1.1  riastrad 		break;
    299  1.1  riastrad 	case 0x40:	/* G80/G90 */
    300  1.1  riastrad 		headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
    301  1.1  riastrad 		if (headerlen < 0x7) {
    302  1.1  riastrad 			NV_ERROR(drm, "LVDS table header not understood\n");
    303  1.1  riastrad 			return -EINVAL;
    304  1.1  riastrad 		}
    305  1.1  riastrad 		recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
    306  1.1  riastrad 		break;
    307  1.1  riastrad 	default:
    308  1.1  riastrad 		NV_ERROR(drm,
    309  1.1  riastrad 			 "LVDS table revision %d.%d not currently supported\n",
    310  1.1  riastrad 			 lvds_ver >> 4, lvds_ver & 0xf);
    311  1.1  riastrad 		return -ENOSYS;
    312  1.1  riastrad 	}
    313  1.1  riastrad 
    314  1.1  riastrad 	lth->lvds_ver = lvds_ver;
    315  1.1  riastrad 	lth->headerlen = headerlen;
    316  1.1  riastrad 	lth->recordlen = recordlen;
    317  1.1  riastrad 
    318  1.1  riastrad 	return 0;
    319  1.1  riastrad }
    320  1.1  riastrad 
    321  1.1  riastrad static int
    322  1.1  riastrad get_fp_strap(struct drm_device *dev, struct nvbios *bios)
    323  1.1  riastrad {
    324  1.3  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    325  1.5  riastrad 	struct nvif_object *device = &drm->client.device.object;
    326  1.1  riastrad 
    327  1.1  riastrad 	/*
    328  1.1  riastrad 	 * The fp strap is normally dictated by the "User Strap" in
    329  1.1  riastrad 	 * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
    330  1.1  riastrad 	 * Internal_Flags struct at 0x48 is set, the user strap gets overriden
    331  1.1  riastrad 	 * by the PCI subsystem ID during POST, but not before the previous user
    332  1.1  riastrad 	 * strap has been committed to CR58 for CR57=0xf on head A, which may be
    333  1.1  riastrad 	 * read and used instead
    334  1.1  riastrad 	 */
    335  1.1  riastrad 
    336  1.1  riastrad 	if (bios->major_version < 5 && bios->data[0x48] & 0x4)
    337  1.1  riastrad 		return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
    338  1.1  riastrad 
    339  1.5  riastrad 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_MAXWELL)
    340  1.3  riastrad 		return nvif_rd32(device, 0x001800) & 0x0000000f;
    341  1.1  riastrad 	else
    342  1.5  riastrad 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
    343  1.3  riastrad 		return (nvif_rd32(device, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
    344  1.3  riastrad 	else
    345  1.3  riastrad 		return (nvif_rd32(device, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
    346  1.1  riastrad }
    347  1.1  riastrad 
    348  1.1  riastrad static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
    349  1.1  riastrad {
    350  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    351  1.1  riastrad 	uint8_t *fptable;
    352  1.1  riastrad 	uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
    353  1.1  riastrad 	int ret, ofs, fpstrapping;
    354  1.1  riastrad 	struct lvdstableheader lth;
    355  1.1  riastrad 
    356  1.1  riastrad 	if (bios->fp.fptablepointer == 0x0) {
    357  1.5  riastrad 		/* Most laptop cards lack an fp table. They use DDC. */
    358  1.5  riastrad 		NV_DEBUG(drm, "Pointer to flat panel table invalid\n");
    359  1.1  riastrad 		bios->digital_min_front_porch = 0x4b;
    360  1.1  riastrad 		return 0;
    361  1.1  riastrad 	}
    362  1.1  riastrad 
    363  1.1  riastrad 	fptable = &bios->data[bios->fp.fptablepointer];
    364  1.1  riastrad 	fptable_ver = fptable[0];
    365  1.1  riastrad 
    366  1.1  riastrad 	switch (fptable_ver) {
    367  1.1  riastrad 	/*
    368  1.1  riastrad 	 * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
    369  1.1  riastrad 	 * version field, and miss one of the spread spectrum/PWM bytes.
    370  1.1  riastrad 	 * This could affect early GF2Go parts (not seen any appropriate ROMs
    371  1.1  riastrad 	 * though). Here we assume that a version of 0x05 matches this case
    372  1.1  riastrad 	 * (combining with a BMP version check would be better), as the
    373  1.1  riastrad 	 * common case for the panel type field is 0x0005, and that is in
    374  1.1  riastrad 	 * fact what we are reading the first byte of.
    375  1.1  riastrad 	 */
    376  1.1  riastrad 	case 0x05:	/* some NV10, 11, 15, 16 */
    377  1.1  riastrad 		recordlen = 42;
    378  1.1  riastrad 		ofs = -1;
    379  1.1  riastrad 		break;
    380  1.1  riastrad 	case 0x10:	/* some NV15/16, and NV11+ */
    381  1.1  riastrad 		recordlen = 44;
    382  1.1  riastrad 		ofs = 0;
    383  1.1  riastrad 		break;
    384  1.1  riastrad 	case 0x20:	/* NV40+ */
    385  1.1  riastrad 		headerlen = fptable[1];
    386  1.1  riastrad 		recordlen = fptable[2];
    387  1.1  riastrad 		fpentries = fptable[3];
    388  1.1  riastrad 		/*
    389  1.1  riastrad 		 * fptable[4] is the minimum
    390  1.1  riastrad 		 * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
    391  1.1  riastrad 		 */
    392  1.1  riastrad 		bios->digital_min_front_porch = fptable[4];
    393  1.1  riastrad 		ofs = -7;
    394  1.1  riastrad 		break;
    395  1.1  riastrad 	default:
    396  1.1  riastrad 		NV_ERROR(drm,
    397  1.1  riastrad 			 "FP table revision %d.%d not currently supported\n",
    398  1.1  riastrad 			 fptable_ver >> 4, fptable_ver & 0xf);
    399  1.1  riastrad 		return -ENOSYS;
    400  1.1  riastrad 	}
    401  1.1  riastrad 
    402  1.1  riastrad 	if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
    403  1.1  riastrad 		return 0;
    404  1.1  riastrad 
    405  1.1  riastrad 	ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
    406  1.1  riastrad 	if (ret)
    407  1.1  riastrad 		return ret;
    408  1.1  riastrad 
    409  1.1  riastrad 	if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
    410  1.1  riastrad 		bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
    411  1.1  riastrad 							lth.headerlen + 1;
    412  1.1  riastrad 		bios->fp.xlatwidth = lth.recordlen;
    413  1.1  riastrad 	}
    414  1.1  riastrad 	if (bios->fp.fpxlatetableptr == 0x0) {
    415  1.1  riastrad 		NV_ERROR(drm, "Pointer to flat panel xlat table invalid\n");
    416  1.1  riastrad 		return -EINVAL;
    417  1.1  riastrad 	}
    418  1.1  riastrad 
    419  1.1  riastrad 	fpstrapping = get_fp_strap(dev, bios);
    420  1.1  riastrad 
    421  1.1  riastrad 	fpindex = bios->data[bios->fp.fpxlatetableptr +
    422  1.1  riastrad 					fpstrapping * bios->fp.xlatwidth];
    423  1.1  riastrad 
    424  1.1  riastrad 	if (fpindex > fpentries) {
    425  1.1  riastrad 		NV_ERROR(drm, "Bad flat panel table index\n");
    426  1.1  riastrad 		return -ENOENT;
    427  1.1  riastrad 	}
    428  1.1  riastrad 
    429  1.1  riastrad 	/* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
    430  1.1  riastrad 	if (lth.lvds_ver > 0x10)
    431  1.1  riastrad 		bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
    432  1.1  riastrad 
    433  1.1  riastrad 	/*
    434  1.1  riastrad 	 * If either the strap or xlated fpindex value are 0xf there is no
    435  1.1  riastrad 	 * panel using a strap-derived bios mode present.  this condition
    436  1.1  riastrad 	 * includes, but is different from, the DDC panel indicator above
    437  1.1  riastrad 	 */
    438  1.1  riastrad 	if (fpstrapping == 0xf || fpindex == 0xf)
    439  1.1  riastrad 		return 0;
    440  1.1  riastrad 
    441  1.1  riastrad 	bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
    442  1.1  riastrad 			    recordlen * fpindex + ofs;
    443  1.1  riastrad 
    444  1.1  riastrad 	NV_INFO(drm, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
    445  1.1  riastrad 		 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
    446  1.1  riastrad 		 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
    447  1.1  riastrad 		 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
    448  1.1  riastrad 
    449  1.1  riastrad 	return 0;
    450  1.1  riastrad }
    451  1.1  riastrad 
    452  1.1  riastrad bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
    453  1.1  riastrad {
    454  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    455  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
    456  1.1  riastrad 	uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
    457  1.1  riastrad 
    458  1.1  riastrad 	if (!mode)	/* just checking whether we can produce a mode */
    459  1.1  riastrad 		return bios->fp.mode_ptr;
    460  1.1  riastrad 
    461  1.1  riastrad 	memset(mode, 0, sizeof(struct drm_display_mode));
    462  1.1  riastrad 	/*
    463  1.1  riastrad 	 * For version 1.0 (version in byte 0):
    464  1.1  riastrad 	 * bytes 1-2 are "panel type", including bits on whether Colour/mono,
    465  1.1  riastrad 	 * single/dual link, and type (TFT etc.)
    466  1.1  riastrad 	 * bytes 3-6 are bits per colour in RGBX
    467  1.1  riastrad 	 */
    468  1.1  riastrad 	mode->clock = ROM16(mode_entry[7]) * 10;
    469  1.1  riastrad 	/* bytes 9-10 is HActive */
    470  1.1  riastrad 	mode->hdisplay = ROM16(mode_entry[11]) + 1;
    471  1.1  riastrad 	/*
    472  1.1  riastrad 	 * bytes 13-14 is HValid Start
    473  1.1  riastrad 	 * bytes 15-16 is HValid End
    474  1.1  riastrad 	 */
    475  1.1  riastrad 	mode->hsync_start = ROM16(mode_entry[17]) + 1;
    476  1.1  riastrad 	mode->hsync_end = ROM16(mode_entry[19]) + 1;
    477  1.1  riastrad 	mode->htotal = ROM16(mode_entry[21]) + 1;
    478  1.1  riastrad 	/* bytes 23-24, 27-30 similarly, but vertical */
    479  1.1  riastrad 	mode->vdisplay = ROM16(mode_entry[25]) + 1;
    480  1.1  riastrad 	mode->vsync_start = ROM16(mode_entry[31]) + 1;
    481  1.1  riastrad 	mode->vsync_end = ROM16(mode_entry[33]) + 1;
    482  1.1  riastrad 	mode->vtotal = ROM16(mode_entry[35]) + 1;
    483  1.1  riastrad 	mode->flags |= (mode_entry[37] & 0x10) ?
    484  1.1  riastrad 			DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
    485  1.1  riastrad 	mode->flags |= (mode_entry[37] & 0x1) ?
    486  1.1  riastrad 			DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
    487  1.1  riastrad 	/*
    488  1.1  riastrad 	 * bytes 38-39 relate to spread spectrum settings
    489  1.1  riastrad 	 * bytes 40-43 are something to do with PWM
    490  1.1  riastrad 	 */
    491  1.1  riastrad 
    492  1.1  riastrad 	mode->status = MODE_OK;
    493  1.1  riastrad 	mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
    494  1.1  riastrad 	drm_mode_set_name(mode);
    495  1.1  riastrad 	return bios->fp.mode_ptr;
    496  1.1  riastrad }
    497  1.1  riastrad 
    498  1.1  riastrad int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
    499  1.1  riastrad {
    500  1.1  riastrad 	/*
    501  1.1  riastrad 	 * The LVDS table header is (mostly) described in
    502  1.1  riastrad 	 * parse_lvds_manufacturer_table_header(): the BIT header additionally
    503  1.1  riastrad 	 * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
    504  1.1  riastrad 	 * straps are not being used for the panel, this specifies the frequency
    505  1.1  riastrad 	 * at which modes should be set up in the dual link style.
    506  1.1  riastrad 	 *
    507  1.1  riastrad 	 * Following the header, the BMP (ver 0xa) table has several records,
    508  1.1  riastrad 	 * indexed by a separate xlat table, indexed in turn by the fp strap in
    509  1.1  riastrad 	 * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
    510  1.1  riastrad 	 * numbers for use by INIT_SUB which controlled panel init and power,
    511  1.1  riastrad 	 * and finally a dword of ms to sleep between power off and on
    512  1.1  riastrad 	 * operations.
    513  1.1  riastrad 	 *
    514  1.1  riastrad 	 * In the BIT versions, the table following the header serves as an
    515  1.1  riastrad 	 * integrated config and xlat table: the records in the table are
    516  1.1  riastrad 	 * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
    517  1.1  riastrad 	 * two bytes - the first as a config byte, the second for indexing the
    518  1.1  riastrad 	 * fp mode table pointed to by the BIT 'D' table
    519  1.1  riastrad 	 *
    520  1.1  riastrad 	 * DDC is not used until after card init, so selecting the correct table
    521  1.1  riastrad 	 * entry and setting the dual link flag for EDID equipped panels,
    522  1.1  riastrad 	 * requiring tests against the native-mode pixel clock, cannot be done
    523  1.1  riastrad 	 * until later, when this function should be called with non-zero pxclk
    524  1.1  riastrad 	 */
    525  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    526  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
    527  1.1  riastrad 	int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
    528  1.1  riastrad 	struct lvdstableheader lth;
    529  1.1  riastrad 	uint16_t lvdsofs;
    530  1.1  riastrad 	int ret, chip_version = bios->chip_version;
    531  1.1  riastrad 
    532  1.1  riastrad 	ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
    533  1.1  riastrad 	if (ret)
    534  1.1  riastrad 		return ret;
    535  1.1  riastrad 
    536  1.1  riastrad 	switch (lth.lvds_ver) {
    537  1.1  riastrad 	case 0x0a:	/* pre NV40 */
    538  1.1  riastrad 		lvdsmanufacturerindex = bios->data[
    539  1.1  riastrad 					bios->fp.fpxlatemanufacturertableptr +
    540  1.1  riastrad 					fpstrapping];
    541  1.1  riastrad 
    542  1.1  riastrad 		/* we're done if this isn't the EDID panel case */
    543  1.1  riastrad 		if (!pxclk)
    544  1.1  riastrad 			break;
    545  1.1  riastrad 
    546  1.1  riastrad 		if (chip_version < 0x25) {
    547  1.1  riastrad 			/* nv17 behaviour
    548  1.1  riastrad 			 *
    549  1.1  riastrad 			 * It seems the old style lvds script pointer is reused
    550  1.1  riastrad 			 * to select 18/24 bit colour depth for EDID panels.
    551  1.1  riastrad 			 */
    552  1.1  riastrad 			lvdsmanufacturerindex =
    553  1.1  riastrad 				(bios->legacy.lvds_single_a_script_ptr & 1) ?
    554  1.1  riastrad 									2 : 0;
    555  1.1  riastrad 			if (pxclk >= bios->fp.duallink_transition_clk)
    556  1.1  riastrad 				lvdsmanufacturerindex++;
    557  1.1  riastrad 		} else if (chip_version < 0x30) {
    558  1.1  riastrad 			/* nv28 behaviour (off-chip encoder)
    559  1.1  riastrad 			 *
    560  1.1  riastrad 			 * nv28 does a complex dance of first using byte 121 of
    561  1.1  riastrad 			 * the EDID to choose the lvdsmanufacturerindex, then
    562  1.1  riastrad 			 * later attempting to match the EDID manufacturer and
    563  1.1  riastrad 			 * product IDs in a table (signature 'pidt' (panel id
    564  1.1  riastrad 			 * table?)), setting an lvdsmanufacturerindex of 0 and
    565  1.1  riastrad 			 * an fp strap of the match index (or 0xf if none)
    566  1.1  riastrad 			 */
    567  1.1  riastrad 			lvdsmanufacturerindex = 0;
    568  1.1  riastrad 		} else {
    569  1.1  riastrad 			/* nv31, nv34 behaviour */
    570  1.1  riastrad 			lvdsmanufacturerindex = 0;
    571  1.1  riastrad 			if (pxclk >= bios->fp.duallink_transition_clk)
    572  1.1  riastrad 				lvdsmanufacturerindex = 2;
    573  1.1  riastrad 			if (pxclk >= 140000)
    574  1.1  riastrad 				lvdsmanufacturerindex = 3;
    575  1.1  riastrad 		}
    576  1.1  riastrad 
    577  1.1  riastrad 		/*
    578  1.1  riastrad 		 * nvidia set the high nibble of (cr57=f, cr58) to
    579  1.1  riastrad 		 * lvdsmanufacturerindex in this case; we don't
    580  1.1  riastrad 		 */
    581  1.1  riastrad 		break;
    582  1.1  riastrad 	case 0x30:	/* NV4x */
    583  1.1  riastrad 	case 0x40:	/* G80/G90 */
    584  1.1  riastrad 		lvdsmanufacturerindex = fpstrapping;
    585  1.1  riastrad 		break;
    586  1.1  riastrad 	default:
    587  1.1  riastrad 		NV_ERROR(drm, "LVDS table revision not currently supported\n");
    588  1.1  riastrad 		return -ENOSYS;
    589  1.1  riastrad 	}
    590  1.1  riastrad 
    591  1.1  riastrad 	lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
    592  1.1  riastrad 	switch (lth.lvds_ver) {
    593  1.1  riastrad 	case 0x0a:
    594  1.1  riastrad 		bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
    595  1.1  riastrad 		bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
    596  1.1  riastrad 		bios->fp.dual_link = bios->data[lvdsofs] & 4;
    597  1.1  riastrad 		bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
    598  1.1  riastrad 		*if_is_24bit = bios->data[lvdsofs] & 16;
    599  1.1  riastrad 		break;
    600  1.1  riastrad 	case 0x30:
    601  1.1  riastrad 	case 0x40:
    602  1.1  riastrad 		/*
    603  1.1  riastrad 		 * No sign of the "power off for reset" or "reset for panel
    604  1.1  riastrad 		 * on" bits, but it's safer to assume we should
    605  1.1  riastrad 		 */
    606  1.1  riastrad 		bios->fp.power_off_for_reset = true;
    607  1.1  riastrad 		bios->fp.reset_after_pclk_change = true;
    608  1.1  riastrad 
    609  1.1  riastrad 		/*
    610  1.1  riastrad 		 * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
    611  1.1  riastrad 		 * over-written, and if_is_24bit isn't used
    612  1.1  riastrad 		 */
    613  1.1  riastrad 		bios->fp.dual_link = bios->data[lvdsofs] & 1;
    614  1.1  riastrad 		bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
    615  1.1  riastrad 		bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
    616  1.1  riastrad 		bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
    617  1.1  riastrad 		break;
    618  1.1  riastrad 	}
    619  1.1  riastrad 
    620  1.1  riastrad 	/* set dual_link flag for EDID case */
    621  1.1  riastrad 	if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
    622  1.1  riastrad 		bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
    623  1.1  riastrad 
    624  1.1  riastrad 	*dl = bios->fp.dual_link;
    625  1.1  riastrad 
    626  1.1  riastrad 	return 0;
    627  1.1  riastrad }
    628  1.1  riastrad 
    629  1.1  riastrad int run_tmds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, int pxclk)
    630  1.1  riastrad {
    631  1.1  riastrad 	/*
    632  1.1  riastrad 	 * the pxclk parameter is in kHz
    633  1.1  riastrad 	 *
    634  1.1  riastrad 	 * This runs the TMDS regs setting code found on BIT bios cards
    635  1.1  riastrad 	 *
    636  1.1  riastrad 	 * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
    637  1.1  riastrad 	 * ffs(or) == 3, use the second.
    638  1.1  riastrad 	 */
    639  1.1  riastrad 
    640  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    641  1.5  riastrad 	struct nvif_object *device = &drm->client.device.object;
    642  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
    643  1.1  riastrad 	int cv = bios->chip_version;
    644  1.1  riastrad 	uint16_t clktable = 0, scriptptr;
    645  1.1  riastrad 	uint32_t sel_clk_binding, sel_clk;
    646  1.1  riastrad 
    647  1.1  riastrad 	/* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
    648  1.1  riastrad 	if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
    649  1.1  riastrad 	    dcbent->location != DCB_LOC_ON_CHIP)
    650  1.1  riastrad 		return 0;
    651  1.1  riastrad 
    652  1.1  riastrad 	switch (ffs(dcbent->or)) {
    653  1.1  riastrad 	case 1:
    654  1.1  riastrad 		clktable = bios->tmds.output0_script_ptr;
    655  1.1  riastrad 		break;
    656  1.1  riastrad 	case 2:
    657  1.1  riastrad 	case 3:
    658  1.1  riastrad 		clktable = bios->tmds.output1_script_ptr;
    659  1.1  riastrad 		break;
    660  1.1  riastrad 	}
    661  1.1  riastrad 
    662  1.1  riastrad 	if (!clktable) {
    663  1.1  riastrad 		NV_ERROR(drm, "Pixel clock comparison table not found\n");
    664  1.1  riastrad 		return -EINVAL;
    665  1.1  riastrad 	}
    666  1.1  riastrad 
    667  1.1  riastrad 	scriptptr = clkcmptable(bios, clktable, pxclk);
    668  1.1  riastrad 
    669  1.1  riastrad 	if (!scriptptr) {
    670  1.1  riastrad 		NV_ERROR(drm, "TMDS output init script not found\n");
    671  1.1  riastrad 		return -ENOENT;
    672  1.1  riastrad 	}
    673  1.1  riastrad 
    674  1.1  riastrad 	/* don't let script change pll->head binding */
    675  1.3  riastrad 	sel_clk_binding = nvif_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
    676  1.1  riastrad 	run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
    677  1.1  riastrad 	sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
    678  1.1  riastrad 	NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
    679  1.1  riastrad 
    680  1.1  riastrad 	return 0;
    681  1.1  riastrad }
    682  1.1  riastrad 
    683  1.1  riastrad static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
    684  1.1  riastrad {
    685  1.1  riastrad 	/*
    686  1.1  riastrad 	 * Parses the init table segment for pointers used in script execution.
    687  1.1  riastrad 	 *
    688  1.1  riastrad 	 * offset + 0  (16 bits): init script tables pointer
    689  1.1  riastrad 	 * offset + 2  (16 bits): macro index table pointer
    690  1.1  riastrad 	 * offset + 4  (16 bits): macro table pointer
    691  1.1  riastrad 	 * offset + 6  (16 bits): condition table pointer
    692  1.1  riastrad 	 * offset + 8  (16 bits): io condition table pointer
    693  1.1  riastrad 	 * offset + 10 (16 bits): io flag condition table pointer
    694  1.1  riastrad 	 * offset + 12 (16 bits): init function table pointer
    695  1.1  riastrad 	 */
    696  1.1  riastrad 
    697  1.1  riastrad 	bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
    698  1.1  riastrad }
    699  1.1  riastrad 
    700  1.1  riastrad static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
    701  1.1  riastrad {
    702  1.1  riastrad 	/*
    703  1.1  riastrad 	 * Parses the load detect values for g80 cards.
    704  1.1  riastrad 	 *
    705  1.1  riastrad 	 * offset + 0 (16 bits): loadval table pointer
    706  1.1  riastrad 	 */
    707  1.1  riastrad 
    708  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    709  1.1  riastrad 	uint16_t load_table_ptr;
    710  1.1  riastrad 	uint8_t version, headerlen, entrylen, num_entries;
    711  1.1  riastrad 
    712  1.1  riastrad 	if (bitentry->length != 3) {
    713  1.1  riastrad 		NV_ERROR(drm, "Do not understand BIT A table\n");
    714  1.1  riastrad 		return -EINVAL;
    715  1.1  riastrad 	}
    716  1.1  riastrad 
    717  1.1  riastrad 	load_table_ptr = ROM16(bios->data[bitentry->offset]);
    718  1.1  riastrad 
    719  1.1  riastrad 	if (load_table_ptr == 0x0) {
    720  1.1  riastrad 		NV_DEBUG(drm, "Pointer to BIT loadval table invalid\n");
    721  1.1  riastrad 		return -EINVAL;
    722  1.1  riastrad 	}
    723  1.1  riastrad 
    724  1.1  riastrad 	version = bios->data[load_table_ptr];
    725  1.1  riastrad 
    726  1.1  riastrad 	if (version != 0x10) {
    727  1.1  riastrad 		NV_ERROR(drm, "BIT loadval table version %d.%d not supported\n",
    728  1.1  riastrad 			 version >> 4, version & 0xF);
    729  1.1  riastrad 		return -ENOSYS;
    730  1.1  riastrad 	}
    731  1.1  riastrad 
    732  1.1  riastrad 	headerlen = bios->data[load_table_ptr + 1];
    733  1.1  riastrad 	entrylen = bios->data[load_table_ptr + 2];
    734  1.1  riastrad 	num_entries = bios->data[load_table_ptr + 3];
    735  1.1  riastrad 
    736  1.1  riastrad 	if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
    737  1.1  riastrad 		NV_ERROR(drm, "Do not understand BIT loadval table\n");
    738  1.1  riastrad 		return -EINVAL;
    739  1.1  riastrad 	}
    740  1.1  riastrad 
    741  1.1  riastrad 	/* First entry is normal dac, 2nd tv-out perhaps? */
    742  1.1  riastrad 	bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
    743  1.1  riastrad 
    744  1.1  riastrad 	return 0;
    745  1.1  riastrad }
    746  1.1  riastrad 
    747  1.1  riastrad static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
    748  1.1  riastrad {
    749  1.1  riastrad 	/*
    750  1.1  riastrad 	 * Parses the flat panel table segment that the bit entry points to.
    751  1.1  riastrad 	 * Starting at bitentry->offset:
    752  1.1  riastrad 	 *
    753  1.1  riastrad 	 * offset + 0  (16 bits): ??? table pointer - seems to have 18 byte
    754  1.1  riastrad 	 * records beginning with a freq.
    755  1.1  riastrad 	 * offset + 2  (16 bits): mode table pointer
    756  1.1  riastrad 	 */
    757  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    758  1.1  riastrad 
    759  1.1  riastrad 	if (bitentry->length != 4) {
    760  1.1  riastrad 		NV_ERROR(drm, "Do not understand BIT display table\n");
    761  1.1  riastrad 		return -EINVAL;
    762  1.1  riastrad 	}
    763  1.1  riastrad 
    764  1.1  riastrad 	bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
    765  1.1  riastrad 
    766  1.1  riastrad 	return 0;
    767  1.1  riastrad }
    768  1.1  riastrad 
    769  1.1  riastrad static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
    770  1.1  riastrad {
    771  1.1  riastrad 	/*
    772  1.1  riastrad 	 * Parses the init table segment that the bit entry points to.
    773  1.1  riastrad 	 *
    774  1.1  riastrad 	 * See parse_script_table_pointers for layout
    775  1.1  riastrad 	 */
    776  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    777  1.1  riastrad 
    778  1.1  riastrad 	if (bitentry->length < 14) {
    779  1.1  riastrad 		NV_ERROR(drm, "Do not understand init table\n");
    780  1.1  riastrad 		return -EINVAL;
    781  1.1  riastrad 	}
    782  1.1  riastrad 
    783  1.1  riastrad 	parse_script_table_pointers(bios, bitentry->offset);
    784  1.1  riastrad 	return 0;
    785  1.1  riastrad }
    786  1.1  riastrad 
    787  1.1  riastrad static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
    788  1.1  riastrad {
    789  1.1  riastrad 	/*
    790  1.1  riastrad 	 * BIT 'i' (info?) table
    791  1.1  riastrad 	 *
    792  1.1  riastrad 	 * offset + 0  (32 bits): BIOS version dword (as in B table)
    793  1.1  riastrad 	 * offset + 5  (8  bits): BIOS feature byte (same as for BMP?)
    794  1.1  riastrad 	 * offset + 13 (16 bits): pointer to table containing DAC load
    795  1.1  riastrad 	 * detection comparison values
    796  1.1  riastrad 	 *
    797  1.1  riastrad 	 * There's other things in the table, purpose unknown
    798  1.1  riastrad 	 */
    799  1.1  riastrad 
    800  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    801  1.1  riastrad 	uint16_t daccmpoffset;
    802  1.1  riastrad 	uint8_t dacver, dacheaderlen;
    803  1.1  riastrad 
    804  1.1  riastrad 	if (bitentry->length < 6) {
    805  1.1  riastrad 		NV_ERROR(drm, "BIT i table too short for needed information\n");
    806  1.1  riastrad 		return -EINVAL;
    807  1.1  riastrad 	}
    808  1.1  riastrad 
    809  1.1  riastrad 	/*
    810  1.1  riastrad 	 * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
    811  1.1  riastrad 	 * Quadro identity crisis), other bits possibly as for BMP feature byte
    812  1.1  riastrad 	 */
    813  1.1  riastrad 	bios->feature_byte = bios->data[bitentry->offset + 5];
    814  1.1  riastrad 	bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
    815  1.1  riastrad 
    816  1.1  riastrad 	if (bitentry->length < 15) {
    817  1.1  riastrad 		NV_WARN(drm, "BIT i table not long enough for DAC load "
    818  1.1  riastrad 			       "detection comparison table\n");
    819  1.1  riastrad 		return -EINVAL;
    820  1.1  riastrad 	}
    821  1.1  riastrad 
    822  1.1  riastrad 	daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
    823  1.1  riastrad 
    824  1.1  riastrad 	/* doesn't exist on g80 */
    825  1.1  riastrad 	if (!daccmpoffset)
    826  1.1  riastrad 		return 0;
    827  1.1  riastrad 
    828  1.1  riastrad 	/*
    829  1.1  riastrad 	 * The first value in the table, following the header, is the
    830  1.1  riastrad 	 * comparison value, the second entry is a comparison value for
    831  1.1  riastrad 	 * TV load detection.
    832  1.1  riastrad 	 */
    833  1.1  riastrad 
    834  1.1  riastrad 	dacver = bios->data[daccmpoffset];
    835  1.1  riastrad 	dacheaderlen = bios->data[daccmpoffset + 1];
    836  1.1  riastrad 
    837  1.1  riastrad 	if (dacver != 0x00 && dacver != 0x10) {
    838  1.1  riastrad 		NV_WARN(drm, "DAC load detection comparison table version "
    839  1.1  riastrad 			       "%d.%d not known\n", dacver >> 4, dacver & 0xf);
    840  1.1  riastrad 		return -ENOSYS;
    841  1.1  riastrad 	}
    842  1.1  riastrad 
    843  1.1  riastrad 	bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
    844  1.1  riastrad 	bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
    845  1.1  riastrad 
    846  1.1  riastrad 	return 0;
    847  1.1  riastrad }
    848  1.1  riastrad 
    849  1.1  riastrad static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
    850  1.1  riastrad {
    851  1.1  riastrad 	/*
    852  1.1  riastrad 	 * Parses the LVDS table segment that the bit entry points to.
    853  1.1  riastrad 	 * Starting at bitentry->offset:
    854  1.1  riastrad 	 *
    855  1.1  riastrad 	 * offset + 0  (16 bits): LVDS strap xlate table pointer
    856  1.1  riastrad 	 */
    857  1.1  riastrad 
    858  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    859  1.1  riastrad 
    860  1.1  riastrad 	if (bitentry->length != 2) {
    861  1.1  riastrad 		NV_ERROR(drm, "Do not understand BIT LVDS table\n");
    862  1.1  riastrad 		return -EINVAL;
    863  1.1  riastrad 	}
    864  1.1  riastrad 
    865  1.1  riastrad 	/*
    866  1.1  riastrad 	 * No idea if it's still called the LVDS manufacturer table, but
    867  1.1  riastrad 	 * the concept's close enough.
    868  1.1  riastrad 	 */
    869  1.1  riastrad 	bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
    870  1.1  riastrad 
    871  1.1  riastrad 	return 0;
    872  1.1  riastrad }
    873  1.1  riastrad 
    874  1.1  riastrad static int
    875  1.1  riastrad parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
    876  1.1  riastrad 		      struct bit_entry *bitentry)
    877  1.1  riastrad {
    878  1.1  riastrad 	/*
    879  1.1  riastrad 	 * offset + 2  (8  bits): number of options in an
    880  1.1  riastrad 	 * 	INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
    881  1.1  riastrad 	 * offset + 3  (16 bits): pointer to strap xlate table for RAM
    882  1.1  riastrad 	 * 	restrict option selection
    883  1.1  riastrad 	 *
    884  1.1  riastrad 	 * There's a bunch of bits in this table other than the RAM restrict
    885  1.1  riastrad 	 * stuff that we don't use - their use currently unknown
    886  1.1  riastrad 	 */
    887  1.1  riastrad 
    888  1.1  riastrad 	/*
    889  1.1  riastrad 	 * Older bios versions don't have a sufficiently long table for
    890  1.1  riastrad 	 * what we want
    891  1.1  riastrad 	 */
    892  1.1  riastrad 	if (bitentry->length < 0x5)
    893  1.1  riastrad 		return 0;
    894  1.1  riastrad 
    895  1.1  riastrad 	if (bitentry->version < 2) {
    896  1.1  riastrad 		bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
    897  1.1  riastrad 		bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
    898  1.1  riastrad 	} else {
    899  1.1  riastrad 		bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
    900  1.1  riastrad 		bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
    901  1.1  riastrad 	}
    902  1.1  riastrad 
    903  1.1  riastrad 	return 0;
    904  1.1  riastrad }
    905  1.1  riastrad 
    906  1.1  riastrad static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
    907  1.1  riastrad {
    908  1.1  riastrad 	/*
    909  1.1  riastrad 	 * Parses the pointer to the TMDS table
    910  1.1  riastrad 	 *
    911  1.1  riastrad 	 * Starting at bitentry->offset:
    912  1.1  riastrad 	 *
    913  1.1  riastrad 	 * offset + 0  (16 bits): TMDS table pointer
    914  1.1  riastrad 	 *
    915  1.1  riastrad 	 * The TMDS table is typically found just before the DCB table, with a
    916  1.1  riastrad 	 * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
    917  1.1  riastrad 	 * length?)
    918  1.1  riastrad 	 *
    919  1.1  riastrad 	 * At offset +7 is a pointer to a script, which I don't know how to
    920  1.1  riastrad 	 * run yet.
    921  1.1  riastrad 	 * At offset +9 is a pointer to another script, likewise
    922  1.1  riastrad 	 * Offset +11 has a pointer to a table where the first word is a pxclk
    923  1.1  riastrad 	 * frequency and the second word a pointer to a script, which should be
    924  1.1  riastrad 	 * run if the comparison pxclk frequency is less than the pxclk desired.
    925  1.1  riastrad 	 * This repeats for decreasing comparison frequencies
    926  1.1  riastrad 	 * Offset +13 has a pointer to a similar table
    927  1.1  riastrad 	 * The selection of table (and possibly +7/+9 script) is dictated by
    928  1.1  riastrad 	 * "or" from the DCB.
    929  1.1  riastrad 	 */
    930  1.1  riastrad 
    931  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    932  1.1  riastrad 	uint16_t tmdstableptr, script1, script2;
    933  1.1  riastrad 
    934  1.1  riastrad 	if (bitentry->length != 2) {
    935  1.1  riastrad 		NV_ERROR(drm, "Do not understand BIT TMDS table\n");
    936  1.1  riastrad 		return -EINVAL;
    937  1.1  riastrad 	}
    938  1.1  riastrad 
    939  1.1  riastrad 	tmdstableptr = ROM16(bios->data[bitentry->offset]);
    940  1.1  riastrad 	if (!tmdstableptr) {
    941  1.5  riastrad 		NV_INFO(drm, "Pointer to TMDS table not found\n");
    942  1.1  riastrad 		return -EINVAL;
    943  1.1  riastrad 	}
    944  1.1  riastrad 
    945  1.1  riastrad 	NV_INFO(drm, "TMDS table version %d.%d\n",
    946  1.1  riastrad 		bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
    947  1.1  riastrad 
    948  1.1  riastrad 	/* nv50+ has v2.0, but we don't parse it atm */
    949  1.1  riastrad 	if (bios->data[tmdstableptr] != 0x11)
    950  1.1  riastrad 		return -ENOSYS;
    951  1.1  riastrad 
    952  1.1  riastrad 	/*
    953  1.1  riastrad 	 * These two scripts are odd: they don't seem to get run even when
    954  1.1  riastrad 	 * they are not stubbed.
    955  1.1  riastrad 	 */
    956  1.1  riastrad 	script1 = ROM16(bios->data[tmdstableptr + 7]);
    957  1.1  riastrad 	script2 = ROM16(bios->data[tmdstableptr + 9]);
    958  1.1  riastrad 	if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
    959  1.1  riastrad 		NV_WARN(drm, "TMDS table script pointers not stubbed\n");
    960  1.1  riastrad 
    961  1.1  riastrad 	bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
    962  1.1  riastrad 	bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
    963  1.1  riastrad 
    964  1.1  riastrad 	return 0;
    965  1.1  riastrad }
    966  1.1  riastrad 
    967  1.1  riastrad struct bit_table {
    968  1.1  riastrad 	const char id;
    969  1.1  riastrad 	int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
    970  1.1  riastrad };
    971  1.1  riastrad 
    972  1.1  riastrad #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
    973  1.1  riastrad 
    974  1.1  riastrad int
    975  1.1  riastrad bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
    976  1.1  riastrad {
    977  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
    978  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
    979  1.1  riastrad 	u8 entries, *entry;
    980  1.1  riastrad 
    981  1.1  riastrad 	if (bios->type != NVBIOS_BIT)
    982  1.1  riastrad 		return -ENODEV;
    983  1.1  riastrad 
    984  1.1  riastrad 	entries = bios->data[bios->offset + 10];
    985  1.1  riastrad 	entry   = &bios->data[bios->offset + 12];
    986  1.1  riastrad 	while (entries--) {
    987  1.1  riastrad 		if (entry[0] == id) {
    988  1.1  riastrad 			bit->id = entry[0];
    989  1.1  riastrad 			bit->version = entry[1];
    990  1.1  riastrad 			bit->length = ROM16(entry[2]);
    991  1.1  riastrad 			bit->offset = ROM16(entry[4]);
    992  1.1  riastrad 			bit->data = ROMPTR(dev, entry[4]);
    993  1.1  riastrad 			return 0;
    994  1.1  riastrad 		}
    995  1.1  riastrad 
    996  1.1  riastrad 		entry += bios->data[bios->offset + 9];
    997  1.1  riastrad 	}
    998  1.1  riastrad 
    999  1.1  riastrad 	return -ENOENT;
   1000  1.1  riastrad }
   1001  1.1  riastrad 
   1002  1.1  riastrad static int
   1003  1.1  riastrad parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
   1004  1.1  riastrad 		struct bit_table *table)
   1005  1.1  riastrad {
   1006  1.1  riastrad 	struct drm_device *dev = bios->dev;
   1007  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1008  1.1  riastrad 	struct bit_entry bitentry;
   1009  1.1  riastrad 
   1010  1.1  riastrad 	if (bit_table(dev, table->id, &bitentry) == 0)
   1011  1.1  riastrad 		return table->parse_fn(dev, bios, &bitentry);
   1012  1.1  riastrad 
   1013  1.1  riastrad 	NV_INFO(drm, "BIT table '%c' not found\n", table->id);
   1014  1.1  riastrad 	return -ENOSYS;
   1015  1.1  riastrad }
   1016  1.1  riastrad 
   1017  1.1  riastrad static int
   1018  1.1  riastrad parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
   1019  1.1  riastrad {
   1020  1.1  riastrad 	int ret;
   1021  1.1  riastrad 
   1022  1.1  riastrad 	/*
   1023  1.1  riastrad 	 * The only restriction on parsing order currently is having 'i' first
   1024  1.1  riastrad 	 * for use of bios->*_version or bios->feature_byte while parsing;
   1025  1.1  riastrad 	 * functions shouldn't be actually *doing* anything apart from pulling
   1026  1.1  riastrad 	 * data from the image into the bios struct, thus no interdependencies
   1027  1.1  riastrad 	 */
   1028  1.1  riastrad 	ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
   1029  1.1  riastrad 	if (ret) /* info? */
   1030  1.1  riastrad 		return ret;
   1031  1.1  riastrad 	if (bios->major_version >= 0x60) /* g80+ */
   1032  1.1  riastrad 		parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
   1033  1.1  riastrad 	parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
   1034  1.1  riastrad 	ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
   1035  1.1  riastrad 	if (ret)
   1036  1.1  riastrad 		return ret;
   1037  1.1  riastrad 	parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
   1038  1.1  riastrad 	parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
   1039  1.1  riastrad 	parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
   1040  1.1  riastrad 
   1041  1.1  riastrad 	return 0;
   1042  1.1  riastrad }
   1043  1.1  riastrad 
   1044  1.1  riastrad static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
   1045  1.1  riastrad {
   1046  1.1  riastrad 	/*
   1047  1.1  riastrad 	 * Parses the BMP structure for useful things, but does not act on them
   1048  1.1  riastrad 	 *
   1049  1.1  riastrad 	 * offset +   5: BMP major version
   1050  1.1  riastrad 	 * offset +   6: BMP minor version
   1051  1.1  riastrad 	 * offset +   9: BMP feature byte
   1052  1.1  riastrad 	 * offset +  10: BCD encoded BIOS version
   1053  1.1  riastrad 	 *
   1054  1.1  riastrad 	 * offset +  18: init script table pointer (for bios versions < 5.10h)
   1055  1.1  riastrad 	 * offset +  20: extra init script table pointer (for bios
   1056  1.1  riastrad 	 * versions < 5.10h)
   1057  1.1  riastrad 	 *
   1058  1.1  riastrad 	 * offset +  24: memory init table pointer (used on early bios versions)
   1059  1.1  riastrad 	 * offset +  26: SDR memory sequencing setup data table
   1060  1.1  riastrad 	 * offset +  28: DDR memory sequencing setup data table
   1061  1.1  riastrad 	 *
   1062  1.1  riastrad 	 * offset +  54: index of I2C CRTC pair to use for CRT output
   1063  1.1  riastrad 	 * offset +  55: index of I2C CRTC pair to use for TV output
   1064  1.1  riastrad 	 * offset +  56: index of I2C CRTC pair to use for flat panel output
   1065  1.1  riastrad 	 * offset +  58: write CRTC index for I2C pair 0
   1066  1.1  riastrad 	 * offset +  59: read CRTC index for I2C pair 0
   1067  1.1  riastrad 	 * offset +  60: write CRTC index for I2C pair 1
   1068  1.1  riastrad 	 * offset +  61: read CRTC index for I2C pair 1
   1069  1.1  riastrad 	 *
   1070  1.1  riastrad 	 * offset +  67: maximum internal PLL frequency (single stage PLL)
   1071  1.1  riastrad 	 * offset +  71: minimum internal PLL frequency (single stage PLL)
   1072  1.1  riastrad 	 *
   1073  1.1  riastrad 	 * offset +  75: script table pointers, as described in
   1074  1.1  riastrad 	 * parse_script_table_pointers
   1075  1.1  riastrad 	 *
   1076  1.1  riastrad 	 * offset +  89: TMDS single link output A table pointer
   1077  1.1  riastrad 	 * offset +  91: TMDS single link output B table pointer
   1078  1.1  riastrad 	 * offset +  95: LVDS single link output A table pointer
   1079  1.1  riastrad 	 * offset + 105: flat panel timings table pointer
   1080  1.1  riastrad 	 * offset + 107: flat panel strapping translation table pointer
   1081  1.1  riastrad 	 * offset + 117: LVDS manufacturer panel config table pointer
   1082  1.1  riastrad 	 * offset + 119: LVDS manufacturer strapping translation table pointer
   1083  1.1  riastrad 	 *
   1084  1.1  riastrad 	 * offset + 142: PLL limits table pointer
   1085  1.1  riastrad 	 *
   1086  1.1  riastrad 	 * offset + 156: minimum pixel clock for LVDS dual link
   1087  1.1  riastrad 	 */
   1088  1.1  riastrad 
   1089  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1090  1.1  riastrad 	uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
   1091  1.1  riastrad 	uint16_t bmplength;
   1092  1.1  riastrad 	uint16_t legacy_scripts_offset, legacy_i2c_offset;
   1093  1.1  riastrad 
   1094  1.1  riastrad 	/* load needed defaults in case we can't parse this info */
   1095  1.1  riastrad 	bios->digital_min_front_porch = 0x4b;
   1096  1.1  riastrad 	bios->fmaxvco = 256000;
   1097  1.1  riastrad 	bios->fminvco = 128000;
   1098  1.1  riastrad 	bios->fp.duallink_transition_clk = 90000;
   1099  1.1  riastrad 
   1100  1.1  riastrad 	bmp_version_major = bmp[5];
   1101  1.1  riastrad 	bmp_version_minor = bmp[6];
   1102  1.1  riastrad 
   1103  1.1  riastrad 	NV_INFO(drm, "BMP version %d.%d\n",
   1104  1.1  riastrad 		 bmp_version_major, bmp_version_minor);
   1105  1.1  riastrad 
   1106  1.1  riastrad 	/*
   1107  1.1  riastrad 	 * Make sure that 0x36 is blank and can't be mistaken for a DCB
   1108  1.1  riastrad 	 * pointer on early versions
   1109  1.1  riastrad 	 */
   1110  1.1  riastrad 	if (bmp_version_major < 5)
   1111  1.1  riastrad 		*(uint16_t *)&bios->data[0x36] = 0;
   1112  1.1  riastrad 
   1113  1.1  riastrad 	/*
   1114  1.1  riastrad 	 * Seems that the minor version was 1 for all major versions prior
   1115  1.1  riastrad 	 * to 5. Version 6 could theoretically exist, but I suspect BIT
   1116  1.1  riastrad 	 * happened instead.
   1117  1.1  riastrad 	 */
   1118  1.1  riastrad 	if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
   1119  1.1  riastrad 		NV_ERROR(drm, "You have an unsupported BMP version. "
   1120  1.1  riastrad 				"Please send in your bios\n");
   1121  1.1  riastrad 		return -ENOSYS;
   1122  1.1  riastrad 	}
   1123  1.1  riastrad 
   1124  1.1  riastrad 	if (bmp_version_major == 0)
   1125  1.1  riastrad 		/* nothing that's currently useful in this version */
   1126  1.1  riastrad 		return 0;
   1127  1.1  riastrad 	else if (bmp_version_major == 1)
   1128  1.1  riastrad 		bmplength = 44; /* exact for 1.01 */
   1129  1.1  riastrad 	else if (bmp_version_major == 2)
   1130  1.1  riastrad 		bmplength = 48; /* exact for 2.01 */
   1131  1.1  riastrad 	else if (bmp_version_major == 3)
   1132  1.1  riastrad 		bmplength = 54;
   1133  1.1  riastrad 		/* guessed - mem init tables added in this version */
   1134  1.1  riastrad 	else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
   1135  1.1  riastrad 		/* don't know if 5.0 exists... */
   1136  1.1  riastrad 		bmplength = 62;
   1137  1.1  riastrad 		/* guessed - BMP I2C indices added in version 4*/
   1138  1.1  riastrad 	else if (bmp_version_minor < 0x6)
   1139  1.1  riastrad 		bmplength = 67; /* exact for 5.01 */
   1140  1.1  riastrad 	else if (bmp_version_minor < 0x10)
   1141  1.1  riastrad 		bmplength = 75; /* exact for 5.06 */
   1142  1.1  riastrad 	else if (bmp_version_minor == 0x10)
   1143  1.1  riastrad 		bmplength = 89; /* exact for 5.10h */
   1144  1.1  riastrad 	else if (bmp_version_minor < 0x14)
   1145  1.1  riastrad 		bmplength = 118; /* exact for 5.11h */
   1146  1.1  riastrad 	else if (bmp_version_minor < 0x24)
   1147  1.1  riastrad 		/*
   1148  1.1  riastrad 		 * Not sure of version where pll limits came in;
   1149  1.1  riastrad 		 * certainly exist by 0x24 though.
   1150  1.1  riastrad 		 */
   1151  1.1  riastrad 		/* length not exact: this is long enough to get lvds members */
   1152  1.1  riastrad 		bmplength = 123;
   1153  1.1  riastrad 	else if (bmp_version_minor < 0x27)
   1154  1.1  riastrad 		/*
   1155  1.1  riastrad 		 * Length not exact: this is long enough to get pll limit
   1156  1.1  riastrad 		 * member
   1157  1.1  riastrad 		 */
   1158  1.1  riastrad 		bmplength = 144;
   1159  1.1  riastrad 	else
   1160  1.1  riastrad 		/*
   1161  1.1  riastrad 		 * Length not exact: this is long enough to get dual link
   1162  1.1  riastrad 		 * transition clock.
   1163  1.1  riastrad 		 */
   1164  1.1  riastrad 		bmplength = 158;
   1165  1.1  riastrad 
   1166  1.1  riastrad 	/* checksum */
   1167  1.1  riastrad 	if (nv_cksum(bmp, 8)) {
   1168  1.1  riastrad 		NV_ERROR(drm, "Bad BMP checksum\n");
   1169  1.1  riastrad 		return -EINVAL;
   1170  1.1  riastrad 	}
   1171  1.1  riastrad 
   1172  1.1  riastrad 	/*
   1173  1.1  riastrad 	 * Bit 4 seems to indicate either a mobile bios or a quadro card --
   1174  1.1  riastrad 	 * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
   1175  1.1  riastrad 	 * (not nv10gl), bit 5 that the flat panel tables are present, and
   1176  1.1  riastrad 	 * bit 6 a tv bios.
   1177  1.1  riastrad 	 */
   1178  1.1  riastrad 	bios->feature_byte = bmp[9];
   1179  1.1  riastrad 
   1180  1.1  riastrad 	if (bmp_version_major < 5 || bmp_version_minor < 0x10)
   1181  1.1  riastrad 		bios->old_style_init = true;
   1182  1.1  riastrad 	legacy_scripts_offset = 18;
   1183  1.1  riastrad 	if (bmp_version_major < 2)
   1184  1.1  riastrad 		legacy_scripts_offset -= 4;
   1185  1.1  riastrad 	bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
   1186  1.1  riastrad 	bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
   1187  1.1  riastrad 
   1188  1.1  riastrad 	if (bmp_version_major > 2) {	/* appears in BMP 3 */
   1189  1.1  riastrad 		bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
   1190  1.1  riastrad 		bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
   1191  1.1  riastrad 		bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
   1192  1.1  riastrad 	}
   1193  1.1  riastrad 
   1194  1.1  riastrad 	legacy_i2c_offset = 0x48;	/* BMP version 2 & 3 */
   1195  1.1  riastrad 	if (bmplength > 61)
   1196  1.1  riastrad 		legacy_i2c_offset = offset + 54;
   1197  1.1  riastrad 	bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
   1198  1.1  riastrad 	bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
   1199  1.1  riastrad 	bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
   1200  1.1  riastrad 
   1201  1.1  riastrad 	if (bmplength > 74) {
   1202  1.1  riastrad 		bios->fmaxvco = ROM32(bmp[67]);
   1203  1.1  riastrad 		bios->fminvco = ROM32(bmp[71]);
   1204  1.1  riastrad 	}
   1205  1.1  riastrad 	if (bmplength > 88)
   1206  1.1  riastrad 		parse_script_table_pointers(bios, offset + 75);
   1207  1.1  riastrad 	if (bmplength > 94) {
   1208  1.1  riastrad 		bios->tmds.output0_script_ptr = ROM16(bmp[89]);
   1209  1.1  riastrad 		bios->tmds.output1_script_ptr = ROM16(bmp[91]);
   1210  1.1  riastrad 		/*
   1211  1.1  riastrad 		 * Never observed in use with lvds scripts, but is reused for
   1212  1.1  riastrad 		 * 18/24 bit panel interface default for EDID equipped panels
   1213  1.1  riastrad 		 * (if_is_24bit not set directly to avoid any oscillation).
   1214  1.1  riastrad 		 */
   1215  1.1  riastrad 		bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
   1216  1.1  riastrad 	}
   1217  1.1  riastrad 	if (bmplength > 108) {
   1218  1.1  riastrad 		bios->fp.fptablepointer = ROM16(bmp[105]);
   1219  1.1  riastrad 		bios->fp.fpxlatetableptr = ROM16(bmp[107]);
   1220  1.1  riastrad 		bios->fp.xlatwidth = 1;
   1221  1.1  riastrad 	}
   1222  1.1  riastrad 	if (bmplength > 120) {
   1223  1.1  riastrad 		bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
   1224  1.1  riastrad 		bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
   1225  1.1  riastrad 	}
   1226  1.1  riastrad #if 0
   1227  1.1  riastrad 	if (bmplength > 143)
   1228  1.1  riastrad 		bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
   1229  1.1  riastrad #endif
   1230  1.1  riastrad 
   1231  1.1  riastrad 	if (bmplength > 157)
   1232  1.1  riastrad 		bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
   1233  1.1  riastrad 
   1234  1.1  riastrad 	return 0;
   1235  1.1  riastrad }
   1236  1.1  riastrad 
   1237  1.1  riastrad static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
   1238  1.1  riastrad {
   1239  1.1  riastrad 	int i, j;
   1240  1.1  riastrad 
   1241  1.1  riastrad 	for (i = 0; i <= (n - len); i++) {
   1242  1.1  riastrad 		for (j = 0; j < len; j++)
   1243  1.1  riastrad 			if (data[i + j] != str[j])
   1244  1.1  riastrad 				break;
   1245  1.1  riastrad 		if (j == len)
   1246  1.1  riastrad 			return i;
   1247  1.1  riastrad 	}
   1248  1.1  riastrad 
   1249  1.1  riastrad 	return 0;
   1250  1.1  riastrad }
   1251  1.1  riastrad 
   1252  1.1  riastrad void *
   1253  1.1  riastrad olddcb_table(struct drm_device *dev)
   1254  1.1  riastrad {
   1255  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1256  1.1  riastrad 	u8 *dcb = NULL;
   1257  1.1  riastrad 
   1258  1.5  riastrad 	if (drm->client.device.info.family > NV_DEVICE_INFO_V0_TNT)
   1259  1.1  riastrad 		dcb = ROMPTR(dev, drm->vbios.data[0x36]);
   1260  1.1  riastrad 	if (!dcb) {
   1261  1.1  riastrad 		NV_WARN(drm, "No DCB data found in VBIOS\n");
   1262  1.1  riastrad 		return NULL;
   1263  1.1  riastrad 	}
   1264  1.1  riastrad 
   1265  1.3  riastrad 	if (dcb[0] >= 0x42) {
   1266  1.1  riastrad 		NV_WARN(drm, "DCB version 0x%02x unknown\n", dcb[0]);
   1267  1.1  riastrad 		return NULL;
   1268  1.1  riastrad 	} else
   1269  1.1  riastrad 	if (dcb[0] >= 0x30) {
   1270  1.1  riastrad 		if (ROM32(dcb[6]) == 0x4edcbdcb)
   1271  1.1  riastrad 			return dcb;
   1272  1.1  riastrad 	} else
   1273  1.1  riastrad 	if (dcb[0] >= 0x20) {
   1274  1.1  riastrad 		if (ROM32(dcb[4]) == 0x4edcbdcb)
   1275  1.1  riastrad 			return dcb;
   1276  1.1  riastrad 	} else
   1277  1.1  riastrad 	if (dcb[0] >= 0x15) {
   1278  1.1  riastrad 		if (!memcmp(&dcb[-7], "DEV_REC", 7))
   1279  1.1  riastrad 			return dcb;
   1280  1.1  riastrad 	} else {
   1281  1.1  riastrad 		/*
   1282  1.1  riastrad 		 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
   1283  1.1  riastrad 		 * always has the same single (crt) entry, even when tv-out
   1284  1.1  riastrad 		 * present, so the conclusion is this version cannot really
   1285  1.1  riastrad 		 * be used.
   1286  1.1  riastrad 		 *
   1287  1.1  riastrad 		 * v1.2 tables (some NV6/10, and NV15+) normally have the
   1288  1.1  riastrad 		 * same 5 entries, which are not specific to the card and so
   1289  1.1  riastrad 		 * no use.
   1290  1.1  riastrad 		 *
   1291  1.1  riastrad 		 * v1.2 does have an I2C table that read_dcb_i2c_table can
   1292  1.1  riastrad 		 * handle, but cards exist (nv11 in #14821) with a bad i2c
   1293  1.1  riastrad 		 * table pointer, so use the indices parsed in
   1294  1.1  riastrad 		 * parse_bmp_structure.
   1295  1.1  riastrad 		 *
   1296  1.1  riastrad 		 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
   1297  1.1  riastrad 		 */
   1298  1.1  riastrad 		NV_WARN(drm, "No useful DCB data in VBIOS\n");
   1299  1.1  riastrad 		return NULL;
   1300  1.1  riastrad 	}
   1301  1.1  riastrad 
   1302  1.1  riastrad 	NV_WARN(drm, "DCB header validation failed\n");
   1303  1.1  riastrad 	return NULL;
   1304  1.1  riastrad }
   1305  1.1  riastrad 
   1306  1.1  riastrad void *
   1307  1.1  riastrad olddcb_outp(struct drm_device *dev, u8 idx)
   1308  1.1  riastrad {
   1309  1.1  riastrad 	u8 *dcb = olddcb_table(dev);
   1310  1.1  riastrad 	if (dcb && dcb[0] >= 0x30) {
   1311  1.1  riastrad 		if (idx < dcb[2])
   1312  1.1  riastrad 			return dcb + dcb[1] + (idx * dcb[3]);
   1313  1.1  riastrad 	} else
   1314  1.1  riastrad 	if (dcb && dcb[0] >= 0x20) {
   1315  1.1  riastrad 		u8 *i2c = ROMPTR(dev, dcb[2]);
   1316  1.1  riastrad 		u8 *ent = dcb + 8 + (idx * 8);
   1317  1.1  riastrad 		if (i2c && ent < i2c)
   1318  1.1  riastrad 			return ent;
   1319  1.1  riastrad 	} else
   1320  1.1  riastrad 	if (dcb && dcb[0] >= 0x15) {
   1321  1.1  riastrad 		u8 *i2c = ROMPTR(dev, dcb[2]);
   1322  1.1  riastrad 		u8 *ent = dcb + 4 + (idx * 10);
   1323  1.1  riastrad 		if (i2c && ent < i2c)
   1324  1.1  riastrad 			return ent;
   1325  1.1  riastrad 	}
   1326  1.1  riastrad 
   1327  1.1  riastrad 	return NULL;
   1328  1.1  riastrad }
   1329  1.1  riastrad 
   1330  1.1  riastrad int
   1331  1.1  riastrad olddcb_outp_foreach(struct drm_device *dev, void *data,
   1332  1.1  riastrad 		 int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
   1333  1.1  riastrad {
   1334  1.1  riastrad 	int ret, idx = -1;
   1335  1.1  riastrad 	u8 *outp = NULL;
   1336  1.1  riastrad 	while ((outp = olddcb_outp(dev, ++idx))) {
   1337  1.1  riastrad 		if (ROM32(outp[0]) == 0x00000000)
   1338  1.1  riastrad 			break; /* seen on an NV11 with DCB v1.5 */
   1339  1.1  riastrad 		if (ROM32(outp[0]) == 0xffffffff)
   1340  1.1  riastrad 			break; /* seen on an NV17 with DCB v2.0 */
   1341  1.1  riastrad 
   1342  1.1  riastrad 		if ((outp[0] & 0x0f) == DCB_OUTPUT_UNUSED)
   1343  1.1  riastrad 			continue;
   1344  1.1  riastrad 		if ((outp[0] & 0x0f) == DCB_OUTPUT_EOL)
   1345  1.1  riastrad 			break;
   1346  1.1  riastrad 
   1347  1.1  riastrad 		ret = exec(dev, data, idx, outp);
   1348  1.1  riastrad 		if (ret)
   1349  1.1  riastrad 			return ret;
   1350  1.1  riastrad 	}
   1351  1.1  riastrad 
   1352  1.1  riastrad 	return 0;
   1353  1.1  riastrad }
   1354  1.1  riastrad 
   1355  1.1  riastrad u8 *
   1356  1.1  riastrad olddcb_conntab(struct drm_device *dev)
   1357  1.1  riastrad {
   1358  1.1  riastrad 	u8 *dcb = olddcb_table(dev);
   1359  1.1  riastrad 	if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
   1360  1.1  riastrad 		u8 *conntab = ROMPTR(dev, dcb[0x14]);
   1361  1.1  riastrad 		if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
   1362  1.1  riastrad 			return conntab;
   1363  1.1  riastrad 	}
   1364  1.1  riastrad 	return NULL;
   1365  1.1  riastrad }
   1366  1.1  riastrad 
   1367  1.1  riastrad u8 *
   1368  1.1  riastrad olddcb_conn(struct drm_device *dev, u8 idx)
   1369  1.1  riastrad {
   1370  1.1  riastrad 	u8 *conntab = olddcb_conntab(dev);
   1371  1.1  riastrad 	if (conntab && idx < conntab[2])
   1372  1.1  riastrad 		return conntab + conntab[1] + (idx * conntab[3]);
   1373  1.1  riastrad 	return NULL;
   1374  1.1  riastrad }
   1375  1.1  riastrad 
   1376  1.1  riastrad static struct dcb_output *new_dcb_entry(struct dcb_table *dcb)
   1377  1.1  riastrad {
   1378  1.1  riastrad 	struct dcb_output *entry = &dcb->entry[dcb->entries];
   1379  1.1  riastrad 
   1380  1.1  riastrad 	memset(entry, 0, sizeof(struct dcb_output));
   1381  1.1  riastrad 	entry->index = dcb->entries++;
   1382  1.1  riastrad 
   1383  1.1  riastrad 	return entry;
   1384  1.1  riastrad }
   1385  1.1  riastrad 
   1386  1.1  riastrad static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
   1387  1.1  riastrad 				 int heads, int or)
   1388  1.1  riastrad {
   1389  1.1  riastrad 	struct dcb_output *entry = new_dcb_entry(dcb);
   1390  1.1  riastrad 
   1391  1.1  riastrad 	entry->type = type;
   1392  1.1  riastrad 	entry->i2c_index = i2c;
   1393  1.1  riastrad 	entry->heads = heads;
   1394  1.1  riastrad 	if (type != DCB_OUTPUT_ANALOG)
   1395  1.1  riastrad 		entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
   1396  1.1  riastrad 	entry->or = or;
   1397  1.1  riastrad }
   1398  1.1  riastrad 
   1399  1.1  riastrad static bool
   1400  1.1  riastrad parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
   1401  1.1  riastrad 		  uint32_t conn, uint32_t conf, struct dcb_output *entry)
   1402  1.1  riastrad {
   1403  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1404  1.3  riastrad 	int link = 0;
   1405  1.1  riastrad 
   1406  1.1  riastrad 	entry->type = conn & 0xf;
   1407  1.1  riastrad 	entry->i2c_index = (conn >> 4) & 0xf;
   1408  1.1  riastrad 	entry->heads = (conn >> 8) & 0xf;
   1409  1.1  riastrad 	entry->connector = (conn >> 12) & 0xf;
   1410  1.1  riastrad 	entry->bus = (conn >> 16) & 0xf;
   1411  1.1  riastrad 	entry->location = (conn >> 20) & 0x3;
   1412  1.1  riastrad 	entry->or = (conn >> 24) & 0xf;
   1413  1.1  riastrad 
   1414  1.1  riastrad 	switch (entry->type) {
   1415  1.1  riastrad 	case DCB_OUTPUT_ANALOG:
   1416  1.1  riastrad 		/*
   1417  1.1  riastrad 		 * Although the rest of a CRT conf dword is usually
   1418  1.1  riastrad 		 * zeros, mac biosen have stuff there so we must mask
   1419  1.1  riastrad 		 */
   1420  1.1  riastrad 		entry->crtconf.maxfreq = (dcb->version < 0x30) ?
   1421  1.1  riastrad 					 (conf & 0xffff) * 10 :
   1422  1.1  riastrad 					 (conf & 0xff) * 10000;
   1423  1.1  riastrad 		break;
   1424  1.1  riastrad 	case DCB_OUTPUT_LVDS:
   1425  1.1  riastrad 		{
   1426  1.1  riastrad 		uint32_t mask;
   1427  1.1  riastrad 		if (conf & 0x1)
   1428  1.1  riastrad 			entry->lvdsconf.use_straps_for_mode = true;
   1429  1.1  riastrad 		if (dcb->version < 0x22) {
   1430  1.1  riastrad 			mask = ~0xd;
   1431  1.1  riastrad 			/*
   1432  1.1  riastrad 			 * The laptop in bug 14567 lies and claims to not use
   1433  1.1  riastrad 			 * straps when it does, so assume all DCB 2.0 laptops
   1434  1.1  riastrad 			 * use straps, until a broken EDID using one is produced
   1435  1.1  riastrad 			 */
   1436  1.1  riastrad 			entry->lvdsconf.use_straps_for_mode = true;
   1437  1.1  riastrad 			/*
   1438  1.1  riastrad 			 * Both 0x4 and 0x8 show up in v2.0 tables; assume they
   1439  1.1  riastrad 			 * mean the same thing (probably wrong, but might work)
   1440  1.1  riastrad 			 */
   1441  1.1  riastrad 			if (conf & 0x4 || conf & 0x8)
   1442  1.1  riastrad 				entry->lvdsconf.use_power_scripts = true;
   1443  1.1  riastrad 		} else {
   1444  1.1  riastrad 			mask = ~0x7;
   1445  1.1  riastrad 			if (conf & 0x2)
   1446  1.1  riastrad 				entry->lvdsconf.use_acpi_for_edid = true;
   1447  1.1  riastrad 			if (conf & 0x4)
   1448  1.1  riastrad 				entry->lvdsconf.use_power_scripts = true;
   1449  1.1  riastrad 			entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
   1450  1.3  riastrad 			link = entry->lvdsconf.sor.link;
   1451  1.1  riastrad 		}
   1452  1.1  riastrad 		if (conf & mask) {
   1453  1.1  riastrad 			/*
   1454  1.1  riastrad 			 * Until we even try to use these on G8x, it's
   1455  1.1  riastrad 			 * useless reporting unknown bits.  They all are.
   1456  1.1  riastrad 			 */
   1457  1.1  riastrad 			if (dcb->version >= 0x40)
   1458  1.1  riastrad 				break;
   1459  1.1  riastrad 
   1460  1.1  riastrad 			NV_ERROR(drm, "Unknown LVDS configuration bits, "
   1461  1.1  riastrad 				      "please report\n");
   1462  1.1  riastrad 		}
   1463  1.1  riastrad 		break;
   1464  1.1  riastrad 		}
   1465  1.1  riastrad 	case DCB_OUTPUT_TV:
   1466  1.1  riastrad 	{
   1467  1.1  riastrad 		if (dcb->version >= 0x30)
   1468  1.1  riastrad 			entry->tvconf.has_component_output = conf & (0x8 << 4);
   1469  1.1  riastrad 		else
   1470  1.1  riastrad 			entry->tvconf.has_component_output = false;
   1471  1.1  riastrad 
   1472  1.1  riastrad 		break;
   1473  1.1  riastrad 	}
   1474  1.1  riastrad 	case DCB_OUTPUT_DP:
   1475  1.1  riastrad 		entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
   1476  1.1  riastrad 		entry->extdev = (conf & 0x0000ff00) >> 8;
   1477  1.1  riastrad 		switch ((conf & 0x00e00000) >> 21) {
   1478  1.1  riastrad 		case 0:
   1479  1.1  riastrad 			entry->dpconf.link_bw = 162000;
   1480  1.1  riastrad 			break;
   1481  1.1  riastrad 		case 1:
   1482  1.1  riastrad 			entry->dpconf.link_bw = 270000;
   1483  1.1  riastrad 			break;
   1484  1.5  riastrad 		case 2:
   1485  1.5  riastrad 			entry->dpconf.link_bw = 540000;
   1486  1.5  riastrad 			break;
   1487  1.5  riastrad 		case 3:
   1488  1.1  riastrad 		default:
   1489  1.5  riastrad 			entry->dpconf.link_bw = 810000;
   1490  1.1  riastrad 			break;
   1491  1.1  riastrad 		}
   1492  1.1  riastrad 		switch ((conf & 0x0f000000) >> 24) {
   1493  1.1  riastrad 		case 0xf:
   1494  1.3  riastrad 		case 0x4:
   1495  1.1  riastrad 			entry->dpconf.link_nr = 4;
   1496  1.1  riastrad 			break;
   1497  1.1  riastrad 		case 0x3:
   1498  1.3  riastrad 		case 0x2:
   1499  1.1  riastrad 			entry->dpconf.link_nr = 2;
   1500  1.1  riastrad 			break;
   1501  1.1  riastrad 		default:
   1502  1.1  riastrad 			entry->dpconf.link_nr = 1;
   1503  1.1  riastrad 			break;
   1504  1.1  riastrad 		}
   1505  1.3  riastrad 		link = entry->dpconf.sor.link;
   1506  1.1  riastrad 		break;
   1507  1.1  riastrad 	case DCB_OUTPUT_TMDS:
   1508  1.1  riastrad 		if (dcb->version >= 0x40) {
   1509  1.1  riastrad 			entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
   1510  1.1  riastrad 			entry->extdev = (conf & 0x0000ff00) >> 8;
   1511  1.3  riastrad 			link = entry->tmdsconf.sor.link;
   1512  1.1  riastrad 		}
   1513  1.1  riastrad 		else if (dcb->version >= 0x30)
   1514  1.1  riastrad 			entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
   1515  1.1  riastrad 		else if (dcb->version >= 0x22)
   1516  1.1  riastrad 			entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
   1517  1.1  riastrad 		break;
   1518  1.1  riastrad 	case DCB_OUTPUT_EOL:
   1519  1.1  riastrad 		/* weird g80 mobile type that "nv" treats as a terminator */
   1520  1.1  riastrad 		dcb->entries--;
   1521  1.1  riastrad 		return false;
   1522  1.1  riastrad 	default:
   1523  1.1  riastrad 		break;
   1524  1.1  riastrad 	}
   1525  1.1  riastrad 
   1526  1.1  riastrad 	if (dcb->version < 0x40) {
   1527  1.1  riastrad 		/* Normal entries consist of a single bit, but dual link has
   1528  1.1  riastrad 		 * the next most significant bit set too
   1529  1.1  riastrad 		 */
   1530  1.1  riastrad 		entry->duallink_possible =
   1531  1.1  riastrad 			((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
   1532  1.1  riastrad 	} else {
   1533  1.1  riastrad 		entry->duallink_possible = (entry->sorconf.link == 3);
   1534  1.1  riastrad 	}
   1535  1.1  riastrad 
   1536  1.1  riastrad 	/* unsure what DCB version introduces this, 3.0? */
   1537  1.1  riastrad 	if (conf & 0x100000)
   1538  1.1  riastrad 		entry->i2c_upper_default = true;
   1539  1.1  riastrad 
   1540  1.5  riastrad 	entry->hasht = (entry->extdev << 8) | (entry->location << 4) |
   1541  1.5  riastrad 			entry->type;
   1542  1.3  riastrad 	entry->hashm = (entry->heads << 8) | (link << 6) | entry->or;
   1543  1.1  riastrad 	return true;
   1544  1.1  riastrad }
   1545  1.1  riastrad 
   1546  1.1  riastrad static bool
   1547  1.1  riastrad parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
   1548  1.1  riastrad 		  uint32_t conn, uint32_t conf, struct dcb_output *entry)
   1549  1.1  riastrad {
   1550  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1551  1.1  riastrad 
   1552  1.1  riastrad 	switch (conn & 0x0000000f) {
   1553  1.1  riastrad 	case 0:
   1554  1.1  riastrad 		entry->type = DCB_OUTPUT_ANALOG;
   1555  1.1  riastrad 		break;
   1556  1.1  riastrad 	case 1:
   1557  1.1  riastrad 		entry->type = DCB_OUTPUT_TV;
   1558  1.1  riastrad 		break;
   1559  1.1  riastrad 	case 2:
   1560  1.1  riastrad 	case 4:
   1561  1.1  riastrad 		if (conn & 0x10)
   1562  1.1  riastrad 			entry->type = DCB_OUTPUT_LVDS;
   1563  1.1  riastrad 		else
   1564  1.1  riastrad 			entry->type = DCB_OUTPUT_TMDS;
   1565  1.1  riastrad 		break;
   1566  1.1  riastrad 	case 3:
   1567  1.1  riastrad 		entry->type = DCB_OUTPUT_LVDS;
   1568  1.1  riastrad 		break;
   1569  1.1  riastrad 	default:
   1570  1.1  riastrad 		NV_ERROR(drm, "Unknown DCB type %d\n", conn & 0x0000000f);
   1571  1.1  riastrad 		return false;
   1572  1.1  riastrad 	}
   1573  1.1  riastrad 
   1574  1.1  riastrad 	entry->i2c_index = (conn & 0x0003c000) >> 14;
   1575  1.1  riastrad 	entry->heads = ((conn & 0x001c0000) >> 18) + 1;
   1576  1.1  riastrad 	entry->or = entry->heads; /* same as heads, hopefully safe enough */
   1577  1.1  riastrad 	entry->location = (conn & 0x01e00000) >> 21;
   1578  1.1  riastrad 	entry->bus = (conn & 0x0e000000) >> 25;
   1579  1.1  riastrad 	entry->duallink_possible = false;
   1580  1.1  riastrad 
   1581  1.1  riastrad 	switch (entry->type) {
   1582  1.1  riastrad 	case DCB_OUTPUT_ANALOG:
   1583  1.1  riastrad 		entry->crtconf.maxfreq = (conf & 0xffff) * 10;
   1584  1.1  riastrad 		break;
   1585  1.1  riastrad 	case DCB_OUTPUT_TV:
   1586  1.1  riastrad 		entry->tvconf.has_component_output = false;
   1587  1.1  riastrad 		break;
   1588  1.1  riastrad 	case DCB_OUTPUT_LVDS:
   1589  1.1  riastrad 		if ((conn & 0x00003f00) >> 8 != 0x10)
   1590  1.1  riastrad 			entry->lvdsconf.use_straps_for_mode = true;
   1591  1.1  riastrad 		entry->lvdsconf.use_power_scripts = true;
   1592  1.1  riastrad 		break;
   1593  1.1  riastrad 	default:
   1594  1.1  riastrad 		break;
   1595  1.1  riastrad 	}
   1596  1.1  riastrad 
   1597  1.1  riastrad 	return true;
   1598  1.1  riastrad }
   1599  1.1  riastrad 
   1600  1.1  riastrad static
   1601  1.1  riastrad void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
   1602  1.1  riastrad {
   1603  1.1  riastrad 	/*
   1604  1.1  riastrad 	 * DCB v2.0 lists each output combination separately.
   1605  1.1  riastrad 	 * Here we merge compatible entries to have fewer outputs, with
   1606  1.1  riastrad 	 * more options
   1607  1.1  riastrad 	 */
   1608  1.1  riastrad 
   1609  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1610  1.1  riastrad 	int i, newentries = 0;
   1611  1.1  riastrad 
   1612  1.1  riastrad 	for (i = 0; i < dcb->entries; i++) {
   1613  1.1  riastrad 		struct dcb_output *ient = &dcb->entry[i];
   1614  1.1  riastrad 		int j;
   1615  1.1  riastrad 
   1616  1.1  riastrad 		for (j = i + 1; j < dcb->entries; j++) {
   1617  1.1  riastrad 			struct dcb_output *jent = &dcb->entry[j];
   1618  1.1  riastrad 
   1619  1.2  riastrad 			if (jent->type == DCB_OUTPUT_MERGED)
   1620  1.1  riastrad 				continue;
   1621  1.1  riastrad 
   1622  1.1  riastrad 			/* merge heads field when all other fields the same */
   1623  1.1  riastrad 			if (jent->i2c_index == ient->i2c_index &&
   1624  1.1  riastrad 			    jent->type == ient->type &&
   1625  1.1  riastrad 			    jent->location == ient->location &&
   1626  1.1  riastrad 			    jent->or == ient->or) {
   1627  1.1  riastrad 				NV_INFO(drm, "Merging DCB entries %d and %d\n",
   1628  1.1  riastrad 					 i, j);
   1629  1.1  riastrad 				ient->heads |= jent->heads;
   1630  1.2  riastrad 				jent->type = DCB_OUTPUT_MERGED;
   1631  1.1  riastrad 			}
   1632  1.1  riastrad 		}
   1633  1.1  riastrad 	}
   1634  1.1  riastrad 
   1635  1.1  riastrad 	/* Compact entries merged into others out of dcb */
   1636  1.1  riastrad 	for (i = 0; i < dcb->entries; i++) {
   1637  1.2  riastrad 		if (dcb->entry[i].type == DCB_OUTPUT_MERGED)
   1638  1.1  riastrad 			continue;
   1639  1.1  riastrad 
   1640  1.1  riastrad 		if (newentries != i) {
   1641  1.1  riastrad 			dcb->entry[newentries] = dcb->entry[i];
   1642  1.1  riastrad 			dcb->entry[newentries].index = newentries;
   1643  1.1  riastrad 		}
   1644  1.1  riastrad 		newentries++;
   1645  1.1  riastrad 	}
   1646  1.1  riastrad 
   1647  1.1  riastrad 	dcb->entries = newentries;
   1648  1.1  riastrad }
   1649  1.1  riastrad 
   1650  1.1  riastrad static bool
   1651  1.1  riastrad apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
   1652  1.1  riastrad {
   1653  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1654  1.1  riastrad 	struct dcb_table *dcb = &drm->vbios.dcb;
   1655  1.1  riastrad 
   1656  1.1  riastrad 	/* Dell Precision M6300
   1657  1.1  riastrad 	 *   DCB entry 2: 02025312 00000010
   1658  1.1  riastrad 	 *   DCB entry 3: 02026312 00000020
   1659  1.1  riastrad 	 *
   1660  1.1  riastrad 	 * Identical, except apparently a different connector on a
   1661  1.1  riastrad 	 * different SOR link.  Not a clue how we're supposed to know
   1662  1.1  riastrad 	 * which one is in use if it even shares an i2c line...
   1663  1.1  riastrad 	 *
   1664  1.1  riastrad 	 * Ignore the connector on the second SOR link to prevent
   1665  1.1  riastrad 	 * nasty problems until this is sorted (assuming it's not a
   1666  1.1  riastrad 	 * VBIOS bug).
   1667  1.1  riastrad 	 */
   1668  1.1  riastrad 	if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
   1669  1.1  riastrad 		if (*conn == 0x02026312 && *conf == 0x00000020)
   1670  1.1  riastrad 			return false;
   1671  1.1  riastrad 	}
   1672  1.1  riastrad 
   1673  1.1  riastrad 	/* GeForce3 Ti 200
   1674  1.1  riastrad 	 *
   1675  1.1  riastrad 	 * DCB reports an LVDS output that should be TMDS:
   1676  1.1  riastrad 	 *   DCB entry 1: f2005014 ffffffff
   1677  1.1  riastrad 	 */
   1678  1.1  riastrad 	if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
   1679  1.1  riastrad 		if (*conn == 0xf2005014 && *conf == 0xffffffff) {
   1680  1.1  riastrad 			fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 1, 1, 1);
   1681  1.1  riastrad 			return false;
   1682  1.1  riastrad 		}
   1683  1.1  riastrad 	}
   1684  1.1  riastrad 
   1685  1.1  riastrad 	/* XFX GT-240X-YA
   1686  1.1  riastrad 	 *
   1687  1.1  riastrad 	 * So many things wrong here, replace the entire encoder table..
   1688  1.1  riastrad 	 */
   1689  1.1  riastrad 	if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
   1690  1.1  riastrad 		if (idx == 0) {
   1691  1.1  riastrad 			*conn = 0x02001300; /* VGA, connector 1 */
   1692  1.1  riastrad 			*conf = 0x00000028;
   1693  1.1  riastrad 		} else
   1694  1.1  riastrad 		if (idx == 1) {
   1695  1.1  riastrad 			*conn = 0x01010312; /* DVI, connector 0 */
   1696  1.1  riastrad 			*conf = 0x00020030;
   1697  1.1  riastrad 		} else
   1698  1.1  riastrad 		if (idx == 2) {
   1699  1.1  riastrad 			*conn = 0x01010310; /* VGA, connector 0 */
   1700  1.1  riastrad 			*conf = 0x00000028;
   1701  1.1  riastrad 		} else
   1702  1.1  riastrad 		if (idx == 3) {
   1703  1.1  riastrad 			*conn = 0x02022362; /* HDMI, connector 2 */
   1704  1.1  riastrad 			*conf = 0x00020010;
   1705  1.1  riastrad 		} else {
   1706  1.1  riastrad 			*conn = 0x0000000e; /* EOL */
   1707  1.1  riastrad 			*conf = 0x00000000;
   1708  1.1  riastrad 		}
   1709  1.1  riastrad 	}
   1710  1.1  riastrad 
   1711  1.1  riastrad 	/* Some other twisted XFX board (rhbz#694914)
   1712  1.1  riastrad 	 *
   1713  1.1  riastrad 	 * The DVI/VGA encoder combo that's supposed to represent the
   1714  1.1  riastrad 	 * DVI-I connector actually point at two different ones, and
   1715  1.1  riastrad 	 * the HDMI connector ends up paired with the VGA instead.
   1716  1.1  riastrad 	 *
   1717  1.1  riastrad 	 * Connector table is missing anything for VGA at all, pointing it
   1718  1.1  riastrad 	 * an invalid conntab entry 2 so we figure it out ourself.
   1719  1.1  riastrad 	 */
   1720  1.1  riastrad 	if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
   1721  1.1  riastrad 		if (idx == 0) {
   1722  1.1  riastrad 			*conn = 0x02002300; /* VGA, connector 2 */
   1723  1.1  riastrad 			*conf = 0x00000028;
   1724  1.1  riastrad 		} else
   1725  1.1  riastrad 		if (idx == 1) {
   1726  1.1  riastrad 			*conn = 0x01010312; /* DVI, connector 0 */
   1727  1.1  riastrad 			*conf = 0x00020030;
   1728  1.1  riastrad 		} else
   1729  1.1  riastrad 		if (idx == 2) {
   1730  1.1  riastrad 			*conn = 0x04020310; /* VGA, connector 0 */
   1731  1.1  riastrad 			*conf = 0x00000028;
   1732  1.1  riastrad 		} else
   1733  1.1  riastrad 		if (idx == 3) {
   1734  1.1  riastrad 			*conn = 0x02021322; /* HDMI, connector 1 */
   1735  1.1  riastrad 			*conf = 0x00020010;
   1736  1.1  riastrad 		} else {
   1737  1.1  riastrad 			*conn = 0x0000000e; /* EOL */
   1738  1.1  riastrad 			*conf = 0x00000000;
   1739  1.1  riastrad 		}
   1740  1.1  riastrad 	}
   1741  1.1  riastrad 
   1742  1.1  riastrad 	/* fdo#50830: connector indices for VGA and DVI-I are backwards */
   1743  1.1  riastrad 	if (nv_match_device(dev, 0x0421, 0x3842, 0xc793)) {
   1744  1.1  riastrad 		if (idx == 0 && *conn == 0x02000300)
   1745  1.1  riastrad 			*conn = 0x02011300;
   1746  1.1  riastrad 		else
   1747  1.1  riastrad 		if (idx == 1 && *conn == 0x04011310)
   1748  1.1  riastrad 			*conn = 0x04000310;
   1749  1.1  riastrad 		else
   1750  1.1  riastrad 		if (idx == 2 && *conn == 0x02011312)
   1751  1.1  riastrad 			*conn = 0x02000312;
   1752  1.1  riastrad 	}
   1753  1.1  riastrad 
   1754  1.1  riastrad 	return true;
   1755  1.1  riastrad }
   1756  1.1  riastrad 
   1757  1.1  riastrad static void
   1758  1.1  riastrad fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
   1759  1.1  riastrad {
   1760  1.1  riastrad 	struct dcb_table *dcb = &bios->dcb;
   1761  1.1  riastrad 	int all_heads = (nv_two_heads(dev) ? 3 : 1);
   1762  1.1  riastrad 
   1763  1.1  riastrad #ifdef __powerpc__
   1764  1.1  riastrad 	/* Apple iMac G4 NV17 */
   1765  1.1  riastrad 	if (of_machine_is_compatible("PowerMac4,5")) {
   1766  1.1  riastrad 		fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 0, all_heads, 1);
   1767  1.1  riastrad 		fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG, 1, all_heads, 2);
   1768  1.1  riastrad 		return;
   1769  1.1  riastrad 	}
   1770  1.1  riastrad #endif
   1771  1.1  riastrad 
   1772  1.1  riastrad 	/* Make up some sane defaults */
   1773  1.1  riastrad 	fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG,
   1774  1.1  riastrad 			     bios->legacy.i2c_indices.crt, 1, 1);
   1775  1.1  riastrad 
   1776  1.1  riastrad 	if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
   1777  1.1  riastrad 		fabricate_dcb_output(dcb, DCB_OUTPUT_TV,
   1778  1.1  riastrad 				     bios->legacy.i2c_indices.tv,
   1779  1.1  riastrad 				     all_heads, 0);
   1780  1.1  riastrad 
   1781  1.1  riastrad 	else if (bios->tmds.output0_script_ptr ||
   1782  1.1  riastrad 		 bios->tmds.output1_script_ptr)
   1783  1.1  riastrad 		fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS,
   1784  1.1  riastrad 				     bios->legacy.i2c_indices.panel,
   1785  1.1  riastrad 				     all_heads, 1);
   1786  1.1  riastrad }
   1787  1.1  riastrad 
   1788  1.1  riastrad static int
   1789  1.1  riastrad parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
   1790  1.1  riastrad {
   1791  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1792  1.1  riastrad 	struct dcb_table *dcb = &drm->vbios.dcb;
   1793  1.1  riastrad 	u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
   1794  1.1  riastrad 	u32 conn = ROM32(outp[0]);
   1795  1.1  riastrad 	bool ret;
   1796  1.1  riastrad 
   1797  1.1  riastrad 	if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
   1798  1.1  riastrad 		struct dcb_output *entry = new_dcb_entry(dcb);
   1799  1.1  riastrad 
   1800  1.1  riastrad 		NV_INFO(drm, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
   1801  1.1  riastrad 
   1802  1.1  riastrad 		if (dcb->version >= 0x20)
   1803  1.1  riastrad 			ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
   1804  1.1  riastrad 		else
   1805  1.1  riastrad 			ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
   1806  1.1  riastrad 		if (!ret)
   1807  1.1  riastrad 			return 1; /* stop parsing */
   1808  1.1  riastrad 
   1809  1.1  riastrad 		/* Ignore the I2C index for on-chip TV-out, as there
   1810  1.1  riastrad 		 * are cards with bogus values (nv31m in bug 23212),
   1811  1.1  riastrad 		 * and it's otherwise useless.
   1812  1.1  riastrad 		 */
   1813  1.1  riastrad 		if (entry->type == DCB_OUTPUT_TV &&
   1814  1.1  riastrad 		    entry->location == DCB_LOC_ON_CHIP)
   1815  1.1  riastrad 			entry->i2c_index = 0x0f;
   1816  1.1  riastrad 	}
   1817  1.1  riastrad 
   1818  1.1  riastrad 	return 0;
   1819  1.1  riastrad }
   1820  1.1  riastrad 
   1821  1.1  riastrad static void
   1822  1.1  riastrad dcb_fake_connectors(struct nvbios *bios)
   1823  1.1  riastrad {
   1824  1.1  riastrad 	struct dcb_table *dcbt = &bios->dcb;
   1825  1.1  riastrad 	u8 map[16] = { };
   1826  1.1  riastrad 	int i, idx = 0;
   1827  1.1  riastrad 
   1828  1.1  riastrad 	/* heuristic: if we ever get a non-zero connector field, assume
   1829  1.1  riastrad 	 * that all the indices are valid and we don't need fake them.
   1830  1.1  riastrad 	 *
   1831  1.1  riastrad 	 * and, as usual, a blacklist of boards with bad bios data..
   1832  1.1  riastrad 	 */
   1833  1.1  riastrad 	if (!nv_match_device(bios->dev, 0x0392, 0x107d, 0x20a2)) {
   1834  1.1  riastrad 		for (i = 0; i < dcbt->entries; i++) {
   1835  1.1  riastrad 			if (dcbt->entry[i].connector)
   1836  1.1  riastrad 				return;
   1837  1.1  riastrad 		}
   1838  1.1  riastrad 	}
   1839  1.1  riastrad 
   1840  1.1  riastrad 	/* no useful connector info available, we need to make it up
   1841  1.1  riastrad 	 * ourselves.  the rule here is: anything on the same i2c bus
   1842  1.1  riastrad 	 * is considered to be on the same connector.  any output
   1843  1.1  riastrad 	 * without an associated i2c bus is assigned its own unique
   1844  1.1  riastrad 	 * connector index.
   1845  1.1  riastrad 	 */
   1846  1.1  riastrad 	for (i = 0; i < dcbt->entries; i++) {
   1847  1.1  riastrad 		u8 i2c = dcbt->entry[i].i2c_index;
   1848  1.1  riastrad 		if (i2c == 0x0f) {
   1849  1.1  riastrad 			dcbt->entry[i].connector = idx++;
   1850  1.1  riastrad 		} else {
   1851  1.1  riastrad 			if (!map[i2c])
   1852  1.1  riastrad 				map[i2c] = ++idx;
   1853  1.1  riastrad 			dcbt->entry[i].connector = map[i2c] - 1;
   1854  1.1  riastrad 		}
   1855  1.1  riastrad 	}
   1856  1.1  riastrad 
   1857  1.1  riastrad 	/* if we created more than one connector, destroy the connector
   1858  1.1  riastrad 	 * table - just in case it has random, rather than stub, entries.
   1859  1.1  riastrad 	 */
   1860  1.1  riastrad 	if (i > 1) {
   1861  1.1  riastrad 		u8 *conntab = olddcb_conntab(bios->dev);
   1862  1.1  riastrad 		if (conntab)
   1863  1.1  riastrad 			conntab[0] = 0x00;
   1864  1.1  riastrad 	}
   1865  1.1  riastrad }
   1866  1.1  riastrad 
   1867  1.1  riastrad static int
   1868  1.1  riastrad parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
   1869  1.1  riastrad {
   1870  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1871  1.1  riastrad 	struct dcb_table *dcb = &bios->dcb;
   1872  1.1  riastrad 	u8 *dcbt, *conn;
   1873  1.1  riastrad 	int idx;
   1874  1.1  riastrad 
   1875  1.1  riastrad 	dcbt = olddcb_table(dev);
   1876  1.1  riastrad 	if (!dcbt) {
   1877  1.1  riastrad 		/* handle pre-DCB boards */
   1878  1.1  riastrad 		if (bios->type == NVBIOS_BMP) {
   1879  1.1  riastrad 			fabricate_dcb_encoder_table(dev, bios);
   1880  1.1  riastrad 			return 0;
   1881  1.1  riastrad 		}
   1882  1.1  riastrad 
   1883  1.1  riastrad 		return -EINVAL;
   1884  1.1  riastrad 	}
   1885  1.1  riastrad 
   1886  1.1  riastrad 	NV_INFO(drm, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
   1887  1.1  riastrad 
   1888  1.1  riastrad 	dcb->version = dcbt[0];
   1889  1.1  riastrad 	olddcb_outp_foreach(dev, NULL, parse_dcb_entry);
   1890  1.1  riastrad 
   1891  1.1  riastrad 	/*
   1892  1.1  riastrad 	 * apart for v2.1+ not being known for requiring merging, this
   1893  1.1  riastrad 	 * guarantees dcbent->index is the index of the entry in the rom image
   1894  1.1  riastrad 	 */
   1895  1.1  riastrad 	if (dcb->version < 0x21)
   1896  1.1  riastrad 		merge_like_dcb_entries(dev, dcb);
   1897  1.1  riastrad 
   1898  1.1  riastrad 	/* dump connector table entries to log, if any exist */
   1899  1.1  riastrad 	idx = -1;
   1900  1.1  riastrad 	while ((conn = olddcb_conn(dev, ++idx))) {
   1901  1.1  riastrad 		if (conn[0] != 0xff) {
   1902  1.1  riastrad 			if (olddcb_conntab(dev)[3] < 4)
   1903  1.3  riastrad 				NV_INFO(drm, "DCB conn %02d: %04x\n",
   1904  1.3  riastrad 					idx, ROM16(conn[0]));
   1905  1.1  riastrad 			else
   1906  1.3  riastrad 				NV_INFO(drm, "DCB conn %02d: %08x\n",
   1907  1.3  riastrad 					idx, ROM32(conn[0]));
   1908  1.1  riastrad 		}
   1909  1.1  riastrad 	}
   1910  1.1  riastrad 	dcb_fake_connectors(bios);
   1911  1.1  riastrad 	return 0;
   1912  1.1  riastrad }
   1913  1.1  riastrad 
   1914  1.1  riastrad static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
   1915  1.1  riastrad {
   1916  1.1  riastrad 	/*
   1917  1.1  riastrad 	 * The header following the "HWSQ" signature has the number of entries,
   1918  1.1  riastrad 	 * and the entry size
   1919  1.1  riastrad 	 *
   1920  1.1  riastrad 	 * An entry consists of a dword to write to the sequencer control reg
   1921  1.1  riastrad 	 * (0x00001304), followed by the ucode bytes, written sequentially,
   1922  1.1  riastrad 	 * starting at reg 0x00001400
   1923  1.1  riastrad 	 */
   1924  1.1  riastrad 
   1925  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1926  1.5  riastrad 	struct nvif_object *device = &drm->client.device.object;
   1927  1.1  riastrad 	uint8_t bytes_to_write;
   1928  1.1  riastrad 	uint16_t hwsq_entry_offset;
   1929  1.1  riastrad 	int i;
   1930  1.1  riastrad 
   1931  1.1  riastrad 	if (bios->data[hwsq_offset] <= entry) {
   1932  1.1  riastrad 		NV_ERROR(drm, "Too few entries in HW sequencer table for "
   1933  1.1  riastrad 				"requested entry\n");
   1934  1.1  riastrad 		return -ENOENT;
   1935  1.1  riastrad 	}
   1936  1.1  riastrad 
   1937  1.1  riastrad 	bytes_to_write = bios->data[hwsq_offset + 1];
   1938  1.1  riastrad 
   1939  1.1  riastrad 	if (bytes_to_write != 36) {
   1940  1.1  riastrad 		NV_ERROR(drm, "Unknown HW sequencer entry size\n");
   1941  1.1  riastrad 		return -EINVAL;
   1942  1.1  riastrad 	}
   1943  1.1  riastrad 
   1944  1.1  riastrad 	NV_INFO(drm, "Loading NV17 power sequencing microcode\n");
   1945  1.1  riastrad 
   1946  1.1  riastrad 	hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
   1947  1.1  riastrad 
   1948  1.1  riastrad 	/* set sequencer control */
   1949  1.3  riastrad 	nvif_wr32(device, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
   1950  1.1  riastrad 	bytes_to_write -= 4;
   1951  1.1  riastrad 
   1952  1.1  riastrad 	/* write ucode */
   1953  1.1  riastrad 	for (i = 0; i < bytes_to_write; i += 4)
   1954  1.3  riastrad 		nvif_wr32(device, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
   1955  1.1  riastrad 
   1956  1.1  riastrad 	/* twiddle NV_PBUS_DEBUG_4 */
   1957  1.3  riastrad 	nvif_wr32(device, NV_PBUS_DEBUG_4, nvif_rd32(device, NV_PBUS_DEBUG_4) | 0x18);
   1958  1.1  riastrad 
   1959  1.1  riastrad 	return 0;
   1960  1.1  riastrad }
   1961  1.1  riastrad 
   1962  1.1  riastrad static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
   1963  1.1  riastrad 					struct nvbios *bios)
   1964  1.1  riastrad {
   1965  1.1  riastrad 	/*
   1966  1.1  riastrad 	 * BMP based cards, from NV17, need a microcode loading to correctly
   1967  1.1  riastrad 	 * control the GPIO etc for LVDS panels
   1968  1.1  riastrad 	 *
   1969  1.1  riastrad 	 * BIT based cards seem to do this directly in the init scripts
   1970  1.1  riastrad 	 *
   1971  1.1  riastrad 	 * The microcode entries are found by the "HWSQ" signature.
   1972  1.1  riastrad 	 */
   1973  1.1  riastrad 
   1974  1.5  riastrad 	static const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
   1975  1.1  riastrad 	const int sz = sizeof(hwsq_signature);
   1976  1.1  riastrad 	int hwsq_offset;
   1977  1.1  riastrad 
   1978  1.1  riastrad 	hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
   1979  1.1  riastrad 	if (!hwsq_offset)
   1980  1.1  riastrad 		return 0;
   1981  1.1  riastrad 
   1982  1.1  riastrad 	/* always use entry 0? */
   1983  1.1  riastrad 	return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
   1984  1.1  riastrad }
   1985  1.1  riastrad 
   1986  1.1  riastrad uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
   1987  1.1  riastrad {
   1988  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   1989  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
   1990  1.5  riastrad 	static const uint8_t edid_sig[] = {
   1991  1.1  riastrad 			0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
   1992  1.1  riastrad 	uint16_t offset = 0;
   1993  1.1  riastrad 	uint16_t newoffset;
   1994  1.1  riastrad 	int searchlen = NV_PROM_SIZE;
   1995  1.1  riastrad 
   1996  1.1  riastrad 	if (bios->fp.edid)
   1997  1.1  riastrad 		return bios->fp.edid;
   1998  1.1  riastrad 
   1999  1.1  riastrad 	while (searchlen) {
   2000  1.1  riastrad 		newoffset = findstr(&bios->data[offset], searchlen,
   2001  1.1  riastrad 								edid_sig, 8);
   2002  1.1  riastrad 		if (!newoffset)
   2003  1.1  riastrad 			return NULL;
   2004  1.1  riastrad 		offset += newoffset;
   2005  1.1  riastrad 		if (!nv_cksum(&bios->data[offset], EDID1_LEN))
   2006  1.1  riastrad 			break;
   2007  1.1  riastrad 
   2008  1.1  riastrad 		searchlen -= offset;
   2009  1.1  riastrad 		offset++;
   2010  1.1  riastrad 	}
   2011  1.1  riastrad 
   2012  1.1  riastrad 	NV_INFO(drm, "Found EDID in BIOS\n");
   2013  1.1  riastrad 
   2014  1.1  riastrad 	return bios->fp.edid = &bios->data[offset];
   2015  1.1  riastrad }
   2016  1.1  riastrad 
   2017  1.1  riastrad static bool NVInitVBIOS(struct drm_device *dev)
   2018  1.1  riastrad {
   2019  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   2020  1.5  riastrad 	struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
   2021  1.1  riastrad 	struct nvbios *legacy = &drm->vbios;
   2022  1.1  riastrad 
   2023  1.1  riastrad 	memset(legacy, 0, sizeof(struct nvbios));
   2024  1.1  riastrad 	spin_lock_init(&legacy->lock);
   2025  1.1  riastrad 	legacy->dev = dev;
   2026  1.1  riastrad 
   2027  1.1  riastrad 	legacy->data = bios->data;
   2028  1.1  riastrad 	legacy->length = bios->size;
   2029  1.1  riastrad 	legacy->major_version = bios->version.major;
   2030  1.1  riastrad 	legacy->chip_version = bios->version.chip;
   2031  1.1  riastrad 	if (bios->bit_offset) {
   2032  1.1  riastrad 		legacy->type = NVBIOS_BIT;
   2033  1.1  riastrad 		legacy->offset = bios->bit_offset;
   2034  1.1  riastrad 		return !parse_bit_structure(legacy, legacy->offset + 6);
   2035  1.1  riastrad 	} else
   2036  1.1  riastrad 	if (bios->bmp_offset) {
   2037  1.1  riastrad 		legacy->type = NVBIOS_BMP;
   2038  1.1  riastrad 		legacy->offset = bios->bmp_offset;
   2039  1.1  riastrad 		return !parse_bmp_structure(dev, legacy, legacy->offset);
   2040  1.1  riastrad 	}
   2041  1.1  riastrad 
   2042  1.1  riastrad 	return false;
   2043  1.1  riastrad }
   2044  1.1  riastrad 
   2045  1.1  riastrad int
   2046  1.1  riastrad nouveau_run_vbios_init(struct drm_device *dev)
   2047  1.1  riastrad {
   2048  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   2049  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
   2050  1.1  riastrad 	int ret = 0;
   2051  1.1  riastrad 
   2052  1.1  riastrad 	/* Reset the BIOS head to 0. */
   2053  1.1  riastrad 	bios->state.crtchead = 0;
   2054  1.1  riastrad 
   2055  1.1  riastrad 	if (bios->major_version < 5)	/* BMP only */
   2056  1.1  riastrad 		load_nv17_hw_sequencer_ucode(dev, bios);
   2057  1.1  riastrad 
   2058  1.1  riastrad 	if (bios->execute) {
   2059  1.1  riastrad 		bios->fp.last_script_invoc = 0;
   2060  1.1  riastrad 		bios->fp.lvds_init_run = false;
   2061  1.1  riastrad 	}
   2062  1.1  riastrad 
   2063  1.1  riastrad 	return ret;
   2064  1.1  riastrad }
   2065  1.1  riastrad 
   2066  1.1  riastrad static bool
   2067  1.1  riastrad nouveau_bios_posted(struct drm_device *dev)
   2068  1.1  riastrad {
   2069  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   2070  1.1  riastrad 	unsigned htotal;
   2071  1.1  riastrad 
   2072  1.5  riastrad 	if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
   2073  1.1  riastrad 		return true;
   2074  1.1  riastrad 
   2075  1.1  riastrad 	htotal  = NVReadVgaCrtc(dev, 0, 0x06);
   2076  1.1  riastrad 	htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
   2077  1.1  riastrad 	htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
   2078  1.1  riastrad 	htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
   2079  1.1  riastrad 	htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
   2080  1.1  riastrad 	return (htotal != 0);
   2081  1.1  riastrad }
   2082  1.1  riastrad 
   2083  1.1  riastrad int
   2084  1.1  riastrad nouveau_bios_init(struct drm_device *dev)
   2085  1.1  riastrad {
   2086  1.1  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   2087  1.1  riastrad 	struct nvbios *bios = &drm->vbios;
   2088  1.1  riastrad 	int ret;
   2089  1.1  riastrad 
   2090  1.1  riastrad 	/* only relevant for PCI devices */
   2091  1.1  riastrad 	if (!dev->pdev)
   2092  1.1  riastrad 		return 0;
   2093  1.1  riastrad 
   2094  1.1  riastrad 	if (!NVInitVBIOS(dev))
   2095  1.1  riastrad 		return -ENODEV;
   2096  1.1  riastrad 
   2097  1.1  riastrad 	ret = parse_dcb_table(dev, bios);
   2098  1.1  riastrad 	if (ret)
   2099  1.1  riastrad 		return ret;
   2100  1.1  riastrad 
   2101  1.1  riastrad 	if (!bios->major_version)	/* we don't run version 0 bios */
   2102  1.1  riastrad 		return 0;
   2103  1.1  riastrad 
   2104  1.1  riastrad 	/* init script execution disabled */
   2105  1.1  riastrad 	bios->execute = false;
   2106  1.1  riastrad 
   2107  1.1  riastrad 	/* ... unless card isn't POSTed already */
   2108  1.1  riastrad 	if (!nouveau_bios_posted(dev)) {
   2109  1.1  riastrad 		NV_INFO(drm, "Adaptor not initialised, "
   2110  1.1  riastrad 			"running VBIOS init tables.\n");
   2111  1.1  riastrad 		bios->execute = true;
   2112  1.1  riastrad 	}
   2113  1.1  riastrad 
   2114  1.1  riastrad 	ret = nouveau_run_vbios_init(dev);
   2115  1.1  riastrad 	if (ret)
   2116  1.1  riastrad 		return ret;
   2117  1.1  riastrad 
   2118  1.1  riastrad 	/* feature_byte on BMP is poor, but init always sets CR4B */
   2119  1.1  riastrad 	if (bios->major_version < 5)
   2120  1.1  riastrad 		bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
   2121  1.1  riastrad 
   2122  1.1  riastrad 	/* all BIT systems need p_f_m_t for digital_min_front_porch */
   2123  1.1  riastrad 	if (bios->is_mobile || bios->major_version >= 5)
   2124  1.1  riastrad 		ret = parse_fp_mode_table(dev, bios);
   2125  1.1  riastrad 
   2126  1.1  riastrad 	/* allow subsequent scripts to execute */
   2127  1.1  riastrad 	bios->execute = true;
   2128  1.1  riastrad 
   2129  1.1  riastrad 	return 0;
   2130  1.1  riastrad }
   2131  1.1  riastrad 
   2132  1.1  riastrad void
   2133  1.1  riastrad nouveau_bios_takedown(struct drm_device *dev)
   2134  1.1  riastrad {
   2135  1.6  riastrad 	struct nouveau_drm *drm = nouveau_drm(dev);
   2136  1.6  riastrad 	struct nvbios *legacy = &drm->vbios;
   2137  1.6  riastrad 
   2138  1.6  riastrad 	spin_lock_destroy(&legacy->lock);
   2139  1.1  riastrad }
   2140