summaryrefslogtreecommitdiff
path: root/src/via_dri.c
blob: d7104b8d7297ccc5e9f16da819fb7b03ec374e23 (plain)
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
/*
 * 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 "xf86.h"
#include "xf86_OSproc.h"

#ifndef _XF86_ANSIC_H
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#endif

#include "xf86Priv.h"

#include "xf86PciInfo.h"
#include "xf86Pci.h"

#define _XF86DRI_SERVER_
#include "dri.h"
#include "GL/glxtokens.h"
#include "GL/glxint.h"
#include "sarea.h"

#include "via_driver.h"
#include "via_dri.h"
#include "via_id.h"
#include "xf86drm.h"
#include "via_2dregs.h"

/*
 * These only came into existence in xorg in may 2005, so provide
 * a fallback. See dri.h for more information.
 */
#ifndef DRIINFO_MAJOR_VERSION
#define DRIINFO_MAJOR_VERSION 4
#endif

#ifndef DRIINFO_MINOR_VERSION
#define DRIINFO_MINOR_VERSION 1
#endif

#ifndef XSERVER_LIBPCIACCESS
# define PCI_BUS(x)	((x)->bus)
# define PCI_DEV(x)	((x)->device)
# define PCI_FUNC(x)	((x)->func)
#else
# define PCI_BUS(x)	(((x)->domain << 8) | (x)->bus)
# define PCI_DEV(x)	((x)->dev)
# define PCI_FUNC(x)	((x)->func)
#endif

extern void GlxSetVisualConfigs(
    int nconfigs,
    __GLXvisualConfig *configs,
    void **configprivs
);

#define VIDEO   0
#define AGP     1
#define AGP_PAGE_SIZE 4096
#define AGP_PAGES 8192
#define AGP_SIZE (AGP_PAGE_SIZE * AGP_PAGES)
#define AGP_CMDBUF_PAGES 512
#define AGP_CMDBUF_SIZE (AGP_PAGE_SIZE * AGP_CMDBUF_PAGES)
#define AGP_MODE_BITS 0x0F
#define AGP_MODE_V3_8x 0x0a
#define AGP_MODE_V3_4x 0x09

static char VIAKernelDriverName[] = "via";
static char VIAClientDriverName[] = "unichrome";

struct ViaDri {
    int scrnIndex;

    DRIInfoPtr		pDRIInfo;
    CARD32              drmVersion; /* major << 16 | minor << 8 | patch */
    int 		numVisualConfigs;
    __GLXvisualConfig*  pVisualConfigs;
    VIAConfigPrivPtr 	pVisualConfigsPriv;
    drm_handle_t 	agpHandle;
    drm_handle_t 	registerHandle;
    unsigned long 	agpAddr;
    drmAddress          agpMappedAddr;
    unsigned char 	*agpBase;
    unsigned int 	agpSize;
    Bool 		IsPCI;
};

/*
 *
 */
static void
VIADRIIrqInit(VIAPtr pVia, VIADRIPtr pVIADRI)
{
    pVIADRI->irqEnabled =
	drmGetInterruptFromBusID(pVia->drmFD, PCI_BUS(pVia->PciInfo),
				 PCI_DEV(pVia->PciInfo), PCI_FUNC(pVia->PciInfo));

    if ((drmCtlInstHandler(pVia->drmFD, pVIADRI->irqEnabled))) {
	xf86DrvMsg(pVia->scrnIndex, X_WARNING,
		   "[drm] Failure adding irq handler. "
		   "Falling back to irq-free operation.\n");
	pVIADRI->irqEnabled = 0;
    }

    if (pVIADRI->irqEnabled)
	xf86DrvMsg(pVia->scrnIndex, X_INFO,
		   "[drm] Irq handler installed, using IRQ %d.\n",
		   pVIADRI->irqEnabled);
}

/*
 *
 */
static void
VIADRIIrqExit(VIAPtr pVia, VIADRIPtr pVIADRI)
{
    if (pVIADRI->irqEnabled) {
	if (drmCtlUninstHandler(pVia->drmFD)) {
	    xf86DrvMsg(pVia->scrnIndex, X_INFO,
		       "[drm] Irq handler uninstalled.\n");
	} else {
	    xf86DrvMsg(pVia->scrnIndex, X_ERROR,
		       "[drm] Could not uninstall irq handler.\n");
	}
    }
}

/*
 *
 */
