fbdev.c 37.8 KB
Newer Older
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
/*
 * Copyright (C) 1994-2003 The XFree86 Project, Inc.  All Rights Reserved.
 *
 * 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 fur-
 * nished 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, FIT-
 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
 * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the XFree86 Project shall not
 * be used in advertising or otherwise to promote the sale, use or other deal-
 * ings in this Software without prior written authorization from the XFree86
 * Project.
 */

Kaleb Keithley's avatar
Kaleb Keithley committed
27
28
/*
 * Authors:  Alan Hourihane, <alanh@fairlite.demon.co.uk>
Michel Dänzer's avatar
Michel Dänzer committed
29
 *	     Michel Dänzer, <michel@tungstengraphics.com>
Kaleb Keithley's avatar
Kaleb Keithley committed
30
31
 */

32
33
34
35
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

36
37
#include <string.h>

Kaleb Keithley's avatar
Kaleb Keithley committed
38
39
40
41
42
43
44
45
46
47
48
/* all driver need this */
#include "xf86.h"
#include "xf86_OSproc.h"

#include "mipointer.h"
#include "micmap.h"
#include "colormapst.h"
#include "xf86cmap.h"
#include "shadow.h"
#include "dgaproc.h"

49
50
#include "cpu_backend.h"

51
#include "sunxi_disp.h"
52
#include "sunxi_disp_hwcursor.h"
53
#include "sunxi_x_g2d.h"
54
#include "backing_store_tuner.h"
55
#include "sunxi_video.h"
56
57
58
59

#ifdef HAVE_LIBUMP
#include "sunxi_mali_ump_dri2.h"
#endif
60

Kaleb Keithley's avatar
Kaleb Keithley committed
61
62
63
/* for visuals */
#include "fb.h"

64
#if GET_ABI_MAJOR(ABI_VIDEODRV_VERSION) < 6
Kaleb Keithley's avatar
Kaleb Keithley committed
65
66
#include "xf86Resources.h"
#include "xf86RAC.h"
67
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
68
69
70
71
72

#include "fbdevhw.h"

#include "xf86xv.h"

Dave Airlie's avatar
Dave Airlie committed
73
74
#include "compat-api.h"

75
#ifdef XSERVER_LIBPCIACCESS
76
77
78
#include <pciaccess.h>
#endif

79
static Bool debug = 0;
Kaleb Keithley's avatar
Kaleb Keithley committed
80

81
#define TRACE_ENTER(str) \
82
    do { if (debug) ErrorF("sunxifb: " str " %d\n",pScrn->scrnIndex); } while (0)
83
#define TRACE_EXIT(str) \
84
    do { if (debug) ErrorF("sunxifb: " str " done\n"); } while (0)
85
#define TRACE(str) \
86
    do { if (debug) ErrorF("sunxifb trace: " str "\n"); } while (0)
Kaleb Keithley's avatar
Kaleb Keithley committed
87
88
89
90
91
92
93

/* -------------------------------------------------------------------- */
/* prototypes                                                           */

static const OptionInfoRec * FBDevAvailableOptions(int chipid, int busid);
static void	FBDevIdentify(int flags);
static Bool	FBDevProbe(DriverPtr drv, int flags);
94
#ifdef XSERVER_LIBPCIACCESS
95
96
97
static Bool	FBDevPciProbe(DriverPtr drv, int entity_num,
     struct pci_device *dev, intptr_t match_data);
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
98
static Bool	FBDevPreInit(ScrnInfoPtr pScrn, int flags);
Dave Airlie's avatar
Dave Airlie committed
99
100
static Bool	FBDevScreenInit(SCREEN_INIT_ARGS_DECL);
static Bool	FBDevCloseScreen(CLOSE_SCREEN_ARGS_DECL);
Kaleb Keithley's avatar
Kaleb Keithley committed
101
102
static void *	FBDevWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
				  CARD32 *size, void *closure);
Dave Airlie's avatar
Dave Airlie committed
103
static void	FBDevPointerMoved(SCRN_ARG_TYPE arg, int x, int y);
Kaleb Keithley's avatar
Kaleb Keithley committed
104
static Bool	FBDevDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen);
105
106
static Bool	FBDevDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op,
				pointer ptr);
Kaleb Keithley's avatar
Kaleb Keithley committed
107
108
109
110
111
112
113
114
115
116
117
118
119


enum { FBDEV_ROTATE_NONE=0, FBDEV_ROTATE_CW=270, FBDEV_ROTATE_UD=180, FBDEV_ROTATE_CCW=90 };


/* -------------------------------------------------------------------- */

/*
 * This is intentionally screen-independent.  It indicates the binding
 * choice made in the first PreInit.
 */
static int pix24bpp = 0;

120
#define FBDEV_VERSION		4000
121
122
#define FBDEV_NAME		"SUNXIFB"
#define FBDEV_DRIVER_NAME	"sunxifb"
Kaleb Keithley's avatar
Kaleb Keithley committed
123

124
#ifdef XSERVER_LIBPCIACCESS
125
126
127
128
129
130
131
132
133
134
static const struct pci_id_match fbdev_device_match[] = {
    {
	PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY, PCI_MATCH_ANY,
	0x00030000, 0x00ffffff, 0
    },

    { 0, 0, 0 },
};
#endif

135
_X_EXPORT DriverRec FBDEV = {
136
	FBDEV_VERSION,
Kaleb Keithley's avatar
Kaleb Keithley committed
137
138
139
140
141
142
143
144
	FBDEV_DRIVER_NAME,
#if 0
	"driver for linux framebuffer devices",
#endif
	FBDevIdentify,
	FBDevProbe,
	FBDevAvailableOptions,
	NULL,
145
	0,
146
147
	FBDevDriverFunc,

148
#ifdef XSERVER_LIBPCIACCESS
149
150
151
    fbdev_device_match,
    FBDevPciProbe
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
152
153
154
155
};

/* Supported "chipsets" */
static SymTabRec FBDevChipsets[] = {
156
    { 0, "sunxifb" },
Kaleb Keithley's avatar
Kaleb Keithley committed
157
158
159
160
161
162
163
    {-1, NULL }
};

/* Supported options */
typedef enum {
	OPTION_SHADOW_FB,
	OPTION_ROTATE,
164
	OPTION_FBDEV,
165
166
	OPTION_DEBUG,
	OPTION_HW_CURSOR,
167
168
169
	OPTION_SW_CURSOR,
	OPTION_DRI2,
	OPTION_DRI2_OVERLAY,
170
	OPTION_SWAPBUFFERS_WAIT,
171
	OPTION_ACCELMETHOD,
172
173
	OPTION_USE_BS,
	OPTION_FORCE_BS,
174
	OPTION_XV_OVERLAY,
Kaleb Keithley's avatar
Kaleb Keithley committed
175
176
177
178
179
180
} FBDevOpts;

