i810_dri.c 47.2 KB
Newer Older
1
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/i810/i810_dri.c,v 1.41 2003/09/24 02:43:23 dawes Exp $ */
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Reformatted with GNU indent (2.2.8), using the following options:
 *
 *    -bad -bap -c41 -cd0 -ncdb -ci6 -cli0 -cp0 -ncs -d0 -di3 -i3 -ip3 -l78
 *    -lp -npcs -psl -sob -ss -br -ce -sc -hnl
 *
 * This provides a good match with the original i810 code and preferred
 * XFree86 formatting conventions.
 *
 * When editing this driver, please follow the existing formatting, and edit
 * with <TAB> characters expanded at 8-column intervals.
 */

15 16 17 18
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

19 20 21
#include <errno.h>
#include <stdio.h>
#include <string.h>
22
#include <assert.h>
23

24 25 26 27 28 29 30 31
#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86Priv.h"

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

#include "windowstr.h"
32 33
#include "shadow.h"
#include "shadowfb.h"
34 35 36 37 38 39 40 41 42 43 44

#include "GL/glxtokens.h"

#include "i810.h"
#include "i810_dri.h"

static char I810KernelDriverName[] = "i810";
static char I810ClientDriverName[] = "i810";

static Bool I810InitVisualConfigs(ScreenPtr pScreen);
static Bool I810CreateContext(ScreenPtr pScreen, VisualPtr visual,
Eric Anholt's avatar
Eric Anholt committed
45
			      drm_context_t hwContext, void *pVisualConfigPriv,
46
			      DRIContextType contextStore);
Eric Anholt's avatar
Eric Anholt committed
47
static void I810DestroyContext(ScreenPtr pScreen, drm_context_t hwContext,
48 49 50 51 52 53 54 55 56 57
			       DRIContextType contextStore);
static void I810DRISwapContext(ScreenPtr pScreen, DRISyncType syncType,
			       DRIContextType readContextType,
			       void *readContextStore,
			       DRIContextType writeContextType,
			       void *writeContextStore);
static void I810DRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index);
static void I810DRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg,
			       RegionPtr prgnSrc, CARD32 index);

58 59 60 61 62 63 64 65 66 67

static void I810EnablePageFlip(ScreenPtr pScreen);
static void I810DisablePageFlip(ScreenPtr pScreen);
static void I810DRITransitionSingleToMulti3d(ScreenPtr pScreen);
static void I810DRITransitionMultiToSingle3d(ScreenPtr pScreen);
static void I810DRITransitionTo3d(ScreenPtr pScreen);
static void I810DRITransitionTo2d(ScreenPtr pScreen);

static void I810DRIRefreshArea(ScrnInfoPtr pScrn, int num, BoxPtr pbox);

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
extern void GlxSetVisualConfigs(int nconfigs,
				__GLXvisualConfig * configs,
				void **configprivs);

static int i810_pitches[] = {
   512,
   1024,
   2048,
   4096,
   0
};

static int i810_pitch_flags[] = {
   0x0,
   0x1,
   0x2,
   0x3,
   0
};

88 89
static unsigned int i810_drm_version = 0;

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
Bool
I810CleanupDma(ScrnInfoPtr pScrn)
{
   I810Ptr pI810 = I810PTR(pScrn);
   drmI810Init info;

   memset(&info, 0, sizeof(drmI810Init));
   info.func = I810_CLEANUP_DMA;

   if (drmCommandWrite(pI810->drmSubFD, DRM_I810_INIT,
		       &info, sizeof(drmI810Init))) {
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		 "[dri] I810 Dma Cleanup Failed\n");
      return FALSE;
   }

   return TRUE;
}

Bool
I810InitDma(ScrnInfoPtr pScrn)
{
   I810Ptr pI810 = I810PTR(pScrn);
Alan Hourihane's avatar
Alan Hourihane committed
113
   I810RingBuffer *ring = pI810->LpRing;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
   I810DRIPtr pI810DRI = (I810DRIPtr) pI810->pDRIInfo->devPrivate;
   drmI810Init info;

   memset(&info, 0, sizeof(drmI810Init));

   info.ring_start = ring->mem.Start;
   info.ring_end = ring->mem.End;
   info.ring_size = ring->mem.Size;
   info.mmio_offset = (unsigned int)pI810DRI->regs;
   info.buffers_offset = (unsigned int)pI810->buffer_map;
   info.sarea_priv_offset = sizeof(XF86DRISAREARec);

   info.front_offset = 0;
   info.back_offset = pI810->BackBuffer.Start;
   info.depth_offset = pI810->DepthBuffer.Start;
   info.overlay_offset = pI810->OverlayStart;
   info.overlay_physical = pI810->OverlayPhysical;
   info.w = pScrn->virtualX;
   info.h = pScrn->virtualY;
   info.pitch = pI810->auxPitch;
   info.pitch_bits = pI810->auxPitchBits;

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
   /* We require DRM v1.2 or greater. Since DRM v1.2 broke compatibility
    * we created a new v1.4 that supports a new init function. Eventually the
    * old init function will go away. If you change the drm interface, make a
    * new init type too so that we can detect the new client.
    */
   switch(i810_drm_version) {
   case ((1<<16) | 0):
   case ((1<<16) | 1):
   case ((1<<16) | 2):
   case ((1<<16) | 3):
      /* Use OLD drm < 1.4 init */
      info.func = I810_INIT_DMA;
      xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] Init PRE v1.4 interface.\n");
      break;
   default:
   case ((1<<16) | 4):
      /*  DRM version 1.3 or greater init */
      info.func = I810_INIT_DMA_1_4;
      xf86DrvMsg(pScrn->scrnIndex, X_INFO, "[drm] Init v1.4 interface.\n");
      break;
   }

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
   if (drmCommandWrite(pI810->drmSubFD, DRM_I810_INIT,
		       &info, sizeof(drmI810Init))) {
      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		 "[drm] I810 Dma Initialization failed.\n");
      return FALSE;
   }

   return TRUE;
}

static Bool
I810InitVisualConfigs(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   I810Ptr pI810 = I810PTR(pScrn);
   int numConfigs = 0;
174 175 176
   __GLXvisualConfig *pConfigs = NULL;
   I810ConfigPrivPtr pI810Configs = NULL;
   I810ConfigPrivPtr *pI810ConfigPtrs = NULL;
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
   int accum, stencil, db, depth;
   int i;

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

      pConfigs =
	    (__GLXvisualConfig *) xcalloc(sizeof(__GLXvisualConfig),
					  numConfigs);
      if (!pConfigs)
	 return FALSE;

      pI810Configs =
	    (I810ConfigPrivPtr) xcalloc(sizeof(I810ConfigPrivRec),
					numConfigs);
      if (!pI810Configs) {
	 xfree(pConfigs);
	 return FALSE;
      }

      pI810ConfigPtrs =
	    (I810ConfigPrivPtr *) xcalloc(sizeof(I810ConfigPrivPtr),
					  numConfigs);
      if (!pI810ConfigPtrs) {
	 xfree(pConfigs);
	 xfree(pI810Configs);
	 return FALSE;
      }

      for (i = 0; i < numConfigs; i++)
	 pI810ConfigPtrs[i] = &pI810Configs[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 = 5;
	       pConfigs[i].greenSize = 6;
	       pConfigs[i].blueSize = 5;
225
	       pConfigs[i].alphaSize = 0;
226 227 228 229 230 231 232 233
	       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;
234
		  pConfigs[i].accumAlphaSize = 0;
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	       } else {
		  pConfigs[i].accumRedSize = 0;
		  pConfigs[i].accumGreenSize = 0;
		  pConfigs[i].accumBlueSize = 0;
		  pConfigs[i].accumAlphaSize = 0;
	       }
	       pConfigs[i].doubleBuffer = db ? TRUE : 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)
255
		  pConfigs[i].visualRating = GLX_SLOW_CONFIG;
256
	       else
