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