Bool
VIADRMCommandBufferStart(VIAPtr pVia)
{
    struct ViaDri *pDri = pVia->Dri;
    drm_via_dma_init_t ringBufInit;

    VIAFUNC(pVia);

    if (pDri->drmVersion < 0x010400)
	return FALSE;

    if (pVia->CommandBufferActive)
	xf86DrvMsg(pVia->scrnIndex, X_ERROR,
		   "%s: Command buffer still active!!!\n", __func__);

    ringBufInit.offset = pDri->agpSize;
    ringBufInit.size = AGP_CMDBUF_SIZE;

    /* Info frome code-snippet on DRI-DEVEL list; Erdi Chen. */
    switch (pVia->Chipset) {
    case VT3118:
    case VT3157:
	ringBufInit.reg_pause_addr = 0x40c;
	break;
    default:
	ringBufInit.reg_pause_addr = 0x418;
	break;
    }

    ringBufInit.func = VIA_INIT_DMA;
    if (drmCommandWrite(pVia->drmFD, DRM_VIA_DMA_INIT, &ringBufInit,
			sizeof(ringBufInit))) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR,
		   "[drm] Failed to initialize DMA ring-buffer: %d\n", errno);
	return FALSE;
    }

    xf86DrvMsg(pVia->scrnIndex, X_INFO,
	       "[drm] Initialized AGP ring-buffer, size 0x%lx at AGP offset 0x%lx.\n",
	       ringBufInit.size, ringBufInit.offset);
    pVia->CommandBufferActive = TRUE;

    return TRUE;
}

/*
 *
 */
void
VIADRMCommandBufferStop(VIAPtr pVia)
{
    drm_via_dma_init_t ringBufInit;

    VIAFUNC(pVia);

    if (!pVia->CommandBufferActive)
	xf86DrvMsg(pVia->scrnIndex, X_ERROR,
		   "%s: Command buffer not active!!!\n", __func__);

    ringBufInit.func = VIA_CLEANUP_DMA;
    if (drmCommandWrite(pVia->drmFD, DRM_VIA_DMA_INIT, &ringBufInit,
			sizeof(ringBufInit))) {
	xf86DrvMsg(pVia->scrnIndex, X_WARNING,
		   "[drm] Failed to clean up DMA ring-buffer: %d\n", errno);
    }

    pVia->CommandBufferActive = FALSE;
}

/*
 *
 */
void
VIADRICloseScreen(ScrnInfoPtr pScrn, ScreenPtr pScreen)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaDri *pDri = pVia->Dri;
    VIADRIPtr pVIADRI;

    if (pVia->CommandBufferActive)
	VIADRMCommandBufferStop(pVia);

    if (pDri->agpSize) {
	drmUnmap(pDri->agpMappedAddr, pDri->agpSize);
	drmRmMap(pVia->drmFD, pDri->agpHandle);
	drmAgpUnbind(pVia->drmFD, pDri->agpHandle);
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] Freeing agp memory\n");
	drmAgpFree(pVia->drmFD, pDri->agpHandle);
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] Releasing agp module\n");
	drmAgpRelease(pVia->drmFD);
    }

    DRICloseScreen(pScreen);

    if (pDri->pDRIInfo) {
	if ((pVIADRI = (VIADRIPtr) pDri->pDRIInfo->devPrivate)) {
	    VIADRIIrqExit(pVia, pVIADRI);
	    free(pVIADRI);
    	    pDri->pDRIInfo->devPrivate = NULL;
	}
	DRIDestroyInfoRec(pDri->pDRIInfo);
	pDri->pDRIInfo = NULL;
    }

    if (pDri->pVisualConfigs) {
	free(pDri->pVisualConfigs);
	pDri->pVisualConfigs = NULL;
    }
    if (pDri->pVisualConfigsPriv) {
	free(pDri->pVisualConfigsPriv);
	pDri->pVisualConfigsPriv = NULL;
    }

    free(pDri);
    pVia->Dri = NULL;
}

/*
 *
 */