257 258
		  pConfigs[i].visualRating = GLX_NONE;
	       pConfigs[i].transparentPixel = GLX_NONE;
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
	       pConfigs[i].transparentRed = 0;
	       pConfigs[i].transparentGreen = 0;
	       pConfigs[i].transparentBlue = 0;
	       pConfigs[i].transparentAlpha = 0;
	       pConfigs[i].transparentIndex = 0;
	       i++;
	    }
	 }
      }
      assert(i == numConfigs);
      break;
   }
   pI810->numVisualConfigs = numConfigs;
   pI810->pVisualConfigs = pConfigs;
   pI810->pVisualConfigsPriv = pI810Configs;
   GlxSetVisualConfigs(numConfigs, pConfigs, (void **)pI810ConfigPtrs);
   return TRUE;
}

static unsigned int
mylog2(unsigned int n)
{
   unsigned int log2 = 1;

   while (n > 1)
      n >>= 1, log2++;
   return log2;
}

Bool
I810DRIScreenInit(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   I810Ptr pI810 = I810PTR(pScrn);
   DRIInfoPtr pDRIInfo;
   I810DRIPtr pI810DRI;
   unsigned long tom;
296 297
   drm_handle_t agpHandle;
   drm_handle_t dcacheHandle;
298 299
   int sysmem_size = 0;
   int back_size = 0;
300
   unsigned int pitch_idx = 0;
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
   int bufs;
   int width = pScrn->displayWidth * pI810->cpp;
   int i;

   /* Hardware 3D rendering only implemented for 16bpp */
   /* And it only works for 5:6:5 (Mark) */
   if (pScrn->depth != 16)
      return FALSE;

   /* Check that the GLX, DRI, and DRM modules have been loaded by testing
    * for known symbols in each module. */
   if (!xf86LoaderCheckSymbol("GlxSetVisualConfigs"))
      return FALSE;
   if (!xf86LoaderCheckSymbol("drmAvailable"))
      return FALSE;
   if (!xf86LoaderCheckSymbol("DRIQueryVersion")) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[dri] I810DRIScreenInit failed (libdri.a too old)\n");
      return FALSE;
   }

   /* Check the DRI version */
   {
      int major, minor, patch;

      DRIQueryVersion(&major, &minor, &patch);
327
      if (major != DRIINFO_MAJOR_VERSION || minor < DRIINFO_MINOR_VERSION) {
328 329
	 xf86DrvMsg(pScreen->myNum, X_ERROR,
		    "[dri] I810DRIScreenInit failed because of a version mismatch.\n"
330 331 332
		    "[dri] libdri version is %d.%d.%d bug version %d.%d.x is needed.\n"
		    "[dri] Disabling DRI.\n", major, minor, patch,
                    DRIINFO_MAJOR_VERSION, DRIINFO_MINOR_VERSION);
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	 return FALSE;
      }
   }

   pDRIInfo = DRICreateInfoRec();
   if (!pDRIInfo) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[dri] DRICreateInfoRec failed.  Disabling DRI.\n");
      return FALSE;
   }

/*     pDRIInfo->wrap.ValidateTree = 0;    */
/*     pDRIInfo->wrap.PostValidateTree = 0;    */

   pI810->pDRIInfo = pDRIInfo;
   pI810->LockHeld = 0;

   pDRIInfo->drmDriverName = I810KernelDriverName;
   pDRIInfo->clientDriverName = I810ClientDriverName;
Eric Anholt's avatar
Eric Anholt committed
352 353 354 355 356
   if (xf86LoaderCheckSymbol("DRICreatePCIBusID")) {
      pDRIInfo->busIdString = DRICreatePCIBusID(pI810->PciInfo);
   } else {
      pDRIInfo->busIdString = xalloc(64);
      sprintf(pDRIInfo->busIdString, "PCI:%d:%d:%d",
357 358 359 360
#if XSERVER_LIBPCIACCESS
	      ((pI810->PciInfo->domain << 8) | pI810->PciInfo->bus),
	      pI810->PciInfo->dev, pI810->PciInfo->func
#else
Eric Anholt's avatar
Eric Anholt committed
361 362
	      ((pciConfigPtr) pI810->PciInfo->thisCard)->busnum,
	      ((pciConfigPtr) pI810->PciInfo->thisCard)->devnum,
363 364 365
	      ((pciConfigPtr) pI810->PciInfo->thisCard)->funcnum
#endif
	      );
Eric Anholt's avatar
Eric Anholt committed
366
   }
367 368 369
   pDRIInfo->ddxDriverMajorVersion = I810_MAJOR_VERSION;
   pDRIInfo->ddxDriverMinorVersion = I810_MINOR_VERSION;
   pDRIInfo->ddxDriverPatchVersion = I810_PATCHLEVEL;
Alan Hourihane's avatar
Alan Hourihane committed
370
   pDRIInfo->frameBufferPhysicalAddress = (pointer) pI810->LinearAddr;
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
   pDRIInfo->frameBufferSize = (((pScrn->displayWidth *
				  pScrn->virtualY * pI810->cpp) +
				 4096 - 1) / 4096) * 4096;

   pDRIInfo->frameBufferStride = pScrn->displayWidth * pI810->cpp;
   pDRIInfo->ddxDrawableTableEntry = I810_MAX_DRAWABLES;

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

   /* For now the mapping works by using a fixed size defined
    * in the SAREA header
    */
   if (sizeof(XF86DRISAREARec) + sizeof(I810SAREARec) > SAREA_MAX) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[dri] Data does not fit in SAREA\n");
      return FALSE;
   }
   pDRIInfo->SAREASize = SAREA_MAX;

   if (!(pI810DRI = (I810DRIPtr) xcalloc(sizeof(I810DRIRec), 1))) {
      DRIDestroyInfoRec(pI810->pDRIInfo);
395
      pI810->pDRIInfo = NULL;
396 397 398 399 400 401 402 403 404 405 406 407
      return FALSE;
   }
   pDRIInfo->devPrivate = pI810DRI;
   pDRIInfo->devPrivateSize = sizeof(I810DRIRec);
   pDRIInfo->contextSize = sizeof(I810DRIContextRec);

   pDRIInfo->CreateContext = I810CreateContext;
   pDRIInfo->DestroyContext = I810DestroyContext;
   pDRIInfo->SwapContext = I810DRISwapContext;
   pDRIInfo->InitBuffers = I810DRIInitBuffers;
   pDRIInfo->MoveBuffers = I810DRIMoveBuffers;
   pDRIInfo->bufferRequests = DRI_ALL_WINDOWS;
408 409 410 411
   pDRIInfo->TransitionTo2d = I810DRITransitionTo2d;
   pDRIInfo->TransitionTo3d = I810DRITransitionTo3d;
   pDRIInfo->TransitionSingleToMulti3D = I810DRITransitionSingleToMulti3d;
   pDRIInfo->TransitionMultiToSingle3D = I810DRITransitionMultiToSingle3d;
412 413 414 415 416 417 418 419 420 421 422

   pDRIInfo->createDummyCtx = TRUE;
   pDRIInfo->createDummyCtxPriv = FALSE;

   /* This adds the framebuffer as a drm map *before* we have asked agp
    * to allocate it.  Scary stuff, hold on...
    */
   if (!DRIScreenInit(pScreen, pDRIInfo, &pI810->drmSubFD)) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[dri] DRIScreenInit failed.  Disabling DRI.\n");
      xfree(pDRIInfo->devPrivate);
423
      pDRIInfo->devPrivate = NULL;
424
      DRIDestroyInfoRec(pI810->pDRIInfo);
