xorg_driver.c revision d12f2bb9c03a9e8a08824c849200f5b23c05914c
1/*
2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 *
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
28 *
29 */
30
31
32#include "xorg-server.h"
33#include "xf86.h"
34#include "xf86_OSproc.h"
35#include "compiler.h"
36#include "xf86PciInfo.h"
37#include "xf86Pci.h"
38#include "mipointer.h"
39#include "micmap.h"
40#include <X11/extensions/randr.h>
41#include "fb.h"
42#include "edid.h"
43#include "xf86i2c.h"
44#include "xf86Crtc.h"
45#include "miscstruct.h"
46#include "dixstruct.h"
47#include "xf86xv.h"
48#ifndef XSERVER_LIBPCIACCESS
49#error "libpciaccess needed"
50#endif
51
52#include <pciaccess.h>
53
54#include "pipe/p_context.h"
55#include "xorg_tracker.h"
56#include "xorg_winsys.h"
57
58#ifdef HAVE_LIBKMS
59#include "libkms.h"
60#endif
61
62/*
63 * Functions and symbols exported to Xorg via pointers.
64 */
65
66static Bool drv_pre_init(ScrnInfoPtr pScrn, int flags);
67static Bool drv_screen_init(int scrnIndex, ScreenPtr pScreen, int argc,
68			    char **argv);
69static Bool drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags);
70static void drv_adjust_frame(int scrnIndex, int x, int y, int flags);
71static Bool drv_enter_vt(int scrnIndex, int flags);
72static void drv_leave_vt(int scrnIndex, int flags);
73static void drv_free_screen(int scrnIndex, int flags);
74static ModeStatus drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose,
75			         int flags);
76
77typedef enum
78{
79    OPTION_SW_CURSOR,
80    OPTION_2D_ACCEL,
81    OPTION_DEBUG_FALLBACK,
82    OPTION_THROTTLE_SWAP,
83    OPTION_THROTTLE_DIRTY
84} drv_option_enums;
85
86static const OptionInfoRec drv_options[] = {
87    {OPTION_SW_CURSOR, "SWcursor", OPTV_BOOLEAN, {0}, FALSE},
88    {OPTION_2D_ACCEL, "2DAccel", OPTV_BOOLEAN, {0}, FALSE},
89    {OPTION_DEBUG_FALLBACK, "DebugFallback", OPTV_BOOLEAN, {0}, FALSE},
90    {OPTION_THROTTLE_SWAP, "SwapThrottling", OPTV_BOOLEAN, {0}, FALSE},
91    {OPTION_THROTTLE_DIRTY, "DirtyThrottling", OPTV_BOOLEAN, {0}, FALSE},
92    {-1, NULL, OPTV_NONE, {0}, FALSE}
93};
94
95
96/*
97 * Exported Xorg driver functions to winsys
98 */
99
100const OptionInfoRec *
101xorg_tracker_available_options(int chipid, int busid)
102{
103    return drv_options;
104}
105
106void
107xorg_tracker_set_functions(ScrnInfoPtr scrn)
108{
109    scrn->PreInit = drv_pre_init;
110    scrn->ScreenInit = drv_screen_init;
111    scrn->SwitchMode = drv_switch_mode;
112    scrn->AdjustFrame = drv_adjust_frame;
113    scrn->EnterVT = drv_enter_vt;
114    scrn->LeaveVT = drv_leave_vt;
115    scrn->FreeScreen = drv_free_screen;
116    scrn->ValidMode = drv_valid_mode;
117}
118
119Bool
120xorg_tracker_have_modesetting(ScrnInfoPtr pScrn, struct pci_device *device)
121{
122    char *BusID = xalloc(64);
123    sprintf(BusID, "pci:%04x:%02x:%02x.%d",
124	    device->domain, device->bus,
125	    device->dev, device->func);
126
127    if (drmCheckModesettingSupported(BusID)) {
128	xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
129		       "Drm modesetting not supported %s\n", BusID);
130	xfree(BusID);
131	return FALSE;
132    }
133
134    xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
135		   "Drm modesetting supported on %s\n", BusID);
136
137    xfree(BusID);
138    return TRUE;
139}
140
141
142/*
143 * Internal function definitions
144 */
145
146static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn);
147static Bool drv_close_screen(int scrnIndex, ScreenPtr pScreen);
148
149
150/*
151 * Internal functions
152 */
153
154static Bool
155drv_get_rec(ScrnInfoPtr pScrn)
156{
157    if (pScrn->driverPrivate)
158	return TRUE;
159
160    pScrn->driverPrivate = xnfcalloc(1, sizeof(modesettingRec));
161
162    return TRUE;
163}
164
165static void
166drv_free_rec(ScrnInfoPtr pScrn)
167{
168    if (!pScrn)
169	return;
170
171    if (!pScrn->driverPrivate)
172	return;
173
174    xfree(pScrn->driverPrivate);
175
176    pScrn->driverPrivate = NULL;
177}
178
179static void
180drv_probe_ddc(ScrnInfoPtr pScrn, int index)
181{
182    ConfiguredMonitor = NULL;
183}
184
185static Bool
186drv_crtc_resize(ScrnInfoPtr pScrn, int width, int height)
187{
188    xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
189    modesettingPtr ms = modesettingPTR(pScrn);
190    ScreenPtr pScreen = pScrn->pScreen;
191    int old_width, old_height;
192    PixmapPtr rootPixmap;
193    int i;
194
195    if (width == pScrn->virtualX && height == pScrn->virtualY)
196	return TRUE;
197
198    old_width = pScrn->virtualX;
199    old_height = pScrn->virtualY;
200    pScrn->virtualX = width;
201    pScrn->virtualY = height;
202
203    /* ms->create_front_buffer will remove the old front buffer */
204
205    rootPixmap = pScreen->GetScreenPixmap(pScreen);
206    if (!pScreen->ModifyPixmapHeader(rootPixmap, width, height, -1, -1, -1, NULL))
207	goto error_modify;
208
209    pScrn->displayWidth = rootPixmap->devKind / (rootPixmap->drawable.bitsPerPixel / 8);
210
211    if (!ms->create_front_buffer(pScrn) || !ms->bind_front_buffer(pScrn))
212	goto error_create;
213
214    /*
215     * create && bind will turn off all crtc(s) in the kernel so we need to
216     * re-enable all the crtcs again. For real HW we might want to do this
217     * before destroying the old framebuffer.
218     */
219    for (i = 0; i < xf86_config->num_crtc; i++) {
220	xf86CrtcPtr crtc = xf86_config->crtc[i];
221
222	if (!crtc->enabled)
223	    continue;
224
225	crtc->funcs->set_mode_major(crtc, &crtc->mode, crtc->rotation, crtc->x, crtc->y);
226    }
227
228    return TRUE;
229
230    /*
231     * This is the error recovery path.
232     */
233error_create:
234    if (!pScreen->ModifyPixmapHeader(rootPixmap, old_width, old_height, -1, -1, -1, NULL))
235	FatalError("failed to resize rootPixmap error path\n");
236
237    pScrn->displayWidth = rootPixmap->devKind / (rootPixmap->drawable.bitsPerPixel / 8);
238
239error_modify:
240    pScrn->virtualX = old_width;
241    pScrn->virtualY = old_height;
242
243    if (ms->create_front_buffer(pScrn) && ms->bind_front_buffer(pScrn))
244	return FALSE;
245
246    FatalError("failed to setup old framebuffer\n");
247    return FALSE;
248}
249
250static const xf86CrtcConfigFuncsRec crtc_config_funcs = {
251    .resize = drv_crtc_resize
252};
253
254static Bool
255drv_init_drm(ScrnInfoPtr pScrn)
256{
257    modesettingPtr ms = modesettingPTR(pScrn);
258
259    /* deal with server regeneration */
260    if (ms->fd < 0) {
261	char *BusID;
262
263	BusID = xalloc(64);
264	sprintf(BusID, "PCI:%d:%d:%d",
265		((ms->PciInfo->domain << 8) | ms->PciInfo->bus),
266		ms->PciInfo->dev, ms->PciInfo->func
267	    );
268
269
270	ms->api = drm_api_create();
271	ms->fd = drmOpen(ms->api ? ms->api->driver_name : NULL, BusID);
272	xfree(BusID);
273
274	if (ms->fd >= 0)
275	    return TRUE;
276
277	if (ms->api && ms->api->destroy)
278	    ms->api->destroy(ms->api);
279
280	ms->api = NULL;
281
282	return FALSE;
283    }
284
285    return TRUE;
286}
287
288static Bool
289drv_close_drm(ScrnInfoPtr pScrn)
290{
291    modesettingPtr ms = modesettingPTR(pScrn);
292
293    if (ms->api && ms->api->destroy)
294	ms->api->destroy(ms->api);
295    ms->api = NULL;
296
297    drmClose(ms->fd);
298    ms->fd = -1;
299
300    return TRUE;
301}
302
303static Bool
304drv_init_resource_management(ScrnInfoPtr pScrn)
305{
306    modesettingPtr ms = modesettingPTR(pScrn);
307    /*
308    ScreenPtr pScreen = pScrn->pScreen;
309    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
310    Bool fbAccessDisabled;
311    CARD8 *fbstart;
312     */
313
314    if (ms->screen || ms->kms)
315	return TRUE;
316
317    if (ms->api) {
318	ms->screen = ms->api->create_screen(ms->api, ms->fd);
319
320	if (ms->screen)
321	    return TRUE;
322
323	if (ms->api->destroy)
324	    ms->api->destroy(ms->api);
325
326	ms->api = NULL;
327    }
328
329#ifdef HAVE_LIBKMS
330    if (!kms_create(ms->fd, &ms->kms))
331	return TRUE;
332#endif
333
334    return FALSE;
335}
336
337static Bool
338drv_close_resource_management(ScrnInfoPtr pScrn)
339{
340    modesettingPtr ms = modesettingPTR(pScrn);
341
342    if (ms->screen) {
343	assert(ms->ctx == NULL);
344	ms->screen->destroy(ms->screen);
345    }
346    ms->screen = NULL;
347
348#ifdef HAVE_LIBKMS
349    if (ms->kms)
350	kms_destroy(&ms->kms);
351#endif
352
353    return TRUE;
354}
355
356static void
357drv_cleanup_fences(ScrnInfoPtr pScrn)
358{
359    modesettingPtr ms = modesettingPTR(pScrn);
360    int i;
361
362    assert(ms->screen);
363
364    for (i = 0; i < XORG_NR_FENCES; i++) {
365	if (ms->fence[i]) {
366	    ms->screen->fence_finish(ms->screen, ms->fence[i], 0);
367	    ms->screen->fence_reference(ms->screen, &ms->fence[i], NULL);
368	}
369    }
370}
371
372static Bool
373drv_pre_init(ScrnInfoPtr pScrn, int flags)
374{
375    xf86CrtcConfigPtr xf86_config;
376    modesettingPtr ms;
377    rgb defaultWeight = { 0, 0, 0 };
378    EntityInfoPtr pEnt;
379    EntPtr msEnt = NULL;
380    int max_width, max_height;
381    CustomizerPtr cust;
382
383    if (pScrn->numEntities != 1)
384	return FALSE;
385
386    pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
387
388    if (flags & PROBE_DETECT) {
389	drv_probe_ddc(pScrn, pEnt->index);
390	return TRUE;
391    }
392
393    cust = (CustomizerPtr) pScrn->driverPrivate;
394    pScrn->driverPrivate = NULL;
395
396    /* Allocate driverPrivate */
397    if (!drv_get_rec(pScrn))
398	return FALSE;
399
400    ms = modesettingPTR(pScrn);
401    ms->pEnt = pEnt;
402    ms->cust = cust;
403
404    pScrn->displayWidth = 640;	       /* default it */
405
406    if (ms->pEnt->location.type != BUS_PCI)
407	return FALSE;
408
409    ms->PciInfo = xf86GetPciInfoForEntity(ms->pEnt->index);
410
411    /* Allocate an entity private if necessary */
412    if (xf86IsEntityShared(pScrn->entityList[0])) {
413	FatalError("Entity");
414#if 0
415	msEnt = xf86GetEntityPrivate(pScrn->entityList[0],
416				     modesettingEntityIndex)->ptr;
417	ms->entityPrivate = msEnt;
418#else
419	(void)msEnt;
420#endif
421    } else
422	ms->entityPrivate = NULL;
423
424    if (xf86IsEntityShared(pScrn->entityList[0])) {
425	if (xf86IsPrimInitDone(pScrn->entityList[0])) {
426	    /* do something */
427	} else {
428	    xf86SetPrimInitDone(pScrn->entityList[0]);
429	}
430    }
431
432    ms->fd = -1;
433    ms->api = NULL;
434    if (!drv_init_drm(pScrn))
435	return FALSE;
436
437    pScrn->monitor = pScrn->confScreen->monitor;
438    pScrn->progClock = TRUE;
439    pScrn->rgbBits = 8;
440
441    if (!xf86SetDepthBpp
442	(pScrn, 0, 0, 0,
443	 PreferConvert24to32 | SupportConvert24to32 | Support32bppFb))
444	return FALSE;
445
446    switch (pScrn->depth) {
447    case 15:
448    case 16:
449    case 24:
450	break;
451    default:
452	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
453		   "Given depth (%d) is not supported by the driver\n",
454		   pScrn->depth);
455	return FALSE;
456    }
457    xf86PrintDepthBpp(pScrn);
458
459    if (!xf86SetWeight(pScrn, defaultWeight, defaultWeight))
460	return FALSE;
461    if (!xf86SetDefaultVisual(pScrn, -1))
462	return FALSE;
463
464    /* Process the options */
465    xf86CollectOptions(pScrn, NULL);
466    if (!(ms->Options = xalloc(sizeof(drv_options))))
467	return FALSE;
468    memcpy(ms->Options, drv_options, sizeof(drv_options));
469    xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->Options);
470
471    /* Allocate an xf86CrtcConfig */
472    xf86CrtcConfigInit(pScrn, &crtc_config_funcs);
473    xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
474
475    max_width = 2048;  /* A very low default */
476    max_height = 2048; /* see screen_init */
477    xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, max_height);
478
479    if (xf86ReturnOptValBool(ms->Options, OPTION_SW_CURSOR, FALSE)) {
480	ms->SWCursor = TRUE;
481    }
482
483    xorg_crtc_init(pScrn);
484    xorg_output_init(pScrn);
485
486    if (!xf86InitialConfiguration(pScrn, TRUE)) {
487	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes.\n");
488	return FALSE;
489    }
490
491    /*
492     * If the driver can do gamma correction, it should call xf86SetGamma() here.
493     */
494    {
495	Gamma zeros = { 0.0, 0.0, 0.0 };
496
497	if (!xf86SetGamma(pScrn, zeros)) {
498	    return FALSE;
499	}
500    }
501
502    if (pScrn->modes == NULL) {
503	xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
504	return FALSE;
505    }
506
507    pScrn->currentMode = pScrn->modes;
508
509    /* Set display resolution */
510    xf86SetDpi(pScrn, 0, 0);
511
512    /* Load the required sub modules */
513    if (!xf86LoadSubModule(pScrn, "fb"))
514	return FALSE;
515
516    /* XXX: these aren't needed when we are using libkms */
517    if (!xf86LoadSubModule(pScrn, "exa"))
518	return FALSE;
519
520#ifdef DRI2
521    if (!xf86LoadSubModule(pScrn, "dri2"))
522	return FALSE;
523#endif
524
525    return TRUE;
526}
527
528static void drv_block_handler(int i, pointer blockData, pointer pTimeout,
529                              pointer pReadmask)
530{
531    ScreenPtr pScreen = screenInfo.screens[i];
532    modesettingPtr ms = modesettingPTR(xf86Screens[pScreen->myNum]);
533
534    pScreen->BlockHandler = ms->blockHandler;
535    pScreen->BlockHandler(i, blockData, pTimeout, pReadmask);
536    pScreen->BlockHandler = drv_block_handler;
537
538    if (ms->ctx) {
539       int j;
540
541       ms->ctx->flush(ms->ctx, PIPE_FLUSH_RENDER_CACHE,
542		      ms->dirtyThrottling ?
543		      &ms->fence[XORG_NR_FENCES-1] :
544		      NULL);
545
546       if (ms->dirtyThrottling) {
547	   if (ms->fence[0])
548	       ms->ctx->screen->fence_finish(ms->ctx->screen,
549					     ms->fence[0], 0);
550
551	   /* The amount of rendering generated by a block handler can be
552	    * quite small.  Let us get a fair way ahead of hardware before
553	    * throttling.
554	    */
555	   for (j = 0; j < XORG_NR_FENCES - 1; j++)
556	       ms->screen->fence_reference(ms->screen,
557					   &ms->fence[j],
558					   ms->fence[j+1]);
559
560	   ms->screen->fence_reference(ms->screen,
561				       &ms->fence[XORG_NR_FENCES-1],
562				       NULL);
563       }
564    }
565
566
567#ifdef DRM_MODE_FEATURE_DIRTYFB
568    {
569	RegionPtr dirty = DamageRegion(ms->damage);
570	unsigned num_cliprects = REGION_NUM_RECTS(dirty);
571
572	if (num_cliprects) {
573	    drmModeClip *clip = alloca(num_cliprects * sizeof(drmModeClip));
574	    BoxPtr rect = REGION_RECTS(dirty);
575	    int i, ret;
576
577	    /* XXX no need for copy? */
578	    for (i = 0; i < num_cliprects; i++, rect++) {
579		clip[i].x1 = rect->x1;
580		clip[i].y1 = rect->y1;
581		clip[i].x2 = rect->x2;
582		clip[i].y2 = rect->y2;
583	    }
584
585	    /* TODO query connector property to see if this is needed */
586	    ret = drmModeDirtyFB(ms->fd, ms->fb_id, clip, num_cliprects);
587	    if (ret) {
588		debug_printf("%s: failed to send dirty (%i, %s)\n",
589			     __func__, ret, strerror(-ret));
590	    }
591
592	    DamageEmpty(ms->damage);
593	}
594    }
595#endif
596}
597
598static Bool
599drv_create_screen_resources(ScreenPtr pScreen)
600{
601    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
602    modesettingPtr ms = modesettingPTR(pScrn);
603    PixmapPtr rootPixmap;
604    Bool ret;
605
606    ms->noEvict = TRUE;
607
608    pScreen->CreateScreenResources = ms->createScreenResources;
609    ret = pScreen->CreateScreenResources(pScreen);
610    pScreen->CreateScreenResources = drv_create_screen_resources;
611
612    ms->bind_front_buffer(pScrn);
613
614    ms->noEvict = FALSE;
615
616    drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
617
618#ifdef DRM_MODE_FEATURE_DIRTYFB
619    rootPixmap = pScreen->GetScreenPixmap(pScreen);
620    ms->damage = DamageCreate(NULL, NULL, DamageReportNone, TRUE,
621                              pScreen, rootPixmap);
622
623    if (ms->damage) {
624       DamageRegister(&rootPixmap->drawable, ms->damage);
625
626       xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Damage tracking initialized\n");
627    } else {
628       xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
629                  "Failed to create screen damage record\n");
630       return FALSE;
631    }
632#else
633    (void)rootPixmap;
634#endif
635
636    return ret;
637}
638
639static Bool
640drv_screen_init(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
641{
642    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
643    modesettingPtr ms = modesettingPTR(pScrn);
644    unsigned max_width, max_height;
645    VisualPtr visual;
646    CustomizerPtr cust = ms->cust;
647    MessageType from_st;
648    MessageType from_dt;
649
650    if (!drv_init_drm(pScrn)) {
651	FatalError("Could not init DRM");
652	return FALSE;
653    }
654
655    if (!drv_init_resource_management(pScrn)) {
656	FatalError("Could not init resource management (!pipe_screen && !libkms)");
657	return FALSE;
658    }
659
660    if (!drv_init_front_buffer_functions(pScrn)) {
661	FatalError("Could not init front buffer manager");
662	return FALSE;
663    }
664
665    /* get max width and height */
666    {
667	drmModeResPtr res;
668	res = drmModeGetResources(ms->fd);
669	max_width = res->max_width;
670	max_height = res->max_height;
671	drmModeFreeResources(res);
672    }
673
674    if (ms->screen) {
675	int max;
676	max = ms->screen->get_param(ms->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
677	max = 1 << (max - 1);
678	max_width = max < max_width ? max : max_width;
679	max_height = max < max_height ? max : max_height;
680    }
681
682    xf86CrtcSetSizeRange(pScrn, 1, 1, max_width, max_height);
683
684    pScrn->pScreen = pScreen;
685
686    /* HW dependent - FIXME */
687    pScrn->displayWidth = pScrn->virtualX;
688
689    miClearVisualTypes();
690
691    if (!miSetVisualTypes(pScrn->depth,
692			  miGetDefaultVisualMask(pScrn->depth),
693			  pScrn->rgbBits, pScrn->defaultVisual))
694	return FALSE;
695
696    if (!miSetPixmapDepths())
697	return FALSE;
698
699    pScrn->memPhysBase = 0;
700    pScrn->fbOffset = 0;
701
702    if (!fbScreenInit(pScreen, NULL,
703		      pScrn->virtualX, pScrn->virtualY,
704		      pScrn->xDpi, pScrn->yDpi,
705		      pScrn->displayWidth, pScrn->bitsPerPixel))
706	return FALSE;
707
708    if (pScrn->bitsPerPixel > 8) {
709	/* Fixup RGB ordering */
710	visual = pScreen->visuals + pScreen->numVisuals;
711	while (--visual >= pScreen->visuals) {
712	    if ((visual->class | DynamicClass) == DirectColor) {
713		visual->offsetRed = pScrn->offset.red;
714		visual->offsetGreen = pScrn->offset.green;
715		visual->offsetBlue = pScrn->offset.blue;
716		visual->redMask = pScrn->mask.red;
717		visual->greenMask = pScrn->mask.green;
718		visual->blueMask = pScrn->mask.blue;
719	    }
720	}
721    }
722
723    fbPictureInit(pScreen, NULL, 0);
724
725    ms->blockHandler = pScreen->BlockHandler;
726    pScreen->BlockHandler = drv_block_handler;
727    ms->createScreenResources = pScreen->CreateScreenResources;
728    pScreen->CreateScreenResources = drv_create_screen_resources;
729
730    xf86SetBlackWhitePixels(pScreen);
731
732    ms->accelerate_2d = xf86ReturnOptValBool(ms->Options, OPTION_2D_ACCEL, FALSE);
733    ms->debug_fallback = xf86ReturnOptValBool(ms->Options, OPTION_DEBUG_FALLBACK, ms->accelerate_2d);
734
735    if (cust && cust->winsys_screen_init)
736	cust->winsys_screen_init(cust, ms->fd);
737
738    ms->swapThrottling = cust ?  cust->swap_throttling : TRUE;
739    from_st = xf86GetOptValBool(ms->Options, OPTION_THROTTLE_SWAP,
740				&ms->swapThrottling) ?
741	X_CONFIG : X_DEFAULT;
742
743    ms->dirtyThrottling = cust ?  cust->dirty_throttling : TRUE;
744    from_dt = xf86GetOptValBool(ms->Options, OPTION_THROTTLE_DIRTY,
745				&ms->dirtyThrottling) ?
746	X_CONFIG : X_DEFAULT;
747
748    if (ms->screen) {
749	ms->exa = xorg_exa_init(pScrn, ms->accelerate_2d);
750
751	xorg_xv_init(pScreen);
752#ifdef DRI2
753	xorg_dri2_init(pScreen);
754#endif
755    }
756
757    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "##################################\n");
758    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "# Usefull debugging info follows #\n");
759    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "##################################\n");
760    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Using %s backend\n",
761	       ms->screen ? "Gallium3D" : "libkms");
762    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "2D Acceleration is %s\n",
763	       ms->screen && ms->accelerate_2d ? "enabled" : "disabled");
764    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Fallback debugging is %s\n",
765	       ms->debug_fallback ? "enabled" : "disabled");
766#ifdef DRI2
767    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "3D Acceleration is %s\n",
768	       ms->screen ? "enabled" : "disabled");
769#else
770    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "3D Acceleration is disabled\n");
771#endif
772    xf86DrvMsg(pScrn->scrnIndex, from_st, "Swap Throttling is %s.\n",
773	       ms->swapThrottling ? "enabled" : "disabled");
774    xf86DrvMsg(pScrn->scrnIndex, from_dt, "Dirty Throttling is %s.\n",
775	       ms->dirtyThrottling ? "enabled" : "disabled");
776
777    xf86DrvMsg(pScrn->scrnIndex, X_INFO, "##################################\n");
778
779    miInitializeBackingStore(pScreen);
780    xf86SetBackingStore(pScreen);
781    xf86SetSilkenMouse(pScreen);
782    miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
783
784    /* Need to extend HWcursor support to handle mask interleave */
785    if (!ms->SWCursor)
786	xf86_cursors_init(pScreen, 64, 64,
787			  HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 |
788			  HARDWARE_CURSOR_ARGB);
789
790    /* Must force it before EnterVT, so we are in control of VT and
791     * later memory should be bound when allocating, e.g rotate_mem */
792    pScrn->vtSema = TRUE;
793
794    pScreen->SaveScreen = xf86SaveScreen;
795    ms->CloseScreen = pScreen->CloseScreen;
796    pScreen->CloseScreen = drv_close_screen;
797
798    if (!xf86CrtcScreenInit(pScreen))
799	return FALSE;
800
801    if (!miCreateDefColormap(pScreen))
802	return FALSE;
803
804    xf86DPMSInit(pScreen, xf86DPMSSet, 0);
805
806    if (serverGeneration == 1)
807	xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
808
809    return drv_enter_vt(scrnIndex, 1);
810}
811
812static void
813drv_adjust_frame(int scrnIndex, int x, int y, int flags)
814{
815    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
816    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
817    xf86OutputPtr output = config->output[config->compat_output];
818    xf86CrtcPtr crtc = output->crtc;
819
820    if (crtc && crtc->enabled) {
821	crtc->funcs->set_mode_major(crtc, pScrn->currentMode,
822				    RR_Rotate_0, x, y);
823	crtc->x = output->initial_x + x;
824	crtc->y = output->initial_y + y;
825    }
826}
827
828static void
829drv_free_screen(int scrnIndex, int flags)
830{
831    drv_free_rec(xf86Screens[scrnIndex]);
832}
833
834static void
835drv_leave_vt(int scrnIndex, int flags)
836{
837    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
838    modesettingPtr ms = modesettingPTR(pScrn);
839    xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
840    CustomizerPtr cust = ms->cust;
841    int o;
842
843    if (cust && cust->winsys_leave_vt)
844	cust->winsys_leave_vt(cust);
845
846    for (o = 0; o < config->num_crtc; o++) {
847	xf86CrtcPtr crtc = config->crtc[o];
848
849	xorg_crtc_cursor_destroy(crtc);
850
851	if (crtc->rotatedPixmap || crtc->rotatedData) {
852	    crtc->funcs->shadow_destroy(crtc, crtc->rotatedPixmap,
853					crtc->rotatedData);
854	    crtc->rotatedPixmap = NULL;
855	    crtc->rotatedData = NULL;
856	}
857    }
858
859    drmModeRmFB(ms->fd, ms->fb_id);
860    ms->fb_id = -1;
861
862    /* idle hardware */
863    if (!ms->kms)
864	drv_cleanup_fences(pScrn);
865
866    if (drmDropMaster(ms->fd))
867	xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
868		   "drmDropMaster failed: %s\n", strerror(errno));
869
870    pScrn->vtSema = FALSE;
871}
872
873/*
874 * This gets called when gaining control of the VT, and from ScreenInit().
875 */
876static Bool
877drv_enter_vt(int scrnIndex, int flags)
878{
879    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
880    modesettingPtr ms = modesettingPTR(pScrn);
881    CustomizerPtr cust = ms->cust;
882
883    if (drmSetMaster(ms->fd)) {
884	if (errno == EINVAL) {
885	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
886		       "drmSetMaster failed: 2.6.29 or newer kernel required for "
887		       "multi-server DRI\n");
888	} else {
889	    xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
890		       "drmSetMaster failed: %s\n", strerror(errno));
891	}
892    }
893
894    if (!ms->create_front_buffer(pScrn))
895	return FALSE;
896
897    if (!flags && !ms->bind_front_buffer(pScrn))
898	return FALSE;
899
900    if (!xf86SetDesiredModes(pScrn))
901	return FALSE;
902
903    if (cust && cust->winsys_enter_vt)
904	cust->winsys_enter_vt(cust);
905
906    return TRUE;
907}
908
909static Bool
910drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags)
911{
912    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
913
914    return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
915}
916
917static Bool
918drv_close_screen(int scrnIndex, ScreenPtr pScreen)
919{
920    ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
921    modesettingPtr ms = modesettingPTR(pScrn);
922    CustomizerPtr cust = ms->cust;
923
924    if (ms->cursor) {
925       FreeCursor(ms->cursor, None);
926       ms->cursor = NULL;
927    }
928
929    if (cust && cust->winsys_screen_close)
930	cust->winsys_screen_close(cust);
931
932#ifdef DRI2
933    if (ms->screen)
934	xorg_dri2_close(pScreen);
935#endif
936
937    pScreen->BlockHandler = ms->blockHandler;
938    pScreen->CreateScreenResources = ms->createScreenResources;
939
940#ifdef DRM_MODE_FEATURE_DIRTYFB
941    if (ms->damage) {
942	DamageUnregister(&pScreen->GetScreenPixmap(pScreen)->drawable, ms->damage);
943	DamageDestroy(ms->damage);
944	ms->damage = NULL;
945    }
946#endif
947
948    drmModeRmFB(ms->fd, ms->fb_id);
949    ms->destroy_front_buffer(pScrn);
950
951    if (ms->exa)
952	xorg_exa_close(pScrn);
953    ms->exa = NULL;
954
955    /* calls drop master make sure we don't talk to 3D HW after that */
956    if (pScrn->vtSema) {
957	drv_leave_vt(scrnIndex, 0);
958    }
959
960    drv_close_resource_management(pScrn);
961
962    drv_close_drm(pScrn);
963
964    pScrn->vtSema = FALSE;
965    pScreen->CloseScreen = ms->CloseScreen;
966    return (*pScreen->CloseScreen) (scrnIndex, pScreen);
967}
968
969static ModeStatus
970drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
971{
972    return MODE_OK;
973}
974
975
976/*
977 * Front buffer backing store functions.
978 */
979
980static Bool
981drv_destroy_front_buffer_ga3d(ScrnInfoPtr pScrn)
982{
983    modesettingPtr ms = modesettingPTR(pScrn);
984
985    if (!ms->root_texture)
986	return TRUE;
987
988    if (ms->fb_id != -1) {
989	drmModeRmFB(ms->fd, ms->fb_id);
990	ms->fb_id = -1;
991    }
992
993    pipe_resource_reference(&ms->root_texture, NULL);
994    return TRUE;
995}
996
997static Bool
998drv_create_front_buffer_ga3d(ScrnInfoPtr pScrn)
999{
1000    modesettingPtr ms = modesettingPTR(pScrn);
1001    struct pipe_resource *tex;
1002    struct winsys_handle whandle;
1003    unsigned fb_id;
1004    int ret;
1005
1006    ms->noEvict = TRUE;
1007
1008    tex = xorg_exa_create_root_texture(pScrn, pScrn->virtualX, pScrn->virtualY,
1009				       pScrn->depth, pScrn->bitsPerPixel);
1010
1011    if (!tex)
1012	return FALSE;
1013
1014    memset(&whandle, 0, sizeof(whandle));
1015    whandle.type = DRM_API_HANDLE_TYPE_KMS;
1016
1017    if (!ms->screen->resource_get_handle(ms->screen, tex, &whandle))
1018	goto err_destroy;
1019
1020    ret = drmModeAddFB(ms->fd,
1021		       pScrn->virtualX,
1022		       pScrn->virtualY,
1023		       pScrn->depth,
1024		       pScrn->bitsPerPixel,
1025		       whandle.stride,
1026		       whandle.handle,
1027		       &fb_id);
1028    if (ret) {
1029	debug_printf("%s: failed to create framebuffer (%i, %s)\n",
1030		     __func__, ret, strerror(-ret));
1031	goto err_destroy;
1032    }
1033
1034    if (!drv_destroy_front_buffer_ga3d(pScrn))
1035	FatalError("%s: failed to take down old framebuffer\n", __func__);
1036
1037    pScrn->frameX0 = 0;
1038    pScrn->frameY0 = 0;
1039    drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
1040
1041    pipe_resource_reference(&ms->root_texture, tex);
1042    pipe_resource_reference(&tex, NULL);
1043    ms->fb_id = fb_id;
1044
1045    return TRUE;
1046
1047err_destroy:
1048    pipe_resource_reference(&tex, NULL);
1049    return FALSE;
1050}
1051
1052static Bool
1053drv_bind_front_buffer_ga3d(ScrnInfoPtr pScrn)
1054{
1055    modesettingPtr ms = modesettingPTR(pScrn);
1056    ScreenPtr pScreen = pScrn->pScreen;
1057    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
1058    struct pipe_resource *check;
1059
1060    xorg_exa_set_displayed_usage(rootPixmap);
1061    xorg_exa_set_shared_usage(rootPixmap);
1062    xorg_exa_set_texture(rootPixmap, ms->root_texture);
1063    if (!pScreen->ModifyPixmapHeader(rootPixmap, -1, -1, -1, -1, -1, NULL))
1064	FatalError("Couldn't adjust screen pixmap\n");
1065
1066    check = xorg_exa_get_texture(rootPixmap);
1067    if (ms->root_texture != check)
1068	FatalError("Created new root texture\n");
1069
1070    pipe_resource_reference(&check, NULL);
1071    return TRUE;
1072}
1073
1074#ifdef HAVE_LIBKMS
1075static Bool
1076drv_destroy_front_buffer_kms(ScrnInfoPtr pScrn)
1077{
1078    modesettingPtr ms = modesettingPTR(pScrn);
1079    ScreenPtr pScreen = pScrn->pScreen;
1080    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
1081
1082    /* XXX Do something with the rootPixmap.
1083     * This currently works fine but if we are getting crashes in
1084     * the fb functions after VT switches maybe look more into it.
1085     */
1086    (void)rootPixmap;
1087
1088    if (!ms->root_bo)
1089	return TRUE;
1090
1091    if (ms->fb_id != -1) {
1092	drmModeRmFB(ms->fd, ms->fb_id);
1093	ms->fb_id = -1;
1094    }
1095
1096    kms_bo_unmap(ms->root_bo);
1097    kms_bo_destroy(&ms->root_bo);
1098    return TRUE;
1099}
1100
1101static Bool
1102drv_create_front_buffer_kms(ScrnInfoPtr pScrn)
1103{
1104    modesettingPtr ms = modesettingPTR(pScrn);
1105    unsigned handle, stride;
1106    struct kms_bo *bo;
1107    unsigned attr[8];
1108    unsigned fb_id;
1109    int ret;
1110
1111    attr[0] = KMS_BO_TYPE;
1112#ifdef KMS_BO_TYPE_SCANOUT_X8R8G8B8
1113    attr[1] = KMS_BO_TYPE_SCANOUT_X8R8G8B8;
1114#else
1115    attr[1] = KMS_BO_TYPE_SCANOUT;
1116#endif
1117    attr[2] = KMS_WIDTH;
1118    attr[3] = pScrn->virtualX;
1119    attr[4] = KMS_HEIGHT;
1120    attr[5] = pScrn->virtualY;
1121    attr[6] = 0;
1122
1123    if (kms_bo_create(ms->kms, attr, &bo))
1124	return FALSE;
1125
1126    if (kms_bo_get_prop(bo, KMS_PITCH, &stride))
1127	goto err_destroy;
1128
1129    if (kms_bo_get_prop(bo, KMS_HANDLE, &handle))
1130	goto err_destroy;
1131
1132    ret = drmModeAddFB(ms->fd,
1133		       pScrn->virtualX,
1134		       pScrn->virtualY,
1135		       pScrn->depth,
1136		       pScrn->bitsPerPixel,
1137		       stride,
1138		       handle,
1139		       &fb_id);
1140    if (ret) {
1141	debug_printf("%s: failed to create framebuffer (%i, %s)",
1142		     __func__, ret, strerror(-ret));
1143	goto err_destroy;
1144    }
1145
1146    if (!drv_destroy_front_buffer_kms(pScrn))
1147	FatalError("%s: could not takedown old bo", __func__);
1148
1149    pScrn->frameX0 = 0;
1150    pScrn->frameY0 = 0;
1151    drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
1152    ms->root_bo = bo;
1153    ms->fb_id = fb_id;
1154
1155    return TRUE;
1156
1157err_destroy:
1158    kms_bo_destroy(&bo);
1159    return FALSE;
1160}
1161
1162static Bool
1163drv_bind_front_buffer_kms(ScrnInfoPtr pScrn)
1164{
1165    modesettingPtr ms = modesettingPTR(pScrn);
1166    ScreenPtr pScreen = pScrn->pScreen;
1167    PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
1168    unsigned stride;
1169    void *ptr;
1170
1171    if (kms_bo_get_prop(ms->root_bo, KMS_PITCH, &stride))
1172	return FALSE;
1173
1174    if (kms_bo_map(ms->root_bo, &ptr))
1175	goto err_destroy;
1176
1177    pScreen->ModifyPixmapHeader(rootPixmap,
1178				pScrn->virtualX,
1179				pScrn->virtualY,
1180				pScreen->rootDepth,
1181				pScrn->bitsPerPixel,
1182				stride,
1183				ptr);
1184
1185    /* This a hack to work around EnableDisableFBAccess setting the pointer
1186     * the real fix would be to replace pScrn->EnableDisableFBAccess hook
1187     * and set the rootPixmap->devPrivate.ptr to something valid before that.
1188     *
1189     * But in its infinit visdome something uses either this some times before
1190     * that, so our hook doesn't get called before the crash happens.
1191     */
1192    pScrn->pixmapPrivate.ptr = ptr;
1193
1194    return TRUE;
1195
1196err_destroy:
1197    kms_bo_destroy(&ms->root_bo);
1198    return FALSE;
1199}
1200#endif /* HAVE_LIBKMS */
1201
1202static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn)
1203{
1204    modesettingPtr ms = modesettingPTR(pScrn);
1205    if (ms->screen) {
1206	ms->destroy_front_buffer = drv_destroy_front_buffer_ga3d;
1207	ms->create_front_buffer = drv_create_front_buffer_ga3d;
1208	ms->bind_front_buffer = drv_bind_front_buffer_ga3d;
1209#ifdef HAVE_LIBKMS
1210    } else if (ms->kms) {
1211	ms->destroy_front_buffer = drv_destroy_front_buffer_kms;
1212	ms->create_front_buffer = drv_create_front_buffer_kms;
1213	ms->bind_front_buffer = drv_bind_front_buffer_kms;
1214#endif
1215    } else
1216	return FALSE;
1217
1218    return TRUE;
1219}
1220
1221CustomizerPtr xorg_customizer(ScrnInfoPtr pScrn)
1222{
1223    return modesettingPTR(pScrn)->cust;
1224}
1225
1226Bool xorg_has_gallium(ScrnInfoPtr pScrn)
1227{
1228    return modesettingPTR(pScrn)->screen != NULL;
1229}
1230
1231/* vim: set sw=4 ts=8 sts=4: */
1232