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