static Bool
VIAInitVisualConfigs(ScrnInfoPtr pScrn)
{
    struct ViaDri *pDri = VIAPTR(pScrn)->Dri;
    __GLXvisualConfig *pConfigs = 0;
    VIAConfigPrivPtr pVIAConfigs = 0;
    VIAConfigPrivPtr *pVIAConfigPtrs = 0;
    int numConfigs = 0;
    int i, db, stencil, accum;

    switch (pScrn->bitsPerPixel) {
	case 8:
	case 24:
	    break;
	case 16:
	    numConfigs = 12;
	    if (!(pConfigs = (__GLXvisualConfig*)calloc(sizeof(__GLXvisualConfig),
						   numConfigs)))
		return FALSE;
	    if (!(pVIAConfigs = (VIAConfigPrivPtr)calloc(sizeof(VIAConfigPrivRec),
						    numConfigs))) {
		free(pConfigs);
    		return FALSE;
	    }
	    if (!(pVIAConfigPtrs = (VIAConfigPrivPtr*)calloc(sizeof(VIAConfigPrivPtr),
							  numConfigs))) {
		free(pConfigs);
		free(pVIAConfigs);
    		return FALSE;
	    }
	    for (i=0; i<numConfigs; i++)
    		pVIAConfigPtrs[i] = &pVIAConfigs[i];

	    i = 0;
	    for (accum = 0; accum <= 1; accum++) {
		/* 32bpp depth buffer disabled, as Mesa has limitations */
    		for (stencil=0; stencil<=2; stencil++) {
    		    for (db = 0; db <= 1; db++) {
			pConfigs[i].vid = -1;
			pConfigs[i].class = -1;
			pConfigs[i].rgba = TRUE;
			pConfigs[i].redSize = -1;
			pConfigs[i].greenSize = -1;
			pConfigs[i].blueSize = -1;
			pConfigs[i].redMask = -1;
			pConfigs[i].greenMask = -1;
			pConfigs[i].blueMask = -1;
			pConfigs[i].alphaSize = 0;
			pConfigs[i].alphaMask = 0;

			if (accum) {
			    pConfigs[i].accumRedSize = 16;
			    pConfigs[i].accumGreenSize = 16;
			    pConfigs[i].accumBlueSize = 16;
			    pConfigs[i].accumAlphaSize = 0;
			}
			else {
			    pConfigs[i].accumRedSize = 0;
			    pConfigs[i].accumGreenSize = 0;
			    pConfigs[i].accumBlueSize = 0;
			    pConfigs[i].accumAlphaSize = 0;
			}
			if (!db)
			    pConfigs[i].doubleBuffer = TRUE;
			else
			    pConfigs[i].doubleBuffer = FALSE;

			pConfigs[i].stereo = FALSE;
			pConfigs[i].bufferSize = -1;

			switch (stencil) {
			    case 0:
	    			pConfigs[i].depthSize = 24;
	    			pConfigs[i].stencilSize = 8;
	    			break;
			    case 1:
	    			pConfigs[i].depthSize = 16;
	    			pConfigs[i].stencilSize = 0;
	    			break;
			    case 2:
	    			pConfigs[i].depthSize = 0;
	    			pConfigs[i].stencilSize = 0;
	    			break;
			    case 3:
	    			pConfigs[i].depthSize = 32;
	    			pConfigs[i].stencilSize = 0;
	    			break;
			}

			pConfigs[i].auxBuffers = 0;
			pConfigs[i].level = 0;
			if (accum) {
			    pConfigs[i].visualRating = GLX_SLOW_VISUAL_EXT;
			} else {
			    pConfigs[i].visualRating = GLX_NONE_EXT;
			}
			pConfigs[i].transparentPixel = GLX_NONE_EXT;
			pConfigs[i].transparentRed = 0;
			pConfigs[i].transparentGreen = 0;
			pConfigs[i].transparentBlue = 0;
			pConfigs[i].transparentAlpha = 0;
			pConfigs[i].transparentIndex = 0;
			i++;
		    }
    		}
	    }

	if (i != numConfigs) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		"[dri] Incorrect initialization of visuals.  Disabling DRI.\n");
    		return FALSE;
	}
	break;

	case 32:
	    numConfigs = 12;
	    if (!(pConfigs = (__GLXvisualConfig*)calloc(sizeof(__GLXvisualConfig),
						   numConfigs)))
		return FALSE;
	    if (!(pVIAConfigs = (VIAConfigPrivPtr)calloc(sizeof(VIAConfigPrivRec),
						    numConfigs))) {
		free(pConfigs);
    		return FALSE;
	    }
	    if (!(pVIAConfigPtrs = (VIAConfigPrivPtr*)calloc(sizeof(VIAConfigPrivPtr),
							  numConfigs))) {
		free(pConfigs);
		free(pVIAConfigs);
    		return FALSE;
	    }
	    for (i=0; i<numConfigs; i++)
    		pVIAConfigPtrs[i] = &pVIAConfigs[i];

	    i = 0;
	    for (accum = 0; accum <= 1; accum++) {
		/* 32bpp depth buffer disabled, as Mesa has limitations */
    		for (stencil=0; stencil<=2; stencil++) {
    		    for (db = 0; db <= 1; db++) {
			pConfigs[i].vid = -1;
			pConfigs[i].class = -1;
			pConfigs[i].rgba = TRUE;
			pConfigs[i].redSize = -1;
			pConfigs[i].greenSize = -1;
			pConfigs[i].blueSize = -1;
			pConfigs[i].redMask = -1;
			pConfigs[i].greenMask = -1;
			pConfigs[i].blueMask = -1;
			pConfigs[i].alphaSize = 8;
			pConfigs[i].alphaMask = 0xFF000000;

			if (accum) {
			    pConfigs[i].accumRedSize = 16;
			    pConfigs[i].accumGreenSize = 16;
			    pConfigs[i].accumBlueSize = 16;
			    pConfigs[i].accumAlphaSize = 16;
			}
			else {
			    pConfigs[i].accumRedSize = 0;
			    pConfigs[i].accumGreenSize = 0;
			    pConfigs[i].accumBlueSize = 0;
			    pConfigs[i].accumAlphaSize = 0;
			}
			if (!db)
			    pConfigs[i].doubleBuffer = TRUE;
			else
			    pConfigs[i].doubleBuffer = FALSE;

			pConfigs[i].stereo = FALSE;
			pConfigs[i].bufferSize = -1;

			switch (stencil) {
			    case 0:
	    			pConfigs[i].depthSize = 24;
	    			pConfigs[i].stencilSize = 8;
	    			break;
			    case 1:
	    			pConfigs[i].depthSize = 16;
	    			pConfigs[i].stencilSize = 0;
	    			break;
			    case 2:
	    			pConfigs[i].depthSize = 0;
	    			pConfigs[i].stencilSize = 0;
	    			break;
			    case 3:
	    			pConfigs[i].depthSize = 32;
	    			pConfigs[i].stencilSize = 0;
	    			break;
			}

			pConfigs[i].auxBuffers = 0;
			pConfigs[i].level = 0;
			if (accum)
			    pConfigs[i].visualRating = GLX_SLOW_VISUAL_EXT;
			else
			    pConfigs[i].visualRating = GLX_NONE_EXT;
			pConfigs[i].transparentPixel = GLX_NONE_EXT;
			pConfigs[i].transparentRed = 0;
			pConfigs[i].transparentGreen = 0;
			pConfigs[i].transparentBlue = 0;
			pConfigs[i].transparentAlpha = 0;
			pConfigs[i].transparentIndex = 0;
			i++;
		    }
    		}
	    }

	if (i != numConfigs) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		"[dri] Incorrect initialization of visuals.  Disabling DRI.\n");
    		return FALSE;
	}

	break;
    }

    pDri->numVisualConfigs = numConfigs;
    pDri->pVisualConfigs = pConfigs;
    pDri->pVisualConfigsPriv = pVIAConfigs;
    GlxSetVisualConfigs(numConfigs, pConfigs, (void**)pVIAConfigPtrs);

    return TRUE;
}

