Need to include nv_include.h before checking that XF86DRI is definied. XF86DRI lives...
[nouveau] / src / nv_dri.c
1 #include "nv_include.h"
2 #ifdef XF86DRI
3 #define _XF86DRI_SERVER_
4 #include "GL/glxint.h"
5 #include "sarea.h"
6 #include "xf86drm.h"
7 #include "dri.h"
8
9
10 static Bool NVCreateContext(ScreenPtr pScreen, VisualPtr visual,
11                 drm_context_t hwContext, void *pVisualConfigPriv,
12                 DRIContextType contextStore)
13 {
14         return TRUE;
15 }
16
17
18 static void NVDestroyContext(ScreenPtr pScreen, drm_context_t hwContext,
19                 DRIContextType contextStore)
20 {
21         return;
22 }
23
24 static void NVDRISwapContext(ScreenPtr pScreen, DRISyncType syncType,
25                 DRIContextType oldContextType,
26                 void *oldContext,
27                 DRIContextType newContextType,
28                 void *newContext)
29 {
30         /* we really should do something here */
31         return;
32 }
33
34 static void NVDRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 indx)
35 {   
36         return;
37 }
38
39 static void NVDRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg,
40                 RegionPtr prgnSrc, CARD32 indx)
41 {
42         return;
43 }       
44
45
46 Bool NVDRIScreenInit(ScrnInfoPtr pScrn)
47 {
48     DRIInfoPtr     pDRIInfo;
49     NVPtr pNv = NVPTR(pScrn);
50     drmVersionPtr drm_version;
51     ScreenPtr pScreen;
52     pScreen = screenInfo.screens[pScrn->scrnIndex];
53
54     if (!xf86LoadSubModule(pScrn, "dri")) {
55         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
56                    "Could not load DRI module\n");
57         return FALSE;
58     }
59
60     xf86LoaderReqSymLists(drmSymbols, NULL);
61     xf86LoaderReqSymLists(driSymbols, NULL);
62     xf86DrvMsg(pScrn->scrnIndex, X_INFO,
63                     "Loaded DRI module\n");
64
65     if (!(pDRIInfo = DRICreateInfoRec())) return FALSE;
66     
67     pNv->pDRIInfo                        = pDRIInfo;
68     pDRIInfo->drmDriverName              = "nouveau";
69
70     pDRIInfo->frameBufferPhysicalAddress = (void *)pNv->FbAddress;
71     pDRIInfo->frameBufferSize            = pNv->FbUsableSize;
72     pDRIInfo->frameBufferStride          = pScrn->displayWidth * pScrn->bitsPerPixel/8;
73  
74     pDRIInfo->ddxDrawableTableEntry      = 1;
75     pDRIInfo->maxDrawableTableEntry      = 1;
76
77     pDRIInfo->devPrivate                 = NULL; 
78     pDRIInfo->devPrivateSize             = 0;
79     pDRIInfo->contextSize                = 0;
80     pDRIInfo->SAREASize                  = SAREA_MAX;
81     
82     pDRIInfo->CreateContext              = NVCreateContext;
83     pDRIInfo->DestroyContext             = NVDestroyContext;
84     pDRIInfo->SwapContext                = NVDRISwapContext;
85     pDRIInfo->InitBuffers                = NVDRIInitBuffers;
86     pDRIInfo->MoveBuffers                = NVDRIMoveBuffers;
87     pDRIInfo->bufferRequests             = DRI_ALL_WINDOWS;
88     if (!DRIScreenInit(pScreen, pDRIInfo, &pNv->drm_fd)) {
89         xf86DrvMsg(pScreen->myNum, X_ERROR,
90                     "[dri] DRIScreenInit failed.  Disabling DRI.\n");
91         xfree(pDRIInfo->devPrivate);
92         pDRIInfo->devPrivate = NULL;
93         DRIDestroyInfoRec(pDRIInfo);
94         pDRIInfo = NULL;
95         return FALSE;
96     }
97     drm_version = drmGetVersion(pNv->drm_fd);
98
99     xf86DrvMsg(pScrn->scrnIndex, X_INFO,
100                "DRM version: %d.%d.%d (name: %s)\n",
101                drm_version->version_major, 
102                drm_version->version_minor, 
103                drm_version->version_patchlevel,
104                drm_version->name);
105
106     return TRUE;
107 }
108
109 Bool NVInitAGP(ScrnInfoPtr pScrn)
110 {
111     NVPtr pNv = NVPTR(pScrn);
112     int agp_mode;
113     unsigned long agp_size;
114
115     if (drmAgpAcquire(pNv->drm_fd)) {
116         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
117                    "Could not access AGP, disabling DMA transfers\n");
118         return FALSE;
119     }        
120
121     agp_size = drmAgpSize(pNv->drm_fd);
122     pNv->agpSize = agp_size < 16*0x100000 ? agp_size : 16*0x100000;
123     pNv->agpPhysical = drmAgpBase(pNv->drm_fd);
124     
125     agp_mode = drmAgpGetMode(pNv->drm_fd);
126     xf86DrvMsg(pScrn->scrnIndex, X_INFO,
127                "AGP: version %d.%d\n"
128                "     mode %x\n"
129                "     base=%08lx size=%08lx (%08lx)\n"
130                , drmAgpVersionMajor(pNv->drm_fd),
131                drmAgpVersionMinor(pNv->drm_fd), agp_mode,
132                pNv->agpPhysical, agp_size, pNv->agpSize);
133
134     if (drmAgpEnable(pNv->drm_fd, agp_mode) < 0) {
135         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
136                    "could not enable AGP\n");
137         return FALSE;
138     }
139
140     if (drmAgpAlloc(pNv->drm_fd, pNv->agpSize, 0, 0, &pNv->drm_agp_handle)) {
141         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
142                    "could not allocate AGP memory\n");
143         return FALSE;
144     }
145
146     if (drmAgpBind(pNv->drm_fd, pNv->drm_agp_handle, 0)) {
147         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
148                    "could not bind AGP memory\n");
149         return FALSE;
150     }
151
152     if (drmAddMap(pNv->drm_fd, 0, pNv->agpSize, /* agp_size, */
153                   DRM_AGP, 0,
154                   &pNv->drm_agp_map_handle)) {
155         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
156                    "could not add AGP map, %s\n", strerror(errno));
157         return FALSE;
158     }        
159     
160     xf86DrvMsg(pScrn->scrnIndex, X_INFO,
161                "agp map handle=%08lx\n", pNv->drm_agp_map_handle);
162     /* no idea why the handle is set to 0 in the addMap call. */
163     
164     if (drmMap(pNv->drm_fd, pNv->agpPhysical, pNv->agpSize /* agp_size */, (drmAddressPtr)&pNv->agpMemory)) {
165         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
166                    "could not map AGP memory: %s\n", strerror(errno));
167         return FALSE;
168     }
169
170     return TRUE;
171     
172 #if 0
173     {
174         char *tmp = malloc(pNv->agpSize);
175         struct timeval tv, tv2;
176         gettimeofday(&tv, 0);
177         memcpy(tmp, pNv->agpMemory, pNv->agpSize);
178         gettimeofday(&tv2, 0);
179         xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
180                    "agp: Memory benchmark %f MB/s\n",
181                    (pNv->agpSize/(1024.*1024.))*1000000
182                    /((int)tv2.tv_usec- (int)tv.tv_usec + 1000000*(tv2.tv_sec-tv.tv_sec)+1));
183         free(tmp);
184     }
185 #endif
186 }
187 #endif
188
189
190
191