summaryrefslogtreecommitdiff
path: root/src/omap_driver.c
blob: 178a4cd74d9aa32a02addcc35dbba37c71fe800a (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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */

/*
 * Copyright © 2011 Texas Instruments, Inc
 *
 * 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 (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 * Authors:
 *    Ian Elliott <ianelliottus@yahoo.com>
 *    Rob Clark <rob@ti.com>
 */

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

#include "omap_driver.h"


Bool omapDebug = 0;

/*
 * Forward declarations:
 */
static const OptionInfoRec *OMAPAvailableOptions(int chipid, int busid);
static void OMAPIdentify(int flags);
static Bool OMAPProbe(DriverPtr drv, int flags);
static Bool OMAPPreInit(ScrnInfoPtr pScrn, int flags);
static Bool OMAPScreenInit(SCREEN_INIT_ARGS_DECL);
static void OMAPLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
		LOCO * colors, VisualPtr pVisual);
static Bool OMAPCloseScreen(CLOSE_SCREEN_ARGS_DECL);
static Bool OMAPCreateScreenResources(ScreenPtr pScreen);
static void OMAPBlockHandler(BLOCKHANDLER_ARGS_DECL);
static Bool OMAPSwitchMode(SWITCH_MODE_ARGS_DECL);
static void OMAPAdjustFrame(ADJUST_FRAME_ARGS_DECL);
static Bool OMAPEnterVT(VT_FUNC_ARGS_DECL);
static void OMAPLeaveVT(VT_FUNC_ARGS_DECL);
static void OMAPFreeScreen(FREE_SCREEN_ARGS_DECL);



/**
 * A structure used by the XFree86 code when loading this driver, so that it
 * can access the Probe() function, and other functions/info that it uses
 * before it calls the Probe() function.  The name of this structure must be
 * the all-upper-case version of the driver name.
 */
_X_EXPORT DriverRec OMAP = {
		OMAP_VERSION,
		(char *)OMAP_DRIVER_NAME,
		OMAPIdentify,
		OMAPProbe,
		OMAPAvailableOptions,
		NULL,
		0,
		NULL,
#ifdef XSERVER_LIBPCIACCESS
		NULL,
		NULL
#endif
};

/** Supported "chipsets." */
static SymTabRec OMAPChipsets[] = {
		/* OMAP2 and earlier not supported */
		{ 0x3430, "OMAP3430 with PowerVR SGX530" },
		{ 0x3630, "OMAP3630 with PowerVR SGX530" },
		{ 0x4430, "OMAP4430 with PowerVR SGX540" },
		{ 0x4460, "OMAP4460 with PowerVR SGX540" },
		/*    { 4470, "OMAP4470 with <redacted> ;-)" }, */
		{ 0x5430, "OMAP5430 with PowerVR SGX544 MP" },
		{ 0x5432, "OMAP5432 with PowerVR SGX544 MP" },
		{-1, NULL }
};

/** Supported options, as enum values. */
typedef enum {
	OPTION_DEBUG,
	OPTION_DRI,
	OPTION_NO_ACCEL,
	OPTION_HW_CURSOR,
	/* TODO: probably need to add an option to let user specify bus-id */
} OMAPOpts;

/** Supported options. */
static const OptionInfoRec OMAPOptions[] = {
	{ OPTION_DEBUG,		"Debug",	OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_DRI,		"DRI",		OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_NO_ACCEL,	"NoAccel",	OPTV_BOOLEAN,	{0},	FALSE },
	{ OPTION_HW_CURSOR,	"HWcursor",	OPTV_BOOLEAN,	{0},	FALSE },
	{ -1,				NULL,		OPTV_NONE,		{0},	FALSE }
};

/**
 * Helper function for opening a connection to the DRM.
 */

static int
OMAPOpenDRM(int n)
{
	char bus_id[32];
	snprintf(bus_id, sizeof(bus_id), "platform:omapdrm:%02d", n);
	return drmOpen("omapdrm", bus_id);
}

