2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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.
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
32 #include "xorg-server.h"
34 #include "xf86_OSproc.h"
36 #include "xf86PciInfo.h"
38 #include "mipointer.h"
40 #include <X11/extensions/randr.h>
45 #include "miscstruct.h"
46 #include "dixstruct.h"
48 #ifndef XSERVER_LIBPCIACCESS
49 #error "libpciaccess needed"
52 #include <pciaccess.h>
54 #include "pipe/p_context.h"
55 #include "xorg_tracker.h"
56 #include "xorg_winsys.h"
63 * Functions and symbols exported to Xorg via pointers.
66 static Bool drv_pre_init(ScrnInfoPtr pScrn, int flags);
67 static Bool drv_screen_init(int scrnIndex, ScreenPtr pScreen, int argc,
69 static Bool drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags);
70 static void drv_adjust_frame(int scrnIndex, int x, int y, int flags);
71 static Bool drv_enter_vt(int scrnIndex, int flags);
72 static void drv_leave_vt(int scrnIndex, int flags);
73 static void drv_free_screen(int scrnIndex, int flags);
74 static ModeStatus drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose,
81 OPTION_DEBUG_FALLBACK,
84 static const OptionInfoRec drv_options[] = {
85 {OPTION_SW_CURSOR, "SWcursor", OPTV_BOOLEAN, {0}, FALSE},
86 {OPTION_2D_ACCEL, "2DAccel", OPTV_BOOLEAN, {0}, FALSE},
87 {OPTION_DEBUG_FALLBACK, "DebugFallback", OPTV_BOOLEAN, {0}, FALSE},
88 {-1, NULL, OPTV_NONE, {0}, FALSE}
93 * Exported Xorg driver functions to winsys
97 xorg_tracker_available_options(int chipid, int busid)
103 xorg_tracker_set_functions(ScrnInfoPtr scrn)
105 scrn->PreInit = drv_pre_init;
106 scrn->ScreenInit = drv_screen_init;
107 scrn->SwitchMode = drv_switch_mode;
108 scrn->AdjustFrame = drv_adjust_frame;
109 scrn->EnterVT = drv_enter_vt;
110 scrn->LeaveVT = drv_leave_vt;
111 scrn->FreeScreen = drv_free_screen;
112 scrn->ValidMode = drv_valid_mode;
116 xorg_tracker_have_modesetting(ScrnInfoPtr pScrn, struct pci_device *device)
118 char *BusID = xalloc(64);
119 sprintf(BusID, "pci:%04x:%02x:%02x.%d",
120 device->domain, device->bus,
121 device->dev, device->func);
123 if (drmCheckModesettingSupported(BusID)) {
124 xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
125 "Drm modesetting not supported %s\n", BusID);
130 xf86DrvMsgVerb(pScrn->scrnIndex, X_INFO, 0,
131 "Drm modesetting supported on %s\n", BusID);
139 * Internal function definitions
142 static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn);
143 static Bool drv_close_screen(int scrnIndex, ScreenPtr pScreen);
144 static Bool drv_save_hw_state(ScrnInfoPtr pScrn);
145 static Bool drv_restore_hw_state(ScrnInfoPtr pScrn);
153 drv_get_rec(ScrnInfoPtr pScrn)
155 if (pScrn->driverPrivate)
158 pScrn->driverPrivate = xnfcalloc(1, sizeof(modesettingRec));
164 drv_free_rec(ScrnInfoPtr pScrn)
169 if (!pScrn->driverPrivate)
172 xfree(pScrn->driverPrivate);
174 pScrn->driverPrivate = NULL;
178 drv_probe_ddc(ScrnInfoPtr pScrn, int index)
180 ConfiguredMonitor = NULL;
184 drv_crtc_resize(ScrnInfoPtr pScrn, int width, int height)
186 xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
187 modesettingPtr ms = modesettingPTR(pScrn);
188 ScreenPtr pScreen = pScrn->pScreen;
189 int old_width, old_height;
190 PixmapPtr rootPixmap;
193 if (width == pScrn->virtualX && height == pScrn->virtualY)
196 old_width = pScrn->virtualX;
197 old_height = pScrn->virtualY;
198 pScrn->virtualX = width;
199 pScrn->virtualY = height;
201 /* ms->create_front_buffer will remove the old front buffer */
203 rootPixmap = pScreen->GetScreenPixmap(pScreen);
204 if (!pScreen->ModifyPixmapHeader(rootPixmap, width, height, -1, -1, -1, NULL))
207 pScrn->displayWidth = rootPixmap->devKind / (rootPixmap->drawable.bitsPerPixel / 8);
209 if (!ms->create_front_buffer(pScrn) || !ms->bind_front_buffer(pScrn))
213 * create && bind will turn off all crtc(s) in the kernel so we need to
214 * re-enable all the crtcs again. For real HW we might want to do this
215 * before destroying the old framebuffer.
217 for (i = 0; i < xf86_config->num_crtc; i++) {
218 xf86CrtcPtr crtc = xf86_config->crtc[i];
223 crtc->funcs->set_mode_major(crtc, &crtc->mode, crtc->rotation, crtc->x, crtc->y);
229 * This is the error recovery path.
232 if (!pScreen->ModifyPixmapHeader(rootPixmap, old_width, old_height, -1, -1, -1, NULL))
233 FatalError("failed to resize rootPixmap error path\n");
235 pScrn->displayWidth = rootPixmap->devKind / (rootPixmap->drawable.bitsPerPixel / 8);
238 pScrn->virtualX = old_width;
239 pScrn->virtualY = old_height;
241 if (ms->create_front_buffer(pScrn) && ms->bind_front_buffer(pScrn))
244 FatalError("failed to setup old framebuffer\n");
248 static const xf86CrtcConfigFuncsRec crtc_config_funcs = {
249 .resize = drv_crtc_resize
253 drv_init_drm(ScrnInfoPtr pScrn)
255 modesettingPtr ms = modesettingPTR(pScrn);
257 /* deal with server regeneration */
262 sprintf(BusID, "PCI:%d:%d:%d",
263 ((ms->PciInfo->domain << 8) | ms->PciInfo->bus),
264 ms->PciInfo->dev, ms->PciInfo->func
268 ms->api = drm_api_create();
269 ms->fd = drmOpen(ms->api ? ms->api->driver_name : NULL, BusID);
275 if (ms->api && ms->api->destroy)
276 ms->api->destroy(ms->api);
287 drv_close_drm(ScrnInfoPtr pScrn)
289 modesettingPtr ms = modesettingPTR(pScrn);
291 if (ms->api && ms->api->destroy)
292 ms->api->destroy(ms->api);
302 drv_init_resource_management(ScrnInfoPtr pScrn)
304 modesettingPtr ms = modesettingPTR(pScrn);
306 ScreenPtr pScreen = pScrn->pScreen;
307 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
308 Bool fbAccessDisabled;
312 if (ms->screen || ms->kms)
316 ms->screen = ms->api->create_screen(ms->api, ms->fd, NULL);
321 if (ms->api->destroy)
322 ms->api->destroy(ms->api);
328 if (!kms_create(ms->fd, &ms->kms))
336 drv_close_resource_management(ScrnInfoPtr pScrn)
338 modesettingPtr ms = modesettingPTR(pScrn);
342 assert(ms->ctx == NULL);
344 for (i = 0; i < XORG_NR_FENCES; i++) {
346 ms->screen->fence_finish(ms->screen, ms->fence[i], 0);
347 ms->screen->fence_reference(ms->screen, &ms->fence[i], NULL);
350 ms->screen->destroy(ms->screen);
356 kms_destroy(&ms->kms);
363 drv_pre_init(ScrnInfoPtr pScrn, int flags)
365 xf86CrtcConfigPtr xf86_config;
367 rgb defaultWeight = { 0, 0, 0 };
370 int max_width, max_height;
373 if (pScrn->numEntities != 1)
376 pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
378 if (flags & PROBE_DETECT) {
379 drv_probe_ddc(pScrn, pEnt->index);
383 cust = (CustomizerPtr) pScrn->driverPrivate;
384 pScrn->driverPrivate = NULL;
386 /* Allocate driverPrivate */
387 if (!drv_get_rec(pScrn))
390 ms = modesettingPTR(pScrn);
391 ms->SaveGeneration = -1;
395 pScrn->displayWidth = 640; /* default it */
397 if (ms->pEnt->location.type != BUS_PCI)
400 ms->PciInfo = xf86GetPciInfoForEntity(ms->pEnt->index);
402 /* Allocate an entity private if necessary */
403 if (xf86IsEntityShared(pScrn->entityList[0])) {
404 FatalError("Entity");
406 msEnt = xf86GetEntityPrivate(pScrn->entityList[0],
407 modesettingEntityIndex)->ptr;
408 ms->entityPrivate = msEnt;
413 ms->entityPrivate = NULL;
415 if (xf86IsEntityShared(pScrn->entityList[0])) {
416 if (xf86IsPrimInitDone(pScrn->entityList[0])) {
419 xf86SetPrimInitDone(pScrn->entityList[0]);
425 if (!drv_init_drm(pScrn))
428 pScrn->monitor = pScrn->confScreen->monitor;
429 pScrn->progClock = TRUE;
434 PreferConvert24to32 | SupportConvert24to32 | Support32bppFb))
437 switch (pScrn->depth) {
443 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
444 "Given depth (%d) is not supported by the driver\n",
448 xf86PrintDepthBpp(pScrn);
450 if (!xf86SetWeight(pScrn, defaultWeight, defaultWeight))
452 if (!xf86SetDefaultVisual(pScrn, -1))
455 /* Process the options */
456 xf86CollectOptions(pScrn, NULL);
457 if (!(ms->Options = xalloc(sizeof(drv_options))))
459 memcpy(ms->Options, drv_options, sizeof(drv_options));
460 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->Options);
462 /* Allocate an xf86CrtcConfig */
463 xf86CrtcConfigInit(pScrn, &crtc_config_funcs);
464 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
466 max_width = 2048; /* A very low default */
467 max_height = 2048; /* see screen_init */
468 xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, max_height);
470 if (xf86ReturnOptValBool(ms->Options, OPTION_SW_CURSOR, FALSE)) {
474 drv_save_hw_state(pScrn);
476 xorg_crtc_init(pScrn);
477 xorg_output_init(pScrn);
479 if (!xf86InitialConfiguration(pScrn, TRUE)) {
480 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes.\n");
481 drv_restore_hw_state(pScrn);
485 drv_restore_hw_state(pScrn);
488 * If the driver can do gamma correction, it should call xf86SetGamma() here.
491 Gamma zeros = { 0.0, 0.0, 0.0 };
493 if (!xf86SetGamma(pScrn, zeros)) {
498 if (pScrn->modes == NULL) {
499 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
503 pScrn->currentMode = pScrn->modes;
505 /* Set display resolution */
506 xf86SetDpi(pScrn, 0, 0);
508 /* Load the required sub modules */
509 if (!xf86LoadSubModule(pScrn, "fb"))
512 /* XXX: these aren't needed when we are using libkms */
513 if (!xf86LoadSubModule(pScrn, "exa"))
517 if (!xf86LoadSubModule(pScrn, "dri2"))
525 drv_save_hw_state(ScrnInfoPtr pScrn)
527 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
533 drv_restore_hw_state(ScrnInfoPtr pScrn)
535 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
540 static void drv_block_handler(int i, pointer blockData, pointer pTimeout,
543 ScreenPtr pScreen = screenInfo.screens[i];
544 modesettingPtr ms = modesettingPTR(xf86Screens[pScreen->myNum]);
546 pScreen->BlockHandler = ms->blockHandler;
547 pScreen->BlockHandler(i, blockData, pTimeout, pReadmask);
548 pScreen->BlockHandler = drv_block_handler;
553 ms->ctx->flush(ms->ctx, PIPE_FLUSH_RENDER_CACHE, &ms->fence[XORG_NR_FENCES-1]);
556 ms->ctx->screen->fence_finish(ms->ctx->screen, ms->fence[0], 0);
558 /* The amount of rendering generated by a block handler can be
559 * quite small. Let us get a fair way ahead of hardware before
562 for (j = 0; j < XORG_NR_FENCES - 1; j++)
563 ms->screen->fence_reference(ms->screen,
567 ms->screen->fence_reference(ms->screen,
568 &ms->fence[XORG_NR_FENCES-1],
573 #ifdef DRM_MODE_FEATURE_DIRTYFB
575 RegionPtr dirty = DamageRegion(ms->damage);
576 unsigned num_cliprects = REGION_NUM_RECTS(dirty);
579 drmModeClip *clip = alloca(num_cliprects * sizeof(drmModeClip));
580 BoxPtr rect = REGION_RECTS(dirty);
583 /* XXX no need for copy? */
584 for (i = 0; i < num_cliprects; i++, rect++) {
585 clip[i].x1 = rect->x1;
586 clip[i].y1 = rect->y1;
587 clip[i].x2 = rect->x2;
588 clip[i].y2 = rect->y2;
591 /* TODO query connector property to see if this is needed */
592 ret = drmModeDirtyFB(ms->fd, ms->fb_id, clip, num_cliprects);
594 debug_printf("%s: failed to send dirty (%i, %s)\n",
595 __func__, ret, strerror(-ret));
598 DamageEmpty(ms->damage);
605 drv_create_screen_resources(ScreenPtr pScreen)
607 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
608 modesettingPtr ms = modesettingPTR(pScrn);
609 PixmapPtr rootPixmap;
614 pScreen->CreateScreenResources = ms->createScreenResources;
615 ret = pScreen->CreateScreenResources(pScreen);
616 pScreen->CreateScreenResources = drv_create_screen_resources;
618 ms->bind_front_buffer(pScrn);
622 drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
624 #ifdef DRM_MODE_FEATURE_DIRTYFB
625 rootPixmap = pScreen->GetScreenPixmap(pScreen);
626 ms->damage = DamageCreate(NULL, NULL, DamageReportNone, TRUE,
627 pScreen, rootPixmap);
630 DamageRegister(&rootPixmap->drawable, ms->damage);
632 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Damage tracking initialized\n");
634 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
635 "Failed to create screen damage record\n");
646 drv_screen_init(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
648 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
649 modesettingPtr ms = modesettingPTR(pScrn);
650 unsigned max_width, max_height;
652 CustomizerPtr cust = ms->cust;
654 if (!drv_init_drm(pScrn)) {
655 FatalError("Could not init DRM");
659 if (!drv_init_resource_management(pScrn)) {
660 FatalError("Could not init resource management (!pipe_screen && !libkms)");
664 if (!drv_init_front_buffer_functions(pScrn)) {
665 FatalError("Could not init front buffer manager");
669 /* get max width and height */
672 res = drmModeGetResources(ms->fd);
673 max_width = res->max_width;
674 max_height = res->max_height;
675 drmModeFreeResources(res);
680 max = ms->screen->get_param(ms->screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
681 max = 1 << (max - 1);
682 max_width = max < max_width ? max : max_width;
683 max_height = max < max_height ? max : max_height;
686 xf86CrtcSetSizeRange(pScrn, 1, 1, max_width, max_height);
688 pScrn->pScreen = pScreen;
690 /* HW dependent - FIXME */
691 pScrn->displayWidth = pScrn->virtualX;
693 miClearVisualTypes();
695 if (!miSetVisualTypes(pScrn->depth,
696 miGetDefaultVisualMask(pScrn->depth),
697 pScrn->rgbBits, pScrn->defaultVisual))
700 if (!miSetPixmapDepths())
703 pScrn->memPhysBase = 0;
706 if (!fbScreenInit(pScreen, NULL,
707 pScrn->virtualX, pScrn->virtualY,
708 pScrn->xDpi, pScrn->yDpi,
709 pScrn->displayWidth, pScrn->bitsPerPixel))
712 if (pScrn->bitsPerPixel > 8) {
713 /* Fixup RGB ordering */
714 visual = pScreen->visuals + pScreen->numVisuals;
715 while (--visual >= pScreen->visuals) {
716 if ((visual->class | DynamicClass) == DirectColor) {
717 visual->offsetRed = pScrn->offset.red;
718 visual->offsetGreen = pScrn->offset.green;
719 visual->offsetBlue = pScrn->offset.blue;
720 visual->redMask = pScrn->mask.red;
721 visual->greenMask = pScrn->mask.green;
722 visual->blueMask = pScrn->mask.blue;
727 fbPictureInit(pScreen, NULL, 0);
729 ms->blockHandler = pScreen->BlockHandler;
730 pScreen->BlockHandler = drv_block_handler;
731 ms->createScreenResources = pScreen->CreateScreenResources;
732 pScreen->CreateScreenResources = drv_create_screen_resources;
734 xf86SetBlackWhitePixels(pScreen);
736 ms->accelerate_2d = xf86ReturnOptValBool(ms->Options, OPTION_2D_ACCEL, FALSE);
737 ms->debug_fallback = xf86ReturnOptValBool(ms->Options, OPTION_DEBUG_FALLBACK, ms->accelerate_2d);
740 ms->exa = xorg_exa_init(pScrn, ms->accelerate_2d);
742 xorg_xv_init(pScreen);
744 xorg_dri2_init(pScreen);
748 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "##################################\n");
749 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "# Usefull debugging info follows #\n");
750 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "##################################\n");
751 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Using %s backend\n",
752 ms->screen ? "Gallium3D" : "libkms");
753 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "2D Acceleration is %s\n",
754 ms->screen && ms->accelerate_2d ? "enabled" : "disabled");
755 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Fallback debugging is %s\n",
756 ms->debug_fallback ? "enabled" : "disabled");
758 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "3D Acceleration is %s\n",
759 ms->screen ? "enabled" : "disabled");
761 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "3D Acceleration is disabled\n");
763 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "##################################\n");
765 miInitializeBackingStore(pScreen);
766 xf86SetBackingStore(pScreen);
767 xf86SetSilkenMouse(pScreen);
768 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
770 /* Need to extend HWcursor support to handle mask interleave */
772 xf86_cursors_init(pScreen, 64, 64,
773 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 |
774 HARDWARE_CURSOR_ARGB);
776 /* Must force it before EnterVT, so we are in control of VT and
777 * later memory should be bound when allocating, e.g rotate_mem */
778 pScrn->vtSema = TRUE;
780 pScreen->SaveScreen = xf86SaveScreen;
781 ms->CloseScreen = pScreen->CloseScreen;
782 pScreen->CloseScreen = drv_close_screen;
784 if (!xf86CrtcScreenInit(pScreen))
787 if (!miCreateDefColormap(pScreen))
790 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
792 if (serverGeneration == 1)
793 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
795 if (cust && cust->winsys_screen_init)
796 cust->winsys_screen_init(cust, ms->fd);
798 return drv_enter_vt(scrnIndex, 1);
802 drv_adjust_frame(int scrnIndex, int x, int y, int flags)
804 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
805 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
806 xf86OutputPtr output = config->output[config->compat_output];
807 xf86CrtcPtr crtc = output->crtc;
809 if (crtc && crtc->enabled) {
810 crtc->funcs->set_mode_major(crtc, pScrn->currentMode,
812 crtc->x = output->initial_x + x;
813 crtc->y = output->initial_y + y;
818 drv_free_screen(int scrnIndex, int flags)
820 drv_free_rec(xf86Screens[scrnIndex]);
824 drv_leave_vt(int scrnIndex, int flags)
826 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
827 modesettingPtr ms = modesettingPTR(pScrn);
828 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
829 CustomizerPtr cust = ms->cust;
832 if (cust && cust->winsys_leave_vt)
833 cust->winsys_leave_vt(cust);
835 for (o = 0; o < config->num_crtc; o++) {
836 xf86CrtcPtr crtc = config->crtc[o];
838 xorg_crtc_cursor_destroy(crtc);
840 if (crtc->rotatedPixmap || crtc->rotatedData) {
841 crtc->funcs->shadow_destroy(crtc, crtc->rotatedPixmap,
843 crtc->rotatedPixmap = NULL;
844 crtc->rotatedData = NULL;
848 drmModeRmFB(ms->fd, ms->fb_id);
851 drv_restore_hw_state(pScrn);
853 if (drmDropMaster(ms->fd))
854 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
855 "drmDropMaster failed: %s\n", strerror(errno));
857 pScrn->vtSema = FALSE;
861 * This gets called when gaining control of the VT, and from ScreenInit().
864 drv_enter_vt(int scrnIndex, int flags)
866 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
867 modesettingPtr ms = modesettingPTR(pScrn);
868 CustomizerPtr cust = ms->cust;
870 if (drmSetMaster(ms->fd)) {
871 if (errno == EINVAL) {
872 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
873 "drmSetMaster failed: 2.6.29 or newer kernel required for "
874 "multi-server DRI\n");
876 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
877 "drmSetMaster failed: %s\n", strerror(errno));
882 * Only save state once per server generation since that's what most
883 * drivers do. Could change this to save state at each VT enter.
885 if (ms->SaveGeneration != serverGeneration) {
886 ms->SaveGeneration = serverGeneration;
887 drv_save_hw_state(pScrn);
890 if (!ms->create_front_buffer(pScrn))
893 if (!flags && !ms->bind_front_buffer(pScrn))
896 if (!xf86SetDesiredModes(pScrn))
899 if (cust && cust->winsys_enter_vt)
900 cust->winsys_enter_vt(cust);
906 drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags)
908 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
910 return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
914 drv_close_screen(int scrnIndex, ScreenPtr pScreen)
916 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
917 modesettingPtr ms = modesettingPTR(pScrn);
918 CustomizerPtr cust = ms->cust;
921 drv_leave_vt(scrnIndex, 0);
925 FreeCursor(ms->cursor, None);
929 if (cust && cust->winsys_screen_close)
930 cust->winsys_screen_close(cust);
934 xorg_dri2_close(pScreen);
937 pScreen->BlockHandler = ms->blockHandler;
938 pScreen->CreateScreenResources = ms->createScreenResources;
940 #ifdef DRM_MODE_FEATURE_DIRTYFB
942 DamageUnregister(&pScreen->GetScreenPixmap(pScreen)->drawable, ms->damage);
943 DamageDestroy(ms->damage);
948 drmModeRmFB(ms->fd, ms->fb_id);
949 ms->destroy_front_buffer(pScrn);
952 xorg_exa_close(pScrn);
955 drv_close_resource_management(pScrn);
957 drv_close_drm(pScrn);
959 pScrn->vtSema = FALSE;
960 pScreen->CloseScreen = ms->CloseScreen;
961 return (*pScreen->CloseScreen) (scrnIndex, pScreen);
965 drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
972 * Front buffer backing store functions.
976 drv_destroy_front_buffer_ga3d(ScrnInfoPtr pScrn)
978 modesettingPtr ms = modesettingPTR(pScrn);
980 if (!ms->root_texture)
983 if (ms->fb_id != -1) {
984 drmModeRmFB(ms->fd, ms->fb_id);
988 pipe_texture_reference(&ms->root_texture, NULL);
993 drv_create_front_buffer_ga3d(ScrnInfoPtr pScrn)
995 modesettingPtr ms = modesettingPTR(pScrn);
996 struct pipe_texture *tex;
997 struct winsys_handle whandle;
1003 tex = xorg_exa_create_root_texture(pScrn, pScrn->virtualX, pScrn->virtualY,
1004 pScrn->depth, pScrn->bitsPerPixel);
1009 memset(&whandle, 0, sizeof(whandle));
1010 whandle.type = DRM_API_HANDLE_TYPE_KMS;
1012 if (!ms->screen->texture_get_handle(ms->screen, tex, &whandle))
1015 ret = drmModeAddFB(ms->fd,
1019 pScrn->bitsPerPixel,
1024 debug_printf("%s: failed to create framebuffer (%i, %s)\n",
1025 __func__, ret, strerror(-ret));
1029 if (!drv_destroy_front_buffer_ga3d(pScrn))
1030 FatalError("%s: failed to take down old framebuffer\n", __func__);
1034 drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
1036 pipe_texture_reference(&ms->root_texture, tex);
1037 pipe_texture_reference(&tex, NULL);
1043 pipe_texture_reference(&tex, NULL);
1048 drv_bind_front_buffer_ga3d(ScrnInfoPtr pScrn)
1050 modesettingPtr ms = modesettingPTR(pScrn);
1051 ScreenPtr pScreen = pScrn->pScreen;
1052 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
1053 struct pipe_texture *check;
1055 xorg_exa_set_displayed_usage(rootPixmap);
1056 xorg_exa_set_shared_usage(rootPixmap);
1057 xorg_exa_set_texture(rootPixmap, ms->root_texture);
1058 if (!pScreen->ModifyPixmapHeader(rootPixmap, -1, -1, -1, -1, -1, NULL))
1059 FatalError("Couldn't adjust screen pixmap\n");
1061 check = xorg_exa_get_texture(rootPixmap);
1062 if (ms->root_texture != check)
1063 FatalError("Created new root texture\n");
1065 pipe_texture_reference(&check, NULL);
1071 drv_destroy_front_buffer_kms(ScrnInfoPtr pScrn)
1073 modesettingPtr ms = modesettingPTR(pScrn);
1074 ScreenPtr pScreen = pScrn->pScreen;
1075 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
1077 /* XXX Do something with the rootPixmap.
1078 * This currently works fine but if we are getting crashes in
1079 * the fb functions after VT switches maybe look more into it.
1086 if (ms->fb_id != -1) {
1087 drmModeRmFB(ms->fd, ms->fb_id);
1091 kms_bo_unmap(ms->root_bo);
1092 kms_bo_destroy(&ms->root_bo);
1097 drv_create_front_buffer_kms(ScrnInfoPtr pScrn)
1099 modesettingPtr ms = modesettingPTR(pScrn);
1100 unsigned handle, stride;
1106 attr[0] = KMS_BO_TYPE;
1107 #ifdef KMS_BO_TYPE_SCANOUT_X8R8G8B8
1108 attr[1] = KMS_BO_TYPE_SCANOUT_X8R8G8B8;
1110 attr[1] = KMS_BO_TYPE_SCANOUT;
1112 attr[2] = KMS_WIDTH;
1113 attr[3] = pScrn->virtualX;
1114 attr[4] = KMS_HEIGHT;
1115 attr[5] = pScrn->virtualY;
1118 if (kms_bo_create(ms->kms, attr, &bo))
1121 if (kms_bo_get_prop(bo, KMS_PITCH, &stride))
1124 if (kms_bo_get_prop(bo, KMS_HANDLE, &handle))
1127 ret = drmModeAddFB(ms->fd,
1131 pScrn->bitsPerPixel,
1136 debug_printf("%s: failed to create framebuffer (%i, %s)",
1137 __func__, ret, strerror(-ret));
1141 if (!drv_destroy_front_buffer_kms(pScrn))
1142 FatalError("%s: could not takedown old bo", __func__);
1146 drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
1153 kms_bo_destroy(&bo);
1158 drv_bind_front_buffer_kms(ScrnInfoPtr pScrn)
1160 modesettingPtr ms = modesettingPTR(pScrn);
1161 ScreenPtr pScreen = pScrn->pScreen;
1162 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
1166 if (kms_bo_get_prop(ms->root_bo, KMS_PITCH, &stride))
1169 if (kms_bo_map(ms->root_bo, &ptr))
1172 pScreen->ModifyPixmapHeader(rootPixmap,
1176 pScrn->bitsPerPixel,
1180 /* This a hack to work around EnableDisableFBAccess setting the pointer
1181 * the real fix would be to replace pScrn->EnableDisableFBAccess hook
1182 * and set the rootPixmap->devPrivate.ptr to something valid before that.
1184 * But in its infinit visdome something uses either this some times before
1185 * that, so our hook doesn't get called before the crash happens.
1187 pScrn->pixmapPrivate.ptr = ptr;
1192 kms_bo_destroy(&ms->root_bo);
1195 #endif /* HAVE_LIBKMS */
1197 static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn)
1199 modesettingPtr ms = modesettingPTR(pScrn);
1201 ms->destroy_front_buffer = drv_destroy_front_buffer_ga3d;
1202 ms->create_front_buffer = drv_create_front_buffer_ga3d;
1203 ms->bind_front_buffer = drv_bind_front_buffer_ga3d;
1205 } else if (ms->kms) {
1206 ms->destroy_front_buffer = drv_destroy_front_buffer_kms;
1207 ms->create_front_buffer = drv_create_front_buffer_kms;
1208 ms->bind_front_buffer = drv_bind_front_buffer_kms;
1216 CustomizerPtr xorg_customizer(ScrnInfoPtr pScrn)
1218 return modesettingPTR(pScrn)->cust;
1221 Bool xorg_has_gallium(ScrnInfoPtr pScrn)
1223 return modesettingPTR(pScrn)->screen != NULL;
1226 /* vim: set sw=4 ts=8 sts=4: */