/*
 * Get DRM version, and do some checks.
 */
static void
ViaDRMGetVersion(VIAPtr pVia)
{
    struct ViaDri *pDri = pVia->Dri;
    drmVersionPtr drmVersion = drmGetVersion(pVia->drmFD);

    VIAFUNC(pVia);

    if (!drmVersion) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR, "[drm] unable to retrieve DRM"
		   " version.\n");
	pDri->drmVersion = 0;
    } else {
	xf86DrvMsg(pVia->scrnIndex, X_INFO,
		   "[drm] using DRM driver version %d.%d.%d (%s)\n",
		   drmVersion->version_major, drmVersion->version_minor,
		   drmVersion->version_patchlevel, drmVersion->date);

	pDri->drmVersion = (drmVersion->version_major << 16) |
	    (drmVersion->version_minor << 8) | drmVersion->version_patchlevel;
    }

    /* Check wether we can use DMA for copying */
    if (pVia->UseDMACopy) {
#ifdef __linux__
#ifdef DRM_VIA_DMA_BLIT
	if (pDri->drmVersion >= 0x020705)
	    xf86DrvMsg(pVia->scrnIndex, X_INFO, "[drm] Using DMA for "
		       "copying to FB\n");
	else {
	    pVia->UseDMACopy = FALSE;
	    xf86DrvMsg(pVia->scrnIndex, X_WARNING, "[drm] Not using DMA for "
		       "copying to FB (drm driver older than 2.7.5).\n");
	}
#else
	pVia->UseDMACopy = FALSE;
	xf86DrvMsg(pVia->scrnIndex, X_INFO, "[drm] Not using DMA for "
		   "copying to FB (via_drm.h is too old).\n");
#endif /* DRM_VIA_DMA_BLIT */
#else
	pVia->UseDMACopy = FALSE;
	xf86DrvMsg(pVia->scrnIndex, X_INFO, "[drm] Not using DMA for "
		   "copying to FB (only present in linux drm).\n");
#endif /* __linux__ */
    }
}

/*
 * TODO: xserver receives driver's swapping event and does something
 *       according the data initialized in this function.
 */
