summaryrefslogtreecommitdiff
path: root/src/gallium/state_trackers/xorg/xorg_driver.c
blob: f53a879a14a1e01364bdf9aa0d9de1a6201e10af (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
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
/*
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * 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, sub license, 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 NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS 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.
 *
 *
 * Author: Alan Hourihane <alanh@tungstengraphics.com>
 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
 *
 */


#include "xorg-server.h"
#include "xf86.h"
#include "xf86_OSproc.h"
#include "compiler.h"
#include "xf86PciInfo.h"
#include "xf86Pci.h"
#include "mipointer.h"
#include "micmap.h"
#include <X11/extensions/randr.h>
#include "fb.h"
#include "edid.h"
#include "xf86i2c.h"
#include "xf86Crtc.h"
#include "miscstruct.h"
#include "dixstruct.h"
#include "xf86xv.h"
#ifndef XSERVER_LIBPCIACCESS
#error "libpciaccess needed"
#endif

#include <pciaccess.h>

#include "pipe/p_context.h"
#include "xorg_tracker.h"
#include "xorg_winsys.h"

#ifdef HAVE_LIBKMS
#include "libkms.h"
#endif

/*
 * Functions and symbols exported to Xorg via pointers.
 */

static Bool drv_pre_init(ScrnInfoPtr pScrn, int flags);
static Bool drv_screen_init(int scrnIndex, ScreenPtr pScreen, int argc,
			    char **argv);
static Bool drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags);
static void drv_adjust_frame(int scrnIndex, int x, int y, int flags);
static Bool drv_enter_vt(int scrnIndex, int flags);
static void drv_leave_vt(int scrnIndex, int flags);
static void drv_free_screen(int scrnIndex, int flags);
static ModeStatus drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose,
			         int flags);

typedef enum
{
    OPTION_SW_CURSOR,
    OPTION_2D_ACCEL,
    OPTION_DEBUG_FALLBACK,
} drv_option_enums;

static const OptionInfoRec drv_options[] = {
    {OPTION_SW_CURSOR, "SWcursor", OPTV_BOOLEAN, {0}, FALSE},
    {OPTION_2D_ACCEL, "2DAccel", OPTV_BOOLEAN, {0}, FALSE},
    {OPTION_DEBUG_FALLBACK, "DebugFallback", OPTV_BOOLEAN, {0}, FALSE},
    {-1, NULL, OPTV_NONE, {0}, FALSE}
};


/*
 * Exported Xorg driver functions to winsys
 */

const OptionInfoRec *
xorg_tracker_available_options(int chipid, int busid)
{
    return drv_options;
}

void
xorg_tracker_set_functions(ScrnInfoPtr scrn)
{
    scrn->PreInit = drv_pre_init;
    scrn->ScreenInit = drv_screen_init;
    scrn->SwitchMode = drv_switch_mode;
    scrn->AdjustFrame = drv_adjust_frame;
    scrn->EnterVT = drv_enter_vt;
    scrn->LeaveVT = drv_leave_vt;
    scrn->FreeScreen = drv_free_screen;
    scrn->ValidMode = drv_valid_mode;
}

Bool
xorg_tracker_have_modesetting(ScrnInfoPtr pScrn, struct pci_device *device)
{
    char *BusID = xalloc(64);
    sprintf(BusID, "pci:%04x:%02x:%02x.%d",
	    device->domain, device->bus,
	    device->dev, device->func);

    if (drmCheckModesettingSupported(BusID)) {
	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
		       "Drm modesetting not supported %s\n", BusID);
	xfree(BusID);
	return FALSE;
    }

    xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
		   "Drm modesetting supported on %s\n", BusID);

    xfree(BusID);
    return TRUE;
}


/*
 * Internal function definitions
 */

static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn);
static Bool drv_close_screen(int scrnIndex, ScreenPtr pScreen);
static Bool drv_save_hw_state(ScrnInfoPtr pScrn);
static Bool drv_restore_hw_state(ScrnInfoPtr pScrn);


/*
 * Internal functions
 */

static Bool
drv_get_rec(ScrnInfoPtr pScrn)
{
    if (pScrn->driverPrivate)
	return TRUE;

    pScrn->driverPrivate = xnfcalloc(sizeof(modesettingRec), 1);

    return TRUE;
}

