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