summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/xfree86/drivers/mga/mga_dri.c
blob: ce3d45611ce757a2941d9d03338114d0ecb00ef5 (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
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/mga/mga_dri.c,v 1.12 2000/11/13 23:31:39 dawes Exp $ */

#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86_ansic.h"
#include "xf86Priv.h"

#include "xf86PciInfo.h"
#include "xf86Pci.h"
#define PSZ 8
#include "cfb.h"
#undef PSZ
#include "cfb16.h"
#include "cfb32.h"

#include "miline.h"

#include "GL/glxtokens.h"

#include "mga_bios.h"
#include "mga_reg.h"
#include "mga.h"
#include "mga_macros.h"
#include "mga_dri.h"
#include "mga_wrap.h"

static char MGAKernelDriverName[] = "mga";
static char MGAClientDriverName[] = "mga";

static Bool MGAInitVisualConfigs(ScreenPtr pScreen);
static Bool MGACreateContext(ScreenPtr pScreen, VisualPtr visual, 
			      drmContext hwContext, void *pVisualConfigPriv,
			      DRIContextType contextStore);
static void MGADestroyContext(ScreenPtr pScreen, drmContext hwContext,
			       DRIContextType contextStore);
static void MGADRISwapContext(ScreenPtr pScreen, DRISyncType syncType, 
			       DRIContextType readContextType, 
			       void *readContextStore,
			       DRIContextType writeContextType, 
			       void *writeContextStore);
static void MGADRISwapContext_shared(ScreenPtr pScreen, DRISyncType syncType, 
			       DRIContextType readContextType, 
			       void *readContextStore,
			       DRIContextType writeContextType, 
			       void *writeContextStore);
extern void Mga8DRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index);
extern void Mga8DRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg, 
			       RegionPtr prgnSrc, CARD32 index);
extern void Mga16DRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index);
extern void Mga16DRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg, 
			       RegionPtr prgnSrc, CARD32 index);
extern void Mga24DRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index);
extern void Mga24DRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg, 
			       RegionPtr prgnSrc, CARD32 index);
extern void Mga32DRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index);
extern void Mga32DRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg, 
			       RegionPtr prgnSrc, CARD32 index);

Bool MgaCleanupDma(ScrnInfoPtr pScrn)
{
   MGAPtr pMGA = MGAPTR(pScrn);
   Bool ret_val;

   ret_val = drmMgaCleanupDma(pMGA->drmSubFD);
   if (ret_val == FALSE)
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "Mga Dma Cleanup Failed\n");

   return ret_val;
}

Bool MgaLockUpdate(ScrnInfoPtr pScrn, drmLockFlags flags)
{
   MGAPtr pMGA = MGAPTR(pScrn);
   Bool ret_val;

   ret_val = drmMgaLockUpdate(pMGA->drmSubFD, flags);
   if (ret_val == FALSE)
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "LockUpdate failed\n");

   return ret_val;
}

Bool MgaInitDma(ScrnInfoPtr pScrn, int prim_size)
{
   MGAPtr pMGA = MGAPTR(pScrn);
   MGADRIPtr pMGADRI = (MGADRIPtr)pMGA->pDRIInfo->devPrivate;
   MGADRIServerPrivatePtr pMGADRIServer = pMGA->DRIServerInfo;
   drmMgaInit init;
   Bool ret_val;
   
   memset(&init, 0, sizeof(drmMgaInit));
   init.reserved_map_agpstart = 0;
   init.reserved_map_idx = 3;
   init.buffer_map_idx = 4;
   init.sarea_priv_offset = sizeof(XF86DRISAREARec);
   init.primary_size = prim_size;
   init.warp_ucode_size = pMGADRIServer->warp_ucode_size;

   switch(pMGA->Chipset) {
   case PCI_CHIP_MGAG400:
      init.chipset = MGA_CARD_TYPE_G400;
      break;
   case PCI_CHIP_MGAG200:
   case PCI_CHIP_MGAG200_PCI:
      init.chipset = MGA_CARD_TYPE_G200;
      break;
   default:
      return FALSE;
   }

   init.frontOffset = pMGADRI->frontOffset;
   init.backOffset = pMGADRI->backOffset;
   init.depthOffset = pMGADRI->depthOffset;
   init.textureOffset = pMGADRI->textureOffset;
   init.textureSize = pMGADRI->textureSize;
   init.agpTextureSize = pMGADRI->agpTextureSize;
   init.cpp = pMGADRI->cpp;
   init.stride = pMGADRI->frontPitch;
   init.mAccess = pMGA->MAccess;
   init.sgram = !pMGA->HasSDRAM;
   
   memcpy(&init.WarpIndex, &pMGADRIServer->WarpIndex, 
	  sizeof(drmMgaWarpIndex) * MGA_MAX_WARP_PIPES);

   xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] Mga Dma Initialization start\n");

   ret_val = drmMgaInitDma(pMGA->drmSubFD, &init);
   if (ret_val == FALSE)
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "[drm] Mga Dma Initialization Failed\n");
   else
      xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] Mga Dma Initialization done\n");
   return ret_val;
}

