⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 intel_dri.c

📁 mesa-6.5-minigui源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * \file server/intel_dri.c * \brief File to perform the device-specific initialization tasks typically * done in the X server. * * Here they are converted to run in the client (or perhaps a standalone * process), and to work with the frame buffer device rather than the X * server infrastructure. *  * Copyright (C) 2006 Dave Airlie (airlied@linux.ie) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sub license, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <unistd.h>#include "driver.h"#include "drm.h"#include "intel.h"#include "i830_dri.h"#include "memops.h"#include "pciaccess.h"static size_t drm_page_size;#define xf86DrvMsg(...) do {} while(0)static Bool I830DRIDoMappings(DRIDriverContext *ctx, I830Rec *pI830, drmI830Sarea *sarea);static int I830DetectMemory(const DRIDriverContext *ctx, I830Rec *pI830){  struct pci_device host_bridge;  uint32_t gmch_ctrl;  int memsize = 0;  int range;  memset(&host_bridge, 0, sizeof(host_bridge));  pci_device_cfg_read_u32(&host_bridge, &gmch_ctrl, I830_GMCH_CTRL);    /* We need to reduce the stolen size, by the GTT and the popup.   * The GTT varying according the the FbMapSize and the popup is 4KB */  range = (ctx->shared.fbSize / (1024*1024)) + 4;   if (IS_I85X(pI830) || IS_I865G(pI830) || IS_I9XX(pI830)) {      switch (gmch_ctrl & I830_GMCH_GMS_MASK) {      case I855_GMCH_GMS_STOLEN_1M:	 memsize = MB(1) - KB(range);	 break;      case I855_GMCH_GMS_STOLEN_4M:	 memsize = MB(4) - KB(range);	 break;      case I855_GMCH_GMS_STOLEN_8M:	 memsize = MB(8) - KB(range);	 break;      case I855_GMCH_GMS_STOLEN_16M:	 memsize = MB(16) - KB(range);	 break;      case I855_GMCH_GMS_STOLEN_32M:	 memsize = MB(32) - KB(range);	 break;      case I915G_GMCH_GMS_STOLEN_48M:	 if (IS_I9XX(pI830))	    memsize = MB(48) - KB(range);	 break;      case I915G_GMCH_GMS_STOLEN_64M:	 if (IS_I9XX(pI830))	    memsize = MB(64) - KB(range);	 break;      }   } else {      switch (gmch_ctrl & I830_GMCH_GMS_MASK) {      case I830_GMCH_GMS_STOLEN_512:	 memsize = KB(512) - KB(range);	 break;      case I830_GMCH_GMS_STOLEN_1024:	 memsize = MB(1) - KB(range);	 break;      case I830_GMCH_GMS_STOLEN_8192:	 memsize = MB(8) - KB(range);	 break;      case I830_GMCH_GMS_LOCAL:	 memsize = 0;	 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,		    "Local memory found, but won't be used.\n");	 break;      }   }   if (memsize > 0) {     fprintf(stderr,		 "detected %d kB stolen memory.\n", memsize / 1024);   } else {     fprintf(stderr,		 "no video memory detected.\n");   }   return memsize;}static int AgpInit(const DRIDriverContext *ctx, I830Rec *info){  unsigned long mode = 0x4;  if (drmAgpAcquire(ctx->drmFD) < 0) {    fprintf(stderr, "[gart] AGP not available\n");    return 0;  }    if (drmAgpEnable(ctx->drmFD, mode) < 0) {    fprintf(stderr, "[gart] AGP not enabled\n");    drmAgpRelease(ctx->drmFD);    return 0;  }  else    fprintf(stderr, "[gart] AGP enabled at %dx\n", ctx->agpmode);  return 1;}static unsigned long AllocFromAGP(const DRIDriverContext *ctx, I830Rec *pI830, long size, unsigned long alignment, I830MemRange  *result){   unsigned long start, end;   unsigned long newApStart, newApEnd;   int ret;   if (!result || !size)      return 0;      if (!alignment)     alignment = 4;   start = ROUND_TO(pI830->MemoryAperture.Start, alignment);   end = ROUND_TO(start + size, alignment);   newApStart = end;   newApEnd = pI830->MemoryAperture.End;   ret=drmAgpAlloc(ctx->drmFD, size, 0, &(result->Physical), (drm_handle_t *)&(result->Key));      if (ret)   {     fprintf(stderr,"drmAgpAlloc failed %d\n", ret);     return 0;   }   pI830->allocatedMemory += size;   pI830->MemoryAperture.Start = newApStart;   pI830->MemoryAperture.End = newApEnd;   pI830->MemoryAperture.Size = newApEnd - newApStart;   pI830->FreeMemory -= size;   result->Start = start;   result->End = start + size;   result->Size = size;   result->Offset = start;   result->Alignment = alignment;   result->Pool = NULL;     return size;}static Bool BindAgpRange(const DRIDriverContext *ctx, I830MemRange *mem){  if (!mem)    return FALSE;    if (mem->Key == -1)    return TRUE;  return drmAgpBind(ctx->drmFD, mem->Key, mem->Offset);}/* simple memory allocation routines needed *//* put ring buffer in low memory *//* need to allocate front, back, depth buffers aligned correctly,   allocate ring buffer, *//* */static BoolI830AllocateMemory(const DRIDriverContext *ctx, I830Rec *pI830){  unsigned long size, ret;  unsigned long lines, lineSize, align;  /* allocate ring buffer */  memset(pI830->LpRing, 0, sizeof(I830RingBuffer));  pI830->LpRing->mem.Key = -1;  size = PRIMARY_RINGBUFFER_SIZE;    ret = AllocFromAGP(ctx, pI830, size, 0x1000, &pI830->LpRing->mem);    if (ret != size)  {    fprintf(stderr,"unable to allocate ring buffer %ld\n", ret);    return FALSE;  }  pI830->LpRing->tail_mask = pI830->LpRing->mem.Size - 1;    /* allocate front buffer */  memset(&(pI830->FrontBuffer), 0, sizeof(pI830->FrontBuffer));  pI830->FrontBuffer.Key = -1;  pI830->FrontBuffer.Pitch = ctx->shared.virtualWidth;  align = KB(512);    lineSize = ctx->shared.virtualWidth * ctx->cpp;  lines = (ctx->shared.virtualHeight + 15) / 16 * 16;  size = lineSize * lines;  size = ROUND_TO_PAGE(size);  ret = AllocFromAGP(ctx, pI830, size, align, &pI830->FrontBuffer);  if (ret != size)  {    fprintf(stderr,"unable to allocate front buffer %ld\n", ret);    return FALSE;  }  memset(&(pI830->BackBuffer), 0, sizeof(pI830->BackBuffer));  pI830->BackBuffer.Key = -1;  pI830->BackBuffer.Pitch = ctx->shared.virtualWidth;  ret = AllocFromAGP(ctx, pI830, size, align, &pI830->BackBuffer);  if (ret != size)  {    fprintf(stderr,"unable to allocate back buffer %ld\n", ret);    return FALSE;  }    memset(&(pI830->DepthBuffer), 0, sizeof(pI830->DepthBuffer));  pI830->DepthBuffer.Key = -1;  pI830->DepthBuffer.Pitch = ctx->shared.virtualWidth;  ret = AllocFromAGP(ctx, pI830, size, align, &pI830->DepthBuffer);  if (ret != size)  {    fprintf(stderr,"unable to allocate depth buffer %ld\n", ret);    return FALSE;  }  memset(&(pI830->ContextMem), 0, sizeof(pI830->ContextMem));  pI830->ContextMem.Key = -1;  size = KB(32);  ret = AllocFromAGP(ctx, pI830, size, align, &pI830->ContextMem);  if (ret != size)  {    fprintf(stderr,"unable to allocate back buffer %ld\n", ret);    return FALSE;  }    memset(&(pI830->TexMem), 0, sizeof(pI830->TexMem));  pI830->TexMem.Key = -1;  size = 32768 * 1024;  ret = AllocFromAGP(ctx, pI830, size, align, &pI830->TexMem);  if (ret != size)  {    fprintf(stderr,"unable to allocate texture memory %ld\n", ret);    return FALSE;  }  return TRUE;}static BoolI830BindMemory(const DRIDriverContext *ctx, I830Rec *pI830){  if (BindAgpRange(ctx, &pI830->LpRing->mem))    return FALSE;  if (BindAgpRange(ctx, &pI830->FrontBuffer))    return FALSE;  if (BindAgpRange(ctx, &pI830->BackBuffer))    return FALSE;  if (BindAgpRange(ctx, &pI830->DepthBuffer))    return FALSE;  if (BindAgpRange(ctx, &pI830->ContextMem))    return FALSE;  if (BindAgpRange(ctx, &pI830->TexMem))    return FALSE;  return TRUE;}static BoolI830CleanupDma(const DRIDriverContext *ctx){   drmI830Init info;   memset(&info, 0, sizeof(drmI830Init));   info.func = I830_CLEANUP_DMA;   if (drmCommandWrite(ctx->drmFD, DRM_I830_INIT,		       &info, sizeof(drmI830Init))) {     xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "I830 Dma Cleanup Failed\n");      return FALSE;   }   return TRUE;}static BoolI830InitDma(const DRIDriverContext *ctx, I830Rec *pI830){   I830RingBuffer *ring = pI830->LpRing;   drmI830Init info;   memset(&info, 0, sizeof(drmI830Init));   info.func = I830_INIT_DMA;   info.ring_start = ring->mem.Start + pI830->LinearAddr;   info.ring_end = ring->mem.End + pI830->LinearAddr;   info.ring_size = ring->mem.Size;   info.mmio_offset = (unsigned int)ctx->MMIOStart;   info.sarea_priv_offset = sizeof(drm_sarea_t);   info.front_offset = pI830->FrontBuffer.Start;   info.back_offset = pI830->BackBuffer.Start;   info.depth_offset = pI830->DepthBuffer.Start;   info.w = ctx->shared.virtualWidth;   info.h = ctx->shared.virtualHeight;   info.pitch = ctx->shared.virtualWidth;   info.back_pitch = pI830->BackBuffer.Pitch;   info.depth_pitch = pI830->DepthBuffer.Pitch;   info.cpp = pI830->cpp;   if (drmCommandWrite(ctx->drmFD, DRM_I830_INIT,		       &info, sizeof(drmI830Init))) {      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,		 "I830 Dma Initialization Failed\n");      return FALSE;   }   return TRUE;}static int I830CheckDRMVersion( const DRIDriverContext *ctx,				  I830Rec *pI830 ){   drmVersionPtr  version;   version = drmGetVersion(ctx->drmFD);   if (version) {     int req_minor, req_patch;     req_minor = 4;     req_patch = 0;	     if (version->version_major != 1 ||	 version->version_minor < req_minor ||	 (version->version_minor == req_minor && 	  version->version_patchlevel < req_patch)) {       /* Incompatible drm version */       fprintf(stderr,	       "[dri] I830DRIScreenInit failed because of a version "	       "mismatch.\n"	       "[dri] i915.o kernel module version is %d.%d.%d "	       "but version 1.%d.%d or newer is needed.\n"	       "[dri] Disabling DRI.\n",	       version->version_major,	       version->version_minor,	       version->version_patchlevel,	       req_minor,	       req_patch);       drmFreeVersion(version);       return 0;     }          pI830->drmMinor = version->version_minor;     drmFreeVersion(version);   }   return 1;}static voidI830SetRingRegs(const DRIDriverContext *ctx, I830Rec *pI830){  unsigned int itemp;  unsigned char *MMIO = ctx->MMIOAddress;   OUTREG(LP_RING + RING_LEN, 0);   OUTREG(LP_RING + RING_TAIL, 0);   OUTREG(LP_RING + RING_HEAD, 0);   if ((long)(pI830->LpRing->mem.Start & I830_RING_START_MASK) !=       pI830->LpRing->mem.Start) {      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,		 "I830SetRingRegs: Ring buffer start (%lx) violates its "		 "mask (%x)\n", pI830->LpRing->mem.Start, I830_RING_START_MASK);   }   /* Don't care about the old value.  Reserved bits must be zero anyway. */   itemp = pI830->LpRing->mem.Start & I830_RING_START_MASK;   OUTREG(LP_RING + RING_START, itemp);   if (((pI830->LpRing->mem.Size - 4096) & I830_RING_NR_PAGES) !=       pI830->LpRing->mem.Size - 4096) {      xf86DrvMsg(pScrn->scrnIndex, X_ERROR,		 "I830SetRingRegs: Ring buffer size - 4096 (%lx) violates its "		 "mask (%x)\n", pI830->LpRing->mem.Size - 4096,		 I830_RING_NR_PAGES);   }   /* Don't care about the old value.  Reserved bits must be zero anyway. */   itemp = (pI830->LpRing->mem.Size - 4096) & I830_RING_NR_PAGES;   itemp |= (RING_NO_REPORT | RING_VALID);   OUTREG(LP_RING + RING_LEN, itemp);   pI830->LpRing->head = INREG(LP_RING + RING_HEAD) & I830_HEAD_MASK;   pI830->LpRing->tail = INREG(LP_RING + RING_TAIL);   pI830->LpRing->space = pI830->LpRing->head - (pI830->LpRing->tail + 8);   if (pI830->LpRing->space < 0)      pI830->LpRing->space += pI830->LpRing->mem.Size;      /* RESET THE DISPLAY PIPE TO POINT TO THE FRONTBUFFER - hacky      hacky hacky */   OUTREG(DSPABASE, pI830->FrontBuffer.Start + pI830->LinearAddr);}static BoolI830SetParam(const DRIDriverContext *ctx, int param, int value){   drmI830SetParam sp;   memset(&sp, 0, sizeof(sp));   sp.param = param;   sp.value = value;   if (drmCommandWrite(ctx->drmFD, DRM_I830_SETPARAM, &sp, sizeof(sp))) {      xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "I830 SetParam Failed\n");      return FALSE;   }   return TRUE;}static BoolI830DRIMapScreenRegions(DRIDriverContext *ctx, I830Rec *pI830, drmI830Sarea *sarea){   fprintf(stderr,              "[drm] Mapping front buffer\n");   if (drmAddMap(ctx->drmFD,                 (drm_handle_t)(sarea->front_offset + pI830->LinearAddr),                 sarea->front_size,                 DRM_FRAME_BUFFER,  /*DRM_AGP,*/                 0,                 &sarea->front_handle) < 0) {     fprintf(stderr,	     "[drm] drmAddMap(front_handle) failed. Disabling DRI\n");      return FALSE;   }   ctx->shared.hFrameBuffer = sarea->front_handle;   ctx->shared.fbSize = sarea->front_size;   fprintf(stderr, "[drm] Front Buffer = 0x%08x\n",	   sarea->front_handle);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -