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