static Bool
VIACreateContext(ScreenPtr pScreen, VisualPtr visual,
		 drm_context_t hwContext, void *pVisualConfigPriv,
		 DRIContextType contextStore)
{
    return TRUE;
}

/*
 *
 */
static void
VIADestroyContext(ScreenPtr pScreen, drm_context_t hwContext,
	   DRIContextType contextStore)
{
    ;
}

/*
 *
 */
static void
VIADRISwapContext(ScreenPtr pScreen, DRISyncType syncType,
		  DRIContextType oldContextType, void *oldContext,
		  DRIContextType newContextType, void *newContext)
{
  /*ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
  VIAPtr pVia = VIAPTR(pScrn);
  */
  return;
}

/*
 *
 */
static void
VIADRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index)
{
  /*ScreenPtr pScreen = pWin->drawable.pScreen;
  ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
  VIAPtr pVia = VIAPTR(pScrn);
  */
  return;
}

/*
 *
 */
static void
VIADRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg,
		  RegionPtr prgnSrc, CARD32 index)
{
  /*ScreenPtr pScreen = pParent->drawable.pScreen;
  ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
  VIAPtr pVia = VIAPTR(pScrn);
  */
  return;
}

/*
 * Add a map for the MMIO registers
 */
static Bool
VIADRIMapInit(VIAPtr pVia)
{
    struct ViaDri *pDri = pVia->Dri;
    int flags = DRM_READ_ONLY;

    if (drmAddMap(pVia->drmFD, pVia->MmioBase, VIA_MMIO_REGSIZE,
		  DRM_REGISTERS, flags, &pDri->registerHandle) < 0) {
	return FALSE;
    }

    xf86DrvMsg(pVia->scrnIndex, X_INFO, "[drm] register handle = 0x%08lx\n",
	       (unsigned long) pDri->registerHandle);

    return TRUE;
}

/*
 *
 */
Bool
VIADRIScreenInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaDri *pDri;
    DRIInfoPtr pDRIInfo;
    VIADRIPtr pVIADRI;

    VIAFUNC(pScrn);

    switch (pVia->Chipset) {
    case VT3122:
    case VT7205:
    case VT3108:
	break;
    default:
	xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "%s: %s is not supported.\n",
		   __func__, pScrn->chipset);
	return FALSE;
    }

    /* Check that the GLX, DRI, and DRM modules have been loaded by testing
    * for canonical symbols in each module. */
    if (!xf86LoaderCheckSymbol("GlxSetVisualConfigs")) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,\
		   "%s: Missing Symbol \"GlxSetVisualConfigs\"\n", __func__);
	return FALSE;
    }
    if (!xf86LoaderCheckSymbol("DRIScreenInit")) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,\
		   "%s: Missing Symbol \"DRIScreenInit\"\n", __func__);
	return FALSE;
    }
    if (!xf86LoaderCheckSymbol("drmAvailable")) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,\
		   "%s: Missing Symbol \"drmAvailable\"\n", __func__);
	return FALSE;
    }
    if (!xf86LoaderCheckSymbol("DRIQueryVersion")) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		 "[dri] VIADRIScreenInit failed (libdri.a too old)\n");
	return FALSE;
    }

    /* Check the DRI version */
    {
	int major, minor, patch;
	DRIQueryVersion(&major, &minor, &patch);
	if ((major != DRIINFO_MAJOR_VERSION) || (minor < DRIINFO_MINOR_VERSION)) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		       "DRI version mismatch. Disabling DRI.\n");
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		       "libDRI %d.%d.%d is present while %d.%d.0 is needed.\n",
		       major, minor, patch, DRIINFO_MAJOR_VERSION,
		       DRIINFO_MINOR_VERSION);
	    return FALSE;
	}
    }

    pDri = xnfcalloc(1, sizeof(struct ViaDri));
    pDri->scrnIndex = pVia->scrnIndex;

    pDri->pDRIInfo = DRICreateInfoRec();
    if (!pDri->pDRIInfo) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,\
		   "%s: DRICreateInfoRec failed.\n", __func__);
	free(pDri);
	return FALSE;
    }

    pDRIInfo = pDri->pDRIInfo;
    pDRIInfo->drmDriverName = VIAKernelDriverName;
    pDRIInfo->clientDriverName = VIAClientDriverName;

    pDRIInfo->busIdString = malloc(64);
    sprintf(pDRIInfo->busIdString, "PCI:%d:%d:%d", PCI_BUS(pVia->PciInfo),
	    PCI_DEV(pVia->PciInfo), PCI_FUNC(pVia->PciInfo));

    pDRIInfo->ddxDriverMajorVersion = VIA_DRIDDX_VERSION_MAJOR;
    pDRIInfo->ddxDriverMinorVersion = VIA_DRIDDX_VERSION_MINOR;
    pDRIInfo->ddxDriverPatchVersion = VIA_DRIDDX_VERSION_PATCH;