static Bool
OMAPOpenDRMMaster(ScrnInfoPtr pScrn, int n)
{
	OMAPPtr pOMAP = OMAPPTR(pScrn);
	drmSetVersion sv;
	int err;

	pOMAP->drmFD = OMAPOpenDRM(n);
	if (pOMAP->drmFD == -1) {
		ERROR_MSG("Cannot open a connection with the DRM.");
		return FALSE;
	}

	/* Check that what we opened was a master or a master-capable FD,
	 * by setting the version of the interface we'll use to talk to it.
	 * (see DRIOpenDRMMaster() in DRI1)
	 */
	sv.drm_di_major = 1;
	sv.drm_di_minor = 1;
	sv.drm_dd_major = -1;
	sv.drm_dd_minor = -1;
	err = drmSetInterfaceVersion(pOMAP->drmFD, &sv);
	if (err != 0) {
		ERROR_MSG("Cannot set the DRM interface version.");
		drmClose(pOMAP->drmFD);
		pOMAP->drmFD = -1;
		return FALSE;
	}

	pOMAP->deviceName = drmGetDeviceNameFromFd(pOMAP->drmFD);

	return TRUE;
}



/**
 * Helper function for closing a connection to the DRM.
 */
static void
OMAPCloseDRMMaster(ScrnInfoPtr pScrn)
{
	OMAPPtr pOMAP = OMAPPTR(pScrn);

	if (pOMAP && (pOMAP->drmFD > 0)) {
		drmFree(pOMAP->deviceName);
		drmClose(pOMAP->drmFD);
		pOMAP->drmFD = -1;
	}
}



/**
 * Helper function for calculating the stride of pixmaps.
 * TODO get stride requirements from kernel driver, or from EXA module,
 * rather than hard-coding..
 */
#define STRIDE_BOUNDARY 32
unsigned int
OMAPCalculateStride(unsigned int width, unsigned int bitsPerPixel)
{
	unsigned int alignedWidth;
	alignedWidth = (width + (STRIDE_BOUNDARY - 1)) & ~(STRIDE_BOUNDARY - 1);
	return ((alignedWidth * bitsPerPixel) + 7) / 8;
}
#define TILED_STRIDE_BOUNDARY 4096
unsigned int
OMAPCalculateTiledStride(unsigned int width, unsigned int bitsPerPixel)
{
	unsigned int stride = OMAPCalculateStride(width, bitsPerPixel);
	stride = (stride + (4096 - 1)) & ~(4096 - 1);
	return stride;
}


static Bool
OMAPMapMem(ScrnInfoPtr pScrn)
{
	OMAPPtr pOMAP = OMAPPTR(pScrn);
	int pitch;

	pitch = OMAPCalculateStride(pScrn->virtualX, pScrn->bitsPerPixel);

	DEBUG_MSG("allocating new scanout buffer: %dx%d (%d)",
			pScrn->virtualX, pScrn->virtualY, pitch);

	pOMAP->scanout = omap_bo_new(pOMAP->dev, pScrn->virtualY * pitch,
			OMAP_BO_SCANOUT | OMAP_BO_WC);
	if (!pOMAP->scanout) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "Error allocating scanout buffer\n");
		return FALSE;
	}

	pScrn->displayWidth = pitch / (pScrn->bitsPerPixel / 8);

	return TRUE;
}


static Bool
OMAPUnmapMem(ScrnInfoPtr pScrn)
{
	OMAPPtr pOMAP = OMAPPTR(pScrn);
	drmmode_remove_fb(pScrn);
	omap_bo_del(pOMAP->scanout);
	pOMAP->scanout = NULL;
	return TRUE;
}



/** Let the XFree86 code know the Setup() function. */
static MODULESETUPPROTO(OMAPSetup);

/** Provide basic version information to the XFree86 code. */
static XF86ModuleVersionInfo OMAPVersRec =
{
		OMAP_DRIVER_NAME,
		MODULEVENDORSTRING,
		MODINFOSTRING1,
		MODINFOSTRING2,
		XORG_VERSION_CURRENT,
		OMAP_MAJOR_VERSION, OMAP_MINOR_VERSION, OMAP_PATCHLEVEL,
		ABI_CLASS_VIDEODRV,
		ABI_VIDEODRV_VERSION,
		MOD_CLASS_VIDEODRV,
		{0, 0, 0, 0}
};

