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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
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
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
|
/*
* Copyright 2005-2015 The Openchrome Project
* [https://www.freedesktop.org/wiki/Openchrome]
* Copyright 2004-2006 Luc Verhaegen.
* Copyright 2004-2005 The Unichrome Project [unichrome.sf.net]
* Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
* Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sub license,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "shadow.h"
#include "globals.h"
#ifdef HAVE_XEXTPROTO_71
#include <X11/extensions/dpmsconst.h>
#else
#define DPMS_SERVER
#include <X11/extensions/dpms.h>
#endif
#include "version.h"
#include "via_driver.h"
#include "drm_fourcc.h"
#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
#include "xf86RAC.h"
#endif
#include "xf86Crtc.h"
#ifdef HAVE_DRI
#include "dri.h"
#else
#include "drm_fourcc.h"
#endif
/* RandR support */
#include "xf86RandR12.h"
typedef struct
{
int major;
int minor;
int patchlevel;
} ViaDRMVersion;
static const ViaDRMVersion drmVIADRMExpected = { 1, 3, 0 };
static const ViaDRMVersion drmVIADRMCompat = { 3, 0, 0 };
static const ViaDRMVersion drmOpenChromeDRMVersion = { 3, 0, 0 };
/* Prototypes. */
static void VIAIdentify(int flags);
#ifdef HAVE_PCIACCESS
static Bool via_pci_probe(DriverPtr drv, int entity_num,
struct pci_device *dev, intptr_t match_data);
#else /* !HAVE_PCIACCESS */
static Bool VIAProbe(DriverPtr drv, int flags);
#endif
static Bool VIAPreInit(ScrnInfoPtr pScrn, int flags);
static Bool VIAScreenInit(SCREEN_INIT_ARGS_DECL);
int gVIAEntityIndex = -1;
#ifdef HAVE_PCIACCESS
#define VIA_DEVICE_MATCH(d,i) \
{ 0x1106, (d), PCI_MATCH_ANY, PCI_MATCH_ANY, 0, 0, (i) }
static const struct pci_id_match via_device_match[] = {
VIA_DEVICE_MATCH (PCI_CHIP_VT3204, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3259, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_CLE3122, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3205, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3314, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3336, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3364, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3324, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3327, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3353, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3409, 0 ),
VIA_DEVICE_MATCH (PCI_CHIP_VT3410, 0 ),
{ 0, 0, 0 },
};
#endif /* HAVE_PCIACCESS */
_X_EXPORT DriverRec VIA = {
VIA_VERSION,
DRIVER_NAME,
VIAIdentify,
#ifdef HAVE_PCIACCESS
NULL,
#else
VIAProbe,
#endif
VIAAvailableOptions,
NULL,
0,
NULL,
#ifdef HAVE_PCIACCESS
via_device_match,
via_pci_probe
#endif
};
/* Supported chipsets */
static SymTabRec VIAChipsets[] = {
{VIA_CLE266, "CLE266"},
{VIA_KM400, "KM400 / KM400A / KN400 / P4M800"},
{VIA_K8M800, "K8M800 / K8N800"},
{VIA_PM800, "PM800 / PN800 / PM880 / CN333 / CN400"},
{VIA_P4M800PRO, "P4M800 Pro / VN800 / CN700"},
{VIA_CX700, "CX700 / VX700"},
{VIA_P4M890, "P4M890 / VN890 / CN800"},
{VIA_K8M890, "K8M890 / K8N890"},
{VIA_P4M900, "P4M900 / VN896 / CN896"},
{VIA_VX800, "VX800 / VX820"},
{VIA_VX855, "VX855 / VX875"},
{VIA_VX900, "VX900"},
{-1, NULL}
};
/* Mapping a PCI device ID to a chipset family identifier. */
static PciChipsets VIAPciChipsets[] = {
{VIA_CLE266, PCI_CHIP_CLE3122, VIA_RES_SHARED},
{VIA_KM400, PCI_CHIP_VT3205, VIA_RES_SHARED},
{VIA_K8M800, PCI_CHIP_VT3204, VIA_RES_SHARED},
{VIA_PM800, PCI_CHIP_VT3259, VIA_RES_SHARED},
{VIA_P4M800PRO, PCI_CHIP_VT3314, VIA_RES_SHARED},
{VIA_CX700, PCI_CHIP_VT3324, VIA_RES_SHARED},
{VIA_P4M890, PCI_CHIP_VT3327, VIA_RES_SHARED},
{VIA_K8M890, PCI_CHIP_VT3336, VIA_RES_SHARED},
{VIA_P4M900, PCI_CHIP_VT3364, VIA_RES_SHARED},
{VIA_VX800, PCI_CHIP_VT3353, VIA_RES_SHARED},
{VIA_VX855, PCI_CHIP_VT3409, VIA_RES_SHARED},
{VIA_VX900, PCI_CHIP_VT3410, VIA_RES_SHARED},
{-1, -1, VIA_RES_UNDEF}
};
static char strEXAOptionName[] = "MigrationHeuristic";
static char strEXAValue[] = "greedy";
#ifdef XFree86LOADER
static MODULESETUPPROTO(VIASetup);
static XF86ModuleVersionInfo VIAVersRec = {
"openchrome",
"https://www.freedesktop.org/wiki/Openchrome/",
MODINFOSTRING1,
MODINFOSTRING2,
#ifdef XORG_VERSION_CURRENT
XORG_VERSION_CURRENT,
#else
XF86_VERSION_CURRENT,
#endif
VIA_MAJOR_VERSION, VIA_MINOR_VERSION, VIA_PATCHLEVEL,
ABI_CLASS_VIDEODRV,
ABI_VIDEODRV_VERSION,
MOD_CLASS_VIDEODRV,
{0, 0, 0, 0}
};
_X_EXPORT XF86ModuleData openchromeModuleData = { &VIAVersRec, VIASetup, NULL };
static pointer
VIASetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
static Bool setupDone = FALSE;
/* Only be loaded once */
if (!setupDone) {
setupDone = TRUE;
xf86AddDriver(&VIA, module,
#ifdef HAVE_PCIACCESS
HaveDriverFuncs
#else
0
#endif
);
return (pointer) 1;
} else {
if (errmaj)
*errmaj = LDR_ONCEONLY;
return NULL;
}
} /* VIASetup */
#endif /* XFree86LOADER */
static Bool
VIASwitchMode(SWITCH_MODE_ARGS_DECL)
{
SCRN_INFO_PTR(arg);
return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
}
static void
VIAAdjustFrame(ADJUST_FRAME_ARGS_DECL)
{
SCRN_INFO_PTR(arg);
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
int i;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAAdjustFrame %dx%d\n", x, y));
for (i = 0; i < xf86_config->num_crtc; i++) {
xf86CrtcPtr crtc = xf86_config->crtc[i];
xf86CrtcSetOrigin(crtc, x, y);
}
}
static Bool
VIAEnterVT_internal(ScrnInfoPtr pScrn, int flags)
{
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
VIAPtr pVia = VIAPTR(pScrn);
int i;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Entered %s.\n", __func__));
for (i = 0; i < xf86_config->num_crtc; i++) {
xf86CrtcPtr crtc = xf86_config->crtc[i];
if (crtc->funcs->save) {
crtc->funcs->save(crtc);
}
}
for (i = 0; i < xf86_config->num_output; i++) {
xf86OutputPtr output = xf86_config->output[i];
if (output->funcs->save) {
output->funcs->save(output);
}
}
if (!xf86SetDesiredModes(pScrn)) {
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Exiting %s.\n", __func__));
return FALSE;
}
if (!flags) {
/* Restore video status. */
if ((!pVia->IsSecondary) && (!pVia->KMS)) {
viaRestoreVideo(pScrn);
}
#ifdef HAVE_DRI
if (pVia->directRenderingType == DRI_1) {
kickVblank(pScrn);
VIADRIRingBufferInit(pScrn);
viaDRIOffscreenRestore(pScrn);
DRIUnlock(xf86ScrnToScreen(pScrn));
}
#endif
}
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Exiting %s.\n", __func__));
return TRUE;
}
static Bool
VIAEnterVT(VT_FUNC_ARGS_DECL)
{
SCRN_INFO_PTR(arg);
return VIAEnterVT_internal(pScrn, 0);
}
static void
VIALeaveVT_internal(ScrnInfoPtr pScrn, int flags)
{
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
VIAPtr pVia = VIAPTR(pScrn);
int i;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Entered %s.\n", __func__));
if (!flags) {
#ifdef HAVE_DRI
if (pVia->directRenderingType == DRI_1) {
volatile drm_via_sarea_t *saPriv = (drm_via_sarea_t *) DRIGetSAREAPrivate(pScrn->pScreen);
DRILock(xf86ScrnToScreen(pScrn), 0);
saPriv->ctxOwner = ~0;
viaAccelSync(pScrn);
VIADRIRingBufferCleanup(pScrn);
viaDRIOffscreenSave(pScrn);
if ((pVia->VQEnable) && (!pVia->KMS)) {
viaDisableVQ(pScrn);
}
}
#endif
/* Save video status and turn off all video activities. */
if ((!pVia->IsSecondary) && (!pVia->KMS)){
viaSaveVideo(pScrn);
}
}
for (i = 0; i < xf86_config->num_output; i++) {
xf86OutputPtr output = xf86_config->output[i];
if (output->funcs->restore) {
output->funcs->restore(output);
}
}
for (i = 0; i < xf86_config->num_crtc; i++) {
xf86CrtcPtr crtc = xf86_config->crtc[i];
if (crtc->funcs->restore) {
crtc->funcs->restore(crtc);
}
}
pScrn->vtSema = FALSE;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Exiting %s.\n", __func__));
}
static void
VIALeaveVT(VT_FUNC_ARGS_DECL)
{
SCRN_INFO_PTR(arg);
VIALeaveVT_internal(pScrn, 0);
}
static void
VIAFreeRec(ScrnInfoPtr pScrn)
{
VIAPtr pVia = VIAPTR(pScrn);
VIADisplayPtr pVIADisplay;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAFreeRec\n"));
if (!pScrn->driverPrivate)
return;
pVIADisplay = pVia->pVIADisplay;
if (pVIADisplay) {
if (pVIADisplay->TVI2CDev)
xf86DestroyI2CDevRec(pVIADisplay->TVI2CDev, TRUE);
pVia->pVIADisplay = NULL;
free(pVIADisplay);
}
if (pVia->VideoRegs)
free(pVia->VideoRegs);
free(pScrn->driverPrivate);
pScrn->driverPrivate = NULL;
} /* VIAFreeRec */
/*
* This only gets called when a screen is being deleted. It does not
* get called routinely at the end of a server generation.
*/
static void
VIAFreeScreen(FREE_SCREEN_ARGS_DECL)
{
SCRN_INFO_PTR(arg);
VIAPtr pVia = VIAPTR(pScrn);
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAFreeScreen\n"));
if (xf86LoaderCheckSymbol("vgaHWFreeHWRec")) {
vgaHWFreeHWRec(pScrn);
}
if (!pVia->KMS) {
viaUnmapFB(pScrn);
viaUnmapMMIO(pScrn);
}
VIAFreeRec(pScrn);
}
static void
VIAIdentify(int flags)
{
xf86PrintChipsets("OPENCHROME", "Driver for VIA Chrome chipsets",
VIAChipsets);
}
#ifdef HAVE_PCIACCESS
static Bool
via_pci_probe(DriverPtr driver, int entity_num,
struct pci_device *device, intptr_t match_data)
{
ScrnInfoPtr scrn = NULL;
scrn = xf86ConfigPciEntity(scrn, 0, entity_num, VIAPciChipsets,
NULL, NULL, NULL, NULL, NULL);
if (scrn != NULL) {
scrn->driverVersion = VIA_VERSION;
scrn->driverName = DRIVER_NAME;
scrn->name = "CHROME";
scrn->Probe = NULL;
scrn->PreInit = VIAPreInit;
scrn->ScreenInit = VIAScreenInit;
scrn->SwitchMode = VIASwitchMode;
scrn->AdjustFrame = VIAAdjustFrame;
scrn->EnterVT = VIAEnterVT;
scrn->LeaveVT = VIALeaveVT;
scrn->FreeScreen = VIAFreeScreen;
xf86Msg(X_NOTICE,
"VIA Technologies does not support this driver in any way.\n");
xf86Msg(X_NOTICE,
"For support, please refer to"
" https://www.freedesktop.org/wiki/Openchrome/.\n");
#ifdef BUILDCOMMENT
xf86Msg(X_NOTICE, BUILDCOMMENT"\n");
#endif
}
return scrn != NULL;
}
#else /* !HAVE_PCIACCESS */
static Bool
VIAProbe(DriverPtr drv, int flags)
{
GDevPtr *devSections;
int *usedChips;
int numDevSections;
int numUsed;
Bool foundScreen = FALSE;
int i;
/* sanity checks */
if ((numDevSections = xf86MatchDevice(DRIVER_NAME, &devSections)) <= 0)
return FALSE;
if (xf86GetPciVideoInfo() == NULL)
return FALSE;
numUsed = xf86MatchPciInstances(DRIVER_NAME,
PCI_VIA_VENDOR_ID,
VIAChipsets,
VIAPciChipsets,
devSections,
numDevSections,
drv,
&usedChips);
free(devSections);
if (numUsed <= 0)
return FALSE;
xf86Msg(X_NOTICE,
"VIA Technologies does not support this driver in any way.\n");
xf86Msg(X_NOTICE, "For support, please refer to"
" https://www.freedesktop.org/wiki/Openchrome/.\n");
#ifdef BUILDCOMMENT
xf86Msg(X_NOTICE, BUILDCOMMENT"\n");
#endif
if (flags & PROBE_DETECT) {
foundScreen = TRUE;
} else {
for (i = 0; i < numUsed; i++) {
ScrnInfoPtr pScrn = xf86AllocateScreen(drv, 0);
EntityInfoPtr pEnt;
if ((pScrn = xf86ConfigPciEntity(pScrn, 0, usedChips[i],
VIAPciChipsets, 0, 0, 0, 0, 0))) {
pScrn->driverVersion = VIA_VERSION;
pScrn->driverName = DRIVER_NAME;
pScrn->name = "CHROME";
pScrn->Probe = VIAProbe;
pScrn->PreInit = VIAPreInit;
pScrn->ScreenInit = VIAScreenInit;
pScrn->SwitchMode = VIASwitchMode;
pScrn->AdjustFrame = VIAAdjustFrame;
pScrn->EnterVT = VIAEnterVT;
pScrn->LeaveVT = VIALeaveVT;
pScrn->FreeScreen = VIAFreeScreen;
foundScreen = TRUE;
}
#if 0
xf86ConfigActivePciEntity(pScrn,
usedChips[i],
VIAPciChipsets,
NULL,
NULL,
NULL,
NULL,
NULL);
#endif
pEnt = xf86GetEntityInfo(usedChips[i]);
/* CLE266 supports dual-head; mark the entity as sharable. */
if (pEnt->chipset == VIA_CLE266 || pEnt->chipset == VIA_KM400) {
static int instance = 0;
DevUnion *pPriv;
xf86SetEntitySharable(usedChips[i]);
xf86SetEntityInstanceForScreen(pScrn,
pScrn->entityList[0], instance);
if (gVIAEntityIndex < 0) {
gVIAEntityIndex = xf86AllocateEntityPrivateIndex();
pPriv = xf86GetEntityPrivate(pScrn->entityList[0],
gVIAEntityIndex);
if (!pPriv->ptr) {
VIAEntPtr pVIAEnt;
pPriv->ptr = xnfcalloc(sizeof(VIAEntRec), 1);
pVIAEnt = pPriv->ptr;
pVIAEnt->IsDRIEnabled = FALSE;
pVIAEnt->BypassSecondary = FALSE;
pVIAEnt->HasSecondary = FALSE;
pVIAEnt->IsSecondaryRestored = FALSE;
}
}
instance++;
}
free(pEnt);
}
}
free(usedChips);
return foundScreen;
} /* VIAProbe */
#endif /* !HAVE_PCIACCESS */
static int
LookupChipSet(PciChipsets *pset, int chipSet)
{
while (pset->numChipset >= 0) {
if (pset->numChipset == chipSet)
return pset->PCIid;
pset++;
}
return -1;
}
static int
LookupChipID(PciChipsets *pset, int ChipID)
{
/* Is there a function to do this for me? */
while (pset->numChipset >= 0) {
if (pset->PCIid == ChipID)
return pset->numChipset;
pset++;
}
return -1;
}
static Bool
VIAGetRec(ScrnInfoPtr pScrn)
{
Bool ret = FALSE;
VIAPtr pVia;
VIADisplayPtr pVIADisplay;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIAGetRec\n"));
if (pScrn->driverPrivate)
return TRUE;
/* allocate VIARec */
pVia = (VIARec *) xnfcalloc(sizeof(VIARec), 1);
if (pVia) {
pVia->pVIADisplay = xnfcalloc(sizeof(VIADisplayRec), 1);
pVIADisplay = pVia->pVIADisplay;
if (pVIADisplay) {
pVIADisplay->TVI2CDev = NULL;
pVia->VideoRegs = (video_via_regs *) xnfcalloc(sizeof(video_via_regs), 1);
if (!pVia->VideoRegs) {
free(pVIADisplay);
free(pVia);
} else {
pScrn->driverPrivate = pVia;
ret = TRUE;
}
}
}
return ret;
} /* VIAGetRec */
static unsigned int
viaConvertDepthToBpp(int bpp, int depth)
{
unsigned int format = DRM_FORMAT_XRGB8888;
unsigned int bppSize;
switch (bpp) {
case 8:
format = DRM_FORMAT_C8;
break;
case 16:
if (depth == 15)
format = DRM_FORMAT_XRGB1555;
else
format = DRM_FORMAT_RGB565;
break;
case 24:
format = DRM_FORMAT_RGB888;
break;
case 32:
if (depth == 24)
format = DRM_FORMAT_XRGB8888;
else if (depth == 30)
format = DRM_FORMAT_XRGB2101010;
break;
default:
break;
}
switch (format) {
case DRM_FORMAT_C8:
bppSize = 1;
break;
case DRM_FORMAT_XRGB1555:
case DRM_FORMAT_RGB565:
bppSize = 2;
break;
case DRM_FORMAT_RGB888:
bppSize = 3;
break;
case DRM_FORMAT_XRGB2101010:
case DRM_FORMAT_XRGB8888:
bppSize = 4;
break;
default:
bppSize = 0;
break;
}
return bppSize;
}
static Bool
via_xf86crtc_resize(ScrnInfoPtr scrn, int width, int height)
{
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(scrn);
drmmode_crtc_private_ptr drmmode_crtc
= xf86_config->crtc[0]->driver_private;
drmmode_ptr drmmode = drmmode_crtc->drmmode;
struct buffer_object *old_front = NULL;
Bool ret;
ScreenPtr screen;
uint32_t old_fb_id;
int i;
int old_width, old_height, old_displayWidth;
int cpp = (scrn->bitsPerPixel + 7) / 8;
PixmapPtr ppix;
void *new_pixels;
VIAPtr pVia = VIAPTR(scrn);
xf86CrtcPtr crtc = NULL;
unsigned int bppSize, alignedPitch;
DEBUG(xf86DrvMsg(scrn->scrnIndex, X_INFO,
"Entered %s.\n", __func__));
screen = xf86ScrnToScreen(scrn);
ppix = screen->GetScreenPixmap(screen);
if ((scrn->virtualX == width) && (scrn->virtualY == height)) {
DEBUG(xf86DrvMsg(scrn->scrnIndex, X_INFO,
"Exiting %s.\n", __func__));
return TRUE;
}
/* Preserve the old screen information just in case it needs to
* be restored. */
old_width = scrn->virtualX;
old_height = scrn->virtualY;
old_displayWidth = scrn->displayWidth;
old_fb_id = drmmode->fb_id;
old_front = drmmode->front_bo;
bppSize = viaConvertDepthToBpp(scrn->bitsPerPixel,
scrn->depth);
alignedPitch = width * bppSize;
alignedPitch = ALIGN_TO(alignedPitch, 16);
drmmode->front_bo = drm_bo_alloc(scrn,
alignedPitch * height,
16, TTM_PL_FLAG_VRAM);
if (!drmmode->front_bo) {
goto fail;
}
scrn->virtualX = width;
scrn->virtualY = height;
scrn->displayWidth = width;
#ifdef HAVE_DRI
if (pVia->KMS) {
ret = drmModeAddFB(drmmode->fd, width, height, scrn->depth,
scrn->bitsPerPixel, width * cpp,
drmmode->front_bo->handle,
&drmmode->fb_id);
if (ret) {
goto fail;
}
}
#endif
new_pixels = drm_bo_map(scrn, drmmode->front_bo);
if (!new_pixels) {
goto fail;
}
if (pVia->shadowFB) {
new_pixels = malloc(scrn->displayWidth * scrn->virtualY *
((scrn->bitsPerPixel + 7) >> 3));
if (!new_pixels) {
goto fail;
}
free(pVia->ShadowPtr);
pVia->ShadowPtr = new_pixels;
}
screen->ModifyPixmapHeader(ppix, width, height, -1, -1,
width * cpp, new_pixels);
xf86DrvMsg(scrn->scrnIndex, X_INFO,
"Allocated a new frame buffer: %dx%d\n",
width, height);
#if XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1,9,99,1,0)
scrn->pixmapPrivate.ptr = ppix->devPrivate.ptr;
#endif
for (i = 0; i < xf86_config->num_crtc; i++) {
crtc = xf86_config->crtc[i];
if (!xf86CrtcInUse(crtc)) {
continue;
}
ret = xf86CrtcSetMode(crtc,
&crtc->desiredMode,
crtc->desiredRotation,
crtc->desiredX, crtc->desiredY);
if (!ret) {
xf86DrvMsg(scrn->scrnIndex, X_INFO,
"Mode setting failed.\n");
goto fail;
}
}
#ifdef HAVE_DRI
if (pVia->KMS && old_fb_id) {
drmModeRmFB(drmmode->fd, old_fb_id);
}
#endif
if (old_fb_id) {
drm_bo_unmap(scrn, old_front);
drm_bo_free(scrn, old_front);
}
xf86DrvMsg(scrn->scrnIndex, X_INFO,
"Screen resize successful.\n");
DEBUG(xf86DrvMsg(scrn->scrnIndex, X_INFO,
"Exiting %s.\n", __func__));
return TRUE;
fail:
if (drmmode->front_bo) {
drm_bo_unmap(scrn, drmmode->front_bo);
drm_bo_free(scrn, drmmode->front_bo);
}
scrn->virtualX = old_width;
scrn->virtualY = old_height;
scrn->displayWidth = old_displayWidth;
drmmode->fb_id = old_fb_id;
drmmode->front_bo = old_front;
xf86DrvMsg(scrn->scrnIndex, X_ERROR,
"An error occurred during screen resize.\n");
DEBUG(xf86DrvMsg(scrn->scrnIndex, X_INFO,
"Exiting %s.\n", __func__));
return FALSE;
}
static const
xf86CrtcConfigFuncsRec via_xf86crtc_config_funcs = {
via_xf86crtc_resize
};
static Bool
VIAPreInit(ScrnInfoPtr pScrn, int flags)
{
EntityInfoPtr pEnt;
VIAPtr pVia;
MessageType from = X_DEFAULT;
#ifdef HAVE_DRI
char *busId = NULL;
drmVersionPtr drmVer;
#endif
rgb defaultWeight = {0, 0, 0};
rgb defaultMask = {0, 0, 0};
Gamma defaultGamma = {0.0, 0.0, 0.0};
XF86OptionPtr option;
Bool status = FALSE;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Entered VIAPreInit.\n"));
pScrn->monitor = pScrn->confScreen->monitor;
/*
* We support depths of 8, 16 and 24.
* We support bpp of 8, 16, and 32.
*/
if (!xf86SetDepthBpp(pScrn, 0, 0, 0, Support32bppFb)) {
goto exit;
} else {
switch (pScrn->depth) {
case 8:
case 16:
case 24:
/* OK */
break;
case 32:
/* OK */
pScrn->depth = 24;
break;
default:
xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
"Given depth (%d) is not supported by this driver!\n",
pScrn->depth);
goto exit;
break;
}
}
pScrn->rgbBits = 8;
/* Print out the depth / bpp that was set. */
xf86PrintDepthBpp(pScrn);
if (pScrn->depth > 8) {
if (!xf86SetWeight(pScrn, defaultWeight, defaultMask)) {
goto exit;
} else {
/* TODO check weight returned is supported. */
}
}
if (!xf86SetDefaultVisual(pScrn, -1)) {
goto exit;
} else {
/* We don't currently support DirectColor at > 8bpp. */
if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
"Given default visual (%s) is not supported "
"at depth %d.\n",
xf86GetVisualName(pScrn->defaultVisual),
pScrn->depth);
goto exit;
}
}
/* If the driver supports gamma correction, set the gamma. */
if (!xf86SetGamma(pScrn, defaultGamma)) {
goto exit;
}
/* This driver uses a programmable clock. */
pScrn->progClock = TRUE;
if (pScrn->numEntities > 1) {
goto exit;
}
if (!VIAGetRec(pScrn)) {
goto exit;
}
pVia = VIAPTR(pScrn);
pVia->IsSecondary = FALSE;
pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
#ifndef HAVE_PCIACCESS
if (pEnt->resources) {
free(pEnt);
VIAFreeRec(pScrn);
return FALSE;
}
#endif
pVia->EntityIndex = pEnt->index;
if (xf86IsEntityShared(pScrn->entityList[0])) {
if (xf86IsPrimInitDone(pScrn->entityList[0])) {
DevUnion *pPriv;
VIAEntPtr pVIAEnt;
VIAPtr pVia1;
pVia->IsSecondary = TRUE;
pPriv = xf86GetEntityPrivate(pScrn->entityList[0], gVIAEntityIndex);
pVIAEnt = pPriv->ptr;
if (pVIAEnt->BypassSecondary) {
free(pEnt);
VIAFreeRec(pScrn);
return FALSE;
}
pVIAEnt->pSecondaryScrn = pScrn;
pVIAEnt->HasSecondary = TRUE;
pVia1 = VIAPTR(pVIAEnt->pPrimaryScrn);
pVia1->HasSecondary = TRUE;
pVia->sharedData = pVia1->sharedData;
} else {
DevUnion *pPriv;
VIAEntPtr pVIAEnt;
xf86SetPrimInitDone(pScrn->entityList[0]);
pPriv = xf86GetEntityPrivate(pScrn->entityList[0], gVIAEntityIndex);
pVia->sharedData = xnfcalloc(sizeof(ViaSharedRec), 1);
pVIAEnt = pPriv->ptr;
pVIAEnt->pPrimaryScrn = pScrn;
pVIAEnt->IsDRIEnabled = FALSE;
pVIAEnt->BypassSecondary = FALSE;
pVIAEnt->HasSecondary = FALSE;
pVIAEnt->RestorePrimary = FALSE;
pVIAEnt->IsSecondaryRestored = FALSE;
}
} else {
pVia->sharedData = xnfcalloc(sizeof(ViaSharedRec), 1);
}
pVia->PciInfo = xf86GetPciInfoForEntity(pEnt->index);
#ifndef HAVE_PCIACCESS
xf86RegisterResources(pEnt->index, NULL, ResNone);
#endif
if (pEnt->device->chipset && *pEnt->device->chipset) {
from = X_CONFIG;
pScrn->chipset = pEnt->device->chipset;
pVia->Chipset = xf86StringToToken(VIAChipsets, pScrn->chipset);
pVia->ChipId = LookupChipSet(VIAPciChipsets, pVia->Chipset);
} else if (pEnt->device->chipID >= 0) {
from = X_CONFIG;
pVia->ChipId = pEnt->device->chipID;
pVia->Chipset = LookupChipID(VIAPciChipsets, pVia->ChipId);
pScrn->chipset = (char *)xf86TokenToString(VIAChipsets, pVia->Chipset);
xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ChipID override: 0x%04X\n",
pEnt->device->chipID);
} else {
from = X_PROBED;
pVia->ChipId = DEVICE_ID(pVia->PciInfo);
pVia->Chipset = LookupChipID(VIAPciChipsets, pVia->ChipId);
pScrn->chipset = (char *)xf86TokenToString(VIAChipsets, pVia->Chipset);
}
xf86DrvMsg(pScrn->scrnIndex, from, "Chipset: %s\n", pScrn->chipset);
if (pEnt->device->chipRev >= 0) {
pVia->ChipRev = pEnt->device->chipRev;
xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "ChipRev override: %d\n",
pVia->ChipRev);
} else {
/* Read PCI bus 0, dev 0, function 0, index 0xF6 to get chip revision */
#ifdef HAVE_PCIACCESS
struct pci_device *bridge = pci_device_get_parent_bridge(pVia->PciInfo);
uint8_t rev = 0;
pci_device_cfg_read_u8(bridge, &rev, 0xF6);
pVia->ChipRev = rev;
#else
pVia->ChipRev = pciReadByte(pciTag(0, 0, 0), 0xF6);
#endif
}
if (pEnt)
free(pEnt);
xf86DrvMsg(pScrn->scrnIndex, from, "Chipset revision: %d\n", pVia->ChipRev);
pVia->directRenderingType = DRI_NONE;
pVia->KMS = FALSE;
#ifdef HAVE_DRI
busId = DRICreatePCIBusID(pVia->PciInfo);
/* Look for OpenChrome DRM first. */
/* KMS supports needs to be present for OpenChrome DRM to
* function properly.*/
pVia->drmmode.fd = drmOpen("openchrome", busId);
if (pVia->drmmode.fd != -1) {
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"OpenChrome DRM detected.\n");
drmVer = drmGetVersion(pVia->drmmode.fd);
if (drmVer) {
pVia->drmVerMajor = drmVer->version_major;
pVia->drmVerMinor = drmVer->version_minor;
pVia->drmVerPatchLevel = drmVer->version_patchlevel;
drmFreeVersion(drmVer);
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"OpenChrome DRM Version: %d.%d.%d\n",
pVia->drmVerMajor, pVia->drmVerMinor,
pVia->drmVerPatchLevel);
if ((pVia->drmVerMajor > drmOpenChromeDRMVersion.major) ||
((pVia->drmVerMajor == drmOpenChromeDRMVersion.major) &&
(pVia->drmVerMinor >= drmOpenChromeDRMVersion.minor))) {
if (!drmCheckModesettingSupported(busId)) {
pVia->KMS = TRUE;
pVia->directRenderingType = DRI_2;
pVia->NoAccel = TRUE;
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"KMS is supported by "
"OpenChrome DRM.\n");
} else {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"KMS is not available.\n");
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Disabling OpenChrome DRM support.\n");
}
} else {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Unsupported version of OpenChrome DRM "
"detected.\n");
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Only OpenChrome DRM Version %d.%d or "
"later is supported.\n",
drmOpenChromeDRMVersion.major,
drmOpenChromeDRMVersion.minor);
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Disabling OpenChrome DRM support.\n");
}
} else {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Not able to obtain OpenChrome DRM version.\n");
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Disabling OpenChrome DRM support.\n");
}
}
free(busId);
/* Now, check for "legacy" DRI1 VIA DRM. */
if (!pVia->KMS) {
busId = DRICreatePCIBusID(pVia->PciInfo);
pVia->drmmode.fd = drmOpen("via", busId);
if (pVia->drmmode.fd != -1) {
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"VIA DRM detected.\n");
drmVer = drmGetVersion(pVia->drmmode.fd);
if (drmVer) {
pVia->drmVerMajor = drmVer->version_major;
pVia->drmVerMinor = drmVer->version_minor;
pVia->drmVerPatchLevel = drmVer->version_patchlevel;
drmFreeVersion(drmVer);
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"VIA DRM Version: %d.%d.%d\n",
pVia->drmVerMajor, pVia->drmVerMinor,
pVia->drmVerPatchLevel);
if (((pVia->drmVerMajor > drmVIADRMExpected.major) &&
(pVia->drmVerMajor < drmVIADRMCompat.major)) ||
((pVia->drmVerMajor == drmVIADRMExpected.major) &&
(pVia->drmVerMinor >= drmVIADRMExpected.minor) &&
(pVia->drmVerMajor < drmVIADRMCompat.major))) {
pVia->directRenderingType = DRI_1;
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"This version of VIA DRM is "
"compatible with OpenChrome DDX.\n");
} else {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"This version of VIA DRM is not "
"compatible with OpenChrome DDX.\n"
"OpenChrome DDX can work with "
"VIA DRM Version %d.%d to %d.%d.\n",
drmVIADRMExpected.major,
drmVIADRMExpected.minor,
drmVIADRMCompat.major,
drmVIADRMCompat.minor);
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Disabling VIA DRM support.\n");
}
} else {
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"Not able to obtain VIA DRM version.\n");
}
} else {
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"OpenChrome DDX will now operate "
"without DRM.\n");
}
free(busId);
}
#endif
option = xf86NewOption(strEXAOptionName, strEXAValue);
xf86CollectOptions(pScrn, option);
viaSetupDefaultOptions(pScrn);
viaProcessOptions(pScrn);
VIAVidHWDiffInit(pScrn);
/*
* After viaUMSPreInit() succeeds, PCI hardware resources are
* memory mapped. If there is an error from this point on, they
* will need to be explicitly relinquished.
*/
if (!pVia->KMS) {
if (!viaUMSPreInit(pScrn)) {
VIAFreeRec(pScrn);
return FALSE;
}
}
/* CRTC handling */
xf86CrtcConfigInit(pScrn, &via_xf86crtc_config_funcs);
if (pVia->KMS) {
if (!KMSCrtcInit(pScrn, &pVia->drmmode)) {
goto fail;
}
} else {
if (!viaUMSCrtcInit(pScrn)) {
goto fail;
}
}
if (!xf86InitialConfiguration(pScrn, TRUE)) {
xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Initial configuration failed\n");
goto fail;
}
if (!pScrn->modes) {
xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes found\n");
goto fail;
}
/* Set up screen parameters. */
pVia->Bpl = pScrn->virtualX * (pScrn->bitsPerPixel >> 3);
/* Set the current mode to the first in the list */
pScrn->currentMode = pScrn->modes;
/* Set display resolution */
xf86SetDpi(pScrn, 0, 0);
if (xf86LoadSubModule(pScrn, "fb") == NULL) {
goto fail;
}
if (!pVia->NoAccel) {
XF86ModReqInfo req;
int errmaj, errmin;
memset(&req, 0, sizeof(req));
req.majorversion = 2;
req.minorversion = 0;
if (!LoadSubModule(pScrn->module, "exa", NULL, NULL, NULL, &req,
&errmaj, &errmin)) {
LoaderErrorMsg(NULL, "exa", errmaj, errmin);
goto fail;
}
}
if (pVia->shadowFB) {
if (!xf86LoadSubModule(pScrn, "shadow")) {
goto fail;
}
}
status = TRUE;
goto exit;
fail:
if (!pVia->KMS) {
viaUnmapFB(pScrn);
viaUnmapMMIO(pScrn);
}
VIAFreeRec(pScrn);
exit:
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Exiting VIAPreInit.\n"));
return status;
}
static void
LoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
LOCO * colors, VisualPtr pVisual)
{
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
CARD16 lut_r[256], lut_g[256], lut_b[256];
int i, j, k, index;
for (k = 0; k < xf86_config->num_crtc; k++) {
xf86CrtcPtr crtc = xf86_config->crtc[k];
switch (pScrn->depth) {
case 15:
for (i = 0; i < numColors; i++) {
index = indices[i];
for (j = 0; j < 8; j++) {
lut_r[index * 8 + j] = colors[index].red << 8;
lut_g[index * 8 + j] = colors[index].green << 8;
lut_b[index * 8 + j] = colors[index].blue << 8;
}
}
break;
case 16:
for (i = 0; i < numColors; i++) {
index = indices[i];
if (index <= 31) {
for (j = 0; j < 8; j++) {
lut_r[index * 8 + j] = colors[index].red << 8;
lut_b[index * 8 + j] = colors[index].blue << 8;
}
}
for (j = 0; j < 4; j++)
lut_g[index * 4 + j] = colors[index].green << 8;
}
break;
default:
for (i = 0; i < numColors; i++) {
index = indices[i];
lut_r[index] = colors[index].red << 8;
lut_g[index] = colors[index].green << 8;
lut_b[index] = colors[index].blue << 8;
}
break;
}
/* Make the change through RandR */
#ifdef RANDR_12_INTERFACE
RRCrtcGammaSet(crtc->randr_crtc, lut_r, lut_g, lut_b);
#else /*RANDR_12_INTERFACE*/
crtc->funcs->gamma_set(crtc, lut_r, lut_g, lut_b, 256);
#endif
}
}
static void
viaUpdatePacked(ScreenPtr pScreen, shadowBufPtr pBuf)
{
shadowUpdatePacked(pScreen, pBuf);
}
static void *
viaShadowWindow(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
CARD32 *size, void *closure)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
VIAPtr pVia = VIAPTR(pScrn);
int stride;
stride = (pScrn->displayWidth * pScrn->bitsPerPixel) / 8;
*size = stride;
return ((uint8_t *) drm_bo_map(pScrn, pVia->drmmode.front_bo) + row * stride + offset);
}
static Bool
VIACreateScreenResources(ScreenPtr pScreen)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
VIAPtr pVia = VIAPTR(pScrn);
PixmapPtr rootPixmap;
void *surface;
pScreen->CreateScreenResources = pVia->CreateScreenResources;
if (!(*pScreen->CreateScreenResources)(pScreen))
return FALSE;
pScreen->CreateScreenResources = VIACreateScreenResources;
rootPixmap = pScreen->GetScreenPixmap(pScreen);
#ifdef HAVE_DRI
drmmode_uevent_init(pScrn, &pVia->drmmode);
#endif
surface = drm_bo_map(pScrn, pVia->drmmode.front_bo);
if (!surface)
return FALSE;
if (pVia->shadowFB)
surface = pVia->ShadowPtr;
if (!pScreen->ModifyPixmapHeader(rootPixmap, -1, -1, -1, -1, -1,
surface))
return FALSE;
if (pVia->shadowFB) {
if (!shadowAdd(pScreen, rootPixmap, viaUpdatePacked,
viaShadowWindow, 0, NULL))
return FALSE;
}
return TRUE;
}
static Bool
VIACloseScreen(CLOSE_SCREEN_ARGS_DECL)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
VIAPtr pVia = VIAPTR(pScrn);
int i;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO, "VIACloseScreen\n"));
if (pVia->directRenderingType != DRI_2)
viaExitVideo(pScrn);
if (!pVia->NoAccel) {
viaExitAccel(pScreen);
}
if (pVia->ShadowPtr) {
shadowRemove(pScreen, pScreen->GetScreenPixmap(pScreen));
free(pVia->ShadowPtr);
pVia->ShadowPtr = NULL;
}
/* Is the display currently visible? */
if (pScrn->vtSema)
VIALeaveVT(VT_FUNC_ARGS(0));
#ifdef HAVE_DRI
drmmode_uevent_fini(pScrn, &pVia->drmmode);
#endif
xf86_cursors_fini(pScreen);
if (pVia->drmmode.front_bo) {
#ifdef HAVE_DRI
if (pVia->KMS && pVia->drmmode.fb_id)
drmModeRmFB(pVia->drmmode.fd, pVia->drmmode.fb_id);
#endif
pVia->drmmode.fb_id = 0;
drm_bo_unmap(pScrn, pVia->drmmode.front_bo);
drm_bo_free(pScrn, pVia->drmmode.front_bo);
}
for (i = 0; i < xf86_config->num_crtc; i++) {
xf86CrtcPtr crtc = xf86_config->crtc[i];
drmmode_crtc_private_ptr iga = crtc->driver_private;
if (iga->cursor_bo)
drm_bo_free(pScrn, iga->cursor_bo);
}
#ifdef HAVE_DRI
if (pVia->directRenderingType == DRI_1)
VIADRICloseScreen(pScreen);
if (pVia->KMS) {
drmmode_uevent_fini(pScrn, &pVia->drmmode);
if (drmDropMaster(pVia->drmmode.fd))
xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
"drmDropMaster failed: %s\n",
strerror(errno));
}
#endif
if (!pVia->KMS) {
viaUnmapFB(pScrn);
viaUnmapMMIO(pScrn);
}
pScrn->vtSema = FALSE;
pScreen->CloseScreen = pVia->CloseScreen;
return (*pScreen->CloseScreen) (CLOSE_SCREEN_ARGS);
}
static Bool
VIAScreenInit(SCREEN_INIT_ARGS_DECL)
{
ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
VIAPtr pVia = VIAPTR(pScrn);
unsigned int bppSize, alignedPitch;
pScrn->displayWidth = pScrn->virtualX;
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Entered %s.\n", __func__));
pScrn->pScreen = pScreen;
miClearVisualTypes();
if (!miSetVisualTypes(pScrn->depth,
(pScrn->bitsPerPixel > 8) && (!pVia->IsSecondary) ?
TrueColorMask : miGetDefaultVisualMask(pScrn->depth),
pScrn->rgbBits, pScrn->defaultVisual)) {
return FALSE;
}
if (!miSetPixmapDepths()) {
return FALSE;
}
if (pVia->shadowFB) {
int pitch = BitmapBytePad(pScrn->bitsPerPixel * pScrn->virtualX);
pVia->shadowFB = FALSE;
pVia->ShadowPtr = malloc(pitch * pScrn->virtualY);
if (pVia->ShadowPtr) {
if (shadowSetup(pScreen))
pVia->shadowFB = TRUE;
}
}
if (!fbScreenInit(pScreen, NULL, pScrn->virtualX, pScrn->virtualY,
pScrn->xDpi, pScrn->yDpi, pScrn->displayWidth,
pScrn->bitsPerPixel))
return FALSE;
if (pScrn->bitsPerPixel > 8) {
VisualPtr visual;
visual = pScreen->visuals + pScreen->numVisuals;
while (--visual >= pScreen->visuals) {
if ((visual->class | DynamicClass) == DirectColor) {
visual->offsetRed = pScrn->offset.red;
visual->offsetGreen = pScrn->offset.green;
visual->offsetBlue = pScrn->offset.blue;
visual->redMask = pScrn->mask.red;
visual->greenMask = pScrn->mask.green;
visual->blueMask = pScrn->mask.blue;
}
}
}
/* Must be after RGB ordering is fixed. */
fbPictureInit(pScreen, NULL, 0);
#ifdef HAVE_DRI
if (pVia->KMS) {
if (drmSetMaster(pVia->drmmode.fd)) {
xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
"drmSetMaster failed: %s\n",
strerror(errno));
return FALSE;
}
}
if (pVia->drmmode.fd != -1) {
if (pVia->directRenderingType == DRI_1) {
/* DRI2 or DRI1 support */
if (VIADRI1ScreenInit(pScreen))
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"DRI1 ScreenInit complete.\n"));
else
pVia->directRenderingType = DRI_NONE;
}
}
#endif
#ifdef HAVE_DRI
if (pVia->directRenderingType != DRI_2)
#endif /* HAVE_DRI */
{
if (!viaUMSCreate(pScrn)) {
return FALSE;
}
}
if ((!pVia->NoAccel) &&
((pVia->directRenderingType == DRI_NONE)
#ifdef HAVE_DRI
|| (pVia->directRenderingType == DRI_1)
#endif /* HAVE_DRI */
)) {
if (!viaUMSAccelInit(pScrn->pScreen)) {
return FALSE;
}
}
xf86SetBackingStore(pScreen);
xf86SetSilkenMouse(pScreen);
xf86SetBlackWhitePixels(pScreen);
miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
if (pVia->drmmode.hwcursor) {
xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
int flags = (HARDWARE_CURSOR_AND_SOURCE_WITH_MASK |
HARDWARE_CURSOR_TRUECOLOR_AT_8BPP);
int cursorSize, size, i = 0;
switch (pVia->Chipset) {
case VIA_CLE266:
case VIA_KM400:
flags |= HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_1;
size = 32;
cursorSize = ((size * size) >> 3) * 2;
break;
default:
flags |= (HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 | HARDWARE_CURSOR_ARGB);
size = 64;
cursorSize = (size * size) << 2;
break;
}
for (i = 0; i < xf86_config->num_crtc; i++) {
xf86CrtcPtr crtc = xf86_config->crtc[i];
drmmode_crtc_private_ptr iga = crtc->driver_private;
/* Set cursor location in frame buffer. */
iga->cursor_bo = drm_bo_alloc(pScrn, cursorSize, 16, TTM_PL_FLAG_VRAM);
}
if (!xf86_cursors_init(pScreen, size, size, flags)) {
pVia->drmmode.hwcursor = FALSE;
xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
"Hardware cursor initialization failed.\n");
}
}
bppSize = viaConvertDepthToBpp(pScrn->bitsPerPixel,
pScrn->depth);
alignedPitch = pScrn->virtualX * bppSize;
alignedPitch = ALIGN_TO(alignedPitch, 16);
pVia->drmmode.front_bo = drm_bo_alloc(pScrn,
alignedPitch *
pScrn->virtualY,
16, TTM_PL_FLAG_VRAM);
if (!pVia->drmmode.front_bo)
return FALSE;
if (!drm_bo_map(pScrn, pVia->drmmode.front_bo))
return FALSE;
pScrn->vtSema = TRUE;
pScreen->SaveScreen = xf86SaveScreen;
pVia->CloseScreen = pScreen->CloseScreen;
pScreen->CloseScreen = VIACloseScreen;
pVia->CreateScreenResources = pScreen->CreateScreenResources;
pScreen->CreateScreenResources = VIACreateScreenResources;
if (!xf86CrtcScreenInit(pScreen))
return FALSE;
if (!miCreateDefColormap(pScreen))
return FALSE;
if (!xf86HandleColormaps(pScreen, 256, 8, LoadPalette, NULL,
CMAP_RELOAD_ON_MODE_SWITCH
| CMAP_PALETTED_TRUECOLOR))
return FALSE;
xf86DPMSInit(pScreen, xf86DPMSSet, 0);
if (!VIAEnterVT_internal(pScrn, 1))
return FALSE;
if (pVia->directRenderingType != DRI_2) {
#ifdef HAVE_DRI
if (pVia->directRenderingType == DRI_1) {
if (!VIADRIFinishScreenInit(pScreen)) {
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Direct rendering disabled.\n");
pVia->directRenderingType = DRI_NONE;
} else
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Direct rendering enabled.\n");
}
#endif
if (!pVia->NoAccel)
viaFinishInitAccel(pScreen);
viaInitVideo(pScrn->pScreen);
}
if (serverGeneration == 1)
xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
#ifdef HAVE_DEBUG
if (pVia->PrintVGARegs) {
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Printing VGA registers.\n");
ViaVgahwPrint(VGAHWPTR(pScrn));
}
if (pVia->PrintTVRegs) {
xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Printing TV registers.\n");
ViaTVPrintRegs(pScrn);
}
#endif
DEBUG(xf86DrvMsg(pScrn->scrnIndex, X_INFO,
"Exiting %s.\n", __func__));
return TRUE;
}
|