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