#if DRIINFO_MAJOR_VERSION < 5
    pDRIInfo->frameBufferPhysicalAddress = pVia->FrameBufferBase;
#else
    pDRIInfo->frameBufferPhysicalAddress = (pointer) pVia->FrameBufferBase;
#endif

    pDRIInfo->frameBufferSize = pScrn->videoRam << 10;

    pDRIInfo->frameBufferStride = (pScrn->displayWidth *
					    pScrn->bitsPerPixel / 8);
    pDRIInfo->ddxDrawableTableEntry = VIA_MAX_DRAWABLES;

    if (SAREA_MAX_DRAWABLES < VIA_MAX_DRAWABLES)
	pDRIInfo->maxDrawableTableEntry = SAREA_MAX_DRAWABLES;
    else
	pDRIInfo->maxDrawableTableEntry = VIA_MAX_DRAWABLES;

#ifdef NOT_DONE
    /* FIXME need to extend DRI protocol to pass this size back to client
    * for SAREA mapping that includes a device private record
    */
    pDRIInfo->SAREASize =
	((sizeof(XF86DRISAREARec) + 0xfff) & 0x1000); /* round to page */
    /* + shared memory device private rec */
#else
    /* For now the mapping works by using a fixed size defined
    * in the SAREA header
    */
    if ((sizeof(XF86DRISAREARec) + sizeof(drm_via_sarea_t)) > SAREA_MAX) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Data does not fit in SAREA\n");
	DRIDestroyInfoRec(pDri->pDRIInfo);
	free(pDri);
	return FALSE;
    }
    pDRIInfo->SAREASize = SAREA_MAX;
#endif

    if (!(pVIADRI = (VIADRIPtr)calloc(sizeof(VIADRIRec),1))) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "%s: failed to allocate pVIADRI.\n", __func__);
	DRIDestroyInfoRec(pDri->pDRIInfo);
	free(pDri);
	return FALSE;
    }
    pDRIInfo->devPrivate = pVIADRI;
    pDRIInfo->devPrivateSize = sizeof(VIADRIRec);
    pDRIInfo->contextSize = sizeof(VIADRIContextRec);

    pDRIInfo->CreateContext = VIACreateContext;
    pDRIInfo->DestroyContext = VIADestroyContext;
    pDRIInfo->SwapContext = VIADRISwapContext;
    pDRIInfo->InitBuffers = VIADRIInitBuffers;
    pDRIInfo->MoveBuffers = VIADRIMoveBuffers;
    pDRIInfo->bufferRequests = DRI_ALL_WINDOWS;

    if (!DRIScreenInit(pScreen, pDRIInfo, &pVia->drmFD)) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
	    "[dri] DRIScreenInit failed.  Disabling DRI.\n");
	free(pDRIInfo->devPrivate);
	pDRIInfo->devPrivate = NULL;
	DRIDestroyInfoRec(pDri->pDRIInfo);
	free(pDri);
	pVia->drmFD = -1;
	return FALSE;
    }

    pVia->Dri = pDri;

    if (!(VIAInitVisualConfigs(pScrn))) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "%s: VIAInitVisualConfigs failed.\n", __func__);
	VIADRICloseScreen(pScrn, pScreen);
	return FALSE;
    }
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[dri] visual configs initialized.\n" );

    /* DRIScreenInit doesn't add all the common mappings.  Add additional mappings here. */
    if (!VIADRIMapInit(pVia)) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "%s: VIADRIMapInit failed.\n", __func__);
	VIADRICloseScreen(pScrn, pScreen);
	return FALSE;
    }
    pVIADRI->regs.size = VIA_MMIO_REGSIZE;
    pVIADRI->regs.handle = pDri->registerHandle;
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] mmio Registers = 0x%08lx\n",
	       (unsigned long) pVIADRI->regs.handle);

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[dri] mmio mapped.\n" );

    ViaDRMGetVersion(pVia);

    return TRUE;
}

/*
 *
 */
