Initial DRI2 support.
[nouveau] / src / nv_dri.c
1 /*
2  * Copyright 2006 Stephane Marchesin
3  * Copyright 2006 Ben Skeggs
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
20  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23
24 #include "nv_include.h"
25
26 #define _XF86DRI_SERVER_
27 #include "GL/glxint.h"
28 #include "GL/glxtokens.h"
29 #include "sarea.h"
30 #include "xf86drm.h"
31 #include "dri.h"
32 #include "nv_dripriv.h"
33 #include "nv_dri.h"
34 #include "drmmode_display.h"
35
36 static Bool NVCreateContext(ScreenPtr pScreen, VisualPtr visual,
37                 drm_context_t hwContext, void *pVisualConfigPriv,
38                 DRIContextType contextStore)
39 {
40         return TRUE;
41 }
42
43
44 static void NVDestroyContext(ScreenPtr pScreen, drm_context_t hwContext,
45                 DRIContextType contextStore)
46 {
47         return;
48 }
49
50 static void NVDRISwapContext(ScreenPtr pScreen, DRISyncType syncType,
51                 DRIContextType oldContextType,
52                 void *oldContext,
53                 DRIContextType newContextType,
54                 void *newContext)
55 {
56         /* we really should do something here */
57         return;
58 }
59
60 static void NVDRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 indx)
61 {   
62         return;
63 }
64
65 static void NVDRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg,
66                 RegionPtr prgnSrc, CARD32 indx)
67 {
68         return;
69 }       
70
71 static void NVDRITransitionTo2d(ScreenPtr pScreen)
72 {
73         return;
74 }
75
76 static void NVDRITransitionTo3d(ScreenPtr pScreen)
77 {
78         return;
79 }
80
81 static void NVDRITransitionSingleToMulti3d(ScreenPtr pScreen)
82 {           
83         return;
84 }           
85         
86 static void NVDRITransitionMultiToSingle3d(ScreenPtr pScreen)
87 {       
88         return;
89 }
90
91 static Bool NVDRIInitVisualConfigs(ScreenPtr pScreen)
92 {
93         ScrnInfoPtr pScrn=xf86Screens[pScreen->myNum];
94         __GLXvisualConfig* pConfigs = NULL;
95         NVConfigPrivPtr pNVConfigs = NULL;
96         NVConfigPrivPtr* pNVConfigPtrs = NULL;
97         int db,depth,alpha,stencil;
98         int depths[]={24,16,0};
99         int num_configs,i;
100
101         switch(pScrn->depth)
102         {
103                 case 16:
104                 case 24:
105                         num_configs=2*3*((pScrn->depth==24)?2:1)*2; /* db*depth*alpha*stencil */
106                         if (!(pConfigs=(__GLXvisualConfig*)xcalloc(sizeof(__GLXvisualConfig),num_configs)))
107                                 return FALSE;
108                         if (!(pNVConfigs=(NVConfigPrivPtr)xcalloc(sizeof(NVConfigPrivRec), num_configs))) {
109                                 xfree(pConfigs);
110                                 return FALSE;
111                         }
112                         if (!(pNVConfigPtrs=(NVConfigPrivPtr *)xcalloc(sizeof(NVConfigPrivPtr),num_configs))) {
113                                 xfree(pConfigs);
114                                 xfree(pNVConfigs);
115                                 return FALSE;
116                         }
117
118                         i = 0;
119                         for(db=1;db>=0;db--)
120                         for(depth=0;depth<3;depth++)
121                         for(alpha=0;alpha<((pScrn->depth==24)?2:1);alpha++)
122                         for(stencil=0;stencil<2;stencil++)
123                         {
124                                 pConfigs[i].vid                = (VisualID)(-1);
125                                 pConfigs[i].class              = -1;
126                                 pConfigs[i].rgba               = TRUE;
127                                 if (pScrn->depth==16)
128                                 {                                       
129                                         pConfigs[i].redSize            = 5;
130                                         pConfigs[i].greenSize          = 6;
131                                         pConfigs[i].blueSize           = 5;
132                                         pConfigs[i].alphaSize          = 0;
133                                         pConfigs[i].redMask            = 0x0000F800;
134                                         pConfigs[i].greenMask          = 0x000007E0;
135                                         pConfigs[i].blueMask           = 0x0000001F;
136                                         pConfigs[i].alphaMask          = 0x00000000;
137                                 } else {
138                                         pConfigs[i].redSize            = 8;
139                                         pConfigs[i].greenSize          = 8;
140                                         pConfigs[i].blueSize           = 8;
141                                         pConfigs[i].redMask            = 0x00FF0000;
142                                         pConfigs[i].greenMask          = 0x0000FF00;
143                                         pConfigs[i].blueMask           = 0x000000FF;
144                                         if (alpha) {
145                                                 pConfigs[i].alphaSize          = 8;
146                                                 pConfigs[i].alphaMask          = 0xFF000000;
147                                         } else {
148                                                 pConfigs[i].alphaSize          = 0;
149                                                 pConfigs[i].alphaMask          = 0x00000000;
150                                         }
151                                 }
152
153                                 pConfigs[i].accumRedSize   = 0;
154                                 pConfigs[i].accumGreenSize = 0;
155                                 pConfigs[i].accumBlueSize  = 0;
156                                 pConfigs[i].accumAlphaSize = 0;
157                                 if (db)
158                                         pConfigs[i].doubleBuffer   = TRUE;
159                                 else
160                                         pConfigs[i].doubleBuffer   = FALSE;
161                                 pConfigs[i].stereo             = FALSE;
162                                 pConfigs[i].bufferSize         = pScrn->depth;
163                                 if (depths[depth] == 24 && stencil) {
164                                         pConfigs[i].depthSize          = depths[depth];
165                                         pConfigs[i].stencilSize        = 8;
166                                 } else {
167                                         pConfigs[i].depthSize          = depths[depth];
168                                         pConfigs[i].stencilSize        = 0;
169                                 }
170                                 pConfigs[i].auxBuffers         = 0;
171                                 pConfigs[i].level              = 0;
172                                 pConfigs[i].visualRating       = GLX_NONE;
173                                 pConfigs[i].transparentPixel   = GLX_NONE;
174                                 pConfigs[i].transparentRed     = 0;
175                                 pConfigs[i].transparentGreen   = 0;
176                                 pConfigs[i].transparentBlue    = 0;
177                                 pConfigs[i].transparentAlpha   = 0;
178                                 pConfigs[i].transparentIndex   = 0;
179                                 i++;
180                         }
181                         break;
182                 default:
183                         xf86DrvMsg(pScreen->myNum, X_ERROR, "[dri] no DRI at %d bpp ",pScrn->depth);
184                         return FALSE;
185         }
186         GlxSetVisualConfigs(num_configs, pConfigs, (void**)pNVConfigPtrs);
187         return TRUE;
188 }
189
190 Bool NVDRIGetVersion(ScrnInfoPtr pScrn)
191 {
192         NVPtr pNv = NVPTR(pScrn);
193         char *busId;
194         int fd = 0;
195
196 #ifdef XF86DRM_MODE
197         /* drm already open */
198         if (pNv->drmmode) {
199                 drmmode_ptr drmmode = pNv->drmmode;
200                 fd = drmmode->fd;
201         }
202 #endif
203
204         {
205                 pointer ret;
206                 int errmaj, errmin;
207
208                 ret = LoadSubModule(pScrn->module, "dri", NULL, NULL, NULL,
209                                     NULL, &errmaj, &errmin);
210                 if (!ret) {
211                         xf86DrvMsg(pScrn->scrnIndex, X_INFO,
212                                         "error %d\n", errmaj);
213                         LoaderErrorMsg(pScrn->name, "dri", errmaj, errmin);
214                 }
215
216                 if (!ret && errmaj != LDR_ONCEONLY)
217                         return FALSE;
218         }
219
220         xf86LoaderReqSymLists(drmSymbols, NULL);
221         xf86LoaderReqSymLists(driSymbols, NULL);
222         xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Loaded DRI module\n");
223
224         if (!fd) {
225                 busId = DRICreatePCIBusID(pNv->PciInfo);
226
227                 fd = drmOpen("nouveau", busId);
228                 xfree(busId);
229         }
230         if (fd < 0) {
231                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
232                         "[dri] Failed to open the DRM\n");
233                 return FALSE;
234         }
235
236         /* Check the lib version */
237         if (xf86LoaderCheckSymbol("drmGetLibVersion"))
238                 pNv->pLibDRMVersion = drmGetLibVersion(0);
239         if (pNv->pLibDRMVersion == NULL) {
240                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
241                 "NVDRIGetVersion failed because libDRM is really "
242                 "way to old to even get a version number out of it.\n"
243                 "[dri] Disabling DRI.\n");
244                 return FALSE;
245         }
246
247         pNv->pKernelDRMVersion = drmGetVersion(fd);
248 #ifdef XF86DRM_MODE
249         if (!pNv->drmmode) /* drmmode still needs the file descriptor */
250 #endif
251         {
252                 drmClose(fd);
253         }
254
255         if (pNv->pKernelDRMVersion == NULL) {
256                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
257                         "failed to get DRM version\n");
258                 return FALSE;
259         }
260         
261         /* temporary lock step versioning */
262         if (pNv->pKernelDRMVersion->version_patchlevel !=
263                         NOUVEAU_DRM_HEADER_PATCHLEVEL) {
264                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
265                         "wrong DRM version\n");
266                 return FALSE;
267         }
268
269         return TRUE;
270 }
271
272 Bool NVDRICheckModules(ScrnInfoPtr pScrn)
273 {
274         if (!xf86LoaderCheckSymbol("GlxSetVisualConfigs")) {
275                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
276                            "[dri] GlxSetVisualConfigs not found.\n");
277                 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
278                            "      NVIDIA's glx present, or glx not loaded.\n");
279                 return FALSE;
280         }
281
282         return TRUE;
283 }
284
285 Bool NVDRIScreenInit(ScrnInfoPtr pScrn)
286 {
287         DRIInfoPtr     pDRIInfo;
288         NOUVEAUDRIPtr  pNOUVEAUDRI;
289         NVPtr pNv = NVPTR(pScrn);
290         ScreenPtr pScreen;
291         pScreen = screenInfo.screens[pScrn->scrnIndex];
292         int drm_page_size;
293         int drm_fd;
294
295 #ifdef XF86DRM_MODE
296         /* drm already open, reuse it */
297         if (pNv->drmmode) {
298                 drmmode_ptr drmmode = pNv->drmmode;
299                 drm_fd = drmmode->fd;
300         }
301 #endif
302
303         if (!NVDRICheckModules(pScrn))
304                 return FALSE;
305
306         drm_page_size = getpagesize();
307         if (!(pDRIInfo = DRICreateInfoRec())) return FALSE;
308
309         pNv->pDRIInfo                        = pDRIInfo;
310         pDRIInfo->drmDriverName              = "nouveau";
311         pDRIInfo->clientDriverName           = "nouveau";
312         pDRIInfo->busIdString                = DRICreatePCIBusID(pNv->PciInfo);
313
314         pDRIInfo->ddxDriverMajorVersion      = NV_MAJOR_VERSION;
315         pDRIInfo->ddxDriverMinorVersion      = NV_MINOR_VERSION;
316         pDRIInfo->ddxDriverPatchVersion      = NV_PATCHLEVEL;
317
318         /*
319          * We set the FB to be in the higher half of VRAM. If we don't, any
320          * VRAM allocations before the FB is mapped will change that map
321          * and we fail.
322          * We should detect when the DRM decides to change the FB area
323          * but we currently don't know how to.
324          */
325         pDRIInfo->frameBufferSize            = 0x1000;
326         pDRIInfo->frameBufferPhysicalAddress = (void *)pNv->VRAMPhysical;
327         pDRIInfo->frameBufferStride          = pScrn->displayWidth * pScrn->bitsPerPixel/8;
328
329         pDRIInfo->ddxDrawableTableEntry      = 1;
330         pDRIInfo->maxDrawableTableEntry      = 1;
331
332         if (!(pNOUVEAUDRI = (NOUVEAUDRIPtr)xcalloc(sizeof(NOUVEAUDRIRec), 1))) {
333                 DRIDestroyInfoRec(pDRIInfo);
334                 pNv->pDRIInfo = NULL;
335                 return FALSE;
336         }
337         pDRIInfo->devPrivate                 = pNOUVEAUDRI; 
338         pDRIInfo->devPrivateSize             = sizeof(NOUVEAUDRIRec);
339         pDRIInfo->contextSize                = sizeof(NVDRIContextRec);
340         pDRIInfo->SAREASize                  = (drm_page_size > SAREA_MAX) ? drm_page_size : SAREA_MAX;
341
342         pDRIInfo->CreateContext              = NVCreateContext;
343         pDRIInfo->DestroyContext             = NVDestroyContext;
344         pDRIInfo->SwapContext                = NVDRISwapContext;
345         pDRIInfo->InitBuffers                = NVDRIInitBuffers;
346         pDRIInfo->MoveBuffers                = NVDRIMoveBuffers;
347         pDRIInfo->bufferRequests             = DRI_ALL_WINDOWS;
348         pDRIInfo->TransitionTo2d             = NVDRITransitionTo2d;
349         pDRIInfo->TransitionTo3d             = NVDRITransitionTo3d;
350         pDRIInfo->TransitionSingleToMulti3D  = NVDRITransitionSingleToMulti3d;
351         pDRIInfo->TransitionMultiToSingle3D  = NVDRITransitionMultiToSingle3d;
352
353         pDRIInfo->createDummyCtx     = FALSE;
354         pDRIInfo->createDummyCtxPriv = FALSE;
355
356         if (!DRIScreenInit(pScreen, pDRIInfo, &drm_fd)) {
357                 xf86DrvMsg(pScreen->myNum, X_ERROR,
358                                 "[dri] DRIScreenInit failed.  Disabling DRI.\n");
359                 xfree(pDRIInfo->devPrivate);
360                 pDRIInfo->devPrivate = NULL;
361                 DRIDestroyInfoRec(pDRIInfo);
362                 pDRIInfo = NULL;
363                 return FALSE;
364         }
365
366         if (!NVDRIInitVisualConfigs(pScreen)) {
367                 xf86DrvMsg(pScreen->myNum, X_ERROR,
368                                 "[dri] NVDRIInitVisualConfigs failed.  Disabling DRI.\n");
369                 xfree(pDRIInfo->devPrivate);
370                 pDRIInfo->devPrivate = NULL;
371                 DRIDestroyInfoRec(pDRIInfo);
372                 pDRIInfo = NULL;
373                 return FALSE;
374         }
375
376         /* need_close = 0, because DRICloseScreen() will handle the closing. */
377         if (nouveau_device_open_existing(&pNv->dev, 0, drm_fd, 0)) {
378                 xf86DrvMsg(pScreen->myNum, X_ERROR, "Error creating device\n");
379                 xfree(pDRIInfo->devPrivate);
380                 pDRIInfo->devPrivate = NULL;
381                 DRIDestroyInfoRec(pDRIInfo);
382                 pDRIInfo = NULL;
383                 return FALSE;
384         }
385
386         return TRUE;
387 }
388
389 Bool NVDRIFinishScreenInit(ScrnInfoPtr pScrn)
390 {
391         ScreenPtr      pScreen = screenInfo.screens[pScrn->scrnIndex];
392         NVPtr          pNv = NVPTR(pScrn);
393         NOUVEAUDRIPtr  pNOUVEAUDRI;
394
395         if (!DRIFinishScreenInit(pScreen)) {
396                 return FALSE;
397         }
398
399         pNOUVEAUDRI                     = (NOUVEAUDRIPtr)pNv->pDRIInfo->devPrivate;
400
401         pNOUVEAUDRI->device_id          = pNv->Chipset;
402
403         pNOUVEAUDRI->width              = pScrn->virtualX;
404         pNOUVEAUDRI->height             = pScrn->virtualY;
405         pNOUVEAUDRI->depth              = pScrn->depth;
406         pNOUVEAUDRI->bpp                = pScrn->bitsPerPixel;
407
408         if (nouveau_bo_handle_get(pNv->FB, &pNOUVEAUDRI->front_handle))
409                 return FALSE;
410         pNOUVEAUDRI->front_pitch        = pScrn->displayWidth;
411
412         return TRUE;
413 }
414
415 void NVDRICloseScreen(ScrnInfoPtr pScrn)
416 {
417         ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
418         NVPtr pNv = NVPTR(pScrn);
419
420         nouveau_device_close(&pNv->dev);
421
422         DRICloseScreen(pScreen);
423
424         /* The channel should have been removed from the drm side, that still leaves a memory leak though. */
425         if (pNv->chan) {
426                 free(pNv->chan);
427                 pNv->chan = NULL;
428         }
429
430         if (pNv->pDRIInfo) {
431                 if (pNv->pDRIInfo->devPrivate) {
432                         xfree(pNv->pDRIInfo->devPrivate);
433                         pNv->pDRIInfo->devPrivate = NULL;
434                 }
435                 DRIDestroyInfoRec(pNv->pDRIInfo);
436                 pNv->pDRIInfo = NULL;
437         }
438 }
439