static const OptionInfoRec FBDevOptions[] = {
	{ OPTION_SHADOW_FB,	"ShadowFB",	OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_ROTATE,	"Rotate",	OPTV_STRING,	{0},	FALSE },
	{ OPTION_FBDEV,		"fbdev",	OPTV_STRING,	{0},	FALSE },
181
	{ OPTION_DEBUG,		"debug",	OPTV_BOOLEAN,	{0},	FALSE },
182
183
	{ OPTION_HW_CURSOR,	"HWCursor",	OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_SW_CURSOR,	"SWCursor",	OPTV_BOOLEAN,	{0},	FALSE },
184
185
	{ OPTION_DRI2,		"DRI2",		OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_DRI2_OVERLAY,	"DRI2HWOverlay",OPTV_BOOLEAN,	{0},	FALSE },
186
	{ OPTION_SWAPBUFFERS_WAIT,"SwapbuffersWait",OPTV_BOOLEAN,{0},	FALSE },
187
	{ OPTION_ACCELMETHOD,	"AccelMethod",	OPTV_STRING,	{0},	FALSE },
188
189
	{ OPTION_USE_BS,	"UseBackingStore",OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_FORCE_BS,	"ForceBackingStore",OPTV_BOOLEAN,{0},	FALSE },
190
	{ OPTION_XV_OVERLAY,	"XVHWOverlay",	OPTV_BOOLEAN,	{0},	FALSE },
Kaleb Keithley's avatar
Kaleb Keithley committed
191
192
193
194
195
196
197
198
199
200
201
	{ -1,			NULL,		OPTV_NONE,	{0},	FALSE }
};

/* -------------------------------------------------------------------- */

#ifdef XFree86LOADER

MODULESETUPPROTO(FBDevSetup);

static XF86ModuleVersionInfo FBDevVersRec =
{
202
	"sunxifb",
Kaleb Keithley's avatar
Kaleb Keithley committed
203
204
205
	MODULEVENDORSTRING,
	MODINFOSTRING1,
	MODINFOSTRING2,
206
	XORG_VERSION_CURRENT,
207
	PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
Kaleb Keithley's avatar
Kaleb Keithley committed
208
209
	ABI_CLASS_VIDEODRV,
	ABI_VIDEODRV_VERSION,
210
	MOD_CLASS_VIDEODRV,
Kaleb Keithley's avatar
Kaleb Keithley committed
211
212
213
	{0,0,0,0}
};

214
_X_EXPORT XF86ModuleData sunxifbModuleData = { &FBDevVersRec, FBDevSetup, NULL };
Kaleb Keithley's avatar
Kaleb Keithley committed
215
216
217
218
219
220
221
222

pointer
FBDevSetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
	static Bool setupDone = FALSE;

	if (!setupDone) {
		setupDone = TRUE;
223
		xf86AddDriver(&FBDEV, module, HaveDriverFuncs);
Kaleb Keithley's avatar
Kaleb Keithley committed
224
225
226
227
228
229
230
231
232
233
234
235
		return (pointer)1;
	} else {
		if (errmaj) *errmaj = LDR_ONCEONLY;
		return NULL;
	}
}

#endif /* XFree86LOADER */

/* -------------------------------------------------------------------- */
/* our private data, and two functions to allocate/free this            */

236
#include "fbdev_priv.h"
Kaleb Keithley's avatar
Kaleb Keithley committed
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252

static Bool
FBDevGetRec(ScrnInfoPtr pScrn)
{
	if (pScrn->driverPrivate != NULL)
		return TRUE;
	
	pScrn->driverPrivate = xnfcalloc(sizeof(FBDevRec), 1);
	return TRUE;
}

static void
FBDevFreeRec(ScrnInfoPtr pScrn)
{
	if (pScrn->driverPrivate == NULL)
		return;
253
	free(pScrn->driverPrivate);
Kaleb Keithley's avatar
Kaleb Keithley committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
	pScrn->driverPrivate = NULL;
}

/* -------------------------------------------------------------------- */

static const OptionInfoRec *
FBDevAvailableOptions(int chipid, int busid)
{
	return FBDevOptions;
}

static void
FBDevIdentify(int flags)
{
	xf86PrintChipsets(FBDEV_NAME, "driver for framebuffer", FBDevChipsets);
}

271

272
#ifdef XSERVER_LIBPCIACCESS
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
static Bool FBDevPciProbe(DriverPtr drv, int entity_num,
			  struct pci_device *dev, intptr_t match_data)
{
    ScrnInfoPtr pScrn = NULL;

    if (!xf86LoadDrvSubModule(drv, "fbdevhw"))
	return FALSE;
	    
    pScrn = xf86ConfigPciEntity(NULL, 0, entity_num, NULL, NULL,
				NULL, NULL, NULL, NULL);
    if (pScrn) {
	char *device;
	GDevPtr devSection = xf86GetDevFromEntity(pScrn->entityList[0],
						  pScrn->entityInstanceList[0]);

	device = xf86FindOptionValue(devSection->options, "fbdev");
	if (fbdevHWProbe(NULL, device, NULL)) {
	    pScrn->driverVersion = FBDEV_VERSION;
	    pScrn->driverName    = FBDEV_DRIVER_NAME;
	    pScrn->name          = FBDEV_NAME;
	    pScrn->Probe         = FBDevProbe;
	    pScrn->PreInit       = FBDevPreInit;
	    pScrn->ScreenInit    = FBDevScreenInit;
	    pScrn->SwitchMode    = fbdevHWSwitchModeWeak();
	    pScrn->AdjustFrame   = fbdevHWAdjustFrameWeak();
	    pScrn->EnterVT       = fbdevHWEnterVTWeak();
	    pScrn->LeaveVT       = fbdevHWLeaveVTWeak();
	    pScrn->ValidMode     = fbdevHWValidModeWeak();

	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
		       "claimed PCI slot %d@%d:%d:%d\n", 
		       dev->bus, dev->domain, dev->dev, dev->func);
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		       "using %s\n", device ? device : "default device");
	}
	else {
	    pScrn = NULL;
	}
    }

    return (pScrn != NULL);
}
#endif