static Bool
MGAInitVisualConfigs(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   MGAPtr pMGA = MGAPTR(pScrn);
   int numConfigs = 0;
   __GLXvisualConfig *pConfigs = 0;
   MGAConfigPrivPtr pMGAConfigs = 0;
   MGAConfigPrivPtr *pMGAConfigPtrs = 0;
   int i, db, depth, stencil, accum;

   switch (pScrn->bitsPerPixel) {
   case 8:
   case 24:
      break;
   case 16:
      numConfigs = 8;

      if (!(pConfigs = (__GLXvisualConfig*)xnfcalloc(sizeof(__GLXvisualConfig),
						     numConfigs))) {
	 return FALSE;
      }
      if (!(pMGAConfigs = (MGAConfigPrivPtr)xnfcalloc(sizeof(MGAConfigPrivRec),
						      numConfigs))) {
	 xfree(pConfigs);
	 return FALSE;
      }
      if (!(pMGAConfigPtrs = (MGAConfigPrivPtr*)xnfcalloc(sizeof(MGAConfigPrivPtr),
							  numConfigs))) {
	 xfree(pConfigs);
	 xfree(pMGAConfigs);
	 return FALSE;
      }
      for (i=0; i<numConfigs; i++) 
	 pMGAConfigPtrs[i] = &pMGAConfigs[i];

      i = 0;
      depth = 1;
      for (accum = 0; accum <= 1; accum++) {
         for (stencil = 0; stencil <= 1; stencil++) { /* no stencil for now */
            for (db=1; db>=0; db--) {
               pConfigs[i].vid = -1;
               pConfigs[i].class = -1;
               pConfigs[i].rgba = TRUE;
               pConfigs[i].redSize = 5;
               pConfigs[i].greenSize = 6;
               pConfigs[i].blueSize = 5;
               pConfigs[i].alphaSize = 0;
               pConfigs[i].redMask = 0x0000F800;
               pConfigs[i].greenMask = 0x000007E0;
               pConfigs[i].blueMask = 0x0000001F;
               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 = 16;
               if (depth)
                  pConfigs[i].depthSize = 16;
               else 
                  pConfigs[i].depthSize = 0;
               if (stencil)
                  pConfigs[i].stencilSize = 8;
               else
                  pConfigs[i].stencilSize = 0;
               pConfigs[i].auxBuffers = 0;
               pConfigs[i].level = 0;
               if (stencil || accum)
                  pConfigs[i].visualRating = GLX_SLOW_VISUAL_EXT;
               else
                  pConfigs[i].visualRating = GLX_NONE_EXT;
               pConfigs[i].transparentPixel = 0;
               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,
                    "[drm] Incorrect initialization of visuals\n");
         return FALSE;
      }
      break;

   case 32:
      numConfigs = 8;

      if (!(pConfigs = (__GLXvisualConfig*)xnfcalloc(sizeof(__GLXvisualConfig),
						     numConfigs))) {
	 return FALSE;
      }
      if (!(pMGAConfigs = (MGAConfigPrivPtr)xnfcalloc(sizeof(MGAConfigPrivRec),
						      numConfigs))) {
	 xfree(pConfigs);
	 return FALSE;
      }
      if (!(pMGAConfigPtrs = (MGAConfigPrivPtr*)xnfcalloc(sizeof(MGAConfigPrivPtr),
							  numConfigs))) {
	 xfree(pConfigs);
	 xfree(pMGAConfigs);
	 return FALSE;
      }
      for (i=0; i<numConfigs; i++) 
	 pMGAConfigPtrs[i] = &pMGAConfigs[i];

      i = 0;
      depth = 1;
      for (accum = 0; accum <= 1; accum++) {
         for (stencil = 0; stencil <= 1; stencil++) { 
            for (db=1; db>=0; db--) {
               pConfigs[i].vid = -1;
               pConfigs[i].class = -1;
               pConfigs[i].rgba = TRUE;
               pConfigs[i].redSize = 8;
               pConfigs[i].greenSize = 8;
               pConfigs[i].blueSize = 8;
               pConfigs[i].alphaSize = 0;
               pConfigs[i].redMask   = 0x00FF0000;
               pConfigs[i].greenMask = 0x0000FF00;
               pConfigs[i].blueMask  = 0x000000FF;
               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 = 32;
               if (depth)
		  if (stencil) 
		     pConfigs[i].depthSize = 24;
		  else
		     pConfigs[i].depthSize = 32;
               else 
                  pConfigs[i].depthSize = 0;
               if (stencil)
                  pConfigs[i].stencilSize = 8;
               else
                  pConfigs[i].stencilSize = 0;
               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 = 0;
               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,
                    "[drm] Incorrect initialization of visuals\n");
         return FALSE;
      }
      break;

   default:
      ;  /* unexpected bits/pixelx */
   }
   pMGA->numVisualConfigs = numConfigs;
   pMGA->pVisualConfigs = pConfigs;
   pMGA->pVisualConfigsPriv = pMGAConfigs;
   GlxSetVisualConfigs(numConfigs, pConfigs, (void**)pMGAConfigPtrs);
   return TRUE;
}

static unsigned int mylog2(unsigned int n)
{
   unsigned int log2 = 1;
   while (n>1) n >>= 1, log2++;
   return log2;
}

static unsigned long MGAParseAgpMode(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   MGAPtr pMga = MGAPTR(pScrn);
   unsigned long mode_mask;

   switch(pMga->agp_mode) {
   case 4:
      mode_mask = ~0x00000003;
      break;
   case 2:
      if (pMga->Chipset == PCI_CHIP_MGAG200) {
	 xf86DrvMsg(pScreen->myNum, X_INFO, 
		    "[drm] Enabling AGP 2x pll encoding\n");
	 OUTREG(MGAREG_AGP_PLL, AGP_PLL_agp2xpllen_enable);
      }
      mode_mask = ~0x00000005;
      break;
   default:
   /* Default to 1X agp mode */
   case 1:
      if (pMga->Chipset == PCI_CHIP_MGAG200) {
	 xf86DrvMsg(pScreen->myNum, X_INFO, 
		    "[drm] Disabling AGP 2x pll encoding\n");
	 OUTREG(MGAREG_AGP_PLL, AGP_PLL_agp2xpllen_disable);
      }
      pMga->agp_mode = 1;
      mode_mask = ~0x00000006;
      break;
   }

   return mode_mask;
}

Bool MGADRIScreenInit(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   MGAPtr pMGA = MGAPTR(pScrn);
   DRIInfoPtr pDRIInfo;
   MGADRIPtr pMGADRI;
   MGADRIServerPrivatePtr pMGADRIServer;
   int bufs, size;
   int prim_size;
   int init_offset;
   int i;
   unsigned long mode_mask;

   switch(pMGA->Chipset) {
   case PCI_CHIP_MGAG400:
   case PCI_CHIP_MGAG200:
#if 0
   case PCI_CHIP_MGAG200_PCI:
#endif
      break;
   default:
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "[drm] Direct rendering only supported with G200/G400 AGP\n");
      return FALSE;
   }

   /* Check that the GLX, DRI, and DRM modules have been loaded by testing
    * for canonical symbols in each module. */
   if (!xf86LoaderCheckSymbol("GlxSetVisualConfigs")) return FALSE;
   if (!xf86LoaderCheckSymbol("DRIScreenInit"))       return FALSE;
   if (!xf86LoaderCheckSymbol("drmAvailable"))        return FALSE;
   if (!xf86LoaderCheckSymbol("DRIQueryVersion")) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "MGADRIScreenInit failed (libdri.a too old)\n");
      return FALSE;
   }
     
   /* Check the DRI version */
   {
      int major, minor, patch;
      DRIQueryVersion(&major, &minor, &patch);
      if (major != 3 || minor != 0 || patch < 0) {
         xf86DrvMsg(pScreen->myNum, X_ERROR,
                    "[drm] MGADRIScreenInit failed (DRI version = %d.%d.%d, expected 3.0.x).  Disabling DRI.\n",
                    major, minor, patch);
         return FALSE;
      }
   }

   xf86DrvMsg(pScreen->myNum, X_INFO, "[drm] bpp: %d depth: %d\n", pScrn->bitsPerPixel, pScrn->depth);

   if ((pScrn->bitsPerPixel / 8) != 2 &&
       (pScrn->bitsPerPixel / 8) != 4) {
      xf86DrvMsg(pScreen->myNum, X_INFO,
                 "[drm] Direct rendering only supported in 16 and 32 bpp modes\n");
      return FALSE;
   }
   
   pDRIInfo = DRICreateInfoRec();
   if (!pDRIInfo)
      return FALSE;
   pMGA->pDRIInfo = pDRIInfo;

   pDRIInfo->drmDriverName = MGAKernelDriverName;
   pDRIInfo->clientDriverName = MGAClientDriverName;
   pDRIInfo->busIdString = xalloc(64);
   sprintf(pDRIInfo->busIdString, "PCI:%d:%d:%d",
           ((pciConfigPtr)pMGA->PciInfo->thisCard)->busnum,
           ((pciConfigPtr)pMGA->PciInfo->thisCard)->devnum,
           ((pciConfigPtr)pMGA->PciInfo->thisCard)->funcnum);
   pDRIInfo->ddxDriverMajorVersion = MGA_MAJOR_VERSION;
   pDRIInfo->ddxDriverMinorVersion = MGA_MINOR_VERSION;
   pDRIInfo->ddxDriverPatchVersion = MGA_PATCHLEVEL;
   pDRIInfo->frameBufferPhysicalAddress = pMGA->FbAddress;
   pDRIInfo->frameBufferSize = pMGA->FbMapSize;
   pDRIInfo->frameBufferStride = pScrn->displayWidth*(pScrn->bitsPerPixel/8);
   pDRIInfo->ddxDrawableTableEntry = MGA_MAX_DRAWABLES;

   MGADRIWrapFunctions( pScreen, pDRIInfo );

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

   /* For now the mapping works by using a fixed size defined
    * in the SAREA header
    */
   if (sizeof(XF86DRISAREARec)+sizeof(MGASAREARec)>SAREA_MAX) {
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "[drm] Data does not fit in SAREA\n");
      return FALSE;
   }
  
   xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] Sarea %d+%d: %d\n",
              sizeof(XF86DRISAREARec), sizeof(MGASAREARec),
              sizeof(XF86DRISAREARec) + sizeof(MGASAREARec));

   pDRIInfo->SAREASize = SAREA_MAX;

   if (!(pMGADRI = (MGADRIPtr)xnfcalloc(sizeof(MGADRIRec),1))) {
      DRIDestroyInfoRec(pMGA->pDRIInfo);
      pMGA->pDRIInfo=0;
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                 "[drm] Failed to allocate memory for private record\n");
      return FALSE;
   }
   if (!(pMGADRIServer = (MGADRIServerPrivatePtr)
	 xnfcalloc(sizeof(MGADRIServerPrivateRec),1))) {
      xfree(pMGADRI);
      DRIDestroyInfoRec(pMGA->pDRIInfo);
      pMGA->pDRIInfo=0;
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                 "[drm] Failed to allocate memory for private record\n");
      return FALSE;
   }

   pDRIInfo->devPrivate = pMGADRI;
   pMGA->DRIServerInfo = pMGADRIServer;
   pDRIInfo->devPrivateSize = sizeof(MGADRIRec);
   pDRIInfo->contextSize = sizeof(MGADRIContextRec);

   pDRIInfo->CreateContext = MGACreateContext;
   pDRIInfo->DestroyContext = MGADestroyContext;
   if (xf86IsEntityShared(pScrn->entityList[0]))
      pDRIInfo->SwapContext = MGADRISwapContext_shared;
   else
      pDRIInfo->SwapContext = MGADRISwapContext;
  
   switch( pScrn->bitsPerPixel ) {
   case 8:
       pDRIInfo->InitBuffers = Mga8DRIInitBuffers;
       pDRIInfo->MoveBuffers = Mga8DRIMoveBuffers;
   case 16:
       pDRIInfo->InitBuffers = Mga16DRIInitBuffers;
       pDRIInfo->MoveBuffers = Mga16DRIMoveBuffers;
   case 24:
       pDRIInfo->InitBuffers = Mga24DRIInitBuffers;
       pDRIInfo->MoveBuffers = Mga24DRIMoveBuffers;
   case 32:
       pDRIInfo->InitBuffers = Mga32DRIInitBuffers;
       pDRIInfo->MoveBuffers = Mga32DRIMoveBuffers;
   }
   
   pDRIInfo->bufferRequests = DRI_ALL_WINDOWS;

   if (!DRIScreenInit(pScreen, pDRIInfo, &pMGA->drmSubFD)) {
      xfree(pMGADRIServer);
      pMGA->DRIServerInfo = 0;
      xfree(pDRIInfo->devPrivate);
      pDRIInfo->devPrivate = 0;
      DRIDestroyInfoRec(pMGA->pDRIInfo);
      pMGA->pDRIInfo = 0;
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[drm] DRIScreenInit Failed\n");
      return FALSE;
   }

   /* Check the MGA DRM version */
   {
      drmVersionPtr version = drmGetVersion(pMGA->drmSubFD);
      if (version) {
         if (version->version_major != 2 ||
             version->version_minor != 0 ||
             version->version_patchlevel < 0) {
            /* incompatible drm version */
            xf86DrvMsg(pScreen->myNum, X_ERROR,
                       "[drm] MGADRIScreenInit failed (DRM version = %d.%d.%d, expected 2.0.x).  Disabling DRI.\n",
                       version->version_major,
                       version->version_minor,
                       version->version_patchlevel);
/*              MGADRICloseScreen(pScreen); */
	    
            drmFreeVersion(version);
            return FALSE;
         }
         drmFreeVersion(version);
      }
   }

   pMGADRIServer->regsSize = MGAIOMAPSIZE;
   if (drmAddMap(pMGA->drmSubFD, (drmHandle)pMGA->IOAddress, 
                 pMGADRIServer->regsSize, DRM_REGISTERS, 0, 
                 &pMGADRIServer->regs)<0) {
      DRICloseScreen(pScreen);
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] drmAddMap failed Register MMIO region\n");
      return FALSE;
   }
   xf86DrvMsg(pScreen->myNum, X_INFO, "[drm] Registers = 0x%08lx\n",
              pMGADRIServer->regs);
   
   /* Agp Support */
   pMGADRIServer->agpAcquired = FALSE;
   pMGADRIServer->agpHandle = 0;
   pMGADRIServer->agpSizep = 0;
   pMGADRIServer->agp_map = 0;
   
   if (drmAgpAcquire(pMGA->drmSubFD) < 0) {
      DRICloseScreen(pScreen);
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[drm] drmAgpAcquire failed\n");
      return FALSE;
   }
   pMGADRIServer->agpAcquired = TRUE;

   pMGADRIServer->warp_ucode_size = mgaGetMicrocodeSize(pScreen);
   if (pMGADRIServer->warp_ucode_size == 0) {
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[drm] microcodeSize is zero\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   mode_mask = MGAParseAgpMode(pScreen);

   pMGADRIServer->agpMode = drmAgpGetMode(pMGA->drmSubFD);
   pMGADRIServer->agpMode &= mode_mask;
   if (drmAgpEnable(pMGA->drmSubFD, pMGADRIServer->agpMode) < 0) {
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[drm] drmAgpEnable failed\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }
   ErrorF("[drm] drmAgpEnabled succeeded for AGP mode %dx\n", pMGA->agp_mode);

   prim_size = 65536;
   init_offset = ((prim_size + pMGADRIServer->warp_ucode_size + 
		  4096 - 1) / 4096) * 4096;
   
   pMGADRIServer->agpSizep = init_offset;
   pMGADRI->agpSize = (drmAgpSize(pMGA->drmSubFD)) - init_offset;

   pMGADRIServer->agpBase = (drmAddress) drmAgpBase(pMGA->drmSubFD);
   if (drmAddMap(pMGA->drmSubFD, 0,
                 init_offset, DRM_AGP, 0, 
                 &pMGADRIServer->agp_private) < 0) {
      DRICloseScreen(pScreen);
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] drmAddMap failed on AGP aperture\n");
      return FALSE;
   }
   
   if (drmMap(pMGA->drmSubFD, (drmHandle)pMGADRIServer->agp_private,
              init_offset, 
              (drmAddressPtr)&pMGADRIServer->agp_map) < -1) {
      DRICloseScreen(pScreen);
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] drmMap failed on AGP aperture\n");
      return FALSE;
   }
   
   /* Now allocate and bind a default of 8 megs */
   drmAgpAlloc(pMGA->drmSubFD, 0x00800000, 0, 0,
               &pMGADRIServer->agpHandle);
   
   if (pMGADRIServer->agpHandle == 0) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] drmAgpAlloc failed\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }
   
   if (drmAgpBind(pMGA->drmSubFD, pMGADRIServer->agpHandle, 0) < 0) {
      DRICloseScreen(pScreen);
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] drmAgpBind failed\n");
      return FALSE;
   }

   mgaInstallMicrocode(pScreen, prim_size);

   if (drmAddMap(pMGA->drmSubFD, (drmHandle)init_offset,
                 pMGADRI->agpSize, DRM_AGP, 0, 
                 &pMGADRI->agp) < 0) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] Failed to map public agp area\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   switch(pMGA->Chipset) {
   case PCI_CHIP_MGAG400:
      pMGADRI->chipset = MGA_CARD_TYPE_G400;
      break;
   case PCI_CHIP_MGAG200:
   case PCI_CHIP_MGAG200_PCI:
      pMGADRI->chipset = MGA_CARD_TYPE_G200;
      break;
   default:
      return FALSE;
   }
   
   pMGADRI->width = pScrn->virtualX;
   pMGADRI->height = pScrn->virtualY;
   pMGADRI->mem = pScrn->videoRam * 1024;
   pMGADRI->cpp = pScrn->bitsPerPixel / 8;
   pMGADRI->frontPitch = pScrn->displayWidth * (pScrn->bitsPerPixel / 8);


   pMGADRI->frontOffset = 0; /* pMGA->YDstOrg * (pScrn->bitsPerPixel / 8) */
   pMGADRI->backOffset = ((pScrn->virtualY + pMGA->numXAALines + 1) * 
			  pScrn->displayWidth *
                          pMGADRI->cpp + 4095) & ~0xFFF;


   xf86DrvMsg(pScreen->myNum, X_INFO, "[drm] calced backoffset: 0x%x\n",
              pMGADRI->backOffset);


   size = pMGADRI->cpp * pScrn->virtualX * pScrn->virtualY;
   size += 4095;
   size &= ~4095;
   pMGADRI->depthOffset = pMGA->FbUsableSize - size;
   pMGADRI->depthOffset &= ~4095;  
   pMGADRI->textureOffset = pMGADRI->backOffset + size;
   pMGADRI->textureSize = pMGADRI->depthOffset - pMGADRI->textureOffset;

   if (pMGADRI->depthOffset < pMGADRI->textureOffset + 512*1024) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] Insufficient memory for direct rendering\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   pMGADRI->mAccess = pMGA->MAccess;

   i = mylog2(pMGADRI->textureSize / MGA_NR_TEX_REGIONS);
   if (i < MGA_LOG_MIN_TEX_REGION_SIZE)
      i = MGA_LOG_MIN_TEX_REGION_SIZE;
  
   pMGADRI->logTextureGranularity = i;
   pMGADRI->textureSize = (pMGADRI->textureSize >> i) << i; /* truncate */


   /* Here is where we need to do initialization of the dma engine */
   if((bufs = drmAddBufs(pMGA->drmSubFD,
                         MGA_DMA_BUF_NR,
                         MGA_DMA_BUF_SZ,
                         DRM_AGP_BUFFER,
                         init_offset)) <= 0) {
     xf86DrvMsg(pScrn->scrnIndex, X_INFO,
                "[drm] failure adding %d %d byte DMA buffers\n",
                MGA_DMA_BUF_NR,
                MGA_DMA_BUF_SZ);
     DRICloseScreen(pScreen);
     return FALSE;
   }

   pMGADRI->agpBufferOffset = init_offset + pMGADRIServer->agp_private;

   /* Calculate texture constants for AGP texture space
    */
   {
      CARD32 agpTextureOffset = MGA_DMA_BUF_SZ * MGA_DMA_BUF_NR;
      CARD32 agpTextureSize = pMGADRI->agpSize - agpTextureOffset;

      i = mylog2(agpTextureSize / MGA_NR_TEX_REGIONS);
      if (i < MGA_LOG_MIN_TEX_REGION_SIZE)
         i = MGA_LOG_MIN_TEX_REGION_SIZE;

      pMGADRI->logAgpTextureGranularity = i;
      pMGADRI->agpTextureSize = (agpTextureSize >> i) << i; 
      pMGADRI->agpTextureOffset = agpTextureOffset;
   }

   
   xf86DrvMsg(pScrn->scrnIndex, X_INFO,
              "[drm] added %d %d byte DMA buffers\n",
              bufs, MGA_DMA_BUF_SZ);


   if ((MgaInitDma(pScrn, prim_size)) != TRUE) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                 "[drm] Failed to initialize dma engine\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   xf86DrvMsg(pScreen->myNum, X_INFO, "[drm] Initialized Dma Engine\n");
   
   if (!pMGADRIServer->irq) {
      pMGADRIServer->irq = drmGetInterruptFromBusID(pMGA->drmSubFD,
                                            ((pciConfigPtr)pMGA->PciInfo
					     ->thisCard)->busnum,
					    ((pciConfigPtr)pMGA->PciInfo
					     ->thisCard)->devnum,
					    ((pciConfigPtr)pMGA->PciInfo
					     ->thisCard)->funcnum);

      if(!pMGADRIServer->irq && !pMGA->ReallyUseIrqZero) {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[drm] Your graphics card has Interrupt zero"
		    " assigned to it.\n"
		    "This is highly unlikely so I'm disabling the DRI.\n"
		    "If your graphics card really has Interrupt zero, please"
		    "add the config option UseIrqZero\n"
		    "to the device section in your XF86Config file.\n"
		    "Please be warned that Interrupt zero is normally "
		    "the timer interrupt on X86 systems.\n"
		    "Using this option could make your system unusable.\n"
		    "The more likely solution is that your graphics card has"
		    " no interrupt assigned to it.\nPlease consult your"
		    " system BIOS manual for instructions on how to enable "
		    "an interrupt for your graphics card.\n");
	 MGADRICloseScreen(pScreen);
	 return FALSE;
      }
      drmCtlInstHandler(pMGA->drmSubFD, pMGADRIServer->irq);
   }

   xf86DrvMsg(pScrn->scrnIndex, X_INFO,
              "[drm] dma control initialized, using IRQ %d\n",
              pMGADRIServer->irq);


   if (!(MGAInitVisualConfigs(pScreen))) {
      DRICloseScreen(pScreen);
      return FALSE;
   }
   xf86DrvMsg(pScrn->scrnIndex, X_INFO, "visual configs initialized\n" );

   return TRUE;
}