static Bool
VIADRIAgpInit(VIAPtr pVia)
{
    struct ViaDri *pDri = pVia->Dri;
    DRIInfoPtr pDRIInfo = pDri->pDRIInfo;
    VIADRIPtr pVIADRI = pDRIInfo->devPrivate;
    drmAddress agpaddr;
    unsigned long agp_phys;
    CARD32 agp_mode;
    CARD32 agp_mode_bits;

    pDri->agpSize = 0;

    if (drmAgpAcquire(pVia->drmFD) < 0) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR, "[drm] drmAgpAcquire failed %d\n", errno);
	return FALSE;
    }

    /* AGPv3 breaks the easy route to do this - in older versions of AGP, the
     * fastest mode bit set chooses your mode. In v3, setting
     * (AGP_MODE_V3_8x | AGP_MODE_V3_4x) is reserved, so aim to choose the
     * fastest v3 speed possible. */
    agp_mode = drmAgpGetMode(pVia->drmFD);
    agp_mode_bits = agp_mode & AGP_MODE_BITS;
    agp_mode &= ~AGP_MODE_BITS;

    if (agp_mode_bits & AGP_MODE_V3_8x)
	agp_mode_bits = AGP_MODE_V3_8x;
    else if (agp_mode_bits & AGP_MODE_V3_4x)
	agp_mode_bits = AGP_MODE_V3_4x;

    agp_mode |= agp_mode_bits;

    if (drmAgpEnable(pVia->drmFD, agp_mode) < 0) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR, "[drm] drmAgpEnable failed\n");
	return FALSE;
    }

    xf86DrvMsg(pVia->scrnIndex, X_INFO, "[drm] drmAgpEnabled succeeded\n");

    if (drmAgpAlloc(pVia->drmFD, AGP_SIZE, 0, &agp_phys, &pDri->agpHandle) < 0) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR, "[drm] drmAgpAlloc failed\n");
	drmAgpRelease(pVia->drmFD);
	return FALSE;
    }

    if (drmAgpBind(pVia->drmFD, pDri->agpHandle, 0) < 0) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR, "[drm] drmAgpBind failed\n");
	drmAgpFree(pVia->drmFD, pDri->agpHandle);
	drmAgpRelease(pVia->drmFD);

	return FALSE;
    }

    /*
     * Place the ring-buffer last in the AGP region, and restrict the
     * public map not to include the buffer for security reasons.
     */
    pDri->agpSize = AGP_SIZE - AGP_CMDBUF_SIZE;
    pDri->agpAddr = drmAgpBase(pVia->drmFD);
    xf86DrvMsg(pVia->scrnIndex, X_INFO, "[drm] agpAddr = 0x%08lx\n",
	       pDri->agpAddr);

    pVIADRI->agp.size = pDri->agpSize;
    if (drmAddMap(pVia->drmFD, (drm_handle_t)0,
		  pVIADRI->agp.size, DRM_AGP, 0,
		  &pVIADRI->agp.handle) < 0) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR,
		   "[drm] Failed to map public agp area\n");
	pVIADRI->agp.size = 0;
	drmAgpUnbind(pVia->drmFD, pDri->agpHandle);
	drmAgpFree(pVia->drmFD, pDri->agpHandle);
	drmAgpRelease(pVia->drmFD);
	return FALSE;
    }

    /* Map AGP from kernel to Xserver - Not really needed */
    /* Why are we not passing agpMapped more directly? -- Luc */
    drmMap(pVia->drmFD, pVIADRI->agp.handle, pVIADRI->agp.size, &agpaddr);
    pDri->agpMappedAddr = agpaddr;

    xf86DrvMsg(pVia->scrnIndex, X_INFO,
	       "[drm] agpBase = %p\n", pDri->agpBase);
    xf86DrvMsg(pVia->scrnIndex, X_INFO,
	       "[drm] agpAddr = 0x%08lx\n", pDri->agpAddr);
    xf86DrvMsg(pVia->scrnIndex, X_INFO,
	       "[drm] agpSize = 0x%08x\n", pDri->agpSize);
    xf86DrvMsg(pVia->scrnIndex, X_INFO,
	       "[drm] agp physical addr = 0x%08lx\n", agp_phys);

    {
	drm_via_agp_t agp;
	agp.offset = 0;
	agp.size = AGP_SIZE-AGP_CMDBUF_SIZE;
	if (drmCommandWrite(pVia->drmFD, DRM_VIA_AGP_INIT, &agp,
			    sizeof(drm_via_agp_t)) < 0) {
	    drmUnmap(agpaddr, pDri->agpSize);
	    drmRmMap(pVia->drmFD, pVIADRI->agp.handle);
	    drmAgpUnbind(pVia->drmFD, pDri->agpHandle);
	    drmAgpFree(pVia->drmFD, pDri->agpHandle);
	    drmAgpRelease(pVia->drmFD);
	    return FALSE;
	}
    }

    return TRUE;
}

/*
 * This function claims whatever FB memory is still left.
 */