Kaleb Keithley's avatar
Kaleb Keithley committed
318
319
320
321
322
323
324
static Bool
FBDevProbe(DriverPtr drv, int flags)
{
	int i;
	ScrnInfoPtr pScrn;
       	GDevPtr *devSections;
	int numDevSections;
325
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
326
	int bus,device,func;
327
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
	char *dev;
	Bool foundScreen = FALSE;

	TRACE("probe start");

	/* For now, just bail out for PROBE_DETECT. */
	if (flags & PROBE_DETECT)
		return FALSE;

	if ((numDevSections = xf86MatchDevice(FBDEV_DRIVER_NAME, &devSections)) <= 0) 
	    return FALSE;
	
	if (!xf86LoadDrvSubModule(drv, "fbdevhw"))
	    return FALSE;
	    
	for (i = 0; i < numDevSections; i++) {
	    Bool isIsa = FALSE;
	    Bool isPci = FALSE;

	    dev = xf86FindOptionValue(devSections[i]->options,"fbdev");
	    if (devSections[i]->busID) {
349
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
350
351
352
353
354
	        if (xf86ParsePciBusString(devSections[i]->busID,&bus,&device,
					  &func)) {
		    if (!xf86CheckPciSlot(bus,device,func))
		        continue;
		    isPci = TRUE;
355
356
		} else
#endif
Adam Jackson's avatar
Adam Jackson committed
357
#ifdef HAVE_ISA
358
		if (xf86ParseIsaBusString(devSections[i]->busID))
Kaleb Keithley's avatar
Kaleb Keithley committed
359
		    isIsa = TRUE;
Adam Jackson's avatar
Adam Jackson committed
360
361
362
		else
#endif
		    0;
Kaleb Keithley's avatar
Kaleb Keithley committed
363
364
365
366
367
		  
	    }
	    if (fbdevHWProbe(NULL,dev,NULL)) {
		pScrn = NULL;
		if (isPci) {
Adam Jackson's avatar
Adam Jackson committed
368
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
		    /* XXX what about when there's no busID set? */
		    int entity;
		    
		    entity = xf86ClaimPciSlot(bus,device,func,drv,
					      0,devSections[i],
					      TRUE);
		    pScrn = xf86ConfigPciEntity(pScrn,0,entity,
						      NULL,RES_SHARED_VGA,
						      NULL,NULL,NULL,NULL);
		    /* xf86DrvMsg() can't be called without setting these */
		    pScrn->driverName    = FBDEV_DRIVER_NAME;
		    pScrn->name          = FBDEV_NAME;
		    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
			       "claimed PCI slot %d:%d:%d\n",bus,device,func);

384
#endif
Adam Jackson's avatar
Adam Jackson committed
385
386
		} else if (isIsa) {
#ifdef HAVE_ISA
Kaleb Keithley's avatar
Kaleb Keithley committed
387
388
389
390
391
392
393
		    int entity;
		    
		    entity = xf86ClaimIsaSlot(drv, 0,
					      devSections[i], TRUE);
		    pScrn = xf86ConfigIsaEntity(pScrn,0,entity,
						      NULL,RES_SHARED_VGA,
						      NULL,NULL,NULL,NULL);
Adam Jackson's avatar
Adam Jackson committed
394
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
395
396
397
398
399
400
401
402
403
404
405
406
		} else {
		   int entity;

		    entity = xf86ClaimFbSlot(drv, 0,
					      devSections[i], TRUE);
		    pScrn = xf86ConfigFbEntity(pScrn,0,entity,
					       NULL,NULL,NULL,NULL);
		   
		}
		if (pScrn) {
		    foundScreen = TRUE;
		    
407
		    pScrn->driverVersion = FBDEV_VERSION;
Kaleb Keithley's avatar
Kaleb Keithley committed
408
409
410
411
412
		    pScrn->driverName    = FBDEV_DRIVER_NAME;
		    pScrn->name          = FBDEV_NAME;
		    pScrn->Probe         = FBDevProbe;
		    pScrn->PreInit       = FBDevPreInit;
		    pScrn->ScreenInit    = FBDevScreenInit;
413
414
415
416
417
		    pScrn->SwitchMode    = fbdevHWSwitchModeWeak();
		    pScrn->AdjustFrame   = fbdevHWAdjustFrameWeak();
		    pScrn->EnterVT       = fbdevHWEnterVTWeak();
		    pScrn->LeaveVT       = fbdevHWLeaveVTWeak();
		    pScrn->ValidMode     = fbdevHWValidModeWeak();
Kaleb Keithley's avatar
Kaleb Keithley committed
418
419
420
421
422
423
		    
		    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
			       "using %s\n", dev ? dev : "default device");
		}
	    }
	}
424
	free(devSections);
Kaleb Keithley's avatar
Kaleb Keithley committed
425
426
427
428
429
430
431
432
433
	TRACE("probe done");
	return foundScreen;
}

static Bool
FBDevPreInit(ScrnInfoPtr pScrn, int flags)
{
	FBDevPtr fPtr;
	int default_depth, fbbpp;
Adam Jackson's avatar
Adam Jackson committed
434
	const char *s;
435
	int type;
436
	cpuinfo_t *cpuinfo;
Kaleb Keithley's avatar
Kaleb Keithley committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452

	if (flags & PROBE_DETECT) return FALSE;

	TRACE_ENTER("PreInit");

	/* Check the number of entities, and fail if it isn't one. */
	if (pScrn->numEntities != 1)
		return FALSE;

	pScrn->monitor = pScrn->confScreen->monitor;

	FBDevGetRec(pScrn);
	fPtr = FBDEVPTR(pScrn);

	fPtr->pEnt = xf86GetEntityInfo(pScrn->entityList[0]);

453
#ifndef XSERVER_LIBPCIACCESS
Kaleb Keithley's avatar
Kaleb Keithley committed
454
455
456
457
458
459
460
461
462
463
	pScrn->racMemFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;
	/* XXX Is this right?  Can probably remove RAC_FB */
	pScrn->racIoFlags = RAC_FB | RAC_COLORMAP | RAC_CURSOR | RAC_VIEWPORT;

	if (fPtr->pEnt->location.type == BUS_PCI &&
	    xf86RegisterResources(fPtr->pEnt->index,NULL,ResExclusive)) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "xf86RegisterResources() found resource conflicts\n");
		return FALSE;
	}
464
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
465
466
467
468
	/* open device */
	if (!fbdevHWInit(pScrn,NULL,xf86FindOptionValue(fPtr->pEnt->device->options,"fbdev")))
		return FALSE;
	default_depth = fbdevHWGetDepth(pScrn,&fbbpp);
469
	if (!xf86SetDepthBpp(pScrn, default_depth, default_depth, fbbpp,
470
			     Support24bppFb | Support32bppFb | SupportConvert32to24 | SupportConvert24to32))
Kaleb Keithley's avatar
Kaleb Keithley committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
		return FALSE;
	xf86PrintDepthBpp(pScrn);

	/* Get the depth24 pixmap format */
	if (pScrn->depth == 24 && pix24bpp == 0)
		pix24bpp = xf86GetBppFromDepth(pScrn, 24);

	/* color weight */
	if (pScrn->depth > 8) {
		rgb zeros = { 0, 0, 0 };
		if (!xf86SetWeight(pScrn, zeros, zeros))
			return FALSE;
	}

	/* visual init */
	if (!xf86SetDefaultVisual(pScrn, -1))
		return FALSE;

	/* We don't currently support DirectColor at > 8bpp */
	if (pScrn->depth > 8 && pScrn->defaultVisual != TrueColor) {
491
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "requested default visual"
Kaleb Keithley's avatar
Kaleb Keithley committed
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
			   " (%s) is not supported at depth %d\n",
			   xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
		return FALSE;
	}

	{
		Gamma zeros = {0.0, 0.0, 0.0};

		if (!xf86SetGamma(pScrn,zeros)) {
			return FALSE;
		}
	}

	pScrn->progClock = TRUE;
	pScrn->rgbBits   = 8;
507
	pScrn->chipset   = "sunxifb";
Kaleb Keithley's avatar
Kaleb Keithley committed
508
509
	pScrn->videoRam  = fbdevHWGetVidmem(pScrn);

510
511
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "hardware: %s (video memory:"
		   " %dkB)\n", fbdevHWGetName(pScrn), pScrn->videoRam/1024);
Kaleb Keithley's avatar
Kaleb Keithley committed
512
513
514

	/* handle options */
	xf86CollectOptions(pScrn, NULL);
515
	if (!(fPtr->Options = malloc(sizeof(FBDevOptions))))
Kaleb Keithley's avatar
Kaleb Keithley committed
516
517
518
519
		return FALSE;
	memcpy(fPtr->Options, FBDevOptions, sizeof(FBDevOptions));
	xf86ProcessOptions(pScrn->scrnIndex, fPtr->pEnt->device->options, fPtr->Options);

520
521
522
523
	/* check the processor type */
	cpuinfo = cpuinfo_init();
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "processor: %s\n",
	           cpuinfo->processor_name);
524
525
	/* don't use shadow by default if we have VFP/NEON or HW acceleration */
	fPtr->shadowFB = !cpuinfo->has_arm_vfp &&