/** Let the XFree86 code know about the VersRec and Setup() function. */
_X_EXPORT XF86ModuleData omapModuleData = { &OMAPVersRec, OMAPSetup, NULL };


/**
 * The first function that the XFree86 code calls, after loading this module.
 */
static pointer
OMAPSetup(pointer module, pointer opts, int *errmaj, int *errmin)
{
	static Bool setupDone = FALSE;

	/* This module should be loaded only once, but check to be sure: */
	if (!setupDone) {
		setupDone = TRUE;
		xf86AddDriver(&OMAP, module, 0);

		/* The return value must be non-NULL on success even though there is no
		 * TearDownProc.
		 */
		return (pointer) 1;
	} else {
		if (errmaj)
			*errmaj = LDR_ONCEONLY;
		return NULL;
	}
}


/**
 * Allocate the driver's Screen-specific, "private" data structure and hook it
 * into the ScrnInfoRec's driverPrivate field.
 */
static Bool
OMAPGetRec(ScrnInfoPtr pScrn)
{
	if (pScrn->driverPrivate != NULL)
		return TRUE;

	pScrn->driverPrivate = calloc(1, sizeof(OMAPRec));
	if (pScrn->driverPrivate == NULL)
		return FALSE;

	return TRUE;
}


/**
 * Free the driver's Screen-specific, "private" data structure and NULL-out the
 * ScrnInfoRec's driverPrivate field.
 */
static void
OMAPFreeRec(ScrnInfoPtr pScrn)
{
	if (pScrn->driverPrivate == NULL)
		return;
	free(pScrn->driverPrivate);
	pScrn->driverPrivate = NULL;
}


/**
 * The mandatory AvailableOptions() function.  It returns the available driver
 * options to the "-configure" option, so that an xorg.conf file can be built
 * and the user can see which options are available for them to use.
 */
static const OptionInfoRec *
OMAPAvailableOptions(int chipid, int busid)
{
	return OMAPOptions;
}



/**
 * The mandatory Identify() function.  It is run before Probe(), and prints out
 * an identifying message, which includes the chipset(s) the driver supports.
 */
static void
OMAPIdentify(int flags)
{
	xf86PrintChipsets(OMAP_NAME, "Driver for TI OMAP", OMAPChipsets);
}



/**
 * The driver's Probe() function.  This function finds all instances of the
 * TI OMAP hardware that the driver supports (from within the "xorg.conf"
 * device sections), and for instances not already claimed by another driver,
 * claim the instances, and allocate a ScrnInfoRec.  Only minimal hardware
 * probing is allowed here.
 */
static Bool
OMAPProbe(DriverPtr drv, int flags)
{
	int i;
	ScrnInfoPtr pScrn;
	GDevPtr *devSections;
	int numDevSections;
	Bool foundScreen = FALSE;

	/* Get the "xorg.conf" file device sections that match this driver, and
	 * return (error out) if there are none:
	 */
	numDevSections = xf86MatchDevice(OMAP_DRIVER_NAME, &devSections);
	if (numDevSections <= 0) {
		EARLY_ERROR_MSG("Did not find any matching device section in "
				"configuration file");
		if (flags & PROBE_DETECT) {
			/* if we are probing, assume one and lets see if we can
			 * open the device to confirm it is there:
			 */
			numDevSections = 1;
		} else {
			return FALSE;
		}
	}

	for (i = 0; i < numDevSections; i++) {
		int fd = OMAPOpenDRM(i);
		if (fd != -1) {

			if (flags & PROBE_DETECT) {
				/* just add the device.. we aren't a PCI device, so
				 * call xf86AddBusDeviceToConfigure() directly
				 */
				xf86AddBusDeviceToConfigure(OMAP_DRIVER_NAME,
						BUS_NONE, NULL, i);
				foundScreen = TRUE;
				continue;
			}

			pScrn = xf86AllocateScreen(drv, 0);

			if (!pScrn) {
				EARLY_ERROR_MSG("Cannot allocate a ScrnInfoPtr");
				return FALSE;
			}

			if (devSections) {
				int entity = xf86ClaimNoSlot(drv, 0, devSections[i], TRUE);
				xf86AddEntityToScreen(pScrn, entity);
			}

			foundScreen = TRUE;

			pScrn->driverVersion = OMAP_VERSION;
			pScrn->driverName    = (char *)OMAP_DRIVER_NAME;
			pScrn->name          = (char *)OMAP_NAME;
			pScrn->Probe         = OMAPProbe;
			pScrn->PreInit       = OMAPPreInit;
			pScrn->ScreenInit    = OMAPScreenInit;
			pScrn->SwitchMode    = OMAPSwitchMode;
			pScrn->AdjustFrame   = OMAPAdjustFrame;
			pScrn->EnterVT       = OMAPEnterVT;
			pScrn->LeaveVT       = OMAPLeaveVT;
			pScrn->FreeScreen    = OMAPFreeScreen;

			/* would be nice to be able to keep the connection open.. but
			 * currently we don't allocate the private until PreInit
			 */
			drmClose(fd);
		}
	}
	free(devSections);
	return foundScreen;
}