425
      pI810->pDRIInfo = NULL;
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
      return FALSE;
   }

   /* Check the i810 DRM versioning */
   {
      drmVersionPtr version;

      /* Check the DRM lib version.
       * drmGetLibVersion was not supported in version 1.0, so check for
       * symbol first to avoid possible crash or hang.
       */
      if (xf86LoaderCheckSymbol("drmGetLibVersion")) {
	 version = drmGetLibVersion(pI810->drmSubFD);
      } else
      {
	 /* drmlib version 1.0.0 didn't have the drmGetLibVersion
	  * entry point.  Fake it by allocating a version record
	  * via drmGetVersion and changing it to version 1.0.0
	  */
	 version = drmGetVersion(pI810->drmSubFD);
	 version->version_major = 1;
	 version->version_minor = 0;
	 version->version_patchlevel = 0;
      }

#define REQ_MAJ 1
#define REQ_MIN 1
      if (version) {
	 if (version->version_major != REQ_MAJ ||
	     version->version_minor < REQ_MIN) {
	    /* incompatible drm library version */
	    xf86DrvMsg(pScreen->myNum, X_ERROR,
		       "[dri] I810DRIScreenInit failed because of a version mismatch.\n"
		       "[dri] libdrm.a module version is %d.%d.%d but version %d.%d.x is needed.\n"
		       "[dri] Disabling DRI.\n",
		       version->version_major,
		       version->version_minor, version->version_patchlevel,
		       REQ_MAJ, REQ_MIN);
	    drmFreeVersion(version);
	    I810DRICloseScreen(pScreen);
	    return FALSE;
	 }
	 drmFreeVersion(version);
      }

      /* Check the i810 DRM version */
      version = drmGetVersion(pI810->drmSubFD);
      if (version) {
474 475
	i810_drm_version = (version->version_major<<16) |
	                    version->version_minor;
476 477 478 479 480 481 482 483 484 485 486 487
	 if (version->version_major != 1 || version->version_minor < 2) {
	    /* incompatible drm version */
	    xf86DrvMsg(pScreen->myNum, X_ERROR,
		       "[dri] I810DRIScreenInit failed because of a version mismatch.\n"
		       "[dri] i810.o kernel module version is %d.%d.%d but version 1.2.0 or greater is needed.\n"
		       "[dri] Disabling DRI.\n",
		       version->version_major,
		       version->version_minor, version->version_patchlevel);
	    I810DRICloseScreen(pScreen);
	    drmFreeVersion(version);
	    return FALSE;
	 }
488
         pI810->drmMinor = version->version_minor;
489 490 491 492 493
	 drmFreeVersion(version);
      }
   }

   pI810DRI->regsSize = I810_REG_SIZE;
Eric Anholt's avatar
Eric Anholt committed
494
   if (drmAddMap(pI810->drmSubFD, (drm_handle_t) pI810->MMIOAddr,
Alan Hourihane's avatar
Alan Hourihane committed
495 496
		 pI810DRI->regsSize, DRM_REGISTERS, 0, 
		 (drmAddress) &pI810DRI->regs) < 0) {
497 498 499 500
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[drm] drmAddMap(regs) failed\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }
501
   xf86DrvMsg(pScreen->myNum, X_INFO, "[drm] Registers = 0x%08x\n",
502
	      (int)pI810DRI->regs);
503

504 505 506 507 508
   pI810->backHandle = DRM_AGP_NO_HANDLE;
   pI810->zHandle = DRM_AGP_NO_HANDLE;
   pI810->cursorHandle = DRM_AGP_NO_HANDLE;
   pI810->xvmcHandle = DRM_AGP_NO_HANDLE;
   pI810->sysmemHandle = DRM_AGP_NO_HANDLE;
509
   pI810->agpAcquired = FALSE;
510
   pI810->dcacheHandle = DRM_AGP_NO_HANDLE;
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

   /* Agp Support - Need this just to get the framebuffer.
    */
   if (drmAgpAcquire(pI810->drmSubFD) < 0) {
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[agp] drmAgpAquire failed\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }
   pI810->agpAcquired = TRUE;

   if (drmAgpEnable(pI810->drmSubFD, 0) < 0) {
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[agp] drmAgpEnable failed\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   memset(&pI810->DcacheMem, 0, sizeof(I810MemRange));
   memset(&pI810->BackBuffer, 0, sizeof(I810MemRange));
   memset(&pI810->DepthBuffer, 0, sizeof(I810MemRange));
   pI810->CursorPhysical = 0;
Alan Hourihane's avatar
Alan Hourihane committed
531
   pI810->CursorARGBPhysical = 0;
532 533 534 535 536

   /* Dcache - half the speed of normal ram, but has use as a Z buffer
    * under the DRI.
    */

Alan Hourihane's avatar
Alan Hourihane committed
537 538
   drmAgpAlloc(pI810->drmSubFD, 4096 * 1024, 1, NULL, 
	       (drmAddress) &dcacheHandle);
539
   pI810->dcacheHandle = dcacheHandle;
540

541
   xf86DrvMsg(pScreen->myNum, X_INFO, "[agp] dcacheHandle : 0x%x\n",
542
	      (int)dcacheHandle);
543 544 545 546 547 548 549 550 551 552 553 554

#define Elements(x) sizeof(x)/sizeof(*x)
   for (pitch_idx = 0; pitch_idx < Elements(i810_pitches); pitch_idx++)
      if (width <= i810_pitches[pitch_idx])
	 break;

   if (pitch_idx == Elements(i810_pitches)) {
      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		 "[dri] Couldn't find depth/back buffer pitch");
      DRICloseScreen(pScreen);
      return FALSE;
   } else {
555 556 557 558 559
      /* for tiled memory to work, the buffer needs to have the
       * number of lines as a multiple of 16 (the tile size),
       *  - airlied */
      int lines = (pScrn->virtualY + 15) / 16 * 16;
      back_size = i810_pitches[pitch_idx] * lines;
560 561 562 563
      back_size = ((back_size + 4096 - 1) / 4096) * 4096;
   }

   sysmem_size = pScrn->videoRam * 1024;
564
   if (dcacheHandle != DRM_AGP_NO_HANDLE) {
565 566 567 568 569
      if (back_size > 4 * 1024 * 1024) {
	 xf86DrvMsg(pScreen->myNum, X_INFO,
		    "[dri] Backsize is larger then 4 meg\n");
	 sysmem_size = sysmem_size - 2 * back_size;
	 drmAgpFree(pI810->drmSubFD, dcacheHandle);
570
	 pI810->dcacheHandle = dcacheHandle = DRM_AGP_NO_HANDLE;
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
      } else {
	 sysmem_size = sysmem_size - back_size;
      }
   } else {
      sysmem_size = sysmem_size - 2 * back_size;
   }

   /* Max size is 48 without XvMC, 41 with 6 surfaces, 40 with 7 surfaces */
   if (pI810->numSurfaces && (pI810->numSurfaces == 6)) {
      if (sysmem_size > (pI810->FbMapSize - 7 * 1024 * 1024)) {
	 sysmem_size = (pI810->FbMapSize - 7 * 1024 * 1024);
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "User requested more memory then fits in the agp aperture\n"
		    "Truncating to %d bytes of memory\n", sysmem_size);
      }
   }
   if (pI810->numSurfaces && (pI810->numSurfaces == 7)) {
      if (sysmem_size > (pI810->FbMapSize - 8 * 1024 * 1024)) {
	 sysmem_size = (pI810->FbMapSize - 8 * 1024 * 1024);
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "User requested more memory then fits in the agp aperture\n"
		    "Truncating to %d bytes of memory\n", sysmem_size);
      }
   }

   if (sysmem_size > pI810->FbMapSize) {
      sysmem_size = pI810->FbMapSize;

      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		 "[dri] User requested more memory then fits in the agp"
		 " aperture\n\tTruncating to %d bytes of memory\n",
		 sysmem_size);
   }

   sysmem_size -= 4096;			/* remove 4k for the hw cursor */
Alan Hourihane's avatar
Alan Hourihane committed
606
   sysmem_size -= 16384;		/* remove 16k for the ARGB hw cursor */
607 608 609 610 611 612 613 614

   pI810->SysMem.Start = 0;
   pI810->SysMem.Size = sysmem_size;
   pI810->SysMem.End = sysmem_size;
   tom = sysmem_size;

   pI810->SavedSysMem = pI810->SysMem;