526
527
528
529
	                 !xf86GetOptValString(fPtr->Options, OPTION_ACCELMETHOD);
	cpuinfo_close(cpuinfo);

	/* but still honour the settings from xorg.conf */
530
	fPtr->shadowFB = xf86ReturnOptValBool(fPtr->Options, OPTION_SHADOW_FB,
531
					      fPtr->shadowFB);
Kaleb Keithley's avatar
Kaleb Keithley committed
532

533
534
	debug = xf86ReturnOptValBool(fPtr->Options, OPTION_DEBUG, FALSE);

Kaleb Keithley's avatar
Kaleb Keithley committed
535
536
537
538
539
540
541
542
543
	/* rotation */
	fPtr->rotate = FBDEV_ROTATE_NONE;
	if ((s = xf86GetOptValString(fPtr->Options, OPTION_ROTATE)))
	{
	  if(!xf86NameCmp(s, "CW"))
	  {
	    fPtr->shadowFB = TRUE;
	    fPtr->rotate = FBDEV_ROTATE_CW;
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
544
		       "rotating screen clockwise\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
545
546
547
548
549
550
	  }
	  else if(!xf86NameCmp(s, "CCW"))
	  {
	    fPtr->shadowFB = TRUE;
	    fPtr->rotate = FBDEV_ROTATE_CCW;
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
551
		       "rotating screen counter-clockwise\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
552
553
554
555
556
557
	  }
	  else if(!xf86NameCmp(s, "UD"))
	  {
	    fPtr->shadowFB = TRUE;
	    fPtr->rotate = FBDEV_ROTATE_UD;
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
558
		       "rotating screen upside-down\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
559
560
561
562
563
564
	  }
	  else
	  {
	    xf86DrvMsg(pScrn->scrnIndex, X_CONFIG,
		       "\"%s\" is not a valid value for Option \"Rotate\"\n", s);
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
565
		       "valid options are \"CW\", \"CCW\" and \"UD\"\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
566
567
568
569
570
	  }
	}

	/* select video modes */

571
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "checking modes against framebuffer device...\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
572
573
	fbdevHWSetVideoModes(pScrn);

574
	xf86DrvMsg(pScrn->scrnIndex, X_INFO, "checking modes against monitor...\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
	{
		DisplayModePtr mode, first = mode = pScrn->modes;
		
		if (mode != NULL) do {
			mode->status = xf86CheckModeForMonitor(mode, pScrn->monitor);
			mode = mode->next;
		} while (mode != NULL && mode != first);

		xf86PruneDriverModes(pScrn);
	}

	if (NULL == pScrn->modes)
		fbdevHWUseBuildinMode(pScrn);
	pScrn->currentMode = pScrn->modes;

590
591
	/* First approximation, may be refined in ScreenInit */
	pScrn->displayWidth = pScrn->virtualX;
Kaleb Keithley's avatar
Kaleb Keithley committed
592
593
594
595
596
597
598

	xf86PrintModes(pScrn);

	/* Set display resolution */
	xf86SetDpi(pScrn, 0, 0);

	/* Load bpp-specific modules */
599
	switch ((type = fbdevHWGetType(pScrn)))
Kaleb Keithley's avatar
Kaleb Keithley committed
600
601
602
603
604
605
606
607
608
609
610
	{
	case FBDEVHW_PACKED_PIXELS:
		switch (pScrn->bitsPerPixel)
		{
		case 8:
		case 16:
		case 24:
		case 32:
			break;
		default:
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
611
612
			"unsupported number of bits per pixel: %d",
			pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
613
614
615
616
617
618
			return FALSE;
		}
		break;
	case FBDEVHW_INTERLEAVED_PLANES:
               /* Not supported yet, don't know what to do with this */
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
619
620
                          "interleaved planes are not yet supported by the "
			  "fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
621
622
623
624
625
		return FALSE;
	case FBDEVHW_TEXT:
               /* This should never happen ...
                * we should check for this much much earlier ... */
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
626
                          "text mode is not supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
627
628
629
630
		return FALSE;
       case FBDEVHW_VGA_PLANES:
               /* Not supported yet */
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
631
632
                          "EGA/VGA planes are not yet supported by the fbdev "
			  "driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
633
634
635
               return FALSE;
       default:
               xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
636
                          "unrecognised fbdev hardware type (%d)\n", type);
Kaleb Keithley's avatar
Kaleb Keithley committed
637
638
               return FALSE;
	}
Adam Jackson's avatar
Adam Jackson committed
639
	if (xf86LoadSubModule(pScrn, "fb") == NULL) {
Kaleb Keithley's avatar
Kaleb Keithley committed
640
641
642
643
644
645
		FBDevFreeRec(pScrn);
		return FALSE;
	}

	/* Load shadow if needed */
	if (fPtr->shadowFB) {
646
647
		xf86DrvMsg(pScrn->scrnIndex, X_CONFIG, "using shadow"
			   " framebuffer\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
648
649
650
651
652
653
654
655
656
657
		if (!xf86LoadSubModule(pScrn, "shadow")) {
			FBDevFreeRec(pScrn);
			return FALSE;
		}
	}

	TRACE_EXIT("PreInit");
	return TRUE;
}

658

659
static Bool
660
FBDevCreateScreenResources(ScreenPtr pScreen)
661
662
{
    PixmapPtr pPixmap;
663
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
664
665
666
667
668
669
670
671
    FBDevPtr fPtr = FBDEVPTR(pScrn);
    Bool ret;

    pScreen->CreateScreenResources = fPtr->CreateScreenResources;
    ret = pScreen->CreateScreenResources(pScreen);
    pScreen->CreateScreenResources = FBDevCreateScreenResources;

    if (!ret)
672
	return FALSE;
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687

    pPixmap = pScreen->GetScreenPixmap(pScreen);

    if (!shadowAdd(pScreen, pPixmap, fPtr->rotate ?
		   shadowUpdateRotatePackedWeak() : shadowUpdatePackedWeak(),
		   FBDevWindowLinear, fPtr->rotate, NULL)) {
	return FALSE;
    }

    return TRUE;
}

static Bool
FBDevShadowInit(ScreenPtr pScreen)
{
688
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
689
    FBDevPtr fPtr = FBDEVPTR(pScrn);
690
691
692
693
694
    
    if (!shadowSetup(pScreen)) {
	return FALSE;
    }

695
696
    fPtr->CreateScreenResources = pScreen->CreateScreenResources;
    pScreen->CreateScreenResources = FBDevCreateScreenResources;
697

698
    return TRUE;
699
700
701
}


