randr12: detypedef private structs and use a define to get to the privates
[nouveau] / src / nv_bios.c
1 /*
2  * Copyright 2005-2006 Erik Waling
3  * Copyright 2006 Stephane Marchesin
4  * Copyright 2007-2008 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 "nv_include.h"
26
27
28 #if defined(__FreeBSD__) || defined(__NetBSD__)
29 #define bswap_16 bswap16
30 #define bswap_32 bswap32
31 #else
32 #include <byteswap.h>
33 #endif
34
35
36 /* FIXME: put these somewhere */
37 #define SEQ_INDEX VGA_SEQ_INDEX
38 #define NV_VGA_CRTCX_OWNER_HEADA 0x0
39 #define NV_VGA_CRTCX_OWNER_HEADB 0x3
40 #define FEATURE_MOBILE 0x10
41
42 #define DEBUGLEVEL 6
43
44 static int crtchead = 0;
45
46 /* this will need remembering across a suspend */
47 static uint32_t saved_nv_pfb_cfg0;
48
49 typedef struct {
50         bool execute;
51         bool repeat;
52 } init_exec_t;
53
54 static uint16_t le16_to_cpu(const uint16_t x)
55 {
56 #if X_BYTE_ORDER == X_BIG_ENDIAN
57         return bswap_16(x);
58 #else
59         return x;
60 #endif
61 }
62
63 static uint32_t le32_to_cpu(const uint32_t x)
64 {
65 #if X_BYTE_ORDER == X_BIG_ENDIAN
66         return bswap_32(x);
67 #else
68         return x;
69 #endif
70 }
71
72 static bool nv_cksum(const uint8_t *data, unsigned int length)
73 {
74         /* there's a few checksums in the BIOS, so here's a generic checking function */
75         int i;
76         uint8_t sum = 0;
77
78         for (i = 0; i < length; i++)
79                 sum += data[i];
80
81         if (sum)
82                 return true;
83
84         return false;
85 }
86
87 static int score_vbios(ScrnInfoPtr pScrn, const uint8_t *data)
88 {
89         /* check for BIOS signature */
90         if (!(data[0] == 0x55 && data[1] == 0xAA)) {
91                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
92                            "... BIOS signature not found\n");
93                 return 0;
94         }
95
96         if (nv_cksum(data, data[2] * 512)) {
97                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
98                            "... BIOS checksum invalid\n");
99                 /* probably ought to set a do_not_execute flag for table parsing here,
100                  * assuming most BIOSen are valid */
101                 return 1;
102         } else
103                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "... appears to be valid\n");
104
105         return 2;
106 }
107
108 static void load_vbios_prom(NVPtr pNv, uint8_t *data)
109 {
110         int i;
111
112         /* enable ROM access */
113         nvWriteMC(pNv, NV_PBUS_PCI_NV_20, NV_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED);
114         for (i = 0; i < NV_PROM_SIZE; i++) {
115                 /* according to nvclock, we need that to work around a 6600GT/6800LE bug */
116                 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
117                 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
118                 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
119                 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
120                 data[i] = NV_RD08(pNv->REGS, NV_PROM_OFFSET + i);
121         }
122         /* disable ROM access */
123         nvWriteMC(pNv, NV_PBUS_PCI_NV_20, NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
124 }
125
126 static void load_vbios_pramin(NVPtr pNv, uint8_t *data)
127 {
128         uint32_t old_bar0_pramin = 0;
129         int i;
130
131         if (pNv->Architecture >= NV_ARCH_50) {
132                 uint32_t vbios_vram = (NV_RD32(pNv->REGS, 0x619f04) & ~0xff) << 8;
133
134                 if (!vbios_vram)
135                         vbios_vram = (NV_RD32(pNv->REGS, 0x1700) << 16) + 0xf0000;
136
137                 old_bar0_pramin = NV_RD32(pNv->REGS, 0x1700);
138                 NV_WR32(pNv->REGS, 0x1700, vbios_vram >> 16);
139         }
140
141         for (i = 0; i < NV_PROM_SIZE; i++)
142                 data[i] = NV_RD08(pNv->REGS, NV_PRAMIN_OFFSET + i);
143
144         if (pNv->Architecture >= NV_ARCH_50)
145                 NV_WR32(pNv->REGS, 0x1700, old_bar0_pramin);
146 }
147
148 static void load_vbios_pci(NVPtr pNv, uint8_t *data)
149 {
150 #if XSERVER_LIBPCIACCESS
151         pci_device_read_rom(pNv->PciInfo, data);
152 #else
153         xf86ReadPciBIOS(0, pNv->PciTag, 0, data, NV_PROM_SIZE);
154 #endif
155 }
156
157 static bool NVShadowVBIOS(ScrnInfoPtr pScrn, uint8_t *data)
158 {
159         NVPtr pNv = NVPTR(pScrn);
160         struct methods {
161                 const char desc[8];
162                 void (*loadbios)(NVPtr, uint8_t *);
163                 int score;
164         } method[] = {
165                 { "PROM", load_vbios_prom },
166                 { "PRAMIN", load_vbios_pramin },
167 #ifndef __powerpc__
168                 { "PCI ROM", load_vbios_pci }
169 #endif
170         };
171         int i;
172
173         for (i = 0; i < sizeof(method) / sizeof(struct methods); i++) {
174                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
175                            "Attempting to load BIOS image from %s\n", method[i].desc);
176                 method[i].loadbios(pNv, data);
177                 if ((method[i].score = score_vbios(pScrn, data)) == 2)
178                         return true;
179         }
180
181         for (i = 0; i < sizeof(method) / sizeof(struct methods); i++)
182                 if (method[i].score == 1) {
183                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
184                                    "Using BIOS image from %s\n", method[i].desc);
185                         method[i].loadbios(pNv, data);
186                         return true;
187                 }
188
189         return false;
190 }
191
192 typedef struct {
193         char* name;
194         uint8_t id;
195         int length;
196         int length_offset;
197         int length_multiplier;
198         bool (*handler)(ScrnInfoPtr pScrn, bios_t *, uint16_t, init_exec_t *);
199 } init_tbl_entry_t;
200
201 typedef struct {
202         uint8_t id[2];
203         uint16_t length;
204         uint16_t offset;
205 } bit_entry_t;
206
207 static void parse_init_table(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset, init_exec_t *iexec);
208
209 #define MACRO_INDEX_SIZE        2
210 #define MACRO_SIZE              8
211 #define CONDITION_SIZE          12
212 #define IO_FLAG_CONDITION_SIZE  9
213 #define MEM_INIT_SIZE           66
214
215 static void still_alive(void)
216 {
217 //      sync();
218 //      usleep(2000);
219 }
220
221 static int nv_valid_reg(ScrnInfoPtr pScrn, uint32_t reg)
222 {
223         NVPtr pNv = NVPTR(pScrn);
224
225         /* C51 has misaligned regs on purpose. Marvellous */
226         if ((reg & 0x3 && pNv->VBIOS.chip_version != 0x51) ||
227                         (reg & 0x2 && pNv->VBIOS.chip_version == 0x51)) {
228                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
229                            "========== misaligned reg 0x%08X ==========\n", reg);
230                 return 0;
231         }
232
233         #define WITHIN(x,y,z) ((x>=y)&&(x<=y+z))
234         if (WITHIN(reg,NV_PMC_OFFSET,NV_PMC_SIZE))
235                 return 1;
236         if (WITHIN(reg,NV_PBUS_OFFSET,NV_PBUS_SIZE))
237                 return 1;
238         if (WITHIN(reg,NV_PFIFO_OFFSET,NV_PFIFO_SIZE))
239                 return 1;
240         if (pNv->VBIOS.chip_version >= 0x80 && WITHIN(reg, NV50_DISPLAY_OFFSET, NV50_DISPLAY_SIZE))
241                 return 1;
242         /* maybe a little large, but it will do for the moment. */
243         if (pNv->VBIOS.chip_version >= 0x80 && WITHIN(reg, 0x1000, 0xEFFF))
244                 return 1;
245         if (pNv->VBIOS.chip_version >= 0x30 && WITHIN(reg,0x4000,0x600))
246                 return 1;
247         if (pNv->VBIOS.chip_version >= 0x40 && WITHIN(reg,0xc000,0x48))
248                 return 1;
249         if (pNv->VBIOS.chip_version >= 0x17 && reg == 0x0000d204)
250                 return 1;
251         if (pNv->VBIOS.chip_version >= 0x40) {
252                 if (reg == 0x00011014 || reg == 0x00020328)
253                         return 1;
254                 if (WITHIN(reg,0x88000,NV_PBUS_SIZE)) /* new PBUS */
255                         return 1;
256         }
257         if (WITHIN(reg,NV_PFB_OFFSET,NV_PFB_SIZE))
258                 return 1;
259         if (WITHIN(reg,NV_PEXTDEV_OFFSET,NV_PEXTDEV_SIZE))
260                 return 1;
261         if (WITHIN(reg,NV_PCRTC0_OFFSET,NV_PCRTC0_SIZE * 2))
262                 return 1;
263         if (WITHIN(reg,NV_PRAMDAC0_OFFSET,NV_PRAMDAC0_SIZE * 2))
264                 return 1;
265         if (pNv->VBIOS.chip_version >= 0x17 && reg == 0x0070fff0)
266                 return 1;
267         if (pNv->VBIOS.chip_version == 0x51 && WITHIN(reg,NV_PRAMIN_OFFSET,NV_PRAMIN_SIZE))
268                 return 1;
269         #undef WITHIN
270
271         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
272                    "========== unknown reg 0x%08X ==========\n", reg);
273
274         return 0;
275 }
276
277 static bool nv_valid_idx_port(ScrnInfoPtr pScrn, uint16_t port)
278 {
279         /* if adding more ports here, the read/write functions below will need
280          * updating so that the correct mmio range (PCIO, PDIO, PVIO) is used
281          * for the port in question
282          */
283         if (port == CRTC_INDEX_COLOR)
284                 return true;
285         if (port == SEQ_INDEX)
286                 return true;
287
288         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
289                    "========== unknown indexed io port 0x%04X ==========\n", port);
290
291         return false;
292 }
293
294 static bool nv_valid_port(ScrnInfoPtr pScrn, uint16_t port)
295 {
296         /* if adding more ports here, the read/write functions below will need
297          * updating so that the correct mmio range (PCIO, PDIO, PVIO) is used
298          * for the port in question
299          */
300         if (port == VGA_ENABLE)
301                 return true;
302
303         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
304                    "========== unknown io port 0x%04X ==========\n", port);
305
306         return false;
307 }
308
309 static uint32_t nv32_rd(ScrnInfoPtr pScrn, uint32_t reg)
310 {
311         NVPtr pNv = NVPTR(pScrn);
312         uint32_t data;
313
314         if (!nv_valid_reg(pScrn, reg))
315                 return 0;
316
317         /* C51 sometimes uses regs with bit0 set in the address. For these
318          * cases there should exist a translation in a BIOS table to an IO
319          * port address which the BIOS uses for accessing the reg
320          *
321          * These only seem to appear for the power control regs to a flat panel
322          * and in C51 mmio traces the normal regs for 0x1308 and 0x1310 are
323          * used - hence the mask below. An S3 suspend-resume mmio trace from a
324          * C51 will be required to see if this is true for the power microcode
325          * in 0x14.., or whether the direct IO port access method is needed
326          */
327         if (reg & 0x1)
328                 reg &= ~0x1;
329
330         data = NV_RD32(pNv->REGS, reg);
331
332         if (DEBUGLEVEL >= 6)
333                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
334                            "    Read:  Reg: 0x%08X, Data: 0x%08X\n", reg, data);
335
336         return data;
337 }
338
339 static void nv32_wr(ScrnInfoPtr pScrn, uint32_t reg, uint32_t data)
340 {
341         NVPtr pNv = NVPTR(pScrn);
342
343         if (!nv_valid_reg(pScrn, reg))
344                 return;
345
346         /* see note in nv32_rd */
347         if (reg & 0x1)
348                 reg &= 0xfffffffe;
349
350         if (DEBUGLEVEL >= 8)
351                 nv32_rd(pScrn, reg);
352         if (DEBUGLEVEL >= 6)
353                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
354                            "    Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
355
356         if (pNv->VBIOS.execute) {
357                 still_alive();
358                 NV_WR32(pNv->REGS, reg, data);
359         }
360 }
361
362 static uint8_t nv_idx_port_rd(ScrnInfoPtr pScrn, uint16_t port, uint8_t index)
363 {
364         NVPtr pNv = NVPTR(pScrn);
365         uint8_t data;
366
367         if (!nv_valid_idx_port(pScrn, port))
368                 return 0;
369
370         if (port == SEQ_INDEX)
371                 data = NVReadVgaSeq(pNv, crtchead, index);
372         else    /* assume CRTC_INDEX_COLOR */
373                 data = NVReadVgaCrtc(pNv, crtchead, index);
374
375         if (DEBUGLEVEL >= 6)
376                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
377                            "    Indexed IO read:  Port: 0x%04X, Index: 0x%02X, Head: 0x%02X, Data: 0x%02X\n",
378                            port, index, crtchead, data);
379
380         return data;
381 }
382
383 static void nv_idx_port_wr(ScrnInfoPtr pScrn, uint16_t port, uint8_t index, uint8_t data)
384 {
385         NVPtr pNv = NVPTR(pScrn);
386
387         if (!nv_valid_idx_port(pScrn, port))
388                 return;
389
390         /* The current head is maintained in a file scope variable crtchead.
391          * We trap changes to CRTCX_OWNER and update the head variable
392          * and hence the register set written.
393          * As CRTCX_OWNER only exists on CRTC0, we update crtchead to head0
394          * in advance of the write, and to head1 after the write
395          */
396         if (port == CRTC_INDEX_COLOR && index == NV_VGA_CRTCX_OWNER && data != NV_VGA_CRTCX_OWNER_HEADB)
397                 crtchead = 0;
398
399         if (DEBUGLEVEL >= 8)
400                 nv_idx_port_rd(pScrn, port, index);
401         if (DEBUGLEVEL >= 6)
402                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
403                            "    Indexed IO write: Port: 0x%04X, Index: 0x%02X, Head: 0x%02X, Data: 0x%02X\n",
404                            port, index, crtchead, data);
405
406         if (pNv->VBIOS.execute) {
407                 still_alive();
408                 if (port == SEQ_INDEX)
409                         NVWriteVgaSeq(pNv, crtchead, index, data);
410                 else    /* assume CRTC_INDEX_COLOR */
411                         NVWriteVgaCrtc(pNv, crtchead, index, data);
412         }
413
414         if (port == CRTC_INDEX_COLOR && index == NV_VGA_CRTCX_OWNER && data == NV_VGA_CRTCX_OWNER_HEADB)
415                 crtchead = 1;
416 }
417
418 static uint8_t nv_port_rd(ScrnInfoPtr pScrn, uint16_t port)
419 {
420         NVPtr pNv = NVPTR(pScrn);
421         uint8_t data;
422
423         if (!nv_valid_port(pScrn, port))
424                 return 0;
425
426         data = NVReadPVIO(pNv, crtchead, port);
427
428         if (DEBUGLEVEL >= 6)
429                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
430                            "    IO read:  Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
431                            port, crtchead, data);
432
433         return data;
434 }
435
436 static void nv_port_wr(ScrnInfoPtr pScrn, uint16_t port, uint8_t data)
437 {
438         NVPtr pNv = NVPTR(pScrn);
439
440         if (!nv_valid_port(pScrn, port))
441                 return;
442
443         if (DEBUGLEVEL >= 8)
444                 nv_port_rd(pScrn, port);
445         if (DEBUGLEVEL >= 6)
446                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
447                            "    IO write: Port: 0x%04X, Head: 0x%02X, Data: 0x%02X\n",
448                            port, crtchead, data);
449
450         if (pNv->VBIOS.execute) {
451                 still_alive();
452                 NVWritePVIO(pNv, crtchead, port, data);
453         }
454 }
455
456 #define ACCESS_UNLOCK 0
457 #define ACCESS_LOCK 1
458 static void crtc_access(ScrnInfoPtr pScrn, bool lock)
459 {
460         NVPtr pNv = NVPTR(pScrn);
461
462         if (pNv->twoHeads)
463                 NVSetOwner(pScrn, 0);
464         NVLockVgaCrtc(pNv, 0, lock);
465         if (pNv->twoHeads) {
466                 NVSetOwner(pScrn, 1);
467                 NVLockVgaCrtc(pNv, 1, lock);
468                 NVSetOwner(pScrn, crtchead);
469         }
470 }
471
472 static bool io_flag_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, uint8_t cond)
473 {
474         /* The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
475          * for the CRTC index; 1 byte for the mask to apply to the value
476          * retrieved from the CRTC; 1 byte for the shift right to apply to the
477          * masked CRTC value; 2 bytes for the offset to the flag array, to
478          * which the shifted value is added; 1 byte for the mask applied to the
479          * value read from the flag array; and 1 byte for the value to compare
480          * against the masked byte from the flag table.
481          */
482
483         uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
484         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[condptr])));
485         uint8_t crtcindex = bios->data[condptr + 2];
486         uint8_t mask = bios->data[condptr + 3];
487         uint8_t shift = bios->data[condptr + 4];
488         uint16_t flagarray = le16_to_cpu(*((uint16_t *)(&bios->data[condptr + 5])));
489         uint8_t flagarraymask = bios->data[condptr + 7];
490         uint8_t cmpval = bios->data[condptr + 8];
491         uint8_t data;
492
493         if (DEBUGLEVEL >= 6)
494                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
495                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, Cmpval: 0x%02X\n",
496                            offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
497
498         data = nv_idx_port_rd(pScrn, crtcport, crtcindex);
499
500         data = bios->data[flagarray + ((data & mask) >> shift)];
501         data &= flagarraymask;
502
503         if (DEBUGLEVEL >= 6)
504                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
505                            "0x%04X: Checking if 0x%02X equals 0x%02X\n",
506                            offset, data, cmpval);
507
508         if (data == cmpval)
509                 return true;
510
511         return false;
512 }
513
514 int getMNP_single(ScrnInfoPtr pScrn, struct pll_lims *pll_lim, int clk, int *bestNM, int *bestlog2P)
515 {
516         /* Find M, N and P for a single stage PLL
517          *
518          * Note that some bioses (NV3x) have lookup tables of precomputed MNP
519          * values, but we're too lazy to use those atm
520          *
521          * "clk" parameter in kHz
522          * returns calculated clock
523          */
524
525         bios_t *bios = &NVPTR(pScrn)->VBIOS;
526         int minvco = pll_lim->vco1.minfreq, maxvco = pll_lim->vco1.maxfreq;
527         int minM = pll_lim->vco1.min_m, maxM = pll_lim->vco1.max_m;
528         int minN = pll_lim->vco1.min_n, maxN = pll_lim->vco1.max_n;
529         int minU = pll_lim->vco1.min_inputfreq, maxU = pll_lim->vco1.max_inputfreq;
530         int maxlog2P;
531         int crystal = pll_lim->refclk;
532         int M, N, log2P, P;
533         int clkP, calcclk;
534         int delta, bestdelta = INT_MAX;
535         int bestclk = 0;
536
537         /* this division verified for nv20, nv18, nv28 (Haiku), and nv34 */
538         /* possibly correlated with introduction of 27MHz crystal */
539         if (bios->chip_version <= 0x16 || bios->chip_version == 0x20) {
540                 if (clk > 250000)
541                         maxM = 6;
542                 if (clk > 340000)
543                         maxM = 2;
544                 maxlog2P = 4;
545         } else if (bios->chip_version < 0x40) {
546                 if (clk > 150000)
547                         maxM = 6;
548                 if (clk > 200000)
549                         maxM = 4;
550                 if (clk > 340000)
551                         maxM = 2;
552                 maxlog2P = 5;
553         } else /* nv4x may be subject to the nv17+ limits, but assume not for now */
554                 maxlog2P = 6;
555
556         if ((clk << maxlog2P) < minvco) {
557                 minvco = clk << maxlog2P;
558                 maxvco = minvco * 2;
559         }
560         if (clk + clk/200 > maxvco)     /* +0.5% */
561                 maxvco = clk + clk/200;
562
563         /* NV34 goes maxlog2P->0, NV20 goes 0->maxlog2P */
564         for (log2P = 0; log2P <= maxlog2P; log2P++) {
565                 P = 1 << log2P;
566                 clkP = clk * P;
567
568                 if (clkP < minvco)
569                         continue;
570                 if (clkP > maxvco)
571                         return bestclk;
572
573                 for (M = minM; M <= maxM; M++) {
574                         if (crystal/M < minU)
575                                 return bestclk;
576                         if (crystal/M > maxU)
577                                 continue;
578
579                         /* add crystal/2 to round better */
580                         N = (clkP * M + crystal/2) / crystal;
581
582                         if (N < minN)
583                                 continue;
584                         if (N > maxN)
585                                 break;
586
587                         /* more rounding additions */
588                         calcclk = ((N * crystal + P/2) / P + M/2) / M;
589                         delta = abs(calcclk - clk);
590                         /* we do an exhaustive search rather than terminating
591                          * on an optimality condition...
592                          */
593                         if (delta < bestdelta) {
594                                 bestdelta = delta;
595                                 bestclk = calcclk;
596                                 *bestNM = N << 8 | M;
597                                 *bestlog2P = log2P;
598                                 if (delta == 0) /* except this one */
599                                         return bestclk;
600                         }
601                 }
602         }
603
604         return bestclk;
605 }
606
607 int getMNP_double(ScrnInfoPtr pScrn, struct pll_lims *pll_lim, int clk, int *bestNM1, int *bestNM2, int *bestlog2P)
608 {
609         /* Find M, N and P for a two stage PLL
610          *
611          * Note that some bioses (NV30+) have lookup tables of precomputed MNP
612          * values, but we're too lazy to use those atm
613          *
614          * "clk" parameter in kHz
615          * returns calculated clock
616          */
617
618         int minvco1 = pll_lim->vco1.minfreq, maxvco1 = pll_lim->vco1.maxfreq;
619         int minvco2 = pll_lim->vco2.minfreq, maxvco2 = pll_lim->vco2.maxfreq;
620         int minU1 = pll_lim->vco1.min_inputfreq, minU2 = pll_lim->vco2.min_inputfreq;
621         int maxU1 = pll_lim->vco1.max_inputfreq, maxU2 = pll_lim->vco2.max_inputfreq;
622         int minM1 = pll_lim->vco1.min_m, maxM1 = pll_lim->vco1.max_m;
623         int minN1 = pll_lim->vco1.min_n, maxN1 = pll_lim->vco1.max_n;
624         int minM2 = pll_lim->vco2.min_m, maxM2 = pll_lim->vco2.max_m;
625         int minN2 = pll_lim->vco2.min_n, maxN2 = pll_lim->vco2.max_n;
626         int crystal = pll_lim->refclk;
627         bool fixedgain2 = (minM2 == maxM2 && minN2 == maxN2);
628         int M1, N1, M2, N2, log2P;
629         int clkP, calcclk1, calcclk2, calcclkout;
630         int delta, bestdelta = INT_MAX;
631         int bestclk = 0;
632
633         int vco2 = (maxvco2 - maxvco2/200) / 2;
634         for (log2P = 0; clk && log2P < 6 && clk <= (vco2 >> log2P); log2P++) /* log2P is maximum of 6 */
635                 ;
636         clkP = clk << log2P;
637
638         if (maxvco2 < clk + clk/200)    /* +0.5% */
639                 maxvco2 = clk + clk/200;
640
641         for (M1 = minM1; M1 <= maxM1; M1++) {
642                 if (crystal/M1 < minU1)
643                         return bestclk;
644                 if (crystal/M1 > maxU1)
645                         continue;
646
647                 for (N1 = minN1; N1 <= maxN1; N1++) {
648                         calcclk1 = crystal * N1 / M1;
649                         if (calcclk1 < minvco1)
650                                 continue;
651                         if (calcclk1 > maxvco1)
652                                 break;
653
654                         for (M2 = minM2; M2 <= maxM2; M2++) {
655                                 if (calcclk1/M2 < minU2)
656                                         break;
657                                 if (calcclk1/M2 > maxU2)
658                                         continue;
659
660                                 /* add calcclk1/2 to round better */
661                                 N2 = (clkP * M2 + calcclk1/2) / calcclk1;
662                                 if (N2 < minN2)
663                                         continue;
664                                 if (N2 > maxN2)
665                                         break;
666
667                                 if (!fixedgain2) {
668                                         if (NVPTR(pScrn)->VBIOS.chip_version < 0x60)
669                                                 if (N2/M2 < 4 || N2/M2 > 10)
670                                                         continue;
671
672                                         calcclk2 = calcclk1 * N2 / M2;
673                                         if (calcclk2 < minvco2)
674                                                 break;
675                                         if (calcclk2 > maxvco2)
676                                                 continue;
677                                 } else
678                                         calcclk2 = calcclk1;
679
680                                 calcclkout = calcclk2 >> log2P;
681                                 delta = abs(calcclkout - clk);
682                                 /* we do an exhaustive search rather than terminating
683                                  * on an optimality condition...
684                                  */
685                                 if (delta < bestdelta) {
686                                         bestdelta = delta;
687                                         bestclk = calcclkout;
688                                         *bestNM1 = N1 << 8 | M1;
689                                         *bestNM2 = N2 << 8 | M2;
690                                         *bestlog2P = log2P;
691                                         if (delta == 0) /* except this one */
692                                                 return bestclk;
693                                 }
694                         }
695                 }
696         }
697
698         return bestclk;
699 }
700
701 static void setPLL_single(ScrnInfoPtr pScrn, uint32_t reg, int NM, int log2P)
702 {
703         bios_t *bios = &NVPTR(pScrn)->VBIOS;
704         uint32_t oldpll = nv32_rd(pScrn, reg);
705         uint32_t pll = (oldpll & 0xfff80000) | log2P << 16 | NM;
706         uint32_t saved_powerctrl_1 = 0;
707         int shift_powerctrl_1 = -4;
708
709         if (oldpll == pll)
710                 return; /* already set */
711
712         /* nv18 doesn't change POWERCTRL_1 for VPLL*; does gf4 need special-casing? */
713         if (bios->chip_version >= 0x17 && bios->chip_version != 0x20) {
714                 switch (reg) {
715                 case NV_RAMDAC_VPLL2:
716                         shift_powerctrl_1 += 4;
717                 case NV_RAMDAC_VPLL:
718                         shift_powerctrl_1 += 4;
719                 case NV_RAMDAC_MPLL:
720                         shift_powerctrl_1 += 4;
721                 case NV_RAMDAC_NVPLL:
722                         shift_powerctrl_1 += 4;
723                 }
724
725                 if (shift_powerctrl_1 >= 0) {
726                         saved_powerctrl_1 = nv32_rd(pScrn, NV_PBUS_POWERCTRL_1);
727                         nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, (saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) | 1 << shift_powerctrl_1);
728                 }
729         }
730
731         /* write NM first */
732         nv32_wr(pScrn, reg, (oldpll & 0xffff0000) | NM);
733
734         /* wait a bit */
735         usleep(64000);
736         nv32_rd(pScrn, reg);
737
738         /* then write P as well */
739         nv32_wr(pScrn, reg, pll);
740
741         if (shift_powerctrl_1 >= 0)
742                 nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, saved_powerctrl_1);
743 }
744
745 static void setPLL_double_highregs(ScrnInfoPtr pScrn, uint32_t reg1, int NM1, int NM2, int log2P)
746 {
747         bios_t *bios = &NVPTR(pScrn)->VBIOS;
748         bool nv3035 = bios->chip_version == 0x30 || bios->chip_version == 0x35;
749         uint32_t reg2 = reg1 + ((reg1 == NV_RAMDAC_VPLL2) ? 0x5c : 0x70);
750         uint32_t oldpll1 = nv32_rd(pScrn, reg1), oldpll2 = !nv3035 ? nv32_rd(pScrn, reg2) : 0;
751         uint32_t pll1 = (oldpll1 & 0xfff80000) | log2P << 16 | NM1;
752         uint32_t pll2 = !nv3035 ? (oldpll2 & 0x7fff0000) | 1 << 31 | NM2 : 0;
753         uint32_t saved_powerctrl_1 = 0, savedc040 = 0, maskc040 = ~0;
754         int shift_powerctrl_1 = -1;
755         bool single_stage = !NM2 || (((NM2 >> 8) & 0xff) == (NM2 & 0xff));
756
757         if (nv3035)
758                 pll1 = (pll1 & 0xfcc7ffff) | (NM2 & (0x18 << 8)) << 13 | (NM2 & (0x7 << 8)) << 11 | 8 << 4 | (NM2 & 7) << 4;
759         
760         if (oldpll1 == pll1 && oldpll2 == pll2)
761                 return; /* already set */
762
763         if (reg1 == NV_RAMDAC_NVPLL) {
764                 shift_powerctrl_1 = 0;
765                 maskc040 = ~(3 << 20);
766         }
767         if (reg1 == NV_RAMDAC_MPLL) {
768                 shift_powerctrl_1 = 4;
769                 maskc040 = ~(3 << 22);
770         }
771         if (shift_powerctrl_1 >= 0) {
772                 saved_powerctrl_1 = nv32_rd(pScrn, NV_PBUS_POWERCTRL_1);
773                 nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, (saved_powerctrl_1 & ~(0xf << shift_powerctrl_1)) | 1 << shift_powerctrl_1);
774         }
775
776         if (bios->chip_version >= 0x40) {
777                 savedc040 = nv32_rd(pScrn, 0xc040);
778                 nv32_wr(pScrn, 0xc040, savedc040 & maskc040);
779
780                 if (!single_stage) {
781                         if (reg1 == NV_RAMDAC_VPLL)
782                                 nv32_wr(pScrn, NV_RAMDAC_580, nv32_rd(pScrn, NV_RAMDAC_580) & ~NV_RAMDAC_580_VPLL1_ACTIVE);
783                         if (reg1 == NV_RAMDAC_VPLL2)
784                                 nv32_wr(pScrn, NV_RAMDAC_580, nv32_rd(pScrn, NV_RAMDAC_580) & ~NV_RAMDAC_580_VPLL2_ACTIVE);
785                 } else {
786                         if (reg1 == NV_RAMDAC_VPLL)
787                                 nv32_wr(pScrn, NV_RAMDAC_580, nv32_rd(pScrn, NV_RAMDAC_580) | NV_RAMDAC_580_VPLL1_ACTIVE);
788                         if (reg1 == NV_RAMDAC_VPLL2)
789                                 nv32_wr(pScrn, NV_RAMDAC_580, nv32_rd(pScrn, NV_RAMDAC_580) | NV_RAMDAC_580_VPLL2_ACTIVE);
790                         pll2 |= 0x011f;
791                 }
792         }
793
794         if (!nv3035)
795                 nv32_wr(pScrn, reg2, pll2);
796         nv32_wr(pScrn, reg1, pll1);
797
798         if (shift_powerctrl_1 >= 0) {
799                 nv32_wr(pScrn, NV_PBUS_POWERCTRL_1, saved_powerctrl_1);
800                 if (bios->chip_version >= 0x40)
801                         nv32_wr(pScrn, 0xc040, savedc040);
802         }
803 }
804
805 static void setPLL_double_lowregs(ScrnInfoPtr pScrn, uint32_t NMNMreg, int NM1, int NM2, int log2P)
806 {
807         /* When setting PLLs, there is a merry game of disabling and enabling
808          * various bits of hardware during the process. This function is a
809          * synthesis of six nv40 traces, nearly each card doing a subtly
810          * different thing. With luck all the necessary bits for each card are
811          * combined herein. Without luck it deviates from each card's formula
812          * so as to not work on any :)
813          */
814
815         uint32_t Preg = NMNMreg - 4;
816         uint32_t oldPval = nv32_rd(pScrn, Preg);
817         uint32_t NMNM = NM2 << 16 | NM1;
818         uint32_t Pval = (oldPval & ((Preg == 0x4020) ? ~(0x11 << 16) : ~(1 << 16))) | 0xc << 28 | log2P << 16;
819         uint32_t saved4600 = 0;
820         /* some cards have different maskc040s */
821         uint32_t maskc040 = ~(3 << 14), savedc040;
822         bool single_stage = !NM2 || (((NM2 >> 8) & 0xff) == (NM2 & 0xff));
823
824         if (nv32_rd(pScrn, NMNMreg) == NMNM && (oldPval & 0xc0070000) == Pval)
825                 return;
826
827         if (Preg == 0x4000)
828                 maskc040 = ~0x333;
829         if (Preg == 0x4058)
830                 maskc040 = ~(0xc << 24);
831
832         if (Preg == 0x4020) {
833                 struct pll_lims pll_lim;
834                 uint8_t Pval2;
835
836                 if (!get_pll_limits(pScrn, Preg, &pll_lim))
837                         return;
838
839                 Pval2 = log2P + pll_lim.log2p_bias;
840                 if (Pval2 > pll_lim.max_log2p_bias)
841                         Pval2 = pll_lim.max_log2p_bias;
842                 Pval |= 1 << 28 | Pval2 << 20;
843
844                 saved4600 = nv32_rd(pScrn, 0x4600);
845                 nv32_wr(pScrn, 0x4600, saved4600 | 8 << 28);
846         }
847         if (single_stage)
848                 Pval |= (Preg == 0x4020) ? 1 << 12 : 1 << 8;
849
850         nv32_wr(pScrn, Preg, oldPval | 1 << 28);
851         nv32_wr(pScrn, Preg, Pval & ~(4 << 28));
852         if (Preg == 0x4020) {
853                 Pval |= 8 << 20;
854                 nv32_wr(pScrn, 0x4020, Pval & ~(0xc << 28));
855                 nv32_wr(pScrn, 0x4038, Pval & ~(0xc << 28));
856         }
857
858         savedc040 = nv32_rd(pScrn, 0xc040);
859         nv32_wr(pScrn, 0xc040, savedc040 & maskc040);
860
861         nv32_wr(pScrn, NMNMreg, NMNM);
862         if (NMNMreg == 0x4024)
863                 nv32_wr(pScrn, 0x403c, NMNM);
864
865         nv32_wr(pScrn, Preg, Pval);
866         if (Preg == 0x4020) {
867                 Pval &= ~(8 << 20);
868                 nv32_wr(pScrn, 0x4020, Pval);
869                 nv32_wr(pScrn, 0x4038, Pval);
870                 nv32_wr(pScrn, 0x4600, saved4600);
871         }
872
873         nv32_wr(pScrn, 0xc040, savedc040);
874
875         if (Preg == 0x4020) {
876                 nv32_wr(pScrn, 0x4020, Pval & ~(1 << 28));
877                 nv32_wr(pScrn, 0x4038, Pval & ~(1 << 28));
878         }
879 }
880
881 static void setPLL(ScrnInfoPtr pScrn, bios_t *bios, uint32_t reg, uint32_t clk)
882 {
883         /* clk in kHz */
884         struct pll_lims pll_lim;
885         int NM1 = 0xbeef, NM2 = 0xdead, log2P;
886
887         /* high regs (such as in the mac g5 table) are not -= 4 */
888         if (!get_pll_limits(pScrn, reg > 0x405c ? reg : reg - 4, &pll_lim))
889                 return;
890
891         if (bios->chip_version >= 0x40 || bios->chip_version == 0x30 ||
892             bios->chip_version == 0x31 || bios->chip_version == 0x35 ||
893             bios->chip_version == 0x36) {
894                 getMNP_double(pScrn, &pll_lim, clk, &NM1, &NM2, &log2P);
895                 if (NM2 == 0xdead) {
896                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
897                                    "Could not find a suitable set of coefficients, giving up\n");
898                         return;
899                 }
900                 if (reg > 0x405c)
901                         setPLL_double_highregs(pScrn, reg, NM1, NM2, log2P);
902                 else
903                         setPLL_double_lowregs(pScrn, reg, NM1, NM2, log2P);
904         } else {
905                 getMNP_single(pScrn, &pll_lim, clk, &NM1, &log2P);
906                 setPLL_single(pScrn, reg, NM1, log2P);
907         }
908 }
909
910 #if 0
911 static bool init_prog(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
912 {
913         /* INIT_PROG   opcode: 0x31
914          * 
915          * offset      (8  bit): opcode
916          * offset + 1  (32 bit): reg
917          * offset + 5  (32 bit): and mask
918          * offset + 9  (8  bit): shift right
919          * offset + 10 (8  bit): number of configurations
920          * offset + 11 (32 bit): register
921          * offset + 15 (32 bit): configuration 1
922          * ...
923          * 
924          * Starting at offset + 15 there are "number of configurations"
925          * 32 bit values. To find out which configuration value to use
926          * read "CRTC reg" on the CRTC controller with index "CRTC index"
927          * and bitwise AND this value with "and mask" and then bit shift the
928          * result "shift right" bits to the right.
929          * Assign "register" with appropriate configuration value.
930          */
931
932         CARD32 reg = *((CARD32 *) (&bios->data[offset + 1]));
933         CARD32 and = *((CARD32 *) (&bios->data[offset + 5]));
934         CARD8 shiftr = *((CARD8 *) (&bios->data[offset + 9]));
935         CARD8 nr = *((CARD8 *) (&bios->data[offset + 10]));
936         CARD32 reg2 = *((CARD32 *) (&bios->data[offset + 11]));
937         CARD8 configuration;
938         CARD32 configval, tmp;
939
940         if (iexec->execute) {
941                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: REG: 0x%04X\n", offset, 
942                                 reg);
943
944                 tmp = nv32_rd(pScrn, reg);
945                 configuration = (tmp & and) >> shiftr;
946
947                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CONFIGURATION TO USE: 0x%02X\n", 
948                                 offset, configuration);
949
950                 if (configuration <= nr) {
951
952                         configval = 
953                                 *((CARD32 *) (&bios->data[offset + 15 + configuration * 4]));
954
955                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: REG: 0x%08X, VALUE: 0x%08X\n", offset, 
956                                         reg2, configval);
957                         
958                         tmp = nv32_rd(pScrn, reg2);
959                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CURRENT VALUE IS: 0x%08X\n",
960                                 offset, tmp);
961                         nv32_wr(pScrn, reg2, configval);
962                 }
963         }
964         return true;
965 }
966 #endif
967
968 static bool init_io_restrict_prog(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
969 {
970         /* INIT_IO_RESTRICT_PROG   opcode: 0x32 ('2')
971          *
972          * offset      (8  bit): opcode
973          * offset + 1  (16 bit): CRTC port
974          * offset + 3  (8  bit): CRTC index
975          * offset + 4  (8  bit): mask
976          * offset + 5  (8  bit): shift
977          * offset + 6  (8  bit): count
978          * offset + 7  (32 bit): register
979          * offset + 11 (32 bit): configuration 1
980          * ...
981          *
982          * Starting at offset + 11 there are "count" 32 bit values.
983          * To find out which value to use read index "CRTC index" on "CRTC port",
984          * AND this value with "mask" and then bit shift right "shift" bits.
985          * Read the appropriate value using this index and write to "register"
986          */
987
988         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
989         uint8_t crtcindex = bios->data[offset + 3];
990         uint8_t mask = bios->data[offset + 4];
991         uint8_t shift = bios->data[offset + 5];
992         uint8_t count = bios->data[offset + 6];
993         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7])));
994         uint8_t config;
995         uint32_t configval;
996
997         if (!iexec->execute)
998                 return true;
999
1000         if (DEBUGLEVEL >= 6)
1001                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1002                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1003                            offset, crtcport, crtcindex, mask, shift, count, reg);
1004
1005         config = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) >> shift;
1006         if (config > count) {
1007                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1008                            "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1009                            offset, config, count);
1010                 return false;
1011         }
1012
1013         configval = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 11 + config * 4])));
1014
1015         if (DEBUGLEVEL >= 6)
1016                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1017                            "0x%04X: Writing config %02X\n", offset, config);
1018
1019         nv32_wr(pScrn, reg, configval);
1020
1021         return true;
1022 }
1023
1024 static bool init_repeat(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1025 {
1026         /* INIT_REPEAT   opcode: 0x33 ('3')
1027          *
1028          * offset      (8 bit): opcode
1029          * offset + 1  (8 bit): count
1030          *
1031          * Execute script following this opcode up to INIT_REPEAT_END
1032          * "count" times
1033          */
1034
1035         uint8_t count = bios->data[offset + 1];
1036         uint8_t i;
1037
1038         /* no iexec->execute check by design */
1039
1040         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1041                    "0x%04X: REPEATING FOLLOWING SEGMENT %d TIMES\n",
1042                    offset, count);
1043
1044         iexec->repeat = true;
1045
1046         /* count - 1, as the script block will execute once when we leave this
1047          * opcode -- this is compatible with bios behaviour as:
1048          * a) the block is always executed at least once, even if count == 0
1049          * b) the bios interpreter skips to the op following INIT_END_REPEAT,
1050          * while we don't
1051          */
1052         for (i = 0; i < count - 1; i++)
1053                 parse_init_table(pScrn, bios, offset + 2, iexec);
1054
1055         iexec->repeat = false;
1056
1057         return true;
1058 }
1059
1060 static bool init_io_restrict_pll(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1061 {
1062         /* INIT_IO_RESTRICT_PLL   opcode: 0x34 ('4')
1063          *
1064          * offset      (8  bit): opcode
1065          * offset + 1  (16 bit): CRTC port
1066          * offset + 3  (8  bit): CRTC index
1067          * offset + 4  (8  bit): mask
1068          * offset + 5  (8  bit): shift
1069          * offset + 6  (8  bit): IO flag condition index
1070          * offset + 7  (8  bit): count
1071          * offset + 8  (32 bit): register
1072          * offset + 12 (16 bit): frequency 1
1073          * ...
1074          *
1075          * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
1076          * Set PLL register "register" to coefficients for frequency n,
1077          * selected by reading index "CRTC index" of "CRTC port" ANDed with
1078          * "mask" and shifted right by "shift". If "IO flag condition index" > 0,
1079          * and condition met, double frequency before setting it.
1080          */
1081
1082         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1083         uint8_t crtcindex = bios->data[offset + 3];
1084         uint8_t mask = bios->data[offset + 4];
1085         uint8_t shift = bios->data[offset + 5];
1086         int8_t io_flag_condition_idx = bios->data[offset + 6];
1087         uint8_t count = bios->data[offset + 7];
1088         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 8])));
1089         uint8_t config;
1090         uint16_t freq;
1091
1092         if (!iexec->execute)
1093                 return true;
1094
1095         if (DEBUGLEVEL >= 6)
1096                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1097                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, IO Flag Condition: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1098                            offset, crtcport, crtcindex, mask, shift, io_flag_condition_idx, count, reg);
1099
1100         config = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) >> shift;
1101         if (config > count) {
1102                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1103                            "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1104                            offset, config, count);
1105                 return false;
1106         }
1107
1108         freq = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 12 + config * 2])));
1109
1110         if (io_flag_condition_idx > 0) {
1111                 if (io_flag_condition(pScrn, bios, offset, io_flag_condition_idx)) {
1112                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1113                                    "0x%04X: CONDITION FULFILLED - FREQ DOUBLED\n", offset);
1114                         freq *= 2;
1115                 } else
1116                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1117                                    "0x%04X: CONDITION IS NOT FULFILLED. FREQ UNCHANGED\n", offset);
1118         }
1119
1120         if (DEBUGLEVEL >= 6)
1121                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1122                            "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
1123                            offset, reg, config, freq);
1124
1125         setPLL(pScrn, bios, reg, freq * 10);
1126
1127         return true;
1128 }
1129
1130 static bool init_end_repeat(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1131 {
1132         /* INIT_END_REPEAT   opcode: 0x36 ('6')
1133          *
1134          * offset      (8 bit): opcode
1135          *
1136          * Marks the end of the block for INIT_REPEAT to repeat
1137          */
1138
1139         /* no iexec->execute check by design */
1140
1141         /* iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
1142          * we're not in repeat mode
1143          */
1144         if (iexec->repeat)
1145                 return false;
1146
1147         return true;
1148 }
1149
1150 static bool init_copy(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1151 {
1152         /* INIT_COPY   opcode: 0x37 ('7')
1153          *
1154          * offset      (8  bit): opcode
1155          * offset + 1  (32 bit): register
1156          * offset + 5  (8  bit): shift
1157          * offset + 6  (8  bit): srcmask
1158          * offset + 7  (16 bit): CRTC port
1159          * offset + 9  (8 bit): CRTC index
1160          * offset + 10  (8 bit): mask
1161          *
1162          * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
1163          * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC port
1164          */
1165
1166         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1167         uint8_t shift = bios->data[offset + 5];
1168         uint8_t srcmask = bios->data[offset + 6];
1169         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 7])));
1170         uint8_t crtcindex = bios->data[offset + 9];
1171         uint8_t mask = bios->data[offset + 10];
1172         uint32_t data;
1173         uint8_t crtcdata;
1174
1175         if (!iexec->execute)
1176                 return true;
1177
1178         if (DEBUGLEVEL >= 6)
1179                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1180                            "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
1181                            offset, reg, shift, srcmask, crtcport, crtcindex, mask);
1182
1183         data = nv32_rd(pScrn, reg);
1184
1185         if (shift < 0x80)
1186                 data >>= shift;
1187         else
1188                 data <<= (0x100 - shift);
1189
1190         data &= srcmask;
1191
1192         crtcdata = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) | (uint8_t)data;
1193         nv_idx_port_wr(pScrn, crtcport, crtcindex, crtcdata);
1194
1195         return true;
1196 }
1197
1198 static bool init_not(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1199 {
1200         /* INIT_NOT   opcode: 0x38 ('8')
1201          *
1202          * offset      (8  bit): opcode
1203          *
1204          * Invert the current execute / no-execute condition (i.e. "else")
1205          */
1206         if (iexec->execute)
1207                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1208                            "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
1209         else
1210                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1211                            "0x%04X: ------ EXECUTING FOLLOWING COMMANDS ------\n", offset);
1212
1213         iexec->execute = !iexec->execute;
1214         return true;
1215 }
1216
1217 static bool init_io_flag_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1218 {
1219         /* INIT_IO_FLAG_CONDITION   opcode: 0x39 ('9')
1220          *
1221          * offset      (8 bit): opcode
1222          * offset + 1  (8 bit): condition number
1223          *
1224          * Check condition "condition number" in the IO flag condition table.
1225          * If condition not met skip subsequent opcodes until condition is
1226          * inverted (INIT_NOT), or we hit INIT_RESUME
1227          */
1228
1229         uint8_t cond = bios->data[offset + 1];
1230
1231         if (!iexec->execute)
1232                 return true;
1233
1234         if (io_flag_condition(pScrn, bios, offset, cond))
1235                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1236                            "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
1237         else {
1238                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1239                            "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
1240                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1241                            "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
1242                 iexec->execute = false;
1243         }
1244
1245         return true;
1246 }
1247
1248 static bool init_idx_addr_latched(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1249 {
1250         /* INIT_INDEX_ADDRESS_LATCHED   opcode: 0x49 ('I')
1251          *
1252          * offset      (8  bit): opcode
1253          * offset + 1  (32 bit): control register
1254          * offset + 5  (32 bit): data register
1255          * offset + 9  (32 bit): mask
1256          * offset + 13 (32 bit): data
1257          * offset + 17 (8  bit): count
1258          * offset + 18 (8  bit): address 1
1259          * offset + 19 (8  bit): data 1
1260          * ...
1261          *
1262          * For each of "count" address and data pairs, write "data n" to "data register",
1263          * read the current value of "control register", and write it back once ANDed
1264          * with "mask", ORed with "data", and ORed with "address n"
1265          */
1266
1267         uint32_t controlreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1268         uint32_t datareg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1269         uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
1270         uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 13])));
1271         uint8_t count = bios->data[offset + 17];
1272         uint32_t value;
1273         int i;
1274
1275         if (!iexec->execute)
1276                 return true;
1277
1278         if (DEBUGLEVEL >= 6)
1279                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1280                            "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
1281                            offset, controlreg, datareg, mask, data, count);
1282
1283         for (i = 0; i < count; i++) {
1284                 uint8_t instaddress = bios->data[offset + 18 + i * 2];
1285                 uint8_t instdata = bios->data[offset + 19 + i * 2];
1286
1287                 if (DEBUGLEVEL >= 6)
1288                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1289                                    "0x%04X: Address: 0x%02X, Data: 0x%02X\n", offset, instaddress, instdata);
1290
1291                 nv32_wr(pScrn, datareg, instdata);
1292                 value = (nv32_rd(pScrn, controlreg) & mask) | data | instaddress;
1293                 nv32_wr(pScrn, controlreg, value);
1294         }
1295
1296         return true;
1297 }
1298
1299 static bool init_io_restrict_pll2(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1300 {
1301         /* INIT_IO_RESTRICT_PLL2   opcode: 0x4A ('J')
1302          *
1303          * offset      (8  bit): opcode
1304          * offset + 1  (16 bit): CRTC port
1305          * offset + 3  (8  bit): CRTC index
1306          * offset + 4  (8  bit): mask
1307          * offset + 5  (8  bit): shift
1308          * offset + 6  (8  bit): count
1309          * offset + 7  (32 bit): register
1310          * offset + 11 (32 bit): frequency 1
1311          * ...
1312          *
1313          * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
1314          * Set PLL register "register" to coefficients for frequency n,
1315          * selected by reading index "CRTC index" of "CRTC port" ANDed with
1316          * "mask" and shifted right by "shift".
1317          */
1318
1319         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1320         uint8_t crtcindex = bios->data[offset + 3];
1321         uint8_t mask = bios->data[offset + 4];
1322         uint8_t shift = bios->data[offset + 5];
1323         uint8_t count = bios->data[offset + 6];
1324         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7])));
1325         uint8_t config;
1326         uint32_t freq;
1327
1328         if (!iexec->execute)
1329                 return true;
1330
1331         if (DEBUGLEVEL >= 6)
1332                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1333                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
1334                            offset, crtcport, crtcindex, mask, shift, count, reg);
1335
1336         if (!reg)
1337                 return true;
1338
1339         config = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) >> shift;
1340         if (config > count) {
1341                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1342                            "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
1343                            offset, config, count);
1344                 return false;
1345         }
1346
1347         freq = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 11 + config * 4])));
1348
1349         if (DEBUGLEVEL >= 6)
1350                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1351                            "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
1352                            offset, reg, config, freq);
1353
1354         setPLL(pScrn, bios, reg, freq);
1355
1356         return true;
1357 }
1358
1359 static bool init_pll2(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1360 {
1361         /* INIT_PLL2   opcode: 0x4B ('K')
1362          *
1363          * offset      (8  bit): opcode
1364          * offset + 1  (32 bit): register
1365          * offset + 5  (32 bit): freq
1366          *
1367          * Set PLL register "register" to coefficients for frequency "freq"
1368          */
1369
1370         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1371         uint32_t freq = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1372
1373         if (!iexec->execute)
1374                 return true;
1375
1376         if (DEBUGLEVEL >= 6)
1377                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1378                            "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
1379                            offset, reg, freq);
1380
1381         setPLL(pScrn, bios, reg, freq);
1382
1383         return true;
1384 }
1385
1386 static uint32_t get_tmds_index_reg(ScrnInfoPtr pScrn, uint8_t mlv)
1387 {
1388         /* For mlv < 0x80, it is an index into a table of TMDS base addresses
1389          * For mlv == 0x80 use the "or" value of the dcb_entry indexed by CR58 for CR57 = 0
1390          * to index a table of offsets to the basic 0x6808b0 address
1391          * For mlv == 0x81 use the "or" value of the dcb_entry indexed by CR58 for CR57 = 0
1392          * to index a table of offsets to the basic 0x6808b0 address, and then flip the offset by 8
1393          */
1394
1395         NVPtr pNv = NVPTR(pScrn);
1396         const int pramdac_offset[13] = {0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000};
1397         const uint32_t pramdac_table[4] = {0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8};
1398
1399         if (mlv >= 0x80) {
1400                 /* here we assume that the DCB table has already been parsed */
1401                 uint8_t dcb_entry;
1402                 int dacoffset;
1403                 /* This register needs to be written to set index for reading CR58 */
1404                 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_57, 0);
1405                 dcb_entry = nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_58);
1406                 if (dcb_entry > pNv->dcb_table.entries) {
1407                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1408                                    "CR58 doesn't have a valid DCB entry currently (%02X)\n", dcb_entry);
1409                         return 0;
1410                 }
1411                 dacoffset = pramdac_offset[pNv->dcb_table.entry[dcb_entry].or];
1412                 if (mlv == 0x81)
1413                         dacoffset ^= 8;
1414                 return (0x6808b0 + dacoffset);
1415         } else {
1416                 if (mlv > (sizeof(pramdac_table) / sizeof(uint32_t))) {
1417                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
1418                                    "Magic Lookup Value too big (%02X)\n", mlv);
1419                         return 0;
1420                 }
1421                 return pramdac_table[mlv];
1422         }
1423 }
1424
1425 static bool init_tmds(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1426 {
1427         /* INIT_TMDS   opcode: 0x4F ('O')       (non-canon name)
1428          *
1429          * offset      (8 bit): opcode
1430          * offset + 1  (8 bit): magic lookup value
1431          * offset + 2  (8 bit): TMDS address
1432          * offset + 3  (8 bit): mask
1433          * offset + 4  (8 bit): data
1434          *
1435          * Read the data reg for TMDS address "TMDS address", AND it with mask
1436          * and OR it with data, then write it back
1437          * "magic lookup value" determines which TMDS base address register is used --
1438          * see get_tmds_index_reg()
1439          */
1440
1441         uint8_t mlv = bios->data[offset + 1];
1442         uint32_t tmdsaddr = bios->data[offset + 2];
1443         uint8_t mask = bios->data[offset + 3];
1444         uint8_t data = bios->data[offset + 4];
1445         uint32_t reg, value;
1446
1447         if (!iexec->execute)
1448                 return true;
1449
1450         if (DEBUGLEVEL >= 6)
1451                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1452                            "0x%04X: MagicLookupValue: 0x%02X, TMDSAddr: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1453                            offset, mlv, tmdsaddr, mask, data);
1454
1455         if (!(reg = get_tmds_index_reg(pScrn, mlv)))
1456                 return false;
1457
1458         nv32_wr(pScrn, reg, tmdsaddr | 0x10000);
1459         value = (nv32_rd(pScrn, reg + 4) & mask) | data;
1460         nv32_wr(pScrn, reg + 4, value);
1461         nv32_wr(pScrn, reg, tmdsaddr);
1462
1463         return true;
1464 }
1465
1466 static bool init_zm_tmds_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1467 {
1468         /* INIT_ZM_TMDS_GROUP   opcode: 0x50 ('P')      (non-canon name)
1469          *
1470          * offset      (8 bit): opcode
1471          * offset + 1  (8 bit): magic lookup value
1472          * offset + 2  (8 bit): count
1473          * offset + 3  (8 bit): addr 1
1474          * offset + 4  (8 bit): data 1
1475          * ...
1476          *
1477          * For each of "count" TMDS address and data pairs write "data n" to "addr n"
1478          * "magic lookup value" determines which TMDS base address register is used --
1479          * see get_tmds_index_reg()
1480          */
1481
1482         uint8_t mlv = bios->data[offset + 1];
1483         uint8_t count = bios->data[offset + 2];
1484         uint32_t reg;
1485         int i;
1486
1487         if (!iexec->execute)
1488                 return true;
1489
1490         if (DEBUGLEVEL >= 6)
1491                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1492                            "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
1493                            offset, mlv, count);
1494
1495         if (!(reg = get_tmds_index_reg(pScrn, mlv)))
1496                 return false;
1497
1498         for (i = 0; i < count; i++) {
1499                 uint8_t tmdsaddr = bios->data[offset + 3 + i * 2];
1500                 uint8_t tmdsdata = bios->data[offset + 4 + i * 2];
1501
1502                 nv32_wr(pScrn, reg + 4, tmdsdata);
1503                 nv32_wr(pScrn, reg, tmdsaddr);
1504         }
1505
1506         return true;
1507 }
1508
1509 static bool init_cr_idx_adr_latch(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1510 {
1511         /* INIT_CR_INDEX_ADDRESS_LATCHED   opcode: 0x51 ('Q')
1512          *
1513          * offset      (8 bit): opcode
1514          * offset + 1  (8 bit): CRTC index1
1515          * offset + 2  (8 bit): CRTC index2
1516          * offset + 3  (8 bit): baseaddr
1517          * offset + 4  (8 bit): count
1518          * offset + 5  (8 bit): data 1
1519          * ...
1520          *
1521          * For each of "count" address and data pairs, write "baseaddr + n" to
1522          * "CRTC index1" and "data n" to "CRTC index2"
1523          * Once complete, restore initial value read from "CRTC index1"
1524          */
1525         uint8_t crtcindex1 = bios->data[offset + 1];
1526         uint8_t crtcindex2 = bios->data[offset + 2];
1527         uint8_t baseaddr = bios->data[offset + 3];
1528         uint8_t count = bios->data[offset + 4];
1529         uint8_t oldaddr, data;
1530         int i;
1531
1532         if (!iexec->execute)
1533                 return true;
1534
1535         if (DEBUGLEVEL >= 6)
1536                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1537                            "0x%04X: Index1: 0x%02X, Index2: 0x%02X, BaseAddr: 0x%02X, Count: 0x%02X\n",
1538                            offset, crtcindex1, crtcindex2, baseaddr, count);
1539
1540         oldaddr = nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, crtcindex1);
1541
1542         for (i = 0; i < count; i++) {
1543                 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex1, baseaddr + i);
1544
1545                 data = bios->data[offset + 5 + i];
1546                 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex2, data);
1547         }
1548
1549         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex1, oldaddr);
1550
1551         return true;
1552 }
1553
1554 static bool init_cr(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1555 {
1556         /* INIT_CR   opcode: 0x52 ('R')
1557          *
1558          * offset      (8  bit): opcode
1559          * offset + 1  (8  bit): CRTC index
1560          * offset + 2  (8  bit): mask
1561          * offset + 3  (8  bit): data
1562          *
1563          * Assign the value of at "CRTC index" ANDed with mask and ORed with data
1564          * back to "CRTC index"
1565          */
1566
1567         uint8_t crtcindex = bios->data[offset + 1];
1568         uint8_t mask = bios->data[offset + 2];
1569         uint8_t data = bios->data[offset + 3];
1570         uint8_t value;
1571
1572         if (!iexec->execute)
1573                 return true;
1574
1575         if (DEBUGLEVEL >= 6)
1576                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1577                            "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1578                            offset, crtcindex, mask, data);
1579
1580         value = (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, crtcindex) & mask) | data;
1581         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex, value);
1582
1583         return true;
1584 }
1585
1586 static bool init_zm_cr(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1587 {
1588         /* INIT_ZM_CR   opcode: 0x53 ('S')
1589          *
1590          * offset      (8 bit): opcode
1591          * offset + 1  (8 bit): CRTC index
1592          * offset + 2  (8 bit): value
1593          *
1594          * Assign "value" to CRTC register with index "CRTC index".
1595          */
1596
1597         uint8_t crtcindex = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1598         uint8_t data = bios->data[offset + 2];
1599
1600         if (!iexec->execute)
1601                 return true;
1602
1603         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex, data);
1604
1605         return true;
1606 }
1607
1608 static bool init_zm_cr_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1609 {
1610         /* INIT_ZM_CR_GROUP   opcode: 0x54 ('T')
1611          *
1612          * offset      (8 bit): opcode
1613          * offset + 1  (8 bit): count
1614          * offset + 2  (8 bit): CRTC index 1
1615          * offset + 3  (8 bit): value 1
1616          * ...
1617          *
1618          * For "count", assign "value n" to CRTC register with index "CRTC index n".
1619          */
1620     
1621         uint8_t count = bios->data[offset + 1];
1622         int i;
1623
1624         if (!iexec->execute)
1625                 return true;
1626
1627         for (i = 0; i < count; i++)
1628                 init_zm_cr(pScrn, bios, offset + 2 + 2 * i - 1, iexec);
1629
1630         return true;
1631 }
1632
1633 static bool init_condition_time(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1634 {
1635         /* INIT_CONDITION_TIME   opcode: 0x56 ('V')
1636          *
1637          * offset      (8 bit): opcode
1638          * offset + 1  (8 bit): condition number
1639          * offset + 2  (8 bit): retries / 50
1640          *
1641          * Check condition "condition number" in the condition table.
1642          * The condition table entry has 4 bytes for the address of the
1643          * register to check, 4 bytes for a mask and 4 for a test value.
1644          * If condition not met sleep for 2ms, and repeat upto "retries" times.
1645          * If still not met after retries, clear execution flag for this table.
1646          */
1647
1648         uint8_t cond = bios->data[offset + 1];
1649         uint16_t retries = bios->data[offset + 2];
1650         uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
1651         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[condptr])));
1652         uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 4])));
1653         uint32_t cmpval = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 8])));
1654         uint32_t data = 0;
1655
1656         if (!iexec->execute)
1657                 return true;
1658
1659         retries *= 50;
1660
1661         if (DEBUGLEVEL >= 6)
1662                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1663                            "0x%04X: Cond: 0x%02X, Retries: 0x%02X\n", offset, cond, retries);
1664
1665         for (; retries > 0; retries--) {
1666                 data = nv32_rd(pScrn, reg) & mask;
1667
1668                 if (DEBUGLEVEL >= 6)
1669                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1670                                    "0x%04X: Checking if 0x%08X equals 0x%08X\n",
1671                                    offset, data, cmpval);
1672
1673                 if (data != cmpval) {
1674                         if (DEBUGLEVEL >= 6)
1675                                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1676                                            "0x%04X: Condition not met, sleeping for 2ms\n", offset);
1677                         usleep(2000);
1678                 } else {
1679                         if (DEBUGLEVEL >= 6)
1680                                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1681                                            "0x%04X: Condition met, continuing\n", offset);
1682                         break;
1683                 }
1684         }
1685
1686         if (data != cmpval) {
1687                 if (DEBUGLEVEL >= 6)
1688                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1689                                    "0x%04X: Condition still not met, skiping following opcodes\n", offset);
1690                 iexec->execute = false;
1691         }
1692
1693         return true;
1694 }
1695
1696 static bool init_zm_reg_sequence(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1697 {
1698         /* INIT_ZM_REG_SEQUENCE   opcode: 0x58 ('X')
1699          *
1700          * offset      (8  bit): opcode
1701          * offset + 1  (32 bit): base register
1702          * offset + 5  (8  bit): count
1703          * offset + 6  (32 bit): value 1
1704          * ...
1705          *
1706          * Starting at offset + 6 there are "count" 32 bit values.
1707          * For "count" iterations set "base register" + 4 * current_iteration
1708          * to "value current_iteration"
1709          */
1710
1711         uint32_t basereg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1712         uint32_t count = bios->data[offset + 5];
1713         int i;
1714
1715         if (!iexec->execute)
1716                 return true;
1717
1718         if (DEBUGLEVEL >= 6)
1719                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1720                            "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1721                            offset, basereg, count);
1722
1723         for (i = 0; i < count; i++) {
1724                 uint32_t reg = basereg + i * 4;
1725                 uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 6 + i * 4])));
1726
1727                 nv32_wr(pScrn, reg, data);
1728         }
1729
1730         return true;
1731 }
1732
1733 #if 0
1734 static bool init_indirect_reg(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1735 {
1736         /* INIT_INDIRECT_REG opcode: 0x5A
1737          *
1738          * offset      (8  bit): opcode
1739          * offset + 1  (32 bit): register
1740          * offset + 5  (16 bit): adress offset (in bios)
1741          *
1742          * Lookup value at offset data in the bios and write it to reg
1743          */
1744         CARD32 reg = *((CARD32 *) (&bios->data[offset + 1]));
1745         CARD16 data = le16_to_cpu(*((CARD16 *) (&bios->data[offset + 5])));
1746         CARD32 data2 = bios->data[data];
1747
1748         if (iexec->execute) {
1749                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1750                                 "0x%04X: REG: 0x%04X, DATA AT: 0x%04X, VALUE IS: 0x%08X\n", 
1751                                 offset, reg, data, data2);
1752
1753                 if (DEBUGLEVEL >= 6) {
1754                         CARD32 tmpval;
1755                         tmpval = nv32_rd(pScrn, reg);
1756                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CURRENT VALUE IS: 0x%08X\n", offset, tmpval);
1757                 }
1758
1759                 nv32_wr(pScrn, reg, data2);
1760         }
1761         return true;
1762 }
1763 #endif
1764
1765 static bool init_sub_direct(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1766 {
1767         /* INIT_SUB_DIRECT   opcode: 0x5B ('[')
1768          *
1769          * offset      (8  bit): opcode
1770          * offset + 1  (16 bit): subroutine offset (in bios)
1771          *
1772          * Calls a subroutine that will execute commands until INIT_DONE
1773          * is found. 
1774          */
1775
1776         uint16_t sub_offset = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1777
1778         if (!iexec->execute)
1779                 return true;
1780
1781         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: EXECUTING SUB-ROUTINE AT 0x%04X\n",
1782                         offset, sub_offset);
1783
1784         parse_init_table(pScrn, bios, sub_offset, iexec);
1785
1786         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: END OF SUB-ROUTINE AT 0x%04X\n",
1787                         offset, sub_offset);
1788
1789         return true;
1790 }
1791
1792 static bool init_copy_nv_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1793 {
1794         /* INIT_COPY_NV_REG   opcode: 0x5F ('_')
1795          *
1796          * offset      (8  bit): opcode
1797          * offset + 1  (32 bit): src reg
1798          * offset + 5  (8  bit): shift
1799          * offset + 6  (32 bit): src mask
1800          * offset + 10 (32 bit): xor
1801          * offset + 14 (32 bit): dst reg
1802          * offset + 18 (32 bit): dst mask
1803          *
1804          * Shift REGVAL("src reg") right by (signed) "shift", AND result with
1805          * "src mask", then XOR with "xor". Write this OR'd with
1806          * (REGVAL("dst reg") AND'd with "dst mask") to "dst reg"
1807          */
1808
1809         uint32_t srcreg = *((uint32_t *)(&bios->data[offset + 1]));
1810         uint8_t shift = bios->data[offset + 5];
1811         uint32_t srcmask = *((uint32_t *)(&bios->data[offset + 6]));
1812         uint32_t xor = *((uint32_t *)(&bios->data[offset + 10]));
1813         uint32_t dstreg = *((uint32_t *)(&bios->data[offset + 14]));
1814         uint32_t dstmask = *((uint32_t *)(&bios->data[offset + 18]));
1815         uint32_t srcvalue, dstvalue;
1816
1817         if (!iexec->execute)
1818                 return true;
1819
1820         if (DEBUGLEVEL >= 6)
1821                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1822                            "0x%04X: SrcReg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%08X, Xor: 0x%08X, DstReg: 0x%08X, DstMask: 0x%08X\n",
1823                            offset, srcreg, shift, srcmask, xor, dstreg, dstmask);
1824
1825         srcvalue = nv32_rd(pScrn, srcreg);
1826
1827         if (shift < 0x80)
1828                 srcvalue >>= shift;
1829         else
1830                 srcvalue <<= (0x100 - shift);
1831
1832         srcvalue = (srcvalue & srcmask) ^ xor;
1833
1834         dstvalue = nv32_rd(pScrn, dstreg) & dstmask;
1835
1836         nv32_wr(pScrn, dstreg, dstvalue | srcvalue);
1837
1838         return true;
1839 }
1840
1841 static bool init_zm_index_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1842 {
1843         /* INIT_ZM_INDEX_IO   opcode: 0x62 ('b')
1844          *
1845          * offset      (8  bit): opcode
1846          * offset + 1  (16 bit): CRTC port
1847          * offset + 3  (8  bit): CRTC index
1848          * offset + 4  (8  bit): data
1849          *
1850          * Write "data" to index "CRTC index" of "CRTC port"
1851          */
1852         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1853         uint8_t crtcindex = bios->data[offset + 3];
1854         uint8_t data = bios->data[offset + 4];
1855
1856         if (!iexec->execute)
1857                 return true;
1858
1859         nv_idx_port_wr(pScrn, crtcport, crtcindex, data);
1860
1861         return true;
1862 }
1863
1864 static bool init_compute_mem(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1865 {
1866         /* INIT_COMPUTE_MEM   opcode: 0x63 ('c')
1867          *
1868          * offset      (8 bit): opcode
1869          *
1870          * This opcode is meant to set NV_PFB_CFG0 (0x100200) appropriately so
1871          * that the hardware can correctly calculate how much VRAM it has
1872          * (and subsequently report that value in 0x10020C)
1873          *
1874          * The implementation of this opcode in general consists of two parts:
1875          * 1) determination of the memory bus width
1876          * 2) determination of how many of the card's RAM pads have ICs attached
1877          *
1878          * 1) is done by a cunning combination of writes to offsets 0x1c and
1879          * 0x3c in the framebuffer, and seeing whether the written values are
1880          * read back correctly. This then affects bits 4-7 of NV_PFB_CFG0
1881          *
1882          * 2) is done by a cunning combination of writes to an offset slightly
1883          * less than the maximum memory reported by 0x10020C, then seeing if
1884          * the test pattern can be read back. This then affects bits 12-15 of
1885          * NV_PFB_CFG0
1886          *
1887          * In this context a "cunning combination" may include multiple reads
1888          * and writes to varying locations, often alternating the test pattern
1889          * and 0, doubtless to make sure buffers are filled, residual charges
1890          * on tracks are removed etc.
1891          *
1892          * Unfortunately, the "cunning combination"s mentioned above, and the
1893          * changes to the bits in NV_PFB_CFG0 differ with nearly every bios
1894          * trace I have.
1895          *
1896          * Therefore, we cheat and assume the value of NV_PFB_CFG0 with which
1897          * we started was correct, and use that instead
1898          */
1899
1900         /* no iexec->execute check by design */
1901
1902         /* on every card I've seen, this step gets done for us earlier in the init scripts
1903         uint8_t crdata = nv_idx_port_rd(pScrn, SEQ_INDEX, 0x01);
1904         nv_idx_port_wr(pScrn, SEQ_INDEX, 0x01, crdata | 0x20);
1905         */
1906
1907         /* this also has probably been done in the scripts, but an mmio trace of
1908          * s3 resume shows nvidia doing it anyway (unlike the SEQ_INDEX write)
1909          */
1910         nv32_wr(pScrn, NV_PFB_REFCTRL, NV_PFB_REFCTRL_VALID_1);
1911
1912         /* write back the saved configuration value */
1913         nv32_wr(pScrn, NV_PFB_CFG0, saved_nv_pfb_cfg0);
1914
1915         return true;
1916 }
1917
1918 static bool init_reset(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1919 {
1920         /* INIT_RESET   opcode: 0x65 ('e')
1921          *
1922          * offset      (8  bit): opcode
1923          * offset + 1  (32 bit): register
1924          * offset + 5  (32 bit): value1
1925          * offset + 9  (32 bit): value2
1926          *
1927          * Assign "value1" to "register", then assign "value2" to "register"
1928          */
1929
1930         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1931         uint32_t value1 = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1932         uint32_t value2 = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
1933         uint32_t pci_nv_19, pci_nv_20;
1934
1935         /* no iexec->execute check by design */
1936
1937         pci_nv_19 = nv32_rd(pScrn, NV_PBUS_PCI_NV_19);
1938         nv32_wr(pScrn, NV_PBUS_PCI_NV_19, 0);
1939         nv32_wr(pScrn, reg, value1);
1940
1941         usleep(10);
1942
1943         nv32_wr(pScrn, reg, value2);
1944         nv32_wr(pScrn, NV_PBUS_PCI_NV_19, pci_nv_19);
1945
1946         pci_nv_20 = nv32_rd(pScrn, NV_PBUS_PCI_NV_20);
1947         pci_nv_20 &= ~NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED;     /* 0xfffffffe */
1948         nv32_wr(pScrn, NV_PBUS_PCI_NV_20, pci_nv_20);
1949
1950         return true;
1951 }
1952
1953 static bool init_configure_mem(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1954 {
1955         /* INIT_CONFIGURE_MEM   opcode: 0x66 ('f')
1956          *
1957          * offset      (8 bit): opcode
1958          *
1959          * Equivalent to INIT_DONE on bios version 3 or greater.
1960          * For early bios versions, sets up the memory registers, using values
1961          * taken from the memory init table
1962          */
1963
1964         /* no iexec->execute check by design */
1965
1966         uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_SCRATCH4) >> 4);
1967         uint16_t seqtbloffs = bios->legacy.sdr_seq_tbl_ptr, meminitdata = meminitoffs + 6;
1968         uint32_t reg, data;
1969
1970         if (bios->major_version > 2)
1971                 return false;
1972
1973         nv_idx_port_wr(pScrn, SEQ_INDEX, 0x01, nv_idx_port_rd(pScrn, SEQ_INDEX, 0x01) | 0x20);
1974
1975         if (bios->data[meminitoffs] & 1)
1976                 seqtbloffs = bios->legacy.ddr_seq_tbl_ptr;
1977
1978         for (reg = le32_to_cpu(*(uint32_t *)&bios->data[seqtbloffs]);
1979              reg != 0xffffffff;
1980              reg = le32_to_cpu(*(uint32_t *)&bios->data[seqtbloffs += 4])) {
1981
1982                 switch (reg) {
1983                 case NV_PFB_PRE:
1984                         data = NV_PFB_PRE_CMD_PRECHARGE;
1985                         break;
1986                 case NV_PFB_PAD:
1987                         data = NV_PFB_PAD_CKE_NORMAL;
1988                         break;
1989                 case NV_PFB_REF:
1990                         data = NV_PFB_REF_CMD_REFRESH;
1991                         break;
1992                 default:
1993                         data = le32_to_cpu(*(uint32_t *)&bios->data[meminitdata]);
1994                         meminitdata += 4;
1995                         if (data == 0xffffffff)
1996                                 continue;
1997                 }
1998
1999                 nv32_wr(pScrn, reg, data);
2000         }
2001
2002         return true;
2003 }
2004
2005 static bool init_configure_clk(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2006 {
2007         /* INIT_CONFIGURE_CLK   opcode: 0x67 ('g')
2008          *
2009          * offset      (8 bit): opcode
2010          *
2011          * Equivalent to INIT_DONE on bios version 3 or greater.
2012          * For early bios versions, sets up the NVClk and MClk PLLs, using
2013          * values taken from the memory init table
2014          */
2015
2016         /* no iexec->execute check by design */
2017
2018         uint16_t meminitoffs = bios->legacy.mem_init_tbl_ptr + MEM_INIT_SIZE * (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_SCRATCH4) >> 4);
2019         int clock;
2020
2021         if (bios->major_version > 2)
2022                 return false;
2023
2024         clock = le16_to_cpu(*(uint16_t *)&bios->data[meminitoffs + 4]) * 10;
2025         setPLL(pScrn, bios, NV_RAMDAC_NVPLL, clock);
2026
2027         clock = le16_to_cpu(*(uint16_t *)&bios->data[meminitoffs + 2]) * 10;
2028         if (bios->data[meminitoffs] & 1) /* DDR */
2029                 clock *= 2;
2030         setPLL(pScrn, bios, NV_RAMDAC_MPLL, clock);
2031
2032         return true;
2033 }
2034
2035 static bool init_configure_preinit(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2036 {
2037         /* INIT_CONFIGURE_PREINIT   opcode: 0x68 ('h')
2038          *
2039          * offset      (8 bit): opcode
2040          *
2041          * Equivalent to INIT_DONE on bios version 3 or greater.
2042          * For early bios versions, does early init, loading ram and crystal
2043          * configuration from straps into CR3C
2044          */
2045
2046         /* no iexec->execute check by design */
2047
2048         uint32_t straps = nv32_rd(pScrn, NV_PEXTDEV_BOOT_0);
2049         uint8_t cr3c = ((straps << 2) & 0xf0) | (straps & (1 << 6));
2050
2051         if (bios->major_version > 2)
2052                 return false;
2053
2054         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_SCRATCH4, cr3c);
2055
2056         return true;
2057 }
2058
2059 static bool init_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2060 {
2061         /* INIT_IO   opcode: 0x69 ('i')
2062          *
2063          * offset      (8  bit): opcode
2064          * offset + 1  (16 bit): CRTC port
2065          * offset + 3  (8  bit): mask
2066          * offset + 4  (8  bit): data
2067          *
2068          * Assign ((IOVAL("crtc port") & "mask") | "data") to "crtc port"
2069          */
2070
2071         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
2072         uint8_t mask = bios->data[offset + 3];
2073         uint8_t data = bios->data[offset + 4];
2074
2075         if (!iexec->execute)
2076                 return true;
2077
2078         if (DEBUGLEVEL >= 6)
2079                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2080                            "0x%04X: Port: 0x%04X, Mask: 0x%02X, Data: 0x%02X\n",
2081                            offset, crtcport, mask, data);
2082
2083         nv_port_wr(pScrn, crtcport, (nv_port_rd(pScrn, crtcport) & mask) | data);
2084
2085         return true;
2086 }
2087
2088 static bool init_sub(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2089 {
2090         /* INIT_SUB   opcode: 0x6B ('k')
2091          *
2092          * offset      (8 bit): opcode
2093          * offset + 1  (8 bit): script number
2094          *
2095          * Execute script number "script number", as a subroutine
2096          */
2097
2098         uint8_t sub = bios->data[offset + 1];
2099
2100         if (!iexec->execute)
2101                 return true;
2102
2103         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2104                    "0x%04X: EXECUTING SUB-SCRIPT %d\n", offset, sub);
2105
2106         parse_init_table(pScrn, bios,
2107                          le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + sub * 2]))),
2108                          iexec);
2109
2110         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2111                    "0x%04X: END OF SUB-SCRIPT %d\n", offset, sub);
2112
2113         return true;
2114 }
2115
2116 static bool init_ram_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2117 {
2118         /* INIT_RAM_CONDITION   opcode: 0x6D ('m')
2119          *
2120          * offset      (8 bit): opcode
2121          * offset + 1  (8 bit): mask
2122          * offset + 2  (8 bit): cmpval
2123          *
2124          * Test if (NV_PFB_BOOT_0 & "mask") equals "cmpval".
2125          * If condition not met skip subsequent opcodes until condition is
2126          * inverted (INIT_NOT), or we hit INIT_RESUME
2127          */
2128
2129         uint8_t mask = bios->data[offset + 1];
2130         uint8_t cmpval = bios->data[offset + 2];
2131         uint8_t data;
2132
2133         if (!iexec->execute)
2134                 return true;
2135
2136         data = nv32_rd(pScrn, NV_PFB_BOOT_0) & mask;
2137
2138         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2139                    "0x%04X: Checking if 0x%08X equals 0x%08X\n", offset, data, cmpval);
2140
2141         if (data == cmpval)
2142                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2143                            "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
2144         else {
2145                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
2146                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2147                            "0x%04X: ------ SKIPPING FOLLOWING COMMANDS ------\n", offset);
2148                 iexec->execute = false;
2149         }
2150
2151         return true;
2152 }
2153
2154 static bool init_nv_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2155 {
2156         /* INIT_NV_REG   opcode: 0x6E ('n')
2157          *
2158          * offset      (8  bit): opcode
2159          * offset + 1  (32 bit): register
2160          * offset + 5  (32 bit): mask
2161          * offset + 9  (32 bit): data
2162          *
2163          * Assign ((REGVAL("register") & "mask") | "data") to "register"
2164          */
2165
2166         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2167         uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
2168         uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
2169
2170         if (!iexec->execute)
2171                 return true;
2172
2173         if (DEBUGLEVEL >= 6)
2174                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2175                            "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
2176                            offset, reg, mask, data);
2177
2178         nv32_wr(pScrn, reg, (nv32_rd(pScrn, reg) & mask) | data);
2179
2180         return true;
2181 }
2182
2183 static bool init_macro(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2184 {
2185         /* INIT_MACRO   opcode: 0x6F ('o')
2186          *
2187          * offset      (8 bit): opcode
2188          * offset + 1  (8 bit): macro number
2189          *
2190          * Look up macro index "macro number" in the macro index table.
2191          * The macro index table entry has 1 byte for the index in the macro table,
2192          * and 1 byte for the number of times to repeat the macro.
2193          * The macro table entry has 4 bytes for the register address and
2194          * 4 bytes for the value to write to that register
2195          */
2196
2197         uint8_t macro_index_tbl_idx = bios->data[offset + 1];
2198         uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
2199         uint8_t macro_tbl_idx = bios->data[tmp];
2200         uint8_t count = bios->data[tmp + 1];
2201         uint32_t reg, data;
2202         int i;
2203
2204         if (!iexec->execute)
2205                 return true;
2206
2207         if (DEBUGLEVEL >= 6)
2208                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2209                            "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, Count: 0x%02X\n",
2210                            offset, macro_index_tbl_idx, macro_tbl_idx, count);
2211
2212         for (i = 0; i < count; i++) {
2213                 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
2214
2215                 reg = le32_to_cpu(*((uint32_t *)(&bios->data[macroentryptr])));
2216                 data = le32_to_cpu(*((uint32_t *)(&bios->data[macroentryptr + 4])));
2217
2218                 nv32_wr(pScrn, reg, data);
2219         }
2220
2221         return true;
2222 }
2223
2224 static bool init_done(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2225 {
2226         /* INIT_DONE   opcode: 0x71 ('q')
2227          *
2228          * offset      (8  bit): opcode
2229          *
2230          * End the current script
2231          */
2232
2233         /* mild retval abuse to stop parsing this table */
2234         return false;
2235 }
2236
2237 static bool init_resume(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2238 {
2239         /* INIT_RESUME   opcode: 0x72 ('r')
2240          *
2241          * offset      (8  bit): opcode
2242          *
2243          * End the current execute / no-execute condition
2244          */
2245
2246         if (iexec->execute)
2247                 return true;
2248
2249         iexec->execute = true;
2250         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2251                    "0x%04X: ---- EXECUTING FOLLOWING COMMANDS ----\n", offset);
2252
2253         return true;
2254 }
2255
2256 #if 0
2257 static bool init_ram_condition2(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
2258 {
2259         /* INIT_RAM_CONDITION2   opcode: 0x73
2260          * 
2261          * offset      (8  bit): opcode
2262          * offset + 1  (8  bit): and mask
2263          * offset + 2  (8  bit): cmpval
2264          *
2265          * Test if (NV_EXTDEV_BOOT & and mask) matches cmpval
2266          */
2267         NVPtr pNv = NVPTR(pScrn);
2268         CARD32 and = *((CARD32 *) (&bios->data[offset + 1]));
2269         CARD32 cmpval = *((CARD32 *) (&bios->data[offset + 5]));
2270         CARD32 data;
2271
2272         if (iexec->execute) {
2273                 data=(nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT))&and;
2274                 
2275                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
2276                                 "0x%04X: CHECKING IF REGVAL: 0x%08X equals COND: 0x%08X\n",
2277                                 offset, data, cmpval);
2278
2279                 if (data == cmpval) {
2280                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
2281                                         "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n",
2282                                         offset);
2283                 } else {
2284                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
2285                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
2286                                         "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
2287                         iexec->execute = false;
2288                 }
2289         }
2290         return true;
2291 }
2292 #endif
2293
2294 static bool init_time(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2295 {
2296         /* INIT_TIME   opcode: 0x74 ('t')
2297          *
2298          * offset      (8  bit): opcode
2299          * offset + 1  (16 bit): time
2300          *
2301          * Sleep for "time" microseconds.
2302          */
2303
2304         uint16_t time = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
2305
2306         if (!iexec->execute)
2307                 return true;
2308
2309         if (DEBUGLEVEL >= 6)
2310                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2311                            "0x%04X: Sleeping for 0x%04X microseconds\n", offset, time);
2312
2313         usleep(time);
2314
2315         return true;
2316 }
2317
2318 static bool init_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2319 {
2320         /* INIT_CONDITION   opcode: 0x75 ('u')
2321          *
2322          * offset      (8 bit): opcode
2323          * offset + 1  (8 bit): condition number
2324          *
2325          * Check condition "condition number" in the condition table.
2326          * The condition table entry has 4 bytes for the address of the
2327          * register to check, 4 bytes for a mask and 4 for a test value.
2328          * If condition not met skip subsequent opcodes until condition is
2329          * inverted (INIT_NOT), or we hit INIT_RESUME
2330          */
2331
2332         uint8_t cond = bios->data[offset + 1];
2333         uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
2334         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[condptr])));
2335         uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 4])));
2336         uint32_t cmpval = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 8])));
2337         uint32_t data;
2338
2339         if (!iexec->execute)
2340                 return true;
2341
2342         if (DEBUGLEVEL >= 6)
2343                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2344                            "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X, Cmpval: 0x%08X\n",
2345                            offset, cond, reg, mask, cmpval);
2346
2347         data = nv32_rd(pScrn, reg) & mask;
2348
2349         if (DEBUGLEVEL >= 6)
2350                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2351                            "0x%04X: Checking if 0x%08X equals 0x%08X\n",
2352                            offset, data, cmpval);
2353
2354         if (data == cmpval) {
2355                 if (DEBUGLEVEL >= 6)
2356                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2357                                    "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
2358         } else {
2359                 if (DEBUGLEVEL >= 6)
2360                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2361                                    "0x%04X: CONDITION IS NOT FULFILLED\n", offset);
2362                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2363                            "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
2364                 iexec->execute = false;
2365         }
2366
2367         return true;
2368 }
2369
2370 static bool init_index_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2371 {
2372         /* INIT_INDEX_IO   opcode: 0x78 ('x')
2373          *
2374          * offset      (8  bit): opcode
2375          * offset + 1  (16 bit): CRTC port
2376          * offset + 3  (8  bit): CRTC index
2377          * offset + 4  (8  bit): mask
2378          * offset + 5  (8  bit): data
2379          *
2380          * Read value at index "CRTC index" on "CRTC port", AND with "mask", OR with "data", write-back
2381          */
2382
2383         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
2384         uint8_t crtcindex = bios->data[offset + 3];
2385         uint8_t mask = bios->data[offset + 4];
2386         uint8_t data = bios->data[offset + 5];
2387         uint8_t value;
2388
2389         if (!iexec->execute)
2390                 return true;
2391
2392         if (DEBUGLEVEL >= 6)
2393                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2394                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
2395                            offset, crtcport, crtcindex, mask, data);
2396
2397         value = (nv_idx_port_rd(pScrn, crtcport, crtcindex) & mask) | data;
2398         nv_idx_port_wr(pScrn, crtcport, crtcindex, value);
2399
2400         return true;
2401 }
2402
2403 static bool init_pll(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2404 {
2405         /* INIT_PLL   opcode: 0x79 ('y')
2406          *
2407          * offset      (8  bit): opcode
2408          * offset + 1  (32 bit): register
2409          * offset + 5  (16 bit): freq
2410          *
2411          * Set PLL register "register" to coefficients for frequency (10kHz) "freq"
2412          */
2413
2414         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2415         uint16_t freq = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 5])));
2416
2417         if (!iexec->execute)
2418                 return true;
2419
2420         if (DEBUGLEVEL >= 6)
2421                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2422                            "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n",
2423                            offset, reg, freq);
2424
2425         setPLL(pScrn, bios, reg, freq * 10);
2426
2427         return true;
2428 }
2429
2430 static bool init_zm_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2431 {
2432         /* INIT_ZM_REG   opcode: 0x7A ('z')
2433          *
2434          * offset      (8  bit): opcode
2435          * offset + 1  (32 bit): register
2436          * offset + 5  (32 bit): value
2437          *
2438          * Assign "value" to "register"
2439          */
2440
2441         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2442         uint32_t value = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
2443
2444         if (!iexec->execute)
2445                 return true;
2446
2447         nv32_wr(pScrn, reg, value);
2448
2449         return true;
2450 }
2451
2452 static bool init_8e(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2453 {
2454         /* INIT_8E   opcode: 0x8E ('')
2455          *
2456          * offset      (8 bit): opcode
2457          *
2458          * The purpose of this opcode is unclear (being for nv50 cards), and
2459          * the literal functionality can be seen in the code below.
2460          *
2461          * A brief synopsis is that for each entry in a table pointed to by the
2462          * DCB table header, depending on the settings of various bits, various
2463          * other bits in registers 0xe100, 0xe104, and 0xe108, are set or
2464          * cleared.
2465          */
2466
2467         uint16_t dcbptr = le16_to_cpu(*(uint16_t *)&bios->data[0x36]);
2468         uint16_t init8etblptr = le16_to_cpu(*(uint16_t *)&bios->data[dcbptr + 10]);
2469         uint8_t headerlen = bios->data[init8etblptr + 1];
2470         uint8_t entries = bios->data[init8etblptr + 2];
2471         uint8_t recordlen = bios->data[init8etblptr + 3];
2472         int i;
2473
2474         if (!dcbptr) {
2475                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2476                            "No Display Configuration Block pointer found\n");
2477                 return false;
2478         }
2479         if (bios->data[dcbptr] != 0x40) {
2480                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2481                            "DCB table not version 4.0\n");
2482                 return false;
2483         }
2484         if (!init8etblptr) {
2485                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
2486                            "Invalid pointer to INIT_8E table\n");
2487                 return false;
2488         }
2489
2490         for (i = 0; i < entries; i++) {
2491                 uint32_t entry = le32_to_cpu(*(uint32_t *)&bios->data[init8etblptr + headerlen + recordlen * i]);
2492                 int shift = (entry & 0x1f) * 4;
2493                 uint32_t mask;
2494                 uint32_t reg = 0xe104;
2495                 uint32_t data;
2496
2497                 if ((entry & 0xff00) == 0xff00)
2498                         continue;
2499
2500                 if (shift >= 32) {
2501                         reg += 4;
2502                         shift -= 32;
2503                 }
2504                 shift %= 32;
2505
2506                 mask = ~(3 << shift);
2507                 if (entry & (1 << 24))
2508                         data = (entry >> 21);
2509                 else
2510                         data = (entry >> 19);
2511                 data = ((data & 3) ^ 2) << shift;
2512
2513                 if (DEBUGLEVEL >= 6)
2514                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2515                                    "0x%04X: Entry: 0x%08X, Reg: 0x%08X, Shift: 0x%02X, Mask: 0x%08X, Data: 0x%08X\n",
2516                                    offset, entry, reg, shift, mask, data);
2517
2518                 nv32_wr(pScrn, reg, (nv32_rd(pScrn, reg) & mask) | data);
2519
2520                 reg = 0xe100;
2521                 shift = entry & 0x1f;
2522
2523                 mask = ~(1 << 16 | 1);
2524                 mask = mask << shift | mask >> (32 - shift);
2525                 data = 0;
2526                 if ((entry & (3 << 25)) == (1 << 25))
2527                         data |= 1;
2528                 if ((entry & (3 << 25)) == (2 << 25))
2529                         data |= 0x10000;
2530                 data <<= shift;
2531
2532                 if (DEBUGLEVEL >= 6)
2533                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2534                                    "0x%04X: Entry: 0x%08X, Reg: 0x%08X, Shift: 0x%02X, Mask: 0x%08X, Data: 0x%08X\n",
2535                                    offset, entry, reg, shift, mask, data);
2536
2537                 nv32_wr(pScrn, reg, (nv32_rd(pScrn, reg) & mask) | data);
2538         }
2539
2540         return true;
2541 }
2542
2543 /* hack to avoid moving the itbl_entry array before this function */
2544 int init_ram_restrict_zm_reg_group_blocklen = 0;
2545
2546 static bool init_ram_restrict_zm_reg_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2547 {
2548         /* INIT_RAM_RESTRICT_ZM_REG_GROUP   opcode: 0x8F ('')
2549          *
2550          * offset      (8  bit): opcode
2551          * offset + 1  (32 bit): reg
2552          * offset + 5  (8  bit): regincrement
2553          * offset + 6  (8  bit): count
2554          * offset + 7  (32 bit): value 1,1
2555          * ...
2556          *
2557          * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
2558          * ram_restrict_table_ptr. The value read from here is 'n', and
2559          * "value 1,n" gets written to "reg". This repeats "count" times and on
2560          * each iteration 'm', "reg" increases by "regincrement" and
2561          * "value m,n" is used. The extent of n is limited by a number read
2562          * from the 'M' BIT table, herein called "blocklen"
2563          */
2564
2565         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2566         uint8_t regincrement = bios->data[offset + 5];
2567         uint8_t count = bios->data[offset + 6];
2568         uint32_t strap_ramcfg, data;
2569         uint16_t blocklen;
2570         uint8_t index;
2571         int i;
2572
2573         /* previously set by 'M' BIT table */
2574         blocklen = init_ram_restrict_zm_reg_group_blocklen;
2575
2576         if (!iexec->execute)
2577                 return true;
2578
2579         if (!blocklen) {
2580                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2581                            "0x%04X: Zero block length - has the M table been parsed?\n", offset);
2582                 return false;
2583         }
2584
2585         strap_ramcfg = (nv32_rd(pScrn, NV_PEXTDEV_BOOT_0) >> 2) & 0xf;
2586         index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
2587
2588         if (DEBUGLEVEL >= 6)
2589                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2590                            "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
2591                            offset, reg, regincrement, count, strap_ramcfg, index);
2592
2593         for (i = 0; i < count; i++) {
2594                 data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7 + index * 4 + blocklen * i])));
2595
2596                 nv32_wr(pScrn, reg, data);
2597
2598                 reg += regincrement;
2599         }
2600
2601         return true;
2602 }
2603
2604 static bool init_copy_zm_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2605 {
2606         /* INIT_COPY_ZM_REG   opcode: 0x90 ('')
2607          *
2608          * offset      (8  bit): opcode
2609          * offset + 1  (32 bit): src reg
2610          * offset + 5  (32 bit): dst reg
2611          *
2612          * Put contents of "src reg" into "dst reg"
2613          */
2614
2615         uint32_t srcreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2616         uint32_t dstreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
2617
2618         if (!iexec->execute)
2619                 return true;
2620
2621         nv32_wr(pScrn, dstreg, nv32_rd(pScrn, srcreg));
2622
2623         return true;
2624 }
2625
2626 static bool init_zm_reg_group_addr_latched(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2627 {
2628         /* INIT_ZM_REG_GROUP_ADDRESS_LATCHED   opcode: 0x91 ('')
2629          *
2630          * offset      (8  bit): opcode
2631          * offset + 1  (32 bit): dst reg
2632          * offset + 5  (8  bit): count
2633          * offset + 6  (32 bit): data 1
2634          * ...
2635          *
2636          * For each of "count" values write "data n" to "dst reg"
2637          */
2638
2639         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
2640         uint8_t count = bios->data[offset + 5];
2641         int i;
2642
2643         if (!iexec->execute)
2644                 return true;
2645
2646         for (i = 0; i < count; i++) {
2647                 uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 6 + 4 * i])));
2648                 nv32_wr(pScrn, reg, data);
2649         }
2650
2651         return true;
2652 }
2653
2654 static bool init_reserved(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
2655 {
2656         /* INIT_RESERVED   opcode: 0x92 ('')
2657          *
2658          * offset      (8 bit): opcode
2659          *
2660          * Seemingly does nothing
2661          */
2662
2663         return true;
2664 }
2665
2666 static init_tbl_entry_t itbl_entry[] = {
2667         /* command name                       , id  , length  , offset  , mult    , command handler                 */
2668 //      { "INIT_PROG"                         , 0x31, 15      , 10      , 4       , init_prog                       },
2669         { "INIT_IO_RESTRICT_PROG"             , 0x32, 11      , 6       , 4       , init_io_restrict_prog           },
2670         { "INIT_REPEAT"                       , 0x33, 2       , 0       , 0       , init_repeat                     },
2671         { "INIT_IO_RESTRICT_PLL"              , 0x34, 12      , 7       , 2       , init_io_restrict_pll            },
2672         { "INIT_END_REPEAT"                   , 0x36, 1       , 0       , 0       , init_end_repeat                 },
2673         { "INIT_COPY"                         , 0x37, 11      , 0       , 0       , init_copy                       },
2674         { "INIT_NOT"                          , 0x38, 1       , 0       , 0       , init_not                        },
2675         { "INIT_IO_FLAG_CONDITION"            , 0x39, 2       , 0       , 0       , init_io_flag_condition          },
2676         { "INIT_INDEX_ADDRESS_LATCHED"        , 0x49, 18      , 17      , 2       , init_idx_addr_latched           },
2677         { "INIT_IO_RESTRICT_PLL2"             , 0x4A, 11      , 6       , 4       , init_io_restrict_pll2           },
2678         { "INIT_PLL2"                         , 0x4B, 9       , 0       , 0       , init_pll2                       },
2679 /*      { "INIT_I2C_BYTE"                     , 0x4C, x       , x       , x       , init_i2c_byte                   }, */
2680 /*      { "INIT_ZM_I2C_BYTE"                  , 0x4D, x       , x       , x       , init_zm_i2c_byte                }, */
2681 /*      { "INIT_ZM_I2C"                       , 0x4E, x       , x       , x       , init_zm_i2c                     }, */
2682         { "INIT_TMDS"                         , 0x4F, 5       , 0       , 0       , init_tmds                       },
2683         { "INIT_ZM_TMDS_GROUP"                , 0x50, 3       , 2       , 2       , init_zm_tmds_group              },
2684         { "INIT_CR_INDEX_ADDRESS_LATCHED"     , 0x51, 5       , 4       , 1       , init_cr_idx_adr_latch           },
2685         { "INIT_CR"                           , 0x52, 4       , 0       , 0       , init_cr                         },
2686         { "INIT_ZM_CR"                        , 0x53, 3       , 0       , 0       , init_zm_cr                      },
2687         { "INIT_ZM_CR_GROUP"                  , 0x54, 2       , 1       , 2       , init_zm_cr_group                },
2688         { "INIT_CONDITION_TIME"               , 0x56, 3       , 0       , 0       , init_condition_time             },
2689         { "INIT_ZM_REG_SEQUENCE"              , 0x58, 6       , 5       , 4       , init_zm_reg_sequence            },
2690 //      { "INIT_INDIRECT_REG"                 , 0x5A, 7       , 0       , 0       , init_indirect_reg               },
2691         { "INIT_SUB_DIRECT"                   , 0x5B, 3       , 0       , 0       , init_sub_direct                 },
2692         { "INIT_COPY_NV_REG"                  , 0x5F, 22      , 0       , 0       , init_copy_nv_reg                },
2693         { "INIT_ZM_INDEX_IO"                  , 0x62, 5       , 0       , 0       , init_zm_index_io                },
2694         { "INIT_COMPUTE_MEM"                  , 0x63, 1       , 0       , 0       , init_compute_mem                },
2695         { "INIT_RESET"                        , 0x65, 13      , 0       , 0       , init_reset                      },
2696         { "INIT_CONFIGURE_MEM"                , 0x66, 1       , 0       , 0       , init_configure_mem              },
2697         { "INIT_CONFIGURE_CLK"                , 0x67, 1       , 0       , 0       , init_configure_clk              },
2698         { "INIT_CONFIGURE_PREINIT"            , 0x68, 1       , 0       , 0       , init_configure_preinit          },
2699         { "INIT_IO"                           , 0x69, 5       , 0       , 0       , init_io                         },
2700         { "INIT_SUB"                          , 0x6B, 2       , 0       , 0       , init_sub                        },
2701         { "INIT_RAM_CONDITION"                , 0x6D, 3       , 0       , 0       , init_ram_condition              },
2702         { "INIT_NV_REG"                       , 0x6E, 13      , 0       , 0       , init_nv_reg                     },
2703         { "INIT_MACRO"                        , 0x6F, 2       , 0       , 0       , init_macro                      },
2704         { "INIT_DONE"                         , 0x71, 1       , 0       , 0       , init_done                       },
2705         { "INIT_RESUME"                       , 0x72, 1       , 0       , 0       , init_resume                     },
2706 //      { "INIT_RAM_CONDITION2"               , 0x73, 9       , 0       , 0       , init_ram_condition2             },
2707         { "INIT_TIME"                         , 0x74, 3       , 0       , 0       , init_time                       },
2708         { "INIT_CONDITION"                    , 0x75, 2       , 0       , 0       , init_condition                  },
2709 /*      { "INIT_IO_CONDITION"                 , 0x76, x       , x       , x       , init_io_condition               }, */
2710         { "INIT_INDEX_IO"                     , 0x78, 6       , 0       , 0       , init_index_io                   },
2711         { "INIT_PLL"                          , 0x79, 7       , 0       , 0       , init_pll                        },
2712         { "INIT_ZM_REG"                       , 0x7A, 9       , 0       , 0       , init_zm_reg                     },
2713         { "INIT_8E"                           , 0x8E, 1       , 0       , 0       , init_8e                         },
2714         /* INIT_RAM_RESTRICT_ZM_REG_GROUP's mult is loaded by M table in BIT */
2715         { "INIT_RAM_RESTRICT_ZM_REG_GROUP"    , 0x8F, 7       , 6       , 0       , init_ram_restrict_zm_reg_group  },
2716         { "INIT_COPY_ZM_REG"                  , 0x90, 9       , 0       , 0       , init_copy_zm_reg                },
2717         { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, 6       , 5       , 4       , init_zm_reg_group_addr_latched  },
2718         { "INIT_RESERVED"                     , 0x92, 1       , 0       , 0       , init_reserved                   },
2719         { 0                                   , 0   , 0       , 0       , 0       , 0                               }
2720 };
2721
2722 static unsigned int get_init_table_entry_length(bios_t *bios, unsigned int offset, int i)
2723 {
2724         /* Calculates the length of a given init table entry. */
2725         return itbl_entry[i].length + bios->data[offset + itbl_entry[i].length_offset]*itbl_entry[i].length_multiplier;
2726 }
2727
2728 static void parse_init_table(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset, init_exec_t *iexec)
2729 {
2730         /* Parses all commands in a init table. */
2731
2732         /* We start out executing all commands found in the
2733          * init table. Some op codes may change the status
2734          * of this variable to SKIP, which will cause
2735          * the following op codes to perform no operation until
2736          * the value is changed back to EXECUTE.
2737          */
2738         unsigned char id;
2739         int i;
2740
2741         int count=0;
2742         /* Loop until INIT_DONE causes us to break out of the loop
2743          * (or until offset > bios length just in case... )
2744          * (and no more than 10000 iterations just in case... ) */
2745         while ((offset < bios->length) && (count++ < 10000)) {
2746                 id = bios->data[offset];
2747
2748                 /* Find matching id in itbl_entry */
2749                 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
2750                         ;
2751
2752                 if (itbl_entry[i].name) {
2753                         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ (0x%02X) - %s ]\n",
2754                                    offset, itbl_entry[i].id, itbl_entry[i].name);
2755
2756                         /* execute eventual command handler */
2757                         if (itbl_entry[i].handler)
2758                                 if (!(*itbl_entry[i].handler)(pScrn, bios, offset, iexec))
2759                                         break;
2760                 } else {
2761                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2762                                    "0x%04X: Init table command not found: 0x%02X\n", offset, id);
2763                         break;
2764                 }
2765
2766                 /* Add the offset of the current command including all data
2767                  * of that command. The offset will then be pointing on the
2768                  * next op code.
2769                  */
2770                 offset += get_init_table_entry_length(bios, offset, i);
2771         }
2772 }
2773
2774 static void parse_init_tables(ScrnInfoPtr pScrn, bios_t *bios)
2775 {
2776         /* Loops and calls parse_init_table() for each present table. */
2777
2778         int i = 0;
2779         uint16_t table;
2780         init_exec_t iexec = {true, false};
2781
2782         while ((table = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + i]))))) {
2783                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2784                            "0x%04X: Parsing init table %d\n", table, i / 2);
2785                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2786                            "0x%04X: ------ EXECUTING FOLLOWING COMMANDS ------\n", table);
2787
2788                 parse_init_table(pScrn, bios, table, &iexec);
2789                 i += 2;
2790         }
2791 }
2792
2793 static void link_head_and_output(ScrnInfoPtr pScrn, struct dcb_entry *dcbent, int head)
2794 {
2795         /* The BIOS scripts don't do this for us, sadly
2796          * Luckily we do know the values ;-)
2797          *
2798          * head < 0 indicates we wish to force a setting with the overrideval
2799          * (for VT restore etc.)
2800          */
2801
2802         NVPtr pNv = NVPTR(pScrn);
2803         int ramdac = (dcbent->or & OUTPUT_C) >> 2;
2804         uint8_t tmds04 = 0x80;
2805
2806         if (head != ramdac)
2807                 tmds04 = 0x88;
2808
2809         if (dcbent->type == OUTPUT_LVDS)
2810                 tmds04 |= 0x01;
2811
2812         nv_write_tmds(pNv, dcbent->or, 0, 0x04, tmds04);
2813
2814         if (dcbent->type == OUTPUT_LVDS && pNv->VBIOS.fp.dual_link)
2815                 nv_write_tmds(pNv, dcbent->or, 1, 0x04, tmds04 ^ 0x08);
2816 }
2817
2818 static uint16_t clkcmptable(bios_t *bios, uint16_t clktable, int pxclk)
2819 {
2820         int compare_record_len, i = 0;
2821         uint16_t compareclk, scriptptr = 0;
2822
2823         if (bios->major_version < 5) /* pre BIT */
2824                 compare_record_len = 3;
2825         else
2826                 compare_record_len = 4;
2827
2828         do {
2829                 compareclk = le16_to_cpu(*((uint16_t *)&bios->data[clktable + compare_record_len * i]));
2830                 if (pxclk >= compareclk * 10) {
2831                         if (bios->major_version < 5) {
2832                                 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
2833                                 scriptptr = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + tmdssub * 2])));
2834                         } else
2835                                 scriptptr = le16_to_cpu(*((uint16_t *)&bios->data[clktable + 2 + compare_record_len * i]));
2836                         break;
2837                 }
2838                 i++;
2839         } while (compareclk);
2840
2841         return scriptptr;
2842 }
2843
2844 static void rundigitaloutscript(ScrnInfoPtr pScrn, uint16_t scriptptr, struct dcb_entry *dcbent, int head)
2845 {
2846         bios_t *bios = &NVPTR(pScrn)->VBIOS;
2847         init_exec_t iexec = {true, false};
2848
2849         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: Parsing digital output script table\n", scriptptr);
2850         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_OWNER,
2851                        head ? NV_VGA_CRTCX_OWNER_HEADB : NV_VGA_CRTCX_OWNER_HEADA);
2852         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_57, 0);
2853         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_58, dcbent->index);
2854         parse_init_table(pScrn, bios, scriptptr, &iexec);
2855
2856         link_head_and_output(pScrn, dcbent, head);
2857 }
2858
2859 static void call_lvds_manufacturer_script(ScrnInfoPtr pScrn, struct dcb_entry *dcbent, int head, enum LVDS_script script)
2860 {
2861         NVPtr pNv = NVPTR(pScrn);
2862         bios_t *bios = &pNv->VBIOS;
2863         uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & OUTPUT_C ? 1 : 0);
2864         uint16_t scriptofs = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + sub * 2])));
2865
2866         if (!bios->fp.xlated_entry || !sub || !scriptofs)
2867                 return;
2868
2869         rundigitaloutscript(pScrn, scriptofs, dcbent, head);
2870
2871         if (script == LVDS_PANEL_OFF)
2872                 /* off-on delay in ms */
2873                 usleep(le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.xlated_entry + 7]));
2874 #ifdef __powerpc__
2875         /* Powerbook specific quirks */
2876         if (script == LVDS_RESET && ((pNv->Chipset & 0xffff) == 0x0179 || (pNv->Chipset & 0xffff) == 0x0329))
2877                 nv_write_tmds(pNv, dcbent->or, 0, 0x02, 0x72);
2878         if ((pNv->Chipset & 0xffff) == 0x0179 || (pNv->Chipset & 0xffff) == 0x0189 || (pNv->Chipset & 0xffff) == 0x0329) {
2879                 if (script == LVDS_PANEL_ON) {
2880                         nv32_wr(pScrn, NV_PBUS_DEBUG_DUALHEAD_CTL, nv32_rd(pScrn, NV_PBUS_DEBUG_DUALHEAD_CTL) | (1 << 31));
2881                         nv32_wr(pScrn, NV_CRTC_GPIO_EXT, nv32_rd(pScrn, NV_CRTC_GPIO_EXT) | 1);
2882                 }
2883                 if (script == LVDS_PANEL_OFF) {
2884                         nv32_wr(pScrn, NV_PBUS_DEBUG_DUALHEAD_CTL, nv32_rd(pScrn, NV_PBUS_DEBUG_DUALHEAD_CTL) & ~(1 << 31));
2885                         nv32_wr(pScrn, NV_CRTC_GPIO_EXT, nv32_rd(pScrn, NV_CRTC_GPIO_EXT) & ~3);
2886                 }
2887         }
2888 #endif
2889 }
2890
2891 static void run_lvds_table(ScrnInfoPtr pScrn, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
2892 {
2893         /* The BIT LVDS table's header has the information to setup the
2894          * necessary registers. Following the standard 4 byte header are:
2895          * A bitmask byte and a dual-link transition pxclk value for use in
2896          * selecting the init script when not using straps; 4 script pointers
2897          * for panel power, selected by output and on/off; and 8 table pointers
2898          * for panel init, the needed one determined by output, and bits in the
2899          * conf byte. These tables are similar to the TMDS tables, consisting
2900          * of a list of pxclks and script pointers.
2901          */
2902
2903         NVPtr pNv = NVPTR(pScrn);
2904         bios_t *bios = &pNv->VBIOS;
2905         unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
2906         uint16_t scriptptr = 0, clktable;
2907         uint8_t clktableptr = 0;
2908
2909         /* for now we assume version 3.0 table - g80 support will need some changes */
2910
2911         switch (script) {
2912         case LVDS_INIT:
2913                 return;
2914         case LVDS_BACKLIGHT_ON:
2915         case LVDS_PANEL_ON:
2916                 scriptptr = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
2917                 break;
2918         case LVDS_BACKLIGHT_OFF:
2919         case LVDS_PANEL_OFF:
2920                 scriptptr = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
2921                 break;
2922         case LVDS_RESET:
2923                 if (dcbent->lvdsconf.use_straps_for_mode) {
2924                         if (bios->fp.dual_link)
2925                                 clktableptr += 2;
2926                         if (bios->fp.BITbit1)
2927                                 clktableptr++;
2928                 } else {
2929                         uint8_t fallback = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
2930                         int fallbackcmpval = (dcbent->or == 4) ? 4 : 1;
2931
2932                         if (bios->fp.dual_link) {
2933                                 clktableptr += 2;
2934                                 fallbackcmpval *= 2;
2935                         }
2936                         if (fallbackcmpval & fallback)
2937                                 clktableptr++;
2938                 }
2939
2940                 /* adding outputset * 8 may not be correct */
2941                 clktable = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 15 + clktableptr * 2 + outputset * 8]);
2942                 if (!clktable) {
2943                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pixel clock comparison table not found\n");
2944                         return;
2945                 }
2946                 scriptptr = clkcmptable(bios, clktable, pxclk);
2947         }
2948
2949         if (!scriptptr) {
2950                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "LVDS output init script not found\n");
2951                 return;
2952         }
2953         rundigitaloutscript(pScrn, scriptptr, dcbent, head);
2954 }
2955
2956 void call_lvds_script(ScrnInfoPtr pScrn, struct dcb_entry *dcbent, int head, enum LVDS_script script, int pxclk)
2957 {
2958         /* LVDS operations are multiplexed in an effort to present a single API
2959          * which works with two vastly differing underlying structures.
2960          * This acts as the demux
2961          */
2962
2963         bios_t *bios = &NVPTR(pScrn)->VBIOS;
2964         uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
2965         uint32_t sel_clk_binding;
2966         static int last_invoc = 0;
2967
2968         if (last_invoc == (script << 1 | head) || !lvds_ver)
2969                 return;
2970
2971         if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
2972                 call_lvds_script(pScrn, dcbent, head, LVDS_RESET, pxclk);
2973         if (script == LVDS_RESET && bios->fp.power_off_for_reset)
2974                 call_lvds_script(pScrn, dcbent, head, LVDS_PANEL_OFF, pxclk);
2975
2976         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Calling LVDS script %d:\n", script);
2977
2978         /* don't let script change pll->head binding */
2979         sel_clk_binding = nv32_rd(pScrn, NV_RAMDAC_SEL_CLK) & 0x50000;
2980
2981         if (lvds_ver < 0x30)
2982                 call_lvds_manufacturer_script(pScrn, dcbent, head, script);
2983         else
2984                 run_lvds_table(pScrn, dcbent, head, script, pxclk);
2985
2986         last_invoc = (script << 1 | head);
2987
2988         nv32_wr(pScrn, NV_RAMDAC_SEL_CLK, (nv32_rd(pScrn, NV_RAMDAC_SEL_CLK) & ~0x50000) | sel_clk_binding);
2989         /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
2990         nv32_wr(pScrn, NV_PBUS_POWERCTRL_2, 0);
2991 }
2992
2993 struct fppointers {
2994         uint16_t fptablepointer;
2995         uint16_t fpxlatetableptr;
2996         int xlatwidth;
2997 };
2998
2999 struct lvdstableheader {
3000         uint8_t lvds_ver, headerlen, recordlen;
3001 };
3002
3003 static void parse_lvds_manufacturer_table_header(ScrnInfoPtr pScrn, bios_t *bios, struct lvdstableheader *lth)
3004 {
3005         /* BMP version (0xa) LVDS table has a simple header of version and
3006          * record length. The BIT LVDS table has the typical BIT table header:
3007          * version byte, header length byte, record length byte, and a byte for
3008          * the maximum number of records that can be held in the table */
3009
3010         uint8_t lvds_ver, headerlen, recordlen;
3011
3012         memset(lth, 0, sizeof(struct lvdstableheader));
3013
3014         if (bios->fp.lvdsmanufacturerpointer == 0x0) {
3015                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3016                            "Pointer to LVDS manufacturer table invalid\n");
3017                 return;
3018         }
3019
3020         lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
3021
3022         switch (lvds_ver) {
3023         case 0x0a:      /* pre NV40 */
3024                 headerlen = 2;
3025                 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3026                 break;
3027         case 0x30:      /* NV4x */
3028                 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3029                 if (headerlen < 0x1f) {
3030                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3031                                    "LVDS table header not understood\n");
3032                         return;
3033                 }
3034                 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3035                 break;
3036         case 0x40:      /* G80/G90 */
3037                 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
3038                 if (headerlen < 0x7) {
3039                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3040                                    "LVDS table header not understood\n");
3041                         return;
3042                 }
3043                 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
3044                 break;
3045         default:
3046                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3047                            "LVDS table revision %d.%d not currently supported\n",
3048                            lvds_ver >> 4, lvds_ver & 0xf);
3049                 return;
3050         }
3051
3052         lth->lvds_ver = lvds_ver;
3053         lth->headerlen = headerlen;
3054         lth->recordlen = recordlen;
3055 }
3056
3057 static void parse_fp_mode_table(ScrnInfoPtr pScrn, bios_t *bios, struct fppointers *fpp)
3058 {
3059         uint8_t *fptable;
3060         uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
3061         int ofs;
3062         struct lvdstableheader lth;
3063         uint16_t modeofs;
3064         DisplayModePtr mode;
3065
3066         if (fpp->fptablepointer == 0x0) {
3067                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3068                            "Pointer to flat panel table invalid\n");
3069                 return;
3070         }
3071
3072         fptable = &bios->data[fpp->fptablepointer];
3073         fptable_ver = fptable[0];
3074
3075         switch (fptable_ver) {
3076         /* BMP version 0x5.0x11 BIOSen have version 1 like tables, but no version field,
3077          * and miss one of the spread spectrum/PWM bytes.
3078          * This could affect early GF2Go parts (not seen any appropriate ROMs though).
3079          * Here we assume that a version of 0x05 matches this case (combining with a
3080          * BMP version check would be better), as the common case for the panel type
3081          * field is 0x0005, and that is in fact what we are reading the first byte of. */
3082         case 0x05:      /* some NV10, 11, 15, 16 */
3083                 recordlen = 42;
3084                 ofs = 6;
3085                 break;
3086         case 0x10:      /* some NV15/16, and NV11+ */
3087                 recordlen = 44;
3088                 ofs = 7;
3089                 break;
3090         case 0x20:      /* NV40+ */
3091                 headerlen = fptable[1];
3092                 recordlen = fptable[2];
3093                 fpentries = fptable[3];
3094                 /* fptable[4] is the minimum RAMDAC_FP_HCRTC->RAMDAC_FP_HSYNC_START gap */
3095                 bios->digital_min_front_porch = fptable[4];
3096                 ofs = 0;
3097                 break;
3098         default:
3099                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3100                            "FP table revision %d.%d not currently supported\n",
3101                            fptable_ver >> 4, fptable_ver & 0xf);
3102                 return;
3103         }
3104
3105         /* non mobile only needs to set digital_min_front_porch */
3106         if (!(bios->feature_byte & FEATURE_MOBILE))
3107                 return;
3108
3109         parse_lvds_manufacturer_table_header(pScrn, bios, &lth);
3110
3111         switch (lth.lvds_ver) {
3112         case 0x0a:
3113                 /* make sure to match the 0xff strapping check below */
3114                 if ((bios->fp.strapping & 0xf) == 0xf)
3115                         bios->data[fpp->fpxlatetableptr + 0xf] = 0xf;
3116                 break;
3117         case 0x30:
3118         case 0x40:
3119                 fpp->fpxlatetableptr = bios->fp.lvdsmanufacturerpointer + lth.headerlen + 1;
3120                 fpp->xlatwidth = lth.recordlen;
3121         }
3122         if (fpp->fpxlatetableptr == 0x0) {
3123                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3124                            "Pointer to flat panel xlat table invalid\n");
3125                 return;
3126         }
3127
3128         /* Query all modes and find one with a matching clock. */
3129         /* Note that this only serves as a backup solution if ddc fails. */
3130         if (lth.lvds_ver == 0x40) {
3131                 uint32_t clock, needed_clock;
3132                 int i, index = 0xF, matches = 0;
3133                 needed_clock = nv32_rd(pScrn, 0x00616404) & 0xFFFFF;
3134                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "LVDS clock seems to be %d KHz.\n", needed_clock);
3135
3136                 for (i = 0; i < fpentries; i++) {
3137                         modeofs = headerlen + recordlen * i;
3138                         clock = le16_to_cpu(*(uint16_t *)&fptable[modeofs]) * 10;
3139                         if (clock == needed_clock) {
3140                                 matches++;
3141                                 index = i;
3142                         }
3143                 }
3144
3145                 if (matches == 1)
3146                         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Found a mode with matching clock\n");
3147                 else
3148                         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Found %d modes, this is not useful\n", matches);
3149
3150                 if (matches != 1)
3151                         index = 0xF;
3152
3153                 fpindex = bios->data[fpp->fpxlatetableptr + index * fpp->xlatwidth];
3154                 bios->fp.strapping = ((fpindex & 0xF) << 4) | (fpindex & 0xF);
3155         } else {
3156                 fpindex = bios->data[fpp->fpxlatetableptr + bios->fp.strapping * fpp->xlatwidth];
3157                 bios->fp.strapping |= fpindex << 4;
3158         }
3159
3160         if (fpindex > fpentries) {
3161                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3162                            "Bad flat panel table index\n");
3163                 return;
3164         }
3165
3166         /* reserved values - means that ddc or hard coded edid should be used */
3167         if (bios->fp.strapping == 0xff) {
3168                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Ignoring FP table\n");
3169                 return;
3170         }
3171
3172         if (!(mode = xcalloc(1, sizeof(DisplayModeRec))))
3173                 return;
3174
3175         modeofs = headerlen + recordlen * fpindex + ofs;
3176         mode->Clock = le16_to_cpu(*(uint16_t *)&fptable[modeofs]) * 10;
3177         mode->HDisplay = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 4] + 1);
3178         mode->HSyncStart = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 10] + 1);
3179         mode->HSyncEnd = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 12] + 1);
3180         mode->HTotal = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 14] + 1);
3181         mode->VDisplay = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 18] + 1);
3182         mode->VSyncStart = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 24] + 1);
3183         mode->VSyncEnd = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 26] + 1);
3184         mode->VTotal = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 28] + 1);
3185         mode->Flags |= (fptable[modeofs + 30] & 0x10) ? V_PHSYNC : V_NHSYNC;
3186         mode->Flags |= (fptable[modeofs + 30] & 0x1) ? V_PVSYNC : V_NVSYNC;
3187
3188         /* for version 1.0:
3189          * bytes 1-2 are "panel type", including bits on whether Colour/mono, single/dual link, and type (TFT etc.)
3190          * bytes 3-6 are bits per colour in RGBX
3191          *  9-10 is HActive
3192          * 11-12 is HDispEnd
3193          * 13-14 is HValid Start
3194          * 15-16 is HValid End
3195          * bytes 38-39 relate to spread spectrum settings
3196          * bytes 40-43 are something to do with PWM */
3197
3198         mode->prev = mode->next = NULL;
3199         mode->status = MODE_OK;
3200         mode->type = M_T_DRIVER | M_T_PREFERRED;
3201         xf86SetModeDefaultName(mode);
3202
3203 //      if (XF86_CRTC_CONFIG_PTR(pScrn)->debug_modes) {
3204                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3205                            "Found flat panel mode in BIOS tables:\n");
3206                 xf86PrintModeline(pScrn->scrnIndex, mode);
3207 //      }
3208
3209         bios->fp.native_mode = mode;
3210 }
3211
3212 void parse_lvds_manufacturer_table(ScrnInfoPtr pScrn, bios_t *bios, int pxclk)
3213 {
3214         /* The LVDS table header is (mostly) described in
3215          * parse_lvds_manufacturer_table_header(): the BIT header additionally
3216          * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
3217          * straps are not being used for the panel, this specifies the frequency
3218          * at which modes should be set up in the dual link style.
3219          *
3220          * Following the header, the BMP (ver 0xa) table has several records,
3221          * indexed by a seperate xlat table, indexed in turn by the fp strap in
3222          * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
3223          * numbers for use by INIT_SUB which controlled panel init and power,
3224          * and finally a dword of ms to sleep between power off and on
3225          * operations.
3226          *
3227          * In the BIT versions, the table following the header serves as an
3228          * integrated config and xlat table: the records in the table are
3229          * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
3230          * two bytes - the first as a config byte, the second for indexing the
3231          * fp mode table pointed to by the BIT 'D' table
3232          *
3233          * Due to the stage at which DDC is used in X's DDX design, the EDID res
3234          * for a panel isn't known at init, so the tests against the pixel clock
3235          * in the EDID case for selection of the correct table entry and setting
3236          * of the dual link flag cannot be done until later - this function may
3237          * be called at runtime with a non-zero pxclk argument to perform these
3238          * tests.
3239          */
3240
3241         unsigned int lvdsmanufacturerindex = 0;
3242         struct lvdstableheader lth;
3243         uint16_t lvdsofs;
3244
3245         parse_lvds_manufacturer_table_header(pScrn, bios, &lth);
3246
3247         switch (lth.lvds_ver) {
3248         case 0:         /* header parsing failed */
3249                 return;
3250         case 0x0a:      /* pre NV40 */
3251                 lvdsmanufacturerindex = bios->data[bios->fp.fpxlatemanufacturertableptr + (bios->fp.strapping & 0xf)];
3252
3253                 /* we're done if this isn't the EDID panel case */
3254                 if (pxclk == 0 || (bios->fp.strapping & 0xf) != 0xf)
3255                         break;
3256
3257                 /* change in behaviour guessed at nv30; see datapoints below */
3258                 if (bios->chip_version < 0x30) {
3259                         /* nv17 behaviour */
3260                         lvdsmanufacturerindex = bios->fp.if_is_24bit ? 2 : 0;
3261                         if (pxclk >= bios->fp.duallink_transition_clk)
3262                                 lvdsmanufacturerindex++;
3263                 } else {
3264                         /* nv31, nv34 behaviour */
3265                         lvdsmanufacturerindex = 0;
3266                         if (pxclk >= bios->fp.duallink_transition_clk)
3267                                 lvdsmanufacturerindex = 2;
3268                         if (pxclk >= 140000)
3269                                 lvdsmanufacturerindex = 3;
3270                 }
3271
3272                 /* nvidia set the high nibble of (cr57=f, cr58) to
3273                  * lvdsmanufacturerindex in this case; we don't */
3274                 break;
3275         case 0x30:      /* NV4x */
3276                 lvdsmanufacturerindex = bios->fp.strapping & 0xf;
3277                 break;
3278         case 0x40:      /* G80/G90 */
3279                 lvdsmanufacturerindex = bios->fp.strapping & 0xf;
3280                 break;
3281         default:
3282                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3283                            "LVDS table revision not currently supported\n");
3284                 return;
3285         }
3286
3287         lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
3288         switch (lth.lvds_ver) {
3289         case 0x0a:
3290                 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
3291                 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
3292                 bios->fp.dual_link = bios->data[lvdsofs] & 4;
3293                 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
3294                 bios->fp.if_is_24bit = bios->data[lvdsofs] & 16;
3295                 break;
3296         case 0x30:
3297                 /* My money would be on there being a 24 bit interface bit in this table,
3298                  * but I have no example of a laptop bios with a 24 bit panel to confirm that.
3299                  * Hence we shout loudly if any bit other than bit 0 is set (I've not even
3300                  * seen bit 1)
3301                  */
3302                 if (bios->data[lvdsofs] > 1)
3303                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3304                                    "You have a very unusual laptop display; please report it\n");
3305                 /* no sign of the "power off for reset" or "reset for panel on" bits, but it's safer to assume we should */
3306                 bios->fp.power_off_for_reset = true;
3307                 bios->fp.reset_after_pclk_change = true;
3308                 bios->fp.dual_link = bios->data[lvdsofs] & 1;
3309                 bios->fp.BITbit1 = bios->data[lvdsofs] & 2;
3310                 bios->fp.duallink_transition_clk = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
3311                 break;
3312         case 0x40:
3313                 /* fairly sure, but not 100% */
3314                 bios->fp.dual_link = bios->data[lvdsofs] & 1;
3315                 bios->fp.duallink_transition_clk = le16_to_cpu(*(uint16_t *)&bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
3316                 break;
3317         }
3318
3319         /* set dual_link flag for EDID case */
3320         if ((bios->fp.strapping & 0xf) == 0xf && pxclk) {
3321                 if (pxclk >= bios->fp.duallink_transition_clk)
3322                         bios->fp.dual_link = true;
3323                 else
3324                         bios->fp.dual_link = false;
3325         }
3326 }
3327
3328 void run_tmds_table(ScrnInfoPtr pScrn, struct dcb_entry *dcbent, int head, int pxclk)
3329 {
3330         /* the pxclk parameter is in kHz
3331          *
3332          * This runs the TMDS regs setting code found on BIT bios cards
3333          *
3334          * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
3335          * ffs(or) == 3, use the second.
3336          */
3337
3338         NVPtr pNv = NVPTR(pScrn);
3339         bios_t *bios = &pNv->VBIOS;
3340         uint16_t clktable = 0, scriptptr;
3341         uint32_t sel_clk_binding;
3342
3343         if (dcbent->location != LOC_ON_CHIP)
3344                 return;
3345
3346         switch (ffs(dcbent->or)) {
3347         case 1:
3348                 clktable = bios->tmds.output0_script_ptr;
3349                 break;
3350         case 2:
3351         case 3:
3352                 clktable = bios->tmds.output1_script_ptr;
3353                 break;
3354         }
3355
3356         if (!clktable) {
3357                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pixel clock comparison table not found\n");
3358                 return;
3359         }
3360
3361         scriptptr = clkcmptable(bios, clktable, pxclk);
3362
3363         if (!scriptptr) {
3364                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "TMDS output init script not found\n");
3365                 return;
3366         }
3367
3368         /* don't let script change pll->head binding */
3369         sel_clk_binding = nv32_rd(pScrn, NV_RAMDAC_SEL_CLK) & 0x50000;
3370         rundigitaloutscript(pScrn, scriptptr, dcbent, head);
3371         nv32_wr(pScrn, NV_RAMDAC_SEL_CLK, (nv32_rd(pScrn, NV_RAMDAC_SEL_CLK) & ~0x50000) | sel_clk_binding);
3372 }
3373
3374 static int get_fp_strap(ScrnInfoPtr pScrn, bios_t *bios)
3375 {
3376         /* the fp strap is normally dictated by the "User Strap" in
3377          * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
3378          * Internal_Flags struct at 0x48 is set, the user strap gets overriden
3379          * by the PCI subsystem ID during POST, but not before the previous user
3380          * strap has been committed to CR58 for CR57=0xf on head A, which may be
3381          * read and used instead
3382          */
3383
3384         /* Now comes the G80/G90 story, i've only got one hint.
3385          * I can read back the clock freq from register 0x00616404.
3386          * So for the moment just write 0xF here.
3387          */
3388
3389         if (bios->chip_version >= 0x80)
3390                 return 0xF;
3391
3392         if (bios->major_version < 5 && bios->data[0x48] & 0x4)
3393                 return (NVReadVgaCrtc5758(NVPTR(pScrn), 0, 0xf) & 0xf);
3394
3395         return ((nv32_rd(pScrn, NV_PEXTDEV_BOOT_0) >> 16) & 0xf);
3396 }
3397
3398 static void parse_bios_version(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset)
3399 {
3400         /* offset + 0  (8 bits): Micro version
3401          * offset + 1  (8 bits): Minor version
3402          * offset + 2  (8 bits): Chip version
3403          * offset + 3  (8 bits): Major version
3404          */
3405
3406         bios->major_version = bios->data[offset + 3];
3407         bios->chip_version = bios->data[offset + 2];
3408         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Bios version %02x.%02x.%02x.%02x\n",
3409                    bios->data[offset + 3], bios->data[offset + 2],
3410                    bios->data[offset + 1], bios->data[offset]);
3411 }
3412
3413 bool get_pll_limits(ScrnInfoPtr pScrn, uint32_t limit_match, struct pll_lims *pll_lim)
3414 {
3415         /* PLL limits table
3416          *
3417          * Version 0x10: NV31
3418          * One byte header (version), one record of 24 bytes
3419          * Version 0x11: NV36 - Not implemented
3420          * Seems to have same record style as 0x10, but 3 records rather than 1
3421          * Version 0x20: Found on Geforce 6 cards
3422          * Trivial 4 byte BIT header. 31 (0x1f) byte record length
3423          * Version 0x21: Found on Geforce 7, 8 and some Geforce 6 cards
3424          * 5 byte header, fifth byte of unknown purpose. 35 (0x23) byte record
3425          * length in general, some (integrated) have an extra configuration byte
3426          */
3427
3428         NVPtr pNv = NVPTR(pScrn);
3429         bios_t *bios = &pNv->VBIOS;
3430         uint8_t pll_lim_ver = 0, headerlen = 0, recordlen = 0, entries = 0;
3431         int pllindex = 0;
3432         uint32_t crystal_strap_mask, crystal_straps;
3433
3434         if (!bios->pll_limit_tbl_ptr) {
3435                 if (bios->chip_version >= 0x40 || bios->chip_version == 0x31 || bios->chip_version == 0x36) {
3436                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pointer to PLL limits table invalid\n");
3437                         return false;
3438                 }
3439         } else {
3440                 pll_lim_ver = bios->data[bios->pll_limit_tbl_ptr];
3441
3442                 if (DEBUGLEVEL >= 7)
3443                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3444                                    "Found PLL limits table version 0x%X\n", pll_lim_ver);
3445         }
3446
3447         crystal_strap_mask = 1 << 6;
3448         /* open coded pNv->twoHeads test */
3449         if (bios->chip_version > 0x10 && bios->chip_version != 0x15 &&
3450             bios->chip_version != 0x1a && bios->chip_version != 0x20)
3451                 crystal_strap_mask |= 1 << 22;
3452         crystal_straps = nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT_0) & crystal_strap_mask;
3453
3454         switch (pll_lim_ver) {
3455         /* we use version 0 to indicate a pre limit table bios (single stage pll)
3456          * and load the hard coded limits instead */
3457         case 0:
3458                 break;
3459         case 0x10:
3460         case 0x11: /* strictly v0x11 has 3 entries, but the last two don't seem to get used */
3461                 headerlen = 1;
3462                 recordlen = 0x18;
3463                 entries = 1;
3464                 pllindex = 0;
3465                 break;
3466         case 0x20:
3467         case 0x21:
3468                 headerlen = bios->data[bios->pll_limit_tbl_ptr + 1];
3469                 recordlen = bios->data[bios->pll_limit_tbl_ptr + 2];
3470                 entries = bios->data[bios->pll_limit_tbl_ptr + 3];
3471                 break;
3472         default:
3473                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
3474                            "PLL limits table revision not currently supported\n");
3475                 return false;
3476         }
3477
3478         /* initialize all members to zero */
3479         memset(pll_lim, 0, sizeof(struct pll_lims));
3480
3481         if (pll_lim_ver == 0x10 || pll_lim_ver == 0x11) {
3482                 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen + recordlen * pllindex;
3483
3484                 pll_lim->vco1.minfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs])));
3485                 pll_lim->vco1.maxfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 4])));
3486                 pll_lim->vco2.minfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 8])));
3487                 pll_lim->vco2.maxfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 12])));
3488                 pll_lim->vco1.min_inputfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 16])));
3489                 pll_lim->vco2.min_inputfreq = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + 20])));
3490                 pll_lim->vco1.max_inputfreq = pll_lim->vco2.max_inputfreq = INT_MAX;
3491
3492                 /* these values taken from nv30/31/36 */
3493                 pll_lim->vco1.min_n = 0x1;
3494                 if (bios->chip_version == 0x36)
3495                         pll_lim->vco1.min_n = 0x5;
3496                 pll_lim->vco1.max_n = 0xff;
3497                 pll_lim->vco1.min_m = 0x1;
3498                 pll_lim->vco1.max_m = 0xd;
3499                 pll_lim->vco2.min_n = 0x4;
3500                 /* on nv30, 31, 36 (i.e. all cards with two stage PLLs with this
3501                  * table version (apart from nv35)), N2 is compared to
3502                  * maxN2 (0x46) and 10 * maxM2 (0x4), so set maxN2 to 0x28 and
3503                  * save a comparison
3504                  */
3505                 pll_lim->vco2.max_n = 0x28;
3506                 if (bios->chip_version == 0x30 || bios->chip_version == 0x35)
3507                        /* only 5 bits available for N2 on nv30/35 */
3508                         pll_lim->vco2.max_n = 0x1f;
3509                 pll_lim->vco2.min_m = 0x1;
3510                 pll_lim->vco2.max_m = 0x4;
3511         } else if (pll_lim_ver) {       /* ver 0x20, 0x21 */
3512                 uint16_t plloffs = bios->pll_limit_tbl_ptr + headerlen;
3513                 uint32_t reg = 0; /* default match */
3514                 int i;
3515
3516                 /* first entry is default match, if nothing better. warn if reg field nonzero */
3517                 if (le32_to_cpu(*((uint32_t *)&bios->data[plloffs])))
3518                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3519                                    "Default PLL limit entry has non-zero register field\n");
3520
3521                 if (limit_match > MAX_PLL_TYPES)
3522                         /* we've been passed a reg as the match */
3523                         reg = limit_match;
3524                 else /* limit match is a pll type */
3525                         for (i = 1; i < entries && !reg; i++) {
3526                                 uint32_t cmpreg = le32_to_cpu(*((uint32_t *)(&bios->data[plloffs + recordlen * i])));
3527
3528                                 if (limit_match == NVPLL && (cmpreg == NV_RAMDAC_NVPLL || cmpreg == 0x4000))
3529                                         reg = cmpreg;
3530                                 if (limit_match == MPLL && (cmpreg == NV_RAMDAC_MPLL || cmpreg == 0x4020))
3531                                         reg = cmpreg;
3532                                 if (limit_match == VPLL1 && (cmpreg == NV_RAMDAC_VPLL || cmpreg == 0x4010))
3533                                         reg = cmpreg;
3534                                 if (limit_match == VPLL2 && (cmpreg == NV_RAMDAC_VPLL2 || cmpreg == 0x4018))
3535                                         reg = cmpreg;
3536                         }
3537
3538                 for (i = 1; i < entries; i++)
3539                         if (le32_to_cpu(*((uint32_t *)&bios->data[plloffs + recordlen * i])) == reg) {
3540                                 pllindex = i;
3541                                 break;
3542                         }
3543
3544                 plloffs += recordlen * pllindex;
3545
3546                 if (DEBUGLEVEL >= 6)
3547                         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Loading PLL limits for reg 0x%08x\n",
3548                                    pllindex ? reg : 0);
3549
3550                 /* frequencies are stored in tables in MHz, kHz are more useful, so we convert */
3551
3552                 /* What output frequencies can each VCO generate? */
3553                 pll_lim->vco1.minfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 4]))) * 1000;
3554                 pll_lim->vco1.maxfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 6]))) * 1000;
3555                 pll_lim->vco2.minfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 8]))) * 1000;
3556                 pll_lim->vco2.maxfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 10]))) * 1000;
3557
3558                 /* What input frequencies do they accept (past the m-divider)? */
3559                 pll_lim->vco1.min_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 12]))) * 1000;
3560                 pll_lim->vco2.min_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 14]))) * 1000;
3561                 pll_lim->vco1.max_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 16]))) * 1000;
3562                 pll_lim->vco2.max_inputfreq = le16_to_cpu(*((uint16_t *)(&bios->data[plloffs + 18]))) * 1000;
3563
3564                 /* What values are accepted as multiplier and divider? */
3565                 pll_lim->vco1.min_n = bios->data[plloffs + 20];
3566                 pll_lim->vco1.max_n = bios->data[plloffs + 21];
3567                 pll_lim->vco1.min_m = bios->data[plloffs + 22];
3568                 pll_lim->vco1.max_m = bios->data[plloffs + 23];
3569                 pll_lim->vco2.min_n = bios->data[plloffs + 24];
3570                 pll_lim->vco2.max_n = bios->data[plloffs + 25];
3571                 pll_lim->vco2.min_m = bios->data[plloffs + 26];
3572                 pll_lim->vco2.max_m = bios->data[plloffs + 27];
3573
3574                 pll_lim->unk1c = bios->data[plloffs + 28];
3575                 pll_lim->max_log2p_bias = bios->data[plloffs + 29];
3576                 pll_lim->log2p_bias = bios->data[plloffs + 30];
3577
3578                 if (recordlen > 0x22)
3579                         pll_lim->refclk = le32_to_cpu(*((uint32_t *)&bios->data[plloffs + 31]));
3580
3581                 if (recordlen > 0x23)
3582                         if (bios->data[plloffs + 35])
3583                                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3584                                            "Bits set in PLL configuration byte (%x)\n", bios->data[plloffs + 35]);
3585
3586                 /* C51 special not seen elsewhere */
3587                 if (bios->chip_version == 0x51 && !pll_lim->refclk) {
3588                         uint32_t sel_clk = nv32_rd(pScrn, NV_RAMDAC_SEL_CLK);
3589
3590                         if (((limit_match == NV_RAMDAC_VPLL || limit_match == VPLL1) && sel_clk & 0x20) ||
3591                             ((limit_match == NV_RAMDAC_VPLL2 || limit_match == VPLL2) && sel_clk & 0x80)) {
3592                                 if (nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_REVISION) < 0xa3)
3593                                         pll_lim->refclk = 200000;
3594                                 else
3595                                         pll_lim->refclk = 25000;
3596                         }
3597                 }
3598         }
3599
3600         /* By now any valid limit table ought to have set a max frequency for
3601          * vco1, so if it's zero it's either a pre limit table bios, or one
3602          * with an empty limit table (seen on nv18)
3603          */
3604         if (!pll_lim->vco1.maxfreq) {
3605                 pll_lim->vco1.minfreq = bios->fminvco;
3606                 pll_lim->vco1.maxfreq = bios->fmaxvco;
3607                 pll_lim->vco1.min_inputfreq = 0;
3608                 pll_lim->vco1.max_inputfreq = INT_MAX;
3609                 pll_lim->vco1.min_n = 0x1;
3610                 pll_lim->vco1.max_n = 0xff;
3611                 pll_lim->vco1.min_m = 0x1;
3612                 if (crystal_straps == 0) {
3613                         /* nv05 does this, nv11 doesn't, nv10 unknown */
3614                         if (bios->chip_version < 0x11)
3615                                 pll_lim->vco1.min_m = 0x7;
3616                         pll_lim->vco1.max_m = 0xd;
3617                 } else {
3618                         if (bios->chip_version < 0x11)
3619                                 pll_lim->vco1.min_m = 0x8;
3620                         pll_lim->vco1.max_m = 0xe;
3621                 }
3622         }
3623
3624         if (!pll_lim->refclk)
3625                 switch (crystal_straps) {
3626                 case 0:
3627                         pll_lim->refclk = 13500;
3628                         break;
3629                 case (1 << 6):
3630                         pll_lim->refclk = 14318;
3631                         break;
3632                 case (1 << 22):
3633                         pll_lim->refclk = 27000;
3634                         break;
3635                 case (1 << 22 | 1 << 6):
3636                         pll_lim->refclk = 25000;
3637                         break;
3638                 }
3639
3640 #if 0 /* for easy debugging */
3641         ErrorF("pll.vco1.minfreq: %d\n", pll_lim->vco1.minfreq);
3642         ErrorF("pll.vco1.maxfreq: %d\n", pll_lim->vco1.maxfreq);
3643         ErrorF("pll.vco2.minfreq: %d\n", pll_lim->vco2.minfreq);
3644         ErrorF("pll.vco2.maxfreq: %d\n", pll_lim->vco2.maxfreq);
3645
3646         ErrorF("pll.vco1.min_inputfreq: %d\n", pll_lim->vco1.min_inputfreq);
3647         ErrorF("pll.vco1.max_inputfreq: %d\n", pll_lim->vco1.max_inputfreq);
3648         ErrorF("pll.vco2.min_inputfreq: %d\n", pll_lim->vco2.min_inputfreq);
3649         ErrorF("pll.vco2.max_inputfreq: %d\n", pll_lim->vco2.max_inputfreq);
3650
3651         ErrorF("pll.vco1.min_n: %d\n", pll_lim->vco1.min_n);
3652         ErrorF("pll.vco1.max_n: %d\n", pll_lim->vco1.max_n);
3653         ErrorF("pll.vco1.min_m: %d\n", pll_lim->vco1.min_m);
3654         ErrorF("pll.vco1.max_m: %d\n", pll_lim->vco1.max_m);
3655         ErrorF("pll.vco2.min_n: %d\n", pll_lim->vco2.min_n);
3656         ErrorF("pll.vco2.max_n: %d\n", pll_lim->vco2.max_n);
3657         ErrorF("pll.vco2.min_m: %d\n", pll_lim->vco2.min_m);
3658         ErrorF("pll.vco2.max_m: %d\n", pll_lim->vco2.max_m);
3659
3660         ErrorF("pll.unk1c: %d\n", pll_lim->unk1c);
3661         ErrorF("pll.max_log2p_bias: %d\n", pll_lim->max_log2p_bias);
3662         ErrorF("pll.log2p_bias: %d\n", pll_lim->log2p_bias);
3663
3664         ErrorF("pll.refclk: %d\n", pll_lim->refclk);
3665 #endif
3666
3667         return true;
3668 }
3669
3670 static int parse_bit_C_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3671 {
3672         /* offset + 8  (16 bits): PLL limits table pointer
3673          *
3674          * There's more in here, but that's unknown.
3675          */
3676
3677         if (bitentry->length < 10) {
3678                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT C table\n");
3679                 return 0;
3680         }
3681
3682         bios->pll_limit_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 8])));
3683
3684         return 1;
3685 }
3686
3687 static int parse_bit_display_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3688 {
3689         /* Parses the flat panel table segment that the bit entry points to.
3690          * Starting at bitentry->offset:
3691          *
3692          * offset + 0  (16 bits): ??? table pointer - seems to have 18 byte records beginning with a freq
3693          * offset + 2  (16 bits): mode table pointer
3694          */
3695
3696         struct fppointers fpp;
3697
3698         if (bitentry->length != 4) {
3699                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT display table\n");
3700                 return 0;
3701         }
3702
3703         memset(&fpp, 0, sizeof(struct fppointers));
3704         fpp.fptablepointer = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 2])));
3705
3706         parse_fp_mode_table(pScrn, bios, &fpp);
3707
3708         return 1;
3709 }
3710
3711 static unsigned int parse_bit_init_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3712 {
3713         /* Parses the init table segment that the bit entry points to.
3714          * Starting at bitentry->offset: 
3715          * 
3716          * offset + 0  (16 bits): init script tables pointer
3717          * offset + 2  (16 bits): macro index table pointer
3718          * offset + 4  (16 bits): macro table pointer
3719          * offset + 6  (16 bits): condition table pointer
3720          * offset + 8  (16 bits): io condition table pointer
3721          * offset + 10 (16 bits): io flag condition table pointer
3722          * offset + 12 (16 bits): init function table pointer
3723          *
3724          */
3725
3726         if (bitentry->length < 14) {
3727                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand init table\n");
3728                 return 0;
3729         }
3730
3731         bios->init_script_tbls_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
3732         bios->macro_index_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 2])));
3733         bios->macro_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 4])));
3734         bios->condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 6])));
3735         bios->io_condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 8])));
3736         bios->io_flag_condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 10])));
3737         bios->init_function_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 12])));
3738
3739         return 1;
3740 }
3741
3742 static int parse_bit_i_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3743 {
3744         /* BIT 'i' (info?) table
3745          *
3746          * offset + 0  (32 bits): BIOS version dword (as in B table)
3747          * offset + 5  (8  bits): BIOS feature byte (same as for BMP?)
3748          * offset + 13 (16 bits): pointer to table containing DAC load detection comparison values
3749          *
3750          * There's other things in the table, purpose unknown
3751          */
3752
3753         uint16_t daccmpoffset;
3754         uint8_t dacversion, dacheaderlen;
3755
3756         if (bitentry->length < 6) {
3757                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3758                            "BIT i table not long enough for BIOS version and feature byte\n");
3759                 return 0;
3760         }
3761
3762         parse_bios_version(pScrn, bios, bitentry->offset);
3763
3764         /* bit 4 seems to indicate a mobile bios, other bits possibly as for BMP feature byte */
3765         bios->feature_byte = bios->data[bitentry->offset + 5];
3766
3767         if (bitentry->length < 15) {
3768                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3769                            "BIT i table not long enough for DAC load detection comparison table\n");
3770                 return 0;
3771         }
3772
3773         daccmpoffset = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 13])));
3774
3775         /* doesn't exist on g80 */
3776         if (!daccmpoffset)
3777                 return 1;
3778
3779         /* The first value in the table, following the header, is the comparison value
3780          * Purpose of subsequent values unknown -- TV load detection?
3781          */
3782
3783         dacversion = bios->data[daccmpoffset];
3784         dacheaderlen = bios->data[daccmpoffset + 1];
3785
3786         if (dacversion != 0x00 && dacversion != 0x10) {
3787                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
3788                            "DAC load detection comparison table version %d.%d not known\n",
3789                            dacversion >> 4, dacversion & 0xf);
3790                 return 0;
3791         } else
3792                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
3793                    "DAC load detection comparison table version %x found\n", dacversion);
3794
3795         bios->dactestval = le32_to_cpu(*((uint32_t *)(&bios->data[daccmpoffset + dacheaderlen])));
3796
3797         return 1;
3798 }
3799
3800 static int parse_bit_lvds_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3801 {
3802         /* Parses the LVDS table segment that the bit entry points to.
3803          * Starting at bitentry->offset:
3804          *
3805          * offset + 0  (16 bits): LVDS strap xlate table pointer
3806          */
3807
3808         if (bitentry->length != 2) {
3809                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT LVDS table\n");
3810                 return 0;
3811         }
3812
3813         /* no idea if it's still called the LVDS manufacturer table, but the concept's close enough */
3814         bios->fp.lvdsmanufacturerpointer = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
3815         bios->fp.strapping = get_fp_strap(pScrn, bios);
3816
3817         parse_lvds_manufacturer_table(pScrn, bios, 0);
3818
3819         return 1;
3820 }
3821
3822 static int parse_bit_M_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3823 {
3824         /* offset + 2  (8  bits): number of options in an INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
3825          * offset + 3  (16 bits): pointer to strap xlate table for RAM restrict option selection
3826          *
3827          * There's a bunch of bits in this table other than the RAM restrict
3828          * stuff that we don't use - their use currently unknown
3829          */
3830
3831         int i;
3832
3833         /* Older bios versions don't have a sufficiently long table for what we want */
3834         if (bitentry->length < 0x5)
3835                 return 1;
3836
3837         /* set up multiplier for INIT_RAM_RESTRICT_ZM_REG_GROUP */
3838         for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != 0x8f); i++)
3839                 ;
3840         itbl_entry[i].length_multiplier = bios->data[bitentry->offset + 2] * 4;
3841         init_ram_restrict_zm_reg_group_blocklen = itbl_entry[i].length_multiplier;
3842
3843         bios->ram_restrict_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 3])));
3844
3845         return 1;
3846 }
3847
3848 static int parse_bit_tmds_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
3849 {
3850         /* Parses the pointer to the TMDS table
3851          *
3852          * Starting at bitentry->offset:
3853          *
3854          * offset + 0  (16 bits): TMDS table pointer
3855          *
3856          * The TMDS table is typically found just before the DCB table, with a
3857          * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
3858          * length?)
3859          *
3860          * At offset +7 is a pointer to a script, which I don't know how to run yet
3861          * At offset +9 is a pointer to another script, likewise
3862          * Offset +11 has a pointer to a table where the first word is a pxclk
3863          * frequency and the second word a pointer to a script, which should be
3864          * run if the comparison pxclk frequency is less than the pxclk desired.
3865          * This repeats for decreasing comparison frequencies
3866          * Offset +13 has a pointer to a similar table
3867          * The selection of table (and possibly +7/+9 script) is dictated by
3868          * "or" from the DCB.
3869          */
3870
3871         uint16_t tmdstableptr, script1, script2;
3872
3873         if (bitentry->length != 2) {
3874                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Do not understand BIT TMDS table\n");
3875                 return 0;
3876         }
3877
3878         tmdstableptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
3879
3880         if (tmdstableptr == 0x0) {
3881                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Pointer to TMDS table invalid\n");
3882                 return 0;
3883         }
3884
3885         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Found TMDS table revision %d.%d\n",
3886                    bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
3887
3888         /* These two scripts are odd: they don't seem to get run even when they are not stubbed */
3889         script1 = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 7]));
3890         script2 = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 9]));
3891         if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
3892                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "TMDS table script pointers not stubbed\n");
3893
3894         bios->tmds.output0_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 11]));
3895         bios->tmds.output1_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 13]));
3896
3897         return 1;
3898 }
3899
3900 static void parse_bit_structure(ScrnInfoPtr pScrn, bios_t *bios, const uint16_t bitoffset)
3901 {
3902         int entries = bios->data[bitoffset + 4];
3903         /* parse i first, I next (which needs C & M before it), and L before D */
3904         char parseorder[] = "iCMILDT";
3905         bit_entry_t bitentry;
3906         int i, j, offset;
3907
3908         for (i = 0; i < sizeof(parseorder); i++) {
3909                 for (j = 0, offset = bitoffset + 6; j < entries; j++, offset += 6) {
3910                         bitentry.id[0] = bios->data[offset];
3911                         bitentry.id[1] = bios->data[offset + 1];
3912                         bitentry.length = le16_to_cpu(*((uint16_t *)&bios->data[offset + 2]));
3913                         bitentry.offset = le16_to_cpu(*((uint16_t *)&bios->data[offset + 4]));
3914
3915                         if (bitentry.id[0] != parseorder[i])
3916                                 continue;
3917
3918                         switch (bitentry.id[0]) {
3919                         case 'C':
3920                                 parse_bit_C_tbl_entry(pScrn, bios, &bitentry);
3921                                 break;
3922                         case 'D':
3923                                 parse_bit_display_tbl_entry(pScrn, bios, &bitentry);
3924                                 break;
3925                         case 'I':
3926                                 parse_bit_init_tbl_entry(pScrn, bios, &bitentry);
3927                                 parse_init_tables(pScrn, bios);
3928                                 break;
3929                         case 'i': /* info? */
3930                                 parse_bit_i_tbl_entry(pScrn, bios, &bitentry);
3931                                 break;
3932                         case 'L':
3933                                 if (bios->feature_byte & FEATURE_MOBILE)
3934                                         parse_bit_lvds_tbl_entry(pScrn, bios, &bitentry);
3935                                 break;
3936                         case 'M': /* memory? */
3937                                 parse_bit_M_tbl_entry(pScrn, bios, &bitentry);
3938                                 break;
3939                         case 'T':
3940                                 parse_bit_tmds_tbl_entry(pScrn, bios, &bitentry);
3941                                 break;
3942                         }
3943                 }
3944         }
3945 }
3946
3947 static void parse_bmp_structure(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset)
3948 {
3949         /* Parse the BMP structure for useful things
3950          *
3951          * offset +   5: BMP major version
3952          * offset +   6: BMP minor version
3953          * offset +  10: BCD encoded BIOS version
3954          *
3955          * offset +  18: init script table pointer (for bios versions < 5.10h)
3956          * offset +  20: extra init script table pointer (for bios versions < 5.10h)
3957          *
3958          * offset +  24: memory init table pointer (used on early bios versions)
3959          * offset +  26: SDR memory sequencing setup data table
3960          * offset +  28: DDR memory sequencing setup data table
3961          *
3962          * offset +  54: index of I2C CRTC pair to use for CRT output
3963          * offset +  55: index of I2C CRTC pair to use for TV output
3964          * offset +  56: index of I2C CRTC pair to use for flat panel output
3965          * offset +  58: write CRTC index for I2C pair 0
3966          * offset +  59: read CRTC index for I2C pair 0
3967          * offset +  60: write CRTC index for I2C pair 1
3968          * offset +  61: read CRTC index for I2C pair 1
3969          *
3970          * offset +  67: maximum internal PLL frequency (single stage PLL)
3971          * offset +  71: minimum internal PLL frequency (single stage PLL)
3972          *
3973          * offset +  75: script table pointers, as for parse_bit_init_tbl_entry
3974          *
3975          * offset +  89: TMDS single link output A table pointer
3976          * offset +  91: TMDS single link output B table pointer
3977          * offset + 105: flat panel timings table pointer
3978          * offset + 107: flat panel strapping translation table pointer
3979          * offset + 117: LVDS manufacturer panel config table pointer
3980          * offset + 119: LVDS manufacturer strapping translation table pointer
3981          *
3982          * offset + 142: PLL limits table pointer
3983          */
3984
3985         NVPtr pNv = NVPTR(pScrn);
3986         uint8_t bmp_version_major, bmp_version_minor;
3987         uint16_t bmplength;
3988         struct fppointers fpp;
3989         uint16_t legacy_scripts_offset, legacy_i2c_offset;
3990
3991         memset(&fpp, 0, sizeof(struct fppointers));
3992
3993         /* load needed defaults in case we can't parse this info */
3994         pNv->dcb_table.i2c_write[0] = 0x3f;
3995         pNv->dcb_table.i2c_read[0] = 0x3e;
3996         pNv->dcb_table.i2c_write[1] = 0x37;
3997         pNv->dcb_table.i2c_read[1] = 0x36;
3998         bios->digital_min_front_porch = 0x4b;
3999         bios->fmaxvco = 256000;
4000         bios->fminvco = 128000;
4001         bios->fp.duallink_transition_clk = 90000;
4002
4003         bmp_version_major = bios->data[offset + 5];
4004         bmp_version_minor = bios->data[offset + 6];
4005
4006         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BMP version %d.%d\n",
4007                    bmp_version_major, bmp_version_minor);
4008
4009         /* Make sure that 0x36 is blank and can't be mistaken for a DCB pointer on early versions */
4010         if (bmp_version_major < 5)
4011                 *(uint16_t *)&bios->data[0x36] = 0;
4012
4013         /* Seems that the minor version was 1 for all major versions prior to 5 */
4014         /* Version 6 could theoretically exist, but I suspect BIT happened instead */
4015         if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
4016                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "You have an unsupported BMP version. Please send in your bios\n");
4017                 return;
4018         }
4019
4020         if (bmp_version_major == 0) /* nothing that's currently useful in this version */
4021                 return;
4022         else if (bmp_version_major == 1)
4023                 bmplength = 44; /* exact for 1.01 */
4024         else if (bmp_version_major == 2)
4025                 bmplength = 48; /* exact for 2.01 */
4026         else if (bmp_version_major == 3)
4027                 bmplength = 54; /* guessed - mem init tables added in this version */
4028         else if (bmp_version_major == 4 || bmp_version_minor < 0x1) /* don't know if 5.0 exists... */
4029                 bmplength = 62; /* guessed - BMP I2C indices added in version 4*/
4030         else if (bmp_version_minor < 0x6)
4031                 bmplength = 67; /* exact for 5.01 */
4032         else if (bmp_version_minor < 0x10)
4033                 bmplength = 75; /* exact for 5.06 */
4034         else if (bmp_version_minor == 0x10)
4035                 bmplength = 89; /* exact for 5.10h */
4036         else if (bmp_version_minor < 0x14)
4037                 bmplength = 118; /* exact for 5.11h */
4038         else if (bmp_version_minor < 0x24) /* not sure of version where pll limits came in;
4039                                             * certainly exist by 0x24 though */
4040                 /* length not exact: this is long enough to get lvds members */
4041                 bmplength = 123;
4042         else if (bmp_version_minor < 0x27)
4043                 /* length not exact: this is long enough to get pll limit member */
4044                 bmplength = 144;
4045         else
4046                 /* length not exact: this is long enough to get dual link transition clock */
4047                 bmplength = 158;
4048
4049         /* checksum */
4050         if (nv_cksum(bios->data + offset, 8)) {
4051                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "Bad BMP checksum\n");
4052                 return;
4053         }
4054
4055         /* bit 4 seems to indicate a mobile bios, bit 5 that the flat panel
4056          * tables are present, and bit 6 a tv bios */
4057         bios->feature_byte = bios->data[offset + 9];
4058
4059         parse_bios_version(pScrn, bios, offset + 10);
4060
4061         legacy_scripts_offset = offset + 18;
4062         if (bmp_version_major < 2)
4063                 legacy_scripts_offset -= 4;
4064         bios->init_script_tbls_ptr = le16_to_cpu(*(uint16_t *)&bios->data[legacy_scripts_offset]);
4065         bios->extra_init_script_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[legacy_scripts_offset + 2]);
4066
4067         if (bmp_version_major > 2) {    /* appears in BMP 3 */
4068                 bios->legacy.mem_init_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[offset + 24]);
4069                 bios->legacy.sdr_seq_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[offset + 26]);
4070                 bios->legacy.ddr_seq_tbl_ptr = le16_to_cpu(*(uint16_t *)&bios->data[offset + 28]);
4071         }
4072
4073         legacy_i2c_offset = 0x48;       /* BMP version 2 & 3 */
4074         if (bmplength > 61)
4075                 legacy_i2c_offset = offset + 54;
4076         bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
4077         bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
4078         bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
4079         pNv->dcb_table.i2c_write[0] = bios->data[legacy_i2c_offset + 4];
4080         pNv->dcb_table.i2c_read[0] = bios->data[legacy_i2c_offset + 5];
4081         pNv->dcb_table.i2c_write[1] = bios->data[legacy_i2c_offset + 6];
4082         pNv->dcb_table.i2c_read[1] = bios->data[legacy_i2c_offset + 7];
4083
4084         if (bmplength > 74) {
4085                 bios->fmaxvco = le32_to_cpu(*((uint32_t *)&bios->data[offset + 67]));
4086                 bios->fminvco = le32_to_cpu(*((uint32_t *)&bios->data[offset + 71]));
4087         }
4088         if (bmplength > 88) {
4089                 bit_entry_t initbitentry;
4090                 initbitentry.length = 14;
4091                 initbitentry.offset = offset + 75;
4092                 parse_bit_init_tbl_entry(pScrn, bios, &initbitentry);
4093         }
4094         if (bmplength > 94) {
4095                 bios->tmds.output0_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[offset + 89]));
4096                 bios->tmds.output1_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[offset + 91]));
4097                 /* it seems the old style lvds script pointer (which I've not observed in use) gets
4098                  * reused as the 18/24 bit panel interface default for EDID equipped panels */
4099                 bios->fp.if_is_24bit = bios->data[offset + 95] & 1;
4100         }
4101         if (bmplength > 108) {
4102                 fpp.fptablepointer = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 105])));
4103                 fpp.fpxlatetableptr = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 107])));
4104                 fpp.xlatwidth = 1;
4105         }
4106         if (bmplength > 120) {
4107                 bios->fp.lvdsmanufacturerpointer = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 117])));
4108                 bios->fp.fpxlatemanufacturertableptr = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 119])));
4109         }
4110         if (bmplength > 143)
4111                 bios->pll_limit_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 142])));
4112
4113         if (bmplength > 157)
4114                 bios->fp.duallink_transition_clk = le16_to_cpu(*((uint16_t *)&bios->data[offset + 156])) * 10;
4115
4116         /* want pll_limit_tbl_ptr set (if available) before init is run */
4117         if (bmp_version_major < 5 || bmp_version_minor < 0x10) {
4118                 init_exec_t iexec = {true, false};
4119                 if (bios->init_script_tbls_ptr)
4120                         parse_init_table(pScrn, bios, bios->init_script_tbls_ptr, &iexec);
4121                 if (bios->extra_init_script_tbl_ptr)
4122                         parse_init_table(pScrn, bios, bios->extra_init_script_tbl_ptr, &iexec);
4123         } else
4124                 parse_init_tables(pScrn, bios);
4125
4126         /* If it's not a laptop, you probably don't care about fptables */
4127         if (!(bios->feature_byte & FEATURE_MOBILE))
4128                 return;
4129
4130         bios->fp.strapping = get_fp_strap(pScrn, bios);
4131         parse_lvds_manufacturer_table(pScrn, bios, 0);
4132         parse_fp_mode_table(pScrn, bios, &fpp);
4133 }
4134
4135 static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
4136 {
4137         int i, j;
4138
4139         for (i = 0; i <= (n - len); i++) {
4140                 for (j = 0; j < len; j++)
4141                         if (data[i + j] != str[j])
4142                                 break;
4143                 if (j == len)
4144                         return i;
4145         }
4146
4147         return 0;
4148 }
4149
4150 static void
4151 read_dcb_i2c_entry(ScrnInfoPtr pScrn, uint8_t dcb_version, uint16_t i2ctabptr, int index)
4152 {
4153         NVPtr pNv = NVPTR(pScrn);
4154         bios_t *bios = &pNv->VBIOS;
4155         uint8_t *i2ctable = &bios->data[i2ctabptr];
4156         uint8_t headerlen = 0;
4157         int i2c_entries = MAX_NUM_DCB_ENTRIES;
4158         int recordoffset = 0, rdofs = 1, wrofs = 0;
4159
4160         if (!i2ctabptr)
4161                 return;
4162
4163         if (dcb_version >= 0x30) {
4164                 if (i2ctable[0] != dcb_version) /* necessary? */
4165                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4166                                    "DCB I2C table version mismatch (%02X vs %02X)\n",
4167                                    i2ctable[0], dcb_version);
4168                 headerlen = i2ctable[1];
4169                 i2c_entries = i2ctable[2];
4170
4171                 /* same address offset used for read and write for C51 and G80 */
4172                 if (bios->chip_version == 0x51)
4173                         rdofs = wrofs = 1;
4174                 if (i2ctable[0] >= 0x40)
4175                         rdofs = wrofs = 0;
4176         }
4177         /* it's your own fault if you call this function on a DCB 1.1 BIOS --
4178          * the test below is for DCB 1.2
4179          */
4180         if (dcb_version < 0x14) {
4181                 recordoffset = 2;
4182                 rdofs = 0;
4183                 wrofs = 1;
4184         }
4185
4186         if (index == 0xf)
4187                 return;
4188         if (index > i2c_entries) {
4189                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4190                            "DCB I2C index too big (%d > %d)\n",
4191                            index, i2ctable[2]);
4192                 return;
4193         }
4194         if (i2ctable[headerlen + 4 * index + 3] == 0xff) {
4195                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4196                            "DCB I2C entry invalid\n");
4197                 return;
4198         }
4199
4200         if (bios->chip_version == 0x51) {
4201                 int port_type = i2ctable[headerlen + 4 * index + 3];
4202
4203                 if (port_type != 4)
4204                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4205                                    "DCB I2C table has port type %d\n", port_type);
4206         }
4207         if (i2ctable[0] >= 0x40) {
4208                 int port_type = i2ctable[headerlen + 4 * index + 3];
4209
4210                 if (port_type != 5)
4211                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4212                                    "DCB I2C table has port type %d\n", port_type);
4213         }
4214
4215         pNv->dcb_table.i2c_read[index] = i2ctable[headerlen + recordoffset + rdofs + 4 * index];
4216         pNv->dcb_table.i2c_write[index] = i2ctable[headerlen + recordoffset + wrofs + 4 * index];
4217 }
4218
4219 static bool
4220 parse_dcb_entry(ScrnInfoPtr pScrn, int index, uint8_t dcb_version, uint16_t i2ctabptr, uint32_t conn, uint32_t conf)
4221 {
4222         NVPtr pNv = NVPTR(pScrn);
4223         struct dcb_entry *entry = &pNv->dcb_table.entry[index];
4224
4225         memset(entry, 0, sizeof (struct dcb_entry));
4226
4227         entry->index = index;
4228         /* safe defaults for a crt */
4229         entry->type = 0;
4230         entry->i2c_index = 0;
4231         entry->heads = 1;
4232         entry->bus = 0;
4233         entry->location = LOC_ON_CHIP;
4234         entry->or = 1;
4235         entry->duallink_possible = false;
4236
4237         if (dcb_version >= 0x20) {
4238                 entry->type = conn & 0xf;
4239                 entry->i2c_index = (conn >> 4) & 0xf;
4240                 entry->heads = (conn >> 8) & 0xf;
4241                 entry->bus = (conn >> 16) & 0xf;
4242                 entry->location = (conn >> 20) & 0xf;
4243                 entry->or = (conn >> 24) & 0xf;
4244                 /* Normal entries consist of a single bit, but dual link has the
4245                  * adjacent more significant bit set too
4246                  */
4247                 if ((1 << (ffs(entry->or) - 1)) * 3 == entry->or)
4248                         entry->duallink_possible = true;
4249
4250                 switch (entry->type) {
4251                 case OUTPUT_LVDS:
4252                         {
4253                         uint32_t mask;
4254                         if (conf & 0x1)
4255                                 entry->lvdsconf.use_straps_for_mode = true;
4256                         if (dcb_version < 0x22) {
4257                                 mask = ~0xd;
4258                                 /* both 0x4 and 0x8 show up in v2.0 tables; assume they mean
4259                                  * the same thing, which is probably wrong, but might work */
4260                                 if (conf & 0x4 || conf & 0x8)
4261                                         entry->lvdsconf.use_power_scripts = true;
4262                         } else {
4263                                 mask = ~0x5;
4264                                 if (conf & 0x4)
4265                                         entry->lvdsconf.use_power_scripts = true;
4266                         }
4267                         if (conf & mask) {
4268                                 /* I'm bored of getting this reported; left as a reminder for someone to fix it */
4269                                 if (dcb_version >= 0x40) {
4270                                         xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4271                                                    "G80+ LVDS not initialized by driver; ignoring conf bits\n");
4272                                         break;
4273                                 }
4274                                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4275                                            "Unknown LVDS configuration bits, please report\n");
4276                                 /* cause output setting to fail, so message is seen */
4277                                 pNv->dcb_table.entries = 0;
4278                                 return false;
4279                         }
4280                         break;
4281                         }
4282                 case 0xe:
4283                         /* weird type that appears on g80 mobile bios; nv driver treats it as a terminator */
4284                         return false;
4285                 }
4286                 read_dcb_i2c_entry(pScrn, dcb_version, i2ctabptr, entry->i2c_index);
4287         } else if (dcb_version >= 0x14 ) {
4288                 if (conn != 0xf0003f00 && conn != 0xf2247f10 &&
4289                     conn != 0xf2204001 && conn != 0xf2204301 && conn != 0xf2204311 && conn != 0xf2208001 && conn != 0xf2244001 && conn != 0xf2244311 && conn != 0xf4204011 && conn != 0xf4208011 && conn != 0xf4248011 &&
4290                     conn != 0xf2045f14 && conn != 0xf2205004) {
4291                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4292                                    "Unknown DCB 1.4 / 1.5 entry, please report\n");
4293                         /* cause output setting to fail, so message is seen */
4294                         pNv->dcb_table.entries = 0;
4295                         return false;
4296                 }
4297                 /* most of the below is a "best guess" atm */
4298                 entry->type = conn & 0xf;
4299                 if (entry->type == 4) { /* digital */
4300                         if (conn & 0x10)
4301                                 entry->type = OUTPUT_LVDS;
4302                         else
4303                                 entry->type = OUTPUT_TMDS;
4304                 }
4305                 /* what's in bits 5-13? could be some brooktree/chrontel/philips thing, in tv case */
4306                 entry->i2c_index = (conn >> 14) & 0xf;
4307                 /* raw heads field is in range 0-1, so move to 1-2 */
4308                 entry->heads = ((conn >> 18) & 0x7) + 1;
4309                 entry->location = (conn >> 21) & 0xf;
4310                 entry->bus = (conn >> 25) & 0x7;
4311                 /* set or to be same as heads -- hopefully safe enough */
4312                 entry->or = entry->heads;
4313
4314                 switch (entry->type) {
4315                 case OUTPUT_LVDS:
4316                         /* this is probably buried in conn's unknown bits */
4317                         entry->lvdsconf.use_power_scripts = true;
4318                         break;
4319                 case OUTPUT_TMDS:
4320                         /* invent a DVI-A output, by copying the fields of the DVI-D output
4321                          * reported to work by math_b on an NV20(!) */
4322                         memcpy(&entry[1], &entry[0], sizeof(struct dcb_entry));
4323                         entry[1].type = OUTPUT_ANALOG;
4324                         pNv->dcb_table.entries++;
4325                 }
4326                 read_dcb_i2c_entry(pScrn, dcb_version, i2ctabptr, entry->i2c_index);
4327         } else if (dcb_version >= 0x12) {
4328                 /* v1.2 tables normally have the same 5 entries, which are not
4329                  * specific to the card, so use the defaults for a crt */
4330                 /* DCB v1.2 does have an I2C table that read_dcb_i2c_table can handle, but cards
4331                  * exist (seen on nv11) where the pointer to the table points to the wrong
4332                  * place, so for now, we rely on the indices parsed in parse_bmp_structure
4333                  */
4334                 entry->i2c_index = pNv->VBIOS.legacy.i2c_indices.crt;
4335         } else { /* pre DCB / v1.1 - use the safe defaults for a crt */
4336                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4337                            "No information in BIOS output table; assuming a CRT output exists\n");
4338                 entry->i2c_index = pNv->VBIOS.legacy.i2c_indices.crt;
4339         }
4340
4341         if (entry->type == OUTPUT_LVDS && pNv->VBIOS.fp.strapping != 0xff)
4342                 entry->lvdsconf.use_straps_for_mode = true;
4343
4344         pNv->dcb_table.entries++;
4345
4346         return true;
4347 }
4348
4349 void merge_like_dcb_entries(ScrnInfoPtr pScrn)
4350 {
4351         /* DCB v2.0 lists each output combination separately.
4352          * Here we merge compatible entries to have fewer outputs, with more options
4353          */
4354
4355         NVPtr pNv = NVPTR(pScrn);
4356         int i, newentries = 0;
4357
4358         for (i = 0; i < pNv->dcb_table.entries; i++) {
4359                 struct dcb_entry *ient = &pNv->dcb_table.entry[i];
4360                 int j;
4361
4362                 for (j = i + 1; j < pNv->dcb_table.entries; j++) {
4363                         struct dcb_entry *jent = &pNv->dcb_table.entry[j];
4364
4365                         if (jent->type == 100) /* already merged entry */
4366                                 continue;
4367
4368                         /* merge heads field when all other fields the same */
4369                         if (jent->i2c_index == ient->i2c_index && jent->type == ient->type && jent->location == ient->location && jent->or == ient->or) {
4370                                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4371                                            "Merging DCB entries %d and %d\n", i, j);
4372                                 ient->heads |= jent->heads;
4373                                 jent->type = 100; /* dummy value */
4374                         }
4375                 }
4376         }
4377
4378         /* Compact entries merged into others out of dcb_table */
4379         for (i = 0; i < pNv->dcb_table.entries; i++) {
4380                 if ( pNv->dcb_table.entry[i].type == 100 )
4381                         continue;
4382
4383                 if (newentries != i)
4384                         memcpy(&pNv->dcb_table.entry[newentries], &pNv->dcb_table.entry[i], sizeof(struct dcb_entry));
4385                 newentries++;
4386         }
4387
4388         pNv->dcb_table.entries = newentries;
4389 }
4390
4391 static unsigned int parse_dcb_table(ScrnInfoPtr pScrn, bios_t *bios)
4392 {
4393         NVPtr pNv = NVPTR(pScrn);
4394         uint16_t dcbptr, i2ctabptr = 0;
4395         uint8_t *dcbtable;
4396         uint8_t dcb_version, headerlen = 0x4, entries = MAX_NUM_DCB_ENTRIES;
4397         bool configblock = true;
4398         int recordlength = 8, confofs = 4;
4399         int i;
4400
4401         pNv->dcb_table.entries = 0;
4402
4403         /* get the offset from 0x36 */
4404         dcbptr = le16_to_cpu(*(uint16_t *)&bios->data[0x36]);
4405
4406         if (dcbptr == 0x0) {
4407                 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
4408                            "No Display Configuration Block pointer found\n");
4409                 /* this situation likely means a really old card, pre DCB, so we'll add the safe CRT entry */
4410                 parse_dcb_entry(pScrn, 0, 0, 0, 0, 0);
4411                 return 1;
4412         }
4413
4414         dcbtable = &bios->data[dcbptr];
4415
4416         /* get DCB version */
4417         dcb_version = dcbtable[0];
4418         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4419                    "Display Configuration Block version %d.%d found\n",
4420                    dcb_version >> 4, dcb_version & 0xf);
4421
4422         if (dcb_version >= 0x20) { /* NV17+ */
4423                 uint32_t sig;
4424
4425                 if (dcb_version >= 0x30) { /* NV40+ */
4426                         headerlen = dcbtable[1];
4427                         entries = dcbtable[2];
4428                         recordlength = dcbtable[3];
4429                         i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[4]);
4430                         sig = le32_to_cpu(*(uint32_t *)&dcbtable[6]);
4431
4432                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4433                                    "DCB header length %d, with %d possible entries\n",
4434                                    headerlen, entries);
4435                 } else {
4436                         i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
4437                         sig = le32_to_cpu(*(uint32_t *)&dcbtable[4]);
4438                         headerlen = 8;
4439                 }
4440
4441                 if (sig != 0x4edcbdcb) {
4442                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4443                                    "Bad Display Configuration Block signature (%08X)\n", sig);
4444                         return 0;
4445                 }
4446         } else if (dcb_version >= 0x14) { /* some NV15/16, and NV11+ */
4447                 char sig[8];
4448
4449                 memset(sig, 0, 8);
4450                 strncpy(sig, (char *)&dcbtable[-7], 7);
4451                 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
4452                 recordlength = 10;
4453                 confofs = 6;
4454
4455                 if (strcmp(sig, "DEV_REC")) {
4456                         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4457                                    "Bad Display Configuration Block signature (%s)\n", sig);
4458                         return 0;
4459                 }
4460         } else if (dcb_version >= 0x12) { /* some NV6/10, and NV15+ */
4461                 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
4462                 configblock = false;
4463         } else {        /* NV5+, maybe NV4 */
4464                 /* DCB 1.1 seems to be quite unhelpful - we'll just add the safe CRT entry */
4465                 parse_dcb_entry(pScrn, 0, dcb_version, 0, 0, 0);
4466                 return 1;
4467         }
4468
4469         if (entries >= MAX_NUM_DCB_ENTRIES)
4470                 entries = MAX_NUM_DCB_ENTRIES;
4471
4472         for (i = 0; i < entries; i++) {
4473                 uint32_t connection, config = 0;
4474
4475                 connection = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + recordlength * i]);
4476                 if (configblock)
4477                         config = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + confofs + recordlength * i]);
4478
4479                 /* Should we allow discontinuous DCBs? Certainly DCB I2C tables can be discontinuous */
4480                 if ((connection & 0x0000000f) == 0x0000000f) /* end of records */
4481                         break;
4482                 if (connection == 0x00000000) /* seen on an NV11 with DCB v1.5 */
4483                         break;
4484
4485                 ErrorF("Raw DCB entry %d: %08x %08x\n", i, connection, config);
4486                 if (!parse_dcb_entry(pScrn, pNv->dcb_table.entries, dcb_version, i2ctabptr, connection, config))
4487                         break;
4488         }
4489
4490         merge_like_dcb_entries(pScrn);
4491
4492         return pNv->dcb_table.entries;
4493 }
4494
4495 static void load_nv17_hw_sequencer_ucode(ScrnInfoPtr pScrn, bios_t *bios, uint16_t hwsq_offset, int entry)
4496 {
4497         /* BMP based cards, from NV17, need a microcode loading to correctly
4498          * control the GPIO etc for LVDS panels
4499          *
4500          * BIT based cards seem to do this directly in the init scripts
4501          *
4502          * The microcode entries are found by the "HWSQ" signature.
4503          * The header following has the number of entries, and the entry size
4504          *
4505          * An entry consists of a dword to write to the sequencer control reg
4506          * (0x00001304), followed by the ucode bytes, written sequentially,
4507          * starting at reg 0x00001400
4508          */
4509
4510         uint8_t bytes_to_write;
4511         uint16_t hwsq_entry_offset;
4512         int i;
4513
4514         if (bios->data[hwsq_offset] <= entry) {
4515                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4516                            "Too few entries in HW sequencer table for requested entry\n");
4517                 return;
4518         }
4519
4520         bytes_to_write = bios->data[hwsq_offset + 1];
4521
4522         if (bytes_to_write != 36) {
4523                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Unknown HW sequencer entry size\n");
4524                 return;
4525         }
4526
4527         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Loading NV17 power sequencing microcode\n");
4528
4529         hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
4530
4531         /* set sequencer control */
4532         nv32_wr(pScrn, 0x00001304, le32_to_cpu(*(uint32_t *)&bios->data[hwsq_entry_offset]));
4533         bytes_to_write -= 4;
4534
4535         /* write ucode */
4536         for (i = 0; i < bytes_to_write; i += 4)
4537                 nv32_wr(pScrn, 0x00001400 + i, le32_to_cpu(*(uint32_t *)&bios->data[hwsq_entry_offset + i + 4]));
4538
4539         /* twiddle NV_PBUS_DEBUG_4 */
4540         nv32_wr(pScrn, NV_PBUS_DEBUG_4, nv32_rd(pScrn, NV_PBUS_DEBUG_4) | 0x18);
4541 }
4542
4543 static void read_bios_edid(ScrnInfoPtr pScrn)
4544 {
4545         bios_t *bios = &NVPTR(pScrn)->VBIOS;
4546         const uint8_t edid_sig[] = { 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
4547         uint16_t offset = 0, newoffset;
4548         int searchlen = NV_PROM_SIZE, i;
4549
4550         while (searchlen) {
4551                 if (!(newoffset = findstr(&bios->data[offset], searchlen, edid_sig, 8)))
4552                         return;
4553                 offset += newoffset;
4554                 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
4555                         break;
4556
4557                 searchlen -= offset;
4558                 offset++;
4559         }
4560
4561         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Found EDID in BIOS\n");
4562
4563         bios->fp.edid = xalloc(EDID1_LEN);
4564         for (i = 0; i < EDID1_LEN; i++)
4565                 bios->fp.edid[i] = bios->data[offset + i];
4566 }
4567
4568 bool NVInitVBIOS(ScrnInfoPtr pScrn)
4569 {
4570         NVPtr pNv = NVPTR(pScrn);
4571
4572         memset(&pNv->VBIOS, 0, sizeof(bios_t));
4573         pNv->VBIOS.data = xalloc(NV_PROM_SIZE);
4574
4575         if (!NVShadowVBIOS(pScrn, pNv->VBIOS.data)) {
4576                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4577                            "No valid BIOS image found\n");
4578                 xfree(pNv->VBIOS.data);
4579                 return false;
4580         }
4581
4582         pNv->VBIOS.length = pNv->VBIOS.data[2] * 512;
4583         if (pNv->VBIOS.length > NV_PROM_SIZE)
4584                 pNv->VBIOS.length = NV_PROM_SIZE;
4585
4586         return true;
4587 }
4588
4589 bool NVRunVBIOSInit(ScrnInfoPtr pScrn)
4590 {
4591         NVPtr pNv = NVPTR(pScrn);
4592         const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
4593         const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
4594         int offset, ret = 0;
4595
4596         crtc_access(pScrn, ACCESS_UNLOCK);
4597
4598         if ((offset = findstr(pNv->VBIOS.data, pNv->VBIOS.length, bit_signature, sizeof(bit_signature)))) {
4599                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BIT BIOS found\n");
4600                 parse_bit_structure(pScrn, &pNv->VBIOS, offset + 6);
4601         } else if ((offset = findstr(pNv->VBIOS.data, pNv->VBIOS.length, bmp_signature, sizeof(bmp_signature)))) {
4602                 const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
4603                 int hwsq_offset;
4604
4605                 if ((hwsq_offset = findstr(pNv->VBIOS.data, pNv->VBIOS.length, hwsq_signature, sizeof(hwsq_signature))))
4606                         /* always use entry 0? */
4607                         load_nv17_hw_sequencer_ucode(pScrn, &pNv->VBIOS, hwsq_offset + sizeof(hwsq_signature), 0);
4608
4609                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BMP BIOS found\n");
4610                 parse_bmp_structure(pScrn, &pNv->VBIOS, offset);
4611         } else {
4612                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
4613                            "No known BIOS signature found\n");
4614                 ret = 1;
4615         }
4616
4617         crtc_access(pScrn, ACCESS_LOCK);
4618
4619         if (ret)
4620                 return false;
4621
4622         return true;
4623 }
4624
4625 unsigned int NVParseBios(ScrnInfoPtr pScrn)
4626 {
4627         NVPtr pNv = NVPTR(pScrn);
4628         uint32_t saved_nv_pextdev_boot_0;
4629         int i;
4630
4631         if (!NVInitVBIOS(pScrn))
4632                 return 0;
4633
4634         /* these will need remembering across a suspend */
4635         saved_nv_pextdev_boot_0 = nv32_rd(pScrn, NV_PEXTDEV_BOOT_0);
4636         saved_nv_pfb_cfg0 = nv32_rd(pScrn, NV_PFB_CFG0);
4637
4638         /* init script execution disabled */
4639         pNv->VBIOS.execute = false;
4640
4641         nv32_wr(pScrn, NV_PEXTDEV_BOOT_0, saved_nv_pextdev_boot_0);
4642
4643         if (!NVRunVBIOSInit(pScrn))
4644                 return 0;
4645
4646         if (parse_dcb_table(pScrn, &pNv->VBIOS))
4647                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
4648                            "Found %d entries in DCB\n", pNv->dcb_table.entries);
4649
4650         for (i = 0 ; i < pNv->dcb_table.entries; i++)
4651                 if (pNv->dcb_table.entry[i].type == OUTPUT_LVDS)
4652                         call_lvds_script(pScrn, &pNv->dcb_table.entry[i], nv_get_digital_bound_head(pNv, pNv->dcb_table.entry[i].or), LVDS_INIT, 0);
4653
4654         if (pNv->VBIOS.feature_byte & FEATURE_MOBILE && !pNv->VBIOS.fp.native_mode)
4655                 read_bios_edid(pScrn);
4656
4657         /* allow subsequent scripts to execute */
4658         pNv->VBIOS.execute = true;
4659
4660         return 1;
4661 }