summaryrefslogtreecommitdiff
path: root/src/ffb_dri.c
blob: 209a9d3338e3fe78c3ee2b12799302dc2eb41de1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/sunffb/ffb_dri.c,v 1.8 2001/04/18 14:52:42 dawes Exp $
 * Acceleration for the Creator and Creator3D framebuffer - DRI/DRM support.
 *
 * Copyright (C) 2000 David S. Miller (davem@redhat.com)
 *
 * 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, sublicense, 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 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * DAVID MILLER 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.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "xf86.h"
#include "xf86_OSproc.h"
#include "xf86_ansic.h"
#include "xf86Priv.h"

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

#include "miline.h"

#include "GL/glxtokens.h"

#include "xf86drm.h"
#include "sarea.h"
#define _XF86DRI_SERVER_
#include "dri.h"

#include "GL/glxint.h"

#include "ffb.h"
#include "ffb_regs.h"
#include "ffb_fifo.h"
#include "ffb_rcache.h"

static char FFBKernelDriverName[] = "ffb";
static char FFBClientDriverName[] = "ffb";

/* Forward declarations. */
static Bool FFBDRICreateContext(ScreenPtr, VisualPtr, drm_context_t,
				void *, DRIContextType);
static void FFBDRIDestroyContext(ScreenPtr, drm_context_t, DRIContextType);

static void FFBDRIInitBuffers(WindowPtr, RegionPtr, CARD32);
static void FFBDRIMoveBuffers(WindowPtr, DDXPointRec, RegionPtr, CARD32);

static void FFBDRISetDrawableIndex(WindowPtr, CARD32);

/* XXX Why isn't this in a header somewhere? XXX */
extern void GlxSetVisualConfigs(int nconfigs, __GLXvisualConfig *configs,
				void **configprivs);

static Bool
FFBDRIInitVisualConfigs(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	FFBPtr pFfb = GET_FFB_FROM_SCRN(pScrn);
	__GLXvisualConfig *pConfigs;
	FFBConfigPrivPtr pFfbConfigs;
	FFBConfigPrivPtr *pFfbConfigPtrs;

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

	pFfbConfigs = (FFBConfigPrivPtr)
		xcalloc(sizeof(FFBConfigPrivRec), 1);
	if (!pFfbConfigs) {
		xfree(pConfigs);
		return FALSE;
	}

	pFfbConfigPtrs = (FFBConfigPrivPtr *)
		xcalloc(sizeof(FFBConfigPrivPtr), 1);
	if (!pFfbConfigPtrs) {
		xfree(pConfigs);
		xfree(pFfbConfigs);
		return FALSE;
	}

	pFfbConfigPtrs[0] = &pFfbConfigs[0];

	pConfigs->vid = -1;
	pConfigs->class = -1;
	pConfigs->rgba = TRUE;
	pConfigs->redSize = 8;
	pConfigs->greenSize = 8;
	pConfigs->blueSize = 8;
	pConfigs->alphaSize = 0;
	pConfigs->redMask = 0x000000ff;
	pConfigs->greenMask = 0x0000ff00;
	pConfigs->blueMask = 0x00ff0000;
	pConfigs->alphaMask = 0;
	pConfigs->accumRedSize = 0;
	pConfigs->accumGreenSize = 0;
	pConfigs->accumBlueSize = 0;
	pConfigs->accumAlphaSize = 0;
	pConfigs->doubleBuffer = TRUE;
	pConfigs->stereo = FALSE;
	pConfigs->bufferSize = 32;
	pConfigs->depthSize = 16;
	pConfigs->stencilSize = 0;
	pConfigs->auxBuffers = 0;
	pConfigs->level = 0;
	pConfigs->visualRating = GLX_NONE;
	pConfigs->transparentPixel = GLX_NONE;
	pConfigs->transparentRed = 0;
	pConfigs->transparentGreen = 0;
	pConfigs->transparentBlue = 0;
	pConfigs->transparentAlpha = 0;
	pConfigs->transparentIndex = 0;

	pFfb->numVisualConfigs = 1;
	pFfb->pVisualConfigs = pConfigs;
	pFfb->pVisualConfigsPriv = pFfbConfigs;

	GlxSetVisualConfigs(1, pConfigs, (void **)pFfbConfigPtrs);

	return TRUE;
}