Kaleb Keithley's avatar
Kaleb Keithley committed
702
static Bool
Dave Airlie's avatar
Dave Airlie committed
703
FBDevScreenInit(SCREEN_INIT_ARGS_DECL)
Kaleb Keithley's avatar
Kaleb Keithley committed
704
{
705
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
706
707
708
	FBDevPtr fPtr = FBDEVPTR(pScrn);
	VisualPtr visual;
	int init_picture = 0;
709
	int ret, flags;
710
	int type;
711
	char *accelmethod;
712
	cpu_backend_t *cpu_backend;
713
	Bool useBackingStore = FALSE, forceBackingStore = FALSE;
Kaleb Keithley's avatar
Kaleb Keithley committed
714
715
716
717
718
719
720
721
722
723
724
725
726
727

	TRACE_ENTER("FBDevScreenInit");

#if DEBUG
	ErrorF("\tbitsPerPixel=%d, depth=%d, defaultVisual=%s\n"
	       "\tmask: %x,%x,%x, offset: %d,%d,%d\n",
	       pScrn->bitsPerPixel,
	       pScrn->depth,
	       xf86GetVisualName(pScrn->defaultVisual),
	       pScrn->mask.red,pScrn->mask.green,pScrn->mask.blue,
	       pScrn->offset.red,pScrn->offset.green,pScrn->offset.blue);
#endif

	if (NULL == (fPtr->fbmem = fbdevHWMapVidmem(pScrn))) {
Dave Airlie's avatar
Dave Airlie committed
728
	        xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"mapping of video memory"
729
			   " failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
730
731
732
733
734
735
736
		return FALSE;
	}
	fPtr->fboff = fbdevHWLinearOffset(pScrn);

	fbdevHWSave(pScrn);

	if (!fbdevHWModeInit(pScrn, pScrn->currentMode)) {
Dave Airlie's avatar
Dave Airlie committed
737
		xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"mode initialization failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
738
739
740
		return FALSE;
	}
	fbdevHWSaveScreen(pScreen, SCREEN_SAVER_ON);
Dave Airlie's avatar
Dave Airlie committed
741
	fbdevHWAdjustFrame(ADJUST_FRAME_ARGS(pScrn, 0, 0));
Kaleb Keithley's avatar
Kaleb Keithley committed
742
743
744
745
746

	/* mi layer */
	miClearVisualTypes();
	if (pScrn->bitsPerPixel > 8) {
		if (!miSetVisualTypes(pScrn->depth, TrueColorMask, pScrn->rgbBits, TrueColor)) {
Dave Airlie's avatar
Dave Airlie committed
747
			xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"visual type setup failed"
748
749
				   " for %d bits per pixel [1]\n",
				   pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
750
751
752
753
754
755
			return FALSE;
		}
	} else {
		if (!miSetVisualTypes(pScrn->depth,
				      miGetDefaultVisualMask(pScrn->depth),
				      pScrn->rgbBits, pScrn->defaultVisual)) {
Dave Airlie's avatar
Dave Airlie committed
756
			xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"visual type setup failed"
757
758
				   " for %d bits per pixel [2]\n",
				   pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
759
760
761
762
			return FALSE;
		}
	}
	if (!miSetPixmapDepths()) {
Dave Airlie's avatar
Dave Airlie committed
763
	  xf86DrvMsg(pScrn->scrnIndex,X_ERROR,"pixmap depth setup failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
764
765
766
767
768
	  return FALSE;
	}

	if(fPtr->rotate==FBDEV_ROTATE_CW || fPtr->rotate==FBDEV_ROTATE_CCW)
	{
769
770
771
	  int tmp = pScrn->virtualX;
	  pScrn->virtualX = pScrn->displayWidth = pScrn->virtualY;
	  pScrn->virtualY = tmp;
772
773
774
775
776
777
778
	} else if (!fPtr->shadowFB) {
		/* FIXME: this doesn't work for all cases, e.g. when each scanline
			has a padding which is independent from the depth (controlfb) */
		pScrn->displayWidth = fbdevHWGetLineLength(pScrn) /
				      (pScrn->bitsPerPixel / 8);

		if (pScrn->displayWidth != pScrn->virtualX) {
Dave Airlie's avatar
Dave Airlie committed
779
			xf86DrvMsg(pScrn->scrnIndex, X_INFO,
780
781
782
				   "Pitch updated to %d after ModeInit\n",
				   pScrn->displayWidth);
		}
Kaleb Keithley's avatar
Kaleb Keithley committed
783
784
785
786
787
788
789
	}

	if(fPtr->rotate && !fPtr->PointerMoved) {
		fPtr->PointerMoved = pScrn->PointerMoved;
		pScrn->PointerMoved = FBDevPointerMoved;
	}

790
	fPtr->fbstart = fPtr->fbmem + fPtr->fboff;
Kaleb Keithley's avatar
Kaleb Keithley committed
791

792
	if (fPtr->shadowFB) {
793
794
	    fPtr->shadow = calloc(1, pScrn->virtualX * pScrn->virtualY *
				  pScrn->bitsPerPixel);
795
796
797
798
799
800
801
802

	    if (!fPtr->shadow) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "Failed to allocate shadow framebuffer\n");
		return FALSE;
	    }
	}

803
	switch ((type = fbdevHWGetType(pScrn)))
Kaleb Keithley's avatar
Kaleb Keithley committed
804
805
806
807
808
809
810
	{
	case FBDEVHW_PACKED_PIXELS:
		switch (pScrn->bitsPerPixel) {
		case 8:
		case 16:
		case 24:
		case 32:
811
812
813
814
815
			ret = fbScreenInit(pScreen, fPtr->shadowFB ? fPtr->shadow
					   : fPtr->fbstart, pScrn->virtualX,
					   pScrn->virtualY, pScrn->xDpi,
					   pScrn->yDpi, pScrn->displayWidth,
					   pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
816
817
818
			init_picture = 1;
			break;
	 	default:
Dave Airlie's avatar
Dave Airlie committed
819
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
820
821
822
				   "internal error: invalid number of bits per"
				   " pixel (%d) encountered in"
				   " FBDevScreenInit()\n", pScrn->bitsPerPixel);
Kaleb Keithley's avatar
Kaleb Keithley committed
823
824
825
826
827
828
829
			ret = FALSE;
			break;
		}
		break;
	case FBDEVHW_INTERLEAVED_PLANES:
		/* This should never happen ...
		* we should check for this much much earlier ... */
Dave Airlie's avatar
Dave Airlie committed
830
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
831
832
		           "internal error: interleaved planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
833
834
835
836
837
		ret = FALSE;
		break;
	case FBDEVHW_TEXT:
		/* This should never happen ...
		* we should check for this much much earlier ... */
Dave Airlie's avatar
Dave Airlie committed
838
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
839
840
		           "internal error: text mode is not supported by the "
			   "fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
841
842
843
844
		ret = FALSE;
		break;
	case FBDEVHW_VGA_PLANES:
		/* Not supported yet */
Dave Airlie's avatar
Dave Airlie committed
845
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
846
847
		           "internal error: EGA/VGA Planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
848
849
850
		ret = FALSE;
		break;
	default:
Dave Airlie's avatar
Dave Airlie committed
851
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
852
		           "internal error: unrecognised hardware type (%d) "
853
			   "encountered in FBDevScreenInit()\n", type);
Kaleb Keithley's avatar
Kaleb Keithley committed
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
		ret = FALSE;
		break;
	}
	if (!ret)
		return FALSE;

	if (pScrn->bitsPerPixel > 8) {
		/* Fixup RGB ordering */
		visual = pScreen->visuals + pScreen->numVisuals;
		while (--visual >= pScreen->visuals) {
			if ((visual->class | DynamicClass) == DirectColor) {
				visual->offsetRed   = pScrn->offset.red;
				visual->offsetGreen = pScrn->offset.green;
				visual->offsetBlue  = pScrn->offset.blue;
				visual->redMask     = pScrn->mask.red;
				visual->greenMask   = pScrn->mask.green;
				visual->blueMask    = pScrn->mask.blue;
			}
		}
	}

	/* must be after RGB ordering fixed */
	if (init_picture && !fbPictureInit(pScreen, NULL, 0))
		xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