615
   if (dcacheHandle != DRM_AGP_NO_HANDLE) {
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
      if (drmAgpBind(pI810->drmSubFD, dcacheHandle, pI810->DepthOffset) == 0) {
	 memset(&pI810->DcacheMem, 0, sizeof(I810MemRange));
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] GART: Found 4096K Z buffer memory\n");
	 pI810->DcacheMem.Start = pI810->DepthOffset;
	 pI810->DcacheMem.Size = 1024 * 4096;
	 pI810->DcacheMem.End =
	       pI810->DcacheMem.Start + pI810->DcacheMem.Size;
	 if (!I810AllocLow
	     (&(pI810->DepthBuffer), &(pI810->DcacheMem), back_size)) {
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "[agp] Depth buffer allocation failed\n");
	    DRICloseScreen(pScreen);
	    return FALSE;
	 }
      } else {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] GART: dcache bind failed\n");
	 drmAgpFree(pI810->drmSubFD, dcacheHandle);
635
	 pI810->dcacheHandle = dcacheHandle = DRM_AGP_NO_HANDLE;
636 637 638 639 640 641
      }
   } else {
      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		 "[agp] GART: no dcache memory found\n");
   }

Alan Hourihane's avatar
Alan Hourihane committed
642 643
   drmAgpAlloc(pI810->drmSubFD, back_size, 0, NULL, 
	       (drmAddress) &agpHandle);
644 645
   pI810->backHandle = agpHandle;

646
   if (agpHandle != DRM_AGP_NO_HANDLE) {
647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667
      if (drmAgpBind(pI810->drmSubFD, agpHandle, pI810->BackOffset) == 0) {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] Bound backbuffer memory\n");

	 pI810->BackBuffer.Start = pI810->BackOffset;
	 pI810->BackBuffer.Size = back_size;
	 pI810->BackBuffer.End = (pI810->BackBuffer.Start +
				  pI810->BackBuffer.Size);
      } else {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] Unable to bind backbuffer.  Disabling DRI.\n");
	 DRICloseScreen(pScreen);
	 return FALSE;
      }
   } else {
      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		 "[dri] Unable to allocate backbuffer memory.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

668
   if (dcacheHandle == DRM_AGP_NO_HANDLE) {
Alan Hourihane's avatar
Alan Hourihane committed
669 670
     drmAgpAlloc(pI810->drmSubFD, back_size, 0, NULL,
		 (drmAddress) &agpHandle);
671

672 673
      pI810->zHandle = agpHandle;

674
      if (agpHandle != DRM_AGP_NO_HANDLE) {
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
	 if (drmAgpBind(pI810->drmSubFD, agpHandle, pI810->DepthOffset) == 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "[agp] Bound depthbuffer memory\n");
	    pI810->DepthBuffer.Start = pI810->DepthOffset;
	    pI810->DepthBuffer.Size = back_size;
	    pI810->DepthBuffer.End = (pI810->DepthBuffer.Start +
				      pI810->DepthBuffer.Size);
	 } else {
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "[agp] Unable to bind depthbuffer.  Disabling DRI.\n");
	    DRICloseScreen(pScreen);
	    return FALSE;
	 }
      } else {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] Unable to allocate depthbuffer memory.  Disabling DRI.\n");
	 DRICloseScreen(pScreen);
	 return FALSE;
      }
   }

   /* Now allocate and bind the agp space.  This memory will include the
    * regular framebuffer as well as texture memory.
    */
Alan Hourihane's avatar
Alan Hourihane committed
699 700
   drmAgpAlloc(pI810->drmSubFD, sysmem_size, 0, NULL, 
	       (drmAddress)&agpHandle);
701
   pI810->sysmemHandle = agpHandle;
Eric Anholt's avatar
Eric Anholt committed
702 703 704 705 706 707 708 709 710 711 712 713
   
   if (agpHandle != DRM_AGP_NO_HANDLE) {
      if (drmAgpBind(pI810->drmSubFD, agpHandle, 0) == 0) {
	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "[agp] Bound System Texture Memory\n");
      } else {
          xf86DrvMsg(pScreen->myNum, X_ERROR, "[agp] Unable to bind system texture memory. Disabling DRI.\n");
	  DRICloseScreen(pScreen);
	  return FALSE;
      }
   } else {
      xf86DrvMsg(pScreen->myNum, X_ERROR, "[agp] Unable to allocate system texture memory. Disabling DRI.\n");
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
      DRICloseScreen(pScreen);
      return FALSE;
   }

/* Allocate 7 or 8MB for XvMC this is setup as follows to best use tiled
   regions and required surface pitches. (Numbers are adjusted if the
   AGP region is only 32MB
   For numSurfaces == 6
   44 - 48MB = 4MB Fence, 8 Tiles wide
   43 - 44MB = 1MB Fence, 8 Tiles wide
   42 - 43MB = 1MB Fence, 4 Tiles wide
   41 - 42MB = 1MB Fence, 4 Tiles wide
   For numSurfaces == 7
   44 - 48MB   = 4MB Fence, 8 Tiles wide
   43 - 44MB   = 1MB Fence, 8 Tiles wide
   42.5 - 43MB = 0.5MB Fence, 8 Tiles wide
   42 - 42.5MB = 0.5MB Fence, 4 Tiles wide
   40 - 42MB   = 2MB Fence, 4 Tiles wide
 */
   if (pI810->numSurfaces) {
      if (pI810->numSurfaces == 6) {
	 pI810->MC.Size = 7 * 1024 * 1024;
	 pI810->MC.Start = pI810->FbMapSize - 7 * 1024 * 1024;

      }
      if (pI810->numSurfaces == 7) {
	 pI810->MC.Size = 8 * 1024 * 1024;
	 pI810->MC.Start = pI810->FbMapSize - 8 * 1024 * 1024;
      }
Alan Hourihane's avatar
Alan Hourihane committed
743 744
      drmAgpAlloc(pI810->drmSubFD, pI810->MC.Size, 0, NULL,
		  (drmAddress) &agpHandle);
745
      
746 747
      pI810->xvmcHandle = agpHandle;

748
      if (agpHandle != DRM_AGP_NO_HANDLE) {
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
	 if (drmAgpBind(pI810->drmSubFD, agpHandle, pI810->MC.Start) == 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "GART: Allocated 7MB for HWMC\n");
	    pI810->MC.End = pI810->MC.Start + pI810->MC.Size;
	 } else {
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "GART: HWMC bind failed\n");
	    pI810->MC.Start = 0;
	    pI810->MC.Size = 0;
	    pI810->MC.End = 0;
	 }
      } else {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "GART: HWMC alloc failed\n");
	 pI810->MC.Start = 0;
	 pI810->MC.Size = 0;
	 pI810->MC.End = 0;
      }
      pI810->xvmcContext = 0;
   }

   drmAgpAlloc(pI810->drmSubFD, 4096, 2,
Alan Hourihane's avatar
Alan Hourihane committed
769 770
	       (unsigned long *)&pI810->CursorPhysical, 
	       (drmAddress) &agpHandle);
771

772 773
   pI810->cursorHandle = agpHandle;

774
   if (agpHandle != DRM_AGP_NO_HANDLE) {
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
      tom = sysmem_size;

      if (drmAgpBind(pI810->drmSubFD, agpHandle, tom) == 0) {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] GART: Allocated 4K for mouse cursor image\n");
	 pI810->CursorStart = tom;
	 tom += 4096;
      } else {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] GART: cursor bind failed\n");
	 pI810->CursorPhysical = 0;
      }
   } else {
      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		 "[agp] GART: cursor alloc failed\n");
      pI810->CursorPhysical = 0;
   }

Alan Hourihane's avatar
Alan Hourihane committed
793
   drmAgpAlloc(pI810->drmSubFD, 16384, 2,
Alan Hourihane's avatar
Alan Hourihane committed
794 795
	       (unsigned long *)&pI810->CursorARGBPhysical,
	       (drmAddress) &agpHandle);