/**
 * The driver's PreInit() function.  Additional hardware probing is allowed
 * now, including display configuration.
 */
static Bool
OMAPPreInit(ScrnInfoPtr pScrn, int flags)
{
	OMAPPtr pOMAP;
	int default_depth, fbbpp;
	rgb defaultWeight = { 0, 0, 0 };
	rgb defaultMask = { 0, 0, 0 };
	Gamma defaultGamma = { 0.0, 0.0, 0.0 };
	uint64_t value;
	int i;

	TRACE_ENTER();

	if (flags & PROBE_DETECT) {
		ERROR_MSG("The %s driver does not support the \"-configure\" or "
				"\"-probe\" command line arguments.", OMAP_NAME);
		return FALSE;
	}

	/* Check the number of entities, and fail if it isn't one. */
	if (pScrn->numEntities != 1) {
		ERROR_MSG("Driver expected 1 entity, but found %d for screen %d",
				pScrn->numEntities, pScrn->scrnIndex);
		return FALSE;
	}

	/* Allocate the driver's Screen-specific, "private" data structure: */
	OMAPGetRec(pScrn);
	pOMAP = OMAPPTR(pScrn);

	pOMAP->pEntityInfo = xf86GetEntityInfo(pScrn->entityList[0]);

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

	/* Get the current depth, and set it for XFree86: */
	default_depth = 24;  /* TODO: get from kernel */
	fbbpp = 32;  /* TODO: get from kernel */

	if (!xf86SetDepthBpp(pScrn, default_depth, 0, fbbpp, Support32bppFb)) {
		/* The above function prints an error message. */
		goto fail;
	}
	xf86PrintDepthBpp(pScrn);

	/* Set the color weight: */
	if (!xf86SetWeight(pScrn, defaultWeight, defaultMask)) {
		/* The above function prints an error message. */
		goto fail;
	}

	/* Set the gamma: */
	if (!xf86SetGamma(pScrn, defaultGamma)) {
		/* The above function prints an error message. */
		goto fail;
	}

	/* Visual init: */
	if (!xf86SetDefaultVisual(pScrn, -1)) {
		/* The above function prints an error message. */
		goto fail;
	}

	/* We don't support 8-bit depths: */
	if (pScrn->depth < 16) {
		ERROR_MSG("The requested default visual (%s) has an unsupported "
				"depth (%d).",
				xf86GetVisualName(pScrn->defaultVisual), pScrn->depth);
		goto fail;
	}

	/* Using a programmable clock: */
	pScrn->progClock = TRUE;

	/* Open a connection to the DRM, so we can communicate with the KMS code: */
	if (!OMAPOpenDRMMaster(pScrn, 0)) {
		goto fail;
	}
	DEBUG_MSG("Became DRM master.");

	/* create DRM device instance: */
	pOMAP->dev = omap_device_new(pOMAP->drmFD);

	/* query chip-id: */
	if (omap_get_param(pOMAP->dev, OMAP_PARAM_CHIPSET_ID, &value)) {
		ERROR_MSG("Could not read chipset");
		goto fail;
	}
	pOMAP->chipset = value;

	/* find matching chipset name: */
	for (i = 0; OMAPChipsets[i].name; i++) {
		if (OMAPChipsets[i].token == pOMAP->chipset) {
			pScrn->chipset = (char *)OMAPChipsets[i].name;
			break;
		}
	}

	if (!pScrn->chipset) {
		ERROR_MSG("Unknown chipset: %s", pScrn->chipset);
		goto fail;
	}

	INFO_MSG("Found chipset: %s", pScrn->chipset);

	/*
	 * Process the "xorg.conf" file options:
	 */
	xf86CollectOptions(pScrn, NULL);
	if (!(pOMAP->pOptionInfo = calloc(1, sizeof(OMAPOptions))))
		return FALSE;
	memcpy(pOMAP->pOptionInfo, OMAPOptions, sizeof(OMAPOptions));
	xf86ProcessOptions(pScrn->scrnIndex, pOMAP->pEntityInfo->device->options,
			pOMAP->pOptionInfo);

	/* Determine if the user wants debug messages turned on: */
	omapDebug = xf86ReturnOptValBool(pOMAP->pOptionInfo, OPTION_DEBUG, FALSE);

	pOMAP->dri = xf86ReturnOptValBool(pOMAP->pOptionInfo, OPTION_DRI, TRUE);

	/* Determine if user wants to disable hw mouse cursor: */
	pOMAP->HWCursor = xf86ReturnOptValBool(pOMAP->pOptionInfo,
			OPTION_HW_CURSOR, TRUE);
	INFO_MSG("Using %s cursor", pOMAP->HWCursor ? "HW" : "SW");

	/* Determine if the user wants to disable acceleration: */
	pOMAP->NoAccel = xf86ReturnOptValBool(pOMAP->pOptionInfo,
			OPTION_NO_ACCEL, FALSE);

	/*
	 * Select the video modes:
	 */
	INFO_MSG("Setting the video modes ...");

	/* Don't call drmCheckModesettingSupported() as its written only for
	 * PCI devices.
	 */

	/* Do initial KMS setup: */
	if (!drmmode_pre_init(pScrn, pOMAP->drmFD, (pScrn->bitsPerPixel >> 3))) {
		ERROR_MSG("Cannot get KMS resources");
	} else {
		INFO_MSG("Got KMS resources");
	}

	xf86RandR12PreInit(pScrn);

	/* Let XFree86 calculate or get (from command line) the display DPI: */
	xf86SetDpi(pScrn, 0, 0);

	/* Ensure we have a supported depth: */
	switch (pScrn->bitsPerPixel) {
	case 16:
	case 24:
	case 32:
		break;
	default:
		ERROR_MSG("The requested number of bits per pixel (%d) is unsupported.",
				pScrn->bitsPerPixel);
		goto fail;
	}


	/* Load external sub-modules now: */

	if (!(xf86LoadSubModule(pScrn, "dri2") &&
			xf86LoadSubModule(pScrn, "exa") &&
			xf86LoadSubModule(pScrn, "fb"))) {
		goto fail;
	}

	switch (pOMAP->chipset) {
	case 0x3430:
	case 0x3630:
	case 0x4430:
	case 0x4460:
	case 0x5430:
	case 0x5432:
		if (xf86LoadSubModule(pScrn, SUB_MODULE_PVR)) {
			INFO_MSG("Loaded the %s sub-module", SUB_MODULE_PVR);
		} else {
			INFO_MSG("Cannot load the %s sub-module", SUB_MODULE_PVR);
			/* note that this is not fatal.. since IMG/PVR EXA module
			 * is closed source, it is only optional.
			 */
			pOMAP->NoAccel = TRUE;  /* don't call InitPowerVREXA() */
		}
		break;
		/* case 0x4470: ..; break; */
	default:
		ERROR_MSG("Unsupported chipset: %d", pOMAP->chipset);
		goto fail;
	}

	TRACE_EXIT();
	return TRUE;

fail:
	TRACE_EXIT();
	OMAPFreeRec(pScrn);
	return FALSE;
}