878
			   "Render extension initialisation failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
879

880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
	/*
	 * by default make use of backing store (the driver decides for which
	 * windows it is beneficial) if shadow is not enabled.
	 */
	useBackingStore = xf86ReturnOptValBool(fPtr->Options, OPTION_USE_BS,
	                                       !fPtr->shadowFB);
#ifndef __arm__
	/*
	 * right now we can only make "smart" decisions on ARM hardware,
	 * everything else (for example x86) would take a performance hit
	 * unless backing store is just used for all windows.
	 */
	forceBackingStore = useBackingStore;
#endif
	/* but still honour the settings from xorg.conf */
	forceBackingStore = xf86ReturnOptValBool(fPtr->Options, OPTION_FORCE_BS,
	                                         forceBackingStore);

	if (useBackingStore || forceBackingStore) {
		fPtr->backing_store_tuner_private =
			BackingStoreTuner_Init(pScreen, forceBackingStore);
	}

903
904
905
906
	/* initialize the 'CPU' backend */
	cpu_backend = cpu_backend_init(fPtr->fbmem, pScrn->videoRam);
	fPtr->cpu_backend_private = cpu_backend;

907
908
909
910
911
	/* try to load G2D kernel module before initializing sunxi-disp */
	if (!xf86LoadKernelModule("g2d_23"))
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "can't load 'g2d_23' kernel module\n");

912
913
914
915
916
917
918
	fPtr->sunxi_disp_private = sunxi_disp_init(xf86FindOptionValue(
	                                fPtr->pEnt->device->options,"fbdev"),
	                                fPtr->fbmem);
	if (!fPtr->sunxi_disp_private)
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "failed to enable the use of sunxi display controller\n");

919
	if (!(accelmethod = xf86GetOptValString(fPtr->Options, OPTION_ACCELMETHOD)) ||
920
						strcasecmp(accelmethod, "g2d") == 0) {
921
922
923
		sunxi_disp_t *disp = fPtr->sunxi_disp_private;
		if (disp && disp->fd_g2d >= 0 &&
		    (fPtr->SunxiG2D_private = SunxiG2D_Init(pScreen, &disp->blt2d))) {
924
			disp->fallback_blt2d = &cpu_backend->blt2d;
925
			xf86DrvMsg(pScrn->scrnIndex, X_INFO, "enabled G2D acceleration\n");
926
927
		}
		else {
928
929
			xf86DrvMsg(pScreen->myNum, X_INFO,
				"No sunxi-g2d hardware detected (check /dev/disp and /dev/g2d)\n");
930
			xf86DrvMsg(pScrn->scrnIndex, X_INFO,
931
				"G2D hardware acceleration can't be enabled\n");
932
933
934
935
		}
	}
	else {
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
936
			"G2D acceleration is disabled via AccelMethod option\n");
937
938
	}

939
	if (!fPtr->SunxiG2D_private && cpu_backend->cpuinfo->has_arm_vfp) {
940
		if ((fPtr->SunxiG2D_private = SunxiG2D_Init(pScreen, &cpu_backend->blt2d))) {
941
			xf86DrvMsg(pScrn->scrnIndex, X_INFO, "enabled VFP/NEON optimizations\n");
942
943
944
		}
	}

945
	if (fPtr->shadowFB && !FBDevShadowInit(pScreen)) {
Dave Airlie's avatar
Dave Airlie committed
946
	    xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
947
		       "shadow framebuffer initialization failed\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
948
949
950
951
952
953
	    return FALSE;
	}

	if (!fPtr->rotate)
	  FBDevDGAInit(pScrn, pScreen);
	else {
Dave Airlie's avatar
Dave Airlie committed
954
955
	  xf86DrvMsg(pScrn->scrnIndex, X_INFO, "display rotated; disabling DGA\n");
	  xf86DrvMsg(pScrn->scrnIndex, X_INFO, "using driver rotation; disabling "
956
			                "XRandR\n");
957
	  xf86DisableRandR();
Kaleb Keithley's avatar
Kaleb Keithley committed
958
	  if (pScrn->bitsPerPixel == 24)
Dave Airlie's avatar
Dave Airlie committed
959
	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING, "rotation might be broken at 24 "
960
                                             "bits per pixel\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
961
962
963
964
965
966
967
968
969
	}

	xf86SetBlackWhitePixels(pScreen);
	xf86SetBackingStore(pScreen);

	/* software cursor */
	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());

	/* colormap */
970
	switch ((type = fbdevHWGetType(pScrn)))
Kaleb Keithley's avatar
Kaleb Keithley committed
971
972
973
	{
	/* XXX It would be simpler to use miCreateDefColormap() in all cases. */
	case FBDEVHW_PACKED_PIXELS:
974
		if (!miCreateDefColormap(pScreen)) {
Dave Airlie's avatar
Dave Airlie committed
975
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
976
977
                                   "internal error: miCreateDefColormap failed "
				   "in FBDevScreenInit()\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
978
			return FALSE;
979
		}
Kaleb Keithley's avatar
Kaleb Keithley committed
980
981
		break;
	case FBDEVHW_INTERLEAVED_PLANES:
Dave Airlie's avatar
Dave Airlie committed
982
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
983
984
		           "internal error: interleaved planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
985
986
		return FALSE;
	case FBDEVHW_TEXT:
Dave Airlie's avatar
Dave Airlie committed
987
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
988
989
		           "internal error: text mode is not supported by "
			   "the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
990
991
		return FALSE;
	case FBDEVHW_VGA_PLANES:
Dave Airlie's avatar
Dave Airlie committed
992
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
993
994
		           "internal error: EGA/VGA planes are not yet "
			   "supported by the fbdev driver\n");
Kaleb Keithley's avatar
Kaleb Keithley committed
995
996
		return FALSE;
	default:
Dave Airlie's avatar
Dave Airlie committed
997
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
998
999
		           "internal error: unrecognised fbdev hardware type "
			   "(%d) encountered in FBDevScreenInit()\n", type);
Kaleb Keithley's avatar
Kaleb Keithley committed
1000
1001
1002
		return FALSE;
	}
	flags = CMAP_PALETTED_TRUECOLOR;
1003
	if(!xf86HandleColormaps(pScreen, 256, 8, fbdevHWLoadPaletteWeak(), 
1004
				NULL, flags))
Kaleb Keithley's avatar
Kaleb Keithley committed
1005
1006
		return FALSE;

1007
	xf86DPMSInit(pScreen, fbdevHWDPMSSetWeak(), 0);
Kaleb Keithley's avatar
Kaleb Keithley committed
1008

1009
	pScreen->SaveScreen = fbdevHWSaveScreenWeak();
Kaleb Keithley's avatar
Kaleb Keithley committed
1010
1011
1012
1013
1014

	/* Wrap the current CloseScreen function */
	fPtr->CloseScreen = pScreen->CloseScreen;
	pScreen->CloseScreen = FBDevCloseScreen;