Alan Hourihane's avatar
Alan Hourihane committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817

   pI810->cursorARGBHandle = agpHandle;

   if (agpHandle != DRM_AGP_NO_HANDLE) {
 	int r;

      if ((r = drmAgpBind(pI810->drmSubFD, agpHandle, tom)) == 0) {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] GART: Allocated 16K for ARGB mouse cursor image\n");
	 pI810->CursorARGBStart = tom;
	 tom += 16384;
      } else {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[agp] GART: ARGB cursor bind failed\n");
	 pI810->CursorARGBPhysical = 0;
      }
   } else {
      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		 "[agp] GART: ARGB cursor alloc failed\n");
      pI810->CursorARGBPhysical = 0;
   }

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
   /* Steal some of the excess cursor space for the overlay regs.
    */
   pI810->OverlayPhysical = pI810->CursorPhysical + 1024;
   pI810->OverlayStart = pI810->CursorStart + 1024;

   I810SetTiledMemory(pScrn, 1,
		      pI810->DepthBuffer.Start,
		      i810_pitches[pitch_idx], 8 * 1024 * 1024);

   I810SetTiledMemory(pScrn, 2,
		      pI810->BackBuffer.Start,
		      i810_pitches[pitch_idx], 8 * 1024 * 1024);

   /* These are for HWMC surfaces */
   if (pI810->numSurfaces == 6) {
      I810SetTiledMemory(pScrn, 3, pI810->MC.Start, 512, 1024 * 1024);

      I810SetTiledMemory(pScrn, 4,
			 pI810->MC.Start + 1024 * 1024, 512, 1024 * 1024);

      I810SetTiledMemory(pScrn, 5,
			 pI810->MC.Start + 1024 * 1024 * 2,
			 1024, 1024 * 1024);

      I810SetTiledMemory(pScrn, 6,
			 pI810->MC.Start + 1024 * 1024 * 3,
			 1024, 4 * 1024 * 1024);
   }
   if (pI810->numSurfaces == 7) {
      I810SetTiledMemory(pScrn, 3, pI810->MC.Start, 512, 2 * 1024 * 1024);

      I810SetTiledMemory(pScrn, 4,
			 pI810->MC.Start + 2 * 1024 * 1024, 512, 512 * 1024);

      I810SetTiledMemory(pScrn, 5,
			 pI810->MC.Start + 2 * 1024 * 1024 + 512 * 1024,
			 1024, 512 * 1024);

      I810SetTiledMemory(pScrn, 6,
			 pI810->MC.Start + 3 * 1024 * 1024,
			 1024, 1 * 1024 * 1024);

      I810SetTiledMemory(pScrn, 7,
			 pI810->MC.Start + 4 * 1024 * 1024,
			 1024, 4 * 1024 * 1024);

   }

   pI810->auxPitch = i810_pitches[pitch_idx];
   pI810->auxPitchBits = i810_pitch_flags[pitch_idx];
   pI810->SavedDcacheMem = pI810->DcacheMem;
   pI810DRI->backbufferSize = pI810->BackBuffer.Size;

Eric Anholt's avatar
Eric Anholt committed
871
   if (drmAddMap(pI810->drmSubFD, (drm_handle_t) pI810->BackBuffer.Start,
872
		 pI810->BackBuffer.Size, DRM_AGP, 0,
Alan Hourihane's avatar
Alan Hourihane committed
873
		 (drmAddress) &pI810DRI->backbuffer) < 0) {
874 875 876 877 878 879 880
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[drm] drmAddMap(backbuffer) failed.  Disabling DRI\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   pI810DRI->depthbufferSize = pI810->DepthBuffer.Size;
Eric Anholt's avatar
Eric Anholt committed
881
   if (drmAddMap(pI810->drmSubFD, (drm_handle_t) pI810->DepthBuffer.Start,
882
		 pI810->DepthBuffer.Size, DRM_AGP, 0,
Alan Hourihane's avatar
Alan Hourihane committed
883
		 (drmAddress) &pI810DRI->depthbuffer) < 0) {
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
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[drm] drmAddMap(depthbuffer) failed.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   /* Allocate FrontBuffer etc. */
   if (!I810AllocateFront(pScrn)) {
      DRICloseScreen(pScreen);
      return FALSE;
   }

   /* Allocate buffer memory */
   I810AllocHigh(&(pI810->BufferMem), &(pI810->SysMem),
		 I810_DMA_BUF_NR * I810_DMA_BUF_SZ);

   xf86DrvMsg(pScreen->myNum, X_INFO, "[dri] Buffer map : %lx\n",
	      pI810->BufferMem.Start);

   if (pI810->BufferMem.Start == 0 ||
       pI810->BufferMem.End - pI810->BufferMem.Start >
       I810_DMA_BUF_NR * I810_DMA_BUF_SZ) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[dri] Not enough memory for dma buffers.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }
Eric Anholt's avatar
Eric Anholt committed
911
   if (drmAddMap(pI810->drmSubFD, (drm_handle_t) pI810->BufferMem.Start,
Alan Hourihane's avatar
Alan Hourihane committed
912 913
		 pI810->BufferMem.Size, DRM_AGP, 0,
		 (drmAddress) &pI810->buffer_map) < 0) {
914 915 916 917 918 919 920 921 922
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[drm] drmAddMap(buffer_map) failed.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   pI810DRI->agp_buffers = pI810->buffer_map;
   pI810DRI->agp_buf_size = pI810->BufferMem.Size;

Alan Hourihane's avatar
Alan Hourihane committed
923
   if (drmAddMap(pI810->drmSubFD, (drm_handle_t) pI810->LpRing->mem.Start,
Alan Hourihane's avatar
Alan Hourihane committed
924 925
		 pI810->LpRing->mem.Size, DRM_AGP, 0, 
		 (drmAddress) &pI810->ring_map) < 0) {
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
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[drm] drmAddMap(ring_map) failed.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   /* Use the rest of memory for textures. */
   pI810DRI->textureSize = pI810->SysMem.Size;

   i = mylog2(pI810DRI->textureSize / I810_NR_TEX_REGIONS);

   if (i < I810_LOG_MIN_TEX_REGION_SIZE)
      i = I810_LOG_MIN_TEX_REGION_SIZE;

   pI810DRI->logTextureGranularity = i;
   pI810DRI->textureSize = (pI810DRI->textureSize >> i) << i;	/* truncate */

   if (pI810DRI->textureSize < 512 * 1024) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[drm] Less then 512k memory left for textures.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   I810AllocLow(&(pI810->TexMem), &(pI810->SysMem), pI810DRI->textureSize);

Eric Anholt's avatar
Eric Anholt committed
952
   if (drmAddMap(pI810->drmSubFD, (drm_handle_t) pI810->TexMem.Start,
Alan Hourihane's avatar
Alan Hourihane committed
953 954
		 pI810->TexMem.Size, DRM_AGP, 0, 
		 (drmAddress) &pI810DRI->textures) < 0) {
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
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[drm] drmAddMap(textures) failed.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   if ((bufs = drmAddBufs(pI810->drmSubFD,
			  I810_DMA_BUF_NR,
			  I810_DMA_BUF_SZ,
			  DRM_AGP_BUFFER, pI810->BufferMem.Start)) <= 0) {
      xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		 "[drm] failure adding %d %d byte DMA buffers.  Disabling DRI.\n",
		 I810_DMA_BUF_NR, I810_DMA_BUF_SZ);
      DRICloseScreen(pScreen);
      return FALSE;
   }

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

   I810InitDma(pScrn);

   /* Okay now initialize the dma engine */

   if (!pI810DRI->irq) {
      pI810DRI->irq = drmGetInterruptFromBusID(pI810->drmSubFD,
981 982 983 984 985 986
#if XSERVER_LIBPCIACCESS
					       ((pI810->PciInfo->domain << 8) |
						pI810->PciInfo->bus),
					       pI810->PciInfo->dev,
					       pI810->PciInfo->func
#else
987 988 989 990 991
					       ((pciConfigPtr) pI810->
						PciInfo->thisCard)->busnum,
					       ((pciConfigPtr) pI810->
						PciInfo->thisCard)->devnum,
					       ((pciConfigPtr) pI810->
992 993 994
						PciInfo->thisCard)->funcnum
#endif
					       );
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
      if ((drmCtlInstHandler(pI810->drmSubFD, pI810DRI->irq)) != 0) {
	 xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		    "[drm] failure adding irq handler, there is a device "
		    "already using that irq\n Consider rearranging your "
		    "PCI cards.  Disabling DRI.\n");
	 DRICloseScreen(pScreen);
	 return FALSE;
      }
   }

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

1008
   pI810DRI->deviceID = DEVICE_ID(pI810->PciInfo);
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
   pI810DRI->width = pScrn->virtualX;
   pI810DRI->height = pScrn->virtualY;
   pI810DRI->mem = pScrn->videoRam * 1024;
   pI810DRI->cpp = pI810->cpp;

   pI810DRI->fbOffset = pI810->FrontBuffer.Start;
   pI810DRI->fbStride = pI810->auxPitch;

   pI810DRI->bitsPerPixel = pScrn->bitsPerPixel;

   pI810DRI->textureOffset = pI810->TexMem.Start;

   pI810DRI->backOffset = pI810->BackBuffer.Start;
   pI810DRI->depthOffset = pI810->DepthBuffer.Start;

Alan Hourihane's avatar
Alan Hourihane committed
1024 1025
   pI810DRI->ringOffset = pI810->LpRing->mem.Start;
   pI810DRI->ringSize = pI810->LpRing->mem.Size;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

   pI810DRI->auxPitch = pI810->auxPitch;
   pI810DRI->auxPitchBits = pI810->auxPitchBits;
   pI810DRI->sarea_priv_offset = sizeof(XF86DRISAREARec);

   if (!(I810InitVisualConfigs(pScreen))) {
      xf86DrvMsg(pScreen->myNum, X_ERROR,
		 "[dri] I810InitVisualConfigs failed.  Disabling DRI.\n");
      DRICloseScreen(pScreen);
      return FALSE;
   }

   xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	      "[dri] visual configs initialized.\n");
   pI810->pDRIInfo->driverSwapMethod = DRI_HIDE_X_CONTEXT;

   return TRUE;
}