static void
init_ffb_sarea(FFBPtr pFfb, ffb_dri_state_t *pFfbSarea)
{
	int i;

	pFfbSarea->flags = 0;

	switch (pFfb->ffb_type) {
	case ffb2_prototype:
	case ffb2_vertical:
	case ffb2_vertical_plus:
	case ffb2_horizontal:
	case ffb2_horizontal_plus:
		pFfbSarea->flags |= FFB_DRI_FFB2;
		break;

	default:
		break;
	};

	if (pFfb->ffb_type == ffb2_vertical_plus ||
	    pFfb->ffb_type == ffb2_horizontal_plus)
		pFfbSarea->flags |= FFB_DRI_FFB2PLUS;

	if (pFfb->dac_info.flags & FFB_DAC_PAC1)
		pFfbSarea->flags |= FFB_DRI_PAC1;

	if (pFfb->dac_info.flags & FFB_DAC_PAC2)
		pFfbSarea->flags |= FFB_DRI_PAC2;

	for (i = 0; i < FFB_DRI_NWIDS; i++)
		pFfbSarea->wid_table[i] = 0;
}

#define	FFB_DFB24_POFF		0x02000000UL
#define	FFB_DFB24_SIZE		0x01000000UL

#define	FFB_FBC_REGS_POFF	0x00600000UL
#define	FFB_FBC_REGS_SIZE	0x00002000UL

#define	FFB_DAC_POFF		0x00400000UL
#define	FFB_DAC_SIZE		0x00002000UL

#define	FFB_SFB8R_POFF		0x04000000UL
#define FFB_SFB8R_SIZE		0x00400000UL

#define	FFB_SFB32_POFF		0x05000000UL
#define FFB_SFB32_SIZE		0x01000000UL

#define	FFB_SFB64_POFF		0x06000000UL
#define FFB_SFB64_SIZE		0x02000000UL

