1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <drm/drmP.h>
26
27#include "nouveau_drv.h"
28#include "nouveau_reg.h"
29#include "dispnv04/hw.h"
30#include "nouveau_encoder.h"
31
32#include <linux/io-mapping.h>
33#include <linux/firmware.h>
34
35
36#define NV_CIO_CRE_44_HEADA 0x0
37#define NV_CIO_CRE_44_HEADB 0x3
38#define FEATURE_MOBILE 0x10
39
40#define EDID1_LEN 128
41
42#define BIOSLOG(sip, fmt, arg...) NV_DEBUG(sip->dev, fmt, ##arg)
43#define LOG_OLD_VALUE(x)
44
45struct init_exec {
46 bool execute;
47 bool repeat;
48};
49
50static bool nv_cksum(const uint8_t *data, unsigned int length)
51{
52
53
54
55
56 int i;
57 uint8_t sum = 0;
58
59 for (i = 0; i < length; i++)
60 sum += data[i];
61
62 if (sum)
63 return true;
64
65 return false;
66}
67
68static uint16_t clkcmptable(struct nvbios *bios, uint16_t clktable, int pxclk)
69{
70 int compare_record_len, i = 0;
71 uint16_t compareclk, scriptptr = 0;
72
73 if (bios->major_version < 5)
74 compare_record_len = 3;
75 else
76 compare_record_len = 4;
77
78 do {
79 compareclk = ROM16(bios->data[clktable + compare_record_len * i]);
80 if (pxclk >= compareclk * 10) {
81 if (bios->major_version < 5) {
82 uint8_t tmdssub = bios->data[clktable + 2 + compare_record_len * i];
83 scriptptr = ROM16(bios->data[bios->init_script_tbls_ptr + tmdssub * 2]);
84 } else
85 scriptptr = ROM16(bios->data[clktable + 2 + compare_record_len * i]);
86 break;
87 }
88 i++;
89 } while (compareclk);
90
91 return scriptptr;
92}
93
94static void
95run_digital_op_script(struct drm_device *dev, uint16_t scriptptr,
96 struct dcb_output *dcbent, int head, bool dl)
97{
98 struct nouveau_drm *drm = nouveau_drm(dev);
99
100 NV_INFO(drm, "0x%04X: Parsing digital output script table\n",
101 scriptptr);
102 NVWriteVgaCrtc(dev, 0, NV_CIO_CRE_44, head ? NV_CIO_CRE_44_HEADB :
103 NV_CIO_CRE_44_HEADA);
104 nouveau_bios_run_init_table(dev, scriptptr, dcbent, head);
105
106 nv04_dfp_bind_head(dev, dcbent, head, dl);
107}
108
109static int call_lvds_manufacturer_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script)
110{
111 struct nouveau_drm *drm = nouveau_drm(dev);
112 struct nvbios *bios = &drm->vbios;
113 uint8_t sub = bios->data[bios->fp.xlated_entry + script] + (bios->fp.link_c_increment && dcbent->or & DCB_OUTPUT_C ? 1 : 0);
114 uint16_t scriptofs = ROM16(bios->data[bios->init_script_tbls_ptr + sub * 2]);
115
116 if (!bios->fp.xlated_entry || !sub || !scriptofs)
117 return -EINVAL;
118
119 run_digital_op_script(dev, scriptofs, dcbent, head, bios->fp.dual_link);
120
121 if (script == LVDS_PANEL_OFF) {
122
123 mdelay(ROM16(bios->data[bios->fp.xlated_entry + 7]));
124 }
125#ifdef __powerpc__
126
127 if (script == LVDS_RESET &&
128 (dev->pdev->device == 0x0179 || dev->pdev->device == 0x0189 ||
129 dev->pdev->device == 0x0329))
130 nv_write_tmds(dev, dcbent->or, 0, 0x02, 0x72);
131#endif
132
133 return 0;
134}
135
136static int run_lvds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
137{
138
139
140
141
142
143
144
145
146
147
148 struct nouveau_drm *drm = nouveau_drm(dev);
149 struct nvbios *bios = &drm->vbios;
150 unsigned int outputset = (dcbent->or == 4) ? 1 : 0;
151 uint16_t scriptptr = 0, clktable;
152
153
154
155
156
157
158 switch (script) {
159 case LVDS_INIT:
160 return -ENOSYS;
161 case LVDS_BACKLIGHT_ON:
162 case LVDS_PANEL_ON:
163 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 7 + outputset * 2]);
164 break;
165 case LVDS_BACKLIGHT_OFF:
166 case LVDS_PANEL_OFF:
167 scriptptr = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 11 + outputset * 2]);
168 break;
169 case LVDS_RESET:
170 clktable = bios->fp.lvdsmanufacturerpointer + 15;
171 if (dcbent->or == 4)
172 clktable += 8;
173
174 if (dcbent->lvdsconf.use_straps_for_mode) {
175 if (bios->fp.dual_link)
176 clktable += 4;
177 if (bios->fp.if_is_24bit)
178 clktable += 2;
179 } else {
180
181 int cmpval_24bit = (dcbent->or == 4) ? 4 : 1;
182
183 if (bios->fp.dual_link) {
184 clktable += 4;
185 cmpval_24bit <<= 1;
186 }
187
188 if (bios->fp.strapless_is_24bit & cmpval_24bit)
189 clktable += 2;
190 }
191
192 clktable = ROM16(bios->data[clktable]);
193 if (!clktable) {
194 NV_ERROR(drm, "Pixel clock comparison table not found\n");
195 return -ENOENT;
196 }
197 scriptptr = clkcmptable(bios, clktable, pxclk);
198 }
199
200 if (!scriptptr) {
201 NV_ERROR(drm, "LVDS output init script not found\n");
202 return -ENOENT;
203 }
204 run_digital_op_script(dev, scriptptr, dcbent, head, bios->fp.dual_link);
205
206 return 0;
207}
208
209int call_lvds_script(struct drm_device *dev, struct dcb_output *dcbent, int head, enum LVDS_script script, int pxclk)
210{
211
212
213
214
215
216
217 struct nouveau_drm *drm = nouveau_drm(dev);
218 struct nvif_object *device = &drm->client.device.object;
219 struct nvbios *bios = &drm->vbios;
220 uint8_t lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
221 uint32_t sel_clk_binding, sel_clk;
222 int ret;
223
224 if (bios->fp.last_script_invoc == (script << 1 | head) || !lvds_ver ||
225 (lvds_ver >= 0x30 && script == LVDS_INIT))
226 return 0;
227
228 if (!bios->fp.lvds_init_run) {
229 bios->fp.lvds_init_run = true;
230 call_lvds_script(dev, dcbent, head, LVDS_INIT, pxclk);
231 }
232
233 if (script == LVDS_PANEL_ON && bios->fp.reset_after_pclk_change)
234 call_lvds_script(dev, dcbent, head, LVDS_RESET, pxclk);
235 if (script == LVDS_RESET && bios->fp.power_off_for_reset)
236 call_lvds_script(dev, dcbent, head, LVDS_PANEL_OFF, pxclk);
237
238 NV_INFO(drm, "Calling LVDS script %d:\n", script);
239
240
241 sel_clk_binding = nvif_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
242
243 if (lvds_ver < 0x30)
244 ret = call_lvds_manufacturer_script(dev, dcbent, head, script);
245 else
246 ret = run_lvds_table(dev, dcbent, head, script, pxclk);
247
248 bios->fp.last_script_invoc = (script << 1 | head);
249
250 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
251 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
252
253 nvif_wr32(device, NV_PBUS_POWERCTRL_2, 0);
254
255 return ret;
256}
257
258struct lvdstableheader {
259 uint8_t lvds_ver, headerlen, recordlen;
260};
261
262static int parse_lvds_manufacturer_table_header(struct drm_device *dev, struct nvbios *bios, struct lvdstableheader *lth)
263{
264
265
266
267
268
269
270
271 struct nouveau_drm *drm = nouveau_drm(dev);
272 uint8_t lvds_ver, headerlen, recordlen;
273
274 memset(lth, 0, sizeof(struct lvdstableheader));
275
276 if (bios->fp.lvdsmanufacturerpointer == 0x0) {
277 NV_ERROR(drm, "Pointer to LVDS manufacturer table invalid\n");
278 return -EINVAL;
279 }
280
281 lvds_ver = bios->data[bios->fp.lvdsmanufacturerpointer];
282
283 switch (lvds_ver) {
284 case 0x0a:
285 headerlen = 2;
286 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
287 break;
288 case 0x30:
289 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
290 if (headerlen < 0x1f) {
291 NV_ERROR(drm, "LVDS table header not understood\n");
292 return -EINVAL;
293 }
294 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
295 break;
296 case 0x40:
297 headerlen = bios->data[bios->fp.lvdsmanufacturerpointer + 1];
298 if (headerlen < 0x7) {
299 NV_ERROR(drm, "LVDS table header not understood\n");
300 return -EINVAL;
301 }
302 recordlen = bios->data[bios->fp.lvdsmanufacturerpointer + 2];
303 break;
304 default:
305 NV_ERROR(drm,
306 "LVDS table revision %d.%d not currently supported\n",
307 lvds_ver >> 4, lvds_ver & 0xf);
308 return -ENOSYS;
309 }
310
311 lth->lvds_ver = lvds_ver;
312 lth->headerlen = headerlen;
313 lth->recordlen = recordlen;
314
315 return 0;
316}
317
318static int
319get_fp_strap(struct drm_device *dev, struct nvbios *bios)
320{
321 struct nouveau_drm *drm = nouveau_drm(dev);
322 struct nvif_object *device = &drm->client.device.object;
323
324
325
326
327
328
329
330
331
332
333 if (bios->major_version < 5 && bios->data[0x48] & 0x4)
334 return NVReadVgaCrtc5758(dev, 0, 0xf) & 0xf;
335
336 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_MAXWELL)
337 return nvif_rd32(device, 0x001800) & 0x0000000f;
338 else
339 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
340 return (nvif_rd32(device, NV_PEXTDEV_BOOT_0) >> 24) & 0xf;
341 else
342 return (nvif_rd32(device, NV_PEXTDEV_BOOT_0) >> 16) & 0xf;
343}
344
345static int parse_fp_mode_table(struct drm_device *dev, struct nvbios *bios)
346{
347 struct nouveau_drm *drm = nouveau_drm(dev);
348 uint8_t *fptable;
349 uint8_t fptable_ver, headerlen = 0, recordlen, fpentries = 0xf, fpindex;
350 int ret, ofs, fpstrapping;
351 struct lvdstableheader lth;
352
353 if (bios->fp.fptablepointer == 0x0) {
354
355 NV_DEBUG(drm, "Pointer to flat panel table invalid\n");
356 bios->digital_min_front_porch = 0x4b;
357 return 0;
358 }
359
360 fptable = &bios->data[bios->fp.fptablepointer];
361 fptable_ver = fptable[0];
362
363 switch (fptable_ver) {
364
365
366
367
368
369
370
371
372
373 case 0x05:
374 recordlen = 42;
375 ofs = -1;
376 break;
377 case 0x10:
378 recordlen = 44;
379 ofs = 0;
380 break;
381 case 0x20:
382 headerlen = fptable[1];
383 recordlen = fptable[2];
384 fpentries = fptable[3];
385
386
387
388
389 bios->digital_min_front_porch = fptable[4];
390 ofs = -7;
391 break;
392 default:
393 NV_ERROR(drm,
394 "FP table revision %d.%d not currently supported\n",
395 fptable_ver >> 4, fptable_ver & 0xf);
396 return -ENOSYS;
397 }
398
399 if (!bios->is_mobile)
400 return 0;
401
402 ret = parse_lvds_manufacturer_table_header(dev, bios, <h);
403 if (ret)
404 return ret;
405
406 if (lth.lvds_ver == 0x30 || lth.lvds_ver == 0x40) {
407 bios->fp.fpxlatetableptr = bios->fp.lvdsmanufacturerpointer +
408 lth.headerlen + 1;
409 bios->fp.xlatwidth = lth.recordlen;
410 }
411 if (bios->fp.fpxlatetableptr == 0x0) {
412 NV_ERROR(drm, "Pointer to flat panel xlat table invalid\n");
413 return -EINVAL;
414 }
415
416 fpstrapping = get_fp_strap(dev, bios);
417
418 fpindex = bios->data[bios->fp.fpxlatetableptr +
419 fpstrapping * bios->fp.xlatwidth];
420
421 if (fpindex > fpentries) {
422 NV_ERROR(drm, "Bad flat panel table index\n");
423 return -ENOENT;
424 }
425
426
427 if (lth.lvds_ver > 0x10)
428 bios->fp_no_ddc = fpstrapping != 0xf || fpindex != 0xf;
429
430
431
432
433
434
435 if (fpstrapping == 0xf || fpindex == 0xf)
436 return 0;
437
438 bios->fp.mode_ptr = bios->fp.fptablepointer + headerlen +
439 recordlen * fpindex + ofs;
440
441 NV_INFO(drm, "BIOS FP mode: %dx%d (%dkHz pixel clock)\n",
442 ROM16(bios->data[bios->fp.mode_ptr + 11]) + 1,
443 ROM16(bios->data[bios->fp.mode_ptr + 25]) + 1,
444 ROM16(bios->data[bios->fp.mode_ptr + 7]) * 10);
445
446 return 0;
447}
448
449bool nouveau_bios_fp_mode(struct drm_device *dev, struct drm_display_mode *mode)
450{
451 struct nouveau_drm *drm = nouveau_drm(dev);
452 struct nvbios *bios = &drm->vbios;
453 uint8_t *mode_entry = &bios->data[bios->fp.mode_ptr];
454
455 if (!mode)
456 return bios->fp.mode_ptr;
457
458 memset(mode, 0, sizeof(struct drm_display_mode));
459
460
461
462
463
464
465 mode->clock = ROM16(mode_entry[7]) * 10;
466
467 mode->hdisplay = ROM16(mode_entry[11]) + 1;
468
469
470
471
472 mode->hsync_start = ROM16(mode_entry[17]) + 1;
473 mode->hsync_end = ROM16(mode_entry[19]) + 1;
474 mode->htotal = ROM16(mode_entry[21]) + 1;
475
476 mode->vdisplay = ROM16(mode_entry[25]) + 1;
477 mode->vsync_start = ROM16(mode_entry[31]) + 1;
478 mode->vsync_end = ROM16(mode_entry[33]) + 1;
479 mode->vtotal = ROM16(mode_entry[35]) + 1;
480 mode->flags |= (mode_entry[37] & 0x10) ?
481 DRM_MODE_FLAG_PHSYNC : DRM_MODE_FLAG_NHSYNC;
482 mode->flags |= (mode_entry[37] & 0x1) ?
483 DRM_MODE_FLAG_PVSYNC : DRM_MODE_FLAG_NVSYNC;
484
485
486
487
488
489 mode->status = MODE_OK;
490 mode->type = DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED;
491 drm_mode_set_name(mode);
492 return bios->fp.mode_ptr;
493}
494
495int nouveau_bios_parse_lvds_table(struct drm_device *dev, int pxclk, bool *dl, bool *if_is_24bit)
496{
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522 struct nouveau_drm *drm = nouveau_drm(dev);
523 struct nvbios *bios = &drm->vbios;
524 int fpstrapping = get_fp_strap(dev, bios), lvdsmanufacturerindex = 0;
525 struct lvdstableheader lth;
526 uint16_t lvdsofs;
527 int ret, chip_version = bios->chip_version;
528
529 ret = parse_lvds_manufacturer_table_header(dev, bios, <h);
530 if (ret)
531 return ret;
532
533 switch (lth.lvds_ver) {
534 case 0x0a:
535 lvdsmanufacturerindex = bios->data[
536 bios->fp.fpxlatemanufacturertableptr +
537 fpstrapping];
538
539
540 if (!pxclk)
541 break;
542
543 if (chip_version < 0x25) {
544
545
546
547
548
549 lvdsmanufacturerindex =
550 (bios->legacy.lvds_single_a_script_ptr & 1) ?
551 2 : 0;
552 if (pxclk >= bios->fp.duallink_transition_clk)
553 lvdsmanufacturerindex++;
554 } else if (chip_version < 0x30) {
555
556
557
558
559
560
561
562
563
564 lvdsmanufacturerindex = 0;
565 } else {
566
567 lvdsmanufacturerindex = 0;
568 if (pxclk >= bios->fp.duallink_transition_clk)
569 lvdsmanufacturerindex = 2;
570 if (pxclk >= 140000)
571 lvdsmanufacturerindex = 3;
572 }
573
574
575
576
577
578 break;
579 case 0x30:
580 case 0x40:
581 lvdsmanufacturerindex = fpstrapping;
582 break;
583 default:
584 NV_ERROR(drm, "LVDS table revision not currently supported\n");
585 return -ENOSYS;
586 }
587
588 lvdsofs = bios->fp.xlated_entry = bios->fp.lvdsmanufacturerpointer + lth.headerlen + lth.recordlen * lvdsmanufacturerindex;
589 switch (lth.lvds_ver) {
590 case 0x0a:
591 bios->fp.power_off_for_reset = bios->data[lvdsofs] & 1;
592 bios->fp.reset_after_pclk_change = bios->data[lvdsofs] & 2;
593 bios->fp.dual_link = bios->data[lvdsofs] & 4;
594 bios->fp.link_c_increment = bios->data[lvdsofs] & 8;
595 *if_is_24bit = bios->data[lvdsofs] & 16;
596 break;
597 case 0x30:
598 case 0x40:
599
600
601
602
603 bios->fp.power_off_for_reset = true;
604 bios->fp.reset_after_pclk_change = true;
605
606
607
608
609
610 bios->fp.dual_link = bios->data[lvdsofs] & 1;
611 bios->fp.if_is_24bit = bios->data[lvdsofs] & 2;
612 bios->fp.strapless_is_24bit = bios->data[bios->fp.lvdsmanufacturerpointer + 4];
613 bios->fp.duallink_transition_clk = ROM16(bios->data[bios->fp.lvdsmanufacturerpointer + 5]) * 10;
614 break;
615 }
616
617
618 if (pxclk && (chip_version < 0x25 || chip_version > 0x28))
619 bios->fp.dual_link = (pxclk >= bios->fp.duallink_transition_clk);
620
621 *dl = bios->fp.dual_link;
622
623 return 0;
624}
625
626int run_tmds_table(struct drm_device *dev, struct dcb_output *dcbent, int head, int pxclk)
627{
628
629
630
631
632
633
634
635
636
637 struct nouveau_drm *drm = nouveau_drm(dev);
638 struct nvif_object *device = &drm->client.device.object;
639 struct nvbios *bios = &drm->vbios;
640 int cv = bios->chip_version;
641 uint16_t clktable = 0, scriptptr;
642 uint32_t sel_clk_binding, sel_clk;
643
644
645 if (cv >= 0x17 && cv != 0x1a && cv != 0x20 &&
646 dcbent->location != DCB_LOC_ON_CHIP)
647 return 0;
648
649 switch (ffs(dcbent->or)) {
650 case 1:
651 clktable = bios->tmds.output0_script_ptr;
652 break;
653 case 2:
654 case 3:
655 clktable = bios->tmds.output1_script_ptr;
656 break;
657 }
658
659 if (!clktable) {
660 NV_ERROR(drm, "Pixel clock comparison table not found\n");
661 return -EINVAL;
662 }
663
664 scriptptr = clkcmptable(bios, clktable, pxclk);
665
666 if (!scriptptr) {
667 NV_ERROR(drm, "TMDS output init script not found\n");
668 return -ENOENT;
669 }
670
671
672 sel_clk_binding = nvif_rd32(device, NV_PRAMDAC_SEL_CLK) & 0x50000;
673 run_digital_op_script(dev, scriptptr, dcbent, head, pxclk >= 165000);
674 sel_clk = NVReadRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK) & ~0x50000;
675 NVWriteRAMDAC(dev, 0, NV_PRAMDAC_SEL_CLK, sel_clk | sel_clk_binding);
676
677 return 0;
678}
679
680static void parse_script_table_pointers(struct nvbios *bios, uint16_t offset)
681{
682
683
684
685
686
687
688
689
690
691
692
693
694 bios->init_script_tbls_ptr = ROM16(bios->data[offset]);
695}
696
697static int parse_bit_A_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
698{
699
700
701
702
703
704
705 struct nouveau_drm *drm = nouveau_drm(dev);
706 uint16_t load_table_ptr;
707 uint8_t version, headerlen, entrylen, num_entries;
708
709 if (bitentry->length != 3) {
710 NV_ERROR(drm, "Do not understand BIT A table\n");
711 return -EINVAL;
712 }
713
714 load_table_ptr = ROM16(bios->data[bitentry->offset]);
715
716 if (load_table_ptr == 0x0) {
717 NV_DEBUG(drm, "Pointer to BIT loadval table invalid\n");
718 return -EINVAL;
719 }
720
721 version = bios->data[load_table_ptr];
722
723 if (version != 0x10) {
724 NV_ERROR(drm, "BIT loadval table version %d.%d not supported\n",
725 version >> 4, version & 0xF);
726 return -ENOSYS;
727 }
728
729 headerlen = bios->data[load_table_ptr + 1];
730 entrylen = bios->data[load_table_ptr + 2];
731 num_entries = bios->data[load_table_ptr + 3];
732
733 if (headerlen != 4 || entrylen != 4 || num_entries != 2) {
734 NV_ERROR(drm, "Do not understand BIT loadval table\n");
735 return -EINVAL;
736 }
737
738
739 bios->dactestval = ROM32(bios->data[load_table_ptr + headerlen]) & 0x3ff;
740
741 return 0;
742}
743
744static int parse_bit_display_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
745{
746
747
748
749
750
751
752
753
754 struct nouveau_drm *drm = nouveau_drm(dev);
755
756 if (bitentry->length != 4) {
757 NV_ERROR(drm, "Do not understand BIT display table\n");
758 return -EINVAL;
759 }
760
761 bios->fp.fptablepointer = ROM16(bios->data[bitentry->offset + 2]);
762
763 return 0;
764}
765
766static int parse_bit_init_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
767{
768
769
770
771
772
773 struct nouveau_drm *drm = nouveau_drm(dev);
774
775 if (bitentry->length < 14) {
776 NV_ERROR(drm, "Do not understand init table\n");
777 return -EINVAL;
778 }
779
780 parse_script_table_pointers(bios, bitentry->offset);
781 return 0;
782}
783
784static int parse_bit_i_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
785{
786
787
788
789
790
791
792
793
794
795
796
797 struct nouveau_drm *drm = nouveau_drm(dev);
798 uint16_t daccmpoffset;
799 uint8_t dacver, dacheaderlen;
800
801 if (bitentry->length < 6) {
802 NV_ERROR(drm, "BIT i table too short for needed information\n");
803 return -EINVAL;
804 }
805
806
807
808
809
810 bios->feature_byte = bios->data[bitentry->offset + 5];
811 bios->is_mobile = bios->feature_byte & FEATURE_MOBILE;
812
813 if (bitentry->length < 15) {
814 NV_WARN(drm, "BIT i table not long enough for DAC load "
815 "detection comparison table\n");
816 return -EINVAL;
817 }
818
819 daccmpoffset = ROM16(bios->data[bitentry->offset + 13]);
820
821
822 if (!daccmpoffset)
823 return 0;
824
825
826
827
828
829
830
831 dacver = bios->data[daccmpoffset];
832 dacheaderlen = bios->data[daccmpoffset + 1];
833
834 if (dacver != 0x00 && dacver != 0x10) {
835 NV_WARN(drm, "DAC load detection comparison table version "
836 "%d.%d not known\n", dacver >> 4, dacver & 0xf);
837 return -ENOSYS;
838 }
839
840 bios->dactestval = ROM32(bios->data[daccmpoffset + dacheaderlen]);
841 bios->tvdactestval = ROM32(bios->data[daccmpoffset + dacheaderlen + 4]);
842
843 return 0;
844}
845
846static int parse_bit_lvds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
847{
848
849
850
851
852
853
854
855 struct nouveau_drm *drm = nouveau_drm(dev);
856
857 if (bitentry->length != 2) {
858 NV_ERROR(drm, "Do not understand BIT LVDS table\n");
859 return -EINVAL;
860 }
861
862
863
864
865
866 bios->fp.lvdsmanufacturerpointer = ROM16(bios->data[bitentry->offset]);
867
868 return 0;
869}
870
871static int
872parse_bit_M_tbl_entry(struct drm_device *dev, struct nvbios *bios,
873 struct bit_entry *bitentry)
874{
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889 if (bitentry->length < 0x5)
890 return 0;
891
892 if (bitentry->version < 2) {
893 bios->ram_restrict_group_count = bios->data[bitentry->offset + 2];
894 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 3]);
895 } else {
896 bios->ram_restrict_group_count = bios->data[bitentry->offset + 0];
897 bios->ram_restrict_tbl_ptr = ROM16(bios->data[bitentry->offset + 1]);
898 }
899
900 return 0;
901}
902
903static int parse_bit_tmds_tbl_entry(struct drm_device *dev, struct nvbios *bios, struct bit_entry *bitentry)
904{
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928 struct nouveau_drm *drm = nouveau_drm(dev);
929 uint16_t tmdstableptr, script1, script2;
930
931 if (bitentry->length != 2) {
932 NV_ERROR(drm, "Do not understand BIT TMDS table\n");
933 return -EINVAL;
934 }
935
936 tmdstableptr = ROM16(bios->data[bitentry->offset]);
937 if (!tmdstableptr) {
938 NV_ERROR(drm, "Pointer to TMDS table invalid\n");
939 return -EINVAL;
940 }
941
942 NV_INFO(drm, "TMDS table version %d.%d\n",
943 bios->data[tmdstableptr] >> 4, bios->data[tmdstableptr] & 0xf);
944
945
946 if (bios->data[tmdstableptr] != 0x11)
947 return -ENOSYS;
948
949
950
951
952
953 script1 = ROM16(bios->data[tmdstableptr + 7]);
954 script2 = ROM16(bios->data[tmdstableptr + 9]);
955 if (bios->data[script1] != 'q' || bios->data[script2] != 'q')
956 NV_WARN(drm, "TMDS table script pointers not stubbed\n");
957
958 bios->tmds.output0_script_ptr = ROM16(bios->data[tmdstableptr + 11]);
959 bios->tmds.output1_script_ptr = ROM16(bios->data[tmdstableptr + 13]);
960
961 return 0;
962}
963
964struct bit_table {
965 const char id;
966 int (* const parse_fn)(struct drm_device *, struct nvbios *, struct bit_entry *);
967};
968
969#define BIT_TABLE(id, funcid) ((struct bit_table){ id, parse_bit_##funcid##_tbl_entry })
970
971int
972bit_table(struct drm_device *dev, u8 id, struct bit_entry *bit)
973{
974 struct nouveau_drm *drm = nouveau_drm(dev);
975 struct nvbios *bios = &drm->vbios;
976 u8 entries, *entry;
977
978 if (bios->type != NVBIOS_BIT)
979 return -ENODEV;
980
981 entries = bios->data[bios->offset + 10];
982 entry = &bios->data[bios->offset + 12];
983 while (entries--) {
984 if (entry[0] == id) {
985 bit->id = entry[0];
986 bit->version = entry[1];
987 bit->length = ROM16(entry[2]);
988 bit->offset = ROM16(entry[4]);
989 bit->data = ROMPTR(dev, entry[4]);
990 return 0;
991 }
992
993 entry += bios->data[bios->offset + 9];
994 }
995
996 return -ENOENT;
997}
998
999static int
1000parse_bit_table(struct nvbios *bios, const uint16_t bitoffset,
1001 struct bit_table *table)
1002{
1003 struct drm_device *dev = bios->dev;
1004 struct nouveau_drm *drm = nouveau_drm(dev);
1005 struct bit_entry bitentry;
1006
1007 if (bit_table(dev, table->id, &bitentry) == 0)
1008 return table->parse_fn(dev, bios, &bitentry);
1009
1010 NV_INFO(drm, "BIT table '%c' not found\n", table->id);
1011 return -ENOSYS;
1012}
1013
1014static int
1015parse_bit_structure(struct nvbios *bios, const uint16_t bitoffset)
1016{
1017 int ret;
1018
1019
1020
1021
1022
1023
1024
1025 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('i', i));
1026 if (ret)
1027 return ret;
1028 if (bios->major_version >= 0x60)
1029 parse_bit_table(bios, bitoffset, &BIT_TABLE('A', A));
1030 parse_bit_table(bios, bitoffset, &BIT_TABLE('D', display));
1031 ret = parse_bit_table(bios, bitoffset, &BIT_TABLE('I', init));
1032 if (ret)
1033 return ret;
1034 parse_bit_table(bios, bitoffset, &BIT_TABLE('M', M));
1035 parse_bit_table(bios, bitoffset, &BIT_TABLE('L', lvds));
1036 parse_bit_table(bios, bitoffset, &BIT_TABLE('T', tmds));
1037
1038 return 0;
1039}
1040
1041static int parse_bmp_structure(struct drm_device *dev, struct nvbios *bios, unsigned int offset)
1042{
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086 struct nouveau_drm *drm = nouveau_drm(dev);
1087 uint8_t *bmp = &bios->data[offset], bmp_version_major, bmp_version_minor;
1088 uint16_t bmplength;
1089 uint16_t legacy_scripts_offset, legacy_i2c_offset;
1090
1091
1092 bios->digital_min_front_porch = 0x4b;
1093 bios->fmaxvco = 256000;
1094 bios->fminvco = 128000;
1095 bios->fp.duallink_transition_clk = 90000;
1096
1097 bmp_version_major = bmp[5];
1098 bmp_version_minor = bmp[6];
1099
1100 NV_INFO(drm, "BMP version %d.%d\n",
1101 bmp_version_major, bmp_version_minor);
1102
1103
1104
1105
1106
1107 if (bmp_version_major < 5)
1108 *(uint16_t *)&bios->data[0x36] = 0;
1109
1110
1111
1112
1113
1114
1115 if ((bmp_version_major < 5 && bmp_version_minor != 1) || bmp_version_major > 5) {
1116 NV_ERROR(drm, "You have an unsupported BMP version. "
1117 "Please send in your bios\n");
1118 return -ENOSYS;
1119 }
1120
1121 if (bmp_version_major == 0)
1122
1123 return 0;
1124 else if (bmp_version_major == 1)
1125 bmplength = 44;
1126 else if (bmp_version_major == 2)
1127 bmplength = 48;
1128 else if (bmp_version_major == 3)
1129 bmplength = 54;
1130
1131 else if (bmp_version_major == 4 || bmp_version_minor < 0x1)
1132
1133 bmplength = 62;
1134
1135 else if (bmp_version_minor < 0x6)
1136 bmplength = 67;
1137 else if (bmp_version_minor < 0x10)
1138 bmplength = 75;
1139 else if (bmp_version_minor == 0x10)
1140 bmplength = 89;
1141 else if (bmp_version_minor < 0x14)
1142 bmplength = 118;
1143 else if (bmp_version_minor < 0x24)
1144
1145
1146
1147
1148
1149 bmplength = 123;
1150 else if (bmp_version_minor < 0x27)
1151
1152
1153
1154
1155 bmplength = 144;
1156 else
1157
1158
1159
1160
1161 bmplength = 158;
1162
1163
1164 if (nv_cksum(bmp, 8)) {
1165 NV_ERROR(drm, "Bad BMP checksum\n");
1166 return -EINVAL;
1167 }
1168
1169
1170
1171
1172
1173
1174
1175 bios->feature_byte = bmp[9];
1176
1177 if (bmp_version_major < 5 || bmp_version_minor < 0x10)
1178 bios->old_style_init = true;
1179 legacy_scripts_offset = 18;
1180 if (bmp_version_major < 2)
1181 legacy_scripts_offset -= 4;
1182 bios->init_script_tbls_ptr = ROM16(bmp[legacy_scripts_offset]);
1183 bios->extra_init_script_tbl_ptr = ROM16(bmp[legacy_scripts_offset + 2]);
1184
1185 if (bmp_version_major > 2) {
1186 bios->legacy.mem_init_tbl_ptr = ROM16(bmp[24]);
1187 bios->legacy.sdr_seq_tbl_ptr = ROM16(bmp[26]);
1188 bios->legacy.ddr_seq_tbl_ptr = ROM16(bmp[28]);
1189 }
1190
1191 legacy_i2c_offset = 0x48;
1192 if (bmplength > 61)
1193 legacy_i2c_offset = offset + 54;
1194 bios->legacy.i2c_indices.crt = bios->data[legacy_i2c_offset];
1195 bios->legacy.i2c_indices.tv = bios->data[legacy_i2c_offset + 1];
1196 bios->legacy.i2c_indices.panel = bios->data[legacy_i2c_offset + 2];
1197
1198 if (bmplength > 74) {
1199 bios->fmaxvco = ROM32(bmp[67]);
1200 bios->fminvco = ROM32(bmp[71]);
1201 }
1202 if (bmplength > 88)
1203 parse_script_table_pointers(bios, offset + 75);
1204 if (bmplength > 94) {
1205 bios->tmds.output0_script_ptr = ROM16(bmp[89]);
1206 bios->tmds.output1_script_ptr = ROM16(bmp[91]);
1207
1208
1209
1210
1211
1212 bios->legacy.lvds_single_a_script_ptr = ROM16(bmp[95]);
1213 }
1214 if (bmplength > 108) {
1215 bios->fp.fptablepointer = ROM16(bmp[105]);
1216 bios->fp.fpxlatetableptr = ROM16(bmp[107]);
1217 bios->fp.xlatwidth = 1;
1218 }
1219 if (bmplength > 120) {
1220 bios->fp.lvdsmanufacturerpointer = ROM16(bmp[117]);
1221 bios->fp.fpxlatemanufacturertableptr = ROM16(bmp[119]);
1222 }
1223#if 0
1224 if (bmplength > 143)
1225 bios->pll_limit_tbl_ptr = ROM16(bmp[142]);
1226#endif
1227
1228 if (bmplength > 157)
1229 bios->fp.duallink_transition_clk = ROM16(bmp[156]) * 10;
1230
1231 return 0;
1232}
1233
1234static uint16_t findstr(uint8_t *data, int n, const uint8_t *str, int len)
1235{
1236 int i, j;
1237
1238 for (i = 0; i <= (n - len); i++) {
1239 for (j = 0; j < len; j++)
1240 if (data[i + j] != str[j])
1241 break;
1242 if (j == len)
1243 return i;
1244 }
1245
1246 return 0;
1247}
1248
1249void *
1250olddcb_table(struct drm_device *dev)
1251{
1252 struct nouveau_drm *drm = nouveau_drm(dev);
1253 u8 *dcb = NULL;
1254
1255 if (drm->client.device.info.family > NV_DEVICE_INFO_V0_TNT)
1256 dcb = ROMPTR(dev, drm->vbios.data[0x36]);
1257 if (!dcb) {
1258 NV_WARN(drm, "No DCB data found in VBIOS\n");
1259 return NULL;
1260 }
1261
1262 if (dcb[0] >= 0x42) {
1263 NV_WARN(drm, "DCB version 0x%02x unknown\n", dcb[0]);
1264 return NULL;
1265 } else
1266 if (dcb[0] >= 0x30) {
1267 if (ROM32(dcb[6]) == 0x4edcbdcb)
1268 return dcb;
1269 } else
1270 if (dcb[0] >= 0x20) {
1271 if (ROM32(dcb[4]) == 0x4edcbdcb)
1272 return dcb;
1273 } else
1274 if (dcb[0] >= 0x15) {
1275 if (!memcmp(&dcb[-7], "DEV_REC", 7))
1276 return dcb;
1277 } else {
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295 NV_WARN(drm, "No useful DCB data in VBIOS\n");
1296 return NULL;
1297 }
1298
1299 NV_WARN(drm, "DCB header validation failed\n");
1300 return NULL;
1301}
1302
1303void *
1304olddcb_outp(struct drm_device *dev, u8 idx)
1305{
1306 u8 *dcb = olddcb_table(dev);
1307 if (dcb && dcb[0] >= 0x30) {
1308 if (idx < dcb[2])
1309 return dcb + dcb[1] + (idx * dcb[3]);
1310 } else
1311 if (dcb && dcb[0] >= 0x20) {
1312 u8 *i2c = ROMPTR(dev, dcb[2]);
1313 u8 *ent = dcb + 8 + (idx * 8);
1314 if (i2c && ent < i2c)
1315 return ent;
1316 } else
1317 if (dcb && dcb[0] >= 0x15) {
1318 u8 *i2c = ROMPTR(dev, dcb[2]);
1319 u8 *ent = dcb + 4 + (idx * 10);
1320 if (i2c && ent < i2c)
1321 return ent;
1322 }
1323
1324 return NULL;
1325}
1326
1327int
1328olddcb_outp_foreach(struct drm_device *dev, void *data,
1329 int (*exec)(struct drm_device *, void *, int idx, u8 *outp))
1330{
1331 int ret, idx = -1;
1332 u8 *outp = NULL;
1333 while ((outp = olddcb_outp(dev, ++idx))) {
1334 if (ROM32(outp[0]) == 0x00000000)
1335 break;
1336 if (ROM32(outp[0]) == 0xffffffff)
1337 break;
1338
1339 if ((outp[0] & 0x0f) == DCB_OUTPUT_UNUSED)
1340 continue;
1341 if ((outp[0] & 0x0f) == DCB_OUTPUT_EOL)
1342 break;
1343
1344 ret = exec(dev, data, idx, outp);
1345 if (ret)
1346 return ret;
1347 }
1348
1349 return 0;
1350}
1351
1352u8 *
1353olddcb_conntab(struct drm_device *dev)
1354{
1355 u8 *dcb = olddcb_table(dev);
1356 if (dcb && dcb[0] >= 0x30 && dcb[1] >= 0x16) {
1357 u8 *conntab = ROMPTR(dev, dcb[0x14]);
1358 if (conntab && conntab[0] >= 0x30 && conntab[0] <= 0x40)
1359 return conntab;
1360 }
1361 return NULL;
1362}
1363
1364u8 *
1365olddcb_conn(struct drm_device *dev, u8 idx)
1366{
1367 u8 *conntab = olddcb_conntab(dev);
1368 if (conntab && idx < conntab[2])
1369 return conntab + conntab[1] + (idx * conntab[3]);
1370 return NULL;
1371}
1372
1373static struct dcb_output *new_dcb_entry(struct dcb_table *dcb)
1374{
1375 struct dcb_output *entry = &dcb->entry[dcb->entries];
1376
1377 memset(entry, 0, sizeof(struct dcb_output));
1378 entry->index = dcb->entries++;
1379
1380 return entry;
1381}
1382
1383static void fabricate_dcb_output(struct dcb_table *dcb, int type, int i2c,
1384 int heads, int or)
1385{
1386 struct dcb_output *entry = new_dcb_entry(dcb);
1387
1388 entry->type = type;
1389 entry->i2c_index = i2c;
1390 entry->heads = heads;
1391 if (type != DCB_OUTPUT_ANALOG)
1392 entry->location = !DCB_LOC_ON_CHIP;
1393 entry->or = or;
1394}
1395
1396static bool
1397parse_dcb20_entry(struct drm_device *dev, struct dcb_table *dcb,
1398 uint32_t conn, uint32_t conf, struct dcb_output *entry)
1399{
1400 struct nouveau_drm *drm = nouveau_drm(dev);
1401 int link = 0;
1402
1403 entry->type = conn & 0xf;
1404 entry->i2c_index = (conn >> 4) & 0xf;
1405 entry->heads = (conn >> 8) & 0xf;
1406 entry->connector = (conn >> 12) & 0xf;
1407 entry->bus = (conn >> 16) & 0xf;
1408 entry->location = (conn >> 20) & 0x3;
1409 entry->or = (conn >> 24) & 0xf;
1410
1411 switch (entry->type) {
1412 case DCB_OUTPUT_ANALOG:
1413
1414
1415
1416
1417 entry->crtconf.maxfreq = (dcb->version < 0x30) ?
1418 (conf & 0xffff) * 10 :
1419 (conf & 0xff) * 10000;
1420 break;
1421 case DCB_OUTPUT_LVDS:
1422 {
1423 uint32_t mask;
1424 if (conf & 0x1)
1425 entry->lvdsconf.use_straps_for_mode = true;
1426 if (dcb->version < 0x22) {
1427 mask = ~0xd;
1428
1429
1430
1431
1432
1433 entry->lvdsconf.use_straps_for_mode = true;
1434
1435
1436
1437
1438 if (conf & 0x4 || conf & 0x8)
1439 entry->lvdsconf.use_power_scripts = true;
1440 } else {
1441 mask = ~0x7;
1442 if (conf & 0x2)
1443 entry->lvdsconf.use_acpi_for_edid = true;
1444 if (conf & 0x4)
1445 entry->lvdsconf.use_power_scripts = true;
1446 entry->lvdsconf.sor.link = (conf & 0x00000030) >> 4;
1447 link = entry->lvdsconf.sor.link;
1448 }
1449 if (conf & mask) {
1450
1451
1452
1453
1454 if (dcb->version >= 0x40)
1455 break;
1456
1457 NV_ERROR(drm, "Unknown LVDS configuration bits, "
1458 "please report\n");
1459 }
1460 break;
1461 }
1462 case DCB_OUTPUT_TV:
1463 {
1464 if (dcb->version >= 0x30)
1465 entry->tvconf.has_component_output = conf & (0x8 << 4);
1466 else
1467 entry->tvconf.has_component_output = false;
1468
1469 break;
1470 }
1471 case DCB_OUTPUT_DP:
1472 entry->dpconf.sor.link = (conf & 0x00000030) >> 4;
1473 entry->extdev = (conf & 0x0000ff00) >> 8;
1474 switch ((conf & 0x00e00000) >> 21) {
1475 case 0:
1476 entry->dpconf.link_bw = 162000;
1477 break;
1478 case 1:
1479 entry->dpconf.link_bw = 270000;
1480 break;
1481 case 2:
1482 entry->dpconf.link_bw = 540000;
1483 break;
1484 case 3:
1485 default:
1486 entry->dpconf.link_bw = 810000;
1487 break;
1488 }
1489 switch ((conf & 0x0f000000) >> 24) {
1490 case 0xf:
1491 case 0x4:
1492 entry->dpconf.link_nr = 4;
1493 break;
1494 case 0x3:
1495 case 0x2:
1496 entry->dpconf.link_nr = 2;
1497 break;
1498 default:
1499 entry->dpconf.link_nr = 1;
1500 break;
1501 }
1502 link = entry->dpconf.sor.link;
1503 break;
1504 case DCB_OUTPUT_TMDS:
1505 if (dcb->version >= 0x40) {
1506 entry->tmdsconf.sor.link = (conf & 0x00000030) >> 4;
1507 entry->extdev = (conf & 0x0000ff00) >> 8;
1508 link = entry->tmdsconf.sor.link;
1509 }
1510 else if (dcb->version >= 0x30)
1511 entry->tmdsconf.slave_addr = (conf & 0x00000700) >> 8;
1512 else if (dcb->version >= 0x22)
1513 entry->tmdsconf.slave_addr = (conf & 0x00000070) >> 4;
1514 break;
1515 case DCB_OUTPUT_EOL:
1516
1517 dcb->entries--;
1518 return false;
1519 default:
1520 break;
1521 }
1522
1523 if (dcb->version < 0x40) {
1524
1525
1526
1527 entry->duallink_possible =
1528 ((1 << (ffs(entry->or) - 1)) * 3 == entry->or);
1529 } else {
1530 entry->duallink_possible = (entry->sorconf.link == 3);
1531 }
1532
1533
1534 if (conf & 0x100000)
1535 entry->i2c_upper_default = true;
1536
1537 entry->hasht = (entry->extdev << 8) | (entry->location << 4) |
1538 entry->type;
1539 entry->hashm = (entry->heads << 8) | (link << 6) | entry->or;
1540 return true;
1541}
1542
1543static bool
1544parse_dcb15_entry(struct drm_device *dev, struct dcb_table *dcb,
1545 uint32_t conn, uint32_t conf, struct dcb_output *entry)
1546{
1547 struct nouveau_drm *drm = nouveau_drm(dev);
1548
1549 switch (conn & 0x0000000f) {
1550 case 0:
1551 entry->type = DCB_OUTPUT_ANALOG;
1552 break;
1553 case 1:
1554 entry->type = DCB_OUTPUT_TV;
1555 break;
1556 case 2:
1557 case 4:
1558 if (conn & 0x10)
1559 entry->type = DCB_OUTPUT_LVDS;
1560 else
1561 entry->type = DCB_OUTPUT_TMDS;
1562 break;
1563 case 3:
1564 entry->type = DCB_OUTPUT_LVDS;
1565 break;
1566 default:
1567 NV_ERROR(drm, "Unknown DCB type %d\n", conn & 0x0000000f);
1568 return false;
1569 }
1570
1571 entry->i2c_index = (conn & 0x0003c000) >> 14;
1572 entry->heads = ((conn & 0x001c0000) >> 18) + 1;
1573 entry->or = entry->heads;
1574 entry->location = (conn & 0x01e00000) >> 21;
1575 entry->bus = (conn & 0x0e000000) >> 25;
1576 entry->duallink_possible = false;
1577
1578 switch (entry->type) {
1579 case DCB_OUTPUT_ANALOG:
1580 entry->crtconf.maxfreq = (conf & 0xffff) * 10;
1581 break;
1582 case DCB_OUTPUT_TV:
1583 entry->tvconf.has_component_output = false;
1584 break;
1585 case DCB_OUTPUT_LVDS:
1586 if ((conn & 0x00003f00) >> 8 != 0x10)
1587 entry->lvdsconf.use_straps_for_mode = true;
1588 entry->lvdsconf.use_power_scripts = true;
1589 break;
1590 default:
1591 break;
1592 }
1593
1594 return true;
1595}
1596
1597static
1598void merge_like_dcb_entries(struct drm_device *dev, struct dcb_table *dcb)
1599{
1600
1601
1602
1603
1604
1605
1606 struct nouveau_drm *drm = nouveau_drm(dev);
1607 int i, newentries = 0;
1608
1609 for (i = 0; i < dcb->entries; i++) {
1610 struct dcb_output *ient = &dcb->entry[i];
1611 int j;
1612
1613 for (j = i + 1; j < dcb->entries; j++) {
1614 struct dcb_output *jent = &dcb->entry[j];
1615
1616 if (jent->type == 100)
1617 continue;
1618
1619
1620 if (jent->i2c_index == ient->i2c_index &&
1621 jent->type == ient->type &&
1622 jent->location == ient->location &&
1623 jent->or == ient->or) {
1624 NV_INFO(drm, "Merging DCB entries %d and %d\n",
1625 i, j);
1626 ient->heads |= jent->heads;
1627 jent->type = 100;
1628 }
1629 }
1630 }
1631
1632
1633 for (i = 0; i < dcb->entries; i++) {
1634 if (dcb->entry[i].type == 100)
1635 continue;
1636
1637 if (newentries != i) {
1638 dcb->entry[newentries] = dcb->entry[i];
1639 dcb->entry[newentries].index = newentries;
1640 }
1641 newentries++;
1642 }
1643
1644 dcb->entries = newentries;
1645}
1646
1647static bool
1648apply_dcb_encoder_quirks(struct drm_device *dev, int idx, u32 *conn, u32 *conf)
1649{
1650 struct nouveau_drm *drm = nouveau_drm(dev);
1651 struct dcb_table *dcb = &drm->vbios.dcb;
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665 if (nv_match_device(dev, 0x040d, 0x1028, 0x019b)) {
1666 if (*conn == 0x02026312 && *conf == 0x00000020)
1667 return false;
1668 }
1669
1670
1671
1672
1673
1674
1675 if (nv_match_device(dev, 0x0201, 0x1462, 0x8851)) {
1676 if (*conn == 0xf2005014 && *conf == 0xffffffff) {
1677 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 1, 1, 1);
1678 return false;
1679 }
1680 }
1681
1682
1683
1684
1685
1686 if (nv_match_device(dev, 0x0ca3, 0x1682, 0x3003)) {
1687 if (idx == 0) {
1688 *conn = 0x02001300;
1689 *conf = 0x00000028;
1690 } else
1691 if (idx == 1) {
1692 *conn = 0x01010312;
1693 *conf = 0x00020030;
1694 } else
1695 if (idx == 2) {
1696 *conn = 0x01010310;
1697 *conf = 0x00000028;
1698 } else
1699 if (idx == 3) {
1700 *conn = 0x02022362;
1701 *conf = 0x00020010;
1702 } else {
1703 *conn = 0x0000000e;
1704 *conf = 0x00000000;
1705 }
1706 }
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717 if (nv_match_device(dev, 0x0615, 0x1682, 0x2605)) {
1718 if (idx == 0) {
1719 *conn = 0x02002300;
1720 *conf = 0x00000028;
1721 } else
1722 if (idx == 1) {
1723 *conn = 0x01010312;
1724 *conf = 0x00020030;
1725 } else
1726 if (idx == 2) {
1727 *conn = 0x04020310;
1728 *conf = 0x00000028;
1729 } else
1730 if (idx == 3) {
1731 *conn = 0x02021322;
1732 *conf = 0x00020010;
1733 } else {
1734 *conn = 0x0000000e;
1735 *conf = 0x00000000;
1736 }
1737 }
1738
1739
1740 if (nv_match_device(dev, 0x0421, 0x3842, 0xc793)) {
1741 if (idx == 0 && *conn == 0x02000300)
1742 *conn = 0x02011300;
1743 else
1744 if (idx == 1 && *conn == 0x04011310)
1745 *conn = 0x04000310;
1746 else
1747 if (idx == 2 && *conn == 0x02011312)
1748 *conn = 0x02000312;
1749 }
1750
1751 return true;
1752}
1753
1754static void
1755fabricate_dcb_encoder_table(struct drm_device *dev, struct nvbios *bios)
1756{
1757 struct dcb_table *dcb = &bios->dcb;
1758 int all_heads = (nv_two_heads(dev) ? 3 : 1);
1759
1760#ifdef __powerpc__
1761
1762 if (of_machine_is_compatible("PowerMac4,5")) {
1763 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS, 0, all_heads, 1);
1764 fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG, 1, all_heads, 2);
1765 return;
1766 }
1767#endif
1768
1769
1770 fabricate_dcb_output(dcb, DCB_OUTPUT_ANALOG,
1771 bios->legacy.i2c_indices.crt, 1, 1);
1772
1773 if (nv04_tv_identify(dev, bios->legacy.i2c_indices.tv) >= 0)
1774 fabricate_dcb_output(dcb, DCB_OUTPUT_TV,
1775 bios->legacy.i2c_indices.tv,
1776 all_heads, 0);
1777
1778 else if (bios->tmds.output0_script_ptr ||
1779 bios->tmds.output1_script_ptr)
1780 fabricate_dcb_output(dcb, DCB_OUTPUT_TMDS,
1781 bios->legacy.i2c_indices.panel,
1782 all_heads, 1);
1783}
1784
1785static int
1786parse_dcb_entry(struct drm_device *dev, void *data, int idx, u8 *outp)
1787{
1788 struct nouveau_drm *drm = nouveau_drm(dev);
1789 struct dcb_table *dcb = &drm->vbios.dcb;
1790 u32 conf = (dcb->version >= 0x20) ? ROM32(outp[4]) : ROM32(outp[6]);
1791 u32 conn = ROM32(outp[0]);
1792 bool ret;
1793
1794 if (apply_dcb_encoder_quirks(dev, idx, &conn, &conf)) {
1795 struct dcb_output *entry = new_dcb_entry(dcb);
1796
1797 NV_INFO(drm, "DCB outp %02d: %08x %08x\n", idx, conn, conf);
1798
1799 if (dcb->version >= 0x20)
1800 ret = parse_dcb20_entry(dev, dcb, conn, conf, entry);
1801 else
1802 ret = parse_dcb15_entry(dev, dcb, conn, conf, entry);
1803 if (!ret)
1804 return 1;
1805
1806
1807
1808
1809
1810 if (entry->type == DCB_OUTPUT_TV &&
1811 entry->location == DCB_LOC_ON_CHIP)
1812 entry->i2c_index = 0x0f;
1813 }
1814
1815 return 0;
1816}
1817
1818static void
1819dcb_fake_connectors(struct nvbios *bios)
1820{
1821 struct dcb_table *dcbt = &bios->dcb;
1822 u8 map[16] = { };
1823 int i, idx = 0;
1824
1825
1826
1827
1828
1829
1830 if (!nv_match_device(bios->dev, 0x0392, 0x107d, 0x20a2)) {
1831 for (i = 0; i < dcbt->entries; i++) {
1832 if (dcbt->entry[i].connector)
1833 return;
1834 }
1835 }
1836
1837
1838
1839
1840
1841
1842
1843 for (i = 0; i < dcbt->entries; i++) {
1844 u8 i2c = dcbt->entry[i].i2c_index;
1845 if (i2c == 0x0f) {
1846 dcbt->entry[i].connector = idx++;
1847 } else {
1848 if (!map[i2c])
1849 map[i2c] = ++idx;
1850 dcbt->entry[i].connector = map[i2c] - 1;
1851 }
1852 }
1853
1854
1855
1856
1857 if (i > 1) {
1858 u8 *conntab = olddcb_conntab(bios->dev);
1859 if (conntab)
1860 conntab[0] = 0x00;
1861 }
1862}
1863
1864static int
1865parse_dcb_table(struct drm_device *dev, struct nvbios *bios)
1866{
1867 struct nouveau_drm *drm = nouveau_drm(dev);
1868 struct dcb_table *dcb = &bios->dcb;
1869 u8 *dcbt, *conn;
1870 int idx;
1871
1872 dcbt = olddcb_table(dev);
1873 if (!dcbt) {
1874
1875 if (bios->type == NVBIOS_BMP) {
1876 fabricate_dcb_encoder_table(dev, bios);
1877 return 0;
1878 }
1879
1880 return -EINVAL;
1881 }
1882
1883 NV_INFO(drm, "DCB version %d.%d\n", dcbt[0] >> 4, dcbt[0] & 0xf);
1884
1885 dcb->version = dcbt[0];
1886 olddcb_outp_foreach(dev, NULL, parse_dcb_entry);
1887
1888
1889
1890
1891
1892 if (dcb->version < 0x21)
1893 merge_like_dcb_entries(dev, dcb);
1894
1895
1896 idx = -1;
1897 while ((conn = olddcb_conn(dev, ++idx))) {
1898 if (conn[0] != 0xff) {
1899 if (olddcb_conntab(dev)[3] < 4)
1900 NV_INFO(drm, "DCB conn %02d: %04x\n",
1901 idx, ROM16(conn[0]));
1902 else
1903 NV_INFO(drm, "DCB conn %02d: %08x\n",
1904 idx, ROM32(conn[0]));
1905 }
1906 }
1907 dcb_fake_connectors(bios);
1908 return 0;
1909}
1910
1911static int load_nv17_hwsq_ucode_entry(struct drm_device *dev, struct nvbios *bios, uint16_t hwsq_offset, int entry)
1912{
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922 struct nouveau_drm *drm = nouveau_drm(dev);
1923 struct nvif_object *device = &drm->client.device.object;
1924 uint8_t bytes_to_write;
1925 uint16_t hwsq_entry_offset;
1926 int i;
1927
1928 if (bios->data[hwsq_offset] <= entry) {
1929 NV_ERROR(drm, "Too few entries in HW sequencer table for "
1930 "requested entry\n");
1931 return -ENOENT;
1932 }
1933
1934 bytes_to_write = bios->data[hwsq_offset + 1];
1935
1936 if (bytes_to_write != 36) {
1937 NV_ERROR(drm, "Unknown HW sequencer entry size\n");
1938 return -EINVAL;
1939 }
1940
1941 NV_INFO(drm, "Loading NV17 power sequencing microcode\n");
1942
1943 hwsq_entry_offset = hwsq_offset + 2 + entry * bytes_to_write;
1944
1945
1946 nvif_wr32(device, 0x00001304, ROM32(bios->data[hwsq_entry_offset]));
1947 bytes_to_write -= 4;
1948
1949
1950 for (i = 0; i < bytes_to_write; i += 4)
1951 nvif_wr32(device, 0x00001400 + i, ROM32(bios->data[hwsq_entry_offset + i + 4]));
1952
1953
1954 nvif_wr32(device, NV_PBUS_DEBUG_4, nvif_rd32(device, NV_PBUS_DEBUG_4) | 0x18);
1955
1956 return 0;
1957}
1958
1959static int load_nv17_hw_sequencer_ucode(struct drm_device *dev,
1960 struct nvbios *bios)
1961{
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971 static const uint8_t hwsq_signature[] = { 'H', 'W', 'S', 'Q' };
1972 const int sz = sizeof(hwsq_signature);
1973 int hwsq_offset;
1974
1975 hwsq_offset = findstr(bios->data, bios->length, hwsq_signature, sz);
1976 if (!hwsq_offset)
1977 return 0;
1978
1979
1980 return load_nv17_hwsq_ucode_entry(dev, bios, hwsq_offset + sz, 0);
1981}
1982
1983uint8_t *nouveau_bios_embedded_edid(struct drm_device *dev)
1984{
1985 struct nouveau_drm *drm = nouveau_drm(dev);
1986 struct nvbios *bios = &drm->vbios;
1987 static const uint8_t edid_sig[] = {
1988 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00 };
1989 uint16_t offset = 0;
1990 uint16_t newoffset;
1991 int searchlen = NV_PROM_SIZE;
1992
1993 if (bios->fp.edid)
1994 return bios->fp.edid;
1995
1996 while (searchlen) {
1997 newoffset = findstr(&bios->data[offset], searchlen,
1998 edid_sig, 8);
1999 if (!newoffset)
2000 return NULL;
2001 offset += newoffset;
2002 if (!nv_cksum(&bios->data[offset], EDID1_LEN))
2003 break;
2004
2005 searchlen -= offset;
2006 offset++;
2007 }
2008
2009 NV_INFO(drm, "Found EDID in BIOS\n");
2010
2011 return bios->fp.edid = &bios->data[offset];
2012}
2013
2014static bool NVInitVBIOS(struct drm_device *dev)
2015{
2016 struct nouveau_drm *drm = nouveau_drm(dev);
2017 struct nvkm_bios *bios = nvxx_bios(&drm->client.device);
2018 struct nvbios *legacy = &drm->vbios;
2019
2020 memset(legacy, 0, sizeof(struct nvbios));
2021 spin_lock_init(&legacy->lock);
2022 legacy->dev = dev;
2023
2024 legacy->data = bios->data;
2025 legacy->length = bios->size;
2026 legacy->major_version = bios->version.major;
2027 legacy->chip_version = bios->version.chip;
2028 if (bios->bit_offset) {
2029 legacy->type = NVBIOS_BIT;
2030 legacy->offset = bios->bit_offset;
2031 return !parse_bit_structure(legacy, legacy->offset + 6);
2032 } else
2033 if (bios->bmp_offset) {
2034 legacy->type = NVBIOS_BMP;
2035 legacy->offset = bios->bmp_offset;
2036 return !parse_bmp_structure(dev, legacy, legacy->offset);
2037 }
2038
2039 return false;
2040}
2041
2042int
2043nouveau_run_vbios_init(struct drm_device *dev)
2044{
2045 struct nouveau_drm *drm = nouveau_drm(dev);
2046 struct nvbios *bios = &drm->vbios;
2047 int ret = 0;
2048
2049
2050 bios->state.crtchead = 0;
2051
2052 if (bios->major_version < 5)
2053 load_nv17_hw_sequencer_ucode(dev, bios);
2054
2055 if (bios->execute) {
2056 bios->fp.last_script_invoc = 0;
2057 bios->fp.lvds_init_run = false;
2058 }
2059
2060 return ret;
2061}
2062
2063static bool
2064nouveau_bios_posted(struct drm_device *dev)
2065{
2066 struct nouveau_drm *drm = nouveau_drm(dev);
2067 unsigned htotal;
2068
2069 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
2070 return true;
2071
2072 htotal = NVReadVgaCrtc(dev, 0, 0x06);
2073 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x01) << 8;
2074 htotal |= (NVReadVgaCrtc(dev, 0, 0x07) & 0x20) << 4;
2075 htotal |= (NVReadVgaCrtc(dev, 0, 0x25) & 0x01) << 10;
2076 htotal |= (NVReadVgaCrtc(dev, 0, 0x41) & 0x01) << 11;
2077 return (htotal != 0);
2078}
2079
2080int
2081nouveau_bios_init(struct drm_device *dev)
2082{
2083 struct nouveau_drm *drm = nouveau_drm(dev);
2084 struct nvbios *bios = &drm->vbios;
2085 int ret;
2086
2087
2088 if (!dev->pdev)
2089 return 0;
2090
2091 if (!NVInitVBIOS(dev))
2092 return -ENODEV;
2093
2094 ret = parse_dcb_table(dev, bios);
2095 if (ret)
2096 return ret;
2097
2098 if (!bios->major_version)
2099 return 0;
2100
2101
2102 bios->execute = false;
2103
2104
2105 if (!nouveau_bios_posted(dev)) {
2106 NV_INFO(drm, "Adaptor not initialised, "
2107 "running VBIOS init tables.\n");
2108 bios->execute = true;
2109 }
2110
2111 ret = nouveau_run_vbios_init(dev);
2112 if (ret)
2113 return ret;
2114
2115
2116 if (bios->major_version < 5)
2117 bios->is_mobile = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_4B) & 0x40;
2118
2119
2120 if (bios->is_mobile || bios->major_version >= 5)
2121 ret = parse_fp_mode_table(dev, bios);
2122
2123
2124 bios->execute = true;
2125
2126 return 0;
2127}
2128
2129void
2130nouveau_bios_takedown(struct drm_device *dev)
2131{
2132}
2133