/**
 * Initialize EXA and DRI2
 */
static void
OMAPAccelInit(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	OMAPPtr pOMAP = OMAPPTR(pScrn);

	if (!pOMAP->NoAccel) {
		switch (pOMAP->chipset) {
		case 0x3430:
		case 0x3630:
		case 0x4430:
		case 0x4460:
		case 0x5430:
		case 0x5432:
			INFO_MSG("Initializing the \"%s\" sub-module ...", SUB_MODULE_PVR);
			pOMAP->pOMAPEXA = InitPowerVREXA(pScreen, pScrn, pOMAP->drmFD);
			if (pOMAP->pOMAPEXA) {
				INFO_MSG("Successfully initialized the \"%s\" sub-module",
						SUB_MODULE_PVR);
			} else {
				INFO_MSG("Could not initialize the \"%s\" sub-module",
						SUB_MODULE_PVR);
				pOMAP->NoAccel = TRUE;
			}
			break;
		default:
			ERROR_MSG("Unsupported chipset: %d", pOMAP->chipset);
			pOMAP->NoAccel = TRUE;
			break;
		}
	}

	if (!pOMAP->pOMAPEXA) {
		pOMAP->pOMAPEXA = InitNullEXA(pScreen, pScrn, pOMAP->drmFD);
	}

	if (pOMAP->dri && pOMAP->pOMAPEXA) {
		pOMAP->dri = OMAPDRI2ScreenInit(pScreen);
	} else {
		pOMAP->dri = FALSE;
	}

	if (OMAPVideoScreenInit(pScreen)) {
		INFO_MSG("Initialized XV");
	} else {
		ERROR_MSG("Could not initialize XV");
	}
}