void
MGADRICloseScreen(ScreenPtr pScreen)
{
  ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
  MGAPtr pMGA = MGAPTR(pScrn);
  MGADRIServerPrivatePtr pMGADRIServer = pMGA->DRIServerInfo;

/* The DRI will automagically clean these up when driFD is closed */
  if(pMGADRIServer->agp_map) {  
     drmUnmap(pMGADRIServer->agp_map, pMGADRIServer->agpSizep);
     pMGADRIServer->agp_map = 0;
  }
  if(pMGADRIServer->agpHandle) {
     pMGADRIServer->agpHandle = 0;
     pMGADRIServer->agpSizep = 0;
  }
  if(pMGADRIServer->agpAcquired == TRUE) {
     pMGADRIServer->agpAcquired = FALSE;
  }

  DRICloseScreen(pScreen);

  if (pMGA->pDRIInfo) {
    if (pMGA->pDRIInfo->devPrivate) {
      xfree(pMGA->pDRIInfo->devPrivate);
      pMGA->pDRIInfo->devPrivate = 0;
    }
    DRIDestroyInfoRec(pMGA->pDRIInfo);
    pMGA->pDRIInfo = 0;
  }
  if(pMGA->DRIServerInfo) {
     xfree(pMGA->DRIServerInfo);
     pMGA->DRIServerInfo = 0;
  }
  if (pMGA->pVisualConfigs) {
    xfree(pMGA->pVisualConfigs);
  }
  if (pMGA->pVisualConfigsPriv) { 
    xfree(pMGA->pVisualConfigsPriv);
  }
}