static void
drv_free_rec(ScrnInfoPtr pScrn)
{
    if (!pScrn)
	return;

    if (!pScrn->driverPrivate)
	return;

    xfree(pScrn->driverPrivate);

    pScrn->driverPrivate = NULL;
}

static void
drv_probe_ddc(ScrnInfoPtr pScrn, int index)
{
    ConfiguredMonitor = NULL;
}

static Bool
drv_crtc_resize(ScrnInfoPtr pScrn, int width, int height)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    PixmapPtr rootPixmap;
    ScreenPtr pScreen = pScrn->pScreen;

    if (width == pScrn->virtualX && height == pScrn->virtualY)
	return TRUE;

    pScrn->virtualX = width;
    pScrn->virtualY = height;

    /*
     * Remove the old framebuffer & texture.
     */
    drmModeRmFB(ms->fd, ms->fb_id);
    if (!ms->destroy_front_buffer(pScrn))
	FatalError("failed to destroy front buffer\n");

    rootPixmap = pScreen->GetScreenPixmap(pScreen);
    if (!pScreen->ModifyPixmapHeader(rootPixmap, width, height, -1, -1, -1, NULL))
	return FALSE;

    pScrn->displayWidth = rootPixmap->devKind / (rootPixmap->drawable.bitsPerPixel / 8);

    /* now create new frontbuffer */
    return ms->create_front_buffer(pScrn) && ms->bind_front_buffer(pScrn);
}

static const xf86CrtcConfigFuncsRec crtc_config_funcs = {
    .resize = drv_crtc_resize
};

static Bool
drv_init_drm(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);

    /* deal with server regeneration */
    if (ms->fd < 0) {
	char *BusID;

	BusID = xalloc(64);
	sprintf(BusID, "PCI:%d:%d:%d",
		((ms->PciInfo->domain << 8) | ms->PciInfo->bus),
		ms->PciInfo->dev, ms->PciInfo->func
	    );


	ms->api = drm_api_create();
	ms->fd = drmOpen(ms->api ? ms->api->driver_name : NULL, BusID);
	xfree(BusID);

	if (ms->fd >= 0)
	    return TRUE;

	if (ms->api && ms->api->destroy)
	    ms->api->destroy(ms->api);

	ms->api = NULL;

	return FALSE;
    }

    return TRUE;
}

static Bool
drv_close_drm(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);

    if (ms->api && ms->api->destroy)
	ms->api->destroy(ms->api);
    ms->api = NULL;

    drmClose(ms->fd);
    ms->fd = -1;

    return TRUE;
}

static Bool
drv_init_resource_management(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    /*
    ScreenPtr pScreen = pScrn->pScreen;
    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
    Bool fbAccessDisabled;
    CARD8 *fbstart;
     */

    if (ms->screen || ms->kms)
	return TRUE;

    if (ms->api) {
	ms->screen = ms->api->create_screen(ms->api, ms->fd, NULL);

	if (ms->screen)
	    return TRUE;

	if (ms->api->destroy)
	    ms->api->destroy(ms->api);

	ms->api = NULL;
    }

#ifdef HAVE_LIBKMS
    if (!kms_create(ms->fd, &ms->kms))
	return TRUE;
#endif

    return FALSE;
}

static Bool
drv_close_resource_management(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    int i;

    if (ms->screen) {
	assert(ms->ctx == NULL);

	for (i = 0; i < XORG_NR_FENCES; i++) {
	    if (ms->fence[i]) {
		ms->screen->fence_finish(ms->screen, ms->fence[i], 0);
		ms->screen->fence_reference(ms->screen, &ms->fence[i], NULL);
	    }
	}
	ms->screen->destroy(ms->screen);
    }
    ms->screen = NULL;

#ifdef HAVE_LIBKMS
    if (ms->kms)
	kms_destroy(&ms->kms);
#endif

    return TRUE;
}

