summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/hw/xfree86/drivers/mga/mga_dri.c
blob: 7f91b6630686d5653286d4adf64a13715d102a9f (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
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/mga/mga_dri.c,v 1.1 2000/02/11 17:25:55 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);
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:
      init.chipset = MGA_CARD_TYPE_G200;
      break;
   case PCI_CHIP_MGAG200_PCI:
   default:
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                 "[drm] Direct rendering not supported on this card/chipset\n");
      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:
   case 32:
      break;
   case 16:
      numConfigs = 4;

      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 <= 0; stencil++) { /* no stencil for now */
            for (db=0; db<=1; 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].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)
                  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;
}


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;

#if XFree86LOADER
   /* Check that the GLX, DRI, and DRM modules have been loaded by testing
    * for canonical symbols in each module. */
   if (!LoaderSymbol("GlxSetVisualConfigs")) return FALSE;
   if (!LoaderSymbol("DRIScreenInit"))       return FALSE;
   if (!LoaderSymbol("drmAvailable"))        return FALSE;
#endif   
     
   /* 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;
      }
   }

   if ((pScrn->bitsPerPixel / 8) != 2) {
      xf86DrvMsg(pScreen->myNum, X_INFO,
                 "[drm] Direct Rendering only supported in 16 bpp mode\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;
   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 != 1 ||
             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 1.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;
   }
   
   pMGADRIServer->agpMode = drmAgpGetMode(pMGA->drmSubFD);
   /* Default to 1X agp mode */
   pMGADRIServer->agpMode &= ~0x00000002;
   if (drmAgpEnable(pMGA->drmSubFD, pMGADRIServer->agpMode) < 0) {
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[drm] drmAgpEnable failed\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }
   ErrorF("[drm] drmAgpEnabled succeeded\n");

   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:
      pMGADRI->chipset = MGA_CARD_TYPE_G200;
      break;
   case PCI_CHIP_MGAG200_PCI:
   default:
      xf86DrvMsg(pScreen->myNum, X_ERROR,
                "[drm] Direct rendering not supported on this card/chipset\n");
      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+129) * pScrn->displayWidth *
                          (pScrn->bitsPerPixel / 8) + 4095) & ~0xFFF;


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


#if 0
   size = 2 * pScrn->virtualX * pScrn->virtualY;
   pMGADRI->depthOffset = (pMGADRI->backOffset + size + 4095) & ~0xFFF;
   pMGADRI->textureOffset = pMGADRI->depthOffset + size;
   pMGADRI->textureSize = pMGA->FbUsableSize - pMGADRI->textureOffset;
#else
   size = 2 * 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;
   }
#endif

   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);
      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;

  MgaCleanupDma(pScrn);

  if(pMGADRIServer->agp_map) {  
     ErrorF("Unmapped agp region\n");
     drmUnmap(pMGADRIServer->agp_map, pMGADRIServer->agpSizep);
     pMGADRIServer->agp_map = 0;
  }
  if(pMGADRIServer->agpHandle) {
     ErrorF("Freeing agp memory\n");
     drmAgpFree(pMGA->drmSubFD, pMGADRIServer->agpHandle);
     pMGADRIServer->agpHandle = 0;
     pMGADRIServer->agpSizep = 0;
  }
  if(pMGADRIServer->agpAcquired == TRUE) {
     ErrorF("releasing agp module\n");
     drmAgpRelease(pMGA->drmSubFD);
     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(12);
      OUTREG(MGAREG_MACCESS, pMga->MAccess);
      OUTREG(MGAREG_PITCH, pLayout->displayWidth);
      OUTREG(MGAREG_YDSTORG, pMga->YDstOrg);
      OUTREG(MGAREG_PLNWT, pMga->PlaneMask);
      OUTREG(MGAREG_BCOL, pMga->BgColor);
      OUTREG(MGAREG_FCOL, pMga->FgColor);
      OUTREG(MGAREG_SRCORG, pMga->SrcOrg);
      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 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;
}



/* 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);
   }
}


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;
   }
}