static Bool
MGACreateContext(ScreenPtr pScreen, VisualPtr visual, 
		  drmContext hwContext, void *pVisualConfigPriv,
		  DRIContextType contextStore)
{
  return TRUE;
}

static void
MGADestroyContext(ScreenPtr pScreen, drmContext hwContext, 
		   DRIContextType contextStore)
{
}

Bool
MGADRIFinishScreenInit(ScreenPtr pScreen)
{
  ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
  MGASAREAPtr sPriv;
  MGAPtr pMGA = MGAPTR(pScrn);

  if (!pMGA->pDRIInfo) return FALSE;

  sPriv = (MGASAREAPtr)DRIGetSAREAPrivate(pScreen);
  pMGA->pDRIInfo->driverSwapMethod = DRI_HIDE_X_CONTEXT;

  xf86memset( sPriv, 0, sizeof(MGASAREARec) );

  return DRIFinishScreenInit(pScreen);
}


void
mgaGetQuiescence( ScrnInfoPtr pScrn )
{
   MGAPtr pMga = MGAPTR(pScrn);

   pMga->have_quiescense = 1;					

   if (pMga->directRenderingEnabled) {
      MGAFBLayout *pLayout = &pMga->CurrentLayout;

      MgaLockUpdate(pScrn, (DRM_LOCK_QUIESCENT | DRM_LOCK_FLUSH));	

      WAITFIFO(11);
      OUTREG(MGAREG_MACCESS, pMga->MAccess);
      OUTREG(MGAREG_PITCH, pLayout->displayWidth);
      pMga->PlaneMask = ~0;
      OUTREG(MGAREG_PLNWT, pMga->PlaneMask);
      pMga->BgColor = 0;
      pMga->FgColor = 0;
      OUTREG(MGAREG_BCOL, pMga->BgColor);
      OUTREG(MGAREG_FCOL, pMga->FgColor);
      OUTREG(MGAREG_SRCORG, pMga->realSrcOrg);
      pMga->SrcOrg = 0;
      OUTREG(MGAREG_DSTORG, pMga->DstOrg);
      OUTREG(MGAREG_OPMODE, MGAOPM_DMA_BLIT);
      OUTREG(MGAREG_CXBNDRY, 0xFFFF0000); /* (maxX << 16) | minX */
      OUTREG(MGAREG_YTOP, 0x00000000);    /* minPixelPointer */
      OUTREG(MGAREG_YBOT, 0x007FFFFF);    /* maxPixelPointer */ 
      pMga->AccelFlags &= ~CLIPPER_ON;
   }
}