static Bool
drv_pre_init(ScrnInfoPtr pScrn, int flags)
{
    xf86CrtcConfigPtr xf86_config;
    modesettingPtr ms;
    rgb defaultWeight = { 0, 0, 0 };
    EntityInfoPtr pEnt;
    EntPtr msEnt = NULL;
    int max_width, max_height;

    if (pScrn->numEntities != 1)
	return FALSE;

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

    if (flags & PROBE_DETECT) {
	drv_probe_ddc(pScrn, pEnt->index);
	return TRUE;
    }

    /* Allocate driverPrivate */
    if (!drv_get_rec(pScrn))
	return FALSE;

    ms = modesettingPTR(pScrn);
    ms->SaveGeneration = -1;
    ms->pEnt = pEnt;

    pScrn->displayWidth = 640;	       /* default it */

    if (ms->pEnt->location.type != BUS_PCI)
	return FALSE;

    ms->PciInfo = xf86GetPciInfoForEntity(ms->pEnt->index);

    /* Allocate an entity private if necessary */
    if (xf86IsEntityShared(pScrn->entityList[0])) {
	FatalError("Entity");
#if 0
	msEnt = xf86GetEntityPrivate(pScrn->entityList[0],
				     modesettingEntityIndex)->ptr;
	ms->entityPrivate = msEnt;
#else
	(void)msEnt;
#endif
    } else
	ms->entityPrivate = NULL;

    if (xf86IsEntityShared(pScrn->entityList[0])) {
	if (xf86IsPrimInitDone(pScrn->entityList[0])) {
	    /* do something */
	} else {
	    xf86SetPrimInitDone(pScrn->entityList[0]);
	}
    }

    ms->fd = -1;
    ms->api = NULL;
    if (!drv_init_drm(pScrn))
	return FALSE;

    pScrn->monitor = pScrn->confScreen->monitor;
    pScrn->progClock = TRUE;
    pScrn->rgbBits = 8;

    if (!xf86SetDepthBpp
	(pScrn, 0, 0, 0,
	 PreferConvert24to32 | SupportConvert24to32 | Support32bppFb))
	return FALSE;

    switch (pScrn->depth) {
    case 15:
    case 16:
    case 24:
	break;
    default:
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "Given depth (%d) is not supported by the driver\n",
		   pScrn->depth);
	return FALSE;
    }
    xf86PrintDepthBpp(pScrn);

    if (!xf86SetWeight(pScrn, defaultWeight, defaultWeight))
	return FALSE;
    if (!xf86SetDefaultVisual(pScrn, -1))
	return FALSE;

    /* Process the options */
    xf86CollectOptions(pScrn, NULL);
    if (!(ms->Options = xalloc(sizeof(drv_options))))
	return FALSE;
    memcpy(ms->Options, drv_options, sizeof(drv_options));
    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->Options);

    /* Allocate an xf86CrtcConfig */
    xf86CrtcConfigInit(pScrn, &crtc_config_funcs);
    xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);

    max_width = 8192;
    max_height = 8192;
    xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, max_height);

    if (xf86ReturnOptValBool(ms->Options, OPTION_SW_CURSOR, FALSE)) {
	ms->SWCursor = TRUE;
    }

    drv_save_hw_state(pScrn);

    xorg_crtc_init(pScrn);
    xorg_output_init(pScrn);

    if (!xf86InitialConfiguration(pScrn, TRUE)) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes.\n");
	drv_restore_hw_state(pScrn);
	return FALSE;
    }

    drv_restore_hw_state(pScrn);

    /*
     * If the driver can do gamma correction, it should call xf86SetGamma() here.
     */
    {
	Gamma zeros = { 0.0, 0.0, 0.0 };

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

    if (pScrn->modes == NULL) {
	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
	return FALSE;
    }

    pScrn->currentMode = pScrn->modes;

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

    /* Load the required sub modules */
    if (!xf86LoadSubModule(pScrn, "fb"))
	return FALSE;

    /* XXX: these aren't needed when we are using libkms */
    if (!xf86LoadSubModule(pScrn, "exa"))
	return FALSE;

#ifdef DRI2
    if (!xf86LoadSubModule(pScrn, "dri2"))
	return FALSE;
#endif

    return TRUE;
}

static Bool
drv_save_hw_state(ScrnInfoPtr pScrn)
{
    /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/

    return TRUE;
}

static Bool
drv_restore_hw_state(ScrnInfoPtr pScrn)
{
    /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/

    return TRUE;
}