void
I810DRICloseScreen(ScreenPtr pScreen)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   I810Ptr pI810 = I810PTR(pScrn);
1050 1051 1052 1053 1054 1055
   I810DRIPtr pI810DRI = (I810DRIPtr) pI810->pDRIInfo->devPrivate;

   if (pI810DRI->irq) {
       drmCtlUninstHandler(pI810->drmSubFD);
       pI810DRI->irq = 0;
   }
1056 1057 1058

   I810CleanupDma(pScrn);

1059
   if (pI810->dcacheHandle!=DRM_AGP_NO_HANDLE)
1060
      drmAgpFree(pI810->drmSubFD, pI810->dcacheHandle);
1061
   if (pI810->backHandle!=DRM_AGP_NO_HANDLE)
1062
      drmAgpFree(pI810->drmSubFD, pI810->backHandle);
1063
   if (pI810->zHandle!=DRM_AGP_NO_HANDLE)
1064
      drmAgpFree(pI810->drmSubFD, pI810->zHandle);
1065
   if (pI810->cursorHandle!=DRM_AGP_NO_HANDLE)
1066
      drmAgpFree(pI810->drmSubFD, pI810->cursorHandle);
1067
   if (pI810->xvmcHandle!=DRM_AGP_NO_HANDLE)
1068
      drmAgpFree(pI810->drmSubFD, pI810->xvmcHandle);
1069
   if (pI810->sysmemHandle!=DRM_AGP_NO_HANDLE)
1070 1071 1072 1073 1074
      drmAgpFree(pI810->drmSubFD, pI810->sysmemHandle);

   if (pI810->agpAcquired == TRUE)
      drmAgpRelease(pI810->drmSubFD);

1075 1076 1077 1078 1079
   pI810->backHandle = DRM_AGP_NO_HANDLE;
   pI810->zHandle = DRM_AGP_NO_HANDLE;
   pI810->cursorHandle = DRM_AGP_NO_HANDLE;
   pI810->xvmcHandle = DRM_AGP_NO_HANDLE;
   pI810->sysmemHandle = DRM_AGP_NO_HANDLE;
1080
   pI810->agpAcquired = FALSE;
1081
   pI810->dcacheHandle = DRM_AGP_NO_HANDLE;
1082 1083 1084 1085 1086 1087

   DRICloseScreen(pScreen);

   if (pI810->pDRIInfo) {
      if (pI810->pDRIInfo->devPrivate) {
	 xfree(pI810->pDRIInfo->devPrivate);
1088
	 pI810->pDRIInfo->devPrivate = NULL;
1089 1090
      }
      DRIDestroyInfoRec(pI810->pDRIInfo);
1091
      pI810->pDRIInfo = NULL;
1092 1093 1094 1095 1096 1097 1098 1099 1100
   }
   if (pI810->pVisualConfigs)
      xfree(pI810->pVisualConfigs);
   if (pI810->pVisualConfigsPriv)
      xfree(pI810->pVisualConfigsPriv);
}

static Bool
I810CreateContext(ScreenPtr pScreen, VisualPtr visual,
Eric Anholt's avatar
Eric Anholt committed
1101
		  drm_context_t hwContext, void *pVisualConfigPriv,
1102 1103 1104 1105 1106 1107
		  DRIContextType contextStore)
{
   return TRUE;
}

static void
Eric Anholt's avatar
Eric Anholt committed
1108
I810DestroyContext(ScreenPtr pScreen, drm_context_t hwContext,
1109 1110 1111 1112 1113 1114 1115 1116
		   DRIContextType contextStore)
{
}

Bool
I810DRIFinishScreenInit(ScreenPtr pScreen)
{
   I810SAREARec *sPriv = (I810SAREARec *) DRIGetSAREAPrivate(pScreen);
1117 1118
   ScrnInfoPtr        pScrn = xf86Screens[pScreen->myNum];
   I810Ptr info  = I810PTR(pScrn);
1119 1120

   memset(sPriv, 0, sizeof(sPriv));
1121 1122 1123 1124 1125 1126 1127 1128

   /* Have shadow run only while there is 3d active.
    */
   if (info->allowPageFlip && info->drmMinor >= 3) {
     ShadowFBInit( pScreen, I810DRIRefreshArea );
   } 
   else
     info->allowPageFlip = 0;
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
   return DRIFinishScreenInit(pScreen);
}

void
I810DRISwapContext(ScreenPtr pScreen, DRISyncType syncType,
		   DRIContextType oldContextType, void *oldContext,
		   DRIContextType newContextType, void *newContext)
{
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   I810Ptr pI810 = I810PTR(pScrn);

   if (syncType == DRI_3D_SYNC &&
       oldContextType == DRI_2D_CONTEXT && newContextType == DRI_2D_CONTEXT) {
      if (I810_DEBUG & DEBUG_VERBOSE_DRI)
	 ErrorF("I810DRISwapContext (in)\n");

      if (!pScrn->vtSema)
	  return;
      pI810->LockHeld = 1;
      I810RefreshRing(pScrn);
   } else if (syncType == DRI_2D_SYNC &&
	      oldContextType == DRI_NO_CONTEXT &&
	      newContextType == DRI_2D_CONTEXT) {
      pI810->LockHeld = 0;
      if (I810_DEBUG & DEBUG_VERBOSE_DRI)
	 ErrorF("I810DRISwapContext (out)\n");
   } else if (I810_DEBUG & DEBUG_VERBOSE_DRI)
      ErrorF("I810DRISwapContext (other)\n");
}