1015
#if XV
1016
1017
1018
	fPtr->SunxiVideo_private = NULL;
	if (xf86ReturnOptValBool(fPtr->Options, OPTION_XV_OVERLAY, TRUE) &&
	fPtr->sunxi_disp_private) {
1019
1020
1021
1022
1023
1024
	    fPtr->SunxiVideo_private = SunxiVideo_Init(pScreen);
	    if (fPtr->SunxiVideo_private)
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "using sunxi disp layers for X video extension\n");
	}
	else {
Kaleb Keithley's avatar
Kaleb Keithley committed
1025
1026
1027
1028
1029
1030
1031
	    XF86VideoAdaptorPtr *ptr;

	    int n = xf86XVListGenericAdaptors(pScrn,&ptr);
	    if (n) {
		xf86XVScreenInit(pScreen,ptr,n);
	    }
	}
1032
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
1033

1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
	if (!xf86ReturnOptValBool(fPtr->Options, OPTION_SW_CURSOR, FALSE) &&
	     xf86ReturnOptValBool(fPtr->Options, OPTION_HW_CURSOR, TRUE)) {

	    fPtr->SunxiDispHardwareCursor_private = SunxiDispHardwareCursor_Init(
	                                pScreen);

	    if (fPtr->SunxiDispHardwareCursor_private)
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "using hardware cursor\n");
	    else
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "failed to enable hardware cursor\n");
	}

1048
1049
1050
1051
#ifdef HAVE_LIBUMP
	if (xf86ReturnOptValBool(fPtr->Options, OPTION_DRI2, TRUE)) {

	    fPtr->SunxiMaliDRI2_private = SunxiMaliDRI2_Init(pScreen,
1052
1053
		xf86ReturnOptValBool(fPtr->Options, OPTION_DRI2_OVERLAY, TRUE),
		xf86ReturnOptValBool(fPtr->Options, OPTION_SWAPBUFFERS_WAIT, TRUE));
1054

1055
	    if (fPtr->SunxiMaliDRI2_private) {
1056
1057
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "using DRI2 integration for Mali GPU (UMP buffers)\n");
1058
1059
1060
1061
1062
1063
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "Mali binary drivers can only accelerate EGL/GLES\n");
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "so AIGLX/GLX is expected to fail or fallback to software\n");
	    }
	    else {
1064
1065
		xf86DrvMsg(pScrn->scrnIndex, X_INFO,
		           "failed to enable DRI2 integration for Mali GPU\n");
1066
	    }
1067
1068
1069
1070
1071
	}
	else {
	    xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	               "DRI2 integration for Mali GPU is disabled in xorg.conf\n");
	}
1072
1073
1074
1075
1076
#else
	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	           "no 3D acceleration because the driver has been compiled without libUMP\n");
	xf86DrvMsg(pScrn->scrnIndex, X_INFO,
	           "if this is wrong and needs to be fixed, please check ./configure log\n");
1077
1078
#endif

Kaleb Keithley's avatar
Kaleb Keithley committed
1079
1080
1081
1082
1083
1084
	TRACE_EXIT("FBDevScreenInit");

	return TRUE;
}

static Bool
Dave Airlie's avatar
Dave Airlie committed
1085
FBDevCloseScreen(CLOSE_SCREEN_ARGS_DECL)
Kaleb Keithley's avatar
Kaleb Keithley committed
1086
{
Dave Airlie's avatar
Dave Airlie committed
1087
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
1088
	FBDevPtr fPtr = FBDEVPTR(pScrn);
1089

1090
1091
1092
1093
1094
1095
1096
1097
#ifdef HAVE_LIBUMP
	if (fPtr->SunxiMaliDRI2_private) {
	    SunxiMaliDRI2_Close(pScreen);
	    free(fPtr->SunxiMaliDRI2_private);
	    fPtr->SunxiMaliDRI2_private = NULL;
	}
#endif

1098
1099
1100
1101
1102
1103
	if (fPtr->SunxiDispHardwareCursor_private) {
	    SunxiDispHardwareCursor_Close(pScreen);
	    free(fPtr->SunxiDispHardwareCursor_private);
	    fPtr->SunxiDispHardwareCursor_private = NULL;
	}

1104
1105
1106
1107
1108
1109
1110
1111
#if XV
	if (fPtr->SunxiVideo_private) {
	    SunxiVideo_Close(pScreen);
	    free(fPtr->SunxiVideo_private);
	    fPtr->SunxiVideo_private = NULL;
	}
#endif

Kaleb Keithley's avatar
Kaleb Keithley committed
1112
1113
	fbdevHWRestore(pScrn);
	fbdevHWUnmapVidmem(pScrn);
1114
	if (fPtr->shadow) {
1115
	    shadowRemove(pScreen, pScreen->GetScreenPixmap(pScreen));
1116
	    free(fPtr->shadow);
1117
1118
	    fPtr->shadow = NULL;
	}
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128

	if (fPtr->SunxiG2D_private) {
	    SunxiG2D_Close(pScreen);
	    free(fPtr->SunxiG2D_private);
	    fPtr->SunxiG2D_private = NULL;
	}
	if (fPtr->sunxi_disp_private) {
	    sunxi_disp_close(fPtr->sunxi_disp_private);
	    fPtr->sunxi_disp_private = NULL;
	}
1129
1130
1131
1132
	if (fPtr->cpu_backend_private) {
	    cpu_backend_close(fPtr->cpu_backend_private);
	    fPtr->cpu_backend_private = NULL;
	}
1133

1134
	if (fPtr->backing_store_tuner_private) {
1135
	    BackingStoreTuner_Close(pScreen);
1136
1137
1138
1139
	    free(fPtr->backing_store_tuner_private);
	    fPtr->backing_store_tuner_private = NULL;
	}

Kaleb Keithley's avatar
Kaleb Keithley committed
1140
	if (fPtr->pDGAMode) {
1141
	  free(fPtr->pDGAMode);
Kaleb Keithley's avatar
Kaleb Keithley committed
1142
1143
1144
1145
1146
	  fPtr->pDGAMode = NULL;
	  fPtr->nDGAMode = 0;
	}
	pScrn->vtSema = FALSE;

1147
	pScreen->CreateScreenResources = fPtr->CreateScreenResources;
Kaleb Keithley's avatar
Kaleb Keithley committed
1148
	pScreen->CloseScreen = fPtr->CloseScreen;
Dave Airlie's avatar
Dave Airlie committed
1149
	return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
Kaleb Keithley's avatar
Kaleb Keithley committed
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
}



/***********************************************************************
 * Shadow stuff
 ***********************************************************************/

static void *
FBDevWindowLinear(ScreenPtr pScreen, CARD32 row, CARD32 offset, int mode,
		 CARD32 *size, void *closure)
{
1162
    ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
Kaleb Keithley's avatar
Kaleb Keithley committed
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
    FBDevPtr fPtr = FBDEVPTR(pScrn);

    if (!pScrn->vtSema)
      return NULL;

    if (fPtr->lineLength)
      *size = fPtr->lineLength;
    else
      *size = fPtr->lineLength = fbdevHWGetLineLength(pScrn);

1173
    return ((CARD8 *)fPtr->fbstart + row * fPtr->lineLength + offset);
Kaleb Keithley's avatar
Kaleb Keithley committed
1174
1175
1176
}