Bool
FFBDRIScreenInit(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	FFBPtr pFfb = GET_FFB_FROM_SCRN(pScrn);
	DRIInfoPtr pDRIInfo;
	FFBDRIPtr pFfbDRI;

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

        /* Check the DRI version */
        {
      		int major, minor, patch;
		DRIQueryVersion(&major, &minor, &patch);
		if (major != DRIINFO_MAJOR_VERSION || minor < DRIINFO_MINOR_VERSION) {
		xf86DrvMsg(pScreen->myNum, X_ERROR,
                    "[dri] FFBDRIScreenInit failed because of a version mismatch.\n"
		    "[dri] libdri version is %d.%d.%d but version %d.%d.x is needed.\n"
		    "[dri]  Disabling DRI.\n",
                    major, minor, patch,
                    DRIINFO_MAJOR_VERSION, DRIINFO_MINOR_VERSION);
		return FALSE;
		}
	}

	pDRIInfo = DRICreateInfoRec();
	if (pDRIInfo == NULL)
		return FALSE;

	pFfb->pDRIInfo = pDRIInfo;

	pDRIInfo->drmDriverName = FFBKernelDriverName;
	pDRIInfo->clientDriverName = FFBClientDriverName;

	pDRIInfo->ddxDriverMajorVersion = 0;
	pDRIInfo->ddxDriverMinorVersion = 1;
	pDRIInfo->ddxDriverPatchVersion = 1;

	pDRIInfo->busIdString = xalloc(64); /* Freed in DRIDestroyInfoRec */
	sprintf(pDRIInfo->busIdString, "SBUS:%s", pFfb->psdp->device);

	/* Dumb rendering port for now... */
	pDRIInfo->frameBufferPhysicalAddress = FFB_DFB24_POFF;
	pDRIInfo->frameBufferSize = FFB_DFB24_SIZE;
	pDRIInfo->frameBufferStride = (2048 * 4);

	/* XXX */
	pDRIInfo->ddxDrawableTableEntry = 15;
	pDRIInfo->maxDrawableTableEntry = 15;
	pDRIInfo->SAREASize = (SAREA_MAX + (0x2000 - 1)) & ~(0x2000 - 1);

	pFfbDRI = (FFBDRIPtr) xcalloc(sizeof(FFBDRIRec), 1);
	if (pFfbDRI == NULL) {
		DRIDestroyInfoRec(pFfb->pDRIInfo);
		return FALSE;
	}

	pDRIInfo->devPrivate		= pFfbDRI;
	pDRIInfo->devPrivateSize	= sizeof(*pFfbDRI);
	pDRIInfo->contextSize		= 0; /* kernel does ctx swaps */

	pDRIInfo->CreateContext		= FFBDRICreateContext;
	pDRIInfo->DestroyContext	= FFBDRIDestroyContext;
	pDRIInfo->InitBuffers		= FFBDRIInitBuffers;
	pDRIInfo->MoveBuffers		= FFBDRIMoveBuffers;
	pDRIInfo->SetDrawableIndex	= FFBDRISetDrawableIndex;

	/* Our InitBuffers depends heavily on this setting. */
	pDRIInfo->bufferRequests	= DRI_3D_WINDOWS_ONLY;

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

	if (!DRIScreenInit(pScreen, pDRIInfo, &(pFfb->drmSubFD))) {
                xf86DrvMsg(pScreen->myNum, X_ERROR,
                           "[dri] DRIScreenInit failed.  Disabling DRI.\n");
		DRIDestroyInfoRec(pFfb->pDRIInfo);
		xfree(pFfbDRI);
		return FALSE;
	}

#if 000 /* XXX this should be cleaned up and used */
        /* Check the ffb DRM version */
        version = drmGetVersion(info->drmFD);
        if (version) {
           if (version->version_major != 1 ||
               version->version_minor < 0) {
              /* incompatible drm version */
              xf86DrvMsg(pScreen->myNum, X_ERROR,
                         "[dri] FFBDRIScreenInit failed because of a version mismatch.\n"
                         "[dri] ffb.o kernel module version is %d.%d.%d but version 1.0.x is needed.\n"
                         "[dri] Disabling the DRI.\n",
                         version->version_major,
                         version->version_minor,
                         version->version_patchlevel);
              drmFreeVersion(version);
              R128DRICloseScreen(pScreen);
	    return FALSE;
           }
           drmFreeVersion(version);
        }
#endif

	pFfb->pFfbSarea = DRIGetSAREAPrivate(pScreen);
	init_ffb_sarea(pFfb, pFfb->pFfbSarea);

	/* Setup device specific direct rendering memory maps. */
	if (drmAddMap(pFfb->drmSubFD,
		      FFB_FBC_REGS_POFF, FFB_FBC_REGS_SIZE,
		      DRM_REGISTERS, 0, &pFfbDRI->hFbcRegs) < 0) {
		DRICloseScreen(pScreen);
		return FALSE;
	}
	pFfbDRI->sFbcRegs = FFB_FBC_REGS_SIZE;

	xf86DrvMsg(pScreen->myNum, X_INFO,
		   "[drm] FBC Register handle = 0x%08x\n",
		   pFfbDRI->hFbcRegs);

	if (drmAddMap(pFfb->drmSubFD,
		      FFB_DAC_POFF, FFB_DAC_SIZE,
		      DRM_REGISTERS, 0, &pFfbDRI->hDacRegs) < 0) {
		DRICloseScreen(pScreen);
		return FALSE;
	}
	pFfbDRI->sDacRegs = FFB_DAC_SIZE;

	xf86DrvMsg(pScreen->myNum, X_INFO,
		   "[drm] DAC Register handle = 0x%08x\n",
		   pFfbDRI->hDacRegs);

	/* Now add maps for the "Smart" views of the framebuffer. */
	if (drmAddMap(pFfb->drmSubFD,
		      FFB_SFB8R_POFF, FFB_SFB8R_SIZE,
		      DRM_REGISTERS, 0, &pFfbDRI->hSfb8r) < 0) {
		DRICloseScreen(pScreen);
		return FALSE;
	}
	pFfbDRI->sSfb8r = FFB_SFB8R_SIZE;

	xf86DrvMsg(pScreen->myNum, X_INFO,
		   "[drm] SFB8R handle = 0x%08x\n",
		   pFfbDRI->hSfb8r);

	if (drmAddMap(pFfb->drmSubFD,
		      FFB_SFB32_POFF, FFB_SFB32_SIZE,
		      DRM_REGISTERS, 0, &pFfbDRI->hSfb32) < 0) {
		DRICloseScreen(pScreen);
		return FALSE;
	}
	pFfbDRI->sSfb32 = FFB_SFB32_SIZE;

	xf86DrvMsg(pScreen->myNum, X_INFO,
		   "[drm] SFB32 handle = 0x%08x\n",
		   pFfbDRI->hSfb32);

	if (drmAddMap(pFfb->drmSubFD,
		      FFB_SFB64_POFF, FFB_SFB64_SIZE,
		      DRM_REGISTERS, 0, &pFfbDRI->hSfb64) < 0) {
		DRICloseScreen(pScreen);
		return FALSE;
	}
	pFfbDRI->sSfb64 = FFB_SFB64_SIZE;

	xf86DrvMsg(pScreen->myNum, X_INFO,
		   "[drm] SFB64 handle = 0x%08x\n",
		   pFfbDRI->hSfb64);

	/* Setup visual configurations. */
	if (!FFBDRIInitVisualConfigs(pScreen)) {
		DRICloseScreen(pScreen);
		return FALSE;
	}

	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		   "[drm] Visual configs initialized\n");

	return TRUE;
}