static void
I810DRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index)
{
   ScreenPtr pScreen = pWin->drawable.pScreen;
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   I810Ptr pI810 = I810PTR(pScrn);
   BoxPtr pbox = REGION_RECTS(prgn);
   int nbox = REGION_NUM_RECTS(prgn);

   if (I810_DEBUG & DEBUG_VERBOSE_DRI)
      ErrorF("I810DRIInitBuffers\n");

   I810SetupForSolidFill(pScrn, 0, GXcopy, -1);
   while (nbox--) {
      I810SelectBuffer(pScrn, I810_SELECT_BACK);
      I810SubsequentSolidFillRect(pScrn, pbox->x1, pbox->y1,
				  pbox->x2 - pbox->x1, pbox->y2 - pbox->y1);
      pbox++;
   }

   /* Clear the depth buffer - uses 0xffff rather than 0.
    */
   pbox = REGION_RECTS(prgn);
   nbox = REGION_NUM_RECTS(prgn);
   I810SelectBuffer(pScrn, I810_SELECT_DEPTH);
   I810SetupForSolidFill(pScrn, 0xffff, GXcopy, -1);
   while (nbox--) {
      I810SubsequentSolidFillRect(pScrn, pbox->x1, pbox->y1,
				  pbox->x2 - pbox->x1, pbox->y2 - pbox->y1);
      pbox++;
   }
   I810SelectBuffer(pScrn, I810_SELECT_FRONT);
1191 1192 1193

   if (pI810->AccelInfoRec)
   	pI810->AccelInfoRec->NeedToSync = TRUE;
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
}

/* This routine is a modified form of XAADoBitBlt with the calls to
 * ScreenToScreenBitBlt built in. My routine has the prgnSrc as source
 * instead of destination. My origin is upside down so the ydir cases
 * are reversed.
 *
 * KW: can you believe that this is called even when a 2d window moves?
 */
static void
I810DRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg,
		   RegionPtr prgnSrc, CARD32 index)
{
   ScreenPtr pScreen = pParent->drawable.pScreen;
   ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
   I810Ptr pI810 = I810PTR(pScrn);
   BoxPtr pboxTmp, pboxNext, pboxBase;
1211
   DDXPointPtr pptTmp, pptNew2 = NULL;
1212 1213 1214 1215 1216 1217 1218 1219
   int xdir, ydir;

   int screenwidth = pScrn->virtualX;
   int screenheight = pScrn->virtualY;

   BoxPtr pbox = REGION_RECTS(prgnSrc);
   int nbox = REGION_NUM_RECTS(prgnSrc);

1220 1221 1222
   BoxPtr pboxNew1 = NULL;
   BoxPtr pboxNew2 = NULL;
   DDXPointPtr pptNew1 = NULL;
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233
   DDXPointPtr pptSrc = &ptOldOrg;

   int dx = pParent->drawable.x - ptOldOrg.x;
   int dy = pParent->drawable.y - ptOldOrg.y;

   /* If the copy will overlap in Y, reverse the order */
   if (dy > 0) {
      ydir = -1;

      if (nbox > 1) {
	 /* Keep ordering in each band, reverse order of bands */
1234
	 pboxNew1 = (BoxPtr) xalloc(sizeof(BoxRec) * nbox);
1235 1236
	 if (!pboxNew1)
	    return;
1237
	 pptNew1 = (DDXPointPtr) xalloc(sizeof(DDXPointRec) * nbox);
1238
	 if (!pptNew1) {
1239
	    xfree(pboxNew1);
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	    return;
	 }
	 pboxBase = pboxNext = pbox + nbox - 1;
	 while (pboxBase >= pbox) {
	    while ((pboxNext >= pbox) && (pboxBase->y1 == pboxNext->y1))
	       pboxNext--;
	    pboxTmp = pboxNext + 1;
	    pptTmp = pptSrc + (pboxTmp - pbox);
	    while (pboxTmp <= pboxBase) {
	       *pboxNew1++ = *pboxTmp++;
	       *pptNew1++ = *pptTmp++;
	    }
	    pboxBase = pboxNext;
	 }
	 pboxNew1 -= nbox;
	 pbox = pboxNew1;
	 pptNew1 -= nbox;
	 pptSrc = pptNew1;
      }
   } else {
      /* No changes required */
      ydir = 1;
   }

   /* If the regions will overlap in X, reverse the order */
   if (dx > 0) {
      xdir = -1;

      if (nbox > 1) {
	 /*reverse orderof rects in each band */
1270 1271
	 pboxNew2 = (BoxPtr) xalloc(sizeof(BoxRec) * nbox);
	 pptNew2 = (DDXPointPtr) xalloc(sizeof(DDXPointRec) * nbox);
1272 1273
	 if (!pboxNew2 || !pptNew2) {
	    if (pptNew2)
1274
	       xfree(pptNew2);
1275
	    if (pboxNew2)
1276
	       xfree(pboxNew2);
1277
	    if (pboxNew1) {
1278 1279
	       xfree(pptNew1);
	       xfree(pboxNew1);
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
	    }
	    return;
	 }
	 pboxBase = pboxNext = pbox;
	 while (pboxBase < pbox + nbox) {
	    while ((pboxNext < pbox + nbox) && (pboxNext->y1 == pboxBase->y1))
	       pboxNext++;
	    pboxTmp = pboxNext;
	    pptTmp = pptSrc + (pboxTmp - pbox);
	    while (pboxTmp != pboxBase) {
	       *pboxNew2++ = *--pboxTmp;
	       *pptNew2++ = *--pptTmp;
	    }
	    pboxBase = pboxNext;
	 }
	 pboxNew2 -= nbox;
	 pbox = pboxNew2;
	 pptNew2 -= nbox;
	 pptSrc = pptNew2;
      }
   } else {
      /* No changes are needed */
      xdir = 1;
   }

   /* SelectBuffer isn't really a good concept for the i810.
    */
   I810EmitFlush(pScrn);
   I810SetupForScreenToScreenCopy(pScrn, xdir, ydir, GXcopy, -1, -1);
   for (; nbox--; pbox++) {

      int x1 = pbox->x1;
      int y1 = pbox->y1;
      int destx = x1 + dx;
      int desty = y1 + dy;
      int w = pbox->x2 - x1 + 1;
      int h = pbox->y2 - y1 + 1;

      if (destx < 0)
	 x1 -= destx, w += destx, destx = 0;
      if (desty < 0)
	 y1 -= desty, h += desty, desty = 0;
      if (destx + w > screenwidth)
	 w = screenwidth - destx;
      if (desty + h > screenheight)
	 h = screenheight - desty;
      if (w <= 0)
	 continue;
      if (h <= 0)
	 continue;

      if (I810_DEBUG & DEBUG_VERBOSE_DRI)
	 ErrorF("MoveBuffers %d,%d %dx%d dx: %d dy: %d\n",
		x1, y1, w, h, dx, dy);
1334
      
1335 1336 1337 1338 1339 1340 1341 1342 1343
      I810SelectBuffer(pScrn, I810_SELECT_BACK);
      I810SubsequentScreenToScreenCopy(pScrn, x1, y1, destx, desty, w, h);
      I810SelectBuffer(pScrn, I810_SELECT_DEPTH);
      I810SubsequentScreenToScreenCopy(pScrn, x1, y1, destx, desty, w, h);
   }
   I810SelectBuffer(pScrn, I810_SELECT_FRONT);
   I810EmitFlush(pScrn);

   if (pboxNew2) {
1344 1345
      xfree(pptNew2);
      xfree(pboxNew2);
1346 1347
   }
   if (pboxNew1) {
1348 1349
      xfree(pptNew1);
      xfree(pboxNew1);
1350 1351
   }

1352 1353
   if (pI810->AccelInfoRec)
	pI810->AccelInfoRec->NeedToSync = TRUE;
1354
}
1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471


/* Use the miext/shadow module to maintain a list of dirty rectangles.
 * These are blitted to the back buffer to keep both buffers clean
 * during page-flipping when the 3d application isn't fullscreen.
 *
 * Unlike most use of the shadow code, both buffers are in video memory.
 *
 * An alternative to this would be to organize for all on-screen drawing
 * operations to be duplicated for the two buffers.  That might be
 * faster, but seems like a lot more work...
 */


/* This should be done *before* XAA syncs or fires its buffer.
 * Otherwise will have to fire it again???
 */