/**
 * The driver's ScreenInit() function.  Fill in pScreen, map the frame buffer,
 * save state, initialize the mode, etc.
 */
static Bool
OMAPScreenInit(SCREEN_INIT_ARGS_DECL)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	OMAPPtr pOMAP = OMAPPTR(pScrn);
	VisualPtr visual;
	xf86CrtcConfigPtr xf86_config;
	int i;

	TRACE_ENTER();

	/* Allocate and map memory areas we need */
	if (!OMAPMapMem(pScrn))
		return FALSE;

	xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);

	/* need to point to new screen on server regeneration */
	for (i = 0; i < xf86_config->num_crtc; i++)
		xf86_config->crtc[i]->scrn = pScrn;
	for (i = 0; i < xf86_config->num_output; i++)
		xf86_config->output[i]->scrn = pScrn;

	/*
	 * The next step is to setup the screen's visuals, and initialize the
	 * framebuffer code.  In cases where the framebuffer's default
	 * choices for things like visual layouts and bits per RGB are OK,
	 * this may be as simple as calling the framebuffer's ScreenInit()
	 * function.  If not, the visuals will need to be setup before calling
	 * a fb ScreenInit() function and fixed up after.
	 *
	 * For most PC hardware at depths >= 8, the defaults that fb uses
	 * are not appropriate.  In this driver, we fixup the visuals after.
	 */

	/*
	 * Reset the visual list.
	 */

	miClearVisualTypes();
	if (!miSetVisualTypes(pScrn->depth, miGetDefaultVisualMask(pScrn->depth),
			pScrn->rgbBits, pScrn->defaultVisual)) {
		ERROR_MSG("Cannot initialize the visual type for %d bits per pixel!",
				pScrn->bitsPerPixel);
		goto fail;
	}

	if (!miSetPixmapDepths()) {
		ERROR_MSG("Cannot initialize the pixmap depth!");
		goto fail;
	}

	/* Initialize some generic 2D drawing functions: */
	if (!fbScreenInit(pScreen, omap_bo_map(pOMAP->scanout),
			pScrn->virtualX, pScrn->virtualY,
			pScrn->xDpi, pScrn->yDpi, pScrn->displayWidth,
			pScrn->bitsPerPixel)) {
		ERROR_MSG("fbScreenInit() failed!");
		goto fail;
	}

	/* 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;
		}
	}

	/* Continue initializing the generic 2D drawing functions after fixing the
	 * RGB ordering:
	 */
	if (!fbPictureInit(pScreen, NULL, 0)) {
		ERROR_MSG("fbPictureInit() failed!");
		goto fail;
	}

	/* Set the initial black & white colormap indices: */
	xf86SetBlackWhitePixels(pScreen);

	/* Initialize external sub-modules for EXA now, this has to be before
	 * miDCInitialize() otherwise stacking order for wrapped ScreenPtr fxns
	 * ends up in the wrong order.
	 */
	OMAPAccelInit(pScreen);

	/* Initialize backing store: */
	miInitializeBackingStore(pScreen);
	xf86SetBackingStore(pScreen);

	/* Cause the cursor position to be updated by the mouse signal handler: */
	xf86SetSilkenMouse(pScreen);

	/* Initialize the cursor: */
	miDCInitialize(pScreen, xf86GetPointerScreenFuncs());

	if (pOMAP->HWCursor) {
		if (!drmmode_cursor_init(pScreen)) {
			ERROR_MSG("Hardware cursor initialization failed");
			pOMAP->HWCursor = FALSE;
		}
	}

	/* XXX -- Is this the right place for this?  The Intel i830 driver says:
	 * "Must force it before EnterVT, so we are in control of VT..."
	 */
	pScrn->vtSema = TRUE;

	/* Take over the virtual terminal from the console, set the desired mode,
	 * etc.:
	 */
	OMAPEnterVT(VT_FUNC_ARGS(0));

	/* Set the desired mode(s): */
	if (!xf86SetDesiredModes(pScrn)) {
		ERROR_MSG("xf86SetDesiredModes() failed!");
		goto fail;
	}

	/* Do some XRandR initialization: */
	if (!xf86CrtcScreenInit(pScreen)) {
		ERROR_MSG("xf86CrtcScreenInit() failed!");
		goto fail;
	}

	if (!miCreateDefColormap(pScreen)) {
		ERROR_MSG("Cannot create colormap!");
		goto fail;
	}

	if (!xf86HandleColormaps(pScreen, 256, 8, OMAPLoadPalette, NULL,
			CMAP_PALETTED_TRUECOLOR)) {
		ERROR_MSG("xf86HandleColormaps() failed!");
		goto fail;
	}

	/* Setup power management: */
	xf86DPMSInit(pScreen, xf86DPMSSet, 0);

	pScreen->SaveScreen = xf86SaveScreen;

	/* Wrap some screen functions: */
	wrap(pOMAP, pScreen, CloseScreen, OMAPCloseScreen);
	wrap(pOMAP, pScreen, CreateScreenResources, OMAPCreateScreenResources);
	wrap(pOMAP, pScreen, BlockHandler, OMAPBlockHandler);

	drmmode_screen_init(pScrn);

	TRACE_EXIT();
	return TRUE;

