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