static Bool
VIADRIFBInit(ScrnInfoPtr pScrn)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaDri *pDri = pVia->Dri;

    /* Guesswork: make sure we at least have 1 page? This should be sane-ish,
     * even though a single page will be pretty much useless */
    if (pVia->FBFreeSize < 4096) {
	xf86DrvMsg(pVia->scrnIndex, X_ERROR,
		   "%s: Not enough FB memory left to use DRI.\n", __func__);
	return FALSE;
    }

    {
	VIADRIPtr pVIADRI = pDri->pDRIInfo->devPrivate;

	pVIADRI->fbOffset = 0;
	pVIADRI->fbSize = pScrn->videoRam << 10;
    }

    {
	drm_via_fb_t fb;
	fb.offset = pVia->FBFreeStart;
	fb.size = pVia->FBFreeSize;

	if (drmCommandWrite(pVia->drmFD, DRM_VIA_FB_INIT, &fb,
			    sizeof(drm_via_fb_t)) < 0) {
	    xf86DrvMsg(pVia->scrnIndex, X_ERROR,
		       "[drm] failed to init frame buffer area\n");
	    return FALSE;
	} else {
	    xf86DrvMsg(pVia->scrnIndex, X_INFO,
		       "[drm] Using FB at 0x%08x (0x%08x)\n",
		       pVia->FBFreeStart, pVia->FBFreeSize);
	    pVia->FBFreeStart = -1;
	    pVia->FBFreeSize = 0;
	    return TRUE;
	}
    }
}

/*
 * Initialize the kernel data structures.
 */
static Bool
VIADRIKernelInit(VIAPtr pVia)
{
    struct ViaDri *pDri = pVia->Dri;
    drm_via_init_t drmInfo;

    memset(&drmInfo, 0, sizeof(drm_via_init_t));

    drmInfo.func = VIA_INIT_MAP;
    drmInfo.sarea_priv_offset = sizeof(XF86DRISAREARec);
    drmInfo.fb_offset = pVia->FrameBufferBase;
    drmInfo.mmio_offset = pDri->registerHandle;

    if (pDri->IsPCI)
	drmInfo.agpAddr = (unsigned long) NULL;
    else
	drmInfo.agpAddr = pDri->agpAddr;

    if ((drmCommandWrite(pVia->drmFD, DRM_VIA_MAP_INIT, &drmInfo,
			 sizeof(drm_via_init_t))) < 0)
	return FALSE;

    return TRUE;
}

/*
 *
 */
Bool
VIADRIFinishScreenInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
{
    VIAPtr pVia = VIAPTR(pScrn);
    struct ViaDri *pDri = pVia->Dri;
    VIADRIPtr pVIADRI;

    pDri->pDRIInfo->driverSwapMethod = DRI_HIDE_X_CONTEXT;

    pDri->IsPCI = !VIADRIAgpInit(pVia);
    if (!pDri->IsPCI)
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Initialised AGP.\n" );
    else
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[dri] using PCI.\n" );

    if (!(VIADRIFBInit(pScrn))) {
	VIADRICloseScreen(pScrn, pScreen);
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "[dri] frame buffer initialization failed.\n" );
	return FALSE;
    }

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[dri] frame buffer initialized.\n" );

    DRIFinishScreenInit(pScreen);

    if (!VIADRIKernelInit(pVia)) {
	VIADRICloseScreen(pScrn, pScreen);
	return FALSE;
    }
    xf86DrvMsg(pScreen->myNum, X_INFO, "[dri] kernel data initialized.\n");

    /* set SAREA value */
    {
	drm_via_sarea_t *saPriv;

	saPriv = (drm_via_sarea_t *) DRIGetSAREAPrivate(pScreen);
	assert(saPriv);
	memset(saPriv, 0, sizeof(*saPriv));
	saPriv->ctxOwner = -1;
    }

    pVIADRI = (VIADRIPtr) pDri->pDRIInfo->devPrivate;
    pVIADRI->deviceID = pVia->Chipset;
    pVIADRI->width = pScrn->virtualX;
    pVIADRI->height = pScrn->virtualY;
    pVIADRI->mem = pScrn->videoRam << 10;
    pVIADRI->bytesPerPixel = (pScrn->bitsPerPixel + 7) / 8;
    pVIADRI->sarea_priv_offset = sizeof(XF86DRISAREARec);
    /* TODO */
    pVIADRI->scrnX = pVIADRI->width;
    pVIADRI->scrnY = pVIADRI->height;

    /* Initialize IRQ */
    if (pVia->DRIIrqEnable)
	VIADRIIrqInit(pVia, pVIADRI);

    if (pVia->UseCommandBuffer)
	VIADRMCommandBufferStart(pVia);

    return TRUE;
}