Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
nouveau_bios.c
Go to the documentation of this file.
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 <subdev/bios.h>
26 
27 #include <drm/drmP.h>
28 
29 #include "nouveau_drm.h"
30 #include "nouveau_reg.h"
31 #include "nouveau_hw.h"
32 #include "nouveau_encoder.h"
33 
34 #include <linux/io-mapping.h>
35 #include <linux/firmware.h>
36 
37 /* these defines are made up */
38 #define NV_CIO_CRE_44_HEADA 0x0
39 #define NV_CIO_CRE_44_HEADB 0x3
40 #define FEATURE_MOBILE 0x10 /* also FEATURE_QUADRO for BMP */
41 
42 #define EDID1_LEN 128
43 
44 #define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
45 #define LOG_OLD_VALUE(x)
46 
47 struct init_exec {
48  bool execute;
49  bool repeat;
50 };
51 
52 static bool nv_cksum(const uint8_t *data, unsigned int length)
53 {
54  /*
55  * There's a few checksums in the BIOS, so here's a generic checking
56  * function.
57  */
58  int i;
59  uint8_t sum = 0;
60 
61  for (i = 0; i < length; i++)
62  sum += data[i];
63 
64  if (sum)
65  return true;
66 
67  return false;
68 }
69 
70 static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
71 {
72  int compare_record_len, i = 0;
73  uint16_t compareclk, scriptptr = 0;
74 
75  if (bios->major_version < 5) /* pre BIT */
76  compare_record_len = 3;
77  else
78  compare_record_len = 4;
79 
80  do {
81  compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
82  if (pxclk >= compareclk * 10) {
83  if (bios->major_version < 5) {
84  uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
85  scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
86  } else
87  scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
88  break;
89  }
90  i++;
91  } while (compareclk);
92 
93  return scriptptr;
94 }
95 
96 static void
97 run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
98  struct dcb_output *dcbent, int head, bool dl)
99 {
100  struct nouveau_drm *drm = nouveau_drm(dev);
101 
102  NV_INFO(drm, "0x%04X: Parsing digital output script table\n",
103  scriptptr);
104  NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, head ? NV_CIO_CRE_44_HEADB :
106  nouveau_bios_run_init_table(dev, scriptptr, dcbent, head);
107 
108  nv04_dfp_bind_head(dev, dcbent, head, dl);
109 }
110 
111 static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script)
112 {
113  struct nouveau_drm *drm = nouveau_drm(dev);
114  struct nvbios *bios = &drm->vbios;
115  uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & DCB_OUTPUT_C ? 1 : 0);
116  uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
117 
118  if (!bios->fp.xlated_entry || !sub || !scriptofs)
119  return -EINVAL;
120 
121  run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
122 
123  if (script == LVDS_PANEL_OFF) {
124  /* off-on delay in ms */
125  mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
126  }
127 #ifdef __powerpc__
128  /* Powerbook specific quirks */
129  if (script == LVDS_RESET &&
130  (dev->pci_device == 0x0179 || dev->pci_device == 0x0189 ||
131  dev->pci_device == 0x0329))
132  nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
133 #endif
134 
135  return 0;
136 }
137 
138 static int run_lvds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
139 {
140  /*
141  * The BIT LVDS table's header has the information to setup the
142  * necessary registers. Following the standard 4 byte header are:
143  * A bitmask byte and a dual-link transition pxclk value for use in
144  * selecting the init script when not using straps; 4 script pointers
145  * for panel power, selected by output and on/off; and 8 table pointers
146  * for panel init, the needed one determined by output, and bits in the
147  * conf byte. These tables are similar to the TMDS tables, consisting
148  * of a list of pxclks and script pointers.
149  */
150  struct nouveau_drm *drm = nouveau_drm(dev);
151  struct nvbios *bios = &drm->vbios;
152  unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
153  uint16_t scriptptr = 0, clktable;
154 
155  /*
156  * For now we assume version 3.0 table - g80 support will need some
157  * changes
158  */
159 
160  switch (script) {
161  case LVDS_INIT:
162  return -ENOSYS;
163  case LVDS_BACKLIGHT_ON:
164  case LVDS_PANEL_ON:
165  scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
166  break;
167  case LVDS_BACKLIGHT_OFF:
168  case LVDS_PANEL_OFF:
169  scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
170  break;
171  case LVDS_RESET:
172  clktable = bios->fp.lvdsmanufacturerpointer + 15;
173  if (dcbent->or == 4)
174  clktable += 8;
175 
176  if (dcbent->lvdsconf.use_straps_for_mode) {
177  if (bios->fp.dual_link)
178  clktable += 4;
179  if (bios->fp.if_is_24bit)
180  clktable += 2;
181  } else {
182  /* using EDID */
183  int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
184 
185  if (bios->fp.dual_link) {
186  clktable += 4;
187  cmpval_24bit <<= 1;
188  }
189 
190  if (bios->fp.strapless_is_24bit & cmpval_24bit)
191  clktable += 2;
192  }
193 
194  clktable = ROM16(bios->data[clktable]);
195  if (!clktable) {
196  NV_ERROR(drm, "Pixel clock comparison table not found\n");
197  return -ENOENT;
198  }
199  scriptptr = clkcmptable(bios, clktable, pxclk);
200  }
201 
202  if (!scriptptr) {
203  NV_ERROR(drm, "LVDS output init script not found\n");
204  return -ENOENT;
205  }
206  run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
207 
208  return 0;
209 }
210 
211 int call_lvds_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
212 {
213  /*
214  * LVDS operations are multiplexed in an effort to present a single API
215  * which works with two vastly differing underlying structures.
216  * This acts as the demux
217  */
218 
219  struct nouveau_drm *drm = nouveau_drm(dev);
220  struct nouveau_device *device = nv_device(drm->device);
221  struct nvbios *bios = &drm->vbios;
222  uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
223  uint32_t sel_clk_binding, sel_clk;
224  int ret;
225 
226  if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
227  (lvds_ver >= 0x30 && script == LVDS_INIT))
228  return 0;
229 
230  if (!bios->fp.lvds_init_run) {
231  bios->fp.lvds_init_run = true;
232  call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
233  }
234 
235  if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
236  call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
237  if (script == LVDS_RESET && bios->fp.power_off_for_reset)
238  call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
239 
240  NV_INFO(drm, "Calling LVDS script %d:\n", script);
241 
242  /* don't let script change pll->head binding */
243  sel_clk_binding = nv_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
244 
245  if (lvds_ver < 0x30)
246  ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
247  else
248  ret = run_lvds_table(dev, dcbent, head, script, pxclk);
249 
250  bios->fp.last_script_invoc = (script << 1 | head);
251 
252  sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
253  NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
254  /* some scripts set a value in NV_PBUS_POWERCTRL_2 and break video overlay */
255  nv_wr32(device, NV_PBUS_POWERCTRL_2, 0);
256 
257  return ret;
258 }
259 
262 };
263 
264 static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
265 {
266  /*
267  * BMP version (0xa) LVDS table has a simple header of version and
268  * record length. The BIT LVDS table has the typical BIT table header:
269  * version byte, header length byte, record length byte, and a byte for
270  * the maximum number of records that can be held in the table.
271  */
272 
273  struct nouveau_drm *drm = nouveau_drm(dev);
274  uint8_t lvds_ver, headerlen, recordlen;
275 
276  memset(lth, 0, sizeof(struct lvdstableheader));
277 
278  if (bios->fp.lvdsmanufacturerpointer == 0x0) {
279  NV_ERROR(drm, "Pointer to LVDS manufacturer table invalid\n");
280  return -EINVAL;
281  }
282 
283  lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
284 
285  switch (lvds_ver) {
286  case 0x0a: /* pre NV40 */
287  headerlen = 2;
288  recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
289  break;
290  case 0x30: /* NV4x */
291  headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
292  if (headerlen < 0x1f) {
293  NV_ERROR(drm, "LVDS table header not understood\n");
294  return -EINVAL;
295  }
296  recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
297  break;
298  case 0x40: /* G80/G90 */
299  headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
300  if (headerlen < 0x7) {
301  NV_ERROR(drm, "LVDS table header not understood\n");
302  return -EINVAL;
303  }
304  recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
305  break;
306  default:
307  NV_ERROR(drm,
308  "LVDS table revision %d.%d not currently supported\n",
309  lvds_ver >> 4, lvds_ver & 0xf);
310  return -ENOSYS;
311  }
312 
313  lth->lvds_ver = lvds_ver;
314  lth->headerlen = headerlen;
315  lth->recordlen = recordlen;
316 
317  return 0;
318 }
319 
320 static int
321 get_fp_strap(struct drm_device *dev, struct nvbios *bios)
322 {
323  struct nouveau_device *device = nouveau_dev(dev);
324 
325  /*
326  * The fp strap is normally dictated by the "User Strap" in
327  * PEXTDEV_BOOT_0[20:16], but on BMP cards when bit 2 of the
328  * Internal_Flags struct at 0x48 is set, the user strap gets overriden
329  * by the PCI subsystem ID during POST, but not before the previous user
330  * strap has been committed to CR58 for CR57=0xf on head A, which may be
331  * read and used instead
332  */
333 
334  if (bios->major_version < 5 && bios->data[0x48] & 0x4)
335  return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
336 
337  if (device->card_type >= NV_50)
338  return (nv_rd32(device, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
339  else
340  return (nv_rd32(device, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
341 }
342 
343 static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
344 {
345  struct nouveau_drm *drm = nouveau_drm(dev);
346  uint8_t *fptable;
347  uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
348  int ret, ofs, fpstrapping;
349  struct lvdstableheader lth;
350 
351  if (bios->fp.fptablepointer == 0x0) {
352  /* Apple cards don't have the fp table; the laptops use DDC */
353  /* The table is also missing on some x86 IGPs */
354 #ifndef __powerpc__
355  NV_ERROR(drm, "Pointer to flat panel table invalid\n");
356 #endif
357  bios->digital_min_front_porch = 0x4b;
358  return 0;
359  }
360 
361  fptable = &bios->data[bios->fp.fptablepointer];
362  fptable_ver = fptable[0];
363 
364  switch (fptable_ver) {
365  /*
366  * BMP version 0x5.0x11 BIOSen have version 1 like tables, but no
367  * version field, and miss one of the spread spectrum/PWM bytes.
368  * This could affect early GF2Go parts (not seen any appropriate ROMs
369  * though). Here we assume that a version of 0x05 matches this case
370  * (combining with a BMP version check would be better), as the
371  * common case for the panel type field is 0x0005, and that is in
372  * fact what we are reading the first byte of.
373  */
374  case 0x05: /* some NV10, 11, 15, 16 */
375  recordlen = 42;
376  ofs = -1;
377  break;
378  case 0x10: /* some NV15/16, and NV11+ */
379  recordlen = 44;
380  ofs = 0;
381  break;
382  case 0x20: /* NV40+ */
383  headerlen = fptable[1];
384  recordlen = fptable[2];
385  fpentries = fptable[3];
386  /*
387  * fptable[4] is the minimum
388  * RAMDAC_FP_HCRTC -> RAMDAC_FP_HSYNC_START gap
389  */
390  bios->digital_min_front_porch = fptable[4];
391  ofs = -7;
392  break;
393  default:
394  NV_ERROR(drm,
395  "FP table revision %d.%d not currently supported\n",
396  fptable_ver >> 4, fptable_ver & 0xf);
397  return -ENOSYS;
398  }
399 
400  if (!bios->is_mobile) /* !mobile only needs digital_min_front_porch */
401  return 0;
402 
403  ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
404  if (ret)
405  return ret;
406 
407  if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
408  bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
409  lth.headerlen + 1;
410  bios->fp.xlatwidth = lth.recordlen;
411  }
412  if (bios->fp.fpxlatetableptr == 0x0) {
413  NV_ERROR(drm, "Pointer to flat panel xlat table invalid\n");
414  return -EINVAL;
415  }
416 
417  fpstrapping = get_fp_strap(dev, bios);
418 
419  fpindex = bios->data[bios->fp.fpxlatetableptr +
420  fpstrapping * bios->fp.xlatwidth];
421 
422  if (fpindex > fpentries) {
423  NV_ERROR(drm, "Bad flat panel table index\n");
424  return -ENOENT;
425  }
426 
427  /* nv4x cards need both a strap value and fpindex of 0xf to use DDC */
428  if (lth.lvds_ver > 0x10)
429  bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
430 
431  /*
432  * If either the strap or xlated fpindex value are 0xf there is no
433  * panel using a strap-derived bios mode present. this condition
434  * includes, but is different from, the DDC panel indicator above
435  */
436  if (fpstrapping == 0xf || fpindex == 0xf)
437  return 0;
438 
439  bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
440  recordlen * fpindex + ofs;
441 
442  NV_INFO(drm, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
443  ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
444  ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
445  ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
446 
447  return 0;
448 }
449 
451 {
452  struct nouveau_drm *drm = nouveau_drm(dev);
453  struct nvbios *bios = &drm->vbios;
454  uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
455 
456  if (!mode) /* just checking whether we can produce a mode */
457  return bios->fp.mode_ptr;
458 
459  memset(mode, 0, sizeof(struct drm_display_mode));
460  /*
461  * For version 1.0 (version in byte 0):
462  * bytes 1-2 are "panel type", including bits on whether Colour/mono,
463  * single/dual link, and type (TFT etc.)
464  * bytes 3-6 are bits per colour in RGBX
465  */
466  mode->clock = ROM16(mode_entry[7]) * 10;
467  /* bytes 9-10 is HActive */
468  mode->hdisplay = ROM16(mode_entry[11]) + 1;
469  /*
470  * bytes 13-14 is HValid Start
471  * bytes 15-16 is HValid End
472  */
473  mode->hsync_start = ROM16(mode_entry[17]) + 1;
474  mode->hsync_end = ROM16(mode_entry[19]) + 1;
475  mode->htotal = ROM16(mode_entry[21]) + 1;
476  /* bytes 23-24, 27-30 similarly, but vertical */
477  mode->vdisplay = ROM16(mode_entry[25]) + 1;
478  mode->vsync_start = ROM16(mode_entry[31]) + 1;
479  mode->vsync_end = ROM16(mode_entry[33]) + 1;
480  mode->vtotal = ROM16(mode_entry[35]) + 1;
481  mode->flags |= (mode_entry[37] & 0x10) ?
483  mode->flags |= (mode_entry[37] & 0x1) ?
485  /*
486  * bytes 38-39 relate to spread spectrum settings
487  * bytes 40-43 are something to do with PWM
488  */
489 
490  mode->status = MODE_OK;
492  drm_mode_set_name(mode);
493  return bios->fp.mode_ptr;
494 }
495 
496 int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
497 {
498  /*
499  * The LVDS table header is (mostly) described in
500  * parse_lvds_manufacturer_table_header(): the BIT header additionally
501  * contains the dual-link transition pxclk (in 10s kHz), at byte 5 - if
502  * straps are not being used for the panel, this specifies the frequency
503  * at which modes should be set up in the dual link style.
504  *
505  * Following the header, the BMP (ver 0xa) table has several records,
506  * indexed by a separate xlat table, indexed in turn by the fp strap in
507  * EXTDEV_BOOT. Each record had a config byte, followed by 6 script
508  * numbers for use by INIT_SUB which controlled panel init and power,
509  * and finally a dword of ms to sleep between power off and on
510  * operations.
511  *
512  * In the BIT versions, the table following the header serves as an
513  * integrated config and xlat table: the records in the table are
514  * indexed by the FP strap nibble in EXTDEV_BOOT, and each record has
515  * two bytes - the first as a config byte, the second for indexing the
516  * fp mode table pointed to by the BIT 'D' table
517  *
518  * DDC is not used until after card init, so selecting the correct table
519  * entry and setting the dual link flag for EDID equipped panels,
520  * requiring tests against the native-mode pixel clock, cannot be done
521  * until later, when this function should be called with non-zero pxclk
522  */
523  struct nouveau_drm *drm = nouveau_drm(dev);
524  struct nvbios *bios = &drm->vbios;
525  int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
526  struct lvdstableheader lth;
527  uint16_t lvdsofs;
528  int ret, chip_version = bios->chip_version;
529 
530  ret = parse_lvds_manufacturer_table_header(dev, bios, &lth);
531  if (ret)
532  return ret;
533 
534  switch (lth.lvds_ver) {
535  case 0x0a: /* pre NV40 */
536  lvdsmanufacturerindex = bios->data[
537  bios->fp.fpxlatemanufacturertableptr +
538  fpstrapping];
539 
540  /* we're done if this isn't the EDID panel case */
541  if (!pxclk)
542  break;
543 
544  if (chip_version < 0x25) {
545  /* nv17 behaviour
546  *
547  * It seems the old style lvds script pointer is reused
548  * to select 18/24 bit colour depth for EDID panels.
549  */
550  lvdsmanufacturerindex =
551  (bios->legacy.lvds_single_a_script_ptr & 1) ?
552  2 : 0;
553  if (pxclk >= bios->fp.duallink_transition_clk)
554  lvdsmanufacturerindex++;
555  } else if (chip_version < 0x30) {
556  /* nv28 behaviour (off-chip encoder)
557  *
558  * nv28 does a complex dance of first using byte 121 of
559  * the EDID to choose the lvdsmanufacturerindex, then
560  * later attempting to match the EDID manufacturer and
561  * product IDs in a table (signature 'pidt' (panel id
562  * table?)), setting an lvdsmanufacturerindex of 0 and
563  * an fp strap of the match index (or 0xf if none)
564  */
565  lvdsmanufacturerindex = 0;
566  } else {
567  /* nv31, nv34 behaviour */
568  lvdsmanufacturerindex = 0;
569  if (pxclk >= bios->fp.duallink_transition_clk)
570  lvdsmanufacturerindex = 2;
571  if (pxclk >= 140000)
572  lvdsmanufacturerindex = 3;
573  }
574 
575  /*
576  * nvidia set the high nibble of (cr57=f, cr58) to
577  * lvdsmanufacturerindex in this case; we don't
578  */
579  break;
580  case 0x30: /* NV4x */
581  case 0x40: /* G80/G90 */
582  lvdsmanufacturerindex = fpstrapping;
583  break;
584  default:
585  NV_ERROR(drm, "LVDS table revision not currently supported\n");
586  return -ENOSYS;
587  }
588 
589  lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
590  switch (lth.lvds_ver) {
591  case 0x0a:
592  bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
593  bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
594  bios->fp.dual_link = bios->data[lvdsofs] & 4;
595  bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
596  *if_is_24bit = bios->data[lvdsofs] & 16;
597  break;
598  case 0x30:
599  case 0x40:
600  /*
601  * No sign of the "power off for reset" or "reset for panel
602  * on" bits, but it's safer to assume we should
603  */
604  bios->fp.power_off_for_reset = true;
605  bios->fp.reset_after_pclk_change = true;
606 
607  /*
608  * It's ok lvdsofs is wrong for nv4x edid case; dual_link is
609  * over-written, and if_is_24bit isn't used
610  */
611  bios->fp.dual_link = bios->data[lvdsofs] & 1;
612  bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
613  bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
614  bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
615  break;
616  }
617 
618  /* set dual_link flag for EDID case */
619  if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
620  bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
621 
622  *dl = bios->fp.dual_link;
623 
624  return 0;
625 }
626 
627 /* BIT 'U'/'d' table encoder subtables have hashes matching them to
628  * a particular set of encoders.
629  *
630  * This function returns true if a particular DCB entry matches.
631  */
632 bool
634 {
635  if ((hash & 0x000000f0) != (dcb->location << 4))
636  return false;
637  if ((hash & 0x0000000f) != dcb->type)
638  return false;
639  if (!(hash & (dcb->or << 16)))
640  return false;
641 
642  switch (dcb->type) {
643  case DCB_OUTPUT_TMDS:
644  case DCB_OUTPUT_LVDS:
645  case DCB_OUTPUT_DP:
646  if (hash & 0x00c00000) {
647  if (!(hash & (dcb->sorconf.link << 22)))
648  return false;
649  }
650  default:
651  return true;
652  }
653 }
654 
655 int
657  struct dcb_output *dcbent, int crtc)
658 {
659  /*
660  * The display script table is located by the BIT 'U' table.
661  *
662  * It contains an array of pointers to various tables describing
663  * a particular output type. The first 32-bits of the output
664  * tables contains similar information to a DCB entry, and is
665  * used to decide whether that particular table is suitable for
666  * the output you want to access.
667  *
668  * The "record header length" field here seems to indicate the
669  * offset of the first configuration entry in the output tables.
670  * This is 10 on most cards I've seen, but 12 has been witnessed
671  * on DP cards, and there's another script pointer within the
672  * header.
673  *
674  * offset + 0 ( 8 bits): version
675  * offset + 1 ( 8 bits): header length
676  * offset + 2 ( 8 bits): record length
677  * offset + 3 ( 8 bits): number of records
678  * offset + 4 ( 8 bits): record header length
679  * offset + 5 (16 bits): pointer to first output script table
680  */
681 
682  struct nouveau_drm *drm = nouveau_drm(dev);
683  struct nvbios *bios = &drm->vbios;
684  uint8_t *table = &bios->data[bios->display.script_table_ptr];
685  uint8_t *otable = NULL;
686  uint16_t script;
687  int i;
688 
689  if (!bios->display.script_table_ptr) {
690  NV_ERROR(drm, "No pointer to output script table\n");
691  return 1;
692  }
693 
694  /*
695  * Nothing useful has been in any of the pre-2.0 tables I've seen,
696  * so until they are, we really don't need to care.
697  */
698  if (table[0] < 0x20)
699  return 1;
700 
701  if (table[0] != 0x20 && table[0] != 0x21) {
702  NV_ERROR(drm, "Output script table version 0x%02x unknown\n",
703  table[0]);
704  return 1;
705  }
706 
707  /*
708  * The output script tables describing a particular output type
709  * look as follows:
710  *
711  * offset + 0 (32 bits): output this table matches (hash of DCB)
712  * offset + 4 ( 8 bits): unknown
713  * offset + 5 ( 8 bits): number of configurations
714  * offset + 6 (16 bits): pointer to some script
715  * offset + 8 (16 bits): pointer to some script
716  *
717  * headerlen == 10
718  * offset + 10 : configuration 0
719  *
720  * headerlen == 12
721  * offset + 10 : pointer to some script
722  * offset + 12 : configuration 0
723  *
724  * Each config entry is as follows:
725  *
726  * offset + 0 (16 bits): unknown, assumed to be a match value
727  * offset + 2 (16 bits): pointer to script table (clock set?)
728  * offset + 4 (16 bits): pointer to script table (reset?)
729  *
730  * There doesn't appear to be a count value to say how many
731  * entries exist in each script table, instead, a 0 value in
732  * the first 16-bit word seems to indicate both the end of the
733  * list and the default entry. The second 16-bit word in the
734  * script tables is a pointer to the script to execute.
735  */
736 
737  NV_DEBUG(drm, "Searching for output entry for %d %d %d\n",
738  dcbent->type, dcbent->location, dcbent->or);
739  for (i = 0; i < table[3]; i++) {
740  otable = ROMPTR(dev, table[table[1] + (i * table[2])]);
741  if (otable && bios_encoder_match(dcbent, ROM32(otable[0])))
742  break;
743  }
744 
745  if (!otable) {
746  NV_DEBUG(drm, "failed to match any output table\n");
747  return 1;
748  }
749 
750  if (pclk < -2 || pclk > 0) {
751  /* Try to find matching script table entry */
752  for (i = 0; i < otable[5]; i++) {
753  if (ROM16(otable[table[4] + i*6]) == type)
754  break;
755  }
756 
757  if (i == otable[5]) {
758  NV_ERROR(drm, "Table 0x%04x not found for %d/%d, "
759  "using first\n",
760  type, dcbent->type, dcbent->or);
761  i = 0;
762  }
763  }
764 
765  if (pclk == 0) {
766  script = ROM16(otable[6]);
767  if (!script) {
768  NV_DEBUG(drm, "output script 0 not found\n");
769  return 1;
770  }
771 
772  NV_DEBUG(drm, "0x%04X: parsing output script 0\n", script);
773  nouveau_bios_run_init_table(dev, script, dcbent, crtc);
774  } else
775  if (pclk == -1) {
776  script = ROM16(otable[8]);
777  if (!script) {
778  NV_DEBUG(drm, "output script 1 not found\n");
779  return 1;
780  }
781 
782  NV_DEBUG(drm, "0x%04X: parsing output script 1\n", script);
783  nouveau_bios_run_init_table(dev, script, dcbent, crtc);
784  } else
785  if (pclk == -2) {
786  if (table[4] >= 12)
787  script = ROM16(otable[10]);
788  else
789  script = 0;
790  if (!script) {
791  NV_DEBUG(drm, "output script 2 not found\n");
792  return 1;
793  }
794 
795  NV_DEBUG(drm, "0x%04X: parsing output script 2\n", script);
796  nouveau_bios_run_init_table(dev, script, dcbent, crtc);
797  } else
798  if (pclk > 0) {
799  script = ROM16(otable[table[4] + i*6 + 2]);
800  if (script)
801  script = clkcmptable(bios, script, pclk);
802  if (!script) {
803  NV_DEBUG(drm, "clock script 0 not found\n");
804  return 1;
805  }
806 
807  NV_DEBUG(drm, "0x%04X: parsing clock script 0\n", script);
808  nouveau_bios_run_init_table(dev, script, dcbent, crtc);
809  } else
810  if (pclk < 0) {
811  script = ROM16(otable[table[4] + i*6 + 4]);
812  if (script)
813  script = clkcmptable(bios, script, -pclk);
814  if (!script) {
815  NV_DEBUG(drm, "clock script 1 not found\n");
816  return 1;
817  }
818 
819  NV_DEBUG(drm, "0x%04X: parsing clock script 1\n", script);
820  nouveau_bios_run_init_table(dev, script, dcbent, crtc);
821  }
822 
823  return 0;
824 }
825 
826 
827 int run_tmds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, int pxclk)
828 {
829  /*
830  * the pxclk parameter is in kHz
831  *
832  * This runs the TMDS regs setting code found on BIT bios cards
833  *
834  * For ffs(or) == 1 use the first table, for ffs(or) == 2 and
835  * ffs(or) == 3, use the second.
836  */
837 
838  struct nouveau_drm *drm = nouveau_drm(dev);
839  struct nouveau_device *device = nv_device(drm->device);
840  struct nvbios *bios = &drm->vbios;
841  int cv = bios->chip_version;
842  uint16_t clktable = 0, scriptptr;
843  uint32_t sel_clk_binding, sel_clk;
844 
845  /* pre-nv17 off-chip tmds uses scripts, post nv17 doesn't */
846  if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
847  dcbent->location != DCB_LOC_ON_CHIP)
848  return 0;
849 
850  switch (ffs(dcbent->or)) {
851  case 1:
852  clktable = bios->tmds.output0_script_ptr;
853  break;
854  case 2:
855  case 3:
856  clktable = bios->tmds.output1_script_ptr;
857  break;
858  }
859 
860  if (!clktable) {
861  NV_ERROR(drm, "Pixel clock comparison table not found\n");
862  return -EINVAL;
863  }
864 
865  scriptptr = clkcmptable(bios, clktable, pxclk);
866 
867  if (!scriptptr) {
868  NV_ERROR(drm, "TMDS output init script not found\n");
869  return -ENOENT;
870  }
871 
872  /* don't let script change pll->head binding */
873  sel_clk_binding = nv_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
874  run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
875  sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
876  NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
877 
878  return 0;
879 }
880 
881 static void parse_bios_version(struct drm_device *dev, struct nvbios *bios, uint16_t offset)
882 {
883  /*
884  * offset + 0 (8 bits): Micro version
885  * offset + 1 (8 bits): Minor version
886  * offset + 2 (8 bits): Chip version
887  * offset + 3 (8 bits): Major version
888  */
889  struct nouveau_drm *drm = nouveau_drm(dev);
890 
891  bios->major_version = bios->data[offset + 3];
892  bios->chip_version = bios->data[offset + 2];
893  NV_INFO(drm, "Bios version %02x.%02x.%02x.%02x\n",
894  bios->data[offset + 3], bios->data[offset + 2],
895  bios->data[offset + 1], bios->data[offset]);
896 }
897 
898 static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
899 {
900  /*
901  * Parses the init table segment for pointers used in script execution.
902  *
903  * offset + 0 (16 bits): init script tables pointer
904  * offset + 2 (16 bits): macro index table pointer
905  * offset + 4 (16 bits): macro table pointer
906  * offset + 6 (16 bits): condition table pointer
907  * offset + 8 (16 bits): io condition table pointer
908  * offset + 10 (16 bits): io flag condition table pointer
909  * offset + 12 (16 bits): init function table pointer
910  */
911 
912  bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
913  bios->macro_index_tbl_ptr = ROM16(bios->data[offset + 2]);
914  bios->macro_tbl_ptr = ROM16(bios->data[offset + 4]);
915  bios->condition_tbl_ptr = ROM16(bios->data[offset + 6]);
916  bios->io_condition_tbl_ptr = ROM16(bios->data[offset + 8]);
917  bios->io_flag_condition_tbl_ptr = ROM16(bios->data[offset + 10]);
918  bios->init_function_tbl_ptr = ROM16(bios->data[offset + 12]);
919 }
920 
921 static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
922 {
923  /*
924  * Parses the load detect values for g80 cards.
925  *
926  * offset + 0 (16 bits): loadval table pointer
927  */
928 
929  struct nouveau_drm *drm = nouveau_drm(dev);
930  uint16_t load_table_ptr;
931  uint8_t version, headerlen, entrylen, num_entries;
932 
933  if (bitentry->length != 3) {
934  NV_ERROR(drm, "Do not understand BIT A table\n");
935  return -EINVAL;
936  }
937 
938  load_table_ptr = ROM16(bios->data[bitentry->offset]);
939 
940  if (load_table_ptr == 0x0) {
941  NV_DEBUG(drm, "Pointer to BIT loadval table invalid\n");
942  return -EINVAL;
943  }
944 
945  version = bios->data[load_table_ptr];
946 
947  if (version != 0x10) {
948  NV_ERROR(drm, "BIT loadval table version %d.%d not supported\n",
949  version >> 4, version & 0xF);
950  return -ENOSYS;
951  }
952 
953  headerlen = bios->data[load_table_ptr + 1];
954  entrylen = bios->data[load_table_ptr + 2];
955  num_entries = bios->data[load_table_ptr + 3];
956 
957  if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
958  NV_ERROR(drm, "Do not understand BIT loadval table\n");
959  return -EINVAL;
960  }
961 
962  /* First entry is normal dac, 2nd tv-out perhaps? */
963  bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
964 
965  return 0;
966 }
967 
968 static int parse_bit_C_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
969 {
970  /*
971  * offset + 8 (16 bits): PLL limits table pointer
972  *
973  * There's more in here, but that's unknown.
974  */
975  struct nouveau_drm *drm = nouveau_drm(dev);
976 
977  if (bitentry->length < 10) {
978  NV_ERROR(drm, "Do not understand BIT C table\n");
979  return -EINVAL;
980  }
981 
982  bios->pll_limit_tbl_ptr = ROM16(bios->data[bitentry->offset + 8]);
983 
984  return 0;
985 }
986 
987 static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
988 {
989  /*
990  * Parses the flat panel table segment that the bit entry points to.
991  * Starting at bitentry->offset:
992  *
993  * offset + 0 (16 bits): ??? table pointer - seems to have 18 byte
994  * records beginning with a freq.
995  * offset + 2 (16 bits): mode table pointer
996  */
997  struct nouveau_drm *drm = nouveau_drm(dev);
998 
999  if (bitentry->length != 4) {
1000  NV_ERROR(drm, "Do not understand BIT display table\n");
1001  return -EINVAL;
1002  }
1003 
1004  bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
1005 
1006  return 0;
1007 }
1008 
1009 static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
1010 {
1011  /*
1012  * Parses the init table segment that the bit entry points to.
1013  *
1014  * See parse_script_table_pointers for layout
1015  */
1016  struct nouveau_drm *drm = nouveau_drm(dev);
1017 
1018  if (bitentry->length < 14) {
1019  NV_ERROR(drm, "Do not understand init table\n");
1020  return -EINVAL;
1021  }
1022 
1023  parse_script_table_pointers(bios, bitentry->offset);
1024 
1025  if (bitentry->length >= 16)
1026  bios->some_script_ptr = ROM16(bios->data[bitentry->offset + 14]);
1027  if (bitentry->length >= 18)
1028  bios->init96_tbl_ptr = ROM16(bios->data[bitentry->offset + 16]);
1029 
1030  return 0;
1031 }
1032 
1033 static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
1034 {
1035  /*
1036  * BIT 'i' (info?) table
1037  *
1038  * offset + 0 (32 bits): BIOS version dword (as in B table)
1039  * offset + 5 (8 bits): BIOS feature byte (same as for BMP?)
1040  * offset + 13 (16 bits): pointer to table containing DAC load
1041  * detection comparison values
1042  *
1043  * There's other things in the table, purpose unknown
1044  */
1045 
1046  struct nouveau_drm *drm = nouveau_drm(dev);
1047  uint16_t daccmpoffset;
1048  uint8_t dacver, dacheaderlen;
1049 
1050  if (bitentry->length < 6) {
1051  NV_ERROR(drm, "BIT i table too short for needed information\n");
1052  return -EINVAL;
1053  }
1054 
1055  parse_bios_version(dev, bios, bitentry->offset);
1056 
1057  /*
1058  * bit 4 seems to indicate a mobile bios (doesn't suffer from BMP's
1059  * Quadro identity crisis), other bits possibly as for BMP feature byte
1060  */
1061  bios->feature_byte = bios->data[bitentry->offset + 5];
1062  bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
1063 
1064  if (bitentry->length < 15) {
1065  NV_WARN(drm, "BIT i table not long enough for DAC load "
1066  "detection comparison table\n");
1067  return -EINVAL;
1068  }
1069 
1070  daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
1071 
1072  /* doesn't exist on g80 */
1073  if (!daccmpoffset)
1074  return 0;
1075 
1076  /*
1077  * The first value in the table, following the header, is the
1078  * comparison value, the second entry is a comparison value for
1079  * TV load detection.
1080  */
1081 
1082  dacver = bios->data[daccmpoffset];
1083  dacheaderlen = bios->data[daccmpoffset + 1];
1084 
1085  if (dacver != 0x00 && dacver != 0x10) {
1086  NV_WARN(drm, "DAC load detection comparison table version "
1087  "%d.%d not known\n", dacver >> 4, dacver & 0xf);
1088  return -ENOSYS;
1089  }
1090 
1091  bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
1092  bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
1093 
1094  return 0;
1095 }
1096 
1097 static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
1098 {
1099  /*
1100  * Parses the LVDS table segment that the bit entry points to.
1101  * Starting at bitentry->offset:
1102  *
1103  * offset + 0 (16 bits): LVDS strap xlate table pointer
1104  */
1105 
1106  struct nouveau_drm *drm = nouveau_drm(dev);
1107 
1108  if (bitentry->length != 2) {
1109  NV_ERROR(drm, "Do not understand BIT LVDS table\n");
1110  return -EINVAL;
1111  }
1112 
1113  /*
1114  * No idea if it's still called the LVDS manufacturer table, but
1115  * the concept's close enough.
1116  */
1117  bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
1118 
1119  return 0;
1120 }
1121 
1122 static int
1123 parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
1124  struct bit_entry *bitentry)
1125 {
1126  /*
1127  * offset + 2 (8 bits): number of options in an
1128  * INIT_RAM_RESTRICT_ZM_REG_GROUP opcode option set
1129  * offset + 3 (16 bits): pointer to strap xlate table for RAM
1130  * restrict option selection
1131  *
1132  * There's a bunch of bits in this table other than the RAM restrict
1133  * stuff that we don't use - their use currently unknown
1134  */
1135 
1136  /*
1137  * Older bios versions don't have a sufficiently long table for
1138  * what we want
1139  */
1140  if (bitentry->length < 0x5)
1141  return 0;
1142 
1143  if (bitentry->version < 2) {
1144  bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
1145  bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
1146  } else {
1147  bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
1148  bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
1149  }
1150 
1151  return 0;
1152 }
1153 
1154 static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
1155 {
1156  /*
1157  * Parses the pointer to the TMDS table
1158  *
1159  * Starting at bitentry->offset:
1160  *
1161  * offset + 0 (16 bits): TMDS table pointer
1162  *
1163  * The TMDS table is typically found just before the DCB table, with a
1164  * characteristic signature of 0x11,0x13 (1.1 being version, 0x13 being
1165  * length?)
1166  *
1167  * At offset +7 is a pointer to a script, which I don't know how to
1168  * run yet.
1169  * At offset +9 is a pointer to another script, likewise
1170  * Offset +11 has a pointer to a table where the first word is a pxclk
1171  * frequency and the second word a pointer to a script, which should be
1172  * run if the comparison pxclk frequency is less than the pxclk desired.
1173  * This repeats for decreasing comparison frequencies
1174  * Offset +13 has a pointer to a similar table
1175  * The selection of table (and possibly +7/+9 script) is dictated by
1176  * "or" from the DCB.
1177  */
1178 
1179  struct nouveau_drm *drm = nouveau_drm(dev);
1180  uint16_t tmdstableptr, script1, script2;
1181 
1182  if (bitentry->length != 2) {
1183  NV_ERROR(drm, "Do not understand BIT TMDS table\n");
1184  return -EINVAL;
1185  }
1186 
1187  tmdstableptr = ROM16(bios->data[bitentry->offset]);
1188  if (!tmdstableptr) {
1189  NV_ERROR(drm, "Pointer to TMDS table invalid\n");
1190  return -EINVAL;
1191  }
1192 
1193  NV_INFO(drm, "TMDS table version %d.%d\n",
1194  bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
1195 
1196  /* nv50+ has v2.0, but we don't parse it atm */
1197  if (bios->data[tmdstableptr] != 0x11)
1198  return -ENOSYS;
1199 
1200  /*
1201  * These two scripts are odd: they don't seem to get run even when
1202  * they are not stubbed.
1203  */
1204  script1 = ROM16(bios->data[tmdstableptr + 7]);
1205  script2 = ROM16(bios->data[tmdstableptr + 9]);
1206  if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
1207  NV_WARN(drm, "TMDS table script pointers not stubbed\n");
1208 
1209  bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
1210  bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
1211 
1212  return 0;
1213 }
1214 
1215 static int
1216 parse_bit_U_tbl_entry(struct drm_device *dev, struct nvbios *bios,
1217  struct bit_entry *bitentry)
1218 {
1219  /*
1220  * Parses the pointer to the G80 output script tables
1221  *
1222  * Starting at bitentry->offset:
1223  *
1224  * offset + 0 (16 bits): output script table pointer
1225  */
1226 
1227  struct nouveau_drm *drm = nouveau_drm(dev);
1228  uint16_t outputscripttableptr;
1229 
1230  if (bitentry->length != 3) {
1231  NV_ERROR(drm, "Do not understand BIT U table\n");
1232  return -EINVAL;
1233  }
1234 
1235  outputscripttableptr = ROM16(bios->data[bitentry->offset]);
1236  bios->display.script_table_ptr = outputscripttableptr;
1237  return 0;
1238 }
1239 
1240 struct bit_table {
1241  const char id;
1242  int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
1243 };
1244 
1245 #define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
1246 
1247 int
1248 bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
1249 {
1250  struct nouveau_drm *drm = nouveau_drm(dev);
1251  struct nvbios *bios = &drm->vbios;
1252  u8 entries, *entry;
1253 
1254  if (bios->type != NVBIOS_BIT)
1255  return -ENODEV;
1256 
1257  entries = bios->data[bios->offset + 10];
1258  entry = &bios->data[bios->offset + 12];
1259  while (entries--) {
1260  if (entry[0] == id) {
1261  bit->id = entry[0];
1262  bit->version = entry[1];
1263  bit->length = ROM16(entry[2]);
1264  bit->offset = ROM16(entry[4]);
1265  bit->data = ROMPTR(dev, entry[4]);
1266  return 0;
1267  }
1268 
1269  entry += bios->data[bios->offset + 9];
1270  }
1271 
1272  return -ENOENT;
1273 }
1274 
1275 static int
1276 parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
1277  struct bit_table *table)
1278 {
1279  struct drm_device *dev = bios->dev;
1280  struct nouveau_drm *drm = nouveau_drm(dev);
1281  struct bit_entry bitentry;
1282 
1283  if (bit_table(dev, table->id, &bitentry) == 0)
1284  return table->parse_fn(dev, bios, &bitentry);
1285 
1286  NV_INFO(drm, "BIT table '%c' not found\n", table->id);
1287  return -ENOSYS;
1288 }
1289 
1290 static int
1291 parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
1292 {
1293  int ret;
1294 
1295  /*
1296  * The only restriction on parsing order currently is having 'i' first
1297  * for use of bios->*_version or bios->feature_byte while parsing;
1298  * functions shouldn't be actually *doing* anything apart from pulling
1299  * data from the image into the bios struct, thus no interdependencies
1300  */
1301  ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
1302  if (ret) /* info? */
1303  return ret;
1304  if (bios->major_version >= 0x60) /* g80+ */
1305  parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
1306  ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('C', C));
1307  if (ret)
1308  return ret;
1309  parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
1310  ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
1311  if (ret)
1312  return ret;
1313  parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M)); /* memory? */
1314  parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
1315  parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
1316  parse_bit_table(bios, bitoffset, &BIT_TABLE('U', U));
1317 
1318  return 0;
1319 }
1320 
1321 static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
1322 {
1323  /*
1324  * Parses the BMP structure for useful things, but does not act on them
1325  *
1326  * offset + 5: BMP major version
1327  * offset + 6: BMP minor version
1328  * offset + 9: BMP feature byte
1329  * offset + 10: BCD encoded BIOS version
1330  *
1331  * offset + 18: init script table pointer (for bios versions < 5.10h)
1332  * offset + 20: extra init script table pointer (for bios
1333  * versions < 5.10h)
1334  *
1335  * offset + 24: memory init table pointer (used on early bios versions)
1336  * offset + 26: SDR memory sequencing setup data table
1337  * offset + 28: DDR memory sequencing setup data table
1338  *
1339  * offset + 54: index of I2C CRTC pair to use for CRT output
1340  * offset + 55: index of I2C CRTC pair to use for TV output
1341  * offset + 56: index of I2C CRTC pair to use for flat panel output
1342  * offset + 58: write CRTC index for I2C pair 0
1343  * offset + 59: read CRTC index for I2C pair 0
1344  * offset + 60: write CRTC index for I2C pair 1
1345  * offset + 61: read CRTC index for I2C pair 1
1346  *
1347  * offset + 67: maximum internal PLL frequency (single stage PLL)
1348  * offset + 71: minimum internal PLL frequency (single stage PLL)
1349  *
1350  * offset + 75: script table pointers, as described in
1351  * parse_script_table_pointers
1352  *
1353  * offset + 89: TMDS single link output A table pointer
1354  * offset + 91: TMDS single link output B table pointer
1355  * offset + 95: LVDS single link output A table pointer
1356  * offset + 105: flat panel timings table pointer
1357  * offset + 107: flat panel strapping translation table pointer
1358  * offset + 117: LVDS manufacturer panel config table pointer
1359  * offset + 119: LVDS manufacturer strapping translation table pointer
1360  *
1361  * offset + 142: PLL limits table pointer
1362  *
1363  * offset + 156: minimum pixel clock for LVDS dual link
1364  */
1365 
1366  struct nouveau_drm *drm = nouveau_drm(dev);
1367  uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
1368  uint16_t bmplength;
1369  uint16_t legacy_scripts_offset, legacy_i2c_offset;
1370 
1371  /* load needed defaults in case we can't parse this info */
1372  bios->digital_min_front_porch = 0x4b;
1373  bios->fmaxvco = 256000;
1374  bios->fminvco = 128000;
1375  bios->fp.duallink_transition_clk = 90000;
1376 
1377  bmp_version_major = bmp[5];
1378  bmp_version_minor = bmp[6];
1379 
1380  NV_INFO(drm, "BMP version %d.%d\n",
1381  bmp_version_major, bmp_version_minor);
1382 
1383  /*
1384  * Make sure that 0x36 is blank and can't be mistaken for a DCB
1385  * pointer on early versions
1386  */
1387  if (bmp_version_major < 5)
1388  *(uint16_t *)&bios->data[0x36] = 0;
1389 
1390  /*
1391  * Seems that the minor version was 1 for all major versions prior
1392  * to 5. Version 6 could theoretically exist, but I suspect BIT
1393  * happened instead.
1394  */
1395  if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
1396  NV_ERROR(drm, "You have an unsupported BMP version. "
1397  "Please send in your bios\n");
1398  return -ENOSYS;
1399  }
1400 
1401  if (bmp_version_major == 0)
1402  /* nothing that's currently useful in this version */
1403  return 0;
1404  else if (bmp_version_major == 1)
1405  bmplength = 44; /* exact for 1.01 */
1406  else if (bmp_version_major == 2)
1407  bmplength = 48; /* exact for 2.01 */
1408  else if (bmp_version_major == 3)
1409  bmplength = 54;
1410  /* guessed - mem init tables added in this version */
1411  else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
1412  /* don't know if 5.0 exists... */
1413  bmplength = 62;
1414  /* guessed - BMP I2C indices added in version 4*/
1415  else if (bmp_version_minor < 0x6)
1416  bmplength = 67; /* exact for 5.01 */
1417  else if (bmp_version_minor < 0x10)
1418  bmplength = 75; /* exact for 5.06 */
1419  else if (bmp_version_minor == 0x10)
1420  bmplength = 89; /* exact for 5.10h */
1421  else if (bmp_version_minor < 0x14)
1422  bmplength = 118; /* exact for 5.11h */
1423  else if (bmp_version_minor < 0x24)
1424  /*
1425  * Not sure of version where pll limits came in;
1426  * certainly exist by 0x24 though.
1427  */
1428  /* length not exact: this is long enough to get lvds members */
1429  bmplength = 123;
1430  else if (bmp_version_minor < 0x27)
1431  /*
1432  * Length not exact: this is long enough to get pll limit
1433  * member
1434  */
1435  bmplength = 144;
1436  else
1437  /*
1438  * Length not exact: this is long enough to get dual link
1439  * transition clock.
1440  */
1441  bmplength = 158;
1442 
1443  /* checksum */
1444  if (nv_cksum(bmp, 8)) {
1445  NV_ERROR(drm, "Bad BMP checksum\n");
1446  return -EINVAL;
1447  }
1448 
1449  /*
1450  * Bit 4 seems to indicate either a mobile bios or a quadro card --
1451  * mobile behaviour consistent (nv11+), quadro only seen nv18gl-nv36gl
1452  * (not nv10gl), bit 5 that the flat panel tables are present, and
1453  * bit 6 a tv bios.
1454  */
1455  bios->feature_byte = bmp[9];
1456 
1457  parse_bios_version(dev, bios, offset + 10);
1458 
1459  if (bmp_version_major < 5 || bmp_version_minor < 0x10)
1460  bios->old_style_init = true;
1461  legacy_scripts_offset = 18;
1462  if (bmp_version_major < 2)
1463  legacy_scripts_offset -= 4;
1464  bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
1465  bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
1466 
1467  if (bmp_version_major > 2) { /* appears in BMP 3 */
1468  bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
1469  bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
1470  bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
1471  }
1472 
1473  legacy_i2c_offset = 0x48; /* BMP version 2 & 3 */
1474  if (bmplength > 61)
1475  legacy_i2c_offset = offset + 54;
1476  bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
1477  bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
1478  bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
1479 
1480  if (bmplength > 74) {
1481  bios->fmaxvco = ROM32(bmp[67]);
1482  bios->fminvco = ROM32(bmp[71]);
1483  }
1484  if (bmplength > 88)
1485  parse_script_table_pointers(bios, offset + 75);
1486  if (bmplength > 94) {
1487  bios->tmds.output0_script_ptr = ROM16(bmp[89]);
1488  bios->tmds.output1_script_ptr = ROM16(bmp[91]);
1489  /*
1490  * Never observed in use with lvds scripts, but is reused for
1491  * 18/24 bit panel interface default for EDID equipped panels
1492  * (if_is_24bit not set directly to avoid any oscillation).
1493  */
1494  bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
1495  }
1496  if (bmplength > 108) {
1497  bios->fp.fptablepointer = ROM16(bmp[105]);
1498  bios->fp.fpxlatetableptr = ROM16(bmp[107]);
1499  bios->fp.xlatwidth = 1;
1500  }
1501  if (bmplength > 120) {
1502  bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
1503  bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
1504  }
1505  if (bmplength > 143)
1506  bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
1507 
1508  if (bmplength > 157)
1509  bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
1510 
1511  return 0;
1512 }
1513 
1514 static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
1515 {
1516  int i, j;
1517 
1518  for (i = 0; i <= (n - len); i++) {
1519  for (j = 0; j < len; j++)
1520  if (data[i + j] != str[j])
1521  break;
1522  if (j == len)
1523  return i;
1524  }
1525 
1526  return 0;
1527 }
1528 
1529 void *
1531 {
1532  struct nouveau_drm *drm = nouveau_drm(dev);
1533  u8 *dcb = NULL;
1534 
1535  if (nv_device(drm->device)->card_type > NV_04)
1536  dcb = ROMPTR(dev, drm->vbios.data[0x36]);
1537  if (!dcb) {
1538  NV_WARN(drm, "No DCB data found in VBIOS\n");
1539  return NULL;
1540  }
1541 
1542  if (dcb[0] >= 0x41) {
1543  NV_WARN(drm, "DCB version 0x%02x unknown\n", dcb[0]);
1544  return NULL;
1545  } else
1546  if (dcb[0] >= 0x30) {
1547  if (ROM32(dcb[6]) == 0x4edcbdcb)
1548  return dcb;
1549  } else
1550  if (dcb[0] >= 0x20) {
1551  if (ROM32(dcb[4]) == 0x4edcbdcb)
1552  return dcb;
1553  } else
1554  if (dcb[0] >= 0x15) {
1555  if (!memcmp(&dcb[-7], "DEV_REC", 7))
1556  return dcb;
1557  } else {
1558  /*
1559  * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
1560  * always has the same single (crt) entry, even when tv-out
1561  * present, so the conclusion is this version cannot really
1562  * be used.
1563  *
1564  * v1.2 tables (some NV6/10, and NV15+) normally have the
1565  * same 5 entries, which are not specific to the card and so
1566  * no use.
1567  *
1568  * v1.2 does have an I2C table that read_dcb_i2c_table can
1569  * handle, but cards exist (nv11 in #14821) with a bad i2c
1570  * table pointer, so use the indices parsed in
1571  * parse_bmp_structure.
1572  *
1573  * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
1574  */
1575  NV_WARN(drm, "No useful DCB data in VBIOS\n");
1576  return NULL;
1577  }
1578 
1579  NV_WARN(drm, "DCB header validation failed\n");
1580  return NULL;
1581 }
1582 
1583 void *
1585 {
1586  u8 *dcb = olddcb_table(dev);
1587  if (dcb && dcb[0] >= 0x30) {
1588  if (idx < dcb[2])
1589  return dcb + dcb[1] + (idx * dcb[3]);
1590  } else
1591  if (dcb && dcb[0] >= 0x20) {
1592  u8 *i2c = ROMPTR(dev, dcb[2]);
1593  u8 *ent = dcb + 8 + (idx * 8);
1594  if (i2c && ent < i2c)
1595  return ent;
1596  } else
1597  if (dcb && dcb[0] >= 0x15) {
1598  u8 *i2c = ROMPTR(dev, dcb[2]);
1599  u8 *ent = dcb + 4 + (idx * 10);
1600  if (i2c && ent < i2c)
1601  return ent;
1602  }
1603 
1604  return NULL;
1605 }
1606 
1607 int
1608 olddcb_outp_foreach(struct drm_device *dev, void *data,
1609  int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
1610 {
1611  int ret, idx = -1;
1612  u8 *outp = NULL;
1613  while ((outp = olddcb_outp(dev, ++idx))) {
1614  if (ROM32(outp[0]) == 0x00000000)
1615  break; /* seen on an NV11 with DCB v1.5 */
1616  if (ROM32(outp[0]) == 0xffffffff)
1617  break; /* seen on an NV17 with DCB v2.0 */
1618 
1619  if ((outp[0] & 0x0f) == DCB_OUTPUT_UNUSED)
1620  continue;
1621  if ((outp[0] & 0x0f) == DCB_OUTPUT_EOL)
1622  break;
1623 
1624  ret = exec(dev, data, idx, outp);
1625  if (ret)
1626  return ret;
1627  }
1628 
1629  return 0;
1630 }
1631 
1632 u8 *
1634 {
1635  u8 *dcb = olddcb_table(dev);
1636  if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
1637  u8 *conntab = ROMPTR(dev, dcb[0x14]);
1638  if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
1639  return conntab;
1640  }
1641  return NULL;
1642 }
1643 
1644 u8 *
1646 {
1647  u8 *conntab = olddcb_conntab(dev);
1648  if (conntab && idx < conntab[2])
1649  return conntab + conntab[1] + (idx * conntab[3]);
1650  return NULL;
1651 }
1652 
1653 static struct dcb_output *new_dcb_entry(struct dcb_table *dcb)
1654 {
1655  struct dcb_output *entry = &dcb->entry[dcb->entries];
1656 
1657  memset(entry, 0, sizeof(struct dcb_output));
1658  entry->index = dcb->entries++;
1659 
1660  return entry;
1661 }
1662 
1663 static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
1664  int heads, int or)
1665 {
1666  struct dcb_output *entry = new_dcb_entry(dcb);
1667 
1668  entry->type = type;
1669  entry->i2c_index = i2c;
1670  entry->heads = heads;
1671  if (type != DCB_OUTPUT_ANALOG)
1672  entry->location = !DCB_LOC_ON_CHIP; /* ie OFF CHIP */
1673  entry->or = or;
1674 }
1675 
1676 static bool
1677 parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
1678  uint32_t conn, uint32_t conf, struct dcb_output *entry)
1679 {
1680  struct nouveau_drm *drm = nouveau_drm(dev);
1681 
1682  entry->type = conn & 0xf;
1683  entry->i2c_index = (conn >> 4) & 0xf;
1684  entry->heads = (conn >> 8) & 0xf;
1685  entry->connector = (conn >> 12) & 0xf;
1686  entry->bus = (conn >> 16) & 0xf;
1687  entry->location = (conn >> 20) & 0x3;
1688  entry->or = (conn >> 24) & 0xf;
1689 
1690  switch (entry->type) {
1691  case DCB_OUTPUT_ANALOG:
1692  /*
1693  * Although the rest of a CRT conf dword is usually
1694  * zeros, mac biosen have stuff there so we must mask
1695  */
1696  entry->crtconf.maxfreq = (dcb->version < 0x30) ?
1697  (conf & 0xffff) * 10 :
1698  (conf & 0xff) * 10000;
1699  break;
1700  case DCB_OUTPUT_LVDS:
1701  {
1702  uint32_t mask;
1703  if (conf & 0x1)
1704  entry->lvdsconf.use_straps_for_mode = true;
1705  if (dcb->version < 0x22) {
1706  mask = ~0xd;
1707  /*
1708  * The laptop in bug 14567 lies and claims to not use
1709  * straps when it does, so assume all DCB 2.0 laptops
1710  * use straps, until a broken EDID using one is produced
1711  */
1712  entry->lvdsconf.use_straps_for_mode = true;
1713  /*
1714  * Both 0x4 and 0x8 show up in v2.0 tables; assume they
1715  * mean the same thing (probably wrong, but might work)
1716  */
1717  if (conf & 0x4 || conf & 0x8)
1718  entry->lvdsconf.use_power_scripts = true;
1719  } else {
1720  mask = ~0x7;
1721  if (conf & 0x2)
1722  entry->lvdsconf.use_acpi_for_edid = true;
1723  if (conf & 0x4)
1724  entry->lvdsconf.use_power_scripts = true;
1725  entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
1726  }
1727  if (conf & mask) {
1728  /*
1729  * Until we even try to use these on G8x, it's
1730  * useless reporting unknown bits. They all are.
1731  */
1732  if (dcb->version >= 0x40)
1733  break;
1734 
1735  NV_ERROR(drm, "Unknown LVDS configuration bits, "
1736  "please report\n");
1737  }
1738  break;
1739  }
1740  case DCB_OUTPUT_TV:
1741  {
1742  if (dcb->version >= 0x30)
1743  entry->tvconf.has_component_output = conf & (0x8 << 4);
1744  else
1745  entry->tvconf.has_component_output = false;
1746 
1747  break;
1748  }
1749  case DCB_OUTPUT_DP:
1750  entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
1751  switch ((conf & 0x00e00000) >> 21) {
1752  case 0:
1753  entry->dpconf.link_bw = 162000;
1754  break;
1755  default:
1756  entry->dpconf.link_bw = 270000;
1757  break;
1758  }
1759  switch ((conf & 0x0f000000) >> 24) {
1760  case 0xf:
1761  entry->dpconf.link_nr = 4;
1762  break;
1763  case 0x3:
1764  entry->dpconf.link_nr = 2;
1765  break;
1766  default:
1767  entry->dpconf.link_nr = 1;
1768  break;
1769  }
1770  break;
1771  case DCB_OUTPUT_TMDS:
1772  if (dcb->version >= 0x40)
1773  entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
1774  else if (dcb->version >= 0x30)
1775  entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
1776  else if (dcb->version >= 0x22)
1777  entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
1778 
1779  break;
1780  case DCB_OUTPUT_EOL:
1781  /* weird g80 mobile type that "nv" treats as a terminator */
1782  dcb->entries--;
1783  return false;
1784  default:
1785  break;
1786  }
1787 
1788  if (dcb->version < 0x40) {
1789  /* Normal entries consist of a single bit, but dual link has
1790  * the next most significant bit set too
1791  */
1792  entry->duallink_possible =
1793  ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
1794  } else {
1795  entry->duallink_possible = (entry->sorconf.link == 3);
1796  }
1797 
1798  /* unsure what DCB version introduces this, 3.0? */
1799  if (conf & 0x100000)
1800  entry->i2c_upper_default = true;
1801 
1802  return true;
1803 }
1804 
1805 static bool
1806 parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
1807  uint32_t conn, uint32_t conf, struct dcb_output *entry)
1808 {
1809  struct nouveau_drm *drm = nouveau_drm(dev);
1810 
1811  switch (conn & 0x0000000f) {
1812  case 0:
1813  entry->type = DCB_OUTPUT_ANALOG;
1814  break;
1815  case 1:
1816  entry->type = DCB_OUTPUT_TV;
1817  break;
1818  case 2:
1819  case 4:
1820  if (conn & 0x10)
1821  entry->type = DCB_OUTPUT_LVDS;
1822  else
1823  entry->type = DCB_OUTPUT_TMDS;
1824  break;
1825  case 3:
1826  entry->type = DCB_OUTPUT_LVDS;
1827  break;
1828  default:
1829  NV_ERROR(drm, "Unknown DCB type %d\n", conn & 0x0000000f);
1830  return false;
1831  }
1832 
1833  entry->i2c_index = (conn & 0x0003c000) >> 14;
1834  entry->heads = ((conn & 0x001c0000) >> 18) + 1;
1835  entry->or = entry->heads; /* same as heads, hopefully safe enough */
1836  entry->location = (conn & 0x01e00000) >> 21;
1837  entry->bus = (conn & 0x0e000000) >> 25;
1838  entry->duallink_possible = false;
1839 
1840  switch (entry->type) {
1841  case DCB_OUTPUT_ANALOG:
1842  entry->crtconf.maxfreq = (conf & 0xffff) * 10;
1843  break;
1844  case DCB_OUTPUT_TV:
1845  entry->tvconf.has_component_output = false;
1846  break;
1847  case DCB_OUTPUT_LVDS:
1848  if ((conn & 0x00003f00) >> 8 != 0x10)
1849  entry->lvdsconf.use_straps_for_mode = true;
1850  entry->lvdsconf.use_power_scripts = true;
1851  break;
1852  default:
1853  break;
1854  }
1855 
1856  return true;
1857 }
1858 
1859 static
1860 void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
1861 {
1862  /*
1863  * DCB v2.0 lists each output combination separately.
1864  * Here we merge compatible entries to have fewer outputs, with
1865  * more options
1866  */
1867 
1868  struct nouveau_drm *drm = nouveau_drm(dev);
1869  int i, newentries = 0;
1870 
1871  for (i = 0; i < dcb->entries; i++) {
1872  struct dcb_output *ient = &dcb->entry[i];
1873  int j;
1874 
1875  for (j = i + 1; j < dcb->entries; j++) {
1876  struct dcb_output *jent = &dcb->entry[j];
1877 
1878  if (jent->type == 100) /* already merged entry */
1879  continue;
1880 
1881  /* merge heads field when all other fields the same */
1882  if (jent->i2c_index == ient->i2c_index &&
1883  jent->type == ient->type &&
1884  jent->location == ient->location &&
1885  jent->or == ient->or) {
1886  NV_INFO(drm, "Merging DCB entries %d and %d\n",
1887  i, j);
1888  ient->heads |= jent->heads;
1889  jent->type = 100; /* dummy value */
1890  }
1891  }
1892  }
1893 
1894  /* Compact entries merged into others out of dcb */
1895  for (i = 0; i < dcb->entries; i++) {
1896  if (dcb->entry[i].type == 100)
1897  continue;
1898 
1899  if (newentries != i) {
1900  dcb->entry[newentries] = dcb->entry[i];
1901  dcb->entry[newentries].index = newentries;
1902  }
1903  newentries++;
1904  }
1905 
1906  dcb->entries = newentries;
1907 }
1908 
1909 static bool
1910 apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
1911 {
1912  struct nouveau_drm *drm = nouveau_drm(dev);
1913  struct dcb_table *dcb = &drm->vbios.dcb;
1914 
1915  /* Dell Precision M6300
1916  * DCB entry 2: 02025312 00000010
1917  * DCB entry 3: 02026312 00000020
1918  *
1919  * Identical, except apparently a different connector on a
1920  * different SOR link. Not a clue how we're supposed to know
1921  * which one is in use if it even shares an i2c line...
1922  *
1923  * Ignore the connector on the second SOR link to prevent
1924  * nasty problems until this is sorted (assuming it's not a
1925  * VBIOS bug).
1926  */
1927  if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
1928  if (*conn == 0x02026312 && *conf == 0x00000020)
1929  return false;
1930  }
1931 
1932  /* GeForce3 Ti 200
1933  *
1934  * DCB reports an LVDS output that should be TMDS:
1935  * DCB entry 1: f2005014 ffffffff
1936  */
1937  if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
1938  if (*conn == 0xf2005014 && *conf == 0xffffffff) {
1939  fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 1, 1, 1);
1940  return false;
1941  }
1942  }
1943 
1944  /* XFX GT-240X-YA
1945  *
1946  * So many things wrong here, replace the entire encoder table..
1947  */
1948  if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
1949  if (idx == 0) {
1950  *conn = 0x02001300; /* VGA, connector 1 */
1951  *conf = 0x00000028;
1952  } else
1953  if (idx == 1) {
1954  *conn = 0x01010312; /* DVI, connector 0 */
1955  *conf = 0x00020030;
1956  } else
1957  if (idx == 2) {
1958  *conn = 0x01010310; /* VGA, connector 0 */
1959  *conf = 0x00000028;
1960  } else
1961  if (idx == 3) {
1962  *conn = 0x02022362; /* HDMI, connector 2 */
1963  *conf = 0x00020010;
1964  } else {
1965  *conn = 0x0000000e; /* EOL */
1966  *conf = 0x00000000;
1967  }
1968  }
1969 
1970  /* Some other twisted XFX board (rhbz#694914)
1971  *
1972  * The DVI/VGA encoder combo that's supposed to represent the
1973  * DVI-I connector actually point at two different ones, and
1974  * the HDMI connector ends up paired with the VGA instead.
1975  *
1976  * Connector table is missing anything for VGA at all, pointing it
1977  * an invalid conntab entry 2 so we figure it out ourself.
1978  */
1979  if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
1980  if (idx == 0) {
1981  *conn = 0x02002300; /* VGA, connector 2 */
1982  *conf = 0x00000028;
1983  } else
1984  if (idx == 1) {
1985  *conn = 0x01010312; /* DVI, connector 0 */
1986  *conf = 0x00020030;
1987  } else
1988  if (idx == 2) {
1989  *conn = 0x04020310; /* VGA, connector 0 */
1990  *conf = 0x00000028;
1991  } else
1992  if (idx == 3) {
1993  *conn = 0x02021322; /* HDMI, connector 1 */
1994  *conf = 0x00020010;
1995  } else {
1996  *conn = 0x0000000e; /* EOL */
1997  *conf = 0x00000000;
1998  }
1999  }
2000 
2001  /* fdo#50830: connector indices for VGA and DVI-I are backwards */
2002  if (nv_match_device(dev, 0x0421, 0x3842, 0xc793)) {
2003  if (idx == 0 && *conn == 0x02000300)
2004  *conn = 0x02011300;
2005  else
2006  if (idx == 1 && *conn == 0x04011310)
2007  *conn = 0x04000310;
2008  else
2009  if (idx == 2 && *conn == 0x02011312)
2010  *conn = 0x02000312;
2011  }
2012 
2013  return true;
2014 }
2015 
2016 static void
2017 fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
2018 {
2019  struct dcb_table *dcb = &bios->dcb;
2020  int all_heads = (nv_two_heads(dev) ? 3 : 1);
2021 
2022 #ifdef __powerpc__
2023  /* Apple iMac G4 NV17 */
2024  if (of_machine_is_compatible("PowerMac4,5")) {
2025  fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 0, all_heads, 1);
2026  fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG, 1, all_heads, 2);
2027  return;
2028  }
2029 #endif
2030 
2031  /* Make up some sane defaults */
2032  fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG,
2033  bios->legacy.i2c_indices.crt, 1, 1);
2034 
2035  if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
2036  fabricate_dcb_output(dcb, DCB_OUTPUT_TV,
2037  bios->legacy.i2c_indices.tv,
2038  all_heads, 0);
2039 
2040  else if (bios->tmds.output0_script_ptr ||
2041  bios->tmds.output1_script_ptr)
2042  fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS,
2043  bios->legacy.i2c_indices.panel,
2044  all_heads, 1);
2045 }
2046 
2047 static int
2048 parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
2049 {
2050  struct nouveau_drm *drm = nouveau_drm(dev);
2051  struct dcb_table *dcb = &drm->vbios.dcb;
2052  u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
2053  u32 conn = ROM32(outp[0]);
2054  bool ret;
2055 
2056  if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
2057  struct dcb_output *entry = new_dcb_entry(dcb);
2058 
2059  NV_INFO(drm, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
2060 
2061  if (dcb->version >= 0x20)
2062  ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
2063  else
2064  ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
2065  if (!ret)
2066  return 1; /* stop parsing */
2067 
2068  /* Ignore the I2C index for on-chip TV-out, as there
2069  * are cards with bogus values (nv31m in bug 23212),
2070  * and it's otherwise useless.
2071  */
2072  if (entry->type == DCB_OUTPUT_TV &&
2073  entry->location == DCB_LOC_ON_CHIP)
2074  entry->i2c_index = 0x0f;
2075  }
2076 
2077  return 0;
2078 }
2079 
2080 static void
2081 dcb_fake_connectors(struct nvbios *bios)
2082 {
2083  struct dcb_table *dcbt = &bios->dcb;
2084  u8 map[16] = { };
2085  int i, idx = 0;
2086 
2087  /* heuristic: if we ever get a non-zero connector field, assume
2088  * that all the indices are valid and we don't need fake them.
2089  *
2090  * and, as usual, a blacklist of boards with bad bios data..
2091  */
2092  if (!nv_match_device(bios->dev, 0x0392, 0x107d, 0x20a2)) {
2093  for (i = 0; i < dcbt->entries; i++) {
2094  if (dcbt->entry[i].connector)
2095  return;
2096  }
2097  }
2098 
2099  /* no useful connector info available, we need to make it up
2100  * ourselves. the rule here is: anything on the same i2c bus
2101  * is considered to be on the same connector. any output
2102  * without an associated i2c bus is assigned its own unique
2103  * connector index.
2104  */
2105  for (i = 0; i < dcbt->entries; i++) {
2106  u8 i2c = dcbt->entry[i].i2c_index;
2107  if (i2c == 0x0f) {
2108  dcbt->entry[i].connector = idx++;
2109  } else {
2110  if (!map[i2c])
2111  map[i2c] = ++idx;
2112  dcbt->entry[i].connector = map[i2c] - 1;
2113  }
2114  }
2115 
2116  /* if we created more than one connector, destroy the connector
2117  * table - just in case it has random, rather than stub, entries.
2118  */
2119  if (i > 1) {
2120  u8 *conntab = olddcb_conntab(bios->dev);
2121  if (conntab)
2122  conntab[0] = 0x00;
2123  }
2124 }
2125 
2126 static int
2127 parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
2128 {
2129  struct nouveau_drm *drm = nouveau_drm(dev);
2130  struct dcb_table *dcb = &bios->dcb;
2131  u8 *dcbt, *conn;
2132  int idx;
2133 
2134  dcbt = olddcb_table(dev);
2135  if (!dcbt) {
2136  /* handle pre-DCB boards */
2137  if (bios->type == NVBIOS_BMP) {
2138  fabricate_dcb_encoder_table(dev, bios);
2139  return 0;
2140  }
2141 
2142  return -EINVAL;
2143  }
2144 
2145  NV_INFO(drm, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
2146 
2147  dcb->version = dcbt[0];
2148  olddcb_outp_foreach(dev, NULL, parse_dcb_entry);
2149 
2150  /*
2151  * apart for v2.1+ not being known for requiring merging, this
2152  * guarantees dcbent->index is the index of the entry in the rom image
2153  */
2154  if (dcb->version < 0x21)
2155  merge_like_dcb_entries(dev, dcb);
2156 
2157  if (!dcb->entries)
2158  return -ENXIO;
2159 
2160  /* dump connector table entries to log, if any exist */
2161  idx = -1;
2162  while ((conn = olddcb_conn(dev, ++idx))) {
2163  if (conn[0] != 0xff) {
2164  NV_INFO(drm, "DCB conn %02d: ", idx);
2165  if (olddcb_conntab(dev)[3] < 4)
2166  printk("%04x\n", ROM16(conn[0]));
2167  else
2168  printk("%08x\n", ROM32(conn[0]));
2169  }
2170  }
2171  dcb_fake_connectors(bios);
2172  return 0;
2173 }
2174 
2175 static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
2176 {
2177  /*
2178  * The header following the "HWSQ" signature has the number of entries,
2179  * and the entry size
2180  *
2181  * An entry consists of a dword to write to the sequencer control reg
2182  * (0x00001304), followed by the ucode bytes, written sequentially,
2183  * starting at reg 0x00001400
2184  */
2185 
2186  struct nouveau_drm *drm = nouveau_drm(dev);
2187  struct nouveau_device *device = nv_device(drm->device);
2188  uint8_t bytes_to_write;
2189  uint16_t hwsq_entry_offset;
2190  int i;
2191 
2192  if (bios->data[hwsq_offset] <= entry) {
2193  NV_ERROR(drm, "Too few entries in HW sequencer table for "
2194  "requested entry\n");
2195  return -ENOENT;
2196  }
2197 
2198  bytes_to_write = bios->data[hwsq_offset + 1];
2199 
2200  if (bytes_to_write != 36) {
2201  NV_ERROR(drm, "Unknown HW sequencer entry size\n");
2202  return -EINVAL;
2203  }
2204 
2205  NV_INFO(drm, "Loading NV17 power sequencing microcode\n");
2206 
2207  hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
2208 
2209  /* set sequencer control */
2210  nv_wr32(device, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
2211  bytes_to_write -= 4;
2212 
2213  /* write ucode */
2214  for (i = 0; i < bytes_to_write; i += 4)
2215  nv_wr32(device, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
2216 
2217  /* twiddle NV_PBUS_DEBUG_4 */
2218  nv_wr32(device, NV_PBUS_DEBUG_4, nv_rd32(device, NV_PBUS_DEBUG_4) | 0x18);
2219 
2220  return 0;
2221 }
2222 
2223 static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
2224  struct nvbios *bios)
2225 {
2226  /*
2227  * BMP based cards, from NV17, need a microcode loading to correctly
2228  * control the GPIO etc for LVDS panels
2229  *
2230  * BIT based cards seem to do this directly in the init scripts
2231  *
2232  * The microcode entries are found by the "HWSQ" signature.
2233  */
2234 
2235  const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
2236  const int sz = sizeof(hwsq_signature);
2237  int hwsq_offset;
2238 
2239  hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
2240  if (!hwsq_offset)
2241  return 0;
2242 
2243  /* always use entry 0? */
2244  return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
2245 }
2246 
2248 {
2249  struct nouveau_drm *drm = nouveau_drm(dev);
2250  struct nvbios *bios = &drm->vbios;
2251  const uint8_t edid_sig[] = {
2252  0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
2253  uint16_t offset = 0;
2254  uint16_t newoffset;
2255  int searchlen = NV_PROM_SIZE;
2256 
2257  if (bios->fp.edid)
2258  return bios->fp.edid;
2259 
2260  while (searchlen) {
2261  newoffset = findstr(&bios->data[offset], searchlen,
2262  edid_sig, 8);
2263  if (!newoffset)
2264  return NULL;
2265  offset += newoffset;
2266  if (!nv_cksum(&bios->data[offset], EDID1_LEN))
2267  break;
2268 
2269  searchlen -= offset;
2270  offset++;
2271  }
2272 
2273  NV_INFO(drm, "Found EDID in BIOS\n");
2274 
2275  return bios->fp.edid = &bios->data[offset];
2276 }
2277 
2278 static bool NVInitVBIOS(struct drm_device *dev)
2279 {
2280  struct nouveau_drm *drm = nouveau_drm(dev);
2281  struct nvbios *bios = &drm->vbios;
2282 
2283  memset(bios, 0, sizeof(struct nvbios));
2284  spin_lock_init(&bios->lock);
2285  bios->dev = dev;
2286 
2287  bios->data = nouveau_bios(drm->device)->data;
2288  bios->length = nouveau_bios(drm->device)->size;
2289  return true;
2290 }
2291 
2292 static int nouveau_parse_vbios_struct(struct drm_device *dev)
2293 {
2294  struct nouveau_drm *drm = nouveau_drm(dev);
2295  struct nvbios *bios = &drm->vbios;
2296  const uint8_t bit_signature[] = { 0xff, 0xb8, 'B', 'I', 'T' };
2297  const uint8_t bmp_signature[] = { 0xff, 0x7f, 'N', 'V', 0x0 };
2298  int offset;
2299 
2300  offset = findstr(bios->data, bios->length,
2301  bit_signature, sizeof(bit_signature));
2302  if (offset) {
2303  NV_INFO(drm, "BIT BIOS found\n");
2304  bios->type = NVBIOS_BIT;
2305  bios->offset = offset;
2306  return parse_bit_structure(bios, offset + 6);
2307  }
2308 
2309  offset = findstr(bios->data, bios->length,
2310  bmp_signature, sizeof(bmp_signature));
2311  if (offset) {
2312  NV_INFO(drm, "BMP BIOS found\n");
2313  bios->type = NVBIOS_BMP;
2314  bios->offset = offset;
2315  return parse_bmp_structure(dev, bios, offset);
2316  }
2317 
2318  NV_ERROR(drm, "No known BIOS signature found\n");
2319  return -ENODEV;
2320 }
2321 
2322 int
2324 {
2325  struct nouveau_drm *drm = nouveau_drm(dev);
2326  struct nvbios *bios = &drm->vbios;
2327  int i, ret = 0;
2328 
2329  /* Reset the BIOS head to 0. */
2330  bios->state.crtchead = 0;
2331 
2332  if (bios->major_version < 5) /* BMP only */
2333  load_nv17_hw_sequencer_ucode(dev, bios);
2334 
2335  if (bios->execute) {
2336  bios->fp.last_script_invoc = 0;
2337  bios->fp.lvds_init_run = false;
2338  }
2339 
2340  if (nv_device(drm->device)->card_type >= NV_50) {
2341  for (i = 0; bios->execute && i < bios->dcb.entries; i++) {
2343  &bios->dcb.entry[i], -1);
2344  }
2345  }
2346 
2347  return ret;
2348 }
2349 
2350 static bool
2351 nouveau_bios_posted(struct drm_device *dev)
2352 {
2353  struct nouveau_drm *drm = nouveau_drm(dev);
2354  unsigned htotal;
2355 
2356  if (nv_device(drm->device)->card_type >= NV_50) {
2357  if (NVReadVgaCrtc(dev, 0, 0x00) == 0 &&
2358  NVReadVgaCrtc(dev, 0, 0x1a) == 0)
2359  return false;
2360  return true;
2361  }
2362 
2363  htotal = NVReadVgaCrtc(dev, 0, 0x06);
2364  htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
2365  htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
2366  htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
2367  htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
2368 
2369  return (htotal != 0);
2370 }
2371 
2372 int
2374 {
2375  struct nouveau_drm *drm = nouveau_drm(dev);
2376  struct nvbios *bios = &drm->vbios;
2377  int ret;
2378 
2379  if (!NVInitVBIOS(dev))
2380  return -ENODEV;
2381 
2382  ret = nouveau_parse_vbios_struct(dev);
2383  if (ret)
2384  return ret;
2385 
2386  ret = parse_dcb_table(dev, bios);
2387  if (ret)
2388  return ret;
2389 
2390  if (!bios->major_version) /* we don't run version 0 bios */
2391  return 0;
2392 
2393  /* init script execution disabled */
2394  bios->execute = false;
2395 
2396  /* ... unless card isn't POSTed already */
2397  if (!nouveau_bios_posted(dev)) {
2398  NV_INFO(drm, "Adaptor not initialised, "
2399  "running VBIOS init tables.\n");
2400  bios->execute = true;
2401  }
2402 
2403  ret = nouveau_run_vbios_init(dev);
2404  if (ret)
2405  return ret;
2406 
2407  /* feature_byte on BMP is poor, but init always sets CR4B */
2408  if (bios->major_version < 5)
2409  bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
2410 
2411  /* all BIT systems need p_f_m_t for digital_min_front_porch */
2412  if (bios->is_mobile || bios->major_version >= 5)
2413  ret = parse_fp_mode_table(dev, bios);
2414 
2415  /* allow subsequent scripts to execute */
2416  bios->execute = true;
2417 
2418  return 0;
2419 }
2420 
2421 void
2423 {
2424 }