void
mgaGetQuiescence_shared( ScrnInfoPtr pScrn )
{
   MGAPtr pMga = MGAPTR(pScrn);
   MGAEntPtr pMgaEnt = pMga->entityPrivate;
   MGAPtr pMga2 = MGAPTR(pMgaEnt->pScrn_2);

   pMga = MGAPTR(pMgaEnt->pScrn_1);
   pMga->have_quiescense = 1;
   pMga2->have_quiescense = 1;
   
   if (pMgaEnt->directRenderingEnabled) {
      MgaLockUpdate(pMgaEnt->pScrn_1, (DRM_LOCK_QUIESCENT | DRM_LOCK_FLUSH));
      pMga->RestoreAccelState(pScrn);
      xf86SetLastScrnFlag(pScrn->entityList[0], pScrn->scrnIndex);
   }
}
   

void
MGASwapContext(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   MGAPtr pMga = MGAPTR(pScrn);

   /* Arrange for dma_quiescence and xaa sync to be called as
    * appropriate.
    */
   pMga->have_quiescense = 0;
   pMga->AccelInfoRec->NeedToSync = TRUE;
}

void
MGASwapContext_shared(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   MGAPtr pMga = MGAPTR(pScrn);
   MGAEntPtr pMgaEnt = pMga->entityPrivate;
   MGAPtr pMga2 = MGAPTR(pMgaEnt->pScrn_2);

   pMga = MGAPTR(pMgaEnt->pScrn_1);
   pMga->have_quiescense = 0;
   pMga->AccelInfoRec->NeedToSync = TRUE;
   pMga2->have_quiescense = 0;
   pMga2->AccelInfoRec->NeedToSync = TRUE;
}


