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