void
FFBDRICloseScreen(ScreenPtr pScreen)
{
	FFBPtr pFfb = GET_FFB_FROM_SCREEN(pScreen);

	DRICloseScreen(pScreen);

	if (pFfb->pDRIInfo) {
		DRIInfoPtr pDRIInfo = pFfb->pDRIInfo;

		if (pDRIInfo->devPrivate)
			xfree(pDRIInfo->devPrivate);
		DRIDestroyInfoRec(pDRIInfo);
		pFfb->pDRIInfo = NULL;
	}

	if (pFfb->pVisualConfigs) {
		xfree(pFfb->pVisualConfigs);
		pFfb->pVisualConfigs = NULL;
	}
	if (pFfb->pVisualConfigsPriv) {
		xfree(pFfb->pVisualConfigsPriv);
		pFfb->pVisualConfigsPriv = NULL;
	}
}

static Bool
FFBDRICreateContext(ScreenPtr pScreen, VisualPtr visual, drm_context_t hwContext,
		 void *pVisualConfigPriv, DRIContextType context)
{
	/* Nothing to do... */
	return TRUE;
}

static void
FFBDRIDestroyContext(ScreenPtr pScreen, drm_context_t hwContext, DRIContextType context)
{
	/* Nothing to do... */
}

Bool
FFBDRIFinishScreenInit(ScreenPtr pScreen)
{
	FFBPtr pFfb = GET_FFB_FROM_SCREEN(pScreen);
	DRIInfoPtr pDRIInfo = pFfb->pDRIInfo;
	FFBDRIPtr pFfbDRI = (FFBDRIPtr) pDRIInfo->devPrivate;
	int i;

	/* This belongs in the kernel.  I'm sorry, the rest
	 * of the current DRI switching mechanisms just suck.
	 */
	pDRIInfo->driverSwapMethod = DRI_KERNEL_SWAP;

	/* Copy over the fast/page filling parameters now that
	 * acceleration has been fully setup.
	 */
	pFfbDRI->disable_pagefill = pFfb->disable_pagefill;
	pFfbDRI->fastfill_small_area = FFB_FFPARMS(pFfb).fastfill_small_area;
	pFfbDRI->pagefill_small_area = FFB_FFPARMS(pFfb).pagefill_small_area;
	pFfbDRI->fastfill_height = FFB_FFPARMS(pFfb).fastfill_height;
	pFfbDRI->fastfill_width = FFB_FFPARMS(pFfb).fastfill_width;
	pFfbDRI->pagefill_height = FFB_FFPARMS(pFfb).pagefill_height;
	pFfbDRI->pagefill_width = FFB_FFPARMS(pFfb).pagefill_width;
	for (i = 0; i < 0x800; i++)
		pFfbDRI->Pf_AlignTab[i] = pFfb->Pf_AlignTab[i];

	return DRIFinishScreenInit(pScreen);
}

