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 modesettingPtr ms
= modesettingPTR(pScrn
);
187 PixmapPtr rootPixmap
;
188 ScreenPtr pScreen
= pScrn
->pScreen
;
190 if (width
== pScrn
->virtualX
&& height
== pScrn
->virtualY
)
193 pScrn
->virtualX
= width
;
194 pScrn
->virtualY
= height
;
197 * Remove the old framebuffer & texture.
199 drmModeRmFB(ms
->fd
, ms
->fb_id
);
200 if (!ms
->destroy_front_buffer(pScrn
))
201 FatalError("failed to destroy front buffer\n");
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 /* now create new frontbuffer */
210 return ms
->create_front_buffer(pScrn
) && ms
->bind_front_buffer(pScrn
);
213 static const xf86CrtcConfigFuncsRec crtc_config_funcs
= {
214 .resize
= drv_crtc_resize
218 drv_init_drm(ScrnInfoPtr pScrn
)
220 modesettingPtr ms
= modesettingPTR(pScrn
);
222 /* deal with server regeneration */
227 sprintf(BusID
, "PCI:%d:%d:%d",
228 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
229 ms
->PciInfo
->dev
, ms
->PciInfo
->func
233 ms
->api
= drm_api_create();
234 ms
->fd
= drmOpen(ms
->api
? ms
->api
->driver_name
: NULL
, BusID
);
240 if (ms
->api
&& ms
->api
->destroy
)
241 ms
->api
->destroy(ms
->api
);
252 drv_close_drm(ScrnInfoPtr pScrn
)
254 modesettingPtr ms
= modesettingPTR(pScrn
);
256 if (ms
->api
&& ms
->api
->destroy
)
257 ms
->api
->destroy(ms
->api
);
267 drv_init_resource_management(ScrnInfoPtr pScrn
)
269 modesettingPtr ms
= modesettingPTR(pScrn
);
271 ScreenPtr pScreen = pScrn->pScreen;
272 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
273 Bool fbAccessDisabled;
277 if (ms
->screen
|| ms
->kms
)
281 ms
->screen
= ms
->api
->create_screen(ms
->api
, ms
->fd
, NULL
);
286 if (ms
->api
->destroy
)
287 ms
->api
->destroy(ms
->api
);
293 if (!kms_create(ms
->fd
, &ms
->kms
))
301 drv_close_resource_management(ScrnInfoPtr pScrn
)
303 modesettingPtr ms
= modesettingPTR(pScrn
);
307 assert(ms
->ctx
== NULL
);
309 for (i
= 0; i
< XORG_NR_FENCES
; i
++) {
311 ms
->screen
->fence_finish(ms
->screen
, ms
->fence
[i
], 0);
312 ms
->screen
->fence_reference(ms
->screen
, &ms
->fence
[i
], NULL
);
315 ms
->screen
->destroy(ms
->screen
);
321 kms_destroy(&ms
->kms
);
328 drv_pre_init(ScrnInfoPtr pScrn
, int flags
)
330 xf86CrtcConfigPtr xf86_config
;
332 rgb defaultWeight
= { 0, 0, 0 };
335 int max_width
, max_height
;
338 if (pScrn
->numEntities
!= 1)
341 pEnt
= xf86GetEntityInfo(pScrn
->entityList
[0]);
343 if (flags
& PROBE_DETECT
) {
344 drv_probe_ddc(pScrn
, pEnt
->index
);
348 cust
= (CustomizerPtr
) pScrn
->driverPrivate
;
349 pScrn
->driverPrivate
= NULL
;
351 /* Allocate driverPrivate */
352 if (!drv_get_rec(pScrn
))
355 ms
= modesettingPTR(pScrn
);
356 ms
->SaveGeneration
= -1;
360 pScrn
->displayWidth
= 640; /* default it */
362 if (ms
->pEnt
->location
.type
!= BUS_PCI
)
365 ms
->PciInfo
= xf86GetPciInfoForEntity(ms
->pEnt
->index
);
367 /* Allocate an entity private if necessary */
368 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
369 FatalError("Entity");
371 msEnt
= xf86GetEntityPrivate(pScrn
->entityList
[0],
372 modesettingEntityIndex
)->ptr
;
373 ms
->entityPrivate
= msEnt
;
378 ms
->entityPrivate
= NULL
;
380 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
381 if (xf86IsPrimInitDone(pScrn
->entityList
[0])) {
384 xf86SetPrimInitDone(pScrn
->entityList
[0]);
390 if (!drv_init_drm(pScrn
))
393 pScrn
->monitor
= pScrn
->confScreen
->monitor
;
394 pScrn
->progClock
= TRUE
;
399 PreferConvert24to32
| SupportConvert24to32
| Support32bppFb
))
402 switch (pScrn
->depth
) {
408 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
409 "Given depth (%d) is not supported by the driver\n",
413 xf86PrintDepthBpp(pScrn
);
415 if (!xf86SetWeight(pScrn
, defaultWeight
, defaultWeight
))
417 if (!xf86SetDefaultVisual(pScrn
, -1))
420 /* Process the options */
421 xf86CollectOptions(pScrn
, NULL
);
422 if (!(ms
->Options
= xalloc(sizeof(drv_options
))))
424 memcpy(ms
->Options
, drv_options
, sizeof(drv_options
));
425 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, ms
->Options
);
427 /* Allocate an xf86CrtcConfig */
428 xf86CrtcConfigInit(pScrn
, &crtc_config_funcs
);
429 xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
433 xf86CrtcSetSizeRange(pScrn
, 320, 200, max_width
, max_height
);
435 if (xf86ReturnOptValBool(ms
->Options
, OPTION_SW_CURSOR
, FALSE
)) {
439 drv_save_hw_state(pScrn
);
441 xorg_crtc_init(pScrn
);
442 xorg_output_init(pScrn
);
444 if (!xf86InitialConfiguration(pScrn
, TRUE
)) {
445 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No valid modes.\n");
446 drv_restore_hw_state(pScrn
);
450 drv_restore_hw_state(pScrn
);
453 * If the driver can do gamma correction, it should call xf86SetGamma() here.
456 Gamma zeros
= { 0.0, 0.0, 0.0 };
458 if (!xf86SetGamma(pScrn
, zeros
)) {
463 if (pScrn
->modes
== NULL
) {
464 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No modes.\n");
468 pScrn
->currentMode
= pScrn
->modes
;
470 /* Set display resolution */
471 xf86SetDpi(pScrn
, 0, 0);
473 /* Load the required sub modules */
474 if (!xf86LoadSubModule(pScrn
, "fb"))
477 /* XXX: these aren't needed when we are using libkms */
478 if (!xf86LoadSubModule(pScrn
, "exa"))
482 if (!xf86LoadSubModule(pScrn
, "dri2"))
490 drv_save_hw_state(ScrnInfoPtr pScrn
)
492 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
498 drv_restore_hw_state(ScrnInfoPtr pScrn
)
500 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
505 static void drv_block_handler(int i
, pointer blockData
, pointer pTimeout
,
508 ScreenPtr pScreen
= screenInfo
.screens
[i
];
509 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
511 pScreen
->BlockHandler
= ms
->blockHandler
;
512 pScreen
->BlockHandler(i
, blockData
, pTimeout
, pReadmask
);
513 pScreen
->BlockHandler
= drv_block_handler
;
518 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_RENDER_CACHE
, &ms
->fence
[XORG_NR_FENCES
-1]);
521 ms
->ctx
->screen
->fence_finish(ms
->ctx
->screen
, ms
->fence
[0], 0);
523 /* The amount of rendering generated by a block handler can be
524 * quite small. Let us get a fair way ahead of hardware before
527 for (j
= 0; j
< XORG_NR_FENCES
- 1; j
++)
528 ms
->screen
->fence_reference(ms
->screen
,
532 ms
->screen
->fence_reference(ms
->screen
,
533 &ms
->fence
[XORG_NR_FENCES
-1],
538 #ifdef DRM_MODE_FEATURE_DIRTYFB
540 RegionPtr dirty
= DamageRegion(ms
->damage
);
541 unsigned num_cliprects
= REGION_NUM_RECTS(dirty
);
544 drmModeClip
*clip
= alloca(num_cliprects
* sizeof(drmModeClip
));
545 BoxPtr rect
= REGION_RECTS(dirty
);
548 /* XXX no need for copy? */
549 for (i
= 0; i
< num_cliprects
; i
++, rect
++) {
550 clip
[i
].x1
= rect
->x1
;
551 clip
[i
].y1
= rect
->y1
;
552 clip
[i
].x2
= rect
->x2
;
553 clip
[i
].y2
= rect
->y2
;
556 /* TODO query connector property to see if this is needed */
557 ret
= drmModeDirtyFB(ms
->fd
, ms
->fb_id
, clip
, num_cliprects
);
559 debug_printf("%s: failed to send dirty (%i, %s)\n",
560 __func__
, ret
, strerror(-ret
));
563 DamageEmpty(ms
->damage
);
570 drv_create_screen_resources(ScreenPtr pScreen
)
572 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
573 modesettingPtr ms
= modesettingPTR(pScrn
);
574 PixmapPtr rootPixmap
;
579 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
580 ret
= pScreen
->CreateScreenResources(pScreen
);
581 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
583 ms
->bind_front_buffer(pScrn
);
587 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
589 #ifdef DRM_MODE_FEATURE_DIRTYFB
590 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
591 ms
->damage
= DamageCreate(NULL
, NULL
, DamageReportNone
, TRUE
,
592 pScreen
, rootPixmap
);
595 DamageRegister(&rootPixmap
->drawable
, ms
->damage
);
597 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Damage tracking initialized\n");
599 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
600 "Failed to create screen damage record\n");
611 drv_screen_init(int scrnIndex
, ScreenPtr pScreen
, int argc
, char **argv
)
613 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
614 modesettingPtr ms
= modesettingPTR(pScrn
);
616 CustomizerPtr cust
= ms
->cust
;
618 if (!drv_init_drm(pScrn
)) {
619 FatalError("Could not init DRM");
623 if (!drv_init_resource_management(pScrn
)) {
624 FatalError("Could not init resource management (!pipe_screen && !libkms)");
628 if (!drv_init_front_buffer_functions(pScrn
)) {
629 FatalError("Could not init front buffer manager");
633 pScrn
->pScreen
= pScreen
;
635 /* HW dependent - FIXME */
636 pScrn
->displayWidth
= pScrn
->virtualX
;
638 miClearVisualTypes();
640 if (!miSetVisualTypes(pScrn
->depth
,
641 miGetDefaultVisualMask(pScrn
->depth
),
642 pScrn
->rgbBits
, pScrn
->defaultVisual
))
645 if (!miSetPixmapDepths())
648 pScrn
->memPhysBase
= 0;
651 if (!fbScreenInit(pScreen
, NULL
,
652 pScrn
->virtualX
, pScrn
->virtualY
,
653 pScrn
->xDpi
, pScrn
->yDpi
,
654 pScrn
->displayWidth
, pScrn
->bitsPerPixel
))
657 if (pScrn
->bitsPerPixel
> 8) {
658 /* Fixup RGB ordering */
659 visual
= pScreen
->visuals
+ pScreen
->numVisuals
;
660 while (--visual
>= pScreen
->visuals
) {
661 if ((visual
->class | DynamicClass
) == DirectColor
) {
662 visual
->offsetRed
= pScrn
->offset
.red
;
663 visual
->offsetGreen
= pScrn
->offset
.green
;
664 visual
->offsetBlue
= pScrn
->offset
.blue
;
665 visual
->redMask
= pScrn
->mask
.red
;
666 visual
->greenMask
= pScrn
->mask
.green
;
667 visual
->blueMask
= pScrn
->mask
.blue
;
672 fbPictureInit(pScreen
, NULL
, 0);
674 ms
->blockHandler
= pScreen
->BlockHandler
;
675 pScreen
->BlockHandler
= drv_block_handler
;
676 ms
->createScreenResources
= pScreen
->CreateScreenResources
;
677 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
679 xf86SetBlackWhitePixels(pScreen
);
681 ms
->accelerate_2d
= xf86ReturnOptValBool(ms
->Options
, OPTION_2D_ACCEL
, FALSE
);
682 ms
->debug_fallback
= xf86ReturnOptValBool(ms
->Options
, OPTION_DEBUG_FALLBACK
, ms
->accelerate_2d
);
685 ms
->exa
= xorg_exa_init(pScrn
, ms
->accelerate_2d
);
687 xorg_xv_init(pScreen
);
689 xorg_dri2_init(pScreen
);
693 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
694 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "# Usefull debugging info follows #\n");
695 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
696 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Using %s backend\n",
697 ms
->screen
? "Gallium3D" : "libkms");
698 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "2D Acceleration is %s\n",
699 ms
->screen
&& ms
->accelerate_2d
? "enabled" : "disabled");
700 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Fallback debugging is %s\n",
701 ms
->debug_fallback
? "enabled" : "disabled");
703 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "3D Acceleration is %s\n",
704 ms
->screen
? "enabled" : "disabled");
706 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "3D Acceleration is disabled\n");
708 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
710 miInitializeBackingStore(pScreen
);
711 xf86SetBackingStore(pScreen
);
712 xf86SetSilkenMouse(pScreen
);
713 miDCInitialize(pScreen
, xf86GetPointerScreenFuncs());
715 /* Need to extend HWcursor support to handle mask interleave */
717 xf86_cursors_init(pScreen
, 64, 64,
718 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64
|
719 HARDWARE_CURSOR_ARGB
);
721 /* Must force it before EnterVT, so we are in control of VT and
722 * later memory should be bound when allocating, e.g rotate_mem */
723 pScrn
->vtSema
= TRUE
;
725 pScreen
->SaveScreen
= xf86SaveScreen
;
726 ms
->CloseScreen
= pScreen
->CloseScreen
;
727 pScreen
->CloseScreen
= drv_close_screen
;
729 if (!xf86CrtcScreenInit(pScreen
))
732 if (!miCreateDefColormap(pScreen
))
735 xf86DPMSInit(pScreen
, xf86DPMSSet
, 0);
737 if (serverGeneration
== 1)
738 xf86ShowUnusedOptions(pScrn
->scrnIndex
, pScrn
->options
);
740 if (cust
&& cust
->winsys_screen_init
)
741 cust
->winsys_screen_init(cust
, ms
->fd
);
743 return drv_enter_vt(scrnIndex
, 1);
747 drv_adjust_frame(int scrnIndex
, int x
, int y
, int flags
)
749 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
750 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
751 xf86OutputPtr output
= config
->output
[config
->compat_output
];
752 xf86CrtcPtr crtc
= output
->crtc
;
754 if (crtc
&& crtc
->enabled
) {
755 crtc
->funcs
->set_mode_major(crtc
, pScrn
->currentMode
,
757 crtc
->x
= output
->initial_x
+ x
;
758 crtc
->y
= output
->initial_y
+ y
;
763 drv_free_screen(int scrnIndex
, int flags
)
765 drv_free_rec(xf86Screens
[scrnIndex
]);
769 drv_leave_vt(int scrnIndex
, int flags
)
771 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
772 modesettingPtr ms
= modesettingPTR(pScrn
);
773 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
774 CustomizerPtr cust
= ms
->cust
;
777 if (cust
&& cust
->winsys_leave_vt
)
778 cust
->winsys_leave_vt(cust
);
780 for (o
= 0; o
< config
->num_crtc
; o
++) {
781 xf86CrtcPtr crtc
= config
->crtc
[o
];
783 xorg_crtc_cursor_destroy(crtc
);
785 if (crtc
->rotatedPixmap
|| crtc
->rotatedData
) {
786 crtc
->funcs
->shadow_destroy(crtc
, crtc
->rotatedPixmap
,
788 crtc
->rotatedPixmap
= NULL
;
789 crtc
->rotatedData
= NULL
;
793 drmModeRmFB(ms
->fd
, ms
->fb_id
);
795 drv_restore_hw_state(pScrn
);
797 if (drmDropMaster(ms
->fd
))
798 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
799 "drmDropMaster failed: %s\n", strerror(errno
));
801 pScrn
->vtSema
= FALSE
;
805 * This gets called when gaining control of the VT, and from ScreenInit().
808 drv_enter_vt(int scrnIndex
, int flags
)
810 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
811 modesettingPtr ms
= modesettingPTR(pScrn
);
812 CustomizerPtr cust
= ms
->cust
;
814 if (drmSetMaster(ms
->fd
)) {
815 if (errno
== EINVAL
) {
816 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
817 "drmSetMaster failed: 2.6.29 or newer kernel required for "
818 "multi-server DRI\n");
820 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
821 "drmSetMaster failed: %s\n", strerror(errno
));
826 * Only save state once per server generation since that's what most
827 * drivers do. Could change this to save state at each VT enter.
829 if (ms
->SaveGeneration
!= serverGeneration
) {
830 ms
->SaveGeneration
= serverGeneration
;
831 drv_save_hw_state(pScrn
);
834 if (!ms
->create_front_buffer(pScrn
))
837 if (!flags
&& !ms
->bind_front_buffer(pScrn
))
840 if (!xf86SetDesiredModes(pScrn
))
843 if (cust
&& cust
->winsys_enter_vt
)
844 cust
->winsys_enter_vt(cust
);
850 drv_switch_mode(int scrnIndex
, DisplayModePtr mode
, int flags
)
852 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
854 return xf86SetSingleMode(pScrn
, mode
, RR_Rotate_0
);
858 drv_close_screen(int scrnIndex
, ScreenPtr pScreen
)
860 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
861 modesettingPtr ms
= modesettingPTR(pScrn
);
862 CustomizerPtr cust
= ms
->cust
;
865 drv_leave_vt(scrnIndex
, 0);
868 if (cust
&& cust
->winsys_screen_close
)
869 cust
->winsys_screen_close(cust
);
873 xorg_dri2_close(pScreen
);
876 pScreen
->BlockHandler
= ms
->blockHandler
;
877 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
879 #ifdef DRM_MODE_FEATURE_DIRTYFB
881 DamageUnregister(&pScreen
->GetScreenPixmap(pScreen
)->drawable
, ms
->damage
);
882 DamageDestroy(ms
->damage
);
887 drmModeRmFB(ms
->fd
, ms
->fb_id
);
888 ms
->destroy_front_buffer(pScrn
);
891 xorg_exa_close(pScrn
);
894 drv_close_resource_management(pScrn
);
896 drv_close_drm(pScrn
);
898 pScrn
->vtSema
= FALSE
;
899 pScreen
->CloseScreen
= ms
->CloseScreen
;
900 return (*pScreen
->CloseScreen
) (scrnIndex
, pScreen
);
904 drv_valid_mode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
, int flags
)
911 * Front buffer backing store functions.
915 drv_destroy_front_buffer_ga3d(ScrnInfoPtr pScrn
)
917 modesettingPtr ms
= modesettingPTR(pScrn
);
918 pipe_texture_reference(&ms
->root_texture
, NULL
);
923 drv_create_front_buffer_ga3d(ScrnInfoPtr pScrn
)
925 modesettingPtr ms
= modesettingPTR(pScrn
);
926 unsigned handle
, stride
;
927 struct pipe_texture
*tex
;
932 tex
= xorg_exa_create_root_texture(pScrn
, pScrn
->virtualX
, pScrn
->virtualY
,
933 pScrn
->depth
, pScrn
->bitsPerPixel
);
938 if (!ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
,
944 ret
= drmModeAddFB(ms
->fd
,
953 debug_printf("%s: failed to create framebuffer (%i, %s)",
954 __func__
, ret
, strerror(-ret
));
960 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
962 pipe_texture_reference(&ms
->root_texture
, tex
);
963 pipe_texture_reference(&tex
, NULL
);
968 pipe_texture_reference(&tex
, NULL
);
973 drv_bind_front_buffer_ga3d(ScrnInfoPtr pScrn
)
975 modesettingPtr ms
= modesettingPTR(pScrn
);
976 ScreenPtr pScreen
= pScrn
->pScreen
;
977 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
978 struct pipe_texture
*check
;
980 xorg_exa_set_displayed_usage(rootPixmap
);
981 xorg_exa_set_shared_usage(rootPixmap
);
982 xorg_exa_set_texture(rootPixmap
, ms
->root_texture
);
983 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, -1, -1, -1, -1, -1, NULL
))
984 FatalError("Couldn't adjust screen pixmap\n");
986 check
= xorg_exa_get_texture(rootPixmap
);
987 if (ms
->root_texture
!= check
)
988 FatalError("Created new root texture\n");
990 pipe_texture_reference(&check
, NULL
);
996 drv_destroy_front_buffer_kms(ScrnInfoPtr pScrn
)
998 modesettingPtr ms
= modesettingPTR(pScrn
);
999 ScreenPtr pScreen
= pScrn
->pScreen
;
1000 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1002 /* XXX Do something with the rootPixmap.
1003 * This currently works fine but if we are getting crashes in
1004 * the fb functions after VT switches maybe look more into it.
1011 kms_bo_unmap(ms
->root_bo
);
1012 kms_bo_destroy(&ms
->root_bo
);
1017 drv_create_front_buffer_kms(ScrnInfoPtr pScrn
)
1019 modesettingPtr ms
= modesettingPTR(pScrn
);
1020 unsigned handle
, stride
;
1025 attr
[0] = KMS_BO_TYPE
;
1026 #ifdef KMS_BO_TYPE_SCANOUT_X8R8G8B8
1027 attr
[1] = KMS_BO_TYPE_SCANOUT_X8R8G8B8
;
1029 attr
[1] = KMS_BO_TYPE_SCANOUT
;
1031 attr
[2] = KMS_WIDTH
;
1032 attr
[3] = pScrn
->virtualX
;
1033 attr
[4] = KMS_HEIGHT
;
1034 attr
[5] = pScrn
->virtualY
;
1037 if (kms_bo_create(ms
->kms
, attr
, &bo
))
1040 if (kms_bo_get_prop(bo
, KMS_PITCH
, &stride
))
1043 if (kms_bo_get_prop(bo
, KMS_HANDLE
, &handle
))
1046 ret
= drmModeAddFB(ms
->fd
,
1050 pScrn
->bitsPerPixel
,
1055 debug_printf("%s: failed to create framebuffer (%i, %s)",
1056 __func__
, ret
, strerror(-ret
));
1062 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
1068 kms_bo_destroy(&bo
);
1073 drv_bind_front_buffer_kms(ScrnInfoPtr pScrn
)
1075 modesettingPtr ms
= modesettingPTR(pScrn
);
1076 ScreenPtr pScreen
= pScrn
->pScreen
;
1077 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1081 if (kms_bo_get_prop(ms
->root_bo
, KMS_PITCH
, &stride
))
1084 if (kms_bo_map(ms
->root_bo
, &ptr
))
1087 pScreen
->ModifyPixmapHeader(rootPixmap
,
1091 pScrn
->bitsPerPixel
,
1095 /* This a hack to work around EnableDisableFBAccess setting the pointer
1096 * the real fix would be to replace pScrn->EnableDisableFBAccess hook
1097 * and set the rootPixmap->devPrivate.ptr to something valid before that.
1099 * But in its infinit visdome something uses either this some times before
1100 * that, so our hook doesn't get called before the crash happens.
1102 pScrn
->pixmapPrivate
.ptr
= ptr
;
1107 kms_bo_destroy(&ms
->root_bo
);
1110 #endif /* HAVE_LIBKMS */
1112 static Bool
drv_init_front_buffer_functions(ScrnInfoPtr pScrn
)
1114 modesettingPtr ms
= modesettingPTR(pScrn
);
1116 ms
->destroy_front_buffer
= drv_destroy_front_buffer_ga3d
;
1117 ms
->create_front_buffer
= drv_create_front_buffer_ga3d
;
1118 ms
->bind_front_buffer
= drv_bind_front_buffer_ga3d
;
1120 } else if (ms
->kms
) {
1121 ms
->destroy_front_buffer
= drv_destroy_front_buffer_kms
;
1122 ms
->create_front_buffer
= drv_create_front_buffer_kms
;
1123 ms
->bind_front_buffer
= drv_bind_front_buffer_kms
;
1131 CustomizerPtr
xorg_customizer(ScrnInfoPtr pScrn
)
1133 return modesettingPTR(pScrn
)->cust
;
1136 Bool
xorg_has_gallium(ScrnInfoPtr pScrn
)
1138 return modesettingPTR(pScrn
)->screen
!= NULL
;
1141 /* vim: set sw=4 ts=8 sts=4: */