static void drv_block_handler(int i, pointer blockData, pointer pTimeout,
                              pointer pReadmask)
{
    ScreenPtr pScreen = screenInfo.screens[i];
    modesettingPtr ms = modesettingPTR(xf86Screens[pScreen->myNum]);

    pScreen->BlockHandler = ms->blockHandler;
    pScreen->BlockHandler(i, blockData, pTimeout, pReadmask);
    pScreen->BlockHandler = drv_block_handler;

    if (ms->ctx) {
       int j;

       ms->ctx->flush(ms->ctx, PIPE_FLUSH_RENDER_CACHE, &ms->fence[XORG_NR_FENCES-1]);
       
       if (ms->fence[0])
          ms->ctx->screen->fence_finish(ms->ctx->screen, ms->fence[0], 0);
  
       /* The amount of rendering generated by a block handler can be
        * quite small.  Let us get a fair way ahead of hardware before
        * throttling.
        */
       for (j = 0; j < XORG_NR_FENCES - 1; j++)
          ms->screen->fence_reference(ms->screen,
                                      &ms->fence[j],
                                      ms->fence[j+1]);

       ms->screen->fence_reference(ms->screen,
                                   &ms->fence[XORG_NR_FENCES-1],
                                   NULL);
    }
        

#ifdef DRM_MODE_FEATURE_DIRTYFB
    {
	RegionPtr dirty = DamageRegion(ms->damage);
	unsigned num_cliprects = REGION_NUM_RECTS(dirty);

	if (num_cliprects) {
	    drmModeClip *clip = alloca(num_cliprects * sizeof(drmModeClip));
	    BoxPtr rect = REGION_RECTS(dirty);
	    int i, ret;

	    /* XXX no need for copy? */
	    for (i = 0; i < num_cliprects; i++, rect++) {
		clip[i].x1 = rect->x1;
		clip[i].y1 = rect->y1;
		clip[i].x2 = rect->x2;
		clip[i].y2 = rect->y2;
	    }

	    /* TODO query connector property to see if this is needed */
	    ret = drmModeDirtyFB(ms->fd, ms->fb_id, clip, num_cliprects);
	    if (ret) {
		debug_printf("%s: failed to send dirty (%i, %s)\n",
			     __func__, ret, strerror(-ret));
	    }

	    DamageEmpty(ms->damage);
	}
    }
#endif
}

static Bool
drv_create_screen_resources(ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    modesettingPtr ms = modesettingPTR(pScrn);
    PixmapPtr rootPixmap;
    Bool ret;

    ms->noEvict = TRUE;

    pScreen->CreateScreenResources = ms->createScreenResources;
    ret = pScreen->CreateScreenResources(pScreen);
    pScreen->CreateScreenResources = drv_create_screen_resources;

    ms->bind_front_buffer(pScrn);

    ms->noEvict = FALSE;

    drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);

#ifdef DRM_MODE_FEATURE_DIRTYFB
    rootPixmap = pScreen->GetScreenPixmap(pScreen);
    ms->damage = DamageCreate(NULL, NULL, DamageReportNone, TRUE,
                              pScreen, rootPixmap);

    if (ms->damage) {
       DamageRegister(&rootPixmap->drawable, ms->damage);

       xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Damage tracking initialized\n");
    } else {
       xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
                  "Failed to create screen damage record\n");
       return FALSE;
    }
#else
    (void)rootPixmap;
#endif

    return ret;
}

static Bool
drv_screen_init(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
{
    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
    modesettingPtr ms = modesettingPTR(pScrn);
    VisualPtr visual;

    if (!drv_init_drm(pScrn)) {
	FatalError("Could not init DRM");
	return FALSE;
    }

    if (!drv_init_resource_management(pScrn)) {
	FatalError("Could not init resource management (!pipe_screen && !libkms)");
	return FALSE;
    }

    if (!drv_init_front_buffer_functions(pScrn)) {
	FatalError("Could not init front buffer manager");
	return FALSE;
    }

    pScrn->pScreen = pScreen;

    /* HW dependent - FIXME */
    pScrn->displayWidth = pScrn->virtualX;

    miClearVisualTypes();

    if (!miSetVisualTypes(pScrn->depth,
			  miGetDefaultVisualMask(pScrn->depth),
			  pScrn->rgbBits, pScrn->defaultVisual))
	return FALSE;

    if (!miSetPixmapDepths())
	return FALSE;

    pScrn->memPhysBase = 0;
    pScrn->fbOffset = 0;

    if (!fbScreenInit(pScreen, NULL,
		      pScrn->virtualX, pScrn->virtualY,
		      pScrn->xDpi, pScrn->yDpi,
		      pScrn->displayWidth, pScrn->bitsPerPixel))
	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;
	    }
	}
    }

    fbPictureInit(pScreen, NULL, 0);

    ms->blockHandler = pScreen->BlockHandler;
    pScreen->BlockHandler = drv_block_handler;
    ms->createScreenResources = pScreen->CreateScreenResources;
    pScreen->CreateScreenResources = drv_create_screen_resources;

    xf86SetBlackWhitePixels(pScreen);

    ms->accelerate_2d = xf86ReturnOptValBool(ms->Options, OPTION_2D_ACCEL, FALSE);
    ms->debug_fallback = xf86ReturnOptValBool(ms->Options, OPTION_DEBUG_FALLBACK, TRUE);

    if (ms->screen) {
	ms->exa = xorg_exa_init(pScrn, ms->accelerate_2d);

	xorg_xv_init(pScreen);
#ifdef DRI2
	xorg_dri2_init(pScreen);
#endif
    }

    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "2D Acceleration is %s\n",
	       ms->screen && ms->accelerate_2d ? "enabled" : "disabled");
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Fallback debugging is %s\n",
	       ms->debug_fallback ? "enabled" : "disabled");
