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