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
,
83 OPTION_THROTTLE_DIRTY
,
87 static const OptionInfoRec drv_options
[] = {
88 {OPTION_SW_CURSOR
, "SWcursor", OPTV_BOOLEAN
, {0}, FALSE
},
89 {OPTION_2D_ACCEL
, "2DAccel", OPTV_BOOLEAN
, {0}, FALSE
},
90 {OPTION_DEBUG_FALLBACK
, "DebugFallback", OPTV_BOOLEAN
, {0}, FALSE
},
91 {OPTION_THROTTLE_SWAP
, "SwapThrottling", OPTV_BOOLEAN
, {0}, FALSE
},
92 {OPTION_THROTTLE_DIRTY
, "DirtyThrottling", OPTV_BOOLEAN
, {0}, FALSE
},
93 {OPTION_3D_ACCEL
, "3DAccel", OPTV_BOOLEAN
, {0}, FALSE
},
94 {-1, NULL
, OPTV_NONE
, {0}, FALSE
}
99 * Exported Xorg driver functions to winsys
102 const OptionInfoRec
*
103 xorg_tracker_available_options(int chipid
, int busid
)
109 xorg_tracker_set_functions(ScrnInfoPtr scrn
)
111 scrn
->PreInit
= drv_pre_init
;
112 scrn
->ScreenInit
= drv_screen_init
;
113 scrn
->SwitchMode
= drv_switch_mode
;
114 scrn
->AdjustFrame
= drv_adjust_frame
;
115 scrn
->EnterVT
= drv_enter_vt
;
116 scrn
->LeaveVT
= drv_leave_vt
;
117 scrn
->FreeScreen
= drv_free_screen
;
118 scrn
->ValidMode
= drv_valid_mode
;
122 xorg_tracker_have_modesetting(ScrnInfoPtr pScrn
, struct pci_device
*device
)
124 char *BusID
= xalloc(64);
125 sprintf(BusID
, "pci:%04x:%02x:%02x.%d",
126 device
->domain
, device
->bus
,
127 device
->dev
, device
->func
);
129 if (drmCheckModesettingSupported(BusID
)) {
130 xf86DrvMsgVerb(pScrn
->scrnIndex
, X_INFO
, 0,
131 "Drm modesetting not supported %s\n", BusID
);
136 xf86DrvMsgVerb(pScrn
->scrnIndex
, X_INFO
, 0,
137 "Drm modesetting supported on %s\n", BusID
);
145 * Internal function definitions
148 static Bool
drv_init_front_buffer_functions(ScrnInfoPtr pScrn
);
149 static Bool
drv_close_screen(int scrnIndex
, ScreenPtr pScreen
);
157 drv_get_rec(ScrnInfoPtr pScrn
)
159 if (pScrn
->driverPrivate
)
162 pScrn
->driverPrivate
= xnfcalloc(1, sizeof(modesettingRec
));
168 drv_free_rec(ScrnInfoPtr pScrn
)
173 if (!pScrn
->driverPrivate
)
176 xfree(pScrn
->driverPrivate
);
178 pScrn
->driverPrivate
= NULL
;
182 drv_probe_ddc(ScrnInfoPtr pScrn
, int index
)
184 ConfiguredMonitor
= NULL
;
188 drv_crtc_resize(ScrnInfoPtr pScrn
, int width
, int height
)
190 xf86CrtcConfigPtr xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
191 modesettingPtr ms
= modesettingPTR(pScrn
);
192 ScreenPtr pScreen
= pScrn
->pScreen
;
193 int old_width
, old_height
;
194 PixmapPtr rootPixmap
;
197 if (width
== pScrn
->virtualX
&& height
== pScrn
->virtualY
)
200 old_width
= pScrn
->virtualX
;
201 old_height
= pScrn
->virtualY
;
202 pScrn
->virtualX
= width
;
203 pScrn
->virtualY
= height
;
205 /* ms->create_front_buffer will remove the old front buffer */
207 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
208 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, width
, height
, -1, -1, -1, NULL
))
211 pScrn
->displayWidth
= rootPixmap
->devKind
/ (rootPixmap
->drawable
.bitsPerPixel
/ 8);
213 if (!ms
->create_front_buffer(pScrn
) || !ms
->bind_front_buffer(pScrn
))
217 * create && bind will turn off all crtc(s) in the kernel so we need to
218 * re-enable all the crtcs again. For real HW we might want to do this
219 * before destroying the old framebuffer.
221 for (i
= 0; i
< xf86_config
->num_crtc
; i
++) {
222 xf86CrtcPtr crtc
= xf86_config
->crtc
[i
];
227 crtc
->funcs
->set_mode_major(crtc
, &crtc
->mode
, crtc
->rotation
, crtc
->x
, crtc
->y
);
233 * This is the error recovery path.
236 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, old_width
, old_height
, -1, -1, -1, NULL
))
237 FatalError("failed to resize rootPixmap error path\n");
239 pScrn
->displayWidth
= rootPixmap
->devKind
/ (rootPixmap
->drawable
.bitsPerPixel
/ 8);
242 pScrn
->virtualX
= old_width
;
243 pScrn
->virtualY
= old_height
;
245 if (ms
->create_front_buffer(pScrn
) && ms
->bind_front_buffer(pScrn
))
248 FatalError("failed to setup old framebuffer\n");
252 static const xf86CrtcConfigFuncsRec crtc_config_funcs
= {
253 .resize
= drv_crtc_resize
257 drv_init_drm(ScrnInfoPtr pScrn
)
259 modesettingPtr ms
= modesettingPTR(pScrn
);
261 /* deal with server regeneration */
266 sprintf(BusID
, "PCI:%d:%d:%d",
267 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
268 ms
->PciInfo
->dev
, ms
->PciInfo
->func
272 ms
->api
= drm_api_create();
273 ms
->fd
= drmOpen(ms
->api
? ms
->api
->driver_name
: NULL
, BusID
);
279 if (ms
->api
&& ms
->api
->destroy
)
280 ms
->api
->destroy(ms
->api
);
291 drv_close_drm(ScrnInfoPtr pScrn
)
293 modesettingPtr ms
= modesettingPTR(pScrn
);
295 if (ms
->api
&& ms
->api
->destroy
)
296 ms
->api
->destroy(ms
->api
);
306 drv_init_resource_management(ScrnInfoPtr pScrn
)
308 modesettingPtr ms
= modesettingPTR(pScrn
);
310 ScreenPtr pScreen = pScrn->pScreen;
311 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
312 Bool fbAccessDisabled;
316 if (ms
->screen
|| ms
->kms
)
320 ms
->screen
= ms
->no3D
? NULL
:
321 ms
->api
->create_screen(ms
->api
, ms
->fd
);
326 if (ms
->api
->destroy
)
327 ms
->api
->destroy(ms
->api
);
333 if (!kms_create(ms
->fd
, &ms
->kms
))
341 drv_close_resource_management(ScrnInfoPtr pScrn
)
343 modesettingPtr ms
= modesettingPTR(pScrn
);
346 assert(ms
->ctx
== NULL
);
347 ms
->screen
->destroy(ms
->screen
);
353 kms_destroy(&ms
->kms
);
360 drv_cleanup_fences(ScrnInfoPtr pScrn
)
362 modesettingPtr ms
= modesettingPTR(pScrn
);
367 for (i
= 0; i
< XORG_NR_FENCES
; i
++) {
369 ms
->screen
->fence_finish(ms
->screen
, ms
->fence
[i
], 0);
370 ms
->screen
->fence_reference(ms
->screen
, &ms
->fence
[i
], NULL
);
376 drv_pre_init(ScrnInfoPtr pScrn
, int flags
)
378 xf86CrtcConfigPtr xf86_config
;
380 rgb defaultWeight
= { 0, 0, 0 };
383 int max_width
, max_height
;
386 if (pScrn
->numEntities
!= 1)
389 pEnt
= xf86GetEntityInfo(pScrn
->entityList
[0]);
391 if (flags
& PROBE_DETECT
) {
392 drv_probe_ddc(pScrn
, pEnt
->index
);
396 cust
= (CustomizerPtr
) pScrn
->driverPrivate
;
397 pScrn
->driverPrivate
= NULL
;
399 /* Allocate driverPrivate */
400 if (!drv_get_rec(pScrn
))
403 ms
= modesettingPTR(pScrn
);
407 pScrn
->displayWidth
= 640; /* default it */
409 if (ms
->pEnt
->location
.type
!= BUS_PCI
)
412 ms
->PciInfo
= xf86GetPciInfoForEntity(ms
->pEnt
->index
);
414 /* Allocate an entity private if necessary */
415 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
416 FatalError("Entity");
418 msEnt
= xf86GetEntityPrivate(pScrn
->entityList
[0],
419 modesettingEntityIndex
)->ptr
;
420 ms
->entityPrivate
= msEnt
;
425 ms
->entityPrivate
= NULL
;
427 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
428 if (xf86IsPrimInitDone(pScrn
->entityList
[0])) {
431 xf86SetPrimInitDone(pScrn
->entityList
[0]);
437 if (!drv_init_drm(pScrn
))
440 pScrn
->monitor
= pScrn
->confScreen
->monitor
;
441 pScrn
->progClock
= TRUE
;
446 PreferConvert24to32
| SupportConvert24to32
| Support32bppFb
))
449 switch (pScrn
->depth
) {
455 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
456 "Given depth (%d) is not supported by the driver\n",
460 xf86PrintDepthBpp(pScrn
);
462 if (!xf86SetWeight(pScrn
, defaultWeight
, defaultWeight
))
464 if (!xf86SetDefaultVisual(pScrn
, -1))
467 /* Process the options */
468 xf86CollectOptions(pScrn
, NULL
);
469 if (!(ms
->Options
= xalloc(sizeof(drv_options
))))
471 memcpy(ms
->Options
, drv_options
, sizeof(drv_options
));
472 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, ms
->Options
);
474 /* Allocate an xf86CrtcConfig */
475 xf86CrtcConfigInit(pScrn
, &crtc_config_funcs
);
476 xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
478 max_width
= 2048; /* A very low default */
479 max_height
= 2048; /* see screen_init */
480 xf86CrtcSetSizeRange(pScrn
, 320, 200, max_width
, max_height
);
482 if (xf86ReturnOptValBool(ms
->Options
, OPTION_SW_CURSOR
, FALSE
)) {
486 xorg_crtc_init(pScrn
);
487 xorg_output_init(pScrn
);
489 if (!xf86InitialConfiguration(pScrn
, TRUE
)) {
490 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No valid modes.\n");
495 * If the driver can do gamma correction, it should call xf86SetGamma() here.
498 Gamma zeros
= { 0.0, 0.0, 0.0 };
500 if (!xf86SetGamma(pScrn
, zeros
)) {
505 if (pScrn
->modes
== NULL
) {
506 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No modes.\n");
510 pScrn
->currentMode
= pScrn
->modes
;
512 /* Set display resolution */
513 xf86SetDpi(pScrn
, 0, 0);
515 /* Load the required sub modules */
516 if (!xf86LoadSubModule(pScrn
, "fb"))
519 /* XXX: these aren't needed when we are using libkms */
520 if (!xf86LoadSubModule(pScrn
, "exa"))
524 if (!xf86LoadSubModule(pScrn
, "dri2"))
531 static void drv_block_handler(int i
, pointer blockData
, pointer pTimeout
,
534 ScreenPtr pScreen
= screenInfo
.screens
[i
];
535 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
537 pScreen
->BlockHandler
= ms
->blockHandler
;
538 pScreen
->BlockHandler(i
, blockData
, pTimeout
, pReadmask
);
539 pScreen
->BlockHandler
= drv_block_handler
;
544 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_RENDER_CACHE
,
545 ms
->dirtyThrottling
?
546 &ms
->fence
[XORG_NR_FENCES
-1] :
549 if (ms
->dirtyThrottling
) {
551 ms
->ctx
->screen
->fence_finish(ms
->ctx
->screen
,
554 /* The amount of rendering generated by a block handler can be
555 * quite small. Let us get a fair way ahead of hardware before
558 for (j
= 0; j
< XORG_NR_FENCES
- 1; j
++)
559 ms
->screen
->fence_reference(ms
->screen
,
563 ms
->screen
->fence_reference(ms
->screen
,
564 &ms
->fence
[XORG_NR_FENCES
-1],
570 #ifdef DRM_MODE_FEATURE_DIRTYFB
572 RegionPtr dirty
= DamageRegion(ms
->damage
);
573 unsigned num_cliprects
= REGION_NUM_RECTS(dirty
);
576 drmModeClip
*clip
= alloca(num_cliprects
* sizeof(drmModeClip
));
577 BoxPtr rect
= REGION_RECTS(dirty
);
580 /* XXX no need for copy? */
581 for (i
= 0; i
< num_cliprects
; i
++, rect
++) {
582 clip
[i
].x1
= rect
->x1
;
583 clip
[i
].y1
= rect
->y1
;
584 clip
[i
].x2
= rect
->x2
;
585 clip
[i
].y2
= rect
->y2
;
588 /* TODO query connector property to see if this is needed */
589 ret
= drmModeDirtyFB(ms
->fd
, ms
->fb_id
, clip
, num_cliprects
);
591 debug_printf("%s: failed to send dirty (%i, %s)\n",
592 __func__
, ret
, strerror(-ret
));
595 DamageEmpty(ms
->damage
);
602 drv_create_screen_resources(ScreenPtr pScreen
)
604 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
605 modesettingPtr ms
= modesettingPTR(pScrn
);
606 PixmapPtr rootPixmap
;
611 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
612 ret
= pScreen
->CreateScreenResources(pScreen
);
613 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
615 ms
->bind_front_buffer(pScrn
);
619 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
621 #ifdef DRM_MODE_FEATURE_DIRTYFB
622 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
623 ms
->damage
= DamageCreate(NULL
, NULL
, DamageReportNone
, TRUE
,
624 pScreen
, rootPixmap
);
627 DamageRegister(&rootPixmap
->drawable
, ms
->damage
);
629 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Damage tracking initialized\n");
631 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
632 "Failed to create screen damage record\n");
643 drv_screen_init(int scrnIndex
, ScreenPtr pScreen
, int argc
, char **argv
)
645 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
646 modesettingPtr ms
= modesettingPTR(pScrn
);
647 unsigned max_width
, max_height
;
649 CustomizerPtr cust
= ms
->cust
;
655 if (!drv_init_drm(pScrn
)) {
656 FatalError("Could not init DRM");
660 use3D
= cust
? !cust
->no_3d
: TRUE
;
661 from_3D
= xf86GetOptValBool(ms
->Options
, OPTION_3D_ACCEL
,
667 if (!drv_init_resource_management(pScrn
)) {
668 FatalError("Could not init resource management (!pipe_screen && !libkms)");
672 if (!drv_init_front_buffer_functions(pScrn
)) {
673 FatalError("Could not init front buffer manager");
677 /* get max width and height */
680 res
= drmModeGetResources(ms
->fd
);
681 max_width
= res
->max_width
;
682 max_height
= res
->max_height
;
683 drmModeFreeResources(res
);
688 max
= ms
->screen
->get_param(ms
->screen
, PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
689 max
= 1 << (max
- 1);
690 max_width
= max
< max_width
? max
: max_width
;
691 max_height
= max
< max_height
? max
: max_height
;
694 xf86CrtcSetSizeRange(pScrn
, 1, 1, max_width
, max_height
);
696 pScrn
->pScreen
= pScreen
;
698 /* HW dependent - FIXME */
699 pScrn
->displayWidth
= pScrn
->virtualX
;
701 miClearVisualTypes();
703 if (!miSetVisualTypes(pScrn
->depth
,
704 miGetDefaultVisualMask(pScrn
->depth
),
705 pScrn
->rgbBits
, pScrn
->defaultVisual
))
708 if (!miSetPixmapDepths())
711 pScrn
->memPhysBase
= 0;
714 if (!fbScreenInit(pScreen
, NULL
,
715 pScrn
->virtualX
, pScrn
->virtualY
,
716 pScrn
->xDpi
, pScrn
->yDpi
,
717 pScrn
->displayWidth
, pScrn
->bitsPerPixel
))
720 if (pScrn
->bitsPerPixel
> 8) {
721 /* Fixup RGB ordering */
722 visual
= pScreen
->visuals
+ pScreen
->numVisuals
;
723 while (--visual
>= pScreen
->visuals
) {
724 if ((visual
->class | DynamicClass
) == DirectColor
) {
725 visual
->offsetRed
= pScrn
->offset
.red
;
726 visual
->offsetGreen
= pScrn
->offset
.green
;
727 visual
->offsetBlue
= pScrn
->offset
.blue
;
728 visual
->redMask
= pScrn
->mask
.red
;
729 visual
->greenMask
= pScrn
->mask
.green
;
730 visual
->blueMask
= pScrn
->mask
.blue
;
735 fbPictureInit(pScreen
, NULL
, 0);
737 ms
->blockHandler
= pScreen
->BlockHandler
;
738 pScreen
->BlockHandler
= drv_block_handler
;
739 ms
->createScreenResources
= pScreen
->CreateScreenResources
;
740 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
742 xf86SetBlackWhitePixels(pScreen
);
744 ms
->accelerate_2d
= xf86ReturnOptValBool(ms
->Options
, OPTION_2D_ACCEL
, FALSE
);
745 ms
->debug_fallback
= xf86ReturnOptValBool(ms
->Options
, OPTION_DEBUG_FALLBACK
, ms
->accelerate_2d
);
747 if (cust
&& cust
->winsys_screen_init
)
748 cust
->winsys_screen_init(cust
, ms
->fd
);
750 ms
->swapThrottling
= cust
? cust
->swap_throttling
: TRUE
;
751 from_st
= xf86GetOptValBool(ms
->Options
, OPTION_THROTTLE_SWAP
,
752 &ms
->swapThrottling
) ?
753 X_CONFIG
: X_DEFAULT
;
755 ms
->dirtyThrottling
= cust
? cust
->dirty_throttling
: TRUE
;
756 from_dt
= xf86GetOptValBool(ms
->Options
, OPTION_THROTTLE_DIRTY
,
757 &ms
->dirtyThrottling
) ?
758 X_CONFIG
: X_DEFAULT
;
761 ms
->exa
= xorg_exa_init(pScrn
, ms
->accelerate_2d
);
763 xorg_xv_init(pScreen
);
765 xorg_dri2_init(pScreen
);
769 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
770 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "# Usefull debugging info follows #\n");
771 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
772 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Using %s backend\n",
773 ms
->screen
? "Gallium3D" : "libkms");
774 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "2D Acceleration is %s\n",
775 ms
->screen
&& ms
->accelerate_2d
? "enabled" : "disabled");
776 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Fallback debugging is %s\n",
777 ms
->debug_fallback
? "enabled" : "disabled");
779 xf86DrvMsg(pScrn
->scrnIndex
, from_3D
, "3D Acceleration is %s\n",
780 ms
->screen
? "enabled" : "disabled");
782 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "3D Acceleration is disabled\n");
784 xf86DrvMsg(pScrn
->scrnIndex
, from_st
, "Swap Throttling is %s.\n",
785 ms
->swapThrottling
? "enabled" : "disabled");
786 xf86DrvMsg(pScrn
->scrnIndex
, from_dt
, "Dirty Throttling is %s.\n",
787 ms
->dirtyThrottling
? "enabled" : "disabled");
789 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
791 miInitializeBackingStore(pScreen
);
792 xf86SetBackingStore(pScreen
);
793 xf86SetSilkenMouse(pScreen
);
794 miDCInitialize(pScreen
, xf86GetPointerScreenFuncs());
796 /* Need to extend HWcursor support to handle mask interleave */
798 xf86_cursors_init(pScreen
, 64, 64,
799 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64
|
800 HARDWARE_CURSOR_ARGB
);
802 /* Must force it before EnterVT, so we are in control of VT and
803 * later memory should be bound when allocating, e.g rotate_mem */
804 pScrn
->vtSema
= TRUE
;
806 pScreen
->SaveScreen
= xf86SaveScreen
;
807 ms
->CloseScreen
= pScreen
->CloseScreen
;
808 pScreen
->CloseScreen
= drv_close_screen
;
810 if (!xf86CrtcScreenInit(pScreen
))
813 if (!miCreateDefColormap(pScreen
))
816 xf86DPMSInit(pScreen
, xf86DPMSSet
, 0);
818 if (serverGeneration
== 1)
819 xf86ShowUnusedOptions(pScrn
->scrnIndex
, pScrn
->options
);
821 return drv_enter_vt(scrnIndex
, 1);
825 drv_adjust_frame(int scrnIndex
, int x
, int y
, int flags
)
827 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
828 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
829 xf86OutputPtr output
= config
->output
[config
->compat_output
];
830 xf86CrtcPtr crtc
= output
->crtc
;
832 if (crtc
&& crtc
->enabled
) {
833 crtc
->funcs
->set_mode_major(crtc
, pScrn
->currentMode
,
835 crtc
->x
= output
->initial_x
+ x
;
836 crtc
->y
= output
->initial_y
+ y
;
841 drv_free_screen(int scrnIndex
, int flags
)
843 drv_free_rec(xf86Screens
[scrnIndex
]);
847 drv_leave_vt(int scrnIndex
, int flags
)
849 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
850 modesettingPtr ms
= modesettingPTR(pScrn
);
851 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
852 CustomizerPtr cust
= ms
->cust
;
855 if (cust
&& cust
->winsys_leave_vt
)
856 cust
->winsys_leave_vt(cust
);
858 for (o
= 0; o
< config
->num_crtc
; o
++) {
859 xf86CrtcPtr crtc
= config
->crtc
[o
];
861 xorg_crtc_cursor_destroy(crtc
);
863 if (crtc
->rotatedPixmap
|| crtc
->rotatedData
) {
864 crtc
->funcs
->shadow_destroy(crtc
, crtc
->rotatedPixmap
,
866 crtc
->rotatedPixmap
= NULL
;
867 crtc
->rotatedData
= NULL
;
871 drmModeRmFB(ms
->fd
, ms
->fb_id
);
876 drv_cleanup_fences(pScrn
);
878 if (drmDropMaster(ms
->fd
))
879 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
880 "drmDropMaster failed: %s\n", strerror(errno
));
882 pScrn
->vtSema
= FALSE
;
886 * This gets called when gaining control of the VT, and from ScreenInit().
889 drv_enter_vt(int scrnIndex
, int flags
)
891 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
892 modesettingPtr ms
= modesettingPTR(pScrn
);
893 CustomizerPtr cust
= ms
->cust
;
895 if (drmSetMaster(ms
->fd
)) {
896 if (errno
== EINVAL
) {
897 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
898 "drmSetMaster failed: 2.6.29 or newer kernel required for "
899 "multi-server DRI\n");
901 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
902 "drmSetMaster failed: %s\n", strerror(errno
));
906 if (!ms
->create_front_buffer(pScrn
))
909 if (!flags
&& !ms
->bind_front_buffer(pScrn
))
912 if (!xf86SetDesiredModes(pScrn
))
915 if (cust
&& cust
->winsys_enter_vt
)
916 cust
->winsys_enter_vt(cust
);
922 drv_switch_mode(int scrnIndex
, DisplayModePtr mode
, int flags
)
924 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
926 return xf86SetSingleMode(pScrn
, mode
, RR_Rotate_0
);
930 drv_close_screen(int scrnIndex
, ScreenPtr pScreen
)
932 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
933 modesettingPtr ms
= modesettingPTR(pScrn
);
934 CustomizerPtr cust
= ms
->cust
;
937 FreeCursor(ms
->cursor
, None
);
941 if (cust
&& cust
->winsys_screen_close
)
942 cust
->winsys_screen_close(cust
);
946 xorg_dri2_close(pScreen
);
949 pScreen
->BlockHandler
= ms
->blockHandler
;
950 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
952 #ifdef DRM_MODE_FEATURE_DIRTYFB
954 DamageUnregister(&pScreen
->GetScreenPixmap(pScreen
)->drawable
, ms
->damage
);
955 DamageDestroy(ms
->damage
);
960 drmModeRmFB(ms
->fd
, ms
->fb_id
);
961 ms
->destroy_front_buffer(pScrn
);
964 xorg_exa_close(pScrn
);
967 /* calls drop master make sure we don't talk to 3D HW after that */
969 drv_leave_vt(scrnIndex
, 0);
972 drv_close_resource_management(pScrn
);
974 drv_close_drm(pScrn
);
976 pScrn
->vtSema
= FALSE
;
977 pScreen
->CloseScreen
= ms
->CloseScreen
;
978 return (*pScreen
->CloseScreen
) (scrnIndex
, pScreen
);
982 drv_valid_mode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
, int flags
)
989 * Front buffer backing store functions.
993 drv_destroy_front_buffer_ga3d(ScrnInfoPtr pScrn
)
995 modesettingPtr ms
= modesettingPTR(pScrn
);
997 if (!ms
->root_texture
)
1000 if (ms
->fb_id
!= -1) {
1001 drmModeRmFB(ms
->fd
, ms
->fb_id
);
1005 pipe_resource_reference(&ms
->root_texture
, NULL
);
1010 drv_create_front_buffer_ga3d(ScrnInfoPtr pScrn
)
1012 modesettingPtr ms
= modesettingPTR(pScrn
);
1013 struct pipe_resource
*tex
;
1014 struct winsys_handle whandle
;
1020 tex
= xorg_exa_create_root_texture(pScrn
, pScrn
->virtualX
, pScrn
->virtualY
,
1021 pScrn
->depth
, pScrn
->bitsPerPixel
);
1026 memset(&whandle
, 0, sizeof(whandle
));
1027 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1029 if (!ms
->screen
->resource_get_handle(ms
->screen
, tex
, &whandle
))
1032 ret
= drmModeAddFB(ms
->fd
,
1036 pScrn
->bitsPerPixel
,
1041 debug_printf("%s: failed to create framebuffer (%i, %s)\n",
1042 __func__
, ret
, strerror(-ret
));
1046 if (!drv_destroy_front_buffer_ga3d(pScrn
))
1047 FatalError("%s: failed to take down old framebuffer\n", __func__
);
1051 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
1053 pipe_resource_reference(&ms
->root_texture
, tex
);
1054 pipe_resource_reference(&tex
, NULL
);
1060 pipe_resource_reference(&tex
, NULL
);
1065 drv_bind_front_buffer_ga3d(ScrnInfoPtr pScrn
)
1067 modesettingPtr ms
= modesettingPTR(pScrn
);
1068 ScreenPtr pScreen
= pScrn
->pScreen
;
1069 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1070 struct pipe_resource
*check
;
1072 xorg_exa_set_displayed_usage(rootPixmap
);
1073 xorg_exa_set_shared_usage(rootPixmap
);
1074 xorg_exa_set_texture(rootPixmap
, ms
->root_texture
);
1075 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, -1, -1, -1, -1, -1, NULL
))
1076 FatalError("Couldn't adjust screen pixmap\n");
1078 check
= xorg_exa_get_texture(rootPixmap
);
1079 if (ms
->root_texture
!= check
)
1080 FatalError("Created new root texture\n");
1082 pipe_resource_reference(&check
, NULL
);
1088 drv_destroy_front_buffer_kms(ScrnInfoPtr pScrn
)
1090 modesettingPtr ms
= modesettingPTR(pScrn
);
1091 ScreenPtr pScreen
= pScrn
->pScreen
;
1092 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1094 /* XXX Do something with the rootPixmap.
1095 * This currently works fine but if we are getting crashes in
1096 * the fb functions after VT switches maybe look more into it.
1103 if (ms
->fb_id
!= -1) {
1104 drmModeRmFB(ms
->fd
, ms
->fb_id
);
1108 kms_bo_unmap(ms
->root_bo
);
1109 kms_bo_destroy(&ms
->root_bo
);
1114 drv_create_front_buffer_kms(ScrnInfoPtr pScrn
)
1116 modesettingPtr ms
= modesettingPTR(pScrn
);
1117 unsigned handle
, stride
;
1123 attr
[0] = KMS_BO_TYPE
;
1124 #ifdef KMS_BO_TYPE_SCANOUT_X8R8G8B8
1125 attr
[1] = KMS_BO_TYPE_SCANOUT_X8R8G8B8
;
1127 attr
[1] = KMS_BO_TYPE_SCANOUT
;
1129 attr
[2] = KMS_WIDTH
;
1130 attr
[3] = pScrn
->virtualX
;
1131 attr
[4] = KMS_HEIGHT
;
1132 attr
[5] = pScrn
->virtualY
;
1135 if (kms_bo_create(ms
->kms
, attr
, &bo
))
1138 if (kms_bo_get_prop(bo
, KMS_PITCH
, &stride
))
1141 if (kms_bo_get_prop(bo
, KMS_HANDLE
, &handle
))
1144 ret
= drmModeAddFB(ms
->fd
,
1148 pScrn
->bitsPerPixel
,
1153 debug_printf("%s: failed to create framebuffer (%i, %s)",
1154 __func__
, ret
, strerror(-ret
));
1158 if (!drv_destroy_front_buffer_kms(pScrn
))
1159 FatalError("%s: could not takedown old bo", __func__
);
1163 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
1170 kms_bo_destroy(&bo
);
1175 drv_bind_front_buffer_kms(ScrnInfoPtr pScrn
)
1177 modesettingPtr ms
= modesettingPTR(pScrn
);
1178 ScreenPtr pScreen
= pScrn
->pScreen
;
1179 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1183 if (kms_bo_get_prop(ms
->root_bo
, KMS_PITCH
, &stride
))
1186 if (kms_bo_map(ms
->root_bo
, &ptr
))
1189 pScreen
->ModifyPixmapHeader(rootPixmap
,
1193 pScrn
->bitsPerPixel
,
1197 /* This a hack to work around EnableDisableFBAccess setting the pointer
1198 * the real fix would be to replace pScrn->EnableDisableFBAccess hook
1199 * and set the rootPixmap->devPrivate.ptr to something valid before that.
1201 * But in its infinit visdome something uses either this some times before
1202 * that, so our hook doesn't get called before the crash happens.
1204 pScrn
->pixmapPrivate
.ptr
= ptr
;
1209 kms_bo_destroy(&ms
->root_bo
);
1212 #endif /* HAVE_LIBKMS */
1214 static Bool
drv_init_front_buffer_functions(ScrnInfoPtr pScrn
)
1216 modesettingPtr ms
= modesettingPTR(pScrn
);
1218 ms
->destroy_front_buffer
= drv_destroy_front_buffer_ga3d
;
1219 ms
->create_front_buffer
= drv_create_front_buffer_ga3d
;
1220 ms
->bind_front_buffer
= drv_bind_front_buffer_ga3d
;
1222 } else if (ms
->kms
) {
1223 ms
->destroy_front_buffer
= drv_destroy_front_buffer_kms
;
1224 ms
->create_front_buffer
= drv_create_front_buffer_kms
;
1225 ms
->bind_front_buffer
= drv_bind_front_buffer_kms
;
1233 CustomizerPtr
xorg_customizer(ScrnInfoPtr pScrn
)
1235 return modesettingPTR(pScrn
)->cust
;
1238 Bool
xorg_has_gallium(ScrnInfoPtr pScrn
)
1240 return modesettingPTR(pScrn
)->screen
!= NULL
;
1243 /* vim: set sw=4 ts=8 sts=4: */