#ifdef DRI2
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "3D Acceleration is %s\n",
	       ms->screen ? "enabled" : "disabled");
#else
    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "3D Acceleration is disabled\n");
#endif

    miInitializeBackingStore(pScreen);
    xf86SetBackingStore(pScreen);
    xf86SetSilkenMouse(pScreen);
    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());

    /* Need to extend HWcursor support to handle mask interleave */
    if (!ms->SWCursor)
	xf86_cursors_init(pScreen, 64, 64,
			  HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 |
			  HARDWARE_CURSOR_ARGB);

    /* Must force it before EnterVT, so we are in control of VT and
     * later memory should be bound when allocating, e.g rotate_mem */
    pScrn->vtSema = TRUE;

    pScreen->SaveScreen = xf86SaveScreen;
    ms->CloseScreen = pScreen->CloseScreen;
    pScreen->CloseScreen = drv_close_screen;

    if (!xf86CrtcScreenInit(pScreen))
	return FALSE;

    if (!miCreateDefColormap(pScreen))
	return FALSE;

    xf86DPMSInit(pScreen, xf86DPMSSet, 0);

    if (serverGeneration == 1)
	xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);

    if (ms->winsys_screen_init)
	ms->winsys_screen_init(pScrn);

    return drv_enter_vt(scrnIndex, 1);
}

static void
drv_adjust_frame(int scrnIndex, int x, int y, int flags)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
    xf86OutputPtr output = config->output[config->compat_output];
    xf86CrtcPtr crtc = output->crtc;

    if (crtc && crtc->enabled) {
	crtc->funcs->set_mode_major(crtc, pScrn->currentMode,
				    RR_Rotate_0, x, y);
	crtc->x = output->initial_x + x;
	crtc->y = output->initial_y + y;
    }
}

static void
drv_free_screen(int scrnIndex, int flags)
{
    drv_free_rec(xf86Screens[scrnIndex]);
}

static void
drv_leave_vt(int scrnIndex, int flags)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    modesettingPtr ms = modesettingPTR(pScrn);
    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
    int o;

    if (ms->winsys_leave_vt)
	ms->winsys_leave_vt(pScrn);

    for (o = 0; o < config->num_crtc; o++) {
	xf86CrtcPtr crtc = config->crtc[o];

	xorg_crtc_cursor_destroy(crtc);

	if (crtc->rotatedPixmap || crtc->rotatedData) {
	    crtc->funcs->shadow_destroy(crtc, crtc->rotatedPixmap,
					crtc->rotatedData);
	    crtc->rotatedPixmap = NULL;
	    crtc->rotatedData = NULL;
	}
    }

    drmModeRmFB(ms->fd, ms->fb_id);

    drv_restore_hw_state(pScrn);

    if (drmDropMaster(ms->fd))
	xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
		   "drmDropMaster failed: %s\n", strerror(errno));

    pScrn->vtSema = FALSE;
}

/*
 * This gets called when gaining control of the VT, and from ScreenInit().
 */