fail:
	TRACE_EXIT();
	return FALSE;
}


static void
OMAPLoadPalette(ScrnInfoPtr pScrn, int numColors, int *indices,
		LOCO * colors, VisualPtr pVisual)
{
	TRACE_ENTER();
	TRACE_EXIT();
}


/**
 * The driver's CloseScreen() function.  This is called at the end of each
 * server generation.  Restore state, unmap the frame buffer (and any other
 * mapped memory regions), and free per-Screen data structures (except those
 * held by pScrn).
 */
static Bool
OMAPCloseScreen(CLOSE_SCREEN_ARGS_DECL)
{
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
	OMAPPtr pOMAP = OMAPPTR(pScrn);

	TRACE_ENTER();

	drmmode_screen_fini(pScrn);

	if (pScrn->vtSema == TRUE) {
		OMAPLeaveVT(VT_FUNC_ARGS(0));
	}

	if (pOMAP->pOMAPEXA) {
		if (pOMAP->pOMAPEXA->CloseScreen) {
			pOMAP->pOMAPEXA->CloseScreen(CLOSE_SCREEN_ARGS);
		}
	}

	if (pOMAP->dri) {
		OMAPDRI2CloseScreen(pScreen);
	}

	OMAPVideoCloseScreen(pScreen);

	OMAPUnmapMem(pScrn);

	pScrn->vtSema = FALSE;

	unwrap(pOMAP, pScreen, CloseScreen);
	unwrap(pOMAP, pScreen, BlockHandler);
	unwrap(pOMAP, pScreen, CreateScreenResources);

	TRACE_EXIT();

	return (*pScreen->CloseScreen)(CLOSE_SCREEN_ARGS);
}



/**
 * Adjust the screen pixmap for the current location of the front buffer.
 * This is done at EnterVT when buffers are bound as long as the resources
 * have already been created, but the first EnterVT happens before
 * CreateScreenResources.
 */