static void
Dave Airlie's avatar
Dave Airlie committed
1177
FBDevPointerMoved(SCRN_ARG_TYPE arg, int x, int y)
Kaleb Keithley's avatar
Kaleb Keithley committed
1178
{
Dave Airlie's avatar
Dave Airlie committed
1179
    SCRN_INFO_PTR(arg);
Kaleb Keithley's avatar
Kaleb Keithley committed
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
    FBDevPtr fPtr = FBDEVPTR(pScrn);
    int newX, newY;

    switch (fPtr->rotate)
    {
    case FBDEV_ROTATE_CW:
	/* 90 degrees CW rotation. */
	newX = pScrn->pScreen->height - y - 1;
	newY = x;
	break;

    case FBDEV_ROTATE_CCW:
	/* 90 degrees CCW rotation. */
	newX = y;
	newY = pScrn->pScreen->width - x - 1;
	break;

    case FBDEV_ROTATE_UD:
	/* 180 degrees UD rotation. */
	newX = pScrn->pScreen->width - x - 1;
	newY = pScrn->pScreen->height - y - 1;
	break;

    default:
	/* No rotation. */
	newX = x;
	newY = y;
	break;
    }

    /* Pass adjusted pointer coordinates to wrapped PointerMoved function. */
Dave Airlie's avatar
Dave Airlie committed
1211
    (*fPtr->PointerMoved)(arg, newX, newY);
Kaleb Keithley's avatar
Kaleb Keithley committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
}


/***********************************************************************
 * DGA stuff
 ***********************************************************************/
static Bool FBDevDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
				   unsigned char **ApertureBase,
				   int *ApertureSize, int *ApertureOffset,
				   int *flags);
static Bool FBDevDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode);
static void FBDevDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags);

static Bool
FBDevDGAOpenFramebuffer(ScrnInfoPtr pScrn, char **DeviceName,
		       unsigned char **ApertureBase, int *ApertureSize,
		       int *ApertureOffset, int *flags)
{
    *DeviceName = NULL;		/* No special device */
    *ApertureBase = (unsigned char *)(pScrn->memPhysBase);
    *ApertureSize = pScrn->videoRam;
    *ApertureOffset = pScrn->fbOffset;
    *flags = 0;

    return TRUE;
}

static Bool
FBDevDGASetMode(ScrnInfoPtr pScrn, DGAModePtr pDGAMode)
{
    DisplayModePtr pMode;
    int scrnIdx = pScrn->pScreen->myNum;
    int frameX0, frameY0;

    if (pDGAMode) {
	pMode = pDGAMode->mode;
	frameX0 = frameY0 = 0;
    }
    else {
	if (!(pMode = pScrn->currentMode))
	    return TRUE;

	frameX0 = pScrn->frameX0;
	frameY0 = pScrn->frameY0;
    }

Dave Airlie's avatar
Dave Airlie committed
1258
    if (!(*pScrn->SwitchMode)(SWITCH_MODE_ARGS(pScrn, pMode)))
Kaleb Keithley's avatar
Kaleb Keithley committed
1259
	return FALSE;
Dave Airlie's avatar
Dave Airlie committed
1260
    (*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, frameX0, frameY0));
Kaleb Keithley's avatar
Kaleb Keithley committed
1261
1262
1263
1264
1265
1266
1267

    return TRUE;
}

static void
FBDevDGASetViewport(ScrnInfoPtr pScrn, int x, int y, int flags)
{
Dave Airlie's avatar
Dave Airlie committed
1268
    (*pScrn->AdjustFrame)(ADJUST_FRAME_ARGS(pScrn, x, y));
Kaleb Keithley's avatar
Kaleb Keithley committed
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
}

static int
FBDevDGAGetViewport(ScrnInfoPtr pScrn)
{
    return (0);
}

static DGAFunctionRec FBDevDGAFunctions =
{
    FBDevDGAOpenFramebuffer,
    NULL,       /* CloseFramebuffer */
    FBDevDGASetMode,
    FBDevDGASetViewport,
    FBDevDGAGetViewport,
    NULL,       /* Sync */
    NULL,       /* FillRect */
    NULL,       /* BlitRect */
    NULL,       /* BlitTransRect */
};

static void
FBDevDGAAddModes(ScrnInfoPtr pScrn)
{
    FBDevPtr fPtr = FBDEVPTR(pScrn);
    DisplayModePtr pMode = pScrn->modes;
    DGAModePtr pDGAMode;

    do {
1298
1299
	pDGAMode = realloc(fPtr->pDGAMode,
		           (fPtr->nDGAMode + 1) * sizeof(DGAModeRec));
Kaleb Keithley's avatar
Kaleb Keithley committed
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
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
	if (!pDGAMode)
	    break;

	fPtr->pDGAMode = pDGAMode;
	pDGAMode += fPtr->nDGAMode;
	(void)memset(pDGAMode, 0, sizeof(DGAModeRec));

	++fPtr->nDGAMode;
	pDGAMode->mode = pMode;
	pDGAMode->flags = DGA_CONCURRENT_ACCESS | DGA_PIXMAP_AVAILABLE;
	pDGAMode->byteOrder = pScrn->imageByteOrder;
	pDGAMode->depth = pScrn->depth;
	pDGAMode->bitsPerPixel = pScrn->bitsPerPixel;
	pDGAMode->red_mask = pScrn->mask.red;
	pDGAMode->green_mask = pScrn->mask.green;
	pDGAMode->blue_mask = pScrn->mask.blue;
	pDGAMode->visualClass = pScrn->bitsPerPixel > 8 ?
	    TrueColor : PseudoColor;
	pDGAMode->xViewportStep = 1;
	pDGAMode->yViewportStep = 1;
	pDGAMode->viewportWidth = pMode->HDisplay;
	pDGAMode->viewportHeight = pMode->VDisplay;

	if (fPtr->lineLength)
	  pDGAMode->bytesPerScanline = fPtr->lineLength;
	else
	  pDGAMode->bytesPerScanline = fPtr->lineLength = fbdevHWGetLineLength(pScrn);

	pDGAMode->imageWidth = pMode->HDisplay;
	pDGAMode->imageHeight =  pMode->VDisplay;
	pDGAMode->pixmapWidth = pDGAMode->imageWidth;
	pDGAMode->pixmapHeight = pDGAMode->imageHeight;
	pDGAMode->maxViewportX = pScrn->virtualX -
				    pDGAMode->viewportWidth;
	pDGAMode->maxViewportY = pScrn->virtualY -
				    pDGAMode->viewportHeight;

	pDGAMode->address = fPtr->fbstart;

	pMode = pMode->next;
    } while (pMode != pScrn->modes);
}

static Bool
FBDevDGAInit(ScrnInfoPtr pScrn, ScreenPtr pScreen)
{
1346
#ifdef XFreeXDGA
Kaleb Keithley's avatar
Kaleb Keithley committed
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
    FBDevPtr fPtr = FBDEVPTR(pScrn);

    if (pScrn->depth < 8)
	return FALSE;

    if (!fPtr->nDGAMode)
	FBDevDGAAddModes(pScrn);

    return (DGAInit(pScreen, &FBDevDGAFunctions,
	    fPtr->pDGAMode, fPtr->nDGAMode));
1357
1358
1359
#else
    return TRUE;
#endif
Kaleb Keithley's avatar
Kaleb Keithley committed
1360
}
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375

static Bool
FBDevDriverFunc(ScrnInfoPtr pScrn, xorgDriverFuncOp op, pointer ptr)
{
    xorgHWFlags *flag;
    
    switch (op) {
	case GET_REQUIRED_HW_INTERFACES:
	    flag = (CARD32*)ptr;
	    (*flag) = 0;
	    return TRUE;
	default:
	    return FALSE;
    }
}