/* This is really only called from validate/postvalidate as we
 * override the dri lock/unlock.  Want to remove validate/postvalidate
 * processing, but need to remove all client-side use of drawable lock
 * first (otherwise there is noone recover when a client dies holding
 * the drawable lock).
 *
 * What does this mean? 
 *
 *   - The above code gets executed every time a
 *     window changes shape or the focus changes, which isn't really
 *     optimal.  
 *   - The X server therefore believes it needs to do an XAA sync
 *     *and* a dma quiescense ioctl each time that happens.
 *
 * We don't wrap wakeuphandler any longer, so at least we can say that
 * this doesn't happen *every time the mouse moves*...
 */
static void
MGADRISwapContext(ScreenPtr pScreen, DRISyncType syncType, 
		   DRIContextType oldContextType, void *oldContext,
		   DRIContextType newContextType, void *newContext)
{
   if (syncType == DRI_3D_SYNC && 
       oldContextType == DRI_2D_CONTEXT &&
       newContextType == DRI_2D_CONTEXT)
   {
      MGASwapContext(pScreen);
   }
}

static void
MGADRISwapContext_shared(ScreenPtr pScreen, DRISyncType syncType, 
			 DRIContextType oldContextType, void *oldContext,
			 DRIContextType newContextType, void *newContext)
{   
   if (syncType == DRI_3D_SYNC && 
       oldContextType == DRI_2D_CONTEXT &&
       newContextType == DRI_2D_CONTEXT)
   {
      MGASwapContext_shared(pScreen);
   }
}

void 
MGASelectBuffer(ScrnInfoPtr pScrn, int which)
{
   MGAPtr pMga = MGAPTR(pScrn);
   MGADRIPtr pMGADRI = (MGADRIPtr)pMga->pDRIInfo->devPrivate;

   switch (which) {
   case MGA_BACK:
      OUTREG(MGAREG_DSTORG, pMGADRI->backOffset);
      OUTREG(MGAREG_SRCORG, pMGADRI->backOffset);
      break;
   case MGA_DEPTH:
      OUTREG(MGAREG_DSTORG, pMGADRI->depthOffset);
      OUTREG(MGAREG_SRCORG, pMGADRI->depthOffset);
      break;
   default:
   case MGA_FRONT:
      OUTREG(MGAREG_DSTORG, pMGADRI->frontOffset);
      OUTREG(MGAREG_SRCORG, pMGADRI->frontOffset);
      break;
   }
}