static Bool
OMAPCreateScreenResources(ScreenPtr pScreen)
{
	ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
	OMAPPtr pOMAP = OMAPPTR(pScrn);

	swap(pOMAP, pScreen, CreateScreenResources);
	if (!(*pScreen->CreateScreenResources) (pScreen))
		return FALSE;
	swap(pOMAP, pScreen, CreateScreenResources);

	return TRUE;
}


static void
OMAPBlockHandler(BLOCKHANDLER_ARGS_DECL)
{
	SCREEN_PTR(arg);
	ScrnInfoPtr pScrn = xf86ScreenToScrn(pScreen);
	OMAPPtr pOMAP = OMAPPTR(pScrn);

	swap(pOMAP, pScreen, BlockHandler);
	(*pScreen->BlockHandler) (BLOCKHANDLER_ARGS);
	swap(pOMAP, pScreen, BlockHandler);

	/* TODO OMAPVideoBlockHandler(), etc.. */
}



/**
 * The driver's SwitchMode() function.  Initialize the new mode for the
 * Screen.
 */
static Bool
OMAPSwitchMode(SWITCH_MODE_ARGS_DECL)
{
	SCRN_INFO_PTR(arg);
	return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
}



/**
 * The driver's AdjustFrame() function.  For cases where the frame buffer is
 * larger than the monitor resolution, this function can pan around the frame
 * buffer within the "viewport" of the monitor.
 */
static void
OMAPAdjustFrame(ADJUST_FRAME_ARGS_DECL)
{
	SCRN_INFO_PTR(arg);
	drmmode_adjust_frame(pScrn, x, y);
}



/**
 * The driver's EnterVT() function.  This is called at server startup time, and
 * when the X server takes over the virtual terminal from the console.  As
 * such, it may need to save the current (i.e. console) HW state, and set the
 * HW state as needed by the X server.
 */
static Bool
OMAPEnterVT(VT_FUNC_ARGS_DECL)
{
	SCRN_INFO_PTR(arg);
	OMAPPtr pOMAP = OMAPPTR(pScrn);
	int ret;

	TRACE_ENTER();

	ret = drmSetMaster(pOMAP->drmFD);
	if (ret) {
		ERROR_MSG("Cannot get DRM master: %s\n", strerror(ret));
	}

	if (!xf86SetDesiredModes(pScrn)) {
		ERROR_MSG("xf86SetDesiredModes() failed!");
		return FALSE;
	}

	TRACE_EXIT();
	return TRUE;
}



/**
 * The driver's LeaveVT() function.  This is called when the X server
 * temporarily gives up the virtual terminal to the console.  As such, it may
 * need to restore the console's HW state.
 */
static void
OMAPLeaveVT(VT_FUNC_ARGS_DECL)
{
	SCRN_INFO_PTR(arg);
	OMAPPtr pOMAP = OMAPPTR(pScrn);
	int ret;

	TRACE_ENTER();

	ret = drmDropMaster(pOMAP->drmFD);
	if (ret) {
		WARNING_MSG("drmDropMaster failed: %s\n", strerror(errno));
	}

	TRACE_EXIT();
}



/**
 * The driver's FreeScreen() function.  This is called at the server's end of
 * life.  This should free any driver-allocated data that was allocated
 * up-to-and-including an unsuccessful ScreenInit() call.
 */
static void
OMAPFreeScreen(FREE_SCREEN_ARGS_DECL)
{
	SCRN_INFO_PTR(arg);
	OMAPPtr pOMAP = OMAPPTR(pScrn);

	TRACE_ENTER();

	if (!pOMAP) {
		/* This can happen if a Screen is deleted after Probe(): */
		return;
	}

	if (pOMAP->pOMAPEXA) {
		if (pOMAP->pOMAPEXA->FreeScreen) {
			pOMAP->pOMAPEXA->FreeScreen(FREE_SCREEN_ARGS(pScrn));
		}
		free(pOMAP->pOMAPEXA);
	}

	omap_device_del(pOMAP->dev);

	OMAPCloseDRMMaster(pScrn);

	OMAPFreeRec(pScrn);

	TRACE_EXIT();
}