randr12: Fix an earlier mistake, make a decent guess about dvi on laptops.
[nouveau] / src / nv_bios.c
1 /*
2  * Copyright 2005-2006 Erik Waling
3  * Copyright 2006 Stephane Marchesin
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 #include "nv_include.h"
25 #include "nvreg.h"
26 #include <byteswap.h>
27
28 /* FIXME: put these somewhere */
29 #define CRTC_INDEX_COLOR VGA_IOBASE_COLOR + VGA_CRTC_INDEX_OFFSET
30 #define NV_VGA_CRTCX_OWNER_HEADA 0x0
31 #define NV_VGA_CRTCX_OWNER_HEADB 0x3
32 #define NV_PBUS_PCI_NV_19 0x0000184C
33 #define NV_PBUS_PCI_NV_20 0x00001850
34 #define NV_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED 0x00000000
35 #define NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED 0x00000001
36 #define NV_PRAMIN_ROM_OFFSET 0x00700000
37
38 #define DEBUGLEVEL 6
39
40 /* TODO: 
41  *       * PLL algorithms.
42  */
43
44 static int crtchead = 0;
45
46 typedef struct {
47         Bool execute;
48         Bool repeat;
49 } init_exec_t;
50
51 static uint16_t le16_to_cpu(const uint16_t x)
52 {
53 #if X_BYTE_ORDER == X_BIG_ENDIAN
54         return bswap_16(x);
55 #else
56         return x;
57 #endif
58 }
59
60 static uint32_t le32_to_cpu(const uint32_t x)
61 {
62 #if X_BYTE_ORDER == X_BIG_ENDIAN
63         return bswap_32(x);
64 #else
65         return x;
66 #endif
67 }
68
69 static Bool nv_cksum(const uint8_t *data, unsigned int length)
70 {
71         /* there's a few checksums in the BIOS, so here's a generic checking function */
72         int i;
73         uint8_t sum = 0;
74
75         for (i = 0; i < length; i++)
76                 sum += data[i];
77
78         if (sum)
79                 return TRUE;
80
81         return FALSE;
82 }
83
84 static int NVValidVBIOS(ScrnInfoPtr pScrn, const uint8_t *data)
85 {
86         /* check for BIOS signature */
87         if (!(data[0] == 0x55 && data[1] == 0xAA)) {
88                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
89                            "... BIOS signature not found\n");
90                 return 0;
91         }
92
93         if (nv_cksum(data, data[2] * 512)) {
94                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
95                            "... BIOS checksum invalid\n");
96                 /* probably ought to set a do_not_execute flag for table parsing here,
97                  * assuming most BIOSen are valid */
98                 return 1;
99         } else
100                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "... appears to be valid\n");
101
102         return 2;
103 }
104
105 static void NVShadowVBIOS_PROM(ScrnInfoPtr pScrn, uint8_t *data)
106 {
107         NVPtr pNv = NVPTR(pScrn);
108         int i;
109
110         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
111                    "Attempting to locate BIOS image in PROM\n");
112
113         /* enable ROM access */
114         nvWriteMC(pNv, NV_PBUS_PCI_NV_20, NV_PBUS_PCI_NV_20_ROM_SHADOW_DISABLED);
115         for (i = 0; i < NV_PROM_SIZE; i++) {
116                 /* according to nvclock, we need that to work around a 6600GT/6800LE bug */
117                 data[i] = pNv->PROM[i];
118                 data[i] = pNv->PROM[i];
119                 data[i] = pNv->PROM[i];
120                 data[i] = pNv->PROM[i];
121                 data[i] = pNv->PROM[i];
122         }
123         /* disable ROM access */
124         nvWriteMC(pNv, NV_PBUS_PCI_NV_20, NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED);
125 }
126
127 static void NVShadowVBIOS_PRAMIN(ScrnInfoPtr pScrn, uint32_t *data)
128 {
129         NVPtr pNv = NVPTR(pScrn);
130         const uint32_t *pramin = (uint32_t *)&pNv->REGS[NV_PRAMIN_ROM_OFFSET/4];
131         uint32_t old_bar0_pramin = 0;
132
133         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
134                    "Attempting to locate BIOS image in PRAMIN\n");
135
136         if (pNv->Architecture >= NV_ARCH_50) {
137                 uint32_t vbios_vram;
138
139                 vbios_vram = (pNv->REGS[0x619f04/4] & ~0xff) << 8;
140                 if (!vbios_vram) {
141                         vbios_vram = pNv->REGS[0x1700/4] << 16;
142                         vbios_vram += 0xf0000;
143                 }
144
145                 old_bar0_pramin = pNv->REGS[0x1700/4];
146                 pNv->REGS[0x1700/4] = vbios_vram >> 16;
147         }
148
149         memcpy(data, pramin, NV_PROM_SIZE);
150
151         if (pNv->Architecture >= NV_ARCH_50) {
152                 pNv->REGS[0x1700/4] = old_bar0_pramin;
153         }
154 }
155
156 static Bool NVShadowVBIOS(ScrnInfoPtr pScrn, uint8_t *data)
157 {
158         NVShadowVBIOS_PROM(pScrn, data);
159         if (NVValidVBIOS(pScrn, data) == 2)
160                 return TRUE;
161
162         NVShadowVBIOS_PRAMIN(pScrn, (uint32_t *)data);
163         if (NVValidVBIOS(pScrn, data))
164                 return TRUE;
165
166         return FALSE;
167 }
168
169 typedef struct {
170         char* name;
171         uint8_t id;
172         int length;
173         int length_offset;
174         int length_multiplier;
175         Bool (*handler)(ScrnInfoPtr pScrn, bios_t *, uint16_t, init_exec_t *);
176 } init_tbl_entry_t;
177
178 typedef struct {
179         uint8_t id[2];
180         uint16_t length;
181         uint16_t offset;
182 } bit_entry_t;
183
184 static void parse_init_table(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset, init_exec_t *iexec);
185
186 #define MACRO_INDEX_SIZE        2
187 #define MACRO_SIZE              8
188 #define CONDITION_SIZE          12
189 #define IO_FLAG_CONDITION_SIZE  9 
190
191 void still_alive()
192 {
193         sync();
194 //      usleep(200000);
195 }
196
197 static int nv_valid_reg(uint32_t reg)
198 {
199         #define WITHIN(x,y,z) ((x>=y)&&(x<y+z))
200         if (WITHIN(reg,NV_PRAMIN_OFFSET,NV_PRAMIN_SIZE))
201                 return 1;
202         if (WITHIN(reg,NV_PCRTC0_OFFSET,NV_PCRTC0_SIZE))
203                 return 1;
204         if (WITHIN(reg,NV_PRAMDAC0_OFFSET,NV_PRAMDAC0_SIZE))
205                 return 1;
206         if (WITHIN(reg,NV_PFB_OFFSET,NV_PFB_SIZE))
207                 return 1;
208         if (WITHIN(reg,NV_PFIFO_OFFSET,NV_PFIFO_SIZE))
209                 return 1;
210         if (WITHIN(reg,NV_PGRAPH_OFFSET,NV_PGRAPH_SIZE))
211                 return 1;
212         if (WITHIN(reg,NV_PEXTDEV_OFFSET,NV_PEXTDEV_SIZE))
213                 return 1;
214         if (WITHIN(reg,NV_PTIMER_OFFSET,NV_PTIMER_SIZE))
215                 return 1;
216         if (WITHIN(reg,NV_PVIDEO_OFFSET,NV_PVIDEO_SIZE))
217                 return 1;
218         if (WITHIN(reg,NV_PMC_OFFSET,NV_PMC_SIZE))
219                 return 1;
220         if (WITHIN(reg,NV_FIFO_OFFSET,NV_FIFO_SIZE))
221                 return 1;
222         if (WITHIN(reg,NV_PCIO0_OFFSET,NV_PCIO0_SIZE))
223                 return 1;
224         if (WITHIN(reg,NV_PDIO0_OFFSET,NV_PDIO0_SIZE))
225                 return 1;
226         if (WITHIN(reg,NV_PVIO_OFFSET,NV_PVIO_SIZE))
227                 return 1;
228         if (WITHIN(reg,NV_PROM_OFFSET,NV_PROM_SIZE))
229                 return 1;
230         if (WITHIN(reg,NV_PRAMIN_ROM_OFFSET,NV_PROM_SIZE))
231                 return 1;
232         /* A new PBUS? */
233         if (WITHIN(reg,0x88000,0x1000))
234                 return 1;
235         #undef WITHIN
236         return 0;
237 }
238
239 static void nv32_rd(ScrnInfoPtr pScrn, uint32_t reg, uint32_t *data)
240 {
241         NVPtr pNv = NVPTR(pScrn);
242
243         if (!nv_valid_reg(reg)) {
244                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
245                            "========= unknown reg 0x%08X ==========\n", reg);
246                 return;
247         }
248         *data = pNv->REGS[reg/4];
249         if (DEBUGLEVEL >= 6)
250                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
251                            "    Read:  Reg: 0x%08X, Data: 0x%08X\n", reg, *data);
252 }
253
254 static int nv32_wr(ScrnInfoPtr pScrn, uint32_t reg, uint32_t data)
255 {
256         NVPtr pNv = NVPTR(pScrn);
257         int specialcase = 0;
258         uint8_t saved1 = 0, saved2 = 0;
259         volatile uint8_t *crtcptr = crtchead ? pNv->PCIO1 : pNv->PCIO0;
260
261         if (DEBUGLEVEL >= 8) {
262                 uint32_t tmp;
263                 nv32_rd(pScrn, reg, &tmp);
264         }
265         if (DEBUGLEVEL >= 6)
266                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
267                            "    Write: Reg: 0x%08X, Data: 0x%08X\n", reg, data);
268         if (!nv_valid_reg(reg)) {
269                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
270                            "========= unknown reg 0x%08X ==========\n", reg);
271                 return 0;
272         }
273
274         if (pNv->VBIOS.execute) {
275                 still_alive();
276
277                 if ((reg & 0xffc) == 0x3c0) {
278                         specialcase = 1;
279                         saved1 = VGA_RD08(crtcptr, VGA_MISC_OUT_R);
280                         saved2 = VGA_RD08(crtcptr, VGA_ENABLE);
281                 }
282                 if ((reg & 0xffc) == 0x3cc) {
283                         specialcase = 2;
284                         saved1 = VGA_RD08(crtcptr, VGA_GRAPH_INDEX);
285                         VGA_WR08(crtcptr, VGA_GRAPH_INDEX, 0x06);
286                         saved2 = VGA_RD08(crtcptr, VGA_GRAPH_DATA);
287                 }
288
289                 pNv->REGS[reg/4] = data;
290
291                 if (specialcase == 1) {
292                         VGA_WR08(crtcptr, VGA_ENABLE, saved2);
293                         VGA_WR08(crtcptr, VGA_MISC_OUT_W, saved1);
294                 }
295                 if (specialcase == 2) {
296                         VGA_WR08(crtcptr, VGA_GRAPH_INDEX, 0x06);
297                         VGA_WR08(crtcptr, VGA_GRAPH_DATA, saved2);
298                         VGA_WR08(crtcptr, VGA_GRAPH_INDEX, saved1);
299                 }
300         }
301
302         return 1;
303 }
304
305 static void nv_idx_port_rd(ScrnInfoPtr pScrn, uint16_t port, uint8_t index, uint8_t *data)
306 {
307         NVPtr pNv = NVPTR(pScrn);
308         volatile uint8_t *ptr = crtchead ? pNv->PCIO1 : pNv->PCIO0;
309
310         VGA_WR08(ptr, port, index);
311         *data = VGA_RD08(ptr, port + 1);
312
313         if (DEBUGLEVEL >= 6)
314                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
315                            "    Indexed read:  Port: 0x%04X, Index: 0x%02X, Head: 0x%02X, Data: 0x%02X\n",
316                            port, index, crtchead, *data);
317 }
318
319 static void nv_idx_port_wr(ScrnInfoPtr pScrn, uint16_t port, uint8_t index, uint8_t data)
320 {
321         NVPtr pNv = NVPTR(pScrn);
322         volatile uint8_t *ptr;
323
324         /* The current head is maintained in a file scope variable crtchead.
325          * We trap changes to CRTCX_OWNER and update the head variable
326          * and hence the register set written.
327          * As CRTCX_OWNER only exists on CRTC0, we update crtchead to head0
328          * in advance of the write, and to head1 after the write
329          */
330         if (port == CRTC_INDEX_COLOR && index == NV_VGA_CRTCX_OWNER && data != NV_VGA_CRTCX_OWNER_HEADB)
331                 crtchead = 0;
332         ptr = crtchead ? pNv->PCIO1 : pNv->PCIO0;
333
334         if (DEBUGLEVEL >= 8) {
335                 uint8_t tmp;
336                 nv_idx_port_rd(pScrn, port, index, &tmp);
337         }
338         if (DEBUGLEVEL >= 6)
339                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
340                            "    Indexed write: Port: 0x%04X, Index: 0x%02X, Head: 0x%02X, Data: 0x%02X\n",
341                            port, index, crtchead, data);
342
343         if (pNv->VBIOS.execute) {
344                 still_alive();
345                 VGA_WR08(ptr, port, index);
346                 VGA_WR08(ptr, port + 1, data);
347         }
348
349         if (port == CRTC_INDEX_COLOR && index == NV_VGA_CRTCX_OWNER && data == NV_VGA_CRTCX_OWNER_HEADB)
350                 crtchead = 1;
351 }
352
353 static Bool io_flag_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, uint8_t cond)
354 {
355         /* The IO flag condition entry has 2 bytes for the CRTC port; 1 byte
356          * for the CRTC index; 1 byte for the mask to apply to the value
357          * retrieved from the CRTC; 1 byte for the shift right to apply to the
358          * masked CRTC value; 2 bytes for the offset to the flag array, to
359          * which the shifted value is added; 1 byte for the mask applied to the
360          * value read from the flag array; and 1 byte for the value to compare
361          * against the masked byte from the flag table.
362          */
363
364         uint16_t condptr = bios->io_flag_condition_tbl_ptr + cond * IO_FLAG_CONDITION_SIZE;
365         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[condptr])));
366         uint8_t crtcindex = bios->data[condptr + 2];
367         uint8_t mask = bios->data[condptr + 3];
368         uint8_t shift = bios->data[condptr + 4];
369         uint16_t flagarray = le16_to_cpu(*((uint16_t *)(&bios->data[condptr + 5])));
370         uint8_t flagarraymask = bios->data[condptr + 7];
371         uint8_t cmpval = bios->data[condptr + 8];
372         uint8_t data;
373
374         if (DEBUGLEVEL >= 6)
375                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
376                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, FlagArray: 0x%04X, FAMask: 0x%02X, Cmpval: 0x%02X\n",
377                            offset, crtcport, crtcindex, mask, shift, flagarray, flagarraymask, cmpval);
378
379         nv_idx_port_rd(pScrn, crtcport, crtcindex, &data);
380
381         data = bios->data[flagarray + ((data & mask) >> shift)];
382         data &= flagarraymask;
383
384         if (DEBUGLEVEL >= 6)
385                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
386                            "0x%04X: Checking if 0x%02X equals 0x%02X\n",
387                            offset, data, cmpval);
388
389         if (data == cmpval)
390                 return TRUE;
391
392         return FALSE;
393 }
394
395 static Bool init_prog(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
396 {
397         /* INIT_PROG   opcode: 0x31
398          * 
399          * offset      (8  bit): opcode
400          * offset + 1  (32 bit): reg
401          * offset + 5  (32 bit): and mask
402          * offset + 9  (8  bit): shift right
403          * offset + 10 (8  bit): number of configurations
404          * offset + 11 (32 bit): register
405          * offset + 15 (32 bit): configuration 1
406          * ...
407          * 
408          * Starting at offset + 15 there are "number of configurations"
409          * 32 bit values. To find out which configuration value to use
410          * read "CRTC reg" on the CRTC controller with index "CRTC index"
411          * and bitwise AND this value with "and mask" and then bit shift the
412          * result "shift right" bits to the right.
413          * Assign "register" with appropriate configuration value.
414          */
415
416         CARD32 reg = *((CARD32 *) (&bios->data[offset + 1]));
417         CARD32 and = *((CARD32 *) (&bios->data[offset + 5]));
418         CARD8 shiftr = *((CARD8 *) (&bios->data[offset + 9]));
419         CARD8 nr = *((CARD8 *) (&bios->data[offset + 10]));
420         CARD32 reg2 = *((CARD32 *) (&bios->data[offset + 11]));
421         CARD8 configuration;
422         CARD32 configval, tmp;
423
424         if (iexec->execute) {
425                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: REG: 0x%04X\n", offset, 
426                                 reg);
427
428                 nv32_rd(pScrn, reg, &tmp);
429                 configuration = (tmp & and) >> shiftr;
430
431                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CONFIGURATION TO USE: 0x%02X\n", 
432                                 offset, configuration);
433
434                 if (configuration <= nr) {
435
436                         configval = 
437                                 *((CARD32 *) (&bios->data[offset + 15 + configuration * 4]));
438
439                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: REG: 0x%08X, VALUE: 0x%08X\n", offset, 
440                                         reg2, configval);
441                         
442                         nv32_rd(pScrn, reg2, &tmp);
443                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CURRENT VALUE IS: 0x%08X\n",
444                                 offset, tmp);
445                         nv32_wr(pScrn, reg2, configval);
446                 }
447         }
448         return TRUE;
449 }
450
451 static Bool init_io_restrict_prog(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
452 {
453         /* INIT_IO_RESTRICT_PROG   opcode: 0x32 ('2')
454          * 
455          * offset      (8  bit): opcode
456          * offset + 1  (16 bit): CRTC port
457          * offset + 3  (8  bit): CRTC index
458          * offset + 4  (8  bit): mask
459          * offset + 5  (8  bit): shift
460          * offset + 6  (8  bit): count
461          * offset + 7  (32 bit): register
462          * offset + 11 (32 bit): configuration 1
463          * ...
464          * 
465          * Starting at offset + 11 there are "count" 32 bit values.
466          * To find out which value to use read index "CRTC index" on "CRTC port",
467          * AND this value with "mask" and then bit shift right "shift" bits.
468          * Read the appropriate value using this index and write to "register"
469          */
470
471         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
472         uint8_t crtcindex = bios->data[offset + 3];
473         uint8_t mask = bios->data[offset + 4];
474         uint8_t shift = bios->data[offset + 5];
475         uint8_t count = bios->data[offset + 6];
476         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7])));
477         uint8_t config;
478         uint32_t configval;
479
480         if (!iexec->execute)
481                 return TRUE;
482
483         if (DEBUGLEVEL >= 6)
484                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
485                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
486                            offset, crtcport, crtcindex, mask, shift, count, reg);
487
488         nv_idx_port_rd(pScrn, crtcport, crtcindex, &config);
489         config = (config & mask) >> shift;
490         if (config > count) {
491                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
492                            "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
493                            offset, config, count);
494                 return FALSE;
495         }
496
497         configval = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 11 + config * 4])));
498
499         if (DEBUGLEVEL >= 6)
500                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
501                            "0x%04X: Writing config %02X\n", offset, config);
502
503         nv32_wr(pScrn, reg, configval);
504
505         return TRUE;
506 }
507
508 static Bool init_repeat(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
509 {
510         /* INIT_REPEAT   opcode: 0x33 ('3')
511          *
512          * offset      (8 bit): opcode
513          * offset + 1  (8 bit): count
514          *
515          * Execute script following this opcode up to INIT_REPEAT_END
516          * "count" times
517          */
518
519         uint8_t count = bios->data[offset + 1];
520         uint8_t i;
521
522         /* no iexec->execute check by design */
523
524         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
525                    "0x%04X: REPEATING FOLLOWING SEGMENT %d TIMES.\n",
526                    offset, count);
527
528         iexec->repeat = TRUE;
529
530         /* count - 1, as the script block will execute once when we leave this
531          * opcode -- this is compatible with bios behaviour as:
532          * a) the block is always executed at least once, even if count == 0
533          * b) the bios interpreter skips to the op following INIT_END_REPEAT,
534          * while we don't
535          */
536         for (i = 0; i < count - 1; i++)
537                 parse_init_table(pScrn, bios, offset + 2, iexec);
538
539         iexec->repeat = FALSE;
540
541         return TRUE;
542 }
543
544 static Bool init_io_restrict_pll(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
545 {
546         /* INIT_IO_RESTRICT_PLL   opcode: 0x34 ('4')
547          *
548          * offset      (8  bit): opcode
549          * offset + 1  (16 bit): CRTC port
550          * offset + 3  (8  bit): CRTC index
551          * offset + 4  (8  bit): mask
552          * offset + 5  (8  bit): shift
553          * offset + 6  (8  bit): IO flag condition index
554          * offset + 7  (8  bit): count
555          * offset + 8  (32 bit): register
556          * offset + 12 (16 bit): frequency 1
557          * ...
558          *
559          * Starting at offset + 12 there are "count" 16 bit frequencies (10kHz).
560          * Set PLL register "register" to coefficients for frequency n,
561          * selected by reading index "CRTC index" of "CRTC port" ANDed with
562          * "mask" and shifted right by "shift". If "IO flag condition index" > 0,
563          * and condition met, double frequency before setting it.
564          */
565
566         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
567         uint8_t crtcindex = bios->data[offset + 3];
568         uint8_t mask = bios->data[offset + 4];
569         uint8_t shift = bios->data[offset + 5];
570         int8_t io_flag_condition_idx = bios->data[offset + 6];
571         uint8_t count = bios->data[offset + 7];
572         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 8])));
573         uint8_t config;
574         uint16_t freq;
575
576         if (!iexec->execute)
577                 return TRUE;
578
579         if (DEBUGLEVEL >= 6)
580                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
581                            "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",
582                            offset, crtcport, crtcindex, mask, shift, io_flag_condition_idx, count, reg);
583
584         nv_idx_port_rd(pScrn, crtcport, crtcindex, &config);
585         config = (config & mask) >> shift;
586         if (config > count) {
587                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
588                            "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
589                            offset, config, count);
590                 return FALSE;
591         }
592
593         freq = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 12 + config * 2])));
594
595         if (io_flag_condition_idx > 0) {
596                 if (io_flag_condition(pScrn, bios, offset, io_flag_condition_idx)) {
597                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
598                                    "0x%04X: CONDITION FULFILLED - FREQ DOUBLED\n", offset);
599                         freq *= 2;
600                 } else
601                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
602                                    "0x%04X: CONDITION IS NOT FULFILLED. FREQ UNCHANGED\n", offset);
603         }
604
605         if (DEBUGLEVEL >= 6)
606                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
607                            "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %d0kHz\n",
608                            offset, reg, config, freq);
609
610         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ NOT YET IMPLEMENTED ]\n", offset);
611
612 #if 0
613         switch (reg) {
614         case 0x00004004:
615                 configval = 0x01014E07;
616                 break;
617         case 0x00004024:
618                 configval = 0x13030E02;
619                 break;
620         }
621 #endif
622         return TRUE;
623 }
624
625 static Bool init_end_repeat(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
626 {
627         /* INIT_END_REPEAT   opcode: 0x36 ('6')
628          *
629          * offset      (8 bit): opcode
630          *
631          * Marks the end of the block for INIT_REPEAT to repeat
632          */
633
634         /* no iexec->execute check by design */
635
636         /* iexec->repeat flag necessary to go past INIT_END_REPEAT opcode when
637          * we're not in repeat mode
638          */
639         if (iexec->repeat)
640                 return FALSE;
641
642         return TRUE;
643 }
644
645 static Bool init_copy(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
646 {
647         /* INIT_COPY   opcode: 0x37 ('7')
648          *
649          * offset      (8  bit): opcode
650          * offset + 1  (32 bit): register
651          * offset + 5  (8  bit): shift
652          * offset + 6  (8  bit): srcmask
653          * offset + 7  (16 bit): CRTC port
654          * offset + 9  (8 bit): CRTC index
655          * offset + 10  (8 bit): mask
656          *
657          * Read index "CRTC index" on "CRTC port", AND with "mask", OR with
658          * (REGVAL("register") >> "shift" & "srcmask") and write-back to CRTC port
659          */
660
661         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
662         uint8_t shift = bios->data[offset + 5];
663         uint8_t srcmask = bios->data[offset + 6];
664         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 7])));
665         uint8_t crtcindex = bios->data[offset + 9];
666         uint8_t mask = bios->data[offset + 10];
667         uint32_t data;
668         uint8_t crtcdata;
669
670         if (!iexec->execute)
671                 return TRUE;
672
673         if (DEBUGLEVEL >= 6)
674                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
675                            "0x%04X: Reg: 0x%08X, Shift: 0x%02X, SrcMask: 0x%02X, Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X\n",
676                            offset, reg, shift, srcmask, crtcport, crtcindex, mask);
677
678         nv32_rd(pScrn, reg, &data);
679
680         if (shift < 0x80)
681                 data >>= shift;
682         else
683                 data <<= (0x100 - shift);
684
685         data &= srcmask;
686
687         nv_idx_port_rd(pScrn, crtcport, crtcindex, &crtcdata);
688         crtcdata = (crtcdata & mask) | (uint8_t)data;
689         nv_idx_port_wr(pScrn, crtcport, crtcindex, crtcdata);
690
691         return TRUE;
692 }
693
694 static Bool init_not(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
695 {
696         /* INIT_NOT   opcode: 0x38 ('8')
697          *
698          * offset      (8  bit): opcode
699          *
700          * Invert the current execute / no-execute condition (i.e. "else")
701          */
702         if (iexec->execute)
703                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
704                            "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
705         else
706                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
707                            "0x%04X: ------ EXECUTING FOLLOWING COMMANDS ------\n", offset);
708
709         iexec->execute = !iexec->execute;
710         return TRUE;
711 }
712
713 static Bool init_io_flag_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
714 {
715         /* INIT_IO_FLAG_CONDITION   opcode: 0x39 ('9')
716          *
717          * offset      (8 bit): opcode
718          * offset + 1  (8 bit): condition number
719          *
720          * Check condition "condition number" in the IO flag condition table.
721          * If condition not met skip subsequent opcodes until condition
722          * is inverted (INIT_NOT), or we hit INIT_RESUME
723          */
724
725         uint8_t cond = bios->data[offset + 1];
726
727         if (!iexec->execute)
728                 return TRUE;
729
730         if (io_flag_condition(pScrn, bios, offset, cond))
731                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
732                            "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
733         else {
734                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
735                            "0x%04X: CONDITION IS NOT FULFILLED.\n", offset);
736                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
737                            "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
738                 iexec->execute = FALSE;
739         }
740
741         return TRUE;
742 }
743
744 Bool init_idx_addr_latched(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
745 {
746         /* INIT_INDEX_ADDRESS_LATCHED   opcode: 0x49 ('I')
747          *
748          * offset      (8  bit): opcode
749          * offset + 1  (32 bit): control register
750          * offset + 5  (32 bit): data register
751          * offset + 9  (32 bit): mask
752          * offset + 13 (32 bit): data
753          * offset + 17 (8  bit): count
754          * offset + 18 (8  bit): address 1
755          * offset + 19 (8  bit): data 1
756          * ...
757          *
758          * For each of "count" address and data pairs, write "data n" to "data register",
759          * read the current value of "control register", and write it back once ANDed
760          * with "mask", ORed with "data", and ORed with "address n"
761          */
762
763         uint32_t controlreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
764         uint32_t datareg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
765         uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
766         uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 13])));
767         uint8_t count = bios->data[offset + 17];
768         uint32_t value;
769         int i;
770
771         if (!iexec->execute)
772                 return TRUE;
773
774         if (DEBUGLEVEL >= 6)
775                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
776                            "0x%04X: ControlReg: 0x%08X, DataReg: 0x%08X, Mask: 0x%08X, Data: 0x%08X, Count: 0x%02X\n",
777                            offset, controlreg, datareg, mask, data, count);
778
779         for (i = 0; i < count; i++) {
780                 uint8_t instaddress = bios->data[offset + 18 + i * 2];
781                 uint8_t instdata = bios->data[offset + 19 + i * 2];
782
783                 if (DEBUGLEVEL >= 6)
784                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
785                                    "0x%04X: Address: 0x%02X, Data: 0x%02X\n", offset, instaddress, instdata);
786
787                 nv32_wr(pScrn, datareg, instdata);
788
789                 nv32_rd(pScrn, controlreg, &value);
790                 value = (value & mask) | data | instaddress;
791
792                 nv32_wr(pScrn, controlreg, value);
793         }
794
795         return TRUE;
796 }
797
798 static Bool init_io_restrict_pll2(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
799 {
800         /* INIT_IO_RESTRICT_PLL2   opcode: 0x4A ('J')
801          *
802          * offset      (8  bit): opcode
803          * offset + 1  (16 bit): CRTC port
804          * offset + 3  (8  bit): CRTC index
805          * offset + 4  (8  bit): mask
806          * offset + 5  (8  bit): shift
807          * offset + 6  (8  bit): count
808          * offset + 7  (32 bit): register
809          * offset + 11 (32 bit): frequency 1
810          * ...
811          *
812          * Starting at offset + 11 there are "count" 32 bit frequencies (kHz).
813          * Set PLL register "register" to coefficients for frequency n,
814          * selected by reading index "CRTC index" of "CRTC port" ANDed with
815          * "mask" and shifted right by "shift".
816          */
817
818         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
819         uint8_t crtcindex = bios->data[offset + 3];
820         uint8_t mask = bios->data[offset + 4];
821         uint8_t shift = bios->data[offset + 5];
822         uint8_t count = bios->data[offset + 6];
823         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7])));
824         uint8_t config;
825         uint32_t freq;
826
827         if (!iexec->execute)
828                 return TRUE;
829
830         if (DEBUGLEVEL >= 6)
831                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
832                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Shift: 0x%02X, Count: 0x%02X, Reg: 0x%08X\n",
833                            offset, crtcport, crtcindex, mask, shift, count, reg);
834
835         if (!reg)
836                 return TRUE;
837
838         nv_idx_port_rd(pScrn, crtcport, crtcindex, &config);
839         config = (config & mask) >> shift;
840         if (config > count) {
841                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
842                            "0x%04X: Config 0x%02X exceeds maximal bound 0x%02X\n",
843                            offset, config, count);
844                 return FALSE;
845         }
846
847         freq = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 11 + config * 4])));
848
849         if (DEBUGLEVEL >= 6)
850                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
851                            "0x%04X: Reg: 0x%08X, Config: 0x%02X, Freq: %dkHz\n",
852                            offset, reg, config, freq);
853
854         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ NOT YET IMPLEMENTED ]\n", offset);
855
856         return TRUE;
857 }
858
859 static Bool init_pll2(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
860 {
861         /* INIT_PLL2   opcode: 0x4B ('K')
862          *
863          * offset      (8  bit): opcode
864          * offset + 1  (32 bit): register
865          * offset + 5  (32 bit): freq
866          *
867          * Set PLL register "register" to coefficients for frequency "freq"
868          */
869
870         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
871         uint32_t freq = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
872
873         if (!iexec->execute)
874                 return TRUE;
875
876         if (DEBUGLEVEL >= 6)
877                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
878                            "0x%04X: Reg: 0x%04X, Freq: %dkHz\n",
879                            offset, reg, freq);
880
881         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ NOT YET IMPLEMENTED ]\n", offset);
882
883         return TRUE;
884 }
885
886 Bool init_50(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
887 {
888         /* INIT_50   opcode: 0x50 ('P')
889          *
890          * offset      (8 bit): opcode
891          * offset + 1  (8 bit): magic lookup value
892          * offset + 2  (8 bit): count
893          * offset + 3  (8 bit): addr 1
894          * offset + 4  (8 bit): data 1
895          * ...
896          *
897          * For each of "count" TMDS address and data pairs write "data n" to "addr n"
898          * "magic lookup value" (mlv) determines which TMDS base address is used:
899          * For mlv < 0x80, it is an index into a table of TMDS base addresses
900          * For mlv == 0x80 use the "or" value of the dcb_entry indexed by CR58 for CR57 = 0
901          * to index a table of offsets to the basic 0x6808b0 address
902          * For mlv == 0x81 use the "or" value of the dcb_entry indexed by CR58 for CR57 = 0
903          * to index a table of offsets to the basic 0x6808b0 address, and then flip the offset by 8
904          */
905         NVPtr pNv = NVPTR(pScrn);
906         uint8_t mlv = bios->data[offset + 1];
907         uint8_t count = bios->data[offset + 2];
908         uint32_t reg;
909         int i;
910
911         int pramdac_offset[13] = {0, 0, 0x8, 0, 0x2000, 0, 0, 0, 0x2008, 0, 0, 0, 0x2000};
912         uint32_t pramdac_table[4] = {0x6808b0, 0x6808b8, 0x6828b0, 0x6828b8};
913
914         if (!iexec->execute)
915                 return TRUE;
916
917         if (DEBUGLEVEL >= 6)
918                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
919                            "0x%04X: MagicLookupValue: 0x%02X, Count: 0x%02X\n",
920                            offset, mlv, count);
921         if (mlv >= 0x80) {
922                 /* here we assume that the DCB table has already been parsed */
923                 uint8_t dcb_entry;
924                 int dacoffset;
925                 /* This register needs to written for correct output */
926                 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, 0x57, 0);
927                 nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, 0x58, &dcb_entry);
928                 if (dcb_entry > pNv->dcb_table.entries) {
929                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
930                                    "0x%04X: CR58 doesn't have a valid DCB entry currently (%02X)\n",
931                                    offset, dcb_entry);
932                         return FALSE;
933                 }
934                 dacoffset = pramdac_offset[pNv->dcb_table.entry[dcb_entry].or];
935                 if (mlv == 0x81)
936                         dacoffset ^= 8;
937                 reg = 0x6808b0 + dacoffset;
938         } else {
939                 if (mlv > (sizeof(pramdac_table) / sizeof(uint32_t))) {
940                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
941                                    "0x%04X: Magic Lookup Value too big (%02X)\n", offset, mlv);
942                         return FALSE;
943                 }
944                 reg = pramdac_table[mlv];
945         }
946
947         for (i = 0; i < count; i++) {
948                 uint8_t tmds_addr = bios->data[offset + 3 + i * 2];
949                 uint8_t tmds_data = bios->data[offset + 4 + i * 2];
950
951                 nv32_wr(pScrn, reg + 4, tmds_data);
952                 nv32_wr(pScrn, reg, tmds_addr);
953         }
954
955         return TRUE;
956 }
957         
958 Bool init_cr_idx_adr_latch(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
959 {
960         /* INIT_CR_INDEX_ADDRESS_LATCHED   opcode: 0x51 ('Q')
961          *
962          * offset      (8 bit): opcode
963          * offset + 1  (8 bit): CRTC index1
964          * offset + 2  (8 bit): CRTC index2
965          * offset + 3  (8 bit): baseaddr
966          * offset + 4  (8 bit): count
967          * offset + 5  (8 bit): data 1
968          * ...
969          *
970          * For each of "count" address and data pairs, write "baseaddr + n" to
971          * "CRTC index1" and "data n" to "CRTC index2"
972          * Once complete, restore initial value read from "CRTC index1"
973          */
974         uint8_t crtcindex1 = bios->data[offset + 1];
975         uint8_t crtcindex2 = bios->data[offset + 2];
976         uint8_t baseaddr = bios->data[offset + 3];
977         uint8_t count = bios->data[offset + 4];
978         uint8_t oldaddr, data;
979         int i;
980
981         if (!iexec->execute)
982                 return TRUE;
983
984         if (DEBUGLEVEL >= 6)
985                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
986                            "0x%04X: Index1: 0x%02X, Index2: 0x%02X, BaseAddr: 0x%02X, Count: 0x%02X\n",
987                            offset, crtcindex1, crtcindex2, baseaddr, count);
988
989         nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, crtcindex1, &oldaddr);
990
991         for (i = 0; i < count; i++) {
992                 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex1, baseaddr + i);
993
994                 data = bios->data[offset + 5 + i];
995                 nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex2, data);
996         }
997
998         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex1, oldaddr);
999
1000         return TRUE;
1001 }
1002
1003 Bool init_cr(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1004 {
1005         /* INIT_CR   opcode: 0x52 ('R')
1006          *
1007          * offset      (8  bit): opcode
1008          * offset + 1  (8  bit): CRTC index
1009          * offset + 2  (8  bit): mask
1010          * offset + 3  (8  bit): data
1011          *
1012          * Assign the value of at "CRTC index" ANDed with mask and ORed with data
1013          * back to "CRTC index"
1014          */
1015
1016         uint8_t crtcindex = bios->data[offset + 1];
1017         uint8_t mask = bios->data[offset + 2];
1018         uint8_t data = bios->data[offset + 3];
1019         uint8_t value;
1020
1021         if (!iexec->execute)
1022                 return TRUE;
1023
1024         if (DEBUGLEVEL >= 6)
1025                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1026                            "0x%04X: Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1027                            offset, crtcindex, mask, data);
1028
1029         nv_idx_port_rd(pScrn, CRTC_INDEX_COLOR, crtcindex, &value);
1030
1031         value = (value & mask) | data;
1032
1033         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex, value);
1034
1035         return TRUE;
1036 }
1037
1038 static Bool init_zm_cr(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1039 {
1040         /* INIT_ZM_CR   opcode: 0x53 ('S')
1041          *
1042          * offset      (8 bit): opcode
1043          * offset + 1  (8 bit): CRTC index
1044          * offset + 2  (8 bit): value
1045          *
1046          * Assign "value" to CRTC register with index "CRTC index".
1047          */
1048
1049         uint8_t crtcindex = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1050         uint8_t data = bios->data[offset + 2];
1051
1052         if (!iexec->execute)
1053                 return TRUE;
1054
1055         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, crtcindex, data);
1056
1057         return TRUE;
1058 }
1059
1060 static Bool init_zm_cr_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1061 {
1062         /* INIT_ZM_CR   opcode: 0x54 ('T')
1063          * 
1064          * offset      (8 bit): opcode
1065          * offset + 1  (8 bit): count
1066          * offset + 2  (8 bit): CRTC index 1
1067          * offset + 3  (8 bit): value 1
1068          * ...
1069          * 
1070          * For "count", assign "value n" to CRTC register with index "CRTC index n".
1071          */
1072     
1073         uint8_t count = bios->data[offset + 1];
1074         int i;
1075         
1076         if (!iexec->execute)
1077                 return TRUE;
1078
1079         for (i = 0; i < count; i++)
1080                 init_zm_cr(pScrn, bios, offset + 2 + 2 * i - 1, iexec);
1081
1082         return TRUE;
1083 }
1084
1085 static Bool init_condition_time(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1086 {
1087         /* My BIOS does not use this command. */
1088         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: [ NOT YET IMPLEMENTED ]\n", offset);
1089
1090         return FALSE;
1091 }
1092
1093 static Bool init_zm_reg_sequence(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1094 {
1095         /* INIT_ZM_REG_SEQUENCE   opcode: 0x58 ('X')
1096          * 
1097          * offset      (8  bit): opcode
1098          * offset + 1  (32 bit): base register
1099          * offset + 5  (8  bit): count
1100          * offset + 6  (32 bit): value 1
1101          * ...
1102          * 
1103          * Starting at offset + 6 there are "count" 32 bit values.
1104          * For "count" iterations set "base register" + 4 * current_iteration
1105          * to "value current_iteration"
1106          */
1107
1108         uint32_t basereg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1109         uint32_t count = bios->data[offset + 5];
1110         int i;
1111
1112         if (!iexec->execute)
1113                 return TRUE;
1114
1115         if (DEBUGLEVEL >= 6)
1116                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1117                            "0x%04X: BaseReg: 0x%08X, Count: 0x%02X\n",
1118                            offset, basereg, count);
1119
1120         for (i = 0; i < count; i++) {
1121                 uint32_t reg = basereg + i * 4;
1122                 uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 6 + i * 4])));
1123
1124                 nv32_wr(pScrn, reg, data);
1125         }
1126
1127         return TRUE;
1128 }
1129
1130 static Bool init_indirect_reg(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1131 {
1132         /* INIT_INDIRECT_REG opcode: 0x5A
1133          * 
1134          * offset      (8  bit): opcode
1135          * offset + 1  (32 bit): register
1136          * offset + 5  (16 bit): adress offset (in bios)
1137          *
1138          * Lookup value at offset data in the bios and write it to reg
1139          */
1140         CARD32 reg = *((CARD32 *) (&bios->data[offset + 1]));
1141         CARD16 data = le16_to_cpu(*((CARD16 *) (&bios->data[offset + 5])));
1142         CARD32 data2 = bios->data[data];
1143
1144         if (iexec->execute) {
1145                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1146                                 "0x%04X: REG: 0x%04X, DATA AT: 0x%04X, VALUE IS: 0x%08X\n", 
1147                                 offset, reg, data, data2);
1148
1149                 if (DEBUGLEVEL >= 6) {
1150                         CARD32 tmpval;
1151                         nv32_rd(pScrn, reg, &tmpval);
1152                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CURRENT VALUE IS: 0x%08X\n", offset, tmpval);
1153                 }
1154
1155                 nv32_wr(pScrn, reg, data2);
1156         }
1157         return TRUE;
1158 }
1159
1160 static Bool init_sub_direct(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1161 {
1162         /* INIT_SUB_DIRECT   opcode: 0x5B ('[')
1163          * 
1164          * offset      (8  bit): opcode
1165          * offset + 1  (16 bit): subroutine offset (in bios)
1166          *
1167          * Calls a subroutine that will execute commands until INIT_DONE
1168          * is found. 
1169          */
1170
1171         uint16_t sub_offset = le16_to_cpu(*((uint16_t *) (&bios->data[offset + 1])));
1172
1173         if (!iexec->execute)
1174                 return TRUE;
1175
1176         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: EXECUTING SUB-ROUTINE AT 0x%04X\n",
1177                         offset, sub_offset);
1178
1179         parse_init_table(pScrn, bios, sub_offset, iexec);
1180
1181         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: END OF SUB-ROUTINE AT 0x%04X\n",
1182                         offset, sub_offset);
1183
1184         return TRUE;
1185 }
1186
1187 static Bool init_copy_nv_reg(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1188 {   
1189         CARD32 srcreg = *((CARD32 *) (&bios->data[offset + 1]));
1190         CARD8 shift = *((CARD8 *) (&bios->data[offset + 5]));
1191         CARD32 and1 = *((CARD32 *) (&bios->data[offset + 6]));
1192         CARD32 xor = *((CARD32 *) (&bios->data[offset + 10]));
1193         CARD32 dstreg = *((CARD32 *) (&bios->data[offset + 14]));
1194         CARD32 and2 = *((CARD32 *) (&bios->data[offset + 18]));
1195         CARD32 srcdata;
1196         CARD32 dstdata;
1197         
1198         if (iexec->execute) {
1199                 nv32_rd(pScrn, srcreg, &srcdata);
1200                 
1201                 if (shift > 0)
1202                         srcdata >>= shift;
1203                 else
1204                         srcdata <<= shift;
1205
1206                 srcdata = (srcdata & and1) ^ xor;
1207
1208                 nv32_rd(pScrn, dstreg, &dstdata);
1209                 dstdata &= and2;
1210
1211                 dstdata |= srcdata;
1212
1213                 CARD32 tmp;             
1214                 nv32_rd(pScrn, dstreg, &tmp);
1215
1216                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: REG: 0x%08X, VALUE: 0x%08X\n", offset, dstreg, 
1217                                 dstdata);
1218
1219                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CURRENT VALUE IS: 0x%08X\n", offset, tmp);
1220
1221                 nv32_wr(pScrn, dstreg, dstdata);
1222         }
1223         return TRUE;
1224 }
1225
1226 static Bool init_zm_index_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1227 {
1228         /* INIT_ZM_INDEX_IO   opcode: 0x62 ('b')
1229          *
1230          * offset      (8  bit): opcode
1231          * offset + 1  (16 bit): CRTC port
1232          * offset + 3  (8  bit): CRTC index
1233          * offset + 4  (8  bit): data
1234          *
1235          * Write "data" to index "CRTC index" of "CRTC port"
1236          */
1237         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1238         uint8_t crtcindex = bios->data[offset + 3];
1239         uint8_t data = bios->data[offset + 4];
1240
1241         if (!iexec->execute)
1242                 return TRUE;
1243
1244         nv_idx_port_wr(pScrn, crtcport, crtcindex, data);
1245
1246         return TRUE;
1247 }
1248
1249 static Bool init_compute_mem(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1250 {
1251         /* INIT_COMPUTE_MEM   opcode: 0x63 ('c')
1252          *
1253          * offset      (8 bit): opcode
1254          *
1255          * FIXME
1256          */
1257
1258         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ NOT YET IMPLEMENTED ]\n", offset);
1259 #if 0
1260         uint16_t ramcfg = le16_to_cpu(*((uint16_t *)(&bios->data[bios->ram_table_offset])));
1261         uint32_t pfb_debug;
1262         uint32_t strapinfo;
1263         uint32_t ramcfg2;
1264
1265         if (!iexec->execute)
1266                 return TRUE;
1267
1268         nv32_rd(pScrn, 0x00101000, &strapinfo);
1269         nv32_rd(pScrn, 0x00100080, &pfb_debug);
1270
1271         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "STRAPINFO: 0x%08X\n", strapinfo);
1272         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "PFB_DEBUG: 0x%08X\n", pfb_debug);
1273         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "RAM CFG: 0x%04X\n", ramcfg);
1274
1275         pfb_debug &= 0xffffffef;
1276         strapinfo >>= 2;
1277         strapinfo &= 0x0000000f;
1278         ramcfg2 = le16_to_cpu(*((uint16_t *)
1279                         (&bios->data[bios->ram_table_offset + (2 * strapinfo)])));
1280
1281         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "AFTER MANIPULATION\n");
1282         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "STRAPINFO: 0x%08X\n", strapinfo);
1283         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "PFB_DEBUG: 0x%08X\n", pfb_debug);
1284         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "RAM CFG2: 0x%08X\n", ramcfg2);
1285
1286
1287         uint32_t reg1;
1288         uint32_t reg2;
1289
1290         nv32_rd(pScrn, 0x00100200, &reg1);
1291         nv32_rd(pScrn, 0x0010020C, &reg2);
1292
1293         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x00100200: 0x%08X\n", reg1);
1294         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x0010020C: 0x%08X\n", reg2);
1295 #endif
1296
1297         return TRUE;
1298 }
1299
1300 static Bool init_reset(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1301 {
1302         /* INIT_RESET   opcode: 0x65 ('e')
1303          *
1304          * offset      (8  bit): opcode
1305          * offset + 1  (32 bit): register
1306          * offset + 5  (32 bit): value1
1307          * offset + 9  (32 bit): value2
1308          *
1309          * Assign "value1" to "register", then assign "value2" to "register"
1310          */
1311
1312         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1313         uint32_t value1 = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1314         uint32_t value2 = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
1315         uint32_t pci_nv_19, pci_nv_20;
1316
1317         /* no iexec->execute check by design */
1318
1319         nv32_rd(pScrn, NV_PBUS_PCI_NV_19, &pci_nv_19);
1320         nv32_wr(pScrn, NV_PBUS_PCI_NV_19, 0);
1321         nv32_wr(pScrn, reg, value1);
1322
1323         usleep(10);
1324
1325         nv32_wr(pScrn, reg, value2);
1326         nv32_wr(pScrn, NV_PBUS_PCI_NV_19, pci_nv_19);
1327
1328         nv32_rd(pScrn, NV_PBUS_PCI_NV_20, &pci_nv_20);
1329         pci_nv_20 &= !NV_PBUS_PCI_NV_20_ROM_SHADOW_ENABLED;     /* 0xfffffffe */
1330         nv32_wr(pScrn, NV_PBUS_PCI_NV_20, pci_nv_20);
1331
1332         return TRUE;
1333 }
1334
1335 static Bool init_index_io8(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1336 {
1337         /* INIT_INDEX_IO8   opcode: 0x69
1338          * 
1339          * offset      (8  bit): opcode
1340          * offset + 1  (16 bit): CRTC reg
1341          * offset + 3  (8  bit): and mask
1342          * offset + 4  (8  bit): or with
1343          * 
1344          * 
1345          */
1346
1347         NVPtr pNv = NVPTR(pScrn);
1348         volatile CARD8 *ptr = crtchead ? pNv->PCIO1 : pNv->PCIO0;
1349         CARD16 reg = le16_to_cpu(*((CARD16 *)(&bios->data[offset + 1])));
1350         CARD8 and  = *((CARD8 *)(&bios->data[offset + 3]));
1351         CARD8 or = *((CARD8 *)(&bios->data[offset + 4]));
1352         CARD8 data;
1353
1354         if (iexec->execute) {
1355                 data = (VGA_RD08(ptr, reg) & and) | or;
1356
1357                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1358                                 "0x%04X: CRTC REG: 0x%04X, VALUE: 0x%02X\n", 
1359                                 offset, reg, data);
1360                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CURRENT VALUE IS: 0x%02X\n", offset, 
1361                                 VGA_RD08(ptr, reg));
1362
1363 #ifdef PERFORM_WRITE
1364                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "init_index_io8 crtcreg 0x%X value 0x%X\n",reg,data);
1365                 still_alive();
1366                 VGA_WR08(ptr, reg, data);
1367 #endif
1368         }
1369         return TRUE;
1370 }
1371
1372 static Bool init_sub(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1373 {
1374         /* INIT_SUB   opcode: 0x6B ('k')
1375          *
1376          * offset      (8 bit): opcode
1377          * offset + 1  (8 bit): script number
1378          *
1379          * Execute script number "script number", as a subroutine
1380          */
1381
1382         uint8_t sub = bios->data[offset + 1];
1383
1384         if (!iexec->execute)
1385                 return TRUE;
1386
1387         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1388                    "0x%04X: EXECUTING SUB-SCRIPT %d\n", offset, sub);
1389
1390         parse_init_table(pScrn, bios,
1391                          le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + sub * 2]))),
1392                          iexec);
1393
1394         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1395                    "0x%04X: END OF SUB-SCRIPT %d\n", offset, sub);
1396
1397         return TRUE;
1398 }
1399
1400 static Bool init_ram_condition(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1401 {
1402         /* INIT_RAM_CONDITION   opcode: 0x6D
1403          * 
1404          * offset      (8  bit): opcode
1405          * offset + 1  (8  bit): and mask
1406          * offset + 2  (8  bit): cmpval
1407          *
1408          * Test if (NV_PFB_BOOT & and mask) matches cmpval
1409          */
1410         NVPtr pNv = NVPTR(pScrn);
1411         CARD8 and = *((CARD8 *) (&bios->data[offset + 1]));
1412         CARD8 cmpval = *((CARD8 *) (&bios->data[offset + 2]));
1413         CARD32 data;
1414
1415         if (iexec->execute) {
1416                 data=(pNv->PFB[NV_PFB_BOOT/4])&and;
1417
1418                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1419                                 "0x%04X: CHECKING IF REGVAL: 0x%08X equals COND: 0x%08X\n",
1420                                 offset, data, cmpval);
1421
1422                 if (data == cmpval) {
1423                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1424                                         "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n",
1425                                         offset);
1426                 } else {
1427                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CONDITION IS NOT FULFILLED.\n", offset);
1428                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1429                                         "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
1430                         iexec->execute = FALSE;     
1431                 }
1432         }
1433         return TRUE;
1434 }
1435
1436 static Bool init_nv_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1437 {
1438         /* INIT_NV_REG   opcode: 0x6E ('n')
1439          * 
1440          * offset      (8  bit): opcode
1441          * offset + 1  (32 bit): register
1442          * offset + 5  (32 bit): mask
1443          * offset + 9  (32 bit): data
1444          *
1445          * Assign ((REGVAL("register") & "mask") | "data") to "register"
1446          */
1447
1448         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1449         uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1450         uint32_t data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 9])));
1451         uint32_t value;
1452
1453         if (!iexec->execute)
1454                 return TRUE;
1455
1456         if (DEBUGLEVEL >= 6)
1457                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1458                            "0x%04X: Reg: 0x%08X, Mask: 0x%08X, Data: 0x%08X\n",
1459                            offset, reg, mask, data);
1460
1461         nv32_rd(pScrn, reg, &value);
1462
1463         value = (value & mask) | data;
1464
1465         nv32_wr(pScrn, reg, value);
1466
1467         return TRUE;
1468 }
1469
1470 static Bool init_macro(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1471 {
1472         /* INIT_MACRO   opcode: 0x6F ('o')
1473          *
1474          * offset      (8 bit): opcode
1475          * offset + 1  (8 bit): macro number
1476          *
1477          * Look up macro index "macro number" in the macro index table.
1478          * The macro index table entry has 1 byte for the index in the macro table,
1479          * and 1 byte for the number of times to repeat the macro.
1480          * The macro table entry has 4 bytes for the register address and
1481          * 4 bytes for the value to write to that register
1482          */
1483
1484         uint8_t macro_index_tbl_idx = bios->data[offset + 1];
1485         uint16_t tmp = bios->macro_index_tbl_ptr + (macro_index_tbl_idx * MACRO_INDEX_SIZE);
1486         uint8_t macro_tbl_idx = bios->data[tmp];
1487         uint8_t count = bios->data[tmp + 1];
1488         uint32_t reg, data;
1489         int i;
1490
1491         if (!iexec->execute)
1492                 return TRUE;
1493
1494         if (DEBUGLEVEL >= 6)
1495                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1496                            "0x%04X: Macro: 0x%02X, MacroTableIndex: 0x%02X, Count: 0x%02X\n",
1497                            offset, macro_index_tbl_idx, macro_tbl_idx, count);
1498
1499         for (i = 0; i < count; i++) {
1500                 uint16_t macroentryptr = bios->macro_tbl_ptr + (macro_tbl_idx + i) * MACRO_SIZE;
1501
1502                 reg = le32_to_cpu(*((uint32_t *)(&bios->data[macroentryptr])));
1503                 data = le32_to_cpu(*((uint32_t *)(&bios->data[macroentryptr + 4])));
1504
1505                 nv32_wr(pScrn, reg, data);
1506         }
1507
1508         return TRUE;
1509 }
1510
1511 static Bool init_done(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1512 {
1513         /* INIT_DONE   opcode: 0x71 ('q')
1514          *
1515          * offset      (8  bit): opcode
1516          *
1517          * End the current script
1518          */
1519
1520         /* mild retval abuse to stop parsing this table */
1521         return FALSE;
1522 }
1523
1524 static Bool init_resume(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1525 {
1526         /* INIT_RESUME   opcode: 0x72 ('r')
1527          *
1528          * offset      (8  bit): opcode
1529          *
1530          * End the current execute / no-execute condition
1531          */
1532
1533         if (iexec->execute)
1534                 return TRUE;
1535
1536         iexec->execute = TRUE;;
1537         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1538                    "0x%04X: ---- EXECUTING FOLLOWING COMMANDS ----\n", offset);
1539
1540         return TRUE;
1541 }
1542
1543 static Bool init_ram_condition2(ScrnInfoPtr pScrn, bios_t *bios, CARD16 offset, init_exec_t *iexec)
1544 {
1545         /* INIT_RAM_CONDITION2   opcode: 0x73
1546          * 
1547          * offset      (8  bit): opcode
1548          * offset + 1  (8  bit): and mask
1549          * offset + 2  (8  bit): cmpval
1550          *
1551          * Test if (NV_EXTDEV_BOOT & and mask) matches cmpval
1552          */
1553         NVPtr pNv = NVPTR(pScrn);
1554         CARD32 and = *((CARD32 *) (&bios->data[offset + 1]));
1555         CARD32 cmpval = *((CARD32 *) (&bios->data[offset + 5]));
1556         CARD32 data;
1557
1558         if (iexec->execute) {
1559                 data=(nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT))&and;
1560                 
1561                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1562                                 "0x%04X: CHECKING IF REGVAL: 0x%08X equals COND: 0x%08X\n",
1563                                 offset, data, cmpval);
1564
1565                 if (data == cmpval) {
1566                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1567                                         "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n",
1568                                         offset);
1569                 } else {
1570                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  "0x%04X: CONDITION IS NOT FULFILLED.\n", offset);
1571                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,  
1572                                         "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
1573                         iexec->execute = FALSE;     
1574                 }
1575         }
1576         return TRUE;
1577 }
1578
1579 static Bool init_time(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1580 {
1581         /* INIT_TIME   opcode: 0x74 ('t')
1582          * 
1583          * offset      (8  bit): opcode
1584          * offset + 1  (16 bit): time
1585          * 
1586          * Sleep for "time" microseconds.
1587          */
1588
1589         uint16_t time = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1590
1591         if (!iexec->execute)
1592                 return TRUE;
1593
1594         if (DEBUGLEVEL >= 6)
1595                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1596                            "0x%04X: Sleeping for 0x%04X microseconds.\n", offset, time);
1597
1598         usleep(time);
1599
1600         return TRUE;
1601 }
1602
1603 static Bool init_condition(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1604 {
1605         /* INIT_CONDITION   opcode: 0x75 ('u')
1606          *
1607          * offset      (8 bit): opcode
1608          * offset + 1  (8 bit): condition number
1609          *
1610          * Check condition "condition number" in the condition table.
1611          * The condition table entry has 4 bytes for the address of the
1612          * register to check, 4 bytes for a mask and 4 for a test value.
1613          * If condition not met skip subsequent opcodes until condition
1614          * is inverted (INIT_NOT), or we hit INIT_RESUME
1615          */
1616
1617         uint8_t cond = bios->data[offset + 1];
1618         uint16_t condptr = bios->condition_tbl_ptr + cond * CONDITION_SIZE;
1619         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[condptr])));
1620         uint32_t mask = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 4])));
1621         uint32_t cmpval = le32_to_cpu(*((uint32_t *)(&bios->data[condptr + 8])));
1622         uint32_t data;
1623
1624         if (!iexec->execute)
1625                 return TRUE;
1626
1627         if (DEBUGLEVEL >= 6)
1628                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1629                            "0x%04X: Cond: 0x%02X, Reg: 0x%08X, Mask: 0x%08X, Cmpval: 0x%08X\n",
1630                            offset, cond, reg, mask, cmpval);
1631
1632         nv32_rd(pScrn, reg, &data);
1633         data &= mask;
1634
1635         if (DEBUGLEVEL >= 6)
1636                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1637                            "0x%04X: Checking if 0x%08X equals 0x%08X\n",
1638                            offset, data, cmpval);
1639
1640         if (data == cmpval) {
1641                 if (DEBUGLEVEL >= 6)
1642                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1643                                    "0x%04X: CONDITION FULFILLED - CONTINUING TO EXECUTE\n", offset);
1644         } else {
1645                 if (DEBUGLEVEL >= 6)
1646                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1647                                    "0x%04X: CONDITION IS NOT FULFILLED.\n", offset);
1648                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1649                            "0x%04X: ------ SKIPPING FOLLOWING COMMANDS  ------\n", offset);
1650                 iexec->execute = FALSE;
1651         }
1652
1653         return TRUE;
1654 }
1655
1656 static Bool init_index_io(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1657 {
1658         /* INIT_INDEX_IO   opcode: 0x78 ('x')
1659          * 
1660          * offset      (8  bit): opcode
1661          * offset + 1  (16 bit): CRTC port
1662          * offset + 3  (8  bit): CRTC index
1663          * offset + 4  (8  bit): mask
1664          * offset + 5  (8  bit): data
1665          * 
1666          * Read value at index "CRTC index" on "CRTC port", AND with "mask", OR with "data", write-back
1667          */
1668
1669         uint16_t crtcport = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 1])));
1670         uint8_t crtcindex = bios->data[offset + 3];
1671         uint8_t mask = bios->data[offset + 4];
1672         uint8_t data = bios->data[offset + 5];
1673         uint8_t value;
1674         
1675         if (!iexec->execute)
1676                 return TRUE;
1677
1678         if (DEBUGLEVEL >= 6)
1679                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1680                            "0x%04X: Port: 0x%04X, Index: 0x%02X, Mask: 0x%02X, Data: 0x%02X\n",
1681                            offset, crtcport, crtcindex, mask, data);
1682
1683         nv_idx_port_rd(pScrn, crtcport, crtcindex, &value);
1684         value = (value & mask) | data;
1685         nv_idx_port_wr(pScrn, crtcport, crtcindex, value);
1686
1687         return TRUE;
1688 }
1689
1690 static Bool init_pll(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1691 {
1692         /* INIT_PLL   opcode: 0x79 ('y')
1693          *
1694          * offset      (8  bit): opcode
1695          * offset + 1  (32 bit): register
1696          * offset + 5  (16 bit): freq
1697          *
1698          * Set PLL register "register" to coefficients for frequency (10kHz) "freq"
1699          */
1700
1701         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1702         uint16_t freq = le16_to_cpu(*((uint16_t *)(&bios->data[offset + 5])));
1703
1704         if (!iexec->execute)
1705                 return TRUE;
1706
1707         if (DEBUGLEVEL >= 6)
1708                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1709                            "0x%04X: Reg: 0x%08X, Freq: %d0kHz\n",
1710                            offset, reg, freq);
1711
1712         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ NOT YET IMPLEMENTED ]\n", offset);
1713
1714 #if 0
1715         switch (reg) {
1716                 case 0x00680508:
1717                 configval = 0x00011F05;
1718                 break;
1719         }
1720 #endif
1721         return TRUE;
1722 }
1723
1724 static Bool init_zm_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1725 {
1726         /* INIT_ZM_REG   opcode: 0x7A ('z')
1727          * 
1728          * offset      (8  bit): opcode
1729          * offset + 1  (32 bit): register
1730          * offset + 5  (32 bit): value
1731          * 
1732          * Assign "value" to "register"
1733          */
1734
1735         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1736         uint32_t value = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1737
1738         if (!iexec->execute)
1739                 return TRUE;
1740
1741         nv32_wr(pScrn, reg, value);
1742
1743         return TRUE;
1744 }
1745
1746 /* hack to avoid moving the itbl_entry array before this function */
1747 int init_ram_restrict_zm_reg_group_blocklen = 0;
1748
1749 static Bool init_ram_restrict_zm_reg_group(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1750 {
1751         /* INIT_RAM_RESTRICT_ZM_REG_GROUP   opcode: 0x8F ('')
1752          *
1753          * offset      (8  bit): opcode
1754          * offset + 1  (32 bit): reg
1755          * offset + 5  (8  bit): regincrement
1756          * offset + 6  (8  bit): count
1757          * offset + 7  (32 bit): value 1,1
1758          * ...
1759          *
1760          * Use the RAMCFG strap of PEXTDEV_BOOT as an index into the table at
1761          * ram_restrict_table_ptr. The value read from here is 'n', and
1762          * "value 1,n" gets written to "reg". This repeats "count" times and on
1763          * each iteration 'm', "reg" increases by "regincrement" and
1764          * "value m,n" is used. The extent of n is limited by a number read
1765          * from the 'M' BIT table, herein called "blocklen"
1766          */
1767
1768         NVPtr pNv = NVPTR(pScrn);
1769         uint32_t reg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1770         uint8_t regincrement = bios->data[offset + 5];
1771         uint8_t count = bios->data[offset + 6];
1772         uint32_t strap_ramcfg, data;
1773         uint16_t blocklen;
1774         uint8_t index;
1775         int i;
1776
1777         /* previously set by 'M' BIT table */
1778         blocklen = init_ram_restrict_zm_reg_group_blocklen;
1779
1780         if (!iexec->execute)
1781                 return TRUE;
1782
1783         if (!blocklen) {
1784                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1785                            "0x%04X: Zero block length - has the M table been parsed?\n", offset);
1786                 return FALSE;
1787         }
1788
1789         strap_ramcfg = (nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT) >> 2) & 0xf;
1790         index = bios->data[bios->ram_restrict_tbl_ptr + strap_ramcfg];
1791
1792         if (DEBUGLEVEL >= 6)
1793                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1794                            "0x%04X: Reg: 0x%08X, RegIncrement: 0x%02X, Count: 0x%02X, StrapRamCfg: 0x%02X, Index: 0x%02X\n",
1795                            offset, reg, regincrement, count, strap_ramcfg, index);
1796
1797         for (i = 0; i < count; i++) {
1798                 data = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 7 + index * 4 + blocklen * i])));
1799
1800                 nv32_wr(pScrn, reg, data);
1801
1802                 reg += regincrement;
1803         }
1804
1805         return TRUE;
1806 }
1807
1808 static Bool init_copy_zm_reg(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1809 {
1810         /* INIT_COPY_ZM_REG   opcode: 0x90 ('')
1811          *
1812          * offset      (8  bit): opcode
1813          * offset + 1  (32 bit): src reg
1814          * offset + 5  (32 bit): dst reg
1815          *
1816          * Put contents of "src reg" into "dst reg"
1817          */
1818
1819         uint32_t srcreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 1])));
1820         uint32_t dstreg = le32_to_cpu(*((uint32_t *)(&bios->data[offset + 5])));
1821         uint32_t data;
1822
1823         if (!iexec->execute)
1824                 return TRUE;
1825
1826         nv32_rd(pScrn, srcreg, &data);
1827         nv32_wr(pScrn, dstreg, data);
1828
1829         return TRUE;
1830 }
1831
1832 static Bool init_reserved(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset, init_exec_t *iexec)
1833 {
1834         /* INIT_RESERVED   opcode: 0x92 ('')
1835          *
1836          * offset      (8 bit): opcode
1837          *
1838          * Seemingly does nothing
1839          */
1840
1841         return TRUE;
1842 }
1843
1844 static init_tbl_entry_t itbl_entry[] = {
1845         /* command name                       , id  , length  , offset  , mult    , command handler                 */
1846 //      { "INIT_PROG"                         , 0x31, 15      , 10      , 4       , init_prog                       },
1847         { "INIT_IO_RESTRICT_PROG"             , 0x32, 11      , 6       , 4       , init_io_restrict_prog           },
1848         { "INIT_REPEAT"                       , 0x33, 2       , 0       , 0       , init_repeat                     },
1849         { "INIT_IO_RESTRICT_PLL"              , 0x34, 12      , 7       , 2       , init_io_restrict_pll            },
1850         { "INIT_END_REPEAT"                   , 0x36, 1       , 0       , 0       , init_end_repeat                 },
1851         { "INIT_COPY"                         , 0x37, 11      , 0       , 0       , init_copy                       },
1852         { "INIT_NOT"                          , 0x38, 1       , 0       , 0       , init_not                        },
1853         { "INIT_IO_FLAG_CONDITION"            , 0x39, 2       , 0       , 0       , init_io_flag_condition          },
1854         { "INIT_INDEX_ADDRESS_LATCHED"        , 0x49, 18      , 17      , 2       , init_idx_addr_latched           },
1855         { "INIT_IO_RESTRICT_PLL2"             , 0x4A, 11      , 6       , 4       , init_io_restrict_pll2           },
1856         { "INIT_PLL2"                         , 0x4B, 9       , 0       , 0       , init_pll2                       },
1857 /*      { "INIT_I2C_BYTE"                     , 0x4C, x       , x       , x       , init_i2c_byte                   }, */
1858 /*      { "INIT_ZM_I2C_BYTE"                  , 0x4D, x       , x       , x       , init_zm_i2c_byte                }, */
1859 /*      { "INIT_ZM_I2C"                       , 0x4E, x       , x       , x       , init_zm_i2c                     }, */
1860         { "INIT_50"                           , 0x50, 3       , 2       , 2       , init_50                         },
1861         { "INIT_CR_INDEX_ADDRESS_LATCHED"     , 0x51, 5       , 4       , 1       , init_cr_idx_adr_latch           },
1862         { "INIT_CR"                           , 0x52, 4       , 0       , 0       , init_cr                         },
1863         { "INIT_ZM_CR"                        , 0x53, 3       , 0       , 0       , init_zm_cr                      },
1864         { "INIT_ZM_CR_GROUP"                  , 0x54, 2       , 1       , 2       , init_zm_cr_group                },
1865 //      { "INIT_CONDITION_TIME"               , 0x56, 3       , 0       , 0       , init_condition_time             },
1866         { "INIT_ZM_REG_SEQUENCE"              , 0x58, 6       , 5       , 4       , init_zm_reg_sequence            },
1867 //      { "INIT_INDIRECT_REG"                 , 0x5A, 7       , 0       , 0       , init_indirect_reg               },
1868         { "INIT_SUB_DIRECT"                   , 0x5B, 3       , 0       , 0       , init_sub_direct                 },
1869 //      { "INIT_COPY_NV_REG"                  , 0x5F, 22      , 0       , 0       , init_copy_nv_reg                },
1870         { "INIT_ZM_INDEX_IO"                  , 0x62, 5       , 0       , 0       , init_zm_index_io                },
1871         { "INIT_COMPUTE_MEM"                  , 0x63, 1       , 0       , 0       , init_compute_mem                },
1872         { "INIT_RESET"                        , 0x65, 13      , 0       , 0       , init_reset                      },
1873 /*      { "INIT_NEXT"                         , 0x66, x       , x       , x       , init_next                       }, */       
1874 /*      { "INIT_NEXT"                         , 0x67, x       , x       , x       , init_next                       }, */       
1875 /*      { "INIT_NEXT"                         , 0x68, x       , x       , x       , init_next                       }, */       
1876 //      { "INIT_INDEX_IO8"                    , 0x69, 5       , 0       , 0       , init_index_io8                  },
1877         { "INIT_SUB"                          , 0x6B, 2       , 0       , 0       , init_sub                        },
1878 //      { "INIT_RAM_CONDITION"                , 0x6D, 3       , 0       , 0       , init_ram_condition              },
1879         { "INIT_NV_REG"                       , 0x6E, 13      , 0       , 0       , init_nv_reg                     },
1880         { "INIT_MACRO"                        , 0x6F, 2       , 0       , 0       , init_macro                      },
1881         { "INIT_DONE"                         , 0x71, 1       , 0       , 0       , init_done                       },
1882         { "INIT_RESUME"                       , 0x72, 1       , 0       , 0       , init_resume                     },
1883 //      { "INIT_RAM_CONDITION2"               , 0x73, 9       , 0       , 0       , init_ram_condition2             },
1884         { "INIT_TIME"                         , 0x74, 3       , 0       , 0       , init_time                       },
1885         { "INIT_CONDITION"                    , 0x75, 2       , 0       , 0       , init_condition                  },
1886 /*      { "INIT_IO_CONDITION"                 , 0x76, x       , x       , x       , init_io_condition               }, */
1887         { "INIT_INDEX_IO"                     , 0x78, 6       , 0       , 0       , init_index_io                   },
1888         { "INIT_PLL"                          , 0x79, 7       , 0       , 0       , init_pll                        },
1889         { "INIT_ZM_REG"                       , 0x7A, 9       , 0       , 0       , init_zm_reg                     },
1890         /* INIT_RAM_RESTRICT_ZM_REG_GROUP's mult is loaded by M table in BIT */
1891         { "INIT_RAM_RESTRICT_ZM_REG_GROUP"    , 0x8F, 7       , 6       , 0       , init_ram_restrict_zm_reg_group  },
1892         { "INIT_COPY_ZM_REG"                  , 0x90, 9       , 0       , 0       , init_copy_zm_reg                },
1893 /*      { "INIT_ZM_REG_GROUP_ADDRESS_LATCHED" , 0x91, x       , x       , x       , init_zm_reg_group_addr_latched  }, */
1894         { "INIT_RESERVED"                     , 0x92, 1       , 0       , 0       , init_reserved                   },
1895         { 0                                   , 0   , 0       , 0       , 0       , 0                               }
1896 };
1897
1898 static unsigned int get_init_table_entry_length(bios_t *bios, unsigned int offset, int i)
1899 {
1900         /* Calculates the length of a given init table entry. */
1901         return itbl_entry[i].length + bios->data[offset + itbl_entry[i].length_offset]*itbl_entry[i].length_multiplier;
1902 }
1903
1904 static void parse_init_table(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset, init_exec_t *iexec)
1905 {
1906         /* Parses all commands in a init table. */
1907
1908         /* We start out executing all commands found in the
1909          * init table. Some op codes may change the status
1910          * of this variable to SKIP, which will cause
1911          * the following op codes to perform no operation until
1912          * the value is changed back to EXECUTE.
1913          */
1914         unsigned char id;
1915         int i;
1916
1917         int count=0;
1918         /* Loop until INIT_DONE causes us to break out of the loop
1919          * (or until offset > bios length just in case... )
1920          * (and no more than 10000 iterations just in case... ) */
1921         while ((offset < bios->length) && (count++ < 10000)) {
1922                 id = bios->data[offset];
1923
1924                 /* Find matching id in itbl_entry */
1925                 for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != id); i++)
1926                         ;
1927
1928                 if (itbl_entry[i].name) {
1929                         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: [ (0x%02X) - %s ]\n",
1930                                    offset, itbl_entry[i].id, itbl_entry[i].name);
1931
1932                         /* execute eventual command handler */
1933                         if (itbl_entry[i].handler)
1934                                 if (!(*itbl_entry[i].handler)(pScrn, bios, offset, iexec))
1935                                         break;
1936                 } else {
1937                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1938                                    "0x%04X: Init table command not found: 0x%02X\n", offset, id);
1939                         break;
1940                 }
1941
1942                 /* Add the offset of the current command including all data
1943                  * of that command. The offset will then be pointing on the
1944                  * next op code.
1945                  */
1946                 offset += get_init_table_entry_length(bios, offset, i);
1947         }
1948 }
1949
1950 void parse_init_tables(ScrnInfoPtr pScrn, bios_t *bios)
1951 {
1952         /* Loops and calls parse_init_table() for each present table. */
1953
1954         int i = 0;
1955         uint16_t table;
1956         init_exec_t iexec = {TRUE, FALSE};
1957
1958         while ((table = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + i]))))) {
1959
1960                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: Parsing init table %d\n",
1961                         table, i / 2);
1962
1963                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
1964                            "0x%04X: ------ EXECUTING FOLLOWING COMMANDS ------\n", table);
1965                 still_alive();
1966                 parse_init_table(pScrn, bios, table, &iexec);
1967                 i += 2;
1968         }
1969 }
1970
1971 struct fppointers {
1972         uint16_t fptablepointer;
1973         uint16_t fpxlatetableptr;
1974         uint16_t lvdsmanufacturerpointer;
1975         uint16_t fpxlatemanufacturertableptr;
1976 };
1977
1978 void link_head_and_output(ScrnInfoPtr pScrn, int head, int dcb_entry, Bool overrideval)
1979 {
1980         /* The BIOS scripts don't do this for us, sadly
1981          * Luckily we do know the values ;-)
1982          *
1983          * head < 0 indicates we wish to force a setting with the overrideval
1984          * (for VT restore etc.)
1985          */
1986
1987         NVPtr pNv = NVPTR(pScrn);
1988
1989         Bool crosswired = FALSE;
1990         uint8_t possible_outputs = ffs(pNv->dcb_table.entry[dcb_entry].or);
1991         uint8_t bus = pNv->dcb_table.entry[dcb_entry].bus;
1992
1993         uint8_t output;
1994
1995         /* Choose an output. */
1996         switch(possible_outputs) {
1997                 case (OUTPUT_0 | OUTPUT_1): /* full freedom */
1998                         output = head;
1999                         break;
2000                 case OUTPUT_1:
2001                         output = 1;
2002                         break;
2003                 case OUTPUT_0:
2004                 default:
2005                         output = 0;
2006                         break;
2007         }
2008
2009         /* This is based on the mmio-traces of:
2010          * A strange 6800GT.
2011          * A 6600GO with DVI.
2012          */
2013         /* I need more dumps to get a good picture of the situation */
2014         if (bus > 2) { /* usually mobile cards with dvi */
2015                 if (head == 1)
2016                         crosswired = TRUE;
2017         } else {
2018                 if (head != output)
2019                         crosswired = TRUE;
2020         }
2021
2022         /* Is this also valid on earlier cards? */
2023
2024         /* 6800GT: */
2025         /* This card has it's dvi output on bus = 1 and or = 1.
2026          * I was wrong, it gets 0x80 on crtc0 (i guess) and 0x88 on crtc1.
2027          * Conclusion: It behaves according to "or", despite earlier ideas.
2028          */
2029
2030         /* 6600GO: */
2031         /* This card has dvi output on bus = 3 and or = 3.
2032          * On crtc1 it gets value 0x88.
2033          * Conclusion: It behaves as if it were on output 0?
2034          */
2035
2036         uint8_t preferred_output = possible_outputs >> 1;
2037
2038         uint8_t tmds04 = 0x80;
2039         uint32_t tmds_ctrl, tmds_ctrl2;
2040
2041         /* Bit 3 crosswires output and bus. */
2042         if (head >= 0 && crosswired)
2043                 tmds04 = 0x88;
2044         if (head < 0 && overrideval)
2045                 tmds04 = 0x88;
2046
2047         if (pNv->dcb_table.entry[dcb_entry].type == OUTPUT_LVDS)
2048                 tmds04 |= 0x01;
2049
2050         tmds_ctrl = NV_PRAMDAC0_OFFSET + (preferred_output ? NV_PRAMDAC0_SIZE : 0) + NV_RAMDAC_FP_TMDS_CONTROL;
2051         tmds_ctrl2 = NV_PRAMDAC0_OFFSET + (preferred_output ? NV_PRAMDAC0_SIZE : 0) + NV_RAMDAC_FP_TMDS_CONTROL_2;
2052
2053         Bool oldexecute = pNv->VBIOS.execute;
2054         pNv->VBIOS.execute = TRUE;
2055         nv32_wr(pScrn, tmds_ctrl + 4, tmds04);
2056         nv32_wr(pScrn, tmds_ctrl, 0x04);
2057         if (pNv->dcb_table.entry[dcb_entry].type == OUTPUT_LVDS && pNv->VBIOS.fp.dual_link)
2058                 nv32_wr(pScrn, tmds_ctrl2 + 4, tmds04 ^ 0x08);
2059         else {
2060                 /* I have encountered no dvi (dual-link or not) that sets to anything else. */
2061                 /* Does this change beyond the 165 MHz boundary? */
2062                 nv32_wr(pScrn, tmds_ctrl2 + 4, 0x0);
2063         }
2064         nv32_wr(pScrn, tmds_ctrl2, 0x04);
2065         pNv->VBIOS.execute = oldexecute;
2066 }
2067
2068 void call_lvds_script(ScrnInfoPtr pScrn, int head, int dcb_entry, enum LVDS_script script)
2069 {
2070         NVPtr pNv = NVPTR(pScrn);
2071         bios_t *bios = &pNv->VBIOS;
2072         init_exec_t iexec = {TRUE, FALSE};
2073
2074         uint8_t sub = bios->data[bios->fp.script_table + script];
2075         uint16_t scriptofs = le16_to_cpu(*((CARD16 *)(&bios->data[bios->init_script_tbls_ptr + sub * 2])));
2076
2077         if (!bios->fp.script_table || !sub || !scriptofs)
2078                 return;
2079
2080         if (script == LVDS_INIT && bios->data[scriptofs] != 'q') {
2081                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "LVDS init script not stubbed\n");
2082                 return;
2083         }
2084
2085         if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
2086                 call_lvds_script(pScrn, head, dcb_entry, LVDS_RESET);
2087         if (script == LVDS_RESET && bios->fp.power_off_for_reset)
2088                 call_lvds_script(pScrn, head, dcb_entry, LVDS_PANEL_OFF);
2089
2090         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Calling LVDS script %d:\n", script);
2091         pNv->VBIOS.execute = TRUE;
2092         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_OWNER,
2093                    head ? NV_VGA_CRTCX_OWNER_HEADB : NV_VGA_CRTCX_OWNER_HEADA);
2094         parse_init_table(pScrn, bios, scriptofs, &iexec);
2095         pNv->VBIOS.execute = FALSE;
2096
2097         if (script == LVDS_PANEL_OFF)
2098                 usleep(bios->fp.off_on_delay * 1000);
2099         if (script == LVDS_RESET)
2100                 link_head_and_output(pScrn, head, dcb_entry, FALSE);
2101 }
2102
2103 static void parse_fp_mode_table(ScrnInfoPtr pScrn, bios_t *bios, struct fppointers *fpp)
2104 {
2105         NVPtr pNv = NVPTR(pScrn);
2106         unsigned int fpstrapping;
2107         uint8_t *fptable, *fpxlatetable;
2108         int fpindex;
2109         uint8_t fptable_ver, headerlen = 0, recordlen = 44;
2110         int ofs;
2111         DisplayModePtr mode;
2112
2113         fpstrapping = (nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT) >> 16) & 0xf;
2114
2115         if (fpp->fptablepointer == 0x0) {
2116                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2117                            "Pointer to flat panel table invalid\n");
2118                 return;
2119         }
2120
2121         fptable = &bios->data[fpp->fptablepointer];
2122
2123         fptable_ver = fptable[0];
2124
2125         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2126                    "Found flat panel mode table revision %d.%d\n",
2127                    fptable_ver >> 4, fptable_ver & 0xf);
2128
2129         switch (fptable_ver) {
2130         /* PINS version 0x5.0x11 BIOSen have version 1 like tables, but no version field,
2131          * and miss one of the spread spectrum/PWM bytes.
2132          * This could affect early GF2Go parts (not seen any appropriate ROMs though).
2133          * Here we assume that a version of 0x05 matches this case (combining with a
2134          * PINS version check would be better), as the common case for the panel type
2135          * field is 0x0005, and that is in fact what we are reading the first byte of. */
2136         case 0x05:      /* some NV10, 11, 15, 16 */
2137                 ofs = 6;
2138                 recordlen = 42;
2139                 goto v1common;
2140         case 0x10:      /* some NV15/16, and NV11+ */
2141                 ofs = 7;
2142 v1common:
2143                 fpxlatetable = &bios->data[fpp->fpxlatetableptr];
2144                 fpindex = fpxlatetable[fpstrapping];
2145                 if (fpindex > 0xf) {
2146                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2147                                    "Bad flat panel table index\n");
2148                         return;
2149                 }
2150                 break;
2151         case 0x20:      /* NV40+ */
2152                 headerlen = fptable[1];
2153                 recordlen = fptable[2]; // check this, or hardcode as 0x20
2154 /*              may be the wrong test, if there's a translation table
2155                 if (fpstrapping > fptable[3]) {
2156                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2157                                    "Flat panel strapping number too high\n");
2158                         return;
2159                 }*/
2160                 ofs = 0;
2161 /*              I don't know where the index for the table comes from in v2.0, so bail
2162                 break;*/
2163         default:
2164                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2165                            "FP Table revision not currently supported\n");
2166                 return;
2167         }
2168
2169         if (!(mode = xcalloc(1, sizeof(DisplayModeRec))))
2170                 return;
2171
2172         int modeofs = headerlen + recordlen * fpindex + ofs;
2173         mode->Clock = le16_to_cpu(*(uint16_t *)&fptable[modeofs]) * 10;
2174         mode->HDisplay = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 2]);
2175         mode->HSyncStart = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 10] + 1);
2176         mode->HSyncEnd = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 12] + 1);
2177         mode->HTotal = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 14] + 1);
2178         mode->VDisplay = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 16]);
2179         mode->VSyncStart = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 24] + 1);
2180         mode->VSyncEnd = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 26] + 1);
2181         mode->VTotal = le16_to_cpu(*(uint16_t *)&fptable[modeofs + 28] + 1);
2182         mode->Flags |= (fptable[modeofs + 30] & 0x10) ? V_PHSYNC : V_NHSYNC;
2183         mode->Flags |= (fptable[modeofs + 30] & 0x1) ? V_PVSYNC : V_NVSYNC;
2184
2185         /* for version 1.0:
2186          * bytes 1-2 are "panel type", including bits on whether Colour/mono, single/dual link, and type (TFT etc.)
2187          * bytes 3-6 are bits per colour in RGBX
2188          * 11-12 is HDispEnd
2189          * 13-14 is HValid Start
2190          * 15-16 is HValid End
2191          * bytes 38-39 relate to spread spectrum settings
2192          * bytes 40-43 are something to do with PWM */
2193
2194         mode->prev = mode->next = NULL;
2195         mode->status = MODE_OK;
2196         mode->type = M_T_DRIVER | M_T_PREFERRED;
2197         xf86SetModeDefaultName(mode);
2198
2199 //      if (pNv->debug_modes) { this should exist
2200                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2201                            "Found flat panel mode in BIOS tables:\n");
2202                 xf86PrintModeline(pScrn->scrnIndex, mode);
2203 //      }
2204
2205         bios->fp.native_mode = mode;
2206 }
2207
2208 static void parse_lvds_manufacturer_table(ScrnInfoPtr pScrn, bios_t *bios, struct fppointers *fpp)
2209 {
2210         NVPtr pNv = NVPTR(pScrn);
2211         unsigned int fpstrapping;
2212         uint8_t *lvdsmanufacturertable, *fpxlatemanufacturertable;
2213         int lvdsmanufacturerindex = 0;
2214         uint8_t lvds_ver, headerlen, recordlen;
2215
2216         fpstrapping = (nvReadEXTDEV(pNv, NV_PEXTDEV_BOOT) >> 16) & 0xf;
2217
2218         if (fpp->lvdsmanufacturerpointer == 0x0) {
2219                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2220                            "Pointer to LVDS manufacturer table invalid\n");
2221                 return;
2222         }
2223
2224         lvdsmanufacturertable = &bios->data[fpp->lvdsmanufacturerpointer];
2225         lvds_ver = lvdsmanufacturertable[0];
2226
2227         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2228                    "Found LVDS manufacturer table revision %d\n",
2229                    lvds_ver);
2230
2231         switch (lvds_ver) {
2232         case 0x0a:      /* pre NV40 */
2233                 fpxlatemanufacturertable = &bios->data[fpp->fpxlatemanufacturertableptr];
2234                 lvdsmanufacturerindex = fpxlatemanufacturertable[fpstrapping];
2235
2236                 headerlen = 2;
2237                 recordlen = lvdsmanufacturertable[1];
2238
2239                 break;
2240 //      case 0x:        /* NV40+ */
2241         default:
2242                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2243                            "LVDS manufacturer table revision not currently supported\n");
2244                 return;
2245         }
2246
2247         uint16_t lvdsofs = bios->fp.script_table = fpp->lvdsmanufacturerpointer + headerlen + recordlen * lvdsmanufacturerindex;
2248         bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
2249         bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
2250         bios->fp.dual_link = bios->data[lvdsofs] & 4;
2251         bios->fp.if_is_24bit = bios->data[lvdsofs] & 16;
2252         bios->fp.off_on_delay = le16_to_cpu(*(uint16_t *)&bios->data[lvdsofs + 7]);
2253 }
2254
2255 void run_tmds_table(ScrnInfoPtr pScrn, bios_t *bios, uint8_t dcb_entry, uint8_t head, uint16_t pxclk)
2256 {
2257         /* the dcb_entry parameter is the index of the appropriate DCB entry
2258          * the pxclk parameter is in 10s of kHz (eg. 108Mhz is 10800, or 0x2a30)
2259          *
2260          * This runs the TMDS regs setting code found on BIT bios cards
2261          *
2262          * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
2263          * ffs(or) == 3, use the second.
2264          */
2265
2266         NVPtr pNv = NVPTR(pScrn);
2267         uint16_t clktable = 0, tmdsscript = 0;
2268         int i = 0;
2269         uint16_t compareclk;
2270         uint8_t compare_record_len, tmdssub;
2271         init_exec_t iexec = {TRUE, FALSE};
2272
2273         if (pNv->dcb_table.entry[dcb_entry].location) /* off chip */
2274                 return;
2275
2276         if (bios->major_version < 5) /* pre BIT */
2277                 compare_record_len = 3;
2278         else
2279                 compare_record_len = 4;
2280
2281         switch (ffs(pNv->dcb_table.entry[dcb_entry].or)) {
2282         case 1:
2283                 clktable = bios->tmds.output0_script_ptr;
2284                 break;
2285         case 2:
2286         case 3:
2287                 clktable = bios->tmds.output1_script_ptr;
2288                 break;
2289         }
2290
2291         if (!clktable) {
2292                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Pixel clock comparison table not found\n");
2293                 return;
2294         }
2295
2296         do {
2297                 compareclk = le16_to_cpu(*((uint16_t *)&bios->data[clktable + compare_record_len * i]));
2298                 if (pxclk >= compareclk) {
2299                         if (bios->major_version < 5) {
2300                                 tmdssub = bios->data[clktable + 2 + compare_record_len * i];
2301                                 tmdsscript = le16_to_cpu(*((uint16_t *)(&bios->data[bios->init_script_tbls_ptr + tmdssub * 2])));
2302                         } else
2303                                 tmdsscript = le16_to_cpu(*((uint16_t *)&bios->data[clktable + 2 + compare_record_len * i]));
2304                         break;
2305                 }
2306                 i++;
2307         } while (compareclk);
2308
2309         if (!tmdsscript) {
2310                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "TMDS script not found\n");
2311                 return;
2312         }
2313
2314         /* This code has to be executed */
2315         bios->execute = TRUE;
2316         /* We must set the owner register appropriately */ 
2317         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, NV_VGA_CRTCX_OWNER, head * 3);
2318
2319         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "0x%04X: Parsing TMDS table\n", tmdsscript);
2320         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, 0x57, 0);
2321         nv_idx_port_wr(pScrn, CRTC_INDEX_COLOR, 0x58, dcb_entry);
2322         parse_init_table(pScrn, bios, tmdsscript, &iexec);
2323         bios->execute = FALSE;
2324
2325         link_head_and_output(pScrn, head, dcb_entry, FALSE);
2326 }
2327
2328 static void parse_bios_version(ScrnInfoPtr pScrn, bios_t *bios, uint16_t offset)
2329 {
2330         /* offset + 0  (8 bits): Micro version
2331          * offset + 1  (8 bits): Minor version
2332          * offset + 2  (8 bits): Chip version
2333          * offset + 3  (8 bits): Major version
2334          */
2335
2336         bios->major_version = bios->data[offset + 3];
2337         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Bios version %02x.%02x.%02x.%02x\n",
2338                    bios->data[offset+3], bios->data[offset+2],
2339                    bios->data[offset+1], bios->data[offset]);
2340 }
2341
2342 static int parse_bit_b_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
2343 {
2344         /* offset + 0  (32 bits): BIOS version dword
2345          *
2346          * There's a bunch of bits in this table other than the bios version
2347          * that we don't use - their use currently unknown
2348          */
2349
2350         if (bitentry->length < 0x4) {
2351                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2352                            "Do not understand B table entry.\n");
2353                 return 0;
2354         }
2355
2356         parse_bios_version(pScrn, bios, bitentry->offset);
2357
2358         return 1;
2359 }
2360
2361 static int parse_bit_m_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
2362 {
2363         /* offset + 2  (8  bits): number of options in an INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
2364          * offset + 3  (16 bits): pointer to strap xlate table for RAM restrict option selection
2365          *
2366          * There's a bunch of bits in this table other than the RAM restrict
2367          * stuff that we don't use - their use currently unknown
2368          */
2369
2370         int i;
2371
2372         /* Older bios versions don't have a sufficiently long table for what we want */
2373         if (bitentry->length < 0x5) 
2374                 return 1;
2375
2376         /* set up multiplier for INIT_RAM_RESTRICT_ZM_REG_GROUP */
2377         for (i = 0; itbl_entry[i].name && (itbl_entry[i].id != 0x8f); i++)
2378                 ;
2379         itbl_entry[i].length_multiplier = bios->data[bitentry->offset + 2] * 4;
2380         init_ram_restrict_zm_reg_group_blocklen = itbl_entry[i].length_multiplier;
2381
2382         bios->ram_restrict_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 3])));
2383
2384         return 1;
2385 }
2386
2387 static int parse_bit_display_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
2388 {
2389         uint16_t table;
2390         /* Parses the flat panel table segment that the bit entry points to.
2391          * Starting at bitentry->offset:
2392          *
2393          * offset + 0  (16 bits): FIXME table pointer
2394          * offset + 2  (16 bits): mode table pointer
2395          */
2396
2397         struct fppointers fpp;
2398
2399         /* If it's not a laptop, you probably don't care about fptables */
2400         /* FIXME: detect mobile BIOS? */
2401
2402         NVPtr pNv = NVPTR(pScrn);
2403
2404         if (!pNv->Mobile)
2405                 return 1;
2406
2407         if (bitentry->length != 4) {
2408                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2409                            "Do not understand BIT display table entry.\n");
2410                 return 0;
2411         }
2412
2413         table = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
2414         fpp.fptablepointer = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 2])));
2415
2416         parse_fp_mode_table(pScrn, bios, &fpp);
2417
2418         return 1;
2419 }
2420
2421 static unsigned int parse_bit_init_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
2422 {
2423         /* Parses the init table segment that the bit entry points to.
2424          * Starting at bitentry->offset: 
2425          * 
2426          * offset + 0  (16 bits): init script tables pointer
2427          * offset + 2  (16 bits): macro index table pointer
2428          * offset + 4  (16 bits): macro table pointer
2429          * offset + 6  (16 bits): condition table pointer
2430          * offset + 8  (16 bits): io condition table pointer
2431          * offset + 10 (16 bits): io flag condition table pointer
2432          * offset + 12 (16 bits): init function table pointer
2433          *
2434          * TODO:
2435          * * Are 'I' bit entries always of length 0xE?
2436          * 
2437          */
2438
2439         if (bitentry->length < 12) {
2440                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2441                            "Unable to recognize BIT init table entry.\n");
2442                 return 0;
2443         }
2444
2445         bios->init_script_tbls_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
2446         bios->macro_index_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 2])));
2447         bios->macro_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 4])));
2448         bios->condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 6])));
2449         bios->io_condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 8])));
2450         bios->io_flag_condition_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 10])));
2451         bios->init_function_tbl_ptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 12])));
2452
2453         parse_init_tables(pScrn, bios);
2454
2455         return 1;
2456 }
2457
2458 static int parse_bit_tmds_tbl_entry(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
2459 {
2460         /* Parses the pointer to the TMDS table
2461          *
2462          * Starting at bitentry->offset:
2463          *
2464          * offset + 0  (16 bits): TMDS table pointer
2465          *
2466          * This table here is typically found just before the DCB table, with a
2467          * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
2468          * length?)
2469          *
2470          * At offset +7 is a pointer to a script, which I don't know how to run yet
2471          * At offset +9 is a pointer to another script, likewise
2472          * Offset +11 has a pointer to a table where the first word is a pxclk
2473          * frequency and the second word a pointer to a script, which should be
2474          * run if the comparison pxclk frequency is less than the pxclk desired.
2475          * This repeats for decreasing comparison frequencies
2476          * Offset +13 has a pointer to a similar table
2477          * The selection of table (and possibly +7/+9 script) is dictated by
2478          * "or" from the DCB.
2479          */
2480
2481         uint16_t tmdstableptr, script1, script2;
2482
2483         if (bitentry->length != 2) {
2484                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2485                            "Do not understand BIT TMDS table entry.\n");
2486                 return 0;
2487         }
2488
2489         tmdstableptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset])));
2490
2491         if (tmdstableptr == 0x0) {
2492                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Pointer to TMDS table invalid\n");
2493                 return 0;
2494         }
2495
2496         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Found TMDS table revision %d.%d\n",
2497                    bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
2498
2499         /* These two scripts are odd: they don't seem to get run even when they are not stubbed */
2500         script1 = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 7]));
2501         script2 = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 9]));
2502         if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
2503                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "TMDS table script pointers not stubbed\n");
2504
2505         bios->tmds.output0_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 11]));
2506         bios->tmds.output1_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[tmdstableptr + 13]));
2507
2508         return 1;
2509 }
2510
2511 static unsigned int parse_bmp_table_pointers(ScrnInfoPtr pScrn, bios_t *bios, bit_entry_t *bitentry)
2512 {
2513         /* Parse the pointers for useful tables in the BMP structure, starting at
2514          * offset 75 from the ..NV. signature.
2515          *
2516          * First 7 pointers as for parse_bit_init_tbl_entry
2517          *
2518          * offset + 30: flat panel timings table pointer
2519          * offset + 32: flat panel strapping translation table pointer
2520          * offset + 42: LVDS manufacturer panel config table pointer
2521          * offset + 44: LVDS manufacturer strapping translation table pointer
2522          */
2523
2524         NVPtr pNv = NVPTR(pScrn);
2525         struct fppointers fpp;
2526
2527         if (!parse_bit_init_tbl_entry(pScrn, bios, bitentry))
2528                 return 0;
2529
2530         /* If it's not a laptop, you probably don't care about fptables */
2531         /* FIXME: detect mobile BIOS? */
2532         if (!pNv->Mobile)
2533                 return 1;
2534
2535         if (bitentry->length > 17) {
2536                 bios->tmds.output0_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[bitentry->offset + 14]));
2537                 bios->tmds.output1_script_ptr = le16_to_cpu(*((uint16_t *)&bios->data[bitentry->offset + 16]));
2538         }
2539
2540         memset(&fpp, 0, sizeof(struct fppointers));
2541         if (bitentry->length > 33) {
2542                 fpp.fptablepointer = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 30])));
2543                 fpp.fpxlatetableptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 32])));
2544                 parse_fp_mode_table(pScrn, bios, &fpp);
2545         }
2546         if (bitentry->length > 45) {
2547                 fpp.lvdsmanufacturerpointer = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 42])));
2548                 fpp.fpxlatemanufacturertableptr = le16_to_cpu(*((uint16_t *)(&bios->data[bitentry->offset + 44])));
2549                 parse_lvds_manufacturer_table(pScrn, bios, &fpp);
2550                 /* I've never seen a valid LVDS_INIT script, so we'll do a test for it here */
2551                 call_lvds_script(pScrn, 0, 0, LVDS_INIT);
2552         }
2553
2554         return 1;
2555 }
2556
2557 static void parse_bit_structure(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset)
2558 {
2559         bit_entry_t bitentry, storedinitentry = {{ 0 }};
2560         char done = 0;
2561
2562         while (!done) {
2563                 bitentry.id[0] = bios->data[offset];
2564                 bitentry.id[1] = bios->data[offset + 1];
2565                 bitentry.length = le16_to_cpu(*((uint16_t *)&bios->data[offset + 2]));
2566                 bitentry.offset = le16_to_cpu(*((uint16_t *)&bios->data[offset + 4]));
2567
2568                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2569                            "0x%04X: Found BIT command with id 0x%02X (%c)\n",
2570                            offset, bitentry.id[0], bitentry.id[0]);
2571
2572                 switch (bitentry.id[0]) {
2573                 case 0:
2574                         /* id[0] = 0 and id[1] = 0 ==> end of BIT struture */
2575                         if (bitentry.id[1] == 0)
2576                                 done = 1;
2577                         break;
2578                 case 'B':
2579                         parse_bit_b_tbl_entry(pScrn, bios, &bitentry);
2580                         break;
2581                 case 'D':
2582                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2583                                    "0x%04X: Found flat panel display table entry in BIT structure.\n", offset);
2584                         parse_bit_display_tbl_entry(pScrn, bios, &bitentry);
2585                         break;
2586                 case 'I':
2587                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2588                                    "0x%04X: Found init table entry in BIT structure.\n", offset);
2589                         memcpy(&storedinitentry, &bitentry, sizeof(bit_entry_t));
2590                         break;
2591                 case 'M': /* memory? */
2592                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2593                                    "0x%04X: Found M table entry in BIT structure.\n", offset);
2594                         parse_bit_m_tbl_entry(pScrn, bios, &bitentry);
2595                         break;
2596                 case 'T':
2597                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2598                                    "0x%04X: Found TMDS table entry in BIT structure.\n", offset);
2599                         parse_bit_tmds_tbl_entry(pScrn, bios, &bitentry);
2600                         break;
2601
2602                         /* TODO: What kind of information does the other BIT entrys point to?
2603                          *       'P' entry is probably performance tables, but there are
2604                          *       quite a few others...
2605                          */
2606                 }
2607
2608                 offset += sizeof(bit_entry_t);
2609         }
2610
2611         /* 'M' table has to be parsed before 'I' can run */
2612         if (storedinitentry.id[0]) {
2613                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2614                            "Parsing previously deferred init table entry.\n");
2615                 parse_bit_init_tbl_entry(pScrn, bios, &storedinitentry);
2616         }
2617 }
2618
2619 static void parse_pins_structure(ScrnInfoPtr pScrn, bios_t *bios, unsigned int offset)
2620 {
2621         int pins_version_major=bios->data[offset+5];
2622         int pins_version_minor=bios->data[offset+6];
2623         int init1 = bios->data[offset + 18] + (bios->data[offset + 19] * 256);
2624         int init2 = bios->data[offset + 20] + (bios->data[offset + 21] * 256);
2625         int init_size = bios->data[offset + 22] + (bios->data[offset + 23] * 256) + 1;
2626         int ram_tab;
2627
2628         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "PINS version %d.%d\n",
2629                    pins_version_major, pins_version_minor);
2630
2631         /* checksum */
2632         if (nv_cksum(bios->data + offset, 8)) {
2633                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "bad PINS checksum\n");
2634                 return;
2635         }
2636
2637         switch (pins_version_major) {
2638                 case 2:
2639                         ram_tab = init1-0x0010;
2640                         break;
2641                 case 3:
2642                 case 4:
2643                 case 5:
2644                         ram_tab = bios->data[offset + 24] + (bios->data[offset + 25] * 256);
2645                         break;
2646                 default:
2647                         return;
2648         }
2649         
2650         if ((pins_version_major==5)&&(pins_version_minor>=6)) {
2651                 /* VCO range info */
2652         }
2653
2654         if ((pins_version_major==5)&&(pins_version_minor>=16)) {
2655                 bit_entry_t bitentry;
2656
2657                 if (pins_version_minor == 0x10)
2658                         bitentry.length = 12; /* I've not seen this version, so be "long enough" */
2659                 else if (pins_version_minor < 0x14)
2660                         bitentry.length = 34;
2661                 else
2662                         bitentry.length = 48; /* versions after 0x14 are longer,
2663                                                  but extra contents unneeded ATM */
2664
2665                 parse_bios_version(pScrn, bios, offset + 10);
2666                 bitentry.offset = offset + 75;
2667                 parse_bmp_table_pointers(pScrn, bios, &bitentry);
2668         } else {
2669                 /* TODO type1 script */
2670         }
2671 }
2672
2673 static unsigned int findstr(bios_t* bios, unsigned char *str, int len)
2674 {
2675         int i;
2676
2677         for (i = 2; i <= (bios->length - len); i++)
2678                 if (strncmp((char *)&bios->data[i], (char *)str, len) == 0)
2679                         return i;
2680
2681         return 0;
2682 }
2683
2684 static Bool parse_dcb_entry(uint8_t dcb_version, uint32_t conn, uint32_t conf, struct dcb_entry *entry)
2685 {
2686         memset(entry, 0, sizeof (struct dcb_entry));
2687
2688         if (dcb_version >= 0x20) {
2689                 entry->type = conn & 0xf;
2690                 entry->i2c_index = (conn >> 4) & 0xf;
2691                 entry->heads = (conn >> 8) & 0xf;
2692                 entry->bus = (conn >> 16) & 0xf;
2693                 entry->location = (conn >> 20) & 0xf;
2694                 entry->or = (conn >> 24) & 0xf;
2695                 if ((1 << ffs(entry->or)) * 3 == entry->or)
2696                         entry->duallink_possible = TRUE;
2697                 else
2698                         entry->duallink_possible = FALSE;
2699
2700                 switch (entry->type) {
2701                 case OUTPUT_LVDS:
2702                         if (conf & 0xfffffffa)
2703                                 ErrorF("Unknown LVDS configuration bits, please report\n");
2704                         if (conf & 0x1)
2705                                 entry->lvdsconf.use_straps_for_mode = TRUE;
2706                         if (conf & 0x4)
2707                                 entry->lvdsconf.use_power_scripts = TRUE;
2708                         break;
2709                 }
2710         } else if (dcb_version >= 0x14 ) {
2711                 if (conn != 0xf0003f00) {
2712                         ErrorF("Unknown DCB 1.4 entry, please report\n");
2713                         return FALSE;
2714                 }
2715                 /* safe defaults for a crt */
2716                 entry->type = 0;
2717                 entry->i2c_index = 0;
2718                 entry->heads = 1;
2719                 entry->bus = 0;
2720                 entry->location = 0;
2721                 entry->or = 1;
2722                 entry->duallink_possible = FALSE;
2723         } else {
2724                 // 1.2 needs more loving
2725                 return FALSE;
2726                 entry->type = 0;
2727                 entry->i2c_index = 0;
2728                 entry->heads = 0;
2729                 entry->bus = 0;
2730                 entry->location = 0;
2731                 entry->or = 0;
2732                 entry->duallink_possible = FALSE;
2733         }
2734
2735         return TRUE;
2736 }
2737
2738 static void
2739 read_dcb_i2c_table(ScrnInfoPtr pScrn, bios_t *bios, uint8_t dcb_version, uint16_t i2ctabptr)
2740 {
2741         NVPtr pNv = NVPTR(pScrn);
2742         uint8_t *i2ctable;
2743         uint8_t headerlen = 0;
2744         int i2c_entries;
2745         int recordoffset = 0, rdofs = 1, wrofs = 0;
2746         int i;
2747
2748         i2c_entries = MAX_NUM_DCB_ENTRIES;
2749         memset(pNv->dcb_table.i2c_read, 0, sizeof(pNv->dcb_table.i2c_read));
2750         memset(pNv->dcb_table.i2c_write, 0, sizeof(pNv->dcb_table.i2c_write));
2751
2752         i2ctable = &bios->data[i2ctabptr];
2753
2754         if (dcb_version >= 0x30) {
2755                 if (i2ctable[0] != dcb_version) { /* necessary? */
2756                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2757                                    "DCB I2C table version mismatch (%02X vs %02X)\n",
2758                                    i2ctable[0], dcb_version);
2759                         return;
2760                 }
2761                 headerlen = i2ctable[1];
2762                 i2c_entries = i2ctable[2];
2763                 if (i2ctable[0] >= 0x40) {
2764                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2765                                    "G80 DCB I2C table detected, arrgh\n"); /* they're plain weird */
2766                         return;
2767                 }
2768         }
2769         /* it's your own fault if you call this function on a DCB 1.1 BIOS */
2770         if (dcb_version < 0x14) {
2771                 recordoffset = 2;
2772                 rdofs = 0;
2773                 wrofs = 1;
2774         }
2775
2776         for (i = 0; i < i2c_entries; i++) {
2777                 if (i2ctable[headerlen + 4 * i + 3] != 0xff) {
2778                         pNv->dcb_table.i2c_read[i] = i2ctable[headerlen + recordoffset + rdofs + 4 * i];
2779                         pNv->dcb_table.i2c_write[i] = i2ctable[headerlen + recordoffset + wrofs + 4 * i];
2780                 }
2781         }
2782 }
2783
2784 static unsigned int parse_dcb_table(ScrnInfoPtr pScrn, bios_t *bios)
2785 {
2786         NVPtr pNv = NVPTR(pScrn);
2787         uint16_t dcbptr, i2ctabptr = 0;
2788         uint8_t *dcbtable;
2789         uint8_t dcb_version, headerlen = 0x4, entries = MAX_NUM_DCB_ENTRIES;
2790         Bool configblock = TRUE;
2791         int recordlength = 8, confofs = 4;
2792         int i;
2793
2794         pNv->dcb_table.entries = 0;
2795
2796         /* get the offset from 0x36 */
2797         dcbptr = le16_to_cpu(*(uint16_t *)&bios->data[0x36]);
2798
2799         if (dcbptr == 0x0) {
2800                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2801                            "No Display Configuration Block pointer found\n");
2802                 return 0;
2803         }
2804
2805         dcbtable = &bios->data[dcbptr];
2806
2807         /* get DCB version */
2808         dcb_version = dcbtable[0];
2809         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2810                    "Display Configuration Block version %d.%d found\n",
2811                    dcb_version >> 4, dcb_version & 0xf);
2812
2813         if (dcb_version >= 0x20) { /* NV17+ */
2814                 uint32_t sig;
2815
2816                 if (dcb_version >= 0x30) { /* NV40+ */
2817                         headerlen = dcbtable[1];
2818                         entries = dcbtable[2];
2819                         i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[4]);
2820                         sig = le32_to_cpu(*(uint32_t *)&dcbtable[6]);
2821
2822                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2823                                    "DCB header length %02X, with %02X possible entries\n",
2824                                    headerlen, entries);
2825                 } else {
2826                         /* dcb_block_count = *(dcbtable[1]); */
2827                         i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
2828                         sig = le32_to_cpu(*(uint32_t *)&dcbtable[4]);
2829                         headerlen = 8;
2830                 }
2831
2832                 if (sig != 0x4edcbdcb) {
2833                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2834                                    "Bad Display Configuration Block signature (%08X)\n", sig);
2835                         return 0;
2836                 }
2837         } else if (dcb_version >= 0x14) { /* some NV15/16, and NV11+ */
2838                 char sig[8];
2839
2840                 memset(sig, 0, 8);
2841                 strncpy(sig, (char *)&dcbtable[-7], 7);
2842                 /* dcb_block_count = *(dcbtable[1]); */
2843                 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
2844                 recordlength = 10;
2845                 confofs = 6;
2846
2847                 if (strcmp(sig, "DEV_REC")) {
2848                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2849                                    "Bad Display Configuration Block signature (%s)\n", sig);
2850                         return 0;
2851                 }
2852         } else if (dcb_version >= 0x12) { /* some NV6/10, and NV15+ */
2853                 /* dcb_block_count = *(dcbtable[1]); */
2854                 i2ctabptr = le16_to_cpu(*(uint16_t *)&dcbtable[2]);
2855                 configblock = FALSE;
2856         } else {        /* NV5+, maybe NV4 */
2857                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2858                            "Structure of Display Configuration Blocks prior to version 1.2 unknown\n");
2859                 return 0;
2860         }
2861
2862         if (entries >= MAX_NUM_DCB_ENTRIES)
2863                 entries = MAX_NUM_DCB_ENTRIES;
2864
2865         for (i = 0; i < entries; i++) {
2866                 uint32_t connection, config = 0;
2867
2868                 connection = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + recordlength * i]);
2869                 if (configblock)
2870                         config = le32_to_cpu(*(uint32_t *)&dcbtable[headerlen + confofs + recordlength * i]);
2871
2872                 /* Should we allow discontinuous DCBs? Certainly DCB I2C tables
2873                  * can be discontinuous */
2874                 if ((connection & 0x0000000f) == 0x0000000f) /* end of records */
2875                         break;
2876
2877                 ErrorF("Raw DCB entry %d: %08x %08x\n", i, connection, config);
2878                 if (!parse_dcb_entry(dcb_version, connection, config, &pNv->dcb_table.entry[i]))
2879                         break;
2880         }
2881         pNv->dcb_table.entries = i;
2882
2883         read_dcb_i2c_table(pScrn, bios, dcb_version, i2ctabptr);
2884
2885         /* This is needed for DCB version 2.0 */
2886         /* Otherwise you end up with multiple outputs that are trying to be activated */
2887         for ( i = 0; i < pNv->dcb_table.entries; i ++) {
2888                 int j;
2889                 int cur_i2c = pNv->dcb_table.entry[i].i2c_index;
2890                 int cur_type = pNv->dcb_table.entry[i].type;
2891                 for ( j = 0; j < pNv->dcb_table.entries; j ++ ) {
2892                         if ( i == j ) continue;
2893                         if ( pNv->dcb_table.entry[j].type == 100) continue; /* merged entry */
2894                         if (( pNv->dcb_table.entry[j].i2c_index == cur_i2c )  && ( pNv->dcb_table.entry[j].type == cur_type ))  {
2895                                 /* We can only merge entries with the same allowed crtc's. */
2896                                 /* This has not occured so far and needs some logic (to merge dual link properly). */ 
2897                                 /* So this remains TODO for the moment. */
2898
2899                                 /* We also merge entries with the same allowed output routes */
2900                                 if (pNv->dcb_table.entry[i].or == pNv->dcb_table.entry[j].or) {
2901                                         xf86DrvMsg(0, X_INFO, "Merging DCB entries %d and %d!\n", i, j);
2902                                         pNv->dcb_table.entry[i].heads |= pNv->dcb_table.entry[j].heads;
2903
2904                                         pNv->dcb_table.entry[j].type = 100; /* dummy value */
2905                                 }
2906                         }
2907                 }
2908         }
2909
2910         /* Remove "disabled" entries (merged) */
2911         int valid_entries[pNv->dcb_table.entries];
2912         int cent = 0;
2913         for ( i = 0; i < pNv->dcb_table.entries; i ++) valid_entries[i] = -1;
2914         for ( i = 0; i < pNv->dcb_table.entries; i ++)
2915                 if ( pNv->dcb_table.entry[i].type != 100 ) {
2916                         valid_entries[cent] = i;
2917                         cent++;
2918                 }
2919         for ( i = 0; i < cent; i++) {
2920                 memmove(&pNv->dcb_table.entry[i], &pNv->dcb_table.entry[valid_entries[i]], sizeof(pNv->dcb_table.entry[i]));
2921                 memmove(&pNv->dcb_table.i2c_read[i], &pNv->dcb_table.i2c_read[valid_entries[i]], sizeof(pNv->dcb_table.i2c_read[i]));
2922                 memmove(&pNv->dcb_table.i2c_write[i], &pNv->dcb_table.i2c_write[valid_entries[i]], sizeof(pNv->dcb_table.i2c_write[i]));
2923         }
2924
2925         pNv->dcb_table.entries = cent;
2926
2927         return pNv->dcb_table.entries;
2928 }
2929
2930 unsigned int NVParseBios(ScrnInfoPtr pScrn)
2931 {
2932         unsigned int bit_offset;
2933         uint8_t nv_signature[]={0xff,0x7f,'N','V',0x0};
2934         uint8_t bit_signature[]={'B','I','T'};
2935         NVPtr pNv;
2936         pNv = NVPTR(pScrn);
2937
2938         pNv->dcb_table.entries = 0;
2939
2940         memset(&pNv->VBIOS, 0, sizeof(bios_t));
2941         pNv->VBIOS.execute = FALSE;
2942         pNv->VBIOS.data = xalloc(64 * 1024);
2943         if (!NVShadowVBIOS(pScrn, pNv->VBIOS.data)) {
2944                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
2945                            "No valid BIOS image found.\n");
2946                 xfree(pNv->VBIOS.data);
2947                 return 0;
2948         }
2949         pNv->VBIOS.length = pNv->VBIOS.data[2] * 512;
2950         if (pNv->VBIOS.length > NV_PROM_SIZE)
2951                 pNv->VBIOS.length = NV_PROM_SIZE;
2952
2953         /* parse Display Configuration Block (DCB) table */
2954         if (parse_dcb_table(pScrn, &pNv->VBIOS))
2955                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2956                            "Found %d entries in DCB.\n", pNv->dcb_table.entries);
2957
2958         /* check for known signatures */
2959         if ((bit_offset = findstr(&pNv->VBIOS, bit_signature, sizeof(bit_signature)))) {
2960                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "BIT signature found.\n");
2961                 parse_bit_structure(pScrn, &pNv->VBIOS, bit_offset + 4);
2962         } else if ((bit_offset = findstr(&pNv->VBIOS, nv_signature, sizeof(nv_signature)))) {
2963                 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "NV signature found.\n");
2964                 parse_pins_structure(pScrn, &pNv->VBIOS, bit_offset);
2965         } else
2966                 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
2967                            "No known script signature found.\n");
2968
2969         return 1;
2970 }