static Bool
drv_enter_vt(int scrnIndex, int flags)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    modesettingPtr ms = modesettingPTR(pScrn);

    if (drmSetMaster(ms->fd)) {
	if (errno == EINVAL) {
	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
		       "drmSetMaster failed: 2.6.29 or newer kernel required for "
		       "multi-server DRI\n");
	} else {
	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
		       "drmSetMaster failed: %s\n", strerror(errno));
	}
    }

    /*
     * Only save state once per server generation since that's what most
     * drivers do.  Could change this to save state at each VT enter.
     */
    if (ms->SaveGeneration != serverGeneration) {
	ms->SaveGeneration = serverGeneration;
	drv_save_hw_state(pScrn);
    }

    if (!ms->create_front_buffer(pScrn))
	return FALSE;

    if (!flags && !ms->bind_front_buffer(pScrn))
	return FALSE;

    if (!xf86SetDesiredModes(pScrn))
	return FALSE;

    if (ms->winsys_enter_vt)
	ms->winsys_enter_vt(pScrn);

    return TRUE;
}

static Bool
drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];

    return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
}

static Bool
drv_close_screen(int scrnIndex, ScreenPtr pScreen)
{
    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
    modesettingPtr ms = modesettingPTR(pScrn);

    if (pScrn->vtSema) {
	drv_leave_vt(scrnIndex, 0);
    }

    if (ms->winsys_screen_close)
	ms->winsys_screen_close(pScrn);

#ifdef DRI2
    if (ms->screen)
	xorg_dri2_close(pScreen);
#endif

    pScreen->BlockHandler = ms->blockHandler;
    pScreen->CreateScreenResources = ms->createScreenResources;

#ifdef DRM_MODE_FEATURE_DIRTYFB
    if (ms->damage) {
	DamageUnregister(&pScreen->GetScreenPixmap(pScreen)->drawable, ms->damage);
	DamageDestroy(ms->damage);
	ms->damage = NULL;
    }
#endif

    drmModeRmFB(ms->fd, ms->fb_id);
    ms->destroy_front_buffer(pScrn);

    if (ms->exa)
	xorg_exa_close(pScrn);
    ms->exa = NULL;

    drv_close_resource_management(pScrn);

    drv_close_drm(pScrn);

    pScrn->vtSema = FALSE;
    pScreen->CloseScreen = ms->CloseScreen;
    return (*pScreen->CloseScreen) (scrnIndex, pScreen);
}

static ModeStatus
drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
{
    return MODE_OK;
}


/*
 * Front buffer backing store functions.
 */

static Bool
drv_destroy_front_buffer_ga3d(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    pipe_texture_reference(&ms->root_texture, NULL);
    return TRUE;
}

static Bool
drv_create_front_buffer_ga3d(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    unsigned handle, stride;
    struct pipe_texture *tex;
    int ret;

    ms->noEvict = TRUE;

    tex = xorg_exa_create_root_texture(pScrn, pScrn->virtualX, pScrn->virtualY,
				       pScrn->depth, pScrn->bitsPerPixel);

    if (!tex)
	return FALSE;

    if (!ms->api->local_handle_from_texture(ms->api, ms->screen,
					    tex,
					    &stride,
					    &handle))
	goto err_destroy;

    ret = drmModeAddFB(ms->fd,
		       pScrn->virtualX,
		       pScrn->virtualY,
		       pScrn->depth,
		       pScrn->bitsPerPixel,
		       stride,
		       handle,
		       &ms->fb_id);
    if (ret) {
	debug_printf("%s: failed to create framebuffer (%i, %s)",
		     __func__, ret, strerror(-ret));
	goto err_destroy;
    }

    pScrn->frameX0 = 0;
    pScrn->frameY0 = 0;
    drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);

    pipe_texture_reference(&ms->root_texture, tex);
    pipe_texture_reference(&tex, NULL);

    return TRUE;

err_destroy:
    pipe_texture_reference(&tex, NULL);
    return FALSE;
}

static Bool
drv_bind_front_buffer_ga3d(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    ScreenPtr pScreen = pScrn->pScreen;
    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
    struct pipe_texture *check;

    xorg_exa_set_displayed_usage(rootPixmap);
    xorg_exa_set_shared_usage(rootPixmap);
    xorg_exa_set_texture(rootPixmap, ms->root_texture);
    if (!pScreen->ModifyPixmapHeader(rootPixmap, -1, -1, -1, -1, -1, NULL))
	FatalError("Couldn't adjust screen pixmap\n");

    check = xorg_exa_get_texture(rootPixmap);
    if (ms->root_texture != check)
	FatalError("Created new root texture\n");

    pipe_texture_reference(&check, NULL);
    return TRUE;
}