static void I810DRIRefreshArea(ScrnInfoPtr pScrn, int num, BoxPtr pbox)
{
    I810Ptr pI810 = I810PTR(pScrn);
    int i;
    I810SAREAPtr  pSAREAPriv = DRIGetSAREAPrivate(pScrn->pScreen);
    unsigned int br13;
    int cpp=2;

    
    /* Don't want to do this when no 3d is active and pages are
     * right-way-round
     */
    if (!pSAREAPriv->pf_active && pSAREAPriv->pf_current_page == 0)
      return;

    br13 = (pI810->auxPitch) | (0xcc << 16);
    
    for (i = 0 ; i < num ; i++, pbox++) {
      unsigned int w = min(pbox->y2, pScrn->virtualY-1) - max(pbox->y1, 0) + 1;
      unsigned int h = min(pbox->x2, pScrn->virtualX-1) - max(pbox->x1, 0) + 1;
      unsigned int dst = max(pbox->x1, 0)*cpp + (max(pbox->y1, 0)*pI810->auxPitch);
      
      BEGIN_LP_RING(6);

      OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
      OUT_RING(br13);
      OUT_RING( (h<<16) | (w*cpp) );
      OUT_RING(pI810->BackBuffer.Start + dst);
      OUT_RING(br13 & 0xffff);
      OUT_RING(dst);

      ADVANCE_LP_RING();
    }

}

static void I810EnablePageFlip(ScreenPtr pScreen)
{
    ScrnInfoPtr         pScrn      = xf86Screens[pScreen->myNum];
    I810Ptr       pI810       = I810PTR(pScrn);
    I810SAREAPtr  pSAREAPriv = DRIGetSAREAPrivate(pScreen);
    int cpp=2;
    pSAREAPriv->pf_enabled = pI810->allowPageFlip;
    pSAREAPriv->pf_active = 0;
    
   if (pI810->allowPageFlip) {
      unsigned int br13 = pI810->auxPitch | (0xcc << 16);
      
      BEGIN_LP_RING(6);

      OUT_RING(BR00_BITBLT_CLIENT | BR00_OP_SRC_COPY_BLT | 0x4);
      OUT_RING(br13);
      OUT_RING((pScrn->virtualY << 16) | (pScrn->virtualX*cpp));
      OUT_RING(pI810->BackBuffer.Start);
      OUT_RING(br13 & 0xFFFF);
      OUT_RING(0);
      ADVANCE_LP_RING();

      pSAREAPriv->pf_active = 1;
   }
   
}

static void I810DisablePageFlip(ScreenPtr pScreen)
{
    I810SAREAPtr  pSAREAPriv = DRIGetSAREAPrivate(pScreen);

    pSAREAPriv->pf_active=0;
}

static void I810DRITransitionSingleToMulti3d(ScreenPtr pScreen)
{
    /* Tell the clients not to pageflip.  How?
     *   -- Field in sarea, plus bumping the window counters.
     *   -- DRM needs to cope with Front-to-Back swapbuffers.
     */
    I810DisablePageFlip(pScreen);
}

static void I810DRITransitionMultiToSingle3d(ScreenPtr pScreen)
{
    /* Let the remaining 3d app start page flipping again */
    I810EnablePageFlip(pScreen);
}

static void I810DRITransitionTo3d(ScreenPtr pScreen)
{
    ScrnInfoPtr    pScrn = xf86Screens[pScreen->myNum];
    I810Ptr  pI810  = I810PTR(pScrn);

    I810EnablePageFlip(pScreen);
    pI810->have3DWindows = 1;
}

static void I810DRITransitionTo2d(ScreenPtr pScreen)
{
    ScrnInfoPtr         pScrn      = xf86Screens[pScreen->myNum];
    I810Ptr       pI810       = I810PTR(pScrn);
    I810SAREAPtr  pSAREAPriv = DRIGetSAREAPrivate(pScreen);

Eric Anholt's avatar
Eric Anholt committed
1472 1473 1474 1475
    /* Try flipping back to the front page if necessary */
    if (pSAREAPriv->pf_current_page == 1)
	drmCommandNone(pI810->drmSubFD, DRM_I810_FLIP);

1476 1477 1478 1479 1480 1481
    /* Shut down shadowing if we've made it back to the front page */
    if (pSAREAPriv->pf_current_page == 0) {
	I810DisablePageFlip(pScreen);
    }
    pI810->have3DWindows = 0;
}
Eric Anholt's avatar
Eric Anholt committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518

Bool
I810DRILeave(ScrnInfoPtr pScrn)
{
   I810Ptr pI810 = I810PTR(pScrn);
    
   if (pI810->directRenderingEnabled) {
      if (pI810->dcacheHandle != 0) 
	 if (drmAgpUnbind(pI810->drmSubFD, pI810->dcacheHandle) != 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,"%s\n",strerror(errno));
	    return FALSE;
	 }
      if (pI810->backHandle != 0) 
	 if (drmAgpUnbind(pI810->drmSubFD, pI810->backHandle) != 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,"%s\n",strerror(errno));
 	    return FALSE;
	 }
      if (pI810->zHandle != 0)
	 if (drmAgpUnbind(pI810->drmSubFD, pI810->zHandle) != 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,"%s\n",strerror(errno));
  	    return FALSE;
	 }
      if (pI810->sysmemHandle != 0)
	 if (drmAgpUnbind(pI810->drmSubFD, pI810->sysmemHandle) != 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,"%s\n",strerror(errno));
  	    return FALSE;
	 }
      if (pI810->xvmcHandle != 0)
	 if (drmAgpUnbind(pI810->drmSubFD, pI810->xvmcHandle) != 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,"%s\n",strerror(errno));
  	    return FALSE;
	 }
      if (pI810->cursorHandle != 0)
	 if (drmAgpUnbind(pI810->drmSubFD, pI810->cursorHandle) != 0) {
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,"%s\n",strerror(errno));
	    return FALSE;
	 }
1519 1520 1521 1522 1523
      if (pI810->cursorARGBHandle != 0)
	  if (drmAgpUnbind(pI810->drmSubFD, pI810->cursorARGBHandle) != 0) {
	      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,"%s\n",strerror(errno));
	      return FALSE;
	  }
Eric Anholt's avatar
Eric Anholt committed
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
      if (pI810->agpAcquired == TRUE)
	 drmAgpRelease(pI810->drmSubFD);
      pI810->agpAcquired = FALSE;
   }
   return TRUE;
}

Bool
I810DRIEnter(ScrnInfoPtr pScrn)
{
   I810Ptr pI810 = I810PTR(pScrn);

   if (pI810->directRenderingEnabled) {

      if (pI810->agpAcquired == FALSE)
	 drmAgpAcquire(pI810->drmSubFD);
      pI810->agpAcquired = TRUE;
      if (pI810->dcacheHandle != 0)
	 if (drmAgpBind(pI810->drmSubFD, pI810->dcacheHandle,
			pI810->DepthOffset) != 0)
	    return FALSE;
      if (pI810->backHandle != 0)
	 if (drmAgpBind(pI810->drmSubFD, pI810->backHandle,
			pI810->BackOffset) != 0)
	    return FALSE;
      if (pI810->zHandle != 0)
	 if (drmAgpBind(pI810->drmSubFD, pI810->zHandle,
			pI810->DepthOffset) != 0)
	    return FALSE;
      if (pI810->sysmemHandle != 0)
	 if (drmAgpBind(pI810->drmSubFD, pI810->sysmemHandle, 0) != 0)
	    return FALSE;
      if (pI810->xvmcHandle != 0)
	 if (drmAgpBind(pI810->drmSubFD, pI810->xvmcHandle,
			pI810->MC.Start) != 0)
	    return FALSE;
      if (pI810->cursorHandle != 0)
	 if (drmAgpBind(pI810->drmSubFD, pI810->cursorHandle,
			pI810->CursorStart) != 0)
	    return FALSE;
Alan Hourihane's avatar
Alan Hourihane committed
1564 1565 1566 1567
      if (pI810->cursorARGBHandle != 0)
	 if (drmAgpBind(pI810->drmSubFD, pI810->cursorARGBHandle,
			pI810->CursorARGBStart) != 0)
	    return FALSE;
Eric Anholt's avatar
Eric Anholt committed
1568 1569 1570
   }
   return TRUE;
}