drm/nouveau: support init table i2c device identifier 0x81
[safe/jmp/linux-2.6] / 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 "drmP.h"
26 #define NV_DEBUG_NOTRACE
27 #include "nouveau_drv.h"
28 #include "nouveau_hw.h"
29 #include "nouveau_encoder.h"
30
31 /* these defines are made up */
32 #define NV_CIO_CRE_44_HEADA 0x0
33 #define NV_CIO_CRE_44_HEADB 0x3
34 #define FEATURE_MOBILE 0x10     /* also FEATURE_QUADRO for BMP */
35 #define LEGACY_I2C_CRT 0x80
36 #define LEGACY_I2C_PANEL 0x81
37 #define LEGACY_I2C_TV 0x82
38
39 #define EDID1_LEN 128
40
41 #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
42 #define LOG_OLD_VALUE(x)
43
44 #define ROM16(x) le16_to_cpu(*(uint16_t *)&(x))
45 #define ROM32(x) le32_to_cpu(*(uint32_t *)&(x))
46
47 struct init_exec {
48         bool execute;
49         bool repeat;
50 };
51
52 static bool nv_cksum(const uint8_t *data, unsigned int length)
53 {
54         /*
55          * There's a few checksums in the BIOS, so here's a generic checking
56          * function.
57          */
58         int i;
59         uint8_t sum = 0;
60
61         for (i = 0; i < length; i++)
62                 sum += data[i];
63
64         if (sum)
65                 return true;
66
67         return false;
68 }
69
70 static int
71 score_vbios(struct drm_device *dev, const uint8_t *data, const bool writeable)
72 {
73         if (!(data[0] == 0x55 && data[1] == 0xAA)) {
74                 NV_TRACEWARN(dev, "... BIOS signature not found\n");
75                 return 0;
76         }
77
78         if (nv_cksum(data, data[2] * 512)) {
79                 NV_TRACEWARN(dev, "... BIOS checksum invalid\n");
80                 /* if a ro image is somewhat bad, it's probably all rubbish */
81                 return writeable ? 2 : 1;
82         } else
83                 NV_TRACE(dev, "... appears to be valid\n");
84
85         return 3;
86 }
87
88 static void load_vbios_prom(struct drm_device *dev, uint8_t *data)
89 {
90         struct drm_nouveau_private *dev_priv = dev->dev_private;
91         uint32_t pci_nv_20, save_pci_nv_20;
92         int pcir_ptr;
93         int i;
94
95         if (dev_priv->card_type >= NV_50)
96                 pci_nv_20 = 0x88050;
97         else
98                 pci_nv_20 = NV_PBUS_PCI_NV_20;
99
100         /* enable ROM access */
101         save_pci_nv_20 = nvReadMC(dev, pci_nv_20);
102         nvWriteMC(dev, pci_nv_20,
103                   save_pci_nv_20 & ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
104
105         /* bail if no rom signature */
106         if (nv_rd08(dev, NV_PROM_OFFSET) != 0x55 ||
107             nv_rd08(dev, NV_PROM_OFFSET + 1) != 0xaa)
108                 goto out;
109
110         /* additional check (see note below) - read PCI record header */
111         pcir_ptr = nv_rd08(dev, NV_PROM_OFFSET + 0x18) |
112                    nv_rd08(dev, NV_PROM_OFFSET + 0x19) << 8;
113         if (nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr) != 'P' ||
114             nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 1) != 'C' ||
115             nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 2) != 'I' ||
116             nv_rd08(dev, NV_PROM_OFFSET + pcir_ptr + 3) != 'R')
117                 goto out;
118
119         /* on some 6600GT/6800LE prom reads are messed up.  nvclock alleges a
120          * a good read may be obtained by waiting or re-reading (cargocult: 5x)
121          * each byte.  we'll hope pramin has something usable instead
122          */
123         for (i = 0; i < NV_PROM_SIZE; i++)
124                 data[i] = nv_rd08(dev, NV_PROM_OFFSET + i);
125
126 out:
127         /* disable ROM access */
128         nvWriteMC(dev, pci_nv_20,
129                   save_pci_nv_20 | NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
130 }
131
132 static void load_vbios_pramin(struct drm_device *dev, uint8_t *data)
133 {
134         struct drm_nouveau_private *dev_priv = dev->dev_private;
135         uint32_t old_bar0_pramin = 0;
136         int i;
137
138         if (dev_priv->card_type >= NV_50) {
139                 uint32_t vbios_vram = (nv_rd32(dev, 0x619f04) & ~0xff) << 8;
140
141                 if (!vbios_vram)
142                         vbios_vram = (nv_rd32(dev, 0x1700) << 16) + 0xf0000;
143
144                 old_bar0_pramin = nv_rd32(dev, 0x1700);
145                 nv_wr32(dev, 0x1700, vbios_vram >> 16);
146         }
147
148         /* bail if no rom signature */
149         if (nv_rd08(dev, NV_PRAMIN_OFFSET) != 0x55 ||
150             nv_rd08(dev, NV_PRAMIN_OFFSET + 1) != 0xaa)
151                 goto out;
152
153         for (i = 0; i < NV_PROM_SIZE; i++)
154                 data[i] = nv_rd08(dev, NV_PRAMIN_OFFSET + i);
155
156 out:
157         if (dev_priv->card_type >= NV_50)
158                 nv_wr32(dev, 0x1700, old_bar0_pramin);
159 }
160
161 static void load_vbios_pci(struct drm_device *dev, uint8_t *data)
162 {
163         void __iomem *rom = NULL;
164         size_t rom_len;
165         int ret;
166
167         ret = pci_enable_rom(dev->pdev);
168         if (ret)
169                 return;
170
171         rom = pci_map_rom(dev->pdev, &rom_len);
172         if (!rom)
173                 goto out;
174         memcpy_fromio(data, rom, rom_len);
175         pci_unmap_rom(dev->pdev, rom);
176
177 out:
178         pci_disable_rom(dev->pdev);
179 }
180
181 struct methods {
182         const char desc[8];
183         void (*loadbios)(struct drm_device *, uint8_t *);
184         const bool rw;
185 };
186
187 static struct methods nv04_methods[] = {
188         { "PROM", load_vbios_prom, false },
189         { "PRAMIN", load_vbios_pramin, true },
190         { "PCIROM", load_vbios_pci, true },
191 };
192
193 static struct methods nv50_methods[] = {
194         { "PRAMIN", load_vbios_pramin, true },
195         { "PROM", load_vbios_prom, false },
196         { "PCIROM", load_vbios_pci, true },
197 };
198
199 #define METHODCNT 3
200
201 static bool NVShadowVBIOS(struct drm_device *dev, uint8_t *data)
202 {
203         struct drm_nouveau_private *dev_priv = dev->dev_private;
204         struct methods *methods;
205         int i;
206         int testscore = 3;
207         int scores[METHODCNT];
208
209         if (nouveau_vbios) {
210                 methods = nv04_methods;
211                 for (i = 0; i < METHODCNT; i++)
212                         if (!strcasecmp(nouveau_vbios, methods[i].desc))
213                                 break;
214
215                 if (i < METHODCNT) {
216                         NV_INFO(dev, "Attempting to use BIOS image from %s\n",
217                                 methods[i].desc);
218
219                         methods[i].loadbios(dev, data);
220                         if (score_vbios(dev, data, methods[i].rw))
221                                 return true;
222                 }
223
224                 NV_ERROR(dev, "VBIOS source \'%s\' invalid\n", nouveau_vbios);
225         }
226
227         if (dev_priv->card_type < NV_50)
228                 methods = nv04_methods;
229         else
230                 methods = nv50_methods;
231
232         for (i = 0; i < METHODCNT; i++) {
233                 NV_TRACE(dev, "Attempting to load BIOS image from %s\n",
234                          methods[i].desc);
235                 data[0] = data[1] = 0;  /* avoid reuse of previous image */
236                 methods[i].loadbios(dev, data);
237                 scores[i] = score_vbios(dev, data, methods[i].rw);
238                 if (scores[i] == testscore)
239                         return true;
240         }
241
242         while (--testscore > 0) {
243                 for (i = 0; i < METHODCNT; i++) {
244                         if (scores[i] == testscore) {
245                                 NV_TRACE(dev, "Using BIOS image from %s\n",
246                                          methods[i].desc);
247                                 methods[i].loadbios(dev, data);
248                                 return true;
249                         }
250                 }
251         }
252
253         NV_ERROR(dev, "No valid BIOS image found\n");
254         return false;
255 }
256
257 struct init_tbl_entry {
258         char *name;
259         uint8_t id;
260         /* Return:
261          *  > 0: success, length of opcode
262          *    0: success, but abort further parsing of table (INIT_DONE etc)
263          *  < 0: failure, table parsing will be aborted
264          */
265         int (*handler)(struct nvbios *, uint16_t, struct init_exec *);
266 };
267
268 struct bit_entry {
269         uint8_t id[2];
270         uint16_t length;
271         uint16_t offset;
272 };
273
274 static int parse_init_table(struct nvbios *, unsigned int, struct init_exec *);
275
276 #define MACRO_INDEX_SIZE        2
277 #define MACRO_SIZE              8
278 #define CONDITION_SIZE          12
279 #define IO_FLAG_CONDITION_SIZE  9
280 #define IO_CONDITION_SIZE       5
281 #define MEM_INIT_SIZE           66
282
283 static void still_alive(void)
284 {
285 #if 0
286         sync();
287         msleep(2);
288 #endif
289 }
290
291 static uint32_t
292 munge_reg(struct nvbios *bios, uint32_t reg)
293 {
294         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
295         struct dcb_entry *dcbent = bios->display.output;
296
297         if (dev_priv->card_type < NV_50)
298                 return reg;
299
300         if (reg & 0x40000000) {
301                 BUG_ON(!dcbent);
302
303                 reg += (ffs(dcbent->or) - 1) * 0x800;
304                 if ((reg & 0x20000000) && !(dcbent->sorconf.link & 1))
305                         reg += 0x00000080;
306         }
307
308         reg &= ~0x60000000;
309         return reg;
310 }
311
312 static int
313 valid_reg(struct nvbios *bios, uint32_t reg)
314 {
315         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
316         struct drm_device *dev = bios->dev;
317
318         /* C51 has misaligned regs on purpose. Marvellous */
319         if (reg & 0x2 ||
320             (reg & 0x1 && dev_priv->vbios.chip_version != 0x51))
321                 NV_ERROR(dev, "======= misaligned reg 0x%08X =======\n", reg);
322
323         /* warn on C51 regs that haven't been verified accessible in tracing */
324         if (reg & 0x1 && dev_priv->vbios.chip_version == 0x51 &&
325             reg != 0x130d && reg != 0x1311 && reg != 0x60081d)
326                 NV_WARN(dev, "=== C51 misaligned reg 0x%08X not verified ===\n",
327                         reg);
328
329         if (reg >= (8*1024*1024)) {
330                 NV_ERROR(dev, "=== reg 0x%08x out of mapped bounds ===\n", reg);
331                 return 0;
332         }
333
334         return 1;
335 }
336
337 static bool
338 valid_idx_port(struct nvbios *bios, uint16_t port)
339 {
340         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
341         struct drm_device *dev = bios->dev;
342
343         /*
344          * If adding more ports here, the read/write functions below will need
345          * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
346          * used for the port in question
347          */
348         if (dev_priv->card_type < NV_50) {
349                 if (port == NV_CIO_CRX__COLOR)
350                         return true;
351                 if (port == NV_VIO_SRX)
352                         return true;
353         } else {
354                 if (port == NV_CIO_CRX__COLOR)
355                         return true;
356         }
357
358         NV_ERROR(dev, "========== unknown indexed io port 0x%04X ==========\n",
359                  port);
360
361         return false;
362 }
363
364 static bool
365 valid_port(struct nvbios *bios, uint16_t port)
366 {
367         struct drm_device *dev = bios->dev;
368
369         /*
370          * If adding more ports here, the read/write functions below will need
371          * updating so that the correct mmio range (PRMCIO, PRMDIO, PRMVIO) is
372          * used for the port in question
373          */
374         if (port == NV_VIO_VSE2)
375                 return true;
376
377         NV_ERROR(dev, "========== unknown io port 0x%04X ==========\n", port);
378
379         return false;
380 }
381
382 static uint32_t
383 bios_rd32(struct nvbios *bios, uint32_t reg)
384 {
385         uint32_t data;
386
387         reg = munge_reg(bios, reg);
388         if (!valid_reg(bios, reg))
389                 return 0;
390
391         /*
392          * C51 sometimes uses regs with bit0 set in the address. For these
393          * cases there should exist a translation in a BIOS table to an IO
394          * port address which the BIOS uses for accessing the reg
395          *
396          * These only seem to appear for the power control regs to a flat panel,
397          * and the GPIO regs at 0x60081*.  In C51 mmio traces the normal regs
398          * for 0x1308 and 0x1310 are used - hence the mask below.  An S3
399          * suspend-resume mmio trace from a C51 will be required to see if this
400          * is true for the power microcode in 0x14.., or whether the direct IO
401          * port access method is needed
402          */
403         if (reg & 0x1)
404                 reg &= ~0x1;
405
406         data = nv_rd32(bios->dev, reg);
407
408         BIOSLOG(bios, " Read:  Reg: 0x%08X, Data: 0x%08X\n", reg, data);
409
410         return data;
411 }
412
413 static void
414 bios_wr32(struct nvbios *bios, uint32_t reg, uint32_t data)
415 {
416         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
417
418         reg = munge_reg(bios, reg);
419         if (!valid_reg(bios, reg))
420                 return;
421
422         /* see note in bios_rd32 */
423         if (reg & 0x1)
424                 reg &= 0xfffffffe;
425
426         LOG_OLD_VALUE(bios_rd32(bios, reg));
427         BIOSLOG(bios, " Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
428
429         if (dev_priv->vbios.execute) {
430                 still_alive();
431                 nv_wr32(bios->dev, reg, data);
432         }
433 }
434
435 static uint8_t
436 bios_idxprt_rd(struct nvbios *bios, uint16_t port, uint8_t index)
437 {
438         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
439         struct drm_device *dev = bios->dev;
440         uint8_t data;
441
442         if (!valid_idx_port(bios, port))
443                 return 0;
444
445         if (dev_priv->card_type < NV_50) {
446                 if (port == NV_VIO_SRX)
447                         data = NVReadVgaSeq(dev, bios->state.crtchead, index);
448                 else    /* assume NV_CIO_CRX__COLOR */
449                         data = NVReadVgaCrtc(dev, bios->state.crtchead, index);
450         } else {
451                 uint32_t data32;
452
453                 data32 = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
454                 data = (data32 >> ((index & 3) << 3)) & 0xff;
455         }
456
457         BIOSLOG(bios, " Indexed IO read:  Port: 0x%04X, Index: 0x%02X, "
458                       "Head: 0x%02X, Data: 0x%02X\n",
459                 port, index, bios->state.crtchead, data);
460         return data;
461 }
462
463 static void
464 bios_idxprt_wr(struct nvbios *bios, uint16_t port, uint8_t index, uint8_t data)
465 {
466         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
467         struct drm_device *dev = bios->dev;
468
469         if (!valid_idx_port(bios, port))
470                 return;
471
472         /*
473          * The current head is maintained in the nvbios member  state.crtchead.
474          * We trap changes to CR44 and update the head variable and hence the
475          * register set written.
476          * As CR44 only exists on CRTC0, we update crtchead to head0 in advance
477          * of the write, and to head1 after the write
478          */
479         if (port == NV_CIO_CRX__COLOR && index == NV_CIO_CRE_44 &&
480             data != NV_CIO_CRE_44_HEADB)
481                 bios->state.crtchead = 0;
482
483         LOG_OLD_VALUE(bios_idxprt_rd(bios, port, index));
484         BIOSLOG(bios, " Indexed IO write: Port: 0x%04X, Index: 0x%02X, "
485                       "Head: 0x%02X, Data: 0x%02X\n",
486                 port, index, bios->state.crtchead, data);
487
488         if (bios->execute && dev_priv->card_type < NV_50) {
489                 still_alive();
490                 if (port == NV_VIO_SRX)
491                         NVWriteVgaSeq(dev, bios->state.crtchead, index, data);
492                 else    /* assume NV_CIO_CRX__COLOR */
493                         NVWriteVgaCrtc(dev, bios->state.crtchead, index, data);
494         } else
495         if (bios->execute) {
496                 uint32_t data32, shift = (index & 3) << 3;
497
498                 still_alive();
499
500                 data32  = bios_rd32(bios, NV50_PDISPLAY_VGACRTC(index & ~3));
501                 data32 &= ~(0xff << shift);
502                 data32 |= (data << shift);
503                 bios_wr32(bios, NV50_PDISPLAY_VGACRTC(index & ~3), data32);
504         }
505
506         if (port == NV_CIO_CRX__COLOR &&
507             index == NV_CIO_CRE_44 && data == NV_CIO_CRE_44_HEADB)
508                 bios->state.crtchead = 1;
509 }
510
511 static uint8_t
512 bios_port_rd(struct nvbios *bios, uint16_t port)
513 {
514         uint8_t data, head = bios->state.crtchead;
515
516         if (!valid_port(bios, port))
517                 return 0;
518
519         data = NVReadPRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port);
520
521         BIOSLOG(bios, " IO read:  Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
522                 port, head, data);
523
524         return data;
525 }
526
527 static void
528 bios_port_wr(struct nvbios *bios, uint16_t port, uint8_t data)
529 {
530         int head = bios->state.crtchead;
531
532         if (!valid_port(bios, port))
533                 return;
534
535         LOG_OLD_VALUE(bios_port_rd(bios, port));
536         BIOSLOG(bios, " IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
537                 port, head, data);
538
539         if (!bios->execute)
540                 return;
541
542         still_alive();
543         NVWritePRMVIO(bios->dev, head, NV_PRMVIO0_OFFSET + port, data);
544 }
545
546 static bool
547 io_flag_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
548 {
549         /*
550          * The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
551          * for the CRTC index; 1 byte for the mask to apply to the value
552          * retrieved from the CRTC; 1 byte for the shift right to apply to the
553          * masked CRTC value; 2 bytes for the offset to the flag array, to
554          * which the shifted value is added; 1 byte for the mask applied to the
555          * value read from the flag array; and 1 byte for the value to compare
556          * against the masked byte from the flag table.
557          */
558
559         uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
560         uint16_t crtcport = ROM16(bios->data[condptr]);
561         uint8_t crtcindex = bios->data[condptr + 2];
562         uint8_t mask = bios->data[condptr + 3];
563         uint8_t shift = bios->data[condptr + 4];
564         uint16_t flagarray = ROM16(bios->data[condptr + 5]);
565         uint8_t flagarraymask = bios->data[condptr + 7];
566         uint8_t cmpval = bios->data[condptr + 8];
567         uint8_t data;
568
569         BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
570                       "Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, "
571                       "Cmpval: 0x%02X\n",
572                 offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
573
574         data = bios_idxprt_rd(bios, crtcport, crtcindex);
575
576         data = bios->data[flagarray + ((data & mask) >> shift)];
577         data &= flagarraymask;
578
579         BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
580                 offset, data, cmpval);
581
582         return (data == cmpval);
583 }
584
585 static bool
586 bios_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
587 {
588         /*
589          * The condition table entry has 4 bytes for the address of the
590          * register to check, 4 bytes for a mask to apply to the register and
591          * 4 for a test comparison value
592          */
593
594         uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
595         uint32_t reg = ROM32(bios->data[condptr]);
596         uint32_t mask = ROM32(bios->data[condptr + 4]);
597         uint32_t cmpval = ROM32(bios->data[condptr + 8]);
598         uint32_t data;
599
600         BIOSLOG(bios, "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X\n",
601                 offset, cond, reg, mask);
602
603         data = bios_rd32(bios, reg) & mask;
604
605         BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
606                 offset, data, cmpval);
607
608         return (data == cmpval);
609 }
610
611 static bool
612 io_condition_met(struct nvbios *bios, uint16_t offset, uint8_t cond)
613 {
614         /*
615          * The IO condition entry has 2 bytes for the IO port address; 1 byte
616          * for the index to write to io_port; 1 byte for the mask to apply to
617          * the byte read from io_port+1; and 1 byte for the value to compare
618          * against the masked byte.
619          */
620
621         uint16_t condptr = bios->io_condition_tbl_ptr + cond * IO_CONDITION_SIZE;
622         uint16_t io_port = ROM16(bios->data[condptr]);
623         uint8_t port_index = bios->data[condptr + 2];
624         uint8_t mask = bios->data[condptr + 3];
625         uint8_t cmpval = bios->data[condptr + 4];
626
627         uint8_t data = bios_idxprt_rd(bios, io_port, port_index) & mask;
628
629         BIOSLOG(bios, "0x%04X: Checking if 0x%02X equals 0x%02X\n",
630                 offset, data, cmpval);
631
632         return (data == cmpval);
633 }
634
635 static int
636 nv50_pll_set(struct drm_device *dev, uint32_t reg, uint32_t clk)
637 {
638         struct drm_nouveau_private *dev_priv = dev->dev_private;
639         uint32_t reg0 = nv_rd32(dev, reg + 0);
640         uint32_t reg1 = nv_rd32(dev, reg + 4);
641         struct nouveau_pll_vals pll;
642         struct pll_lims pll_limits;
643         int ret;
644
645         ret = get_pll_limits(dev, reg, &pll_limits);
646         if (ret)
647                 return ret;
648
649         clk = nouveau_calc_pll_mnp(dev, &pll_limits, clk, &pll);
650         if (!clk)
651                 return -ERANGE;
652
653         reg0 = (reg0 & 0xfff8ffff) | (pll.log2P << 16);
654         reg1 = (reg1 & 0xffff0000) | (pll.N1 << 8) | pll.M1;
655
656         if (dev_priv->vbios.execute) {
657                 still_alive();
658                 nv_wr32(dev, reg + 4, reg1);
659                 nv_wr32(dev, reg + 0, reg0);
660         }
661
662         return 0;
663 }
664
665 static int
666 setPLL(struct nvbios *bios, uint32_t reg, uint32_t clk)
667 {
668         struct drm_device *dev = bios->dev;
669         struct drm_nouveau_private *dev_priv = dev->dev_private;
670         /* clk in kHz */
671         struct pll_lims pll_lim;
672         struct nouveau_pll_vals pllvals;
673         int ret;
674
675         if (dev_priv->card_type >= NV_50)
676                 return nv50_pll_set(dev, reg, clk);
677
678         /* high regs (such as in the mac g5 table) are not -= 4 */
679         ret = get_pll_limits(dev, reg > 0x405c ? reg : reg - 4, &pll_lim);
680         if (ret)
681                 return ret;
682
683         clk = nouveau_calc_pll_mnp(dev, &pll_lim, clk, &pllvals);
684         if (!clk)
685                 return -ERANGE;
686
687         if (bios->execute) {
688                 still_alive();
689                 nouveau_hw_setpll(dev, reg, &pllvals);
690         }
691
692         return 0;
693 }
694
695 static int dcb_entry_idx_from_crtchead(struct drm_device *dev)
696 {
697         struct drm_nouveau_private *dev_priv = dev->dev_private;
698         struct nvbios *bios = &dev_priv->vbios;
699
700         /*
701          * For the results of this function to be correct, CR44 must have been
702          * set (using bios_idxprt_wr to set crtchead), CR58 set for CR57 = 0,
703          * and the DCB table parsed, before the script calling the function is
704          * run.  run_digital_op_script is example of how to do such setup
705          */
706
707         uint8_t dcb_entry = NVReadVgaCrtc5758(dev, bios->state.crtchead, 0);
708
709         if (dcb_entry > bios->dcb.entries) {
710                 NV_ERROR(dev, "CR58 doesn't have a valid DCB entry currently "
711                                 "(%02X)\n", dcb_entry);
712                 dcb_entry = 0x7f;       /* unused / invalid marker */
713         }
714
715         return dcb_entry;
716 }
717
718 static int
719 read_dcb_i2c_entry(struct drm_device *dev, int dcb_version, uint8_t *i2ctable, int index, struct dcb_i2c_entry *i2c)
720 {
721         uint8_t dcb_i2c_ver = dcb_version, headerlen = 0, entry_len = 4;
722         int i2c_entries = DCB_MAX_NUM_I2C_ENTRIES;
723         int recordoffset = 0, rdofs = 1, wrofs = 0;
724         uint8_t port_type = 0;
725
726         if (!i2ctable)
727                 return -EINVAL;
728
729         if (dcb_version >= 0x30) {
730                 if (i2ctable[0] != dcb_version) /* necessary? */
731                         NV_WARN(dev,
732                                 "DCB I2C table version mismatch (%02X vs %02X)\n",
733                                 i2ctable[0], dcb_version);
734                 dcb_i2c_ver = i2ctable[0];
735                 headerlen = i2ctable[1];
736                 if (i2ctable[2] <= DCB_MAX_NUM_I2C_ENTRIES)
737                         i2c_entries = i2ctable[2];
738                 else
739                         NV_WARN(dev,
740                                 "DCB I2C table has more entries than indexable "
741                                 "(%d entries, max %d)\n", i2ctable[2],
742                                 DCB_MAX_NUM_I2C_ENTRIES);
743                 entry_len = i2ctable[3];
744                 /* [4] is i2c_default_indices, read in parse_dcb_table() */
745         }
746         /*
747          * It's your own fault if you call this function on a DCB 1.1 BIOS --
748          * the test below is for DCB 1.2
749          */
750         if (dcb_version < 0x14) {
751                 recordoffset = 2;
752                 rdofs = 0;
753                 wrofs = 1;
754         }
755
756         if (index == 0xf)
757                 return 0;
758         if (index >= i2c_entries) {
759                 NV_ERROR(dev, "DCB I2C index too big (%d >= %d)\n",
760                          index, i2ctable[2]);
761                 return -ENOENT;
762         }
763         if (i2ctable[headerlen + entry_len * index + 3] == 0xff) {
764                 NV_ERROR(dev, "DCB I2C entry invalid\n");
765                 return -EINVAL;
766         }
767
768         if (dcb_i2c_ver >= 0x30) {
769                 port_type = i2ctable[headerlen + recordoffset + 3 + entry_len * index];
770
771                 /*
772                  * Fixup for chips using same address offset for read and
773                  * write.
774                  */
775                 if (port_type == 4)     /* seen on C51 */
776                         rdofs = wrofs = 1;
777                 if (port_type >= 5)     /* G80+ */
778                         rdofs = wrofs = 0;
779         }
780
781         if (dcb_i2c_ver >= 0x40) {
782                 if (port_type != 5 && port_type != 6)
783                         NV_WARN(dev, "DCB I2C table has port type %d\n", port_type);
784
785                 i2c->entry = ROM32(i2ctable[headerlen + recordoffset + entry_len * index]);
786         }
787
788         i2c->port_type = port_type;
789         i2c->read = i2ctable[headerlen + recordoffset + rdofs + entry_len * index];
790         i2c->write = i2ctable[headerlen + recordoffset + wrofs + entry_len * index];
791
792         return 0;
793 }
794
795 static struct nouveau_i2c_chan *
796 init_i2c_device_find(struct drm_device *dev, int i2c_index)
797 {
798         struct drm_nouveau_private *dev_priv = dev->dev_private;
799         struct dcb_table *dcb = &dev_priv->vbios.dcb;
800
801         if (i2c_index == 0xff) {
802                 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
803                 int idx = dcb_entry_idx_from_crtchead(dev), shift = 0;
804                 int default_indices = dcb->i2c_default_indices;
805
806                 if (idx != 0x7f && dcb->entry[idx].i2c_upper_default)
807                         shift = 4;
808
809                 i2c_index = (default_indices >> shift) & 0xf;
810         }
811         if (i2c_index == 0x80)  /* g80+ */
812                 i2c_index = dcb->i2c_default_indices & 0xf;
813         else
814         if (i2c_index == 0x81)
815                 i2c_index = (dcb->i2c_default_indices & 0xf0) >> 4;
816
817         if (i2c_index > DCB_MAX_NUM_I2C_ENTRIES) {
818                 NV_ERROR(dev, "invalid i2c_index 0x%x\n", i2c_index);
819                 return NULL;
820         }
821
822         /* Make sure i2c table entry has been parsed, it may not
823          * have been if this is a bus not referenced by a DCB encoder
824          */
825         read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
826                            i2c_index, &dcb->i2c[i2c_index]);
827
828         return nouveau_i2c_find(dev, i2c_index);
829 }
830
831 static uint32_t
832 get_tmds_index_reg(struct drm_device *dev, uint8_t mlv)
833 {
834         /*
835          * For mlv < 0x80, it is an index into a table of TMDS base addresses.
836          * For mlv == 0x80 use the "or" value of the dcb_entry indexed by
837          * CR58 for CR57 = 0 to index a table of offsets to the basic
838          * 0x6808b0 address.
839          * For mlv == 0x81 use the "or" value of the dcb_entry indexed by
840          * CR58 for CR57 = 0 to index a table of offsets to the basic
841          * 0x6808b0 address, and then flip the offset by 8.
842          */
843
844         struct drm_nouveau_private *dev_priv = dev->dev_private;
845         struct nvbios *bios = &dev_priv->vbios;
846         const int pramdac_offset[13] = {
847                 0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000 };
848         const uint32_t pramdac_table[4] = {
849                 0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8 };
850
851         if (mlv >= 0x80) {
852                 int dcb_entry, dacoffset;
853
854                 /* note: dcb_entry_idx_from_crtchead needs pre-script set-up */
855                 dcb_entry = dcb_entry_idx_from_crtchead(dev);
856                 if (dcb_entry == 0x7f)
857                         return 0;
858                 dacoffset = pramdac_offset[bios->dcb.entry[dcb_entry].or];
859                 if (mlv == 0x81)
860                         dacoffset ^= 8;
861                 return 0x6808b0 + dacoffset;
862         } else {
863                 if (mlv >= ARRAY_SIZE(pramdac_table)) {
864                         NV_ERROR(dev, "Magic Lookup Value too big (%02X)\n",
865                                                                         mlv);
866                         return 0;
867                 }
868                 return pramdac_table[mlv];
869         }
870 }
871
872 static int
873 init_io_restrict_prog(struct nvbios *bios, uint16_t offset,
874                       struct init_exec *iexec)
875 {
876         /*
877          * INIT_IO_RESTRICT_PROG   opcode: 0x32 ('2')
878          *
879          * offset      (8  bit): opcode
880          * offset + 1  (16 bit): CRTC port
881          * offset + 3  (8  bit): CRTC index
882          * offset + 4  (8  bit): mask
883          * offset + 5  (8  bit): shift
884          * offset + 6  (8  bit): count
885          * offset + 7  (32 bit): register
886          * offset + 11 (32 bit): configuration 1
887          * ...
888          *
889          * Starting at offset + 11 there are "count" 32 bit values.
890          * To find out which value to use read index "CRTC index" on "CRTC
891          * port", AND this value with "mask" and then bit shift right "shift"
892          * bits.  Read the appropriate value using this index and write to
893          * "register"
894          */
895
896         uint16_t crtcport = ROM16(bios->data[offset + 1]);
897         uint8_t crtcindex = bios->data[offset + 3];
898         uint8_t mask = bios->data[offset + 4];
899         uint8_t shift = bios->data[offset + 5];
900         uint8_t count = bios->data[offset + 6];
901         uint32_t reg = ROM32(bios->data[offset + 7]);
902         uint8_t config;
903         uint32_t configval;
904         int len = 11 + count * 4;
905
906         if (!iexec->execute)
907                 return len;
908
909         BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
910                       "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
911                 offset, crtcport, crtcindex, mask, shift, count, reg);
912
913         config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
914         if (config > count) {
915                 NV_ERROR(bios->dev,
916                          "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
917                          offset, config, count);
918                 return -EINVAL;
919         }
920
921         configval = ROM32(bios->data[offset + 11 + config * 4]);
922
923         BIOSLOG(bios, "0x%04X: Writing config %02X\n", offset, config);
924
925         bios_wr32(bios, reg, configval);
926
927         return len;
928 }
929
930 static int
931 init_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
932 {
933         /*
934          * INIT_REPEAT   opcode: 0x33 ('3')
935          *
936          * offset      (8 bit): opcode
937          * offset + 1  (8 bit): count
938          *
939          * Execute script following this opcode up to INIT_REPEAT_END
940          * "count" times
941          */
942
943         uint8_t count = bios->data[offset + 1];
944         uint8_t i;
945
946         /* no iexec->execute check by design */
947
948         BIOSLOG(bios, "0x%04X: Repeating following segment %d times\n",
949                 offset, count);
950
951         iexec->repeat = true;
952
953         /*
954          * count - 1, as the script block will execute once when we leave this
955          * opcode -- this is compatible with bios behaviour as:
956          * a) the block is always executed at least once, even if count == 0
957          * b) the bios interpreter skips to the op following INIT_END_REPEAT,
958          * while we don't
959          */
960         for (i = 0; i < count - 1; i++)
961                 parse_init_table(bios, offset + 2, iexec);
962
963         iexec->repeat = false;
964
965         return 2;
966 }
967
968 static int
969 init_io_restrict_pll(struct nvbios *bios, uint16_t offset,
970                      struct init_exec *iexec)
971 {
972         /*
973          * INIT_IO_RESTRICT_PLL   opcode: 0x34 ('4')
974          *
975          * offset      (8  bit): opcode
976          * offset + 1  (16 bit): CRTC port
977          * offset + 3  (8  bit): CRTC index
978          * offset + 4  (8  bit): mask
979          * offset + 5  (8  bit): shift
980          * offset + 6  (8  bit): IO flag condition index
981          * offset + 7  (8  bit): count
982          * offset + 8  (32 bit): register
983          * offset + 12 (16 bit): frequency 1
984          * ...
985          *
986          * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
987          * Set PLL register "register" to coefficients for frequency n,
988          * selected by reading index "CRTC index" of "CRTC port" ANDed with
989          * "mask" and shifted right by "shift".
990          *
991          * If "IO flag condition index" > 0, and condition met, double
992          * frequency before setting it.
993          */
994
995         uint16_t crtcport = ROM16(bios->data[offset + 1]);
996         uint8_t crtcindex = bios->data[offset + 3];
997         uint8_t mask = bios->data[offset + 4];
998         uint8_t shift = bios->data[offset + 5];
999         int8_t io_flag_condition_idx = bios->data[offset + 6];
1000         uint8_t count = bios->data[offset + 7];
1001         uint32_t reg = ROM32(bios->data[offset + 8]);
1002         uint8_t config;
1003         uint16_t freq;
1004         int len = 12 + count * 2;
1005
1006         if (!iexec->execute)
1007                 return len;
1008
1009         BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1010                       "Shift: 0x%02X, IO Flag Condition: 0x%02X, "
1011                       "Count: 0x%02X, Reg: 0x%08X\n",
1012                 offset, crtcport, crtcindex, mask, shift,
1013                 io_flag_condition_idx, count, reg);
1014
1015         config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1016         if (config > count) {
1017                 NV_ERROR(bios->dev,
1018                          "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1019                          offset, config, count);
1020                 return -EINVAL;
1021         }
1022
1023         freq = ROM16(bios->data[offset + 12 + config * 2]);
1024
1025         if (io_flag_condition_idx > 0) {
1026                 if (io_flag_condition_met(bios, offset, io_flag_condition_idx)) {
1027                         BIOSLOG(bios, "0x%04X: Condition fulfilled -- "
1028                                       "frequency doubled\n", offset);
1029                         freq *= 2;
1030                 } else
1031                         BIOSLOG(bios, "0x%04X: Condition not fulfilled -- "
1032                                       "frequency unchanged\n", offset);
1033         }
1034
1035         BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
1036                 offset, reg, config, freq);
1037
1038         setPLL(bios, reg, freq * 10);
1039
1040         return len;
1041 }
1042
1043 static int
1044 init_end_repeat(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1045 {
1046         /*
1047          * INIT_END_REPEAT   opcode: 0x36 ('6')
1048          *
1049          * offset      (8 bit): opcode
1050          *
1051          * Marks the end of the block for INIT_REPEAT to repeat
1052          */
1053
1054         /* no iexec->execute check by design */
1055
1056         /*
1057          * iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
1058          * we're not in repeat mode
1059          */
1060         if (iexec->repeat)
1061                 return 0;
1062
1063         return 1;
1064 }
1065
1066 static int
1067 init_copy(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1068 {
1069         /*
1070          * INIT_COPY   opcode: 0x37 ('7')
1071          *
1072          * offset      (8  bit): opcode
1073          * offset + 1  (32 bit): register
1074          * offset + 5  (8  bit): shift
1075          * offset + 6  (8  bit): srcmask
1076          * offset + 7  (16 bit): CRTC port
1077          * offset + 9  (8 bit): CRTC index
1078          * offset + 10  (8 bit): mask
1079          *
1080          * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
1081          * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC
1082          * port
1083          */
1084
1085         uint32_t reg = ROM32(bios->data[offset + 1]);
1086         uint8_t shift = bios->data[offset + 5];
1087         uint8_t srcmask = bios->data[offset + 6];
1088         uint16_t crtcport = ROM16(bios->data[offset + 7]);
1089         uint8_t crtcindex = bios->data[offset + 9];
1090         uint8_t mask = bios->data[offset + 10];
1091         uint32_t data;
1092         uint8_t crtcdata;
1093
1094         if (!iexec->execute)
1095                 return 11;
1096
1097         BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, "
1098                       "Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1099                 offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1100
1101         data = bios_rd32(bios, reg);
1102
1103         if (shift < 0x80)
1104                 data >>= shift;
1105         else
1106                 data <<= (0x100 - shift);
1107
1108         data &= srcmask;
1109
1110         crtcdata  = bios_idxprt_rd(bios, crtcport, crtcindex) & mask;
1111         crtcdata |= (uint8_t)data;
1112         bios_idxprt_wr(bios, crtcport, crtcindex, crtcdata);
1113
1114         return 11;
1115 }
1116
1117 static int
1118 init_not(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1119 {
1120         /*
1121          * INIT_NOT   opcode: 0x38 ('8')
1122          *
1123          * offset      (8  bit): opcode
1124          *
1125          * Invert the current execute / no-execute condition (i.e. "else")
1126          */
1127         if (iexec->execute)
1128                 BIOSLOG(bios, "0x%04X: ------ Skipping following commands  ------\n", offset);
1129         else
1130                 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", offset);
1131
1132         iexec->execute = !iexec->execute;
1133         return 1;
1134 }
1135
1136 static int
1137 init_io_flag_condition(struct nvbios *bios, uint16_t offset,
1138                        struct init_exec *iexec)
1139 {
1140         /*
1141          * INIT_IO_FLAG_CONDITION   opcode: 0x39 ('9')
1142          *
1143          * offset      (8 bit): opcode
1144          * offset + 1  (8 bit): condition number
1145          *
1146          * Check condition "condition number" in the IO flag condition table.
1147          * If condition not met skip subsequent opcodes until condition is
1148          * inverted (INIT_NOT), or we hit INIT_RESUME
1149          */
1150
1151         uint8_t cond = bios->data[offset + 1];
1152
1153         if (!iexec->execute)
1154                 return 2;
1155
1156         if (io_flag_condition_met(bios, offset, cond))
1157                 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
1158         else {
1159                 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
1160                 iexec->execute = false;
1161         }
1162
1163         return 2;
1164 }
1165
1166 static int
1167 init_dp_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1168 {
1169         /*
1170          * INIT_DP_CONDITION   opcode: 0x3A ('')
1171          *
1172          * offset      (8 bit): opcode
1173          * offset + 1  (8 bit): "sub" opcode
1174          * offset + 2  (8 bit): unknown
1175          *
1176          */
1177
1178         struct bit_displayport_encoder_table *dpe = NULL;
1179         struct dcb_entry *dcb = bios->display.output;
1180         struct drm_device *dev = bios->dev;
1181         uint8_t cond = bios->data[offset + 1];
1182         int dummy;
1183
1184         BIOSLOG(bios, "0x%04X: subop 0x%02X\n", offset, cond);
1185
1186         if (!iexec->execute)
1187                 return 3;
1188
1189         dpe = nouveau_bios_dp_table(dev, dcb, &dummy);
1190         if (!dpe) {
1191                 NV_ERROR(dev, "0x%04X: INIT_3A: no encoder table!!\n", offset);
1192                 return -EINVAL;
1193         }
1194
1195         switch (cond) {
1196         case 0:
1197         {
1198                 struct dcb_connector_table_entry *ent =
1199                         &bios->dcb.connector.entry[dcb->connector];
1200
1201                 if (ent->type != DCB_CONNECTOR_eDP)
1202                         iexec->execute = false;
1203         }
1204                 break;
1205         case 1:
1206         case 2:
1207                 if (!(dpe->unknown & cond))
1208                         iexec->execute = false;
1209                 break;
1210         case 5:
1211         {
1212                 struct nouveau_i2c_chan *auxch;
1213                 int ret;
1214
1215                 auxch = nouveau_i2c_find(dev, bios->display.output->i2c_index);
1216                 if (!auxch)
1217                         return -ENODEV;
1218
1219                 ret = nouveau_dp_auxch(auxch, 9, 0xd, &cond, 1);
1220                 if (ret)
1221                         return ret;
1222
1223                 if (cond & 1)
1224                         iexec->execute = false;
1225         }
1226                 break;
1227         default:
1228                 NV_WARN(dev, "0x%04X: unknown INIT_3A op: %d\n", offset, cond);
1229                 break;
1230         }
1231
1232         if (iexec->execute)
1233                 BIOSLOG(bios, "0x%04X: continuing to execute\n", offset);
1234         else
1235                 BIOSLOG(bios, "0x%04X: skipping following commands\n", offset);
1236
1237         return 3;
1238 }
1239
1240 static int
1241 init_op_3b(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1242 {
1243         /*
1244          * INIT_3B   opcode: 0x3B ('')
1245          *
1246          * offset      (8 bit): opcode
1247          * offset + 1  (8 bit): crtc index
1248          *
1249          */
1250
1251         uint8_t or = ffs(bios->display.output->or) - 1;
1252         uint8_t index = bios->data[offset + 1];
1253         uint8_t data;
1254
1255         if (!iexec->execute)
1256                 return 2;
1257
1258         data = bios_idxprt_rd(bios, 0x3d4, index);
1259         bios_idxprt_wr(bios, 0x3d4, index, data & ~(1 << or));
1260         return 2;
1261 }
1262
1263 static int
1264 init_op_3c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1265 {
1266         /*
1267          * INIT_3C   opcode: 0x3C ('')
1268          *
1269          * offset      (8 bit): opcode
1270          * offset + 1  (8 bit): crtc index
1271          *
1272          */
1273
1274         uint8_t or = ffs(bios->display.output->or) - 1;
1275         uint8_t index = bios->data[offset + 1];
1276         uint8_t data;
1277
1278         if (!iexec->execute)
1279                 return 2;
1280
1281         data = bios_idxprt_rd(bios, 0x3d4, index);
1282         bios_idxprt_wr(bios, 0x3d4, index, data | (1 << or));
1283         return 2;
1284 }
1285
1286 static int
1287 init_idx_addr_latched(struct nvbios *bios, uint16_t offset,
1288                       struct init_exec *iexec)
1289 {
1290         /*
1291          * INIT_INDEX_ADDRESS_LATCHED   opcode: 0x49 ('I')
1292          *
1293          * offset      (8  bit): opcode
1294          * offset + 1  (32 bit): control register
1295          * offset + 5  (32 bit): data register
1296          * offset + 9  (32 bit): mask
1297          * offset + 13 (32 bit): data
1298          * offset + 17 (8  bit): count
1299          * offset + 18 (8  bit): address 1
1300          * offset + 19 (8  bit): data 1
1301          * ...
1302          *
1303          * For each of "count" address and data pairs, write "data n" to
1304          * "data register", read the current value of "control register",
1305          * and write it back once ANDed with "mask", ORed with "data",
1306          * and ORed with "address n"
1307          */
1308
1309         uint32_t controlreg = ROM32(bios->data[offset + 1]);
1310         uint32_t datareg = ROM32(bios->data[offset + 5]);
1311         uint32_t mask = ROM32(bios->data[offset + 9]);
1312         uint32_t data = ROM32(bios->data[offset + 13]);
1313         uint8_t count = bios->data[offset + 17];
1314         int len = 18 + count * 2;
1315         uint32_t value;
1316         int i;
1317
1318         if (!iexec->execute)
1319                 return len;
1320
1321         BIOSLOG(bios, "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, "
1322                       "Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1323                 offset, controlreg, datareg, mask, data, count);
1324
1325         for (i = 0; i < count; i++) {
1326                 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1327                 uint8_t instdata = bios->data[offset + 19 + i * 2];
1328
1329                 BIOSLOG(bios, "0x%04X: Address: 0x%02X, Data: 0x%02X\n",
1330                         offset, instaddress, instdata);
1331
1332                 bios_wr32(bios, datareg, instdata);
1333                 value  = bios_rd32(bios, controlreg) & mask;
1334                 value |= data;
1335                 value |= instaddress;
1336                 bios_wr32(bios, controlreg, value);
1337         }
1338
1339         return len;
1340 }
1341
1342 static int
1343 init_io_restrict_pll2(struct nvbios *bios, uint16_t offset,
1344                       struct init_exec *iexec)
1345 {
1346         /*
1347          * INIT_IO_RESTRICT_PLL2   opcode: 0x4A ('J')
1348          *
1349          * offset      (8  bit): opcode
1350          * offset + 1  (16 bit): CRTC port
1351          * offset + 3  (8  bit): CRTC index
1352          * offset + 4  (8  bit): mask
1353          * offset + 5  (8  bit): shift
1354          * offset + 6  (8  bit): count
1355          * offset + 7  (32 bit): register
1356          * offset + 11 (32 bit): frequency 1
1357          * ...
1358          *
1359          * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1360          * Set PLL register "register" to coefficients for frequency n,
1361          * selected by reading index "CRTC index" of "CRTC port" ANDed with
1362          * "mask" and shifted right by "shift".
1363          */
1364
1365         uint16_t crtcport = ROM16(bios->data[offset + 1]);
1366         uint8_t crtcindex = bios->data[offset + 3];
1367         uint8_t mask = bios->data[offset + 4];
1368         uint8_t shift = bios->data[offset + 5];
1369         uint8_t count = bios->data[offset + 6];
1370         uint32_t reg = ROM32(bios->data[offset + 7]);
1371         int len = 11 + count * 4;
1372         uint8_t config;
1373         uint32_t freq;
1374
1375         if (!iexec->execute)
1376                 return len;
1377
1378         BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
1379                       "Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1380                 offset, crtcport, crtcindex, mask, shift, count, reg);
1381
1382         if (!reg)
1383                 return len;
1384
1385         config = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) >> shift;
1386         if (config > count) {
1387                 NV_ERROR(bios->dev,
1388                          "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1389                          offset, config, count);
1390                 return -EINVAL;
1391         }
1392
1393         freq = ROM32(bios->data[offset + 11 + config * 4]);
1394
1395         BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1396                 offset, reg, config, freq);
1397
1398         setPLL(bios, reg, freq);
1399
1400         return len;
1401 }
1402
1403 static int
1404 init_pll2(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1405 {
1406         /*
1407          * INIT_PLL2   opcode: 0x4B ('K')
1408          *
1409          * offset      (8  bit): opcode
1410          * offset + 1  (32 bit): register
1411          * offset + 5  (32 bit): freq
1412          *
1413          * Set PLL register "register" to coefficients for frequency "freq"
1414          */
1415
1416         uint32_t reg = ROM32(bios->data[offset + 1]);
1417         uint32_t freq = ROM32(bios->data[offset + 5]);
1418
1419         if (!iexec->execute)
1420                 return 9;
1421
1422         BIOSLOG(bios, "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1423                 offset, reg, freq);
1424
1425         setPLL(bios, reg, freq);
1426         return 9;
1427 }
1428
1429 static int
1430 init_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1431 {
1432         /*
1433          * INIT_I2C_BYTE   opcode: 0x4C ('L')
1434          *
1435          * offset      (8 bit): opcode
1436          * offset + 1  (8 bit): DCB I2C table entry index
1437          * offset + 2  (8 bit): I2C slave address
1438          * offset + 3  (8 bit): count
1439          * offset + 4  (8 bit): I2C register 1
1440          * offset + 5  (8 bit): mask 1
1441          * offset + 6  (8 bit): data 1
1442          * ...
1443          *
1444          * For each of "count" registers given by "I2C register n" on the device
1445          * addressed by "I2C slave address" on the I2C bus given by
1446          * "DCB I2C table entry index", read the register, AND the result with
1447          * "mask n" and OR it with "data n" before writing it back to the device
1448          */
1449
1450         uint8_t i2c_index = bios->data[offset + 1];
1451         uint8_t i2c_address = bios->data[offset + 2];
1452         uint8_t count = bios->data[offset + 3];
1453         int len = 4 + count * 3;
1454         struct nouveau_i2c_chan *chan;
1455         struct i2c_msg msg;
1456         int i;
1457
1458         if (!iexec->execute)
1459                 return len;
1460
1461         BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1462                       "Count: 0x%02X\n",
1463                 offset, i2c_index, i2c_address, count);
1464
1465         chan = init_i2c_device_find(bios->dev, i2c_index);
1466         if (!chan)
1467                 return -ENODEV;
1468
1469         for (i = 0; i < count; i++) {
1470                 uint8_t i2c_reg = bios->data[offset + 4 + i * 3];
1471                 uint8_t mask = bios->data[offset + 5 + i * 3];
1472                 uint8_t data = bios->data[offset + 6 + i * 3];
1473                 uint8_t value;
1474
1475                 msg.addr = i2c_address;
1476                 msg.flags = I2C_M_RD;
1477                 msg.len = 1;
1478                 msg.buf = &value;
1479                 if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
1480                         return -EIO;
1481
1482                 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Value: 0x%02X, "
1483                               "Mask: 0x%02X, Data: 0x%02X\n",
1484                         offset, i2c_reg, value, mask, data);
1485
1486                 value = (value & mask) | data;
1487
1488                 if (bios->execute) {
1489                         msg.addr = i2c_address;
1490                         msg.flags = 0;
1491                         msg.len = 1;
1492                         msg.buf = &value;
1493                         if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
1494                                 return -EIO;
1495                 }
1496         }
1497
1498         return len;
1499 }
1500
1501 static int
1502 init_zm_i2c_byte(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1503 {
1504         /*
1505          * INIT_ZM_I2C_BYTE   opcode: 0x4D ('M')
1506          *
1507          * offset      (8 bit): opcode
1508          * offset + 1  (8 bit): DCB I2C table entry index
1509          * offset + 2  (8 bit): I2C slave address
1510          * offset + 3  (8 bit): count
1511          * offset + 4  (8 bit): I2C register 1
1512          * offset + 5  (8 bit): data 1
1513          * ...
1514          *
1515          * For each of "count" registers given by "I2C register n" on the device
1516          * addressed by "I2C slave address" on the I2C bus given by
1517          * "DCB I2C table entry index", set the register to "data n"
1518          */
1519
1520         uint8_t i2c_index = bios->data[offset + 1];
1521         uint8_t i2c_address = bios->data[offset + 2];
1522         uint8_t count = bios->data[offset + 3];
1523         int len = 4 + count * 2;
1524         struct nouveau_i2c_chan *chan;
1525         struct i2c_msg msg;
1526         int i;
1527
1528         if (!iexec->execute)
1529                 return len;
1530
1531         BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1532                       "Count: 0x%02X\n",
1533                 offset, i2c_index, i2c_address, count);
1534
1535         chan = init_i2c_device_find(bios->dev, i2c_index);
1536         if (!chan)
1537                 return -ENODEV;
1538
1539         for (i = 0; i < count; i++) {
1540                 uint8_t i2c_reg = bios->data[offset + 4 + i * 2];
1541                 uint8_t data = bios->data[offset + 5 + i * 2];
1542
1543                 BIOSLOG(bios, "0x%04X: I2CReg: 0x%02X, Data: 0x%02X\n",
1544                         offset, i2c_reg, data);
1545
1546                 if (bios->execute) {
1547                         msg.addr = i2c_address;
1548                         msg.flags = 0;
1549                         msg.len = 1;
1550                         msg.buf = &data;
1551                         if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
1552                                 return -EIO;
1553                 }
1554         }
1555
1556         return len;
1557 }
1558
1559 static int
1560 init_zm_i2c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1561 {
1562         /*
1563          * INIT_ZM_I2C   opcode: 0x4E ('N')
1564          *
1565          * offset      (8 bit): opcode
1566          * offset + 1  (8 bit): DCB I2C table entry index
1567          * offset + 2  (8 bit): I2C slave address
1568          * offset + 3  (8 bit): count
1569          * offset + 4  (8 bit): data 1
1570          * ...
1571          *
1572          * Send "count" bytes ("data n") to the device addressed by "I2C slave
1573          * address" on the I2C bus given by "DCB I2C table entry index"
1574          */
1575
1576         uint8_t i2c_index = bios->data[offset + 1];
1577         uint8_t i2c_address = bios->data[offset + 2];
1578         uint8_t count = bios->data[offset + 3];
1579         int len = 4 + count;
1580         struct nouveau_i2c_chan *chan;
1581         struct i2c_msg msg;
1582         uint8_t data[256];
1583         int i;
1584
1585         if (!iexec->execute)
1586                 return len;
1587
1588         BIOSLOG(bios, "0x%04X: DCBI2CIndex: 0x%02X, I2CAddress: 0x%02X, "
1589                       "Count: 0x%02X\n",
1590                 offset, i2c_index, i2c_address, count);
1591
1592         chan = init_i2c_device_find(bios->dev, i2c_index);
1593         if (!chan)
1594                 return -ENODEV;
1595
1596         for (i = 0; i < count; i++) {
1597                 data[i] = bios->data[offset + 4 + i];
1598
1599                 BIOSLOG(bios, "0x%04X: Data: 0x%02X\n", offset, data[i]);
1600         }
1601
1602         if (bios->execute) {
1603                 msg.addr = i2c_address;
1604                 msg.flags = 0;
1605                 msg.len = count;
1606                 msg.buf = data;
1607                 if (i2c_transfer(&chan->adapter, &msg, 1) != 1)
1608                         return -EIO;
1609         }
1610
1611         return len;
1612 }
1613
1614 static int
1615 init_tmds(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1616 {
1617         /*
1618          * INIT_TMDS   opcode: 0x4F ('O')       (non-canon name)
1619          *
1620          * offset      (8 bit): opcode
1621          * offset + 1  (8 bit): magic lookup value
1622          * offset + 2  (8 bit): TMDS address
1623          * offset + 3  (8 bit): mask
1624          * offset + 4  (8 bit): data
1625          *
1626          * Read the data reg for TMDS address "TMDS address", AND it with mask
1627          * and OR it with data, then write it back
1628          * "magic lookup value" determines which TMDS base address register is
1629          * used -- see get_tmds_index_reg()
1630          */
1631
1632         uint8_t mlv = bios->data[offset + 1];
1633         uint32_t tmdsaddr = bios->data[offset + 2];
1634         uint8_t mask = bios->data[offset + 3];
1635         uint8_t data = bios->data[offset + 4];
1636         uint32_t reg, value;
1637
1638         if (!iexec->execute)
1639                 return 5;
1640
1641         BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, "
1642                       "Mask: 0x%02X, Data: 0x%02X\n",
1643                 offset, mlv, tmdsaddr, mask, data);
1644
1645         reg = get_tmds_index_reg(bios->dev, mlv);
1646         if (!reg)
1647                 return -EINVAL;
1648
1649         bios_wr32(bios, reg,
1650                   tmdsaddr | NV_PRAMDAC_FP_TMDS_CONTROL_WRITE_DISABLE);
1651         value = (bios_rd32(bios, reg + 4) & mask) | data;
1652         bios_wr32(bios, reg + 4, value);
1653         bios_wr32(bios, reg, tmdsaddr);
1654
1655         return 5;
1656 }
1657
1658 static int
1659 init_zm_tmds_group(struct nvbios *bios, uint16_t offset,
1660                    struct init_exec *iexec)
1661 {
1662         /*
1663          * INIT_ZM_TMDS_GROUP   opcode: 0x50 ('P')      (non-canon name)
1664          *
1665          * offset      (8 bit): opcode
1666          * offset + 1  (8 bit): magic lookup value
1667          * offset + 2  (8 bit): count
1668          * offset + 3  (8 bit): addr 1
1669          * offset + 4  (8 bit): data 1
1670          * ...
1671          *
1672          * For each of "count" TMDS address and data pairs write "data n" to
1673          * "addr n".  "magic lookup value" determines which TMDS base address
1674          * register is used -- see get_tmds_index_reg()
1675          */
1676
1677         uint8_t mlv = bios->data[offset + 1];
1678         uint8_t count = bios->data[offset + 2];
1679         int len = 3 + count * 2;
1680         uint32_t reg;
1681         int i;
1682
1683         if (!iexec->execute)
1684                 return len;
1685
1686         BIOSLOG(bios, "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1687                 offset, mlv, count);
1688
1689         reg = get_tmds_index_reg(bios->dev, mlv);
1690         if (!reg)
1691                 return -EINVAL;
1692
1693         for (i = 0; i < count; i++) {
1694                 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1695                 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1696
1697                 bios_wr32(bios, reg + 4, tmdsdata);
1698                 bios_wr32(bios, reg, tmdsaddr);
1699         }
1700
1701         return len;
1702 }
1703
1704 static int
1705 init_cr_idx_adr_latch(struct nvbios *bios, uint16_t offset,
1706                       struct init_exec *iexec)
1707 {
1708         /*
1709          * INIT_CR_INDEX_ADDRESS_LATCHED   opcode: 0x51 ('Q')
1710          *
1711          * offset      (8 bit): opcode
1712          * offset + 1  (8 bit): CRTC index1
1713          * offset + 2  (8 bit): CRTC index2
1714          * offset + 3  (8 bit): baseaddr
1715          * offset + 4  (8 bit): count
1716          * offset + 5  (8 bit): data 1
1717          * ...
1718          *
1719          * For each of "count" address and data pairs, write "baseaddr + n" to
1720          * "CRTC index1" and "data n" to "CRTC index2"
1721          * Once complete, restore initial value read from "CRTC index1"
1722          */
1723         uint8_t crtcindex1 = bios->data[offset + 1];
1724         uint8_t crtcindex2 = bios->data[offset + 2];
1725         uint8_t baseaddr = bios->data[offset + 3];
1726         uint8_t count = bios->data[offset + 4];
1727         int len = 5 + count;
1728         uint8_t oldaddr, data;
1729         int i;
1730
1731         if (!iexec->execute)
1732                 return len;
1733
1734         BIOSLOG(bios, "0x%04X: Index1: 0x%02X, Index2: 0x%02X, "
1735                       "BaseAddr: 0x%02X, Count: 0x%02X\n",
1736                 offset, crtcindex1, crtcindex2, baseaddr, count);
1737
1738         oldaddr = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex1);
1739
1740         for (i = 0; i < count; i++) {
1741                 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1,
1742                                      baseaddr + i);
1743                 data = bios->data[offset + 5 + i];
1744                 bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex2, data);
1745         }
1746
1747         bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex1, oldaddr);
1748
1749         return len;
1750 }
1751
1752 static int
1753 init_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1754 {
1755         /*
1756          * INIT_CR   opcode: 0x52 ('R')
1757          *
1758          * offset      (8  bit): opcode
1759          * offset + 1  (8  bit): CRTC index
1760          * offset + 2  (8  bit): mask
1761          * offset + 3  (8  bit): data
1762          *
1763          * Assign the value of at "CRTC index" ANDed with mask and ORed with
1764          * data back to "CRTC index"
1765          */
1766
1767         uint8_t crtcindex = bios->data[offset + 1];
1768         uint8_t mask = bios->data[offset + 2];
1769         uint8_t data = bios->data[offset + 3];
1770         uint8_t value;
1771
1772         if (!iexec->execute)
1773                 return 4;
1774
1775         BIOSLOG(bios, "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1776                 offset, crtcindex, mask, data);
1777
1778         value  = bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, crtcindex) & mask;
1779         value |= data;
1780         bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, value);
1781
1782         return 4;
1783 }
1784
1785 static int
1786 init_zm_cr(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1787 {
1788         /*
1789          * INIT_ZM_CR   opcode: 0x53 ('S')
1790          *
1791          * offset      (8 bit): opcode
1792          * offset + 1  (8 bit): CRTC index
1793          * offset + 2  (8 bit): value
1794          *
1795          * Assign "value" to CRTC register with index "CRTC index".
1796          */
1797
1798         uint8_t crtcindex = ROM32(bios->data[offset + 1]);
1799         uint8_t data = bios->data[offset + 2];
1800
1801         if (!iexec->execute)
1802                 return 3;
1803
1804         bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, crtcindex, data);
1805
1806         return 3;
1807 }
1808
1809 static int
1810 init_zm_cr_group(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1811 {
1812         /*
1813          * INIT_ZM_CR_GROUP   opcode: 0x54 ('T')
1814          *
1815          * offset      (8 bit): opcode
1816          * offset + 1  (8 bit): count
1817          * offset + 2  (8 bit): CRTC index 1
1818          * offset + 3  (8 bit): value 1
1819          * ...
1820          *
1821          * For "count", assign "value n" to CRTC register with index
1822          * "CRTC index n".
1823          */
1824
1825         uint8_t count = bios->data[offset + 1];
1826         int len = 2 + count * 2;
1827         int i;
1828
1829         if (!iexec->execute)
1830                 return len;
1831
1832         for (i = 0; i < count; i++)
1833                 init_zm_cr(bios, offset + 2 + 2 * i - 1, iexec);
1834
1835         return len;
1836 }
1837
1838 static int
1839 init_condition_time(struct nvbios *bios, uint16_t offset,
1840                     struct init_exec *iexec)
1841 {
1842         /*
1843          * INIT_CONDITION_TIME   opcode: 0x56 ('V')
1844          *
1845          * offset      (8 bit): opcode
1846          * offset + 1  (8 bit): condition number
1847          * offset + 2  (8 bit): retries / 50
1848          *
1849          * Check condition "condition number" in the condition table.
1850          * Bios code then sleeps for 2ms if the condition is not met, and
1851          * repeats up to "retries" times, but on one C51 this has proved
1852          * insufficient.  In mmiotraces the driver sleeps for 20ms, so we do
1853          * this, and bail after "retries" times, or 2s, whichever is less.
1854          * If still not met after retries, clear execution flag for this table.
1855          */
1856
1857         uint8_t cond = bios->data[offset + 1];
1858         uint16_t retries = bios->data[offset + 2] * 50;
1859         unsigned cnt;
1860
1861         if (!iexec->execute)
1862                 return 3;
1863
1864         if (retries > 100)
1865                 retries = 100;
1866
1867         BIOSLOG(bios, "0x%04X: Condition: 0x%02X, Retries: 0x%02X\n",
1868                 offset, cond, retries);
1869
1870         if (!bios->execute) /* avoid 2s delays when "faking" execution */
1871                 retries = 1;
1872
1873         for (cnt = 0; cnt < retries; cnt++) {
1874                 if (bios_condition_met(bios, offset, cond)) {
1875                         BIOSLOG(bios, "0x%04X: Condition met, continuing\n",
1876                                                                 offset);
1877                         break;
1878                 } else {
1879                         BIOSLOG(bios, "0x%04X: "
1880                                 "Condition not met, sleeping for 20ms\n",
1881                                                                 offset);
1882                         msleep(20);
1883                 }
1884         }
1885
1886         if (!bios_condition_met(bios, offset, cond)) {
1887                 NV_WARN(bios->dev,
1888                         "0x%04X: Condition still not met after %dms, "
1889                         "skipping following opcodes\n", offset, 20 * retries);
1890                 iexec->execute = false;
1891         }
1892
1893         return 3;
1894 }
1895
1896 static int
1897 init_zm_reg_sequence(struct nvbios *bios, uint16_t offset,
1898                      struct init_exec *iexec)
1899 {
1900         /*
1901          * INIT_ZM_REG_SEQUENCE   opcode: 0x58 ('X')
1902          *
1903          * offset      (8  bit): opcode
1904          * offset + 1  (32 bit): base register
1905          * offset + 5  (8  bit): count
1906          * offset + 6  (32 bit): value 1
1907          * ...
1908          *
1909          * Starting at offset + 6 there are "count" 32 bit values.
1910          * For "count" iterations set "base register" + 4 * current_iteration
1911          * to "value current_iteration"
1912          */
1913
1914         uint32_t basereg = ROM32(bios->data[offset + 1]);
1915         uint32_t count = bios->data[offset + 5];
1916         int len = 6 + count * 4;
1917         int i;
1918
1919         if (!iexec->execute)
1920                 return len;
1921
1922         BIOSLOG(bios, "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1923                 offset, basereg, count);
1924
1925         for (i = 0; i < count; i++) {
1926                 uint32_t reg = basereg + i * 4;
1927                 uint32_t data = ROM32(bios->data[offset + 6 + i * 4]);
1928
1929                 bios_wr32(bios, reg, data);
1930         }
1931
1932         return len;
1933 }
1934
1935 static int
1936 init_sub_direct(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1937 {
1938         /*
1939          * INIT_SUB_DIRECT   opcode: 0x5B ('[')
1940          *
1941          * offset      (8  bit): opcode
1942          * offset + 1  (16 bit): subroutine offset (in bios)
1943          *
1944          * Calls a subroutine that will execute commands until INIT_DONE
1945          * is found.
1946          */
1947
1948         uint16_t sub_offset = ROM16(bios->data[offset + 1]);
1949
1950         if (!iexec->execute)
1951                 return 3;
1952
1953         BIOSLOG(bios, "0x%04X: Executing subroutine at 0x%04X\n",
1954                 offset, sub_offset);
1955
1956         parse_init_table(bios, sub_offset, iexec);
1957
1958         BIOSLOG(bios, "0x%04X: End of 0x%04X subroutine\n", offset, sub_offset);
1959
1960         return 3;
1961 }
1962
1963 static int
1964 init_copy_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
1965 {
1966         /*
1967          * INIT_COPY_NV_REG   opcode: 0x5F ('_')
1968          *
1969          * offset      (8  bit): opcode
1970          * offset + 1  (32 bit): src reg
1971          * offset + 5  (8  bit): shift
1972          * offset + 6  (32 bit): src mask
1973          * offset + 10 (32 bit): xor
1974          * offset + 14 (32 bit): dst reg
1975          * offset + 18 (32 bit): dst mask
1976          *
1977          * Shift REGVAL("src reg") right by (signed) "shift", AND result with
1978          * "src mask", then XOR with "xor". Write this OR'd with
1979          * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
1980          */
1981
1982         uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
1983         uint8_t shift = bios->data[offset + 5];
1984         uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
1985         uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
1986         uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
1987         uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
1988         uint32_t srcvalue, dstvalue;
1989
1990         if (!iexec->execute)
1991                 return 22;
1992
1993         BIOSLOG(bios, "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, "
1994                       "Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
1995                 offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
1996
1997         srcvalue = bios_rd32(bios, srcreg);
1998
1999         if (shift < 0x80)
2000                 srcvalue >>= shift;
2001         else
2002                 srcvalue <<= (0x100 - shift);
2003
2004         srcvalue = (srcvalue & srcmask) ^ xor;
2005
2006         dstvalue = bios_rd32(bios, dstreg) & dstmask;
2007
2008         bios_wr32(bios, dstreg, dstvalue | srcvalue);
2009
2010         return 22;
2011 }
2012
2013 static int
2014 init_zm_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2015 {
2016         /*
2017          * INIT_ZM_INDEX_IO   opcode: 0x62 ('b')
2018          *
2019          * offset      (8  bit): opcode
2020          * offset + 1  (16 bit): CRTC port
2021          * offset + 3  (8  bit): CRTC index
2022          * offset + 4  (8  bit): data
2023          *
2024          * Write "data" to index "CRTC index" of "CRTC port"
2025          */
2026         uint16_t crtcport = ROM16(bios->data[offset + 1]);
2027         uint8_t crtcindex = bios->data[offset + 3];
2028         uint8_t data = bios->data[offset + 4];
2029
2030         if (!iexec->execute)
2031                 return 5;
2032
2033         bios_idxprt_wr(bios, crtcport, crtcindex, data);
2034
2035         return 5;
2036 }
2037
2038 static int
2039 init_compute_mem(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2040 {
2041         /*
2042          * INIT_COMPUTE_MEM   opcode: 0x63 ('c')
2043          *
2044          * offset      (8 bit): opcode
2045          *
2046          * This opcode is meant to set NV_PFB_CFG0 (0x100200) appropriately so
2047          * that the hardware can correctly calculate how much VRAM it has
2048          * (and subsequently report that value in NV_PFB_CSTATUS (0x10020C))
2049          *
2050          * The implementation of this opcode in general consists of two parts:
2051          * 1) determination of the memory bus width
2052          * 2) determination of how many of the card's RAM pads have ICs attached
2053          *
2054          * 1) is done by a cunning combination of writes to offsets 0x1c and
2055          * 0x3c in the framebuffer, and seeing whether the written values are
2056          * read back correctly. This then affects bits 4-7 of NV_PFB_CFG0
2057          *
2058          * 2) is done by a cunning combination of writes to an offset slightly
2059          * less than the maximum memory reported by NV_PFB_CSTATUS, then seeing
2060          * if the test pattern can be read back. This then affects bits 12-15 of
2061          * NV_PFB_CFG0
2062          *
2063          * In this context a "cunning combination" may include multiple reads
2064          * and writes to varying locations, often alternating the test pattern
2065          * and 0, doubtless to make sure buffers are filled, residual charges
2066          * on tracks are removed etc.
2067          *
2068          * Unfortunately, the "cunning combination"s mentioned above, and the
2069          * changes to the bits in NV_PFB_CFG0 differ with nearly every bios
2070          * trace I have.
2071          *
2072          * Therefore, we cheat and assume the value of NV_PFB_CFG0 with which
2073          * we started was correct, and use that instead
2074          */
2075
2076         /* no iexec->execute check by design */
2077
2078         /*
2079          * This appears to be a NOP on G8x chipsets, both io logs of the VBIOS
2080          * and kmmio traces of the binary driver POSTing the card show nothing
2081          * being done for this opcode.  why is it still listed in the table?!
2082          */
2083
2084         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2085
2086         if (dev_priv->card_type >= NV_40)
2087                 return 1;
2088
2089         /*
2090          * On every card I've seen, this step gets done for us earlier in
2091          * the init scripts
2092         uint8_t crdata = bios_idxprt_rd(dev, NV_VIO_SRX, 0x01);
2093         bios_idxprt_wr(dev, NV_VIO_SRX, 0x01, crdata | 0x20);
2094          */
2095
2096         /*
2097          * This also has probably been done in the scripts, but an mmio trace of
2098          * s3 resume shows nvidia doing it anyway (unlike the NV_VIO_SRX write)
2099          */
2100         bios_wr32(bios, NV_PFB_REFCTRL, NV_PFB_REFCTRL_VALID_1);
2101
2102         /* write back the saved configuration value */
2103         bios_wr32(bios, NV_PFB_CFG0, bios->state.saved_nv_pfb_cfg0);
2104
2105         return 1;
2106 }
2107
2108 static int
2109 init_reset(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2110 {
2111         /*
2112          * INIT_RESET   opcode: 0x65 ('e')
2113          *
2114          * offset      (8  bit): opcode
2115          * offset + 1  (32 bit): register
2116          * offset + 5  (32 bit): value1
2117          * offset + 9  (32 bit): value2
2118          *
2119          * Assign "value1" to "register", then assign "value2" to "register"
2120          */
2121
2122         uint32_t reg = ROM32(bios->data[offset + 1]);
2123         uint32_t value1 = ROM32(bios->data[offset + 5]);
2124         uint32_t value2 = ROM32(bios->data[offset + 9]);
2125         uint32_t pci_nv_19, pci_nv_20;
2126
2127         /* no iexec->execute check by design */
2128
2129         pci_nv_19 = bios_rd32(bios, NV_PBUS_PCI_NV_19);
2130         bios_wr32(bios, NV_PBUS_PCI_NV_19, 0);
2131         bios_wr32(bios, reg, value1);
2132
2133         udelay(10);
2134
2135         bios_wr32(bios, reg, value2);
2136         bios_wr32(bios, NV_PBUS_PCI_NV_19, pci_nv_19);
2137
2138         pci_nv_20 = bios_rd32(bios, NV_PBUS_PCI_NV_20);
2139         pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED;     /* 0xfffffffe */
2140         bios_wr32(bios, NV_PBUS_PCI_NV_20, pci_nv_20);
2141
2142         return 13;
2143 }
2144
2145 static int
2146 init_configure_mem(struct nvbios *bios, uint16_t offset,
2147                    struct init_exec *iexec)
2148 {
2149         /*
2150          * INIT_CONFIGURE_MEM   opcode: 0x66 ('f')
2151          *
2152          * offset      (8 bit): opcode
2153          *
2154          * Equivalent to INIT_DONE on bios version 3 or greater.
2155          * For early bios versions, sets up the memory registers, using values
2156          * taken from the memory init table
2157          */
2158
2159         /* no iexec->execute check by design */
2160
2161         uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
2162         uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
2163         uint32_t reg, data;
2164
2165         if (bios->major_version > 2)
2166                 return -ENODEV;
2167
2168         bios_idxprt_wr(bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX, bios_idxprt_rd(
2169                        bios, NV_VIO_SRX, NV_VIO_SR_CLOCK_INDEX) | 0x20);
2170
2171         if (bios->data[meminitoffs] & 1)
2172                 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
2173
2174         for (reg = ROM32(bios->data[seqtbloffs]);
2175              reg != 0xffffffff;
2176              reg = ROM32(bios->data[seqtbloffs += 4])) {
2177
2178                 switch (reg) {
2179                 case NV_PFB_PRE:
2180                         data = NV_PFB_PRE_CMD_PRECHARGE;
2181                         break;
2182                 case NV_PFB_PAD:
2183                         data = NV_PFB_PAD_CKE_NORMAL;
2184                         break;
2185                 case NV_PFB_REF:
2186                         data = NV_PFB_REF_CMD_REFRESH;
2187                         break;
2188                 default:
2189                         data = ROM32(bios->data[meminitdata]);
2190                         meminitdata += 4;
2191                         if (data == 0xffffffff)
2192                                 continue;
2193                 }
2194
2195                 bios_wr32(bios, reg, data);
2196         }
2197
2198         return 1;
2199 }
2200
2201 static int
2202 init_configure_clk(struct nvbios *bios, uint16_t offset,
2203                    struct init_exec *iexec)
2204 {
2205         /*
2206          * INIT_CONFIGURE_CLK   opcode: 0x67 ('g')
2207          *
2208          * offset      (8 bit): opcode
2209          *
2210          * Equivalent to INIT_DONE on bios version 3 or greater.
2211          * For early bios versions, sets up the NVClk and MClk PLLs, using
2212          * values taken from the memory init table
2213          */
2214
2215         /* no iexec->execute check by design */
2216
2217         uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_SCRATCH4__INDEX) >> 4);
2218         int clock;
2219
2220         if (bios->major_version > 2)
2221                 return -ENODEV;
2222
2223         clock = ROM16(bios->data[meminitoffs + 4]) * 10;
2224         setPLL(bios, NV_PRAMDAC_NVPLL_COEFF, clock);
2225
2226         clock = ROM16(bios->data[meminitoffs + 2]) * 10;
2227         if (bios->data[meminitoffs] & 1) /* DDR */
2228                 clock *= 2;
2229         setPLL(bios, NV_PRAMDAC_MPLL_COEFF, clock);
2230
2231         return 1;
2232 }
2233
2234 static int
2235 init_configure_preinit(struct nvbios *bios, uint16_t offset,
2236                        struct init_exec *iexec)
2237 {
2238         /*
2239          * INIT_CONFIGURE_PREINIT   opcode: 0x68 ('h')
2240          *
2241          * offset      (8 bit): opcode
2242          *
2243          * Equivalent to INIT_DONE on bios version 3 or greater.
2244          * For early bios versions, does early init, loading ram and crystal
2245          * configuration from straps into CR3C
2246          */
2247
2248         /* no iexec->execute check by design */
2249
2250         uint32_t straps = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
2251         uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & (1 << 6));
2252
2253         if (bios->major_version > 2)
2254                 return -ENODEV;
2255
2256         bios_idxprt_wr(bios, NV_CIO_CRX__COLOR,
2257                              NV_CIO_CRE_SCRATCH4__INDEX, cr3c);
2258
2259         return 1;
2260 }
2261
2262 static int
2263 init_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2264 {
2265         /*
2266          * INIT_IO   opcode: 0x69 ('i')
2267          *
2268          * offset      (8  bit): opcode
2269          * offset + 1  (16 bit): CRTC port
2270          * offset + 3  (8  bit): mask
2271          * offset + 4  (8  bit): data
2272          *
2273          * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2274          */
2275
2276         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2277         uint16_t crtcport = ROM16(bios->data[offset + 1]);
2278         uint8_t mask = bios->data[offset + 3];
2279         uint8_t data = bios->data[offset + 4];
2280
2281         if (!iexec->execute)
2282                 return 5;
2283
2284         BIOSLOG(bios, "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2285                 offset, crtcport, mask, data);
2286
2287         /*
2288          * I have no idea what this does, but NVIDIA do this magic sequence
2289          * in the places where this INIT_IO happens..
2290          */
2291         if (dev_priv->card_type >= NV_50 && crtcport == 0x3c3 && data == 1) {
2292                 int i;
2293
2294                 bios_wr32(bios, 0x614100, (bios_rd32(
2295                           bios, 0x614100) & 0x0fffffff) | 0x00800000);
2296
2297                 bios_wr32(bios, 0x00e18c, bios_rd32(
2298                           bios, 0x00e18c) | 0x00020000);
2299
2300                 bios_wr32(bios, 0x614900, (bios_rd32(
2301                           bios, 0x614900) & 0x0fffffff) | 0x00800000);
2302
2303                 bios_wr32(bios, 0x000200, bios_rd32(
2304                           bios, 0x000200) & ~0x40000000);
2305
2306                 mdelay(10);
2307
2308                 bios_wr32(bios, 0x00e18c, bios_rd32(
2309                           bios, 0x00e18c) & ~0x00020000);
2310
2311                 bios_wr32(bios, 0x000200, bios_rd32(
2312                           bios, 0x000200) | 0x40000000);
2313
2314                 bios_wr32(bios, 0x614100, 0x00800018);
2315                 bios_wr32(bios, 0x614900, 0x00800018);
2316
2317                 mdelay(10);
2318
2319                 bios_wr32(bios, 0x614100, 0x10000018);
2320                 bios_wr32(bios, 0x614900, 0x10000018);
2321
2322                 for (i = 0; i < 3; i++)
2323                         bios_wr32(bios, 0x614280 + (i*0x800), bios_rd32(
2324                                   bios, 0x614280 + (i*0x800)) & 0xf0f0f0f0);
2325
2326                 for (i = 0; i < 2; i++)
2327                         bios_wr32(bios, 0x614300 + (i*0x800), bios_rd32(
2328                                   bios, 0x614300 + (i*0x800)) & 0xfffff0f0);
2329
2330                 for (i = 0; i < 3; i++)
2331                         bios_wr32(bios, 0x614380 + (i*0x800), bios_rd32(
2332                                   bios, 0x614380 + (i*0x800)) & 0xfffff0f0);
2333
2334                 for (i = 0; i < 2; i++)
2335                         bios_wr32(bios, 0x614200 + (i*0x800), bios_rd32(
2336                                   bios, 0x614200 + (i*0x800)) & 0xfffffff0);
2337
2338                 for (i = 0; i < 2; i++)
2339                         bios_wr32(bios, 0x614108 + (i*0x800), bios_rd32(
2340                                   bios, 0x614108 + (i*0x800)) & 0x0fffffff);
2341                 return 5;
2342         }
2343
2344         bios_port_wr(bios, crtcport, (bios_port_rd(bios, crtcport) & mask) |
2345                                                                         data);
2346         return 5;
2347 }
2348
2349 static int
2350 init_sub(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2351 {
2352         /*
2353          * INIT_SUB   opcode: 0x6B ('k')
2354          *
2355          * offset      (8 bit): opcode
2356          * offset + 1  (8 bit): script number
2357          *
2358          * Execute script number "script number", as a subroutine
2359          */
2360
2361         uint8_t sub = bios->data[offset + 1];
2362
2363         if (!iexec->execute)
2364                 return 2;
2365
2366         BIOSLOG(bios, "0x%04X: Calling script %d\n", offset, sub);
2367
2368         parse_init_table(bios,
2369                          ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]),
2370                          iexec);
2371
2372         BIOSLOG(bios, "0x%04X: End of script %d\n", offset, sub);
2373
2374         return 2;
2375 }
2376
2377 static int
2378 init_ram_condition(struct nvbios *bios, uint16_t offset,
2379                    struct init_exec *iexec)
2380 {
2381         /*
2382          * INIT_RAM_CONDITION   opcode: 0x6D ('m')
2383          *
2384          * offset      (8 bit): opcode
2385          * offset + 1  (8 bit): mask
2386          * offset + 2  (8 bit): cmpval
2387          *
2388          * Test if (NV_PFB_BOOT_0 & "mask") equals "cmpval".
2389          * If condition not met skip subsequent opcodes until condition is
2390          * inverted (INIT_NOT), or we hit INIT_RESUME
2391          */
2392
2393         uint8_t mask = bios->data[offset + 1];
2394         uint8_t cmpval = bios->data[offset + 2];
2395         uint8_t data;
2396
2397         if (!iexec->execute)
2398                 return 3;
2399
2400         data = bios_rd32(bios, NV_PFB_BOOT_0) & mask;
2401
2402         BIOSLOG(bios, "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2403                 offset, data, cmpval);
2404
2405         if (data == cmpval)
2406                 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2407         else {
2408                 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2409                 iexec->execute = false;
2410         }
2411
2412         return 3;
2413 }
2414
2415 static int
2416 init_nv_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2417 {
2418         /*
2419          * INIT_NV_REG   opcode: 0x6E ('n')
2420          *
2421          * offset      (8  bit): opcode
2422          * offset + 1  (32 bit): register
2423          * offset + 5  (32 bit): mask
2424          * offset + 9  (32 bit): data
2425          *
2426          * Assign ((REGVAL("register") & "mask") | "data") to "register"
2427          */
2428
2429         uint32_t reg = ROM32(bios->data[offset + 1]);
2430         uint32_t mask = ROM32(bios->data[offset + 5]);
2431         uint32_t data = ROM32(bios->data[offset + 9]);
2432
2433         if (!iexec->execute)
2434                 return 13;
2435
2436         BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2437                 offset, reg, mask, data);
2438
2439         bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | data);
2440
2441         return 13;
2442 }
2443
2444 static int
2445 init_macro(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2446 {
2447         /*
2448          * INIT_MACRO   opcode: 0x6F ('o')
2449          *
2450          * offset      (8 bit): opcode
2451          * offset + 1  (8 bit): macro number
2452          *
2453          * Look up macro index "macro number" in the macro index table.
2454          * The macro index table entry has 1 byte for the index in the macro
2455          * table, and 1 byte for the number of times to repeat the macro.
2456          * The macro table entry has 4 bytes for the register address and
2457          * 4 bytes for the value to write to that register
2458          */
2459
2460         uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2461         uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2462         uint8_t macro_tbl_idx = bios->data[tmp];
2463         uint8_t count = bios->data[tmp + 1];
2464         uint32_t reg, data;
2465         int i;
2466
2467         if (!iexec->execute)
2468                 return 2;
2469
2470         BIOSLOG(bios, "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, "
2471                       "Count: 0x%02X\n",
2472                 offset, macro_index_tbl_idx, macro_tbl_idx, count);
2473
2474         for (i = 0; i < count; i++) {
2475                 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2476
2477                 reg = ROM32(bios->data[macroentryptr]);
2478                 data = ROM32(bios->data[macroentryptr + 4]);
2479
2480                 bios_wr32(bios, reg, data);
2481         }
2482
2483         return 2;
2484 }
2485
2486 static int
2487 init_done(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2488 {
2489         /*
2490          * INIT_DONE   opcode: 0x71 ('q')
2491          *
2492          * offset      (8  bit): opcode
2493          *
2494          * End the current script
2495          */
2496
2497         /* mild retval abuse to stop parsing this table */
2498         return 0;
2499 }
2500
2501 static int
2502 init_resume(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2503 {
2504         /*
2505          * INIT_RESUME   opcode: 0x72 ('r')
2506          *
2507          * offset      (8  bit): opcode
2508          *
2509          * End the current execute / no-execute condition
2510          */
2511
2512         if (iexec->execute)
2513                 return 1;
2514
2515         iexec->execute = true;
2516         BIOSLOG(bios, "0x%04X: ---- Executing following commands ----\n", offset);
2517
2518         return 1;
2519 }
2520
2521 static int
2522 init_time(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2523 {
2524         /*
2525          * INIT_TIME   opcode: 0x74 ('t')
2526          *
2527          * offset      (8  bit): opcode
2528          * offset + 1  (16 bit): time
2529          *
2530          * Sleep for "time" microseconds.
2531          */
2532
2533         unsigned time = ROM16(bios->data[offset + 1]);
2534
2535         if (!iexec->execute)
2536                 return 3;
2537
2538         BIOSLOG(bios, "0x%04X: Sleeping for 0x%04X microseconds\n",
2539                 offset, time);
2540
2541         if (time < 1000)
2542                 udelay(time);
2543         else
2544                 msleep((time + 900) / 1000);
2545
2546         return 3;
2547 }
2548
2549 static int
2550 init_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2551 {
2552         /*
2553          * INIT_CONDITION   opcode: 0x75 ('u')
2554          *
2555          * offset      (8 bit): opcode
2556          * offset + 1  (8 bit): condition number
2557          *
2558          * Check condition "condition number" in the condition table.
2559          * If condition not met skip subsequent opcodes until condition is
2560          * inverted (INIT_NOT), or we hit INIT_RESUME
2561          */
2562
2563         uint8_t cond = bios->data[offset + 1];
2564
2565         if (!iexec->execute)
2566                 return 2;
2567
2568         BIOSLOG(bios, "0x%04X: Condition: 0x%02X\n", offset, cond);
2569
2570         if (bios_condition_met(bios, offset, cond))
2571                 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2572         else {
2573                 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2574                 iexec->execute = false;
2575         }
2576
2577         return 2;
2578 }
2579
2580 static int
2581 init_io_condition(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2582 {
2583         /*
2584          * INIT_IO_CONDITION  opcode: 0x76
2585          *
2586          * offset      (8 bit): opcode
2587          * offset + 1  (8 bit): condition number
2588          *
2589          * Check condition "condition number" in the io condition table.
2590          * If condition not met skip subsequent opcodes until condition is
2591          * inverted (INIT_NOT), or we hit INIT_RESUME
2592          */
2593
2594         uint8_t cond = bios->data[offset + 1];
2595
2596         if (!iexec->execute)
2597                 return 2;
2598
2599         BIOSLOG(bios, "0x%04X: IO condition: 0x%02X\n", offset, cond);
2600
2601         if (io_condition_met(bios, offset, cond))
2602                 BIOSLOG(bios, "0x%04X: Condition fulfilled -- continuing to execute\n", offset);
2603         else {
2604                 BIOSLOG(bios, "0x%04X: Condition not fulfilled -- skipping following commands\n", offset);
2605                 iexec->execute = false;
2606         }
2607
2608         return 2;
2609 }
2610
2611 static int
2612 init_index_io(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2613 {
2614         /*
2615          * INIT_INDEX_IO   opcode: 0x78 ('x')
2616          *
2617          * offset      (8  bit): opcode
2618          * offset + 1  (16 bit): CRTC port
2619          * offset + 3  (8  bit): CRTC index
2620          * offset + 4  (8  bit): mask
2621          * offset + 5  (8  bit): data
2622          *
2623          * Read value at index "CRTC index" on "CRTC port", AND with "mask",
2624          * OR with "data", write-back
2625          */
2626
2627         uint16_t crtcport = ROM16(bios->data[offset + 1]);
2628         uint8_t crtcindex = bios->data[offset + 3];
2629         uint8_t mask = bios->data[offset + 4];
2630         uint8_t data = bios->data[offset + 5];
2631         uint8_t value;
2632
2633         if (!iexec->execute)
2634                 return 6;
2635
2636         BIOSLOG(bios, "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, "
2637                       "Data: 0x%02X\n",
2638                 offset, crtcport, crtcindex, mask, data);
2639
2640         value = (bios_idxprt_rd(bios, crtcport, crtcindex) & mask) | data;
2641         bios_idxprt_wr(bios, crtcport, crtcindex, value);
2642
2643         return 6;
2644 }
2645
2646 static int
2647 init_pll(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2648 {
2649         /*
2650          * INIT_PLL   opcode: 0x79 ('y')
2651          *
2652          * offset      (8  bit): opcode
2653          * offset + 1  (32 bit): register
2654          * offset + 5  (16 bit): freq
2655          *
2656          * Set PLL register "register" to coefficients for frequency (10kHz)
2657          * "freq"
2658          */
2659
2660         uint32_t reg = ROM32(bios->data[offset + 1]);
2661         uint16_t freq = ROM16(bios->data[offset + 5]);
2662
2663         if (!iexec->execute)
2664                 return 7;
2665
2666         BIOSLOG(bios, "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n", offset, reg, freq);
2667
2668         setPLL(bios, reg, freq * 10);
2669
2670         return 7;
2671 }
2672
2673 static int
2674 init_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2675 {
2676         /*
2677          * INIT_ZM_REG   opcode: 0x7A ('z')
2678          *
2679          * offset      (8  bit): opcode
2680          * offset + 1  (32 bit): register
2681          * offset + 5  (32 bit): value
2682          *
2683          * Assign "value" to "register"
2684          */
2685
2686         uint32_t reg = ROM32(bios->data[offset + 1]);
2687         uint32_t value = ROM32(bios->data[offset + 5]);
2688
2689         if (!iexec->execute)
2690                 return 9;
2691
2692         if (reg == 0x000200)
2693                 value |= 1;
2694
2695         bios_wr32(bios, reg, value);
2696
2697         return 9;
2698 }
2699
2700 static int
2701 init_ram_restrict_pll(struct nvbios *bios, uint16_t offset,
2702                       struct init_exec *iexec)
2703 {
2704         /*
2705          * INIT_RAM_RESTRICT_PLL   opcode: 0x87 ('')
2706          *
2707          * offset      (8 bit): opcode
2708          * offset + 1  (8 bit): PLL type
2709          * offset + 2 (32 bit): frequency 0
2710          *
2711          * Uses the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
2712          * ram_restrict_table_ptr.  The value read from there is used to select
2713          * a frequency from the table starting at 'frequency 0' to be
2714          * programmed into the PLL corresponding to 'type'.
2715          *
2716          * The PLL limits table on cards using this opcode has a mapping of
2717          * 'type' to the relevant registers.
2718          */
2719
2720         struct drm_device *dev = bios->dev;
2721         uint32_t strap = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) & 0x0000003c) >> 2;
2722         uint8_t index = bios->data[bios->ram_restrict_tbl_ptr + strap];
2723         uint8_t type = bios->data[offset + 1];
2724         uint32_t freq = ROM32(bios->data[offset + 2 + (index * 4)]);
2725         uint8_t *pll_limits = &bios->data[bios->pll_limit_tbl_ptr], *entry;
2726         int len = 2 + bios->ram_restrict_group_count * 4;
2727         int i;
2728
2729         if (!iexec->execute)
2730                 return len;
2731
2732         if (!bios->pll_limit_tbl_ptr || (pll_limits[0] & 0xf0) != 0x30) {
2733                 NV_ERROR(dev, "PLL limits table not version 3.x\n");
2734                 return len; /* deliberate, allow default clocks to remain */
2735         }
2736
2737         entry = pll_limits + pll_limits[1];
2738         for (i = 0; i < pll_limits[3]; i++, entry += pll_limits[2]) {
2739                 if (entry[0] == type) {
2740                         uint32_t reg = ROM32(entry[3]);
2741
2742                         BIOSLOG(bios, "0x%04X: "
2743                                       "Type %02x Reg 0x%08x Freq %dKHz\n",
2744                                 offset, type, reg, freq);
2745
2746                         setPLL(bios, reg, freq);
2747                         return len;
2748                 }
2749         }
2750
2751         NV_ERROR(dev, "PLL type 0x%02x not found in PLL limits table", type);
2752         return len;
2753 }
2754
2755 static int
2756 init_8c(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2757 {
2758         /*
2759          * INIT_8C   opcode: 0x8C ('')
2760          *
2761          * NOP so far....
2762          *
2763          */
2764
2765         return 1;
2766 }
2767
2768 static int
2769 init_8d(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2770 {
2771         /*
2772          * INIT_8D   opcode: 0x8D ('')
2773          *
2774          * NOP so far....
2775          *
2776          */
2777
2778         return 1;
2779 }
2780
2781 static int
2782 init_gpio(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2783 {
2784         /*
2785          * INIT_GPIO   opcode: 0x8E ('')
2786          *
2787          * offset      (8 bit): opcode
2788          *
2789          * Loop over all entries in the DCB GPIO table, and initialise
2790          * each GPIO according to various values listed in each entry
2791          */
2792
2793         struct drm_nouveau_private *dev_priv = bios->dev->dev_private;
2794         const uint32_t nv50_gpio_ctl[2] = { 0xe100, 0xe28c };
2795         int i;
2796
2797         if (dev_priv->card_type != NV_50) {
2798                 NV_ERROR(bios->dev, "INIT_GPIO on unsupported chipset\n");
2799                 return -ENODEV;
2800         }
2801
2802         if (!iexec->execute)
2803                 return 1;
2804
2805         for (i = 0; i < bios->dcb.gpio.entries; i++) {
2806                 struct dcb_gpio_entry *gpio = &bios->dcb.gpio.entry[i];
2807                 uint32_t r, s, v;
2808
2809                 BIOSLOG(bios, "0x%04X: Entry: 0x%08X\n", offset, gpio->entry);
2810
2811                 nv50_gpio_set(bios->dev, gpio->tag, gpio->state_default);
2812
2813                 /* The NVIDIA binary driver doesn't appear to actually do
2814                  * any of this, my VBIOS does however.
2815                  */
2816                 /* Not a clue, needs de-magicing */
2817                 r = nv50_gpio_ctl[gpio->line >> 4];
2818                 s = (gpio->line & 0x0f);
2819                 v = bios_rd32(bios, r) & ~(0x00010001 << s);
2820                 switch ((gpio->entry & 0x06000000) >> 25) {
2821                 case 1:
2822                         v |= (0x00000001 << s);
2823                         break;
2824                 case 2:
2825                         v |= (0x00010000 << s);
2826                         break;
2827                 default:
2828                         break;
2829                 }
2830                 bios_wr32(bios, r, v);
2831         }
2832
2833         return 1;
2834 }
2835
2836 static int
2837 init_ram_restrict_zm_reg_group(struct nvbios *bios, uint16_t offset,
2838                                struct init_exec *iexec)
2839 {
2840         /*
2841          * INIT_RAM_RESTRICT_ZM_REG_GROUP   opcode: 0x8F ('')
2842          *
2843          * offset      (8  bit): opcode
2844          * offset + 1  (32 bit): reg
2845          * offset + 5  (8  bit): regincrement
2846          * offset + 6  (8  bit): count
2847          * offset + 7  (32 bit): value 1,1
2848          * ...
2849          *
2850          * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
2851          * ram_restrict_table_ptr. The value read from here is 'n', and
2852          * "value 1,n" gets written to "reg". This repeats "count" times and on
2853          * each iteration 'm', "reg" increases by "regincrement" and
2854          * "value m,n" is used. The extent of n is limited by a number read
2855          * from the 'M' BIT table, herein called "blocklen"
2856          */
2857
2858         uint32_t reg = ROM32(bios->data[offset + 1]);
2859         uint8_t regincrement = bios->data[offset + 5];
2860         uint8_t count = bios->data[offset + 6];
2861         uint32_t strap_ramcfg, data;
2862         /* previously set by 'M' BIT table */
2863         uint16_t blocklen = bios->ram_restrict_group_count * 4;
2864         int len = 7 + count * blocklen;
2865         uint8_t index;
2866         int i;
2867
2868
2869         if (!iexec->execute)
2870                 return len;
2871
2872         if (!blocklen) {
2873                 NV_ERROR(bios->dev,
2874                          "0x%04X: Zero block length - has the M table "
2875                          "been parsed?\n", offset);
2876                 return -EINVAL;
2877         }
2878
2879         strap_ramcfg = (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
2880         index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
2881
2882         BIOSLOG(bios, "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, "
2883                       "Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
2884                 offset, reg, regincrement, count, strap_ramcfg, index);
2885
2886         for (i = 0; i < count; i++) {
2887                 data = ROM32(bios->data[offset + 7 + index * 4 + blocklen * i]);
2888
2889                 bios_wr32(bios, reg, data);
2890
2891                 reg += regincrement;
2892         }
2893
2894         return len;
2895 }
2896
2897 static int
2898 init_copy_zm_reg(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2899 {
2900         /*
2901          * INIT_COPY_ZM_REG   opcode: 0x90 ('')
2902          *
2903          * offset      (8  bit): opcode
2904          * offset + 1  (32 bit): src reg
2905          * offset + 5  (32 bit): dst reg
2906          *
2907          * Put contents of "src reg" into "dst reg"
2908          */
2909
2910         uint32_t srcreg = ROM32(bios->data[offset + 1]);
2911         uint32_t dstreg = ROM32(bios->data[offset + 5]);
2912
2913         if (!iexec->execute)
2914                 return 9;
2915
2916         bios_wr32(bios, dstreg, bios_rd32(bios, srcreg));
2917
2918         return 9;
2919 }
2920
2921 static int
2922 init_zm_reg_group_addr_latched(struct nvbios *bios, uint16_t offset,
2923                                struct init_exec *iexec)
2924 {
2925         /*
2926          * INIT_ZM_REG_GROUP_ADDRESS_LATCHED   opcode: 0x91 ('')
2927          *
2928          * offset      (8  bit): opcode
2929          * offset + 1  (32 bit): dst reg
2930          * offset + 5  (8  bit): count
2931          * offset + 6  (32 bit): data 1
2932          * ...
2933          *
2934          * For each of "count" values write "data n" to "dst reg"
2935          */
2936
2937         uint32_t reg = ROM32(bios->data[offset + 1]);
2938         uint8_t count = bios->data[offset + 5];
2939         int len = 6 + count * 4;
2940         int i;
2941
2942         if (!iexec->execute)
2943                 return len;
2944
2945         for (i = 0; i < count; i++) {
2946                 uint32_t data = ROM32(bios->data[offset + 6 + 4 * i]);
2947                 bios_wr32(bios, reg, data);
2948         }
2949
2950         return len;
2951 }
2952
2953 static int
2954 init_reserved(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2955 {
2956         /*
2957          * INIT_RESERVED   opcode: 0x92 ('')
2958          *
2959          * offset      (8 bit): opcode
2960          *
2961          * Seemingly does nothing
2962          */
2963
2964         return 1;
2965 }
2966
2967 static int
2968 init_96(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
2969 {
2970         /*
2971          * INIT_96   opcode: 0x96 ('')
2972          *
2973          * offset      (8  bit): opcode
2974          * offset + 1  (32 bit): sreg
2975          * offset + 5  (8  bit): sshift
2976          * offset + 6  (8  bit): smask
2977          * offset + 7  (8  bit): index
2978          * offset + 8  (32 bit): reg
2979          * offset + 12 (32 bit): mask
2980          * offset + 16 (8  bit): shift
2981          *
2982          */
2983
2984         uint16_t xlatptr = bios->init96_tbl_ptr + (bios->data[offset + 7] * 2);
2985         uint32_t reg = ROM32(bios->data[offset + 8]);
2986         uint32_t mask = ROM32(bios->data[offset + 12]);
2987         uint32_t val;
2988
2989         val = bios_rd32(bios, ROM32(bios->data[offset + 1]));
2990         if (bios->data[offset + 5] < 0x80)
2991                 val >>= bios->data[offset + 5];
2992         else
2993                 val <<= (0x100 - bios->data[offset + 5]);
2994         val &= bios->data[offset + 6];
2995
2996         val   = bios->data[ROM16(bios->data[xlatptr]) + val];
2997         val <<= bios->data[offset + 16];
2998
2999         if (!iexec->execute)
3000                 return 17;
3001
3002         bios_wr32(bios, reg, (bios_rd32(bios, reg) & mask) | val);
3003         return 17;
3004 }
3005
3006 static int
3007 init_97(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3008 {
3009         /*
3010          * INIT_97   opcode: 0x97 ('')
3011          *
3012          * offset      (8  bit): opcode
3013          * offset + 1  (32 bit): register
3014          * offset + 5  (32 bit): mask
3015          * offset + 9  (32 bit): value
3016          *
3017          * Adds "value" to "register" preserving the fields specified
3018          * by "mask"
3019          */
3020
3021         uint32_t reg = ROM32(bios->data[offset + 1]);
3022         uint32_t mask = ROM32(bios->data[offset + 5]);
3023         uint32_t add = ROM32(bios->data[offset + 9]);
3024         uint32_t val;
3025
3026         val = bios_rd32(bios, reg);
3027         val = (val & mask) | ((val + add) & ~mask);
3028
3029         if (!iexec->execute)
3030                 return 13;
3031
3032         bios_wr32(bios, reg, val);
3033         return 13;
3034 }
3035
3036 static int
3037 init_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3038 {
3039         /*
3040          * INIT_AUXCH   opcode: 0x98 ('')
3041          *
3042          * offset      (8  bit): opcode
3043          * offset + 1  (32 bit): address
3044          * offset + 5  (8  bit): count
3045          * offset + 6  (8  bit): mask 0
3046          * offset + 7  (8  bit): data 0
3047          *  ...
3048          *
3049          */
3050
3051         struct drm_device *dev = bios->dev;
3052         struct nouveau_i2c_chan *auxch;
3053         uint32_t addr = ROM32(bios->data[offset + 1]);
3054         uint8_t count = bios->data[offset + 5];
3055         int len = 6 + count * 2;
3056         int ret, i;
3057
3058         if (!bios->display.output) {
3059                 NV_ERROR(dev, "INIT_AUXCH: no active output\n");
3060                 return -EINVAL;
3061         }
3062
3063         auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3064         if (!auxch) {
3065                 NV_ERROR(dev, "INIT_AUXCH: couldn't get auxch %d\n",
3066                          bios->display.output->i2c_index);
3067                 return -ENODEV;
3068         }
3069
3070         if (!iexec->execute)
3071                 return len;
3072
3073         offset += 6;
3074         for (i = 0; i < count; i++, offset += 2) {
3075                 uint8_t data;
3076
3077                 ret = nouveau_dp_auxch(auxch, 9, addr, &data, 1);
3078                 if (ret) {
3079                         NV_ERROR(dev, "INIT_AUXCH: rd auxch fail %d\n", ret);
3080                         return ret;
3081                 }
3082
3083                 data &= bios->data[offset + 0];
3084                 data |= bios->data[offset + 1];
3085
3086                 ret = nouveau_dp_auxch(auxch, 8, addr, &data, 1);
3087                 if (ret) {
3088                         NV_ERROR(dev, "INIT_AUXCH: wr auxch fail %d\n", ret);
3089                         return ret;
3090                 }
3091         }
3092
3093         return len;
3094 }
3095
3096 static int
3097 init_zm_auxch(struct nvbios *bios, uint16_t offset, struct init_exec *iexec)
3098 {
3099         /*
3100          * INIT_ZM_AUXCH   opcode: 0x99 ('')
3101          *
3102          * offset      (8  bit): opcode
3103          * offset + 1  (32 bit): address
3104          * offset + 5  (8  bit): count
3105          * offset + 6  (8  bit): data 0
3106          *  ...
3107          *
3108          */
3109
3110         struct drm_device *dev = bios->dev;
3111         struct nouveau_i2c_chan *auxch;
3112         uint32_t addr = ROM32(bios->data[offset + 1]);
3113         uint8_t count = bios->data[offset + 5];
3114         int len = 6 + count;
3115         int ret, i;
3116
3117         if (!bios->display.output) {
3118                 NV_ERROR(dev, "INIT_ZM_AUXCH: no active output\n");
3119                 return -EINVAL;
3120         }
3121
3122         auxch = init_i2c_device_find(dev, bios->display.output->i2c_index);
3123         if (!auxch) {
3124                 NV_ERROR(dev, "INIT_ZM_AUXCH: couldn't get auxch %d\n",
3125                          bios->display.output->i2c_index);
3126                 return -ENODEV;
3127         }
3128
3129         if (!iexec->execute)
3130                 return len;
3131
3132         offset += 6;
3133         for (i = 0; i < count; i++, offset++) {
3134                 ret = nouveau_dp_auxch(auxch, 8, addr, &bios->data[offset], 1);
3135                 if (ret) {
3136                         NV_ERROR(dev, "INIT_ZM_AUXCH: wr auxch fail %d\n", ret);
3137                         return ret;
3138                 }
3139         }
3140
3141         return len;
3142 }
3143
3144 static struct init_tbl_entry itbl_entry[] = {
3145         /* command name                       , id  , length  , offset  , mult    , command handler                 */
3146         /* INIT_PROG (0x31, 15, 10, 4) removed due to no example of use */
3147         { "INIT_IO_RESTRICT_PROG"             , 0x32, init_io_restrict_prog           },
3148         { "INIT_REPEAT"                       , 0x33, init_repeat                     },
3149         { "INIT_IO_RESTRICT_PLL"              , 0x34, init_io_restrict_pll            },
3150         { "INIT_END_REPEAT"                   , 0x36, init_end_repeat                 },
3151         { "INIT_COPY"                         , 0x37, init_copy                       },
3152         { "INIT_NOT"                          , 0x38, init_not                        },
3153         { "INIT_IO_FLAG_CONDITION"            , 0x39, init_io_flag_condition          },
3154         { "INIT_DP_CONDITION"                 , 0x3A, init_dp_condition               },
3155         { "INIT_OP_3B"                        , 0x3B, init_op_3b                      },
3156         { "INIT_OP_3C"                        , 0x3C, init_op_3c                      },
3157         { "INIT_INDEX_ADDRESS_LATCHED"        , 0x49, init_idx_addr_latched           },
3158         { "INIT_IO_RESTRICT_PLL2"             , 0x4A, init_io_restrict_pll2           },
3159         { "INIT_PLL2"                         , 0x4B, init_pll2                       },
3160         { "INIT_I2C_BYTE"                     , 0x4C, init_i2c_byte                   },
3161         { "INIT_ZM_I2C_BYTE"                  , 0x4D, init_zm_i2c_byte                },
3162         { "INIT_ZM_I2C"                       , 0x4E, init_zm_i2c                     },
3163         { "INIT_TMDS"                         , 0x4F, init_tmds                       },
3164         { "INIT_ZM_TMDS_GROUP"                , 0x50, init_zm_tmds_group              },
3165         { "INIT_CR_INDEX_ADDRESS_LATCHED"     , 0x51, init_cr_idx_adr_latch           },
3166         { "INIT_CR"                           , 0x52, init_cr                         },
3167         { "INIT_ZM_CR"                        , 0x53, init_zm_cr                      },
3168         { "INIT_ZM_CR_GROUP"                  , 0x54, init_zm_cr_group                },
3169         { "INIT_CONDITION_TIME"               , 0x56, init_condition_time             },
3170         { "INIT_ZM_REG_SEQUENCE"              , 0x58, init_zm_reg_sequence            },
3171         /* INIT_INDIRECT_REG (0x5A, 7, 0, 0) removed due to no example of use */
3172         { "INIT_SUB_DIRECT"                   , 0x5B, init_sub_direct                 },
3173         { "INIT_COPY_NV_REG"                  , 0x5F, init_copy_nv_reg                },
3174         { "INIT_ZM_INDEX_IO"                  , 0x62, init_zm_index_io                },
3175         { "INIT_COMPUTE_MEM"                  , 0x63, init_compute_mem                },
3176         { "INIT_RESET"                        , 0x65, init_reset                      },
3177         { "INIT_CONFIGURE_MEM"                , 0x66, init_configure_mem              },
3178         { "INIT_CONFIGURE_CLK"                , 0x67, init_configure_clk              },
3179         { "INIT_CONFIGURE_PREINIT"            , 0x68, init_configure_preinit          },
3180         { "INIT_IO"                           , 0x69, init_io                         },
3181         { "INIT_SUB"                          , 0x6B, init_sub                        },
3182         { "INIT_RAM_CONDITION"                , 0x6D, init_ram_condition              },
3183         { "INIT_NV_REG"                       , 0x6E, init_nv_reg                     },
3184         { "INIT_MACRO"                        , 0x6F, init_macro                      },
3185         { "INIT_DONE"                         , 0x71, init_done                       },
3186         { "INIT_RESUME"                       , 0x72, init_resume                     },
3187         /* INIT_RAM_CONDITION2 (0x73, 9, 0, 0) removed due to no example of use */
3188         { "INIT_TIME"                         , 0x74, init_time                       },
3189         { "INIT_CONDITION"                    , 0x75, init_condition                  },
3190         { "INIT_IO_CONDITION"                 , 0x76, init_io_condition               },
3191         { "INIT_INDEX_IO"                     , 0x78, init_index_io                   },
3192         { "INIT_PLL"                          , 0x79, init_pll                        },
3193         { "INIT_ZM_REG"                       , 0x7A, init_zm_reg                     },
3194         { "INIT_RAM_RESTRICT_PLL"             , 0x87, init_ram_restrict_pll           },
3195         { "INIT_8C"                           , 0x8C, init_8c                         },
3196         { "INIT_8D"                           , 0x8D, init_8d                         },
3197         { "INIT_GPIO"                         , 0x8E, init_gpio                       },
3198         { "INIT_RAM_RESTRICT_ZM_REG_GROUP"    , 0x8F, init_ram_restrict_zm_reg_group  },
3199         { "INIT_COPY_ZM_REG"                  , 0x90, init_copy_zm_reg                },
3200         { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, init_zm_reg_group_addr_latched  },
3201         { "INIT_RESERVED"                     , 0x92, init_reserved                   },
3202         { "INIT_96"                           , 0x96, init_96                         },
3203         { "INIT_97"                           , 0x97, init_97                         },
3204         { "INIT_AUXCH"                        , 0x98, init_auxch                      },
3205         { "INIT_ZM_AUXCH"                     , 0x99, init_zm_auxch                   },
3206         { NULL                                , 0   , NULL                            }
3207 };
3208
3209 #define MAX_TABLE_OPS 1000
3210
3211 static int
3212 parse_init_table(struct nvbios *bios, unsigned int offset,
3213                  struct init_exec *iexec)
3214 {
3215         /*
3216          * Parses all commands in an init table.
3217          *
3218          * We start out executing all commands found in the init table. Some
3219          * opcodes may change the status of iexec->execute to SKIP, which will
3220          * cause the following opcodes to perform no operation until the value
3221          * is changed back to EXECUTE.
3222          */
3223
3224         int count = 0, i, ret;
3225         uint8_t id;
3226
3227         /*
3228          * Loop until INIT_DONE causes us to break out of the loop
3229          * (or until offset > bios length just in case... )
3230          * (and no more than MAX_TABLE_OPS iterations, just in case... )
3231          */
3232         while ((offset < bios->length) && (count++ < MAX_TABLE_OPS)) {
3233                 id = bios->data[offset];
3234
3235                 /* Find matching id in itbl_entry */
3236                 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
3237                         ;
3238
3239                 if (!itbl_entry[i].name) {
3240                         NV_ERROR(bios->dev,
3241                                  "0x%04X: Init table command not found: "
3242                                  "0x%02X\n", offset, id);
3243                         return -ENOENT;
3244                 }
3245
3246                 BIOSLOG(bios, "0x%04X: [ (0x%02X) - %s ]\n", offset,
3247                         itbl_entry[i].id, itbl_entry[i].name);
3248
3249                 /* execute eventual command handler */
3250                 ret = (*itbl_entry[i].handler)(bios, offset, iexec);
3251                 if (ret < 0) {
3252                         NV_ERROR(bios->dev, "0x%04X: Failed parsing init "
3253                                  "table opcode: %s %d\n", offset,
3254                                  itbl_entry[i].name, ret);
3255                 }
3256
3257                 if (ret <= 0)
3258                         break;
3259
3260                 /*
3261                  * Add the offset of the current command including all data
3262                  * of that command. The offset will then be pointing on the
3263                  * next op code.
3264                  */
3265                 offset += ret;
3266         }
3267
3268         if (offset >= bios->length)
3269                 NV_WARN(bios->dev,
3270                         "Offset 0x%04X greater than known bios image length.  "
3271                         "Corrupt image?\n", offset);
3272         if (count >= MAX_TABLE_OPS)
3273                 NV_WARN(bios->dev,
3274                         "More than %d opcodes to a table is unlikely, "
3275                         "is the bios image corrupt?\n", MAX_TABLE_OPS);
3276
3277         return 0;
3278 }
3279
3280 static void
3281 parse_init_tables(struct nvbios *bios)
3282 {
3283         /* Loops and calls parse_init_table() for each present table. */
3284
3285         int i = 0;
3286         uint16_t table;
3287         struct init_exec iexec = {true, false};
3288
3289         if (bios->old_style_init) {
3290                 if (bios->init_script_tbls_ptr)
3291                         parse_init_table(bios, bios->init_script_tbls_ptr, &iexec);
3292                 if (bios->extra_init_script_tbl_ptr)
3293                         parse_init_table(bios, bios->extra_init_script_tbl_ptr, &iexec);
3294
3295                 return;
3296         }
3297
3298         while ((table = ROM16(bios->data[bios->init_script_tbls_ptr + i]))) {
3299                 NV_INFO(bios->dev,
3300                         "Parsing VBIOS init table %d at offset 0x%04X\n",
3301                         i / 2, table);
3302                 BIOSLOG(bios, "0x%04X: ------ Executing following commands ------\n", table);
3303
3304                 parse_init_table(bios, table, &iexec);
3305                 i += 2;
3306         }
3307 }
3308
3309 static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
3310 {
3311         int compare_record_len, i = 0;
3312         uint16_t compareclk, scriptptr = 0;
3313
3314         if (bios->major_version < 5) /* pre BIT */
3315                 compare_record_len = 3;
3316         else
3317                 compare_record_len = 4;
3318
3319         do {
3320                 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
3321                 if (pxclk >= compareclk * 10) {
3322                         if (bios->major_version < 5) {
3323                                 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
3324                                 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
3325                         } else
3326                                 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
3327                         break;
3328                 }
3329                 i++;
3330         } while (compareclk);
3331
3332         return scriptptr;
3333 }
3334
3335 static void
3336 run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
3337                       struct dcb_entry *dcbent, int head, bool dl)
3338 {
3339         struct drm_nouveau_private *dev_priv = dev->dev_private;
3340         struct nvbios *bios = &dev_priv->vbios;
3341         struct init_exec iexec = {true, false};
3342
3343         NV_TRACE(dev, "0x%04X: Parsing digital output script table\n",
3344                  scriptptr);
3345         bios_idxprt_wr(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_44,
3346                        head ? NV_CIO_CRE_44_HEADB : NV_CIO_CRE_44_HEADA);
3347         /* note: if dcb entries have been merged, index may be misleading */
3348         NVWriteVgaCrtc5758(dev, head, 0, dcbent->index);
3349         parse_init_table(bios, scriptptr, &iexec);
3350
3351         nv04_dfp_bind_head(dev, dcbent, head, dl);
3352 }
3353
3354 static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script)
3355 {
3356         struct drm_nouveau_private *dev_priv = dev->dev_private;
3357         struct nvbios *bios = &dev_priv->vbios;
3358         uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
3359         uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
3360
3361         if (!bios->fp.xlated_entry || !sub || !scriptofs)
3362                 return -EINVAL;
3363
3364         run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
3365
3366         if (script == LVDS_PANEL_OFF) {
3367                 /* off-on delay in ms */
3368                 msleep(ROM16(bios->data[bios->fp.xlated_entry + 7]));
3369         }
3370 #ifdef __powerpc__
3371         /* Powerbook specific quirks */
3372         if ((dev->pci_device & 0xffff) == 0x0179 ||
3373             (dev->pci_device & 0xffff) == 0x0189 ||
3374             (dev->pci_device & 0xffff) == 0x0329) {
3375                 if (script == LVDS_RESET) {
3376                         nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
3377
3378                 } else if (script == LVDS_PANEL_ON) {
3379                         bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
3380                                   bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
3381                                   | (1 << 31));
3382                         bios_wr32(bios, NV_PCRTC_GPIO_EXT,
3383                                   bios_rd32(bios, NV_PCRTC_GPIO_EXT) | 1);
3384
3385                 } else if (script == LVDS_PANEL_OFF) {
3386                         bios_wr32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL,
3387                                   bios_rd32(bios, NV_PBUS_DEBUG_DUALHEAD_CTL)
3388                                   & ~(1 << 31));
3389                         bios_wr32(bios, NV_PCRTC_GPIO_EXT,
3390                                   bios_rd32(bios, NV_PCRTC_GPIO_EXT) & ~3);
3391                 }
3392         }
3393 #endif
3394
3395         return 0;
3396 }
3397
3398 static int run_lvds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3399 {
3400         /*
3401          * The BIT LVDS table's header has the information to setup the
3402          * necessary registers. Following the standard 4 byte header are:
3403          * A bitmask byte and a dual-link transition pxclk value for use in
3404          * selecting the init script when not using straps; 4 script pointers
3405          * for panel power, selected by output and on/off; and 8 table pointers
3406          * for panel init, the needed one determined by output, and bits in the
3407          * conf byte. These tables are similar to the TMDS tables, consisting
3408          * of a list of pxclks and script pointers.
3409          */
3410         struct drm_nouveau_private *dev_priv = dev->dev_private;
3411         struct nvbios *bios = &dev_priv->vbios;
3412         unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
3413         uint16_t scriptptr = 0, clktable;
3414
3415         /*
3416          * For now we assume version 3.0 table - g80 support will need some
3417          * changes
3418          */
3419
3420         switch (script) {
3421         case LVDS_INIT:
3422                 return -ENOSYS;
3423         case LVDS_BACKLIGHT_ON:
3424         case LVDS_PANEL_ON:
3425                 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
3426                 break;
3427         case LVDS_BACKLIGHT_OFF:
3428         case LVDS_PANEL_OFF:
3429                 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
3430                 break;
3431         case LVDS_RESET:
3432                 clktable = bios->fp.lvdsmanufacturerpointer + 15;
3433                 if (dcbent->or == 4)
3434                         clktable += 8;
3435
3436                 if (dcbent->lvdsconf.use_straps_for_mode) {
3437                         if (bios->fp.dual_link)
3438                                 clktable += 4;
3439                         if (bios->fp.if_is_24bit)
3440                                 clktable += 2;
3441                 } else {
3442                         /* using EDID */
3443                         int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
3444
3445                         if (bios->fp.dual_link) {
3446                                 clktable += 4;
3447                                 cmpval_24bit <<= 1;
3448                         }
3449
3450                         if (bios->fp.strapless_is_24bit & cmpval_24bit)
3451                                 clktable += 2;
3452                 }
3453
3454                 clktable = ROM16(bios->data[clktable]);
3455                 if (!clktable) {
3456                         NV_ERROR(dev, "Pixel clock comparison table not found\n");
3457                         return -ENOENT;
3458                 }
3459                 scriptptr = clkcmptable(bios, clktable, pxclk);
3460         }
3461
3462         if (!scriptptr) {
3463                 NV_ERROR(dev, "LVDS output init script not found\n");
3464                 return -ENOENT;
3465         }
3466         run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
3467
3468         return 0;
3469 }
3470
3471 int call_lvds_script(struct drm_device *dev, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
3472 {
3473         /*
3474          * LVDS operations are multiplexed in an effort to present a single API
3475          * which works with two vastly differing underlying structures.
3476          * This acts as the demux
3477          */
3478
3479         struct drm_nouveau_private *dev_priv = dev->dev_private;
3480         struct nvbios *bios = &dev_priv->vbios;
3481         uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3482         uint32_t sel_clk_binding, sel_clk;
3483         int ret;
3484
3485         if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
3486             (lvds_ver >= 0x30 && script == LVDS_INIT))
3487                 return 0;
3488
3489         if (!bios->fp.lvds_init_run) {
3490                 bios->fp.lvds_init_run = true;
3491                 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
3492         }
3493
3494         if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
3495                 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
3496         if (script == LVDS_RESET && bios->fp.power_off_for_reset)
3497                 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
3498
3499         NV_TRACE(dev, "Calling LVDS script %d:\n", script);
3500
3501         /* don't let script change pll->head binding */
3502         sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
3503
3504         if (lvds_ver < 0x30)
3505                 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
3506         else
3507                 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
3508
3509         bios->fp.last_script_invoc = (script << 1 | head);
3510
3511         sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
3512         NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
3513         /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
3514         nvWriteMC(dev, NV_PBUS_POWERCTRL_2, 0);
3515
3516         return ret;
3517 }
3518
3519 struct lvdstableheader {
3520         uint8_t lvds_ver, headerlen, recordlen;
3521 };
3522
3523 static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
3524 {
3525         /*
3526          * BMP version (0xa) LVDS table has a simple header of version and
3527          * record length. The BIT LVDS table has the typical BIT table header:
3528          * version byte, header length byte, record length byte, and a byte for
3529          * the maximum number of records that can be held in the table.
3530          */
3531
3532         uint8_t lvds_ver, headerlen, recordlen;
3533
3534         memset(lth, 0, sizeof(struct lvdstableheader));
3535
3536         if (bios->fp.lvdsmanufacturerpointer == 0x0) {
3537                 NV_ERROR(dev, "Pointer to LVDS manufacturer table invalid\n");
3538                 return -EINVAL;
3539         }
3540
3541         lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3542
3543         switch (lvds_ver) {
3544         case 0x0a:      /* pre NV40 */
3545                 headerlen = 2;
3546                 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3547                 break;
3548         case 0x30:      /* NV4x */
3549                 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3550                 if (headerlen < 0x1f) {
3551                         NV_ERROR(dev, "LVDS table header not understood\n");
3552                         return -EINVAL;
3553                 }
3554                 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3555                 break;
3556         case 0x40:      /* G80/G90 */
3557                 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3558                 if (headerlen < 0x7) {
3559                         NV_ERROR(dev, "LVDS table header not understood\n");
3560                         return -EINVAL;
3561                 }
3562                 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3563                 break;
3564         default:
3565                 NV_ERROR(dev,
3566                          "LVDS table revision %d.%d not currently supported\n",
3567                          lvds_ver >> 4, lvds_ver & 0xf);
3568                 return -ENOSYS;
3569         }
3570
3571         lth->lvds_ver = lvds_ver;
3572         lth->headerlen = headerlen;
3573         lth->recordlen = recordlen;
3574
3575         return 0;
3576 }
3577
3578 static int
3579 get_fp_strap(struct drm_device *dev, struct nvbios *bios)
3580 {
3581         struct drm_nouveau_private *dev_priv = dev->dev_private;
3582
3583         /*
3584          * The fp strap is normally dictated by the "User Strap" in
3585          * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
3586          * Internal_Flags struct at 0x48 is set, the user strap gets overriden
3587          * by the PCI subsystem ID during POST, but not before the previous user
3588          * strap has been committed to CR58 for CR57=0xf on head A, which may be
3589          * read and used instead
3590          */
3591
3592         if (bios->major_version < 5 && bios->data[0x48] & 0x4)
3593                 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
3594
3595         if (dev_priv->card_type >= NV_50)
3596                 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
3597         else
3598                 return (bios_rd32(bios, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
3599 }
3600
3601 static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
3602 {
3603         uint8_t *fptable;
3604         uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
3605         int ret, ofs, fpstrapping;
3606         struct lvdstableheader lth;
3607
3608         if (bios->fp.fptablepointer == 0x0) {
3609                 /* Apple cards don't have the fp table; the laptops use DDC */
3610                 /* The table is also missing on some x86 IGPs */
3611 #ifndef __powerpc__
3612                 NV_ERROR(dev, "Pointer to flat panel table invalid\n");
3613 #endif
3614                 bios->digital_min_front_porch = 0x4b;
3615                 return 0;
3616         }
3617
3618         fptable = &bios->data[bios->fp.fptablepointer];
3619         fptable_ver = fptable[0];
3620
3621         switch (fptable_ver) {
3622         /*
3623          * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
3624          * version field, and miss one of the spread spectrum/PWM bytes.
3625          * This could affect early GF2Go parts (not seen any appropriate ROMs
3626          * though). Here we assume that a version of 0x05 matches this case
3627          * (combining with a BMP version check would be better), as the
3628          * common case for the panel type field is 0x0005, and that is in
3629          * fact what we are reading the first byte of.
3630          */
3631         case 0x05:      /* some NV10, 11, 15, 16 */
3632                 recordlen = 42;
3633                 ofs = -1;
3634                 break;
3635         case 0x10:      /* some NV15/16, and NV11+ */
3636                 recordlen = 44;
3637                 ofs = 0;
3638                 break;
3639         case 0x20:      /* NV40+ */
3640                 headerlen = fptable[1];
3641                 recordlen = fptable[2];
3642                 fpentries = fptable[3];
3643                 /*
3644                  * fptable[4] is the minimum
3645                  * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
3646                  */
3647                 bios->digital_min_front_porch = fptable[4];
3648                 ofs = -7;
3649                 break;
3650         default:
3651                 NV_ERROR(dev,
3652                          "FP table revision %d.%d not currently supported\n",
3653                          fptable_ver >> 4, fptable_ver & 0xf);
3654                 return -ENOSYS;
3655         }
3656
3657         if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
3658                 return 0;
3659
3660         ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
3661         if (ret)
3662                 return ret;
3663
3664         if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
3665                 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
3666                                                         lth.headerlen + 1;
3667                 bios->fp.xlatwidth = lth.recordlen;
3668         }
3669         if (bios->fp.fpxlatetableptr == 0x0) {
3670                 NV_ERROR(dev, "Pointer to flat panel xlat table invalid\n");
3671                 return -EINVAL;
3672         }
3673
3674         fpstrapping = get_fp_strap(dev, bios);
3675
3676         fpindex = bios->data[bios->fp.fpxlatetableptr +
3677                                         fpstrapping * bios->fp.xlatwidth];
3678
3679         if (fpindex > fpentries) {
3680                 NV_ERROR(dev, "Bad flat panel table index\n");
3681                 return -ENOENT;
3682         }
3683
3684         /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
3685         if (lth.lvds_ver > 0x10)
3686                 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
3687
3688         /*
3689          * If either the strap or xlated fpindex value are 0xf there is no
3690          * panel using a strap-derived bios mode present.  this condition
3691          * includes, but is different from, the DDC panel indicator above
3692          */
3693         if (fpstrapping == 0xf || fpindex == 0xf)
3694                 return 0;
3695
3696         bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
3697                             recordlen * fpindex + ofs;
3698
3699         NV_TRACE(dev, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
3700                  ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
3701                  ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
3702                  ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
3703
3704         return 0;
3705 }
3706
3707 bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
3708 {
3709         struct drm_nouveau_private *dev_priv = dev->dev_private;
3710         struct nvbios *bios = &dev_priv->vbios;
3711         uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
3712
3713         if (!mode)      /* just checking whether we can produce a mode */
3714                 return bios->fp.mode_ptr;
3715
3716         memset(mode, 0, sizeof(struct drm_display_mode));
3717         /*
3718          * For version 1.0 (version in byte 0):
3719          * bytes 1-2 are "panel type", including bits on whether Colour/mono,
3720          * single/dual link, and type (TFT etc.)
3721          * bytes 3-6 are bits per colour in RGBX
3722          */
3723         mode->clock = ROM16(mode_entry[7]) * 10;
3724         /* bytes 9-10 is HActive */
3725         mode->hdisplay = ROM16(mode_entry[11]) + 1;
3726         /*
3727          * bytes 13-14 is HValid Start
3728          * bytes 15-16 is HValid End
3729          */
3730         mode->hsync_start = ROM16(mode_entry[17]) + 1;
3731         mode->hsync_end = ROM16(mode_entry[19]) + 1;
3732         mode->htotal = ROM16(mode_entry[21]) + 1;
3733         /* bytes 23-24, 27-30 similarly, but vertical */
3734         mode->vdisplay = ROM16(mode_entry[25]) + 1;
3735         mode->vsync_start = ROM16(mode_entry[31]) + 1;
3736         mode->vsync_end = ROM16(mode_entry[33]) + 1;
3737         mode->vtotal = ROM16(mode_entry[35]) + 1;
3738         mode->flags |= (mode_entry[37] & 0x10) ?
3739                         DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
3740         mode->flags |= (mode_entry[37] & 0x1) ?
3741                         DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
3742         /*
3743          * bytes 38-39 relate to spread spectrum settings
3744          * bytes 40-43 are something to do with PWM
3745          */
3746
3747         mode->status = MODE_OK;
3748         mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
3749         drm_mode_set_name(mode);
3750         return bios->fp.mode_ptr;
3751 }
3752
3753 int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
3754 {
3755         /*
3756          * The LVDS table header is (mostly) described in
3757          * parse_lvds_manufacturer_table_header(): the BIT header additionally
3758          * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
3759          * straps are not being used for the panel, this specifies the frequency
3760          * at which modes should be set up in the dual link style.
3761          *
3762          * Following the header, the BMP (ver 0xa) table has several records,
3763          * indexed by a separate xlat table, indexed in turn by the fp strap in
3764          * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
3765          * numbers for use by INIT_SUB which controlled panel init and power,
3766          * and finally a dword of ms to sleep between power off and on
3767          * operations.
3768          *
3769          * In the BIT versions, the table following the header serves as an
3770          * integrated config and xlat table: the records in the table are
3771          * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
3772          * two bytes - the first as a config byte, the second for indexing the
3773          * fp mode table pointed to by the BIT 'D' table
3774          *
3775          * DDC is not used until after card init, so selecting the correct table
3776          * entry and setting the dual link flag for EDID equipped panels,
3777          * requiring tests against the native-mode pixel clock, cannot be done
3778          * until later, when this function should be called with non-zero pxclk
3779          */
3780         struct drm_nouveau_private *dev_priv = dev->dev_private;
3781         struct nvbios *bios = &dev_priv->vbios;
3782         int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
3783         struct lvdstableheader lth;
3784         uint16_t lvdsofs;
3785         int ret, chip_version = bios->chip_version;
3786
3787         ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
3788         if (ret)
3789                 return ret;
3790
3791         switch (lth.lvds_ver) {
3792         case 0x0a:      /* pre NV40 */
3793                 lvdsmanufacturerindex = bios->data[
3794                                         bios->fp.fpxlatemanufacturertableptr +
3795                                         fpstrapping];
3796
3797                 /* we're done if this isn't the EDID panel case */
3798                 if (!pxclk)
3799                         break;
3800
3801                 if (chip_version < 0x25) {
3802                         /* nv17 behaviour
3803                          *
3804                          * It seems the old style lvds script pointer is reused
3805                          * to select 18/24 bit colour depth for EDID panels.
3806                          */
3807                         lvdsmanufacturerindex =
3808                                 (bios->legacy.lvds_single_a_script_ptr & 1) ?
3809                                                                         2 : 0;
3810                         if (pxclk >= bios->fp.duallink_transition_clk)
3811                                 lvdsmanufacturerindex++;
3812                 } else if (chip_version < 0x30) {
3813                         /* nv28 behaviour (off-chip encoder)
3814                          *
3815                          * nv28 does a complex dance of first using byte 121 of
3816                          * the EDID to choose the lvdsmanufacturerindex, then
3817                          * later attempting to match the EDID manufacturer and
3818                          * product IDs in a table (signature 'pidt' (panel id
3819                          * table?)), setting an lvdsmanufacturerindex of 0 and
3820                          * an fp strap of the match index (or 0xf if none)
3821                          */
3822                         lvdsmanufacturerindex = 0;
3823                 } else {
3824                         /* nv31, nv34 behaviour */
3825                         lvdsmanufacturerindex = 0;
3826                         if (pxclk >= bios->fp.duallink_transition_clk)
3827                                 lvdsmanufacturerindex = 2;
3828                         if (pxclk >= 140000)
3829                                 lvdsmanufacturerindex = 3;
3830                 }
3831
3832                 /*
3833                  * nvidia set the high nibble of (cr57=f, cr58) to
3834                  * lvdsmanufacturerindex in this case; we don't
3835                  */
3836                 break;
3837         case 0x30:      /* NV4x */
3838         case 0x40:      /* G80/G90 */
3839                 lvdsmanufacturerindex = fpstrapping;
3840                 break;
3841         default:
3842                 NV_ERROR(dev, "LVDS table revision not currently supported\n");
3843                 return -ENOSYS;
3844         }
3845
3846         lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
3847         switch (lth.lvds_ver) {
3848         case 0x0a:
3849                 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
3850                 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
3851                 bios->fp.dual_link = bios->data[lvdsofs] & 4;
3852                 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
3853                 *if_is_24bit = bios->data[lvdsofs] & 16;
3854                 break;
3855         case 0x30:
3856         case 0x40:
3857                 /*
3858                  * No sign of the "power off for reset" or "reset for panel
3859                  * on" bits, but it's safer to assume we should
3860                  */
3861                 bios->fp.power_off_for_reset = true;
3862                 bios->fp.reset_after_pclk_change = true;
3863
3864                 /*
3865                  * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
3866                  * over-written, and if_is_24bit isn't used
3867                  */
3868                 bios->fp.dual_link = bios->data[lvdsofs] & 1;
3869                 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
3870                 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
3871                 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
3872                 break;
3873         }
3874
3875         /* Dell Latitude D620 reports a too-high value for the dual-link
3876          * transition freq, causing us to program the panel incorrectly.
3877          *
3878          * It doesn't appear the VBIOS actually uses its transition freq
3879          * (90000kHz), instead it uses the "Number of LVDS channels" field
3880          * out of the panel ID structure (http://www.spwg.org/).
3881          *
3882          * For the moment, a quirk will do :)
3883          */
3884         if ((dev->pdev->device == 0x01d7) &&
3885             (dev->pdev->subsystem_vendor == 0x1028) &&
3886             (dev->pdev->subsystem_device == 0x01c2)) {
3887                 bios->fp.duallink_transition_clk = 80000;
3888         }
3889
3890         /* set dual_link flag for EDID case */
3891         if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
3892                 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
3893
3894         *dl = bios->fp.dual_link;
3895
3896         return 0;
3897 }
3898
3899 static uint8_t *
3900 bios_output_config_match(struct drm_device *dev, struct dcb_entry *dcbent,
3901                          uint16_t record, int record_len, int record_nr)
3902 {
3903         struct drm_nouveau_private *dev_priv = dev->dev_private;
3904         struct nvbios *bios = &dev_priv->vbios;
3905         uint32_t entry;
3906         uint16_t table;
3907         int i, v;
3908
3909         for (i = 0; i < record_nr; i++, record += record_len) {
3910                 table = ROM16(bios->data[record]);
3911                 if (!table)
3912                         continue;
3913                 entry = ROM32(bios->data[table]);
3914
3915                 v = (entry & 0x000f0000) >> 16;
3916                 if (!(v & dcbent->or))
3917                         continue;
3918
3919                 v = (entry & 0x000000f0) >> 4;
3920                 if (v != dcbent->location)
3921                         continue;
3922
3923                 v = (entry & 0x0000000f);
3924                 if (v != dcbent->type)
3925                         continue;
3926
3927                 return &bios->data[table];
3928         }
3929
3930         return NULL;
3931 }
3932
3933 void *
3934 nouveau_bios_dp_table(struct drm_device *dev, struct dcb_entry *dcbent,
3935                       int *length)
3936 {
3937         struct drm_nouveau_private *dev_priv = dev->dev_private;
3938         struct nvbios *bios = &dev_priv->vbios;
3939         uint8_t *table;
3940
3941         if (!bios->display.dp_table_ptr) {
3942                 NV_ERROR(dev, "No pointer to DisplayPort table\n");
3943                 return NULL;
3944         }
3945         table = &bios->data[bios->display.dp_table_ptr];
3946
3947         if (table[0] != 0x20 && table[0] != 0x21) {
3948                 NV_ERROR(dev, "DisplayPort table version 0x%02x unknown\n",
3949                          table[0]);
3950                 return NULL;
3951         }
3952
3953         *length = table[4];
3954         return bios_output_config_match(dev, dcbent,
3955                                         bios->display.dp_table_ptr + table[1],
3956                                         table[2], table[3]);
3957 }
3958
3959 int
3960 nouveau_bios_run_display_table(struct drm_device *dev, struct dcb_entry *dcbent,
3961                                uint32_t sub, int pxclk)
3962 {
3963         /*
3964          * The display script table is located by the BIT 'U' table.
3965          *
3966          * It contains an array of pointers to various tables describing
3967          * a particular output type.  The first 32-bits of the output
3968          * tables contains similar information to a DCB entry, and is
3969          * used to decide whether that particular table is suitable for
3970          * the output you want to access.
3971          *
3972          * The "record header length" field here seems to indicate the
3973          * offset of the first configuration entry in the output tables.
3974          * This is 10 on most cards I've seen, but 12 has been witnessed
3975          * on DP cards, and there's another script pointer within the
3976          * header.
3977          *
3978          * offset + 0   ( 8 bits): version
3979          * offset + 1   ( 8 bits): header length
3980          * offset + 2   ( 8 bits): record length
3981          * offset + 3   ( 8 bits): number of records
3982          * offset + 4   ( 8 bits): record header length
3983          * offset + 5   (16 bits): pointer to first output script table
3984          */
3985
3986         struct drm_nouveau_private *dev_priv = dev->dev_private;
3987         struct nvbios *bios = &dev_priv->vbios;
3988         uint8_t *table = &bios->data[bios->display.script_table_ptr];
3989         uint8_t *otable = NULL;
3990         uint16_t script;
3991         int i = 0;
3992
3993         if (!bios->display.script_table_ptr) {
3994                 NV_ERROR(dev, "No pointer to output script table\n");
3995                 return 1;
3996         }
3997
3998         /*
3999          * Nothing useful has been in any of the pre-2.0 tables I've seen,
4000          * so until they are, we really don't need to care.
4001          */
4002         if (table[0] < 0x20)
4003                 return 1;
4004
4005         if (table[0] != 0x20 && table[0] != 0x21) {
4006                 NV_ERROR(dev, "Output script table version 0x%02x unknown\n",
4007                          table[0]);
4008                 return 1;
4009         }
4010
4011         /*
4012          * The output script tables describing a particular output type
4013          * look as follows:
4014          *
4015          * offset + 0   (32 bits): output this table matches (hash of DCB)
4016          * offset + 4   ( 8 bits): unknown
4017          * offset + 5   ( 8 bits): number of configurations
4018          * offset + 6   (16 bits): pointer to some script
4019          * offset + 8   (16 bits): pointer to some script
4020          *
4021          * headerlen == 10
4022          * offset + 10           : configuration 0
4023          *
4024          * headerlen == 12
4025          * offset + 10           : pointer to some script
4026          * offset + 12           : configuration 0
4027          *
4028          * Each config entry is as follows:
4029          *
4030          * offset + 0   (16 bits): unknown, assumed to be a match value
4031          * offset + 2   (16 bits): pointer to script table (clock set?)
4032          * offset + 4   (16 bits): pointer to script table (reset?)
4033          *
4034          * There doesn't appear to be a count value to say how many
4035          * entries exist in each script table, instead, a 0 value in
4036          * the first 16-bit word seems to indicate both the end of the
4037          * list and the default entry.  The second 16-bit word in the
4038          * script tables is a pointer to the script to execute.
4039          */
4040
4041         NV_DEBUG_KMS(dev, "Searching for output entry for %d %d %d\n",
4042                         dcbent->type, dcbent->location, dcbent->or);
4043         otable = bios_output_config_match(dev, dcbent, table[1] +
4044                                           bios->display.script_table_ptr,
4045                                           table[2], table[3]);
4046         if (!otable) {
4047                 NV_ERROR(dev, "Couldn't find matching output script table\n");
4048                 return 1;
4049         }
4050
4051         if (pxclk < -2 || pxclk > 0) {
4052                 /* Try to find matching script table entry */
4053                 for (i = 0; i < otable[5]; i++) {
4054                         if (ROM16(otable[table[4] + i*6]) == sub)
4055                                 break;
4056                 }
4057
4058                 if (i == otable[5]) {
4059                         NV_ERROR(dev, "Table 0x%04x not found for %d/%d, "
4060                                       "using first\n",
4061                                  sub, dcbent->type, dcbent->or);
4062                         i = 0;
4063                 }
4064         }
4065
4066         if (pxclk == 0) {
4067                 script = ROM16(otable[6]);
4068                 if (!script) {
4069                         NV_DEBUG_KMS(dev, "output script 0 not found\n");
4070                         return 1;
4071                 }
4072
4073                 NV_TRACE(dev, "0x%04X: parsing output script 0\n", script);
4074                 nouveau_bios_run_init_table(dev, script, dcbent);
4075         } else
4076         if (pxclk == -1) {
4077                 script = ROM16(otable[8]);
4078                 if (!script) {
4079                         NV_DEBUG_KMS(dev, "output script 1 not found\n");
4080                         return 1;
4081                 }
4082
4083                 NV_TRACE(dev, "0x%04X: parsing output script 1\n", script);
4084                 nouveau_bios_run_init_table(dev, script, dcbent);
4085         } else
4086         if (pxclk == -2) {
4087                 if (table[4] >= 12)
4088                         script = ROM16(otable[10]);
4089                 else
4090                         script = 0;
4091                 if (!script) {
4092                         NV_DEBUG_KMS(dev, "output script 2 not found\n");
4093                         return 1;
4094                 }
4095
4096                 NV_TRACE(dev, "0x%04X: parsing output script 2\n", script);
4097                 nouveau_bios_run_init_table(dev, script, dcbent);
4098         } else
4099         if (pxclk > 0) {
4100                 script = ROM16(otable[table[4] + i*6 + 2]);
4101                 if (script)
4102                         script = clkcmptable(bios, script, pxclk);
4103                 if (!script) {
4104                         NV_ERROR(dev, "clock script 0 not found\n");
4105                         return 1;
4106                 }
4107
4108                 NV_TRACE(dev, "0x%04X: parsing clock script 0\n", script);
4109                 nouveau_bios_run_init_table(dev, script, dcbent);
4110         } else
4111         if (pxclk < 0) {
4112                 script = ROM16(otable[table[4] + i*6 + 4]);
4113                 if (script)
4114                         script = clkcmptable(bios, script, -pxclk);
4115                 if (!script) {
4116                         NV_DEBUG_KMS(dev, "clock script 1 not found\n");
4117                         return 1;
4118                 }
4119
4120                 NV_TRACE(dev, "0x%04X: parsing clock script 1\n", script);
4121                 nouveau_bios_run_init_table(dev, script, dcbent);
4122         }
4123
4124         return 0;
4125 }
4126
4127
4128 int run_tmds_table(struct drm_device *dev, struct dcb_entry *dcbent, int head, int pxclk)
4129 {
4130         /*
4131          * the pxclk parameter is in kHz
4132          *
4133          * This runs the TMDS regs setting code found on BIT bios cards
4134          *
4135          * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
4136          * ffs(or) == 3, use the second.
4137          */
4138
4139         struct drm_nouveau_private *dev_priv = dev->dev_private;
4140         struct nvbios *bios = &dev_priv->vbios;
4141         int cv = bios->chip_version;
4142         uint16_t clktable = 0, scriptptr;
4143         uint32_t sel_clk_binding, sel_clk;
4144
4145         /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
4146         if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
4147             dcbent->location != DCB_LOC_ON_CHIP)
4148                 return 0;
4149
4150         switch (ffs(dcbent->or)) {
4151         case 1:
4152                 clktable = bios->tmds.output0_script_ptr;
4153                 break;
4154         case 2:
4155         case 3:
4156                 clktable = bios->tmds.output1_script_ptr;
4157                 break;
4158         }
4159
4160         if (!clktable) {
4161                 NV_ERROR(dev, "Pixel clock comparison table not found\n");
4162                 return -EINVAL;
4163         }
4164
4165         scriptptr = clkcmptable(bios, clktable, pxclk);
4166
4167         if (!scriptptr) {
4168                 NV_ERROR(dev, "TMDS output init script not found\n");
4169                 return -ENOENT;
4170         }
4171
4172         /* don't let script change pll->head binding */
4173         sel_clk_binding = bios_rd32(bios, NV_PRAMDAC_SEL_CLK) & 0x50000;
4174         run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
4175         sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
4176         NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
4177
4178         return 0;
4179 }
4180
4181 int get_pll_limits(struct drm_device *dev, uint32_t limit_match, struct pll_lims *pll_lim)
4182 {
4183         /*
4184          * PLL limits table
4185          *
4186          * Version 0x10: NV30, NV31
4187          * One byte header (version), one record of 24 bytes
4188          * Version 0x11: NV36 - Not implemented
4189          * Seems to have same record style as 0x10, but 3 records rather than 1
4190          * Version 0x20: Found on Geforce 6 cards
4191          * Trivial 4 byte BIT header. 31 (0x1f) byte record length
4192          * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
4193          * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
4194          * length in general, some (integrated) have an extra configuration byte
4195          * Version 0x30: Found on Geforce 8, separates the register mapping
4196          * from the limits tables.
4197          */
4198
4199         struct drm_nouveau_private *dev_priv = dev->dev_private;
4200         struct nvbios *bios = &dev_priv->vbios;
4201         int cv = bios->chip_version, pllindex = 0;
4202         uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
4203         uint32_t crystal_strap_mask, crystal_straps;
4204
4205         if (!bios->pll_limit_tbl_ptr) {
4206                 if (cv == 0x30 || cv == 0x31 || cv == 0x35 || cv == 0x36 ||
4207                     cv >= 0x40) {
4208                         NV_ERROR(dev, "Pointer to PLL limits table invalid\n");
4209                         return -EINVAL;
4210                 }
4211         } else
4212                 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
4213
4214         crystal_strap_mask = 1 << 6;
4215         /* open coded dev->twoHeads test */
4216         if (cv > 0x10 && cv != 0x15 && cv != 0x1a && cv != 0x20)
4217                 crystal_strap_mask |= 1 << 22;
4218         crystal_straps = nvReadEXTDEV(dev, NV_PEXTDEV_BOOT_0) &
4219                                                         crystal_strap_mask;
4220
4221         switch (pll_lim_ver) {
4222         /*
4223          * We use version 0 to indicate a pre limit table bios (single stage
4224          * pll) and load the hard coded limits instead.
4225          */
4226         case 0:
4227                 break;
4228         case 0x10:
4229         case 0x11:
4230                 /*
4231                  * Strictly v0x11 has 3 entries, but the last two don't seem
4232                  * to get used.
4233                  */
4234                 headerlen = 1;
4235                 recordlen = 0x18;
4236                 entries = 1;
4237                 pllindex = 0;
4238                 break;
4239         case 0x20:
4240         case 0x21:
4241         case 0x30:
4242         case 0x40:
4243                 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
4244                 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
4245                 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
4246                 break;
4247         default:
4248                 NV_ERROR(dev, "PLL limits table revision 0x%X not currently "
4249                                 "supported\n", pll_lim_ver);
4250                 return -ENOSYS;
4251         }
4252
4253         /* initialize all members to zero */
4254         memset(pll_lim, 0, sizeof(struct pll_lims));
4255
4256         if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
4257                 uint8_t *pll_rec = &bios->data[bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex];
4258
4259                 pll_lim->vco1.minfreq = ROM32(pll_rec[0]);
4260                 pll_lim->vco1.maxfreq = ROM32(pll_rec[4]);
4261                 pll_lim->vco2.minfreq = ROM32(pll_rec[8]);
4262                 pll_lim->vco2.maxfreq = ROM32(pll_rec[12]);
4263                 pll_lim->vco1.min_inputfreq = ROM32(pll_rec[16]);
4264                 pll_lim->vco2.min_inputfreq = ROM32(pll_rec[20]);
4265                 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
4266
4267                 /* these values taken from nv30/31/36 */
4268                 pll_lim->vco1.min_n = 0x1;
4269                 if (cv == 0x36)
4270                         pll_lim->vco1.min_n = 0x5;
4271                 pll_lim->vco1.max_n = 0xff;
4272                 pll_lim->vco1.min_m = 0x1;
4273                 pll_lim->vco1.max_m = 0xd;
4274                 pll_lim->vco2.min_n = 0x4;
4275                 /*
4276                  * On nv30, 31, 36 (i.e. all cards with two stage PLLs with this
4277                  * table version (apart from nv35)), N2 is compared to
4278                  * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
4279                  * save a comparison
4280                  */
4281                 pll_lim->vco2.max_n = 0x28;
4282                 if (cv == 0x30 || cv == 0x35)
4283                         /* only 5 bits available for N2 on nv30/35 */
4284                         pll_lim->vco2.max_n = 0x1f;
4285                 pll_lim->vco2.min_m = 0x1;
4286                 pll_lim->vco2.max_m = 0x4;
4287                 pll_lim->max_log2p = 0x7;
4288                 pll_lim->max_usable_log2p = 0x6;
4289         } else if (pll_lim_ver == 0x20 || pll_lim_ver == 0x21) {
4290                 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
4291                 uint32_t reg = 0; /* default match */
4292                 uint8_t *pll_rec;
4293                 int i;
4294
4295                 /*
4296                  * First entry is default match, if nothing better. warn if
4297                  * reg field nonzero
4298                  */
4299                 if (ROM32(bios->data[plloffs]))
4300                         NV_WARN(dev, "Default PLL limit entry has non-zero "
4301                                        "register field\n");
4302
4303                 if (limit_match > MAX_PLL_TYPES)
4304                         /* we've been passed a reg as the match */
4305                         reg = limit_match;
4306                 else /* limit match is a pll type */
4307                         for (i = 1; i < entries && !reg; i++) {
4308                                 uint32_t cmpreg = ROM32(bios->data[plloffs + recordlen * i]);
4309
4310                                 if (limit_match == NVPLL &&
4311                                     (cmpreg == NV_PRAMDAC_NVPLL_COEFF || cmpreg == 0x4000))
4312                                         reg = cmpreg;
4313                                 if (limit_match == MPLL &&
4314                                     (cmpreg == NV_PRAMDAC_MPLL_COEFF || cmpreg == 0x4020))
4315                                         reg = cmpreg;
4316                                 if (limit_match == VPLL1 &&
4317                                     (cmpreg == NV_PRAMDAC_VPLL_COEFF || cmpreg == 0x4010))
4318                                         reg = cmpreg;
4319                                 if (limit_match == VPLL2 &&
4320                                     (cmpreg == NV_RAMDAC_VPLL2 || cmpreg == 0x4018))
4321                                         reg = cmpreg;
4322                         }
4323
4324                 for (i = 1; i < entries; i++)
4325                         if (ROM32(bios->data[plloffs + recordlen * i]) == reg) {
4326                                 pllindex = i;
4327                                 break;
4328                         }
4329
4330                 pll_rec = &bios->data[plloffs + recordlen * pllindex];
4331
4332                 BIOSLOG(bios, "Loading PLL limits for reg 0x%08x\n",
4333                         pllindex ? reg : 0);
4334
4335                 /*
4336                  * Frequencies are stored in tables in MHz, kHz are more
4337                  * useful, so we convert.
4338                  */
4339
4340                 /* What output frequencies can each VCO generate? */
4341                 pll_lim->vco1.minfreq = ROM16(pll_rec[4]) * 1000;
4342                 pll_lim->vco1.maxfreq = ROM16(pll_rec[6]) * 1000;
4343                 pll_lim->vco2.minfreq = ROM16(pll_rec[8]) * 1000;
4344                 pll_lim->vco2.maxfreq = ROM16(pll_rec[10]) * 1000;
4345
4346                 /* What input frequencies they accept (past the m-divider)? */
4347                 pll_lim->vco1.min_inputfreq = ROM16(pll_rec[12]) * 1000;
4348                 pll_lim->vco2.min_inputfreq = ROM16(pll_rec[14]) * 1000;
4349                 pll_lim->vco1.max_inputfreq = ROM16(pll_rec[16]) * 1000;
4350                 pll_lim->vco2.max_inputfreq = ROM16(pll_rec[18]) * 1000;
4351
4352                 /* What values are accepted as multiplier and divider? */
4353                 pll_lim->vco1.min_n = pll_rec[20];
4354                 pll_lim->vco1.max_n = pll_rec[21];
4355                 pll_lim->vco1.min_m = pll_rec[22];
4356                 pll_lim->vco1.max_m = pll_rec[23];
4357                 pll_lim->vco2.min_n = pll_rec[24];
4358                 pll_lim->vco2.max_n = pll_rec[25];
4359                 pll_lim->vco2.min_m = pll_rec[26];
4360                 pll_lim->vco2.max_m = pll_rec[27];
4361
4362                 pll_lim->max_usable_log2p = pll_lim->max_log2p = pll_rec[29];
4363                 if (pll_lim->max_log2p > 0x7)
4364                         /* pll decoding in nv_hw.c assumes never > 7 */
4365                         NV_WARN(dev, "Max log2 P value greater than 7 (%d)\n",
4366                                 pll_lim->max_log2p);
4367                 if (cv < 0x60)
4368                         pll_lim->max_usable_log2p = 0x6;
4369                 pll_lim->log2p_bias = pll_rec[30];
4370
4371                 if (recordlen > 0x22)
4372                         pll_lim->refclk = ROM32(pll_rec[31]);
4373
4374                 if (recordlen > 0x23 && pll_rec[35])
4375                         NV_WARN(dev,
4376                                 "Bits set in PLL configuration byte (%x)\n",
4377                                 pll_rec[35]);
4378
4379                 /* C51 special not seen elsewhere */
4380                 if (cv == 0x51 && !pll_lim->refclk) {
4381                         uint32_t sel_clk = bios_rd32(bios, NV_PRAMDAC_SEL_CLK);
4382
4383                         if (((limit_match == NV_PRAMDAC_VPLL_COEFF || limit_match == VPLL1) && sel_clk & 0x20) ||
4384                             ((limit_match == NV_RAMDAC_VPLL2 || limit_match == VPLL2) && sel_clk & 0x80)) {
4385                                 if (bios_idxprt_rd(bios, NV_CIO_CRX__COLOR, NV_CIO_CRE_CHIP_ID_INDEX) < 0xa3)
4386                                         pll_lim->refclk = 200000;
4387                                 else
4388                                         pll_lim->refclk = 25000;
4389                         }
4390                 }
4391         } else if (pll_lim_ver == 0x30) { /* ver 0x30 */
4392                 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4393                 uint8_t *record = NULL;
4394                 int i;
4395
4396                 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
4397                         limit_match);
4398
4399                 for (i = 0; i < entries; i++, entry += recordlen) {
4400                         if (ROM32(entry[3]) == limit_match) {
4401                                 record = &bios->data[ROM16(entry[1])];
4402                                 break;
4403                         }
4404                 }
4405
4406                 if (!record) {
4407                         NV_ERROR(dev, "Register 0x%08x not found in PLL "
4408                                  "limits table", limit_match);
4409                         return -ENOENT;
4410                 }
4411
4412                 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4413                 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4414                 pll_lim->vco2.minfreq = ROM16(record[4]) * 1000;
4415                 pll_lim->vco2.maxfreq = ROM16(record[6]) * 1000;
4416                 pll_lim->vco1.min_inputfreq = ROM16(record[8]) * 1000;
4417                 pll_lim->vco2.min_inputfreq = ROM16(record[10]) * 1000;
4418                 pll_lim->vco1.max_inputfreq = ROM16(record[12]) * 1000;
4419                 pll_lim->vco2.max_inputfreq = ROM16(record[14]) * 1000;
4420                 pll_lim->vco1.min_n = record[16];
4421                 pll_lim->vco1.max_n = record[17];
4422                 pll_lim->vco1.min_m = record[18];
4423                 pll_lim->vco1.max_m = record[19];
4424                 pll_lim->vco2.min_n = record[20];
4425                 pll_lim->vco2.max_n = record[21];
4426                 pll_lim->vco2.min_m = record[22];
4427                 pll_lim->vco2.max_m = record[23];
4428                 pll_lim->max_usable_log2p = pll_lim->max_log2p = record[25];
4429                 pll_lim->log2p_bias = record[27];
4430                 pll_lim->refclk = ROM32(record[28]);
4431         } else if (pll_lim_ver) { /* ver 0x40 */
4432                 uint8_t *entry = &bios->data[bios->pll_limit_tbl_ptr + headerlen];
4433                 uint8_t *record = NULL;
4434                 int i;
4435
4436                 BIOSLOG(bios, "Loading PLL limits for register 0x%08x\n",
4437                         limit_match);
4438
4439                 for (i = 0; i < entries; i++, entry += recordlen) {
4440                         if (ROM32(entry[3]) == limit_match) {
4441                                 record = &bios->data[ROM16(entry[1])];
4442                                 break;
4443                         }
4444                 }
4445
4446                 if (!record) {
4447                         NV_ERROR(dev, "Register 0x%08x not found in PLL "
4448                                  "limits table", limit_match);
4449                         return -ENOENT;
4450                 }
4451
4452                 pll_lim->vco1.minfreq = ROM16(record[0]) * 1000;
4453                 pll_lim->vco1.maxfreq = ROM16(record[2]) * 1000;
4454                 pll_lim->vco1.min_inputfreq = ROM16(record[4]) * 1000;
4455                 pll_lim->vco1.max_inputfreq = ROM16(record[6]) * 1000;
4456                 pll_lim->vco1.min_m = record[8];
4457                 pll_lim->vco1.max_m = record[9];
4458                 pll_lim->vco1.min_n = record[10];
4459                 pll_lim->vco1.max_n = record[11];
4460                 pll_lim->min_p = record[12];
4461                 pll_lim->max_p = record[13];
4462                 /* where did this go to?? */
4463                 if (limit_match == 0x00614100 || limit_match == 0x00614900)
4464                         pll_lim->refclk = 27000;
4465                 else
4466                         pll_lim->refclk = 100000;
4467         }
4468
4469         /*
4470          * By now any valid limit table ought to have set a max frequency for
4471          * vco1, so if it's zero it's either a pre limit table bios, or one
4472          * with an empty limit table (seen on nv18)
4473          */
4474         if (!pll_lim->vco1.maxfreq) {
4475                 pll_lim->vco1.minfreq = bios->fminvco;
4476                 pll_lim->vco1.maxfreq = bios->fmaxvco;
4477                 pll_lim->vco1.min_inputfreq = 0;
4478                 pll_lim->vco1.max_inputfreq = INT_MAX;
4479                 pll_lim->vco1.min_n = 0x1;
4480                 pll_lim->vco1.max_n = 0xff;
4481                 pll_lim->vco1.min_m = 0x1;
4482                 if (crystal_straps == 0) {
4483                         /* nv05 does this, nv11 doesn't, nv10 unknown */
4484                         if (cv < 0x11)
4485                                 pll_lim->vco1.min_m = 0x7;
4486                         pll_lim->vco1.max_m = 0xd;
4487                 } else {
4488                         if (cv < 0x11)
4489                                 pll_lim->vco1.min_m = 0x8;
4490                         pll_lim->vco1.max_m = 0xe;
4491                 }
4492                 if (cv < 0x17 || cv == 0x1a || cv == 0x20)
4493                         pll_lim->max_log2p = 4;
4494                 else
4495                         pll_lim->max_log2p = 5;
4496                 pll_lim->max_usable_log2p = pll_lim->max_log2p;
4497         }
4498
4499         if (!pll_lim->refclk)
4500                 switch (crystal_straps) {
4501                 case 0:
4502                         pll_lim->refclk = 13500;
4503                         break;
4504                 case (1 << 6):
4505                         pll_lim->refclk = 14318;
4506                         break;
4507                 case (1 << 22):
4508                         pll_lim->refclk = 27000;
4509                         break;
4510                 case (1 << 22 | 1 << 6):
4511                         pll_lim->refclk = 25000;
4512                         break;
4513                 }
4514
4515         NV_DEBUG(dev, "pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
4516         NV_DEBUG(dev, "pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
4517         NV_DEBUG(dev, "pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
4518         NV_DEBUG(dev, "pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
4519         NV_DEBUG(dev, "pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
4520         NV_DEBUG(dev, "pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
4521         NV_DEBUG(dev, "pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
4522         NV_DEBUG(dev, "pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
4523         if (pll_lim->vco2.maxfreq) {
4524                 NV_DEBUG(dev, "pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
4525                 NV_DEBUG(dev, "pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
4526                 NV_DEBUG(dev, "pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
4527                 NV_DEBUG(dev, "pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
4528                 NV_DEBUG(dev, "pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
4529                 NV_DEBUG(dev, "pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
4530                 NV_DEBUG(dev, "pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
4531                 NV_DEBUG(dev, "pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
4532         }
4533         if (!pll_lim->max_p) {
4534                 NV_DEBUG(dev, "pll.max_log2p: %d\n", pll_lim->max_log2p);
4535                 NV_DEBUG(dev, "pll.log2p_bias: %d\n", pll_lim->log2p_bias);
4536         } else {
4537                 NV_DEBUG(dev, "pll.min_p: %d\n", pll_lim->min_p);
4538                 NV_DEBUG(dev, "pll.max_p: %d\n", pll_lim->max_p);
4539         }
4540         NV_DEBUG(dev, "pll.refclk: %d\n", pll_lim->refclk);
4541
4542         return 0;
4543 }
4544
4545 static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
4546 {
4547         /*
4548          * offset + 0  (8 bits): Micro version
4549          * offset + 1  (8 bits): Minor version
4550          * offset + 2  (8 bits): Chip version
4551          * offset + 3  (8 bits): Major version
4552          */
4553
4554         bios->major_version = bios->data[offset + 3];
4555         bios->chip_version = bios->data[offset + 2];
4556         NV_TRACE(dev, "Bios version %02x.%02x.%02x.%02x\n",
4557                  bios->data[offset + 3], bios->data[offset + 2],
4558                  bios->data[offset + 1], bios->data[offset]);
4559 }
4560
4561 static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
4562 {
4563         /*
4564          * Parses the init table segment for pointers used in script execution.
4565          *
4566          * offset + 0  (16 bits): init script tables pointer
4567          * offset + 2  (16 bits): macro index table pointer
4568          * offset + 4  (16 bits): macro table pointer
4569          * offset + 6  (16 bits): condition table pointer
4570          * offset + 8  (16 bits): io condition table pointer
4571          * offset + 10 (16 bits): io flag condition table pointer
4572          * offset + 12 (16 bits): init function table pointer
4573          */
4574
4575         bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
4576         bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
4577         bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
4578         bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
4579         bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
4580         bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
4581         bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
4582 }
4583
4584 static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4585 {
4586         /*
4587          * Parses the load detect values for g80 cards.
4588          *
4589          * offset + 0 (16 bits): loadval table pointer
4590          */
4591
4592         uint16_t load_table_ptr;
4593         uint8_t version, headerlen, entrylen, num_entries;
4594
4595         if (bitentry->length != 3) {
4596                 NV_ERROR(dev, "Do not understand BIT A table\n");
4597                 return -EINVAL;
4598         }
4599
4600         load_table_ptr = ROM16(bios->data[bitentry->offset]);
4601
4602         if (load_table_ptr == 0x0) {
4603                 NV_ERROR(dev, "Pointer to BIT loadval table invalid\n");
4604                 return -EINVAL;
4605         }
4606
4607         version = bios->data[load_table_ptr];
4608
4609         if (version != 0x10) {
4610                 NV_ERROR(dev, "BIT loadval table version %d.%d not supported\n",
4611                          version >> 4, version & 0xF);
4612                 return -ENOSYS;
4613         }
4614
4615         headerlen = bios->data[load_table_ptr + 1];
4616         entrylen = bios->data[load_table_ptr + 2];
4617         num_entries = bios->data[load_table_ptr + 3];
4618
4619         if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
4620                 NV_ERROR(dev, "Do not understand BIT loadval table\n");
4621                 return -EINVAL;
4622         }
4623
4624         /* First entry is normal dac, 2nd tv-out perhaps? */
4625         bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
4626
4627         return 0;
4628 }
4629
4630 static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4631 {
4632         /*
4633          * offset + 8  (16 bits): PLL limits table pointer
4634          *
4635          * There's more in here, but that's unknown.
4636          */
4637
4638         if (bitentry->length < 10) {
4639                 NV_ERROR(dev, "Do not understand BIT C table\n");
4640                 return -EINVAL;
4641         }
4642
4643         bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
4644
4645         return 0;
4646 }
4647
4648 static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4649 {
4650         /*
4651          * Parses the flat panel table segment that the bit entry points to.
4652          * Starting at bitentry->offset:
4653          *
4654          * offset + 0  (16 bits): ??? table pointer - seems to have 18 byte
4655          * records beginning with a freq.
4656          * offset + 2  (16 bits): mode table pointer
4657          */
4658
4659         if (bitentry->length != 4) {
4660                 NV_ERROR(dev, "Do not understand BIT display table\n");
4661                 return -EINVAL;
4662         }
4663
4664         bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
4665
4666         return 0;
4667 }
4668
4669 static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4670 {
4671         /*
4672          * Parses the init table segment that the bit entry points to.
4673          *
4674          * See parse_script_table_pointers for layout
4675          */
4676
4677         if (bitentry->length < 14) {
4678                 NV_ERROR(dev, "Do not understand init table\n");
4679                 return -EINVAL;
4680         }
4681
4682         parse_script_table_pointers(bios, bitentry->offset);
4683
4684         if (bitentry->length >= 16)
4685                 bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
4686         if (bitentry->length >= 18)
4687                 bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
4688
4689         return 0;
4690 }
4691
4692 static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4693 {
4694         /*
4695          * BIT 'i' (info?) table
4696          *
4697          * offset + 0  (32 bits): BIOS version dword (as in B table)
4698          * offset + 5  (8  bits): BIOS feature byte (same as for BMP?)
4699          * offset + 13 (16 bits): pointer to table containing DAC load
4700          * detection comparison values
4701          *
4702          * There's other things in the table, purpose unknown
4703          */
4704
4705         uint16_t daccmpoffset;
4706         uint8_t dacver, dacheaderlen;
4707
4708         if (bitentry->length < 6) {
4709                 NV_ERROR(dev, "BIT i table too short for needed information\n");
4710                 return -EINVAL;
4711         }
4712
4713         parse_bios_version(dev, bios, bitentry->offset);
4714
4715         /*
4716          * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
4717          * Quadro identity crisis), other bits possibly as for BMP feature byte
4718          */
4719         bios->feature_byte = bios->data[bitentry->offset + 5];
4720         bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
4721
4722         if (bitentry->length < 15) {
4723                 NV_WARN(dev, "BIT i table not long enough for DAC load "
4724                                "detection comparison table\n");
4725                 return -EINVAL;
4726         }
4727
4728         daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
4729
4730         /* doesn't exist on g80 */
4731         if (!daccmpoffset)
4732                 return 0;
4733
4734         /*
4735          * The first value in the table, following the header, is the
4736          * comparison value, the second entry is a comparison value for
4737          * TV load detection.
4738          */
4739
4740         dacver = bios->data[daccmpoffset];
4741         dacheaderlen = bios->data[daccmpoffset + 1];
4742
4743         if (dacver != 0x00 && dacver != 0x10) {
4744                 NV_WARN(dev, "DAC load detection comparison table version "
4745                                "%d.%d not known\n", dacver >> 4, dacver & 0xf);
4746                 return -ENOSYS;
4747         }
4748
4749         bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
4750         bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
4751
4752         return 0;
4753 }
4754
4755 static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4756 {
4757         /*
4758          * Parses the LVDS table segment that the bit entry points to.
4759          * Starting at bitentry->offset:
4760          *
4761          * offset + 0  (16 bits): LVDS strap xlate table pointer
4762          */
4763
4764         if (bitentry->length != 2) {
4765                 NV_ERROR(dev, "Do not understand BIT LVDS table\n");
4766                 return -EINVAL;
4767         }
4768
4769         /*
4770          * No idea if it's still called the LVDS manufacturer table, but
4771          * the concept's close enough.
4772          */
4773         bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
4774
4775         return 0;
4776 }
4777
4778 static int
4779 parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
4780                       struct bit_entry *bitentry)
4781 {
4782         /*
4783          * offset + 2  (8  bits): number of options in an
4784          *      INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
4785          * offset + 3  (16 bits): pointer to strap xlate table for RAM
4786          *      restrict option selection
4787          *
4788          * There's a bunch of bits in this table other than the RAM restrict
4789          * stuff that we don't use - their use currently unknown
4790          */
4791
4792         /*
4793          * Older bios versions don't have a sufficiently long table for
4794          * what we want
4795          */
4796         if (bitentry->length < 0x5)
4797                 return 0;
4798
4799         if (bitentry->id[1] < 2) {
4800                 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
4801                 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
4802         } else {
4803                 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
4804                 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
4805         }
4806
4807         return 0;
4808 }
4809
4810 static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
4811 {
4812         /*
4813          * Parses the pointer to the TMDS table
4814          *
4815          * Starting at bitentry->offset:
4816          *
4817          * offset + 0  (16 bits): TMDS table pointer
4818          *
4819          * The TMDS table is typically found just before the DCB table, with a
4820          * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
4821          * length?)
4822          *
4823          * At offset +7 is a pointer to a script, which I don't know how to
4824          * run yet.
4825          * At offset +9 is a pointer to another script, likewise
4826          * Offset +11 has a pointer to a table where the first word is a pxclk
4827          * frequency and the second word a pointer to a script, which should be
4828          * run if the comparison pxclk frequency is less than the pxclk desired.
4829          * This repeats for decreasing comparison frequencies
4830          * Offset +13 has a pointer to a similar table
4831          * The selection of table (and possibly +7/+9 script) is dictated by
4832          * "or" from the DCB.
4833          */
4834
4835         uint16_t tmdstableptr, script1, script2;
4836
4837         if (bitentry->length != 2) {
4838                 NV_ERROR(dev, "Do not understand BIT TMDS table\n");
4839                 return -EINVAL;
4840         }
4841
4842         tmdstableptr = ROM16(bios->data[bitentry->offset]);
4843
4844         if (tmdstableptr == 0x0) {
4845                 NV_ERROR(dev, "Pointer to TMDS table invalid\n");
4846                 return -EINVAL;
4847         }
4848
4849         /* nv50+ has v2.0, but we don't parse it atm */
4850         if (bios->data[tmdstableptr] != 0x11) {
4851                 NV_WARN(dev,
4852                         "TMDS table revision %d.%d not currently supported\n",
4853                         bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
4854                 return -ENOSYS;
4855         }
4856
4857         /*
4858          * These two scripts are odd: they don't seem to get run even when
4859          * they are not stubbed.
4860          */
4861         script1 = ROM16(bios->data[tmdstableptr + 7]);
4862         script2 = ROM16(bios->data[tmdstableptr + 9]);
4863         if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
4864                 NV_WARN(dev, "TMDS table script pointers not stubbed\n");
4865
4866         bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
4867         bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
4868
4869         return 0;
4870 }
4871
4872 static int
4873 parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
4874                       struct bit_entry *bitentry)
4875 {
4876         /*
4877          * Parses the pointer to the G80 output script tables
4878          *
4879          * Starting at bitentry->offset:
4880          *
4881          * offset + 0  (16 bits): output script table pointer
4882          */
4883
4884         uint16_t outputscripttableptr;
4885
4886         if (bitentry->length != 3) {
4887                 NV_ERROR(dev, "Do not understand BIT U table\n");
4888                 return -EINVAL;
4889         }
4890
4891         outputscripttableptr = ROM16(bios->data[bitentry->offset]);
4892         bios->display.script_table_ptr = outputscripttableptr;
4893         return 0;
4894 }
4895
4896 static int
4897 parse_bit_displayport_tbl_entry(struct drm_device *dev, struct nvbios *bios,
4898                                 struct bit_entry *bitentry)
4899 {
4900         bios->display.dp_table_ptr = ROM16(bios->data[bitentry->offset]);
4901         return 0;
4902 }
4903
4904 struct bit_table {
4905         const char id;
4906         int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
4907 };
4908
4909 #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
4910
4911 static int
4912 parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
4913                 struct bit_table *table)
4914 {
4915         struct drm_device *dev = bios->dev;
4916         uint8_t maxentries = bios->data[bitoffset + 4];
4917         int i, offset;
4918         struct bit_entry bitentry;
4919
4920         for (i = 0, offset = bitoffset + 6; i < maxentries; i++, offset += 6) {
4921                 bitentry.id[0] = bios->data[offset];
4922
4923                 if (bitentry.id[0] != table->id)
4924                         continue;
4925
4926                 bitentry.id[1] = bios->data[offset + 1];
4927                 bitentry.length = ROM16(bios->data[offset + 2]);
4928                 bitentry.offset = ROM16(bios->data[offset + 4]);
4929
4930                 return table->parse_fn(dev, bios, &bitentry);
4931         }
4932
4933         NV_INFO(dev, "BIT table '%c' not found\n", table->id);
4934         return -ENOSYS;
4935 }
4936
4937 static int
4938 parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
4939 {
4940         int ret;
4941
4942         /*
4943          * The only restriction on parsing order currently is having 'i' first
4944          * for use of bios->*_version or bios->feature_byte while parsing;
4945          * functions shouldn't be actually *doing* anything apart from pulling
4946          * data from the image into the bios struct, thus no interdependencies
4947          */
4948         ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
4949         if (ret) /* info? */
4950                 return ret;
4951         if (bios->major_version >= 0x60) /* g80+ */
4952                 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
4953         ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
4954         if (ret)
4955                 return ret;
4956         parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
4957         ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
4958         if (ret)
4959                 return ret;
4960         parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
4961         parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
4962         parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
4963         parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
4964         parse_bit_table(bios, bitoffset, &BIT_TABLE('d', displayport));
4965
4966         return 0;
4967 }
4968
4969 static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
4970 {
4971         /*
4972          * Parses the BMP structure for useful things, but does not act on them
4973          *
4974          * offset +   5: BMP major version
4975          * offset +   6: BMP minor version
4976          * offset +   9: BMP feature byte
4977          * offset +  10: BCD encoded BIOS version
4978          *
4979          * offset +  18: init script table pointer (for bios versions < 5.10h)
4980          * offset +  20: extra init script table pointer (for bios
4981          * versions < 5.10h)
4982          *
4983          * offset +  24: memory init table pointer (used on early bios versions)
4984          * offset +  26: SDR memory sequencing setup data table
4985          * offset +  28: DDR memory sequencing setup data table
4986          *
4987          * offset +  54: index of I2C CRTC pair to use for CRT output
4988          * offset +  55: index of I2C CRTC pair to use for TV output
4989          * offset +  56: index of I2C CRTC pair to use for flat panel output
4990          * offset +  58: write CRTC index for I2C pair 0
4991          * offset +  59: read CRTC index for I2C pair 0
4992          * offset +  60: write CRTC index for I2C pair 1
4993          * offset +  61: read CRTC index for I2C pair 1
4994          *
4995          * offset +  67: maximum internal PLL frequency (single stage PLL)
4996          * offset +  71: minimum internal PLL frequency (single stage PLL)
4997          *
4998          * offset +  75: script table pointers, as described in
4999          * parse_script_table_pointers
5000          *
5001          * offset +  89: TMDS single link output A table pointer
5002          * offset +  91: TMDS single link output B table pointer
5003          * offset +  95: LVDS single link output A table pointer
5004          * offset + 105: flat panel timings table pointer
5005          * offset + 107: flat panel strapping translation table pointer
5006          * offset + 117: LVDS manufacturer panel config table pointer
5007          * offset + 119: LVDS manufacturer strapping translation table pointer
5008          *
5009          * offset + 142: PLL limits table pointer
5010          *
5011          * offset + 156: minimum pixel clock for LVDS dual link
5012          */
5013
5014         uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
5015         uint16_t bmplength;
5016         uint16_t legacy_scripts_offset, legacy_i2c_offset;
5017
5018         /* load needed defaults in case we can't parse this info */
5019         bios->dcb.i2c[0].write = NV_CIO_CRE_DDC_WR__INDEX;
5020         bios->dcb.i2c[0].read = NV_CIO_CRE_DDC_STATUS__INDEX;
5021         bios->dcb.i2c[1].write = NV_CIO_CRE_DDC0_WR__INDEX;
5022         bios->dcb.i2c[1].read = NV_CIO_CRE_DDC0_STATUS__INDEX;
5023         bios->digital_min_front_porch = 0x4b;
5024         bios->fmaxvco = 256000;
5025         bios->fminvco = 128000;
5026         bios->fp.duallink_transition_clk = 90000;
5027
5028         bmp_version_major = bmp[5];
5029         bmp_version_minor = bmp[6];
5030
5031         NV_TRACE(dev, "BMP version %d.%d\n",
5032                  bmp_version_major, bmp_version_minor);
5033
5034         /*
5035          * Make sure that 0x36 is blank and can't be mistaken for a DCB
5036          * pointer on early versions
5037          */
5038         if (bmp_version_major < 5)
5039                 *(uint16_t *)&bios->data[0x36] = 0;
5040
5041         /*
5042          * Seems that the minor version was 1 for all major versions prior
5043          * to 5. Version 6 could theoretically exist, but I suspect BIT
5044          * happened instead.
5045          */
5046         if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
5047                 NV_ERROR(dev, "You have an unsupported BMP version. "
5048                                 "Please send in your bios\n");
5049                 return -ENOSYS;
5050         }
5051
5052         if (bmp_version_major == 0)
5053                 /* nothing that's currently useful in this version */
5054                 return 0;
5055         else if (bmp_version_major == 1)
5056                 bmplength = 44; /* exact for 1.01 */
5057         else if (bmp_version_major == 2)
5058                 bmplength = 48; /* exact for 2.01 */
5059         else if (bmp_version_major == 3)
5060                 bmplength = 54;
5061                 /* guessed - mem init tables added in this version */
5062         else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
5063                 /* don't know if 5.0 exists... */
5064                 bmplength = 62;
5065                 /* guessed - BMP I2C indices added in version 4*/
5066         else if (bmp_version_minor < 0x6)
5067                 bmplength = 67; /* exact for 5.01 */
5068         else if (bmp_version_minor < 0x10)
5069                 bmplength = 75; /* exact for 5.06 */
5070         else if (bmp_version_minor == 0x10)
5071                 bmplength = 89; /* exact for 5.10h */
5072         else if (bmp_version_minor < 0x14)
5073                 bmplength = 118; /* exact for 5.11h */
5074         else if (bmp_version_minor < 0x24)
5075                 /*
5076                  * Not sure of version where pll limits came in;
5077                  * certainly exist by 0x24 though.
5078                  */
5079                 /* length not exact: this is long enough to get lvds members */
5080                 bmplength = 123;
5081         else if (bmp_version_minor < 0x27)
5082                 /*
5083                  * Length not exact: this is long enough to get pll limit
5084                  * member
5085                  */
5086                 bmplength = 144;
5087         else
5088                 /*
5089                  * Length not exact: this is long enough to get dual link
5090                  * transition clock.
5091                  */
5092                 bmplength = 158;
5093
5094         /* checksum */
5095         if (nv_cksum(bmp, 8)) {
5096                 NV_ERROR(dev, "Bad BMP checksum\n");
5097                 return -EINVAL;
5098         }
5099
5100         /*
5101          * Bit 4 seems to indicate either a mobile bios or a quadro card --
5102          * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
5103          * (not nv10gl), bit 5 that the flat panel tables are present, and
5104          * bit 6 a tv bios.
5105          */
5106         bios->feature_byte = bmp[9];
5107
5108         parse_bios_version(dev, bios, offset + 10);
5109
5110         if (bmp_version_major < 5 || bmp_version_minor < 0x10)
5111                 bios->old_style_init = true;
5112         legacy_scripts_offset = 18;
5113         if (bmp_version_major < 2)
5114                 legacy_scripts_offset -= 4;
5115         bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
5116         bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
5117
5118         if (bmp_version_major > 2) {    /* appears in BMP 3 */
5119                 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
5120                 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
5121                 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
5122         }
5123
5124         legacy_i2c_offset = 0x48;       /* BMP version 2 & 3 */
5125         if (bmplength > 61)
5126                 legacy_i2c_offset = offset + 54;
5127         bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
5128         bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
5129         bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
5130         bios->dcb.i2c[0].write = bios->data[legacy_i2c_offset + 4];
5131         bios->dcb.i2c[0].read = bios->data[legacy_i2c_offset + 5];
5132         bios->dcb.i2c[1].write = bios->data[legacy_i2c_offset + 6];
5133         bios->dcb.i2c[1].read = bios->data[legacy_i2c_offset + 7];
5134
5135         if (bmplength > 74) {
5136                 bios->fmaxvco = ROM32(bmp[67]);
5137                 bios->fminvco = ROM32(bmp[71]);
5138         }
5139         if (bmplength > 88)
5140                 parse_script_table_pointers(bios, offset + 75);
5141         if (bmplength > 94) {
5142                 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
5143                 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
5144                 /*
5145                  * Never observed in use with lvds scripts, but is reused for
5146                  * 18/24 bit panel interface default for EDID equipped panels
5147                  * (if_is_24bit not set directly to avoid any oscillation).
5148                  */
5149                 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
5150         }
5151         if (bmplength > 108) {
5152                 bios->fp.fptablepointer = ROM16(bmp[105]);
5153                 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
5154                 bios->fp.xlatwidth = 1;
5155         }
5156         if (bmplength > 120) {
5157                 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
5158                 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
5159         }
5160         if (bmplength > 143)
5161                 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
5162
5163         if (bmplength > 157)
5164                 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
5165
5166         return 0;
5167 }
5168
5169 static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
5170 {
5171         int i, j;
5172
5173         for (i = 0; i <= (n - len); i++) {
5174                 for (j = 0; j < len; j++)
5175                         if (data[i + j] != str[j])
5176                                 break;
5177                 if (j == len)
5178                         return i;
5179         }
5180
5181         return 0;
5182 }
5183
5184 static struct dcb_gpio_entry *
5185 new_gpio_entry(struct nvbios *bios)
5186 {
5187         struct dcb_gpio_table *gpio = &bios->dcb.gpio;
5188
5189         return &gpio->entry[gpio->entries++];
5190 }
5191
5192 struct dcb_gpio_entry *
5193 nouveau_bios_gpio_entry(struct drm_device *dev, enum dcb_gpio_tag tag)
5194 {
5195         struct drm_nouveau_private *dev_priv = dev->dev_private;
5196         struct nvbios *bios = &dev_priv->vbios;
5197         int i;
5198
5199         for (i = 0; i < bios->dcb.gpio.entries; i++) {
5200                 if (bios->dcb.gpio.entry[i].tag != tag)
5201                         continue;
5202
5203                 return &bios->dcb.gpio.entry[i];
5204         }
5205
5206         return NULL;
5207 }
5208
5209 static void
5210 parse_dcb30_gpio_entry(struct nvbios *bios, uint16_t offset)
5211 {
5212         struct dcb_gpio_entry *gpio;
5213         uint16_t ent = ROM16(bios->data[offset]);
5214         uint8_t line = ent & 0x1f,
5215                 tag = ent >> 5 & 0x3f,
5216                 flags = ent >> 11 & 0x1f;
5217
5218         if (tag == 0x3f)
5219                 return;
5220
5221         gpio = new_gpio_entry(bios);
5222
5223         gpio->tag = tag;
5224         gpio->line = line;
5225         gpio->invert = flags != 4;
5226         gpio->entry = ent;
5227 }
5228
5229 static void
5230 parse_dcb40_gpio_entry(struct nvbios *bios, uint16_t offset)
5231 {
5232         uint32_t entry = ROM32(bios->data[offset]);
5233         struct dcb_gpio_entry *gpio;
5234
5235         if ((entry & 0x0000ff00) == 0x0000ff00)
5236                 return;
5237
5238         gpio = new_gpio_entry(bios);
5239         gpio->tag = (entry & 0x0000ff00) >> 8;
5240         gpio->line = (entry & 0x0000001f) >> 0;
5241         gpio->state_default = (entry & 0x01000000) >> 24;
5242         gpio->state[0] = (entry & 0x18000000) >> 27;
5243         gpio->state[1] = (entry & 0x60000000) >> 29;
5244         gpio->entry = entry;
5245 }
5246
5247 static void
5248 parse_dcb_gpio_table(struct nvbios *bios)
5249 {
5250         struct drm_device *dev = bios->dev;
5251         uint16_t gpio_table_ptr = bios->dcb.gpio_table_ptr;
5252         uint8_t *gpio_table = &bios->data[gpio_table_ptr];
5253         int header_len = gpio_table[1],
5254             entries = gpio_table[2],
5255             entry_len = gpio_table[3];
5256         void (*parse_entry)(struct nvbios *, uint16_t) = NULL;
5257         int i;
5258
5259         if (bios->dcb.version >= 0x40) {
5260                 if (gpio_table_ptr && entry_len != 4) {
5261                         NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
5262                         return;
5263                 }
5264
5265                 parse_entry = parse_dcb40_gpio_entry;
5266
5267         } else if (bios->dcb.version >= 0x30) {
5268                 if (gpio_table_ptr && entry_len != 2) {
5269                         NV_WARN(dev, "Invalid DCB GPIO table entry length.\n");
5270                         return;
5271                 }
5272
5273                 parse_entry = parse_dcb30_gpio_entry;
5274
5275         } else if (bios->dcb.version >= 0x22) {
5276                 /*
5277                  * DCBs older than v3.0 don't really have a GPIO
5278                  * table, instead they keep some GPIO info at fixed
5279                  * locations.
5280                  */
5281                 uint16_t dcbptr = ROM16(bios->data[0x36]);
5282                 uint8_t *tvdac_gpio = &bios->data[dcbptr - 5];
5283
5284                 if (tvdac_gpio[0] & 1) {
5285                         struct dcb_gpio_entry *gpio = new_gpio_entry(bios);
5286
5287                         gpio->tag = DCB_GPIO_TVDAC0;
5288                         gpio->line = tvdac_gpio[1] >> 4;
5289                         gpio->invert = tvdac_gpio[0] & 2;
5290                 }
5291         }
5292
5293         if (!gpio_table_ptr)
5294                 return;
5295
5296         if (entries > DCB_MAX_NUM_GPIO_ENTRIES) {
5297                 NV_WARN(dev, "Too many entries in the DCB GPIO table.\n");
5298                 entries = DCB_MAX_NUM_GPIO_ENTRIES;
5299         }
5300
5301         for (i = 0; i < entries; i++)
5302                 parse_entry(bios, gpio_table_ptr + header_len + entry_len * i);
5303 }
5304
5305 struct dcb_connector_table_entry *
5306 nouveau_bios_connector_entry(struct drm_device *dev, int index)
5307 {
5308         struct drm_nouveau_private *dev_priv = dev->dev_private;
5309         struct nvbios *bios = &dev_priv->vbios;
5310         struct dcb_connector_table_entry *cte;
5311
5312         if (index >= bios->dcb.connector.entries)
5313                 return NULL;
5314
5315         cte = &bios->dcb.connector.entry[index];
5316         if (cte->type == 0xff)
5317                 return NULL;
5318
5319         return cte;
5320 }
5321
5322 static enum dcb_connector_type
5323 divine_connector_type(struct nvbios *bios, int index)
5324 {
5325         struct dcb_table *dcb = &bios->dcb;
5326         unsigned encoders = 0, type = DCB_CONNECTOR_NONE;
5327         int i;
5328
5329         for (i = 0; i < dcb->entries; i++) {
5330                 if (dcb->entry[i].connector == index)
5331                         encoders |= (1 << dcb->entry[i].type);
5332         }
5333
5334         if (encoders & (1 << OUTPUT_DP)) {
5335                 if (encoders & (1 << OUTPUT_TMDS))
5336                         type = DCB_CONNECTOR_DP;
5337                 else
5338                         type = DCB_CONNECTOR_eDP;
5339         } else
5340         if (encoders & (1 << OUTPUT_TMDS)) {
5341                 if (encoders & (1 << OUTPUT_ANALOG))
5342                         type = DCB_CONNECTOR_DVI_I;
5343                 else
5344                         type = DCB_CONNECTOR_DVI_D;
5345         } else
5346         if (encoders & (1 << OUTPUT_ANALOG)) {
5347                 type = DCB_CONNECTOR_VGA;
5348         } else
5349         if (encoders & (1 << OUTPUT_LVDS)) {
5350                 type = DCB_CONNECTOR_LVDS;
5351         } else
5352         if (encoders & (1 << OUTPUT_TV)) {
5353                 type = DCB_CONNECTOR_TV_0;
5354         }
5355
5356         return type;
5357 }
5358
5359 static void
5360 apply_dcb_connector_quirks(struct nvbios *bios, int idx)
5361 {
5362         struct dcb_connector_table_entry *cte = &bios->dcb.connector.entry[idx];
5363         struct drm_device *dev = bios->dev;
5364
5365         /* Gigabyte NX85T */
5366         if ((dev->pdev->device == 0x0421) &&
5367             (dev->pdev->subsystem_vendor == 0x1458) &&
5368             (dev->pdev->subsystem_device == 0x344c)) {
5369                 if (cte->type == DCB_CONNECTOR_HDMI_1)
5370                         cte->type = DCB_CONNECTOR_DVI_I;
5371         }
5372 }
5373
5374 static void
5375 parse_dcb_connector_table(struct nvbios *bios)
5376 {
5377         struct drm_device *dev = bios->dev;
5378         struct dcb_connector_table *ct = &bios->dcb.connector;
5379         struct dcb_connector_table_entry *cte;
5380         uint8_t *conntab = &bios->data[bios->dcb.connector_table_ptr];
5381         uint8_t *entry;
5382         int i;
5383
5384         if (!bios->dcb.connector_table_ptr) {
5385                 NV_DEBUG_KMS(dev, "No DCB connector table present\n");
5386                 return;
5387         }
5388
5389         NV_INFO(dev, "DCB connector table: VHER 0x%02x %d %d %d\n",
5390                 conntab[0], conntab[1], conntab[2], conntab[3]);
5391         if ((conntab[0] != 0x30 && conntab[0] != 0x40) ||
5392             (conntab[3] != 2 && conntab[3] != 4)) {
5393                 NV_ERROR(dev, "  Unknown!  Please report.\n");
5394                 return;
5395         }
5396
5397         ct->entries = conntab[2];
5398
5399         entry = conntab + conntab[1];
5400         cte = &ct->entry[0];
5401         for (i = 0; i < conntab[2]; i++, entry += conntab[3], cte++) {
5402                 cte->index = i;
5403                 if (conntab[3] == 2)
5404                         cte->entry = ROM16(entry[0]);
5405                 else
5406                         cte->entry = ROM32(entry[0]);
5407
5408                 cte->type  = (cte->entry & 0x000000ff) >> 0;
5409                 cte->index2 = (cte->entry & 0x00000f00) >> 8;
5410                 switch (cte->entry & 0x00033000) {
5411                 case 0x00001000:
5412                         cte->gpio_tag = 0x07;
5413                         break;
5414                 case 0x00002000:
5415                         cte->gpio_tag = 0x08;
5416                         break;
5417                 case 0x00010000:
5418                         cte->gpio_tag = 0x51;
5419                         break;
5420                 case 0x00020000:
5421                         cte->gpio_tag = 0x52;
5422                         break;
5423                 default:
5424                         cte->gpio_tag = 0xff;
5425                         break;
5426                 }
5427
5428                 if (cte->type == 0xff)
5429                         continue;
5430
5431                 apply_dcb_connector_quirks(bios, i);
5432
5433                 NV_INFO(dev, "  %d: 0x%08x: type 0x%02x idx %d tag 0x%02x\n",
5434                         i, cte->entry, cte->type, cte->index, cte->gpio_tag);
5435
5436                 /* check for known types, fallback to guessing the type
5437                  * from attached encoders if we hit an unknown.
5438                  */
5439                 switch (cte->type) {
5440                 case DCB_CONNECTOR_VGA:
5441                 case DCB_CONNECTOR_TV_0:
5442                 case DCB_CONNECTOR_TV_1:
5443                 case DCB_CONNECTOR_TV_3:
5444                 case DCB_CONNECTOR_DVI_I:
5445                 case DCB_CONNECTOR_DVI_D:
5446                 case DCB_CONNECTOR_LVDS:
5447                 case DCB_CONNECTOR_DP:
5448                 case DCB_CONNECTOR_eDP:
5449                 case DCB_CONNECTOR_HDMI_0:
5450                 case DCB_CONNECTOR_HDMI_1:
5451                         break;
5452                 default:
5453                         cte->type = divine_connector_type(bios, cte->index);
5454                         NV_WARN(dev, "unknown type, using 0x%02x\n", cte->type);
5455                         break;
5456                 }
5457
5458                 if (nouveau_override_conntype) {
5459                         int type = divine_connector_type(bios, cte->index);
5460                         if (type != cte->type)
5461                                 NV_WARN(dev, " -> type 0x%02x\n", cte->type);
5462                 }
5463
5464         }
5465 }
5466
5467 static struct dcb_entry *new_dcb_entry(struct dcb_table *dcb)
5468 {
5469         struct dcb_entry *entry = &dcb->entry[dcb->entries];
5470
5471         memset(entry, 0, sizeof(struct dcb_entry));
5472         entry->index = dcb->entries++;
5473
5474         return entry;
5475 }
5476
5477 static void fabricate_vga_output(struct dcb_table *dcb, int i2c, int heads)
5478 {
5479         struct dcb_entry *entry = new_dcb_entry(dcb);
5480
5481         entry->type = 0;
5482         entry->i2c_index = i2c;
5483         entry->heads = heads;
5484         entry->location = DCB_LOC_ON_CHIP;
5485         /* "or" mostly unused in early gen crt modesetting, 0 is fine */
5486 }
5487
5488 static void fabricate_dvi_i_output(struct dcb_table *dcb, bool twoHeads)
5489 {
5490         struct dcb_entry *entry = new_dcb_entry(dcb);
5491
5492         entry->type = 2;
5493         entry->i2c_index = LEGACY_I2C_PANEL;
5494         entry->heads = twoHeads ? 3 : 1;
5495         entry->location = !DCB_LOC_ON_CHIP;     /* ie OFF CHIP */
5496         entry->or = 1;  /* means |0x10 gets set on CRE_LCD__INDEX */
5497         entry->duallink_possible = false; /* SiI164 and co. are single link */
5498
5499 #if 0
5500         /*
5501          * For dvi-a either crtc probably works, but my card appears to only
5502          * support dvi-d.  "nvidia" still attempts to program it for dvi-a,
5503          * doing the full fp output setup (program 0x6808.. fp dimension regs,
5504          * setting 0x680848 to 0x10000111 to enable, maybe setting 0x680880);
5505          * the monitor picks up the mode res ok and lights up, but no pixel
5506          * data appears, so the board manufacturer probably connected up the
5507          * sync lines, but missed the video traces / components
5508          *
5509          * with this introduction, dvi-a left as an exercise for the reader.
5510          */
5511         fabricate_vga_output(dcb, LEGACY_I2C_PANEL, entry->heads);
5512 #endif
5513 }
5514
5515 static void fabricate_tv_output(struct dcb_table *dcb, bool twoHeads)
5516 {
5517         struct dcb_entry *entry = new_dcb_entry(dcb);
5518
5519         entry->type = 1;
5520         entry->i2c_index = LEGACY_I2C_TV;
5521         entry->heads = twoHeads ? 3 : 1;
5522         entry->location = !DCB_LOC_ON_CHIP;     /* ie OFF CHIP */
5523 }
5524
5525 static bool
5526 parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
5527                   uint32_t conn, uint32_t conf, struct dcb_entry *entry)
5528 {
5529         entry->type = conn & 0xf;
5530         entry->i2c_index = (conn >> 4) & 0xf;
5531         entry->heads = (conn >> 8) & 0xf;
5532         if (dcb->version >= 0x40)
5533                 entry->connector = (conn >> 12) & 0xf;
5534         entry->bus = (conn >> 16) & 0xf;
5535         entry->location = (conn >> 20) & 0x3;
5536         entry->or = (conn >> 24) & 0xf;
5537         /*
5538          * Normal entries consist of a single bit, but dual link has the
5539          * next most significant bit set too
5540          */
5541         entry->duallink_possible =
5542                         ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
5543
5544         switch (entry->type) {
5545         case OUTPUT_ANALOG:
5546                 /*
5547                  * Although the rest of a CRT conf dword is usually
5548                  * zeros, mac biosen have stuff there so we must mask
5549                  */
5550                 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
5551                                          (conf & 0xffff) * 10 :
5552                                          (conf & 0xff) * 10000;
5553                 break;
5554         case OUTPUT_LVDS:
5555                 {
5556                 uint32_t mask;
5557                 if (conf & 0x1)
5558                         entry->lvdsconf.use_straps_for_mode = true;
5559                 if (dcb->version < 0x22) {
5560                         mask = ~0xd;
5561                         /*
5562                          * The laptop in bug 14567 lies and claims to not use
5563                          * straps when it does, so assume all DCB 2.0 laptops
5564                          * use straps, until a broken EDID using one is produced
5565                          */
5566                         entry->lvdsconf.use_straps_for_mode = true;
5567                         /*
5568                          * Both 0x4 and 0x8 show up in v2.0 tables; assume they
5569                          * mean the same thing (probably wrong, but might work)
5570                          */
5571                         if (conf & 0x4 || conf & 0x8)
5572                                 entry->lvdsconf.use_power_scripts = true;
5573                 } else {
5574                         mask = ~0x5;
5575                         if (conf & 0x4)
5576                                 entry->lvdsconf.use_power_scripts = true;
5577                 }
5578                 if (conf & mask) {
5579                         /*
5580                          * Until we even try to use these on G8x, it's
5581                          * useless reporting unknown bits.  They all are.
5582                          */
5583                         if (dcb->version >= 0x40)
5584                                 break;
5585
5586                         NV_ERROR(dev, "Unknown LVDS configuration bits, "
5587                                       "please report\n");
5588                 }
5589                 break;
5590                 }
5591         case OUTPUT_TV:
5592         {
5593                 if (dcb->version >= 0x30)
5594                         entry->tvconf.has_component_output = conf & (0x8 << 4);
5595                 else
5596                         entry->tvconf.has_component_output = false;
5597
5598                 break;
5599         }
5600         case OUTPUT_DP:
5601                 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
5602                 entry->dpconf.link_bw = (conf & 0x00e00000) >> 21;
5603                 switch ((conf & 0x0f000000) >> 24) {
5604                 case 0xf:
5605                         entry->dpconf.link_nr = 4;
5606                         break;
5607                 case 0x3:
5608                         entry->dpconf.link_nr = 2;
5609                         break;
5610                 default:
5611                         entry->dpconf.link_nr = 1;
5612                         break;
5613                 }
5614                 break;
5615         case OUTPUT_TMDS:
5616                 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
5617                 break;
5618         case 0xe:
5619                 /* weird g80 mobile type that "nv" treats as a terminator */
5620                 dcb->entries--;
5621                 return false;
5622         default:
5623                 break;
5624         }
5625
5626         /* unsure what DCB version introduces this, 3.0? */
5627         if (conf & 0x100000)
5628                 entry->i2c_upper_default = true;
5629
5630         return true;
5631 }
5632
5633 static bool
5634 parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
5635                   uint32_t conn, uint32_t conf, struct dcb_entry *entry)
5636 {
5637         switch (conn & 0x0000000f) {
5638         case 0:
5639                 entry->type = OUTPUT_ANALOG;
5640                 break;
5641         case 1:
5642                 entry->type = OUTPUT_TV;
5643                 break;
5644         case 2:
5645         case 3:
5646                 entry->type = OUTPUT_LVDS;
5647                 break;
5648         case 4:
5649                 switch ((conn & 0x000000f0) >> 4) {
5650                 case 0:
5651                         entry->type = OUTPUT_TMDS;
5652                         break;
5653                 case 1:
5654                         entry->type = OUTPUT_LVDS;
5655                         break;
5656                 default:
5657                         NV_ERROR(dev, "Unknown DCB subtype 4/%d\n",
5658                                  (conn & 0x000000f0) >> 4);
5659                         return false;
5660                 }
5661                 break;
5662         default:
5663                 NV_ERROR(dev, "Unknown DCB type %d\n", conn & 0x0000000f);
5664                 return false;
5665         }
5666
5667         entry->i2c_index = (conn & 0x0003c000) >> 14;
5668         entry->heads = ((conn & 0x001c0000) >> 18) + 1;
5669         entry->or = entry->heads; /* same as heads, hopefully safe enough */
5670         entry->location = (conn & 0x01e00000) >> 21;
5671         entry->bus = (conn & 0x0e000000) >> 25;
5672         entry->duallink_possible = false;
5673
5674         switch (entry->type) {
5675         case OUTPUT_ANALOG:
5676                 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
5677                 break;
5678         case OUTPUT_TV:
5679                 entry->tvconf.has_component_output = false;
5680                 break;
5681         case OUTPUT_TMDS:
5682                 /*
5683                  * Invent a DVI-A output, by copying the fields of the DVI-D
5684                  * output; reported to work by math_b on an NV20(!).
5685                  */
5686                 fabricate_vga_output(dcb, entry->i2c_index, entry->heads);
5687                 break;
5688         case OUTPUT_LVDS:
5689                 if ((conn & 0x00003f00) != 0x10)
5690                         entry->lvdsconf.use_straps_for_mode = true;
5691                 entry->lvdsconf.use_power_scripts = true;
5692                 break;
5693         default:
5694                 break;
5695         }
5696
5697         return true;
5698 }
5699
5700 static bool parse_dcb_entry(struct drm_device *dev, struct dcb_table *dcb,
5701                             uint32_t conn, uint32_t conf)
5702 {
5703         struct dcb_entry *entry = new_dcb_entry(dcb);
5704         bool ret;
5705
5706         if (dcb->version >= 0x20)
5707                 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
5708         else
5709                 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
5710         if (!ret)
5711                 return ret;
5712
5713         read_dcb_i2c_entry(dev, dcb->version, dcb->i2c_table,
5714                            entry->i2c_index, &dcb->i2c[entry->i2c_index]);
5715
5716         return true;
5717 }
5718
5719 static
5720 void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
5721 {
5722         /*
5723          * DCB v2.0 lists each output combination separately.
5724          * Here we merge compatible entries to have fewer outputs, with
5725          * more options
5726          */
5727
5728         int i, newentries = 0;
5729
5730         for (i = 0; i < dcb->entries; i++) {
5731                 struct dcb_entry *ient = &dcb->entry[i];
5732                 int j;
5733
5734                 for (j = i + 1; j < dcb->entries; j++) {
5735                         struct dcb_entry *jent = &dcb->entry[j];
5736
5737                         if (jent->type == 100) /* already merged entry */
5738                                 continue;
5739
5740                         /* merge heads field when all other fields the same */
5741                         if (jent->i2c_index == ient->i2c_index &&
5742                             jent->type == ient->type &&
5743                             jent->location == ient->location &&
5744                             jent->or == ient->or) {
5745                                 NV_TRACE(dev, "Merging DCB entries %d and %d\n",
5746                                          i, j);
5747                                 ient->heads |= jent->heads;
5748                                 jent->type = 100; /* dummy value */
5749                         }
5750                 }
5751         }
5752
5753         /* Compact entries merged into others out of dcb */
5754         for (i = 0; i < dcb->entries; i++) {
5755                 if (dcb->entry[i].type == 100)
5756                         continue;
5757
5758                 if (newentries != i) {
5759                         dcb->entry[newentries] = dcb->entry[i];
5760                         dcb->entry[newentries].index = newentries;
5761                 }
5762                 newentries++;
5763         }
5764
5765         dcb->entries = newentries;
5766 }
5767
5768 static int
5769 parse_dcb_table(struct drm_device *dev, struct nvbios *bios, bool twoHeads)
5770 {
5771         struct drm_nouveau_private *dev_priv = dev->dev_private;
5772         struct dcb_table *dcb = &bios->dcb;
5773         uint16_t dcbptr = 0, i2ctabptr = 0;
5774         uint8_t *dcbtable;
5775         uint8_t headerlen = 0x4, entries = DCB_MAX_NUM_ENTRIES;
5776         bool configblock = true;
5777         int recordlength = 8, confofs = 4;
5778         int i;
5779
5780         /* get the offset from 0x36 */
5781         if (dev_priv->card_type > NV_04) {
5782                 dcbptr = ROM16(bios->data[0x36]);
5783                 if (dcbptr == 0x0000)
5784                         NV_WARN(dev, "No output data (DCB) found in BIOS\n");
5785         }
5786
5787         /* this situation likely means a really old card, pre DCB */
5788         if (dcbptr == 0x0) {
5789                 NV_INFO(dev, "Assuming a CRT output exists\n");
5790                 fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
5791
5792                 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
5793                         fabricate_tv_output(dcb, twoHeads);
5794
5795                 return 0;
5796         }
5797
5798         dcbtable = &bios->data[dcbptr];
5799
5800         /* get DCB version */
5801         dcb->version = dcbtable[0];
5802         NV_TRACE(dev, "Found Display Configuration Block version %d.%d\n",
5803                  dcb->version >> 4, dcb->version & 0xf);
5804
5805         if (dcb->version >= 0x20) { /* NV17+ */
5806                 uint32_t sig;
5807
5808                 if (dcb->version >= 0x30) { /* NV40+ */
5809                         headerlen = dcbtable[1];
5810                         entries = dcbtable[2];
5811                         recordlength = dcbtable[3];
5812                         i2ctabptr = ROM16(dcbtable[4]);
5813                         sig = ROM32(dcbtable[6]);
5814                         dcb->gpio_table_ptr = ROM16(dcbtable[10]);
5815                         dcb->connector_table_ptr = ROM16(dcbtable[20]);
5816                 } else {
5817                         i2ctabptr = ROM16(dcbtable[2]);
5818                         sig = ROM32(dcbtable[4]);
5819                         headerlen = 8;
5820                 }
5821
5822                 if (sig != 0x4edcbdcb) {
5823                         NV_ERROR(dev, "Bad Display Configuration Block "
5824                                         "signature (%08X)\n", sig);
5825                         return -EINVAL;
5826                 }
5827         } else if (dcb->version >= 0x15) { /* some NV11 and NV20 */
5828                 char sig[8] = { 0 };
5829
5830                 strncpy(sig, (char *)&dcbtable[-7], 7);
5831                 i2ctabptr = ROM16(dcbtable[2]);
5832                 recordlength = 10;
5833                 confofs = 6;
5834
5835                 if (strcmp(sig, "DEV_REC")) {
5836                         NV_ERROR(dev, "Bad Display Configuration Block "
5837                                         "signature (%s)\n", sig);
5838                         return -EINVAL;
5839                 }
5840         } else {
5841                 /*
5842                  * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but always
5843                  * has the same single (crt) entry, even when tv-out present, so
5844                  * the conclusion is this version cannot really be used.
5845                  * v1.2 tables (some NV6/10, and NV15+) normally have the same
5846                  * 5 entries, which are not specific to the card and so no use.
5847                  * v1.2 does have an I2C table that read_dcb_i2c_table can
5848                  * handle, but cards exist (nv11 in #14821) with a bad i2c table
5849                  * pointer, so use the indices parsed in parse_bmp_structure.
5850                  * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
5851                  */
5852                 NV_TRACEWARN(dev, "No useful information in BIOS output table; "
5853                                   "adding all possible outputs\n");
5854                 fabricate_vga_output(dcb, LEGACY_I2C_CRT, 1);
5855
5856                 /*
5857                  * Attempt to detect TV before DVI because the test
5858                  * for the former is more accurate and it rules the
5859                  * latter out.
5860                  */
5861                 if (nv04_tv_identify(dev,
5862                                      bios->legacy.i2c_indices.tv) >= 0)
5863                         fabricate_tv_output(dcb, twoHeads);
5864
5865                 else if (bios->tmds.output0_script_ptr ||
5866                          bios->tmds.output1_script_ptr)
5867                         fabricate_dvi_i_output(dcb, twoHeads);
5868
5869                 return 0;
5870         }
5871
5872         if (!i2ctabptr)
5873                 NV_WARN(dev, "No pointer to DCB I2C port table\n");
5874         else {
5875                 dcb->i2c_table = &bios->data[i2ctabptr];
5876                 if (dcb->version >= 0x30)
5877                         dcb->i2c_default_indices = dcb->i2c_table[4];
5878         }
5879
5880         if (entries > DCB_MAX_NUM_ENTRIES)
5881                 entries = DCB_MAX_NUM_ENTRIES;
5882
5883         for (i = 0; i < entries; i++) {
5884                 uint32_t connection, config = 0;
5885
5886                 connection = ROM32(dcbtable[headerlen + recordlength * i]);
5887                 if (configblock)
5888                         config = ROM32(dcbtable[headerlen + confofs + recordlength * i]);
5889
5890                 /* seen on an NV11 with DCB v1.5 */
5891                 if (connection == 0x00000000)
5892                         break;
5893
5894                 /* seen on an NV17 with DCB v2.0 */
5895                 if (connection == 0xffffffff)
5896                         break;
5897
5898                 if ((connection & 0x0000000f) == 0x0000000f)
5899                         continue;
5900
5901                 NV_TRACEWARN(dev, "Raw DCB entry %d: %08x %08x\n",
5902                              dcb->entries, connection, config);
5903
5904                 if (!parse_dcb_entry(dev, dcb, connection, config))
5905                         break;
5906         }
5907
5908         /*
5909          * apart for v2.1+ not being known for requiring merging, this
5910          * guarantees dcbent->index is the index of the entry in the rom image
5911          */
5912         if (dcb->version < 0x21)
5913                 merge_like_dcb_entries(dev, dcb);
5914
5915         if (!dcb->entries)
5916                 return -ENXIO;
5917
5918         parse_dcb_gpio_table(bios);
5919         parse_dcb_connector_table(bios);
5920         return 0;
5921 }
5922
5923 static void
5924 fixup_legacy_connector(struct nvbios *bios)
5925 {
5926         struct dcb_table *dcb = &bios->dcb;
5927         int i, i2c, i2c_conn[DCB_MAX_NUM_I2C_ENTRIES] = { };
5928
5929         /*
5930          * DCB 3.0 also has the table in most cases, but there are some cards
5931          * where the table is filled with stub entries, and the DCB entriy
5932          * indices are all 0.  We don't need the connector indices on pre-G80
5933          * chips (yet?) so limit the use to DCB 4.0 and above.
5934          */
5935         if (dcb->version >= 0x40)
5936                 return;
5937
5938         dcb->connector.entries = 0;
5939
5940         /*
5941          * No known connector info before v3.0, so make it up.  the rule here
5942          * is: anything on the same i2c bus is considered to be on the same
5943          * connector.  any output without an associated i2c bus is assigned
5944          * its own unique connector index.
5945          */
5946         for (i = 0; i < dcb->entries; i++) {
5947                 /*
5948                  * Ignore the I2C index for on-chip TV-out, as there
5949                  * are cards with bogus values (nv31m in bug 23212),
5950                  * and it's otherwise useless.
5951                  */
5952                 if (dcb->entry[i].type == OUTPUT_TV &&
5953                     dcb->entry[i].location == DCB_LOC_ON_CHIP)
5954                         dcb->entry[i].i2c_index = 0xf;
5955                 i2c = dcb->entry[i].i2c_index;
5956
5957                 if (i2c_conn[i2c]) {
5958                         dcb->entry[i].connector = i2c_conn[i2c] - 1;
5959                         continue;
5960                 }
5961
5962                 dcb->entry[i].connector = dcb->connector.entries++;
5963                 if (i2c != 0xf)
5964                         i2c_conn[i2c] = dcb->connector.entries;
5965         }
5966
5967         /* Fake the connector table as well as just connector indices */
5968         for (i = 0; i < dcb->connector.entries; i++) {
5969                 dcb->connector.entry[i].index = i;
5970                 dcb->connector.entry[i].type = divine_connector_type(bios, i);
5971                 dcb->connector.entry[i].gpio_tag = 0xff;
5972         }
5973 }
5974
5975 static void
5976 fixup_legacy_i2c(struct nvbios *bios)
5977 {
5978         struct dcb_table *dcb = &bios->dcb;
5979         int i;
5980
5981         for (i = 0; i < dcb->entries; i++) {
5982                 if (dcb->entry[i].i2c_index == LEGACY_I2C_CRT)
5983                         dcb->entry[i].i2c_index = bios->legacy.i2c_indices.crt;
5984                 if (dcb->entry[i].i2c_index == LEGACY_I2C_PANEL)
5985                         dcb->entry[i].i2c_index = bios->legacy.i2c_indices.panel;
5986                 if (dcb->entry[i].i2c_index == LEGACY_I2C_TV)
5987                         dcb->entry[i].i2c_index = bios->legacy.i2c_indices.tv;
5988         }
5989 }
5990
5991 static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
5992 {
5993         /*
5994          * The header following the "HWSQ" signature has the number of entries,
5995          * and the entry size
5996          *
5997          * An entry consists of a dword to write to the sequencer control reg
5998          * (0x00001304), followed by the ucode bytes, written sequentially,
5999          * starting at reg 0x00001400
6000          */
6001
6002         uint8_t bytes_to_write;
6003         uint16_t hwsq_entry_offset;
6004         int i;
6005
6006         if (bios->data[hwsq_offset] <= entry) {
6007                 NV_ERROR(dev, "Too few entries in HW sequencer table for "
6008                                 "requested entry\n");
6009                 return -ENOENT;
6010         }
6011
6012         bytes_to_write = bios->data[hwsq_offset + 1];
6013
6014         if (bytes_to_write != 36) {
6015                 NV_ERROR(dev, "Unknown HW sequencer entry size\n");
6016                 return -EINVAL;
6017         }
6018
6019         NV_TRACE(dev, "Loading NV17 power sequencing microcode\n");
6020
6021         hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
6022
6023         /* set sequencer control */
6024         bios_wr32(bios, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
6025         bytes_to_write -= 4;
6026
6027         /* write ucode */
6028         for (i = 0; i < bytes_to_write; i += 4)
6029                 bios_wr32(bios, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
6030
6031         /* twiddle NV_PBUS_DEBUG_4 */
6032         bios_wr32(bios, NV_PBUS_DEBUG_4, bios_rd32(bios, NV_PBUS_DEBUG_4) | 0x18);
6033
6034         return 0;
6035 }
6036
6037 static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
6038                                         struct nvbios *bios)
6039 {
6040         /*
6041          * BMP based cards, from NV17, need a microcode loading to correctly
6042          * control the GPIO etc for LVDS panels
6043          *
6044          * BIT based cards seem to do this directly in the init scripts
6045          *
6046          * The microcode entries are found by the "HWSQ" signature.
6047          */
6048
6049         const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
6050         const int sz = sizeof(hwsq_signature);
6051         int hwsq_offset;
6052
6053         hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
6054         if (!hwsq_offset)
6055                 return 0;
6056
6057         /* always use entry 0? */
6058         return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
6059 }
6060
6061 uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
6062 {
6063         struct drm_nouveau_private *dev_priv = dev->dev_private;
6064         struct nvbios *bios = &dev_priv->vbios;
6065         const uint8_t edid_sig[] = {
6066                         0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
6067         uint16_t offset = 0;
6068         uint16_t newoffset;
6069         int searchlen = NV_PROM_SIZE;
6070
6071         if (bios->fp.edid)
6072                 return bios->fp.edid;
6073
6074         while (searchlen) {
6075                 newoffset = findstr(&bios->data[offset], searchlen,
6076                                                                 edid_sig, 8);
6077                 if (!newoffset)
6078                         return NULL;
6079                 offset += newoffset;
6080                 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
6081                         break;
6082
6083                 searchlen -= offset;
6084                 offset++;
6085         }
6086
6087         NV_TRACE(dev, "Found EDID in BIOS\n");
6088
6089         return bios->fp.edid = &bios->data[offset];
6090 }
6091
6092 void
6093 nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
6094                             struct dcb_entry *dcbent)
6095 {
6096         struct drm_nouveau_private *dev_priv = dev->dev_private;
6097         struct nvbios *bios = &dev_priv->vbios;
6098         struct init_exec iexec = { true, false };
6099
6100         mutex_lock(&bios->lock);
6101         bios->display.output = dcbent;
6102         parse_init_table(bios, table, &iexec);
6103         bios->display.output = NULL;
6104         mutex_unlock(&bios->lock);
6105 }
6106
6107 static bool NVInitVBIOS(struct drm_device *dev)
6108 {
6109         struct drm_nouveau_private *dev_priv = dev->dev_private;
6110         struct nvbios *bios = &dev_priv->vbios;
6111
6112         memset(bios, 0, sizeof(struct nvbios));
6113         mutex_init(&bios->lock);
6114         bios->dev = dev;
6115
6116         if (!NVShadowVBIOS(dev, bios->data))
6117                 return false;
6118
6119         bios->length = NV_PROM_SIZE;
6120         return true;
6121 }
6122
6123 static int nouveau_parse_vbios_struct(struct drm_device *dev)
6124 {
6125         struct drm_nouveau_private *dev_priv = dev->dev_private;
6126         struct nvbios *bios = &dev_priv->vbios;
6127         const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
6128         const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
6129         int offset;
6130
6131         offset = findstr(bios->data, bios->length,
6132                                         bit_signature, sizeof(bit_signature));
6133         if (offset) {
6134                 NV_TRACE(dev, "BIT BIOS found\n");
6135                 return parse_bit_structure(bios, offset + 6);
6136         }
6137
6138         offset = findstr(bios->data, bios->length,
6139                                         bmp_signature, sizeof(bmp_signature));
6140         if (offset) {
6141                 NV_TRACE(dev, "BMP BIOS found\n");
6142                 return parse_bmp_structure(dev, bios, offset);
6143         }
6144
6145         NV_ERROR(dev, "No known BIOS signature found\n");
6146         return -ENODEV;
6147 }
6148
6149 int
6150 nouveau_run_vbios_init(struct drm_device *dev)
6151 {
6152         struct drm_nouveau_private *dev_priv = dev->dev_private;
6153         struct nvbios *bios = &dev_priv->vbios;
6154         int i, ret = 0;
6155
6156         NVLockVgaCrtcs(dev, false);
6157         if (nv_two_heads(dev))
6158                 NVSetOwner(dev, bios->state.crtchead);
6159
6160         if (bios->major_version < 5)    /* BMP only */
6161                 load_nv17_hw_sequencer_ucode(dev, bios);
6162
6163         if (bios->execute) {
6164                 bios->fp.last_script_invoc = 0;
6165                 bios->fp.lvds_init_run = false;
6166         }
6167
6168         parse_init_tables(bios);
6169
6170         /*
6171          * Runs some additional script seen on G8x VBIOSen.  The VBIOS'
6172          * parser will run this right after the init tables, the binary
6173          * driver appears to run it at some point later.
6174          */
6175         if (bios->some_script_ptr) {
6176                 struct init_exec iexec = {true, false};
6177
6178                 NV_INFO(dev, "Parsing VBIOS init table at offset 0x%04X\n",
6179                         bios->some_script_ptr);
6180                 parse_init_table(bios, bios->some_script_ptr, &iexec);
6181         }
6182
6183         if (dev_priv->card_type >= NV_50) {
6184                 for (i = 0; i < bios->dcb.entries; i++) {
6185                         nouveau_bios_run_display_table(dev,
6186                                                        &bios->dcb.entry[i],
6187                                                        0, 0);
6188                 }
6189         }
6190
6191         NVLockVgaCrtcs(dev, true);
6192
6193         return ret;
6194 }
6195
6196 static void
6197 nouveau_bios_i2c_devices_takedown(struct drm_device *dev)
6198 {
6199         struct drm_nouveau_private *dev_priv = dev->dev_private;
6200         struct nvbios *bios = &dev_priv->vbios;
6201         struct dcb_i2c_entry *entry;
6202         int i;
6203
6204         entry = &bios->dcb.i2c[0];
6205         for (i = 0; i < DCB_MAX_NUM_I2C_ENTRIES; i++, entry++)
6206                 nouveau_i2c_fini(dev, entry);
6207 }
6208
6209 int
6210 nouveau_bios_init(struct drm_device *dev)
6211 {
6212         struct drm_nouveau_private *dev_priv = dev->dev_private;
6213         struct nvbios *bios = &dev_priv->vbios;
6214         uint32_t saved_nv_pextdev_boot_0;
6215         bool was_locked;
6216         int ret;
6217
6218         if (!NVInitVBIOS(dev))
6219                 return -ENODEV;
6220
6221         ret = nouveau_parse_vbios_struct(dev);
6222         if (ret)
6223                 return ret;
6224
6225         ret = parse_dcb_table(dev, bios, nv_two_heads(dev));
6226         if (ret)
6227                 return ret;
6228
6229         fixup_legacy_i2c(bios);
6230         fixup_legacy_connector(bios);
6231
6232         if (!bios->major_version)       /* we don't run version 0 bios */
6233                 return 0;
6234
6235         /* these will need remembering across a suspend */
6236         saved_nv_pextdev_boot_0 = bios_rd32(bios, NV_PEXTDEV_BOOT_0);
6237         bios->state.saved_nv_pfb_cfg0 = bios_rd32(bios, NV_PFB_CFG0);
6238
6239         /* init script execution disabled */
6240         bios->execute = false;
6241
6242         /* ... unless card isn't POSTed already */
6243         if (dev_priv->card_type >= NV_10 &&
6244             NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
6245             NVReadVgaCrtc(dev, 0, 0x1a) == 0) {
6246                 NV_INFO(dev, "Adaptor not initialised\n");
6247                 if (dev_priv->card_type < NV_50) {
6248                         NV_ERROR(dev, "Unable to POST this chipset\n");
6249                         return -ENODEV;
6250                 }
6251
6252                 NV_INFO(dev, "Running VBIOS init tables\n");
6253                 bios->execute = true;
6254         }
6255
6256         bios_wr32(bios, NV_PEXTDEV_BOOT_0, saved_nv_pextdev_boot_0);
6257
6258         ret = nouveau_run_vbios_init(dev);
6259         if (ret)
6260                 return ret;
6261
6262         /* feature_byte on BMP is poor, but init always sets CR4B */
6263         was_locked = NVLockVgaCrtcs(dev, false);
6264         if (bios->major_version < 5)
6265                 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
6266
6267         /* all BIT systems need p_f_m_t for digital_min_front_porch */
6268         if (bios->is_mobile || bios->major_version >= 5)
6269                 ret = parse_fp_mode_table(dev, bios);
6270         NVLockVgaCrtcs(dev, was_locked);
6271
6272         /* allow subsequent scripts to execute */
6273         bios->execute = true;
6274
6275         return 0;
6276 }
6277
6278 void
6279 nouveau_bios_takedown(struct drm_device *dev)
6280 {
6281         nouveau_bios_i2c_devices_takedown(dev);
6282 }