#ifdef HAVE_LIBKMS
static Bool
drv_destroy_front_buffer_kms(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    ScreenPtr pScreen = pScrn->pScreen;
    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);

    /* XXX Do something with the rootPixmap.
     * This currently works fine but if we are getting crashes in
     * the fb functions after VT switches maybe look more into it.
     */
    (void)rootPixmap;

    if (!ms->root_bo)
	return TRUE;

    kms_bo_unmap(ms->root_bo);
    kms_bo_destroy(&ms->root_bo);
    return TRUE;
}

static Bool
drv_create_front_buffer_kms(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    unsigned handle, stride;
    struct kms_bo *bo;
    unsigned attr[8];
    int ret;

    attr[0] = KMS_BO_TYPE;
#ifdef KMS_BO_TYPE_SCANOUT_X8R8G8B8
    attr[1] = KMS_BO_TYPE_SCANOUT_X8R8G8B8;
#else
    attr[1] = KMS_BO_TYPE_SCANOUT;
#endif
    attr[2] = KMS_WIDTH;
    attr[3] = pScrn->virtualX;
    attr[4] = KMS_HEIGHT;
    attr[5] = pScrn->virtualY;
    attr[6] = 0;

    if (kms_bo_create(ms->kms, attr, &bo))
	return FALSE;

    if (kms_bo_get_prop(bo, KMS_PITCH, &stride))
	goto err_destroy;

    if (kms_bo_get_prop(bo, KMS_HANDLE, &handle))
	goto err_destroy;

    ret = drmModeAddFB(ms->fd,
		       pScrn->virtualX,
		       pScrn->virtualY,
		       pScrn->depth,
		       pScrn->bitsPerPixel,
		       stride,
		       handle,
		       &ms->fb_id);
    if (ret) {
	debug_printf("%s: failed to create framebuffer (%i, %s)",
		     __func__, ret, strerror(-ret));
	goto err_destroy;
    }

    pScrn->frameX0 = 0;
    pScrn->frameY0 = 0;
    drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
    ms->root_bo = bo;

    return TRUE;

err_destroy:
    kms_bo_destroy(&bo);
    return FALSE;
}

static Bool
drv_bind_front_buffer_kms(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    ScreenPtr pScreen = pScrn->pScreen;
    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
    unsigned stride;
    void *ptr;

    if (kms_bo_get_prop(ms->root_bo, KMS_PITCH, &stride))
	return FALSE;

    if (kms_bo_map(ms->root_bo, &ptr))
	goto err_destroy;

    pScreen->ModifyPixmapHeader(rootPixmap,
				pScrn->virtualX,
				pScrn->virtualY,
				pScreen->rootDepth,
				pScrn->bitsPerPixel,
				stride,
				ptr);

    /* This a hack to work around EnableDisableFBAccess setting the pointer
     * the real fix would be to replace pScrn->EnableDisableFBAccess hook
     * and set the rootPixmap->devPrivate.ptr to something valid before that.
     *
     * But in its infinit visdome something uses either this some times before
     * that, so our hook doesn't get called before the crash happens.
     */
    pScrn->pixmapPrivate.ptr = ptr;

    return TRUE;

err_destroy:
    kms_bo_destroy(&ms->root_bo);
    return FALSE;
}
#endif /* HAVE_LIBKMS */

static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn)
{
    modesettingPtr ms = modesettingPTR(pScrn);
    if (ms->screen) {
	ms->destroy_front_buffer = drv_destroy_front_buffer_ga3d;
	ms->create_front_buffer = drv_create_front_buffer_ga3d;
	ms->bind_front_buffer = drv_bind_front_buffer_ga3d;
#ifdef HAVE_LIBKMS
    } else if (ms->kms) {
	ms->destroy_front_buffer = drv_destroy_front_buffer_kms;
	ms->create_front_buffer = drv_create_front_buffer_kms;
	ms->bind_front_buffer = drv_bind_front_buffer_kms;
#endif
    } else
	return FALSE;

    return TRUE;
}

/* vim: set sw=4 ts=8 sts=4: */