static void
FFBDRIInitBuffers(WindowPtr pWin, RegionPtr prgn, CARD32 index)
{
	ScreenPtr pScreen = pWin->drawable.pScreen;
	FFBPtr pFfb = GET_FFB_FROM_SCREEN(pScreen);
	CreatorPrivWinPtr pFfbPrivWin = CreatorGetWindowPrivate(pWin);
	ffb_fbcPtr ffb = pFfb->regs;
	unsigned int fbc;
	BoxPtr pBox;
	int nBox;

	fbc = pFfbPrivWin->fbc_base;
	fbc = (fbc & ~FFB_FBC_WB_MASK) | FFB_FBC_WB_AB;
	fbc = (fbc & ~FFB_FBC_XE_MASK) | FFB_FBC_XE_ON;
	fbc = (fbc & ~FFB_FBC_RGBE_MASK) | FFB_FBC_RGBE_OFF;

	pBox = REGION_RECTS(prgn);
	nBox = (int) REGION_NUM_RECTS(prgn);
	FFB_WRITE_ROP(pFfb, ffb, (FFB_ROP_NEW | (FFB_ROP_NEW << 8)));
	FFB_WRITE_PPC(pFfb, ffb,
		      (FFB_PPC_APE_DISABLE | FFB_PPC_CS_CONST | FFB_PPC_XS_WID),
		      (FFB_PPC_APE_MASK | FFB_PPC_CS_MASK | FFB_PPC_XS_MASK));
	FFB_WRITE_PMASK(pFfb, ffb, ~0);
	FFB_WRITE_DRAWOP(pFfb, ffb, FFB_DRAWOP_RECTANGLE);
	FFB_WRITE_FBC(pFfb, ffb, fbc);
	FFB_WRITE_WID(pFfb, ffb, FFB_WID_WIN(pWin));

	while(nBox--) {
		register int x, y, w, h;

		x = pBox->x1;
		y = pBox->y1;
		w = (pBox->x2 - x);
		h = (pBox->y2 - y);
		FFBFifo(pFfb, 4);
		FFB_WRITE64(&ffb->by, y, x);
		FFB_WRITE64_2(&ffb->bh, h, w);
		pBox++;
	}
	pFfb->rp_active = 1;
	FFBSync(pFfb, ffb);
}

static void
FFBDRIMoveBuffers(WindowPtr pParent, DDXPointRec ptOldOrg,
		  RegionPtr prgnSrc, CARD32 index)
{
}

static void
FFBDRISetDrawableIndex(WindowPtr pWin, CARD32 index)
{
	ScreenPtr pScreen = pWin->drawable.pScreen;
	FFBPtr pFfb = GET_FFB_FROM_SCREEN(pScreen);
	CreatorPrivWinPtr pFfbPrivWin = CreatorGetWindowPrivate(pWin);
	unsigned int wid;

	if (FFBWidIsShared(pFfb, pFfbPrivWin->wid)) {
		wid = FFBWidUnshare(pFfb, pFfbPrivWin->wid);
		if (wid == (unsigned int) -1)
			return;

		ErrorF("FFB: Allocated WID %x for DRI window.\n", wid);
		pFfbPrivWin->wid = wid;

		/* Now update the SAREA. */
		pFfb->pFfbSarea->wid_table[index] = wid;
	}
}