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"
49 #include "xorgVersion.h"
50 #ifndef XSERVER_LIBPCIACCESS
51 #error "libpciaccess needed"
54 #include <pciaccess.h>
56 #include "state_tracker/drm_driver.h"
57 #include "pipe/p_context.h"
58 #include "xorg_tracker.h"
59 #include "xorg_winsys.h"
66 * Functions and symbols exported to Xorg via pointers.
69 static Bool
drv_pre_init(ScrnInfoPtr pScrn
, int flags
);
70 static Bool
drv_screen_init(int scrnIndex
, ScreenPtr pScreen
, int argc
,
72 static Bool
drv_switch_mode(int scrnIndex
, DisplayModePtr mode
, int flags
);
73 static void drv_adjust_frame(int scrnIndex
, int x
, int y
, int flags
);
74 static Bool
drv_enter_vt(int scrnIndex
, int flags
);
75 static void drv_leave_vt(int scrnIndex
, int flags
);
76 static void drv_free_screen(int scrnIndex
, int flags
);
77 static ModeStatus
drv_valid_mode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
,
84 OPTION_DEBUG_FALLBACK
,
86 OPTION_THROTTLE_DIRTY
,
90 static const OptionInfoRec drv_options
[] = {
91 {OPTION_SW_CURSOR
, "SWcursor", OPTV_BOOLEAN
, {0}, FALSE
},
92 {OPTION_2D_ACCEL
, "2DAccel", OPTV_BOOLEAN
, {0}, FALSE
},
93 {OPTION_DEBUG_FALLBACK
, "DebugFallback", OPTV_BOOLEAN
, {0}, FALSE
},
94 {OPTION_THROTTLE_SWAP
, "SwapThrottling", OPTV_BOOLEAN
, {0}, FALSE
},
95 {OPTION_THROTTLE_DIRTY
, "DirtyThrottling", OPTV_BOOLEAN
, {0}, FALSE
},
96 {OPTION_3D_ACCEL
, "3DAccel", OPTV_BOOLEAN
, {0}, FALSE
},
97 {-1, NULL
, OPTV_NONE
, {0}, FALSE
}
102 * Exported Xorg driver functions to winsys
105 const OptionInfoRec
*
106 xorg_tracker_available_options(int chipid
, int busid
)
112 xorg_tracker_set_functions(ScrnInfoPtr scrn
)
114 scrn
->PreInit
= drv_pre_init
;
115 scrn
->ScreenInit
= drv_screen_init
;
116 scrn
->SwitchMode
= drv_switch_mode
;
117 scrn
->AdjustFrame
= drv_adjust_frame
;
118 scrn
->EnterVT
= drv_enter_vt
;
119 scrn
->LeaveVT
= drv_leave_vt
;
120 scrn
->FreeScreen
= drv_free_screen
;
121 scrn
->ValidMode
= drv_valid_mode
;
125 xorg_tracker_have_modesetting(ScrnInfoPtr pScrn
, struct pci_device
*device
)
127 char *BusID
= malloc(64);
128 sprintf(BusID
, "pci:%04x:%02x:%02x.%d",
129 device
->domain
, device
->bus
,
130 device
->dev
, device
->func
);
132 if (drmCheckModesettingSupported(BusID
)) {
133 xf86DrvMsgVerb(pScrn
->scrnIndex
, X_INFO
, 0,
134 "Drm modesetting not supported %s\n", BusID
);
139 xf86DrvMsgVerb(pScrn
->scrnIndex
, X_INFO
, 0,
140 "Drm modesetting supported on %s\n", BusID
);
148 * Internal function definitions
151 static Bool
drv_init_front_buffer_functions(ScrnInfoPtr pScrn
);
152 static Bool
drv_close_screen(int scrnIndex
, ScreenPtr pScreen
);
160 drv_get_rec(ScrnInfoPtr pScrn
)
162 if (pScrn
->driverPrivate
)
165 pScrn
->driverPrivate
= xnfcalloc(1, sizeof(modesettingRec
));
171 drv_free_rec(ScrnInfoPtr pScrn
)
176 if (!pScrn
->driverPrivate
)
179 free(pScrn
->driverPrivate
);
181 pScrn
->driverPrivate
= NULL
;
185 drv_probe_ddc(ScrnInfoPtr pScrn
, int index
)
187 ConfiguredMonitor
= NULL
;
191 drv_crtc_resize(ScrnInfoPtr pScrn
, int width
, int height
)
193 xf86CrtcConfigPtr xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
194 modesettingPtr ms
= modesettingPTR(pScrn
);
195 CustomizerPtr cust
= ms
->cust
;
196 ScreenPtr pScreen
= pScrn
->pScreen
;
197 int old_width
, old_height
;
198 PixmapPtr rootPixmap
;
201 if (width
== pScrn
->virtualX
&& height
== pScrn
->virtualY
)
204 if (cust
&& cust
->winsys_check_fb_size
&&
205 !cust
->winsys_check_fb_size(cust
, width
*pScrn
->bitsPerPixel
/ 8,
207 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
208 "Requested framebuffer size %dx%dx%d will not fit "
209 "in display memory.\n",
210 width
, height
, pScrn
->bitsPerPixel
);
214 old_width
= pScrn
->virtualX
;
215 old_height
= pScrn
->virtualY
;
216 pScrn
->virtualX
= width
;
217 pScrn
->virtualY
= height
;
219 /* ms->create_front_buffer will remove the old front buffer */
221 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
222 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, width
, height
, -1, -1, -1, NULL
))
225 pScrn
->displayWidth
= rootPixmap
->devKind
/ (rootPixmap
->drawable
.bitsPerPixel
/ 8);
227 if (!ms
->create_front_buffer(pScrn
) || !ms
->bind_front_buffer(pScrn
))
231 * create && bind will turn off all crtc(s) in the kernel so we need to
232 * re-enable all the crtcs again. For real HW we might want to do this
233 * before destroying the old framebuffer.
235 for (i
= 0; i
< xf86_config
->num_crtc
; i
++) {
236 xf86CrtcPtr crtc
= xf86_config
->crtc
[i
];
241 crtc
->funcs
->set_mode_major(crtc
, &crtc
->mode
, crtc
->rotation
, crtc
->x
, crtc
->y
);
247 * This is the error recovery path.
250 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, old_width
, old_height
, -1, -1, -1, NULL
))
251 FatalError("failed to resize rootPixmap error path\n");
253 pScrn
->displayWidth
= rootPixmap
->devKind
/ (rootPixmap
->drawable
.bitsPerPixel
/ 8);
256 pScrn
->virtualX
= old_width
;
257 pScrn
->virtualY
= old_height
;
259 if (ms
->create_front_buffer(pScrn
) && ms
->bind_front_buffer(pScrn
))
262 FatalError("failed to setup old framebuffer\n");
266 static const xf86CrtcConfigFuncsRec crtc_config_funcs
= {
267 .resize
= drv_crtc_resize
271 drv_init_drm(ScrnInfoPtr pScrn
)
273 modesettingPtr ms
= modesettingPTR(pScrn
);
275 /* deal with server regeneration */
280 sprintf(BusID
, "PCI:%d:%d:%d",
281 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
282 ms
->PciInfo
->dev
, ms
->PciInfo
->func
286 ms
->fd
= drmOpen(driver_descriptor
.driver_name
, BusID
);
300 drv_init_resource_management(ScrnInfoPtr pScrn
)
302 modesettingPtr ms
= modesettingPTR(pScrn
);
304 ScreenPtr pScreen = pScrn->pScreen;
305 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
306 Bool fbAccessDisabled;
310 if (ms
->screen
|| ms
->kms
)
314 ms
->screen
= driver_descriptor
.create_screen(ms
->fd
);
320 if (!kms_create(ms
->fd
, &ms
->kms
))
328 drv_cleanup_fences(ScrnInfoPtr pScrn
)
330 modesettingPtr ms
= modesettingPTR(pScrn
);
335 for (i
= 0; i
< XORG_NR_FENCES
; i
++) {
337 ms
->screen
->fence_finish(ms
->screen
, ms
->fence
[i
],
338 PIPE_TIMEOUT_INFINITE
);
339 ms
->screen
->fence_reference(ms
->screen
, &ms
->fence
[i
], NULL
);
345 drv_pre_init(ScrnInfoPtr pScrn
, int flags
)
347 xf86CrtcConfigPtr xf86_config
;
349 rgb defaultWeight
= { 0, 0, 0 };
355 if (pScrn
->numEntities
!= 1)
358 pEnt
= xf86GetEntityInfo(pScrn
->entityList
[0]);
360 if (flags
& PROBE_DETECT
) {
361 drv_probe_ddc(pScrn
, pEnt
->index
);
365 cust
= (CustomizerPtr
) pScrn
->driverPrivate
;
366 pScrn
->driverPrivate
= NULL
;
368 /* Allocate driverPrivate */
369 if (!drv_get_rec(pScrn
))
372 ms
= modesettingPTR(pScrn
);
377 pScrn
->displayWidth
= 640; /* default it */
379 if (ms
->pEnt
->location
.type
!= BUS_PCI
)
382 ms
->PciInfo
= xf86GetPciInfoForEntity(ms
->pEnt
->index
);
384 /* Allocate an entity private if necessary */
385 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
386 FatalError("Entity");
388 msEnt
= xf86GetEntityPrivate(pScrn
->entityList
[0],
389 modesettingEntityIndex
)->ptr
;
390 ms
->entityPrivate
= msEnt
;
395 ms
->entityPrivate
= NULL
;
397 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
398 if (xf86IsPrimInitDone(pScrn
->entityList
[0])) {
401 xf86SetPrimInitDone(pScrn
->entityList
[0]);
406 if (!drv_init_drm(pScrn
))
409 pScrn
->monitor
= pScrn
->confScreen
->monitor
;
410 pScrn
->progClock
= TRUE
;
415 PreferConvert24to32
| SupportConvert24to32
| Support32bppFb
))
418 switch (pScrn
->depth
) {
425 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
426 "Given depth (%d) is not supported by the driver\n",
430 xf86PrintDepthBpp(pScrn
);
432 if (!xf86SetWeight(pScrn
, defaultWeight
, defaultWeight
))
434 if (!xf86SetDefaultVisual(pScrn
, -1))
437 /* Process the options */
438 xf86CollectOptions(pScrn
, NULL
);
439 if (!(ms
->Options
= malloc(sizeof(drv_options
))))
441 memcpy(ms
->Options
, drv_options
, sizeof(drv_options
));
442 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, ms
->Options
);
444 use3D
= cust
? !cust
->no_3d
: TRUE
;
445 ms
->from_3D
= xf86GetOptValBool(ms
->Options
, OPTION_3D_ACCEL
,
451 if (!drv_init_resource_management(pScrn
)) {
452 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "Could not init "
453 "Gallium3D or libKMS.\n");
457 /* Allocate an xf86CrtcConfig */
458 xf86CrtcConfigInit(pScrn
, &crtc_config_funcs
);
459 xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
461 /* get max width and height */
464 int max_width
, max_height
;
466 res
= drmModeGetResources(ms
->fd
);
467 max_width
= res
->max_width
;
468 max_height
= res
->max_height
;
473 max
= ms
->screen
->get_param(ms
->screen
,
474 PIPE_CAP_MAX_TEXTURE_2D_LEVELS
);
475 max
= 1 << (max
- 1);
476 max_width
= max
< max_width
? max
: max_width
;
477 max_height
= max
< max_height
? max
: max_height
;
480 xf86CrtcSetSizeRange(pScrn
, res
->min_width
,
481 res
->min_height
, max_width
, max_height
);
482 xf86DrvMsg(pScrn
->scrnIndex
, X_PROBED
,
483 "Min width %d, Max Width %d.\n",
484 res
->min_width
, max_width
);
485 xf86DrvMsg(pScrn
->scrnIndex
, X_PROBED
,
486 "Min height %d, Max Height %d.\n",
487 res
->min_height
, max_height
);
488 drmModeFreeResources(res
);
492 if (xf86ReturnOptValBool(ms
->Options
, OPTION_SW_CURSOR
, FALSE
)) {
496 xorg_crtc_init(pScrn
);
497 xorg_output_init(pScrn
);
499 if (cust
&& cust
->winsys_pre_init
&& !cust
->winsys_pre_init(cust
, ms
->fd
))
502 if (!xf86InitialConfiguration(pScrn
, TRUE
)) {
503 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No valid modes.\n");
508 * If the driver can do gamma correction, it should call xf86SetGamma() here.
511 Gamma zeros
= { 0.0, 0.0, 0.0 };
513 if (!xf86SetGamma(pScrn
, zeros
)) {
518 if (pScrn
->modes
== NULL
) {
519 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No modes.\n");
523 pScrn
->currentMode
= pScrn
->modes
;
525 /* Set display resolution */
526 xf86SetDpi(pScrn
, 0, 0);
528 /* Load the required sub modules */
529 if (!xf86LoadSubModule(pScrn
, "fb"))
532 /* XXX: these aren't needed when we are using libkms */
533 if (!xf86LoadSubModule(pScrn
, "exa"))
537 if (!xf86LoadSubModule(pScrn
, "dri2"))
544 void xorg_flush(ScreenPtr pScreen
)
546 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
551 ms
->ctx
->flush(ms
->ctx
,
552 ms
->dirtyThrottling
?
553 &ms
->fence
[XORG_NR_FENCES
-1] :
556 if (ms
->dirtyThrottling
) {
558 ms
->ctx
->screen
->fence_finish(ms
->ctx
->screen
,
560 PIPE_TIMEOUT_INFINITE
);
562 /* The amount of rendering generated by a block handler can be
563 * quite small. Let us get a fair way ahead of hardware before
566 for (j
= 0; j
< XORG_NR_FENCES
- 1; j
++)
567 ms
->screen
->fence_reference(ms
->screen
,
571 ms
->screen
->fence_reference(ms
->screen
,
572 &ms
->fence
[XORG_NR_FENCES
-1],
577 #ifdef DRM_MODE_FEATURE_DIRTYFB
579 RegionPtr dirty
= DamageRegion(ms
->damage
);
580 unsigned num_cliprects
= REGION_NUM_RECTS(dirty
);
583 drmModeClip
*clip
= alloca(num_cliprects
* sizeof(drmModeClip
));
584 BoxPtr rect
= REGION_RECTS(dirty
);
587 /* XXX no need for copy? */
588 for (i
= 0; i
< num_cliprects
; i
++, rect
++) {
589 clip
[i
].x1
= rect
->x1
;
590 clip
[i
].y1
= rect
->y1
;
591 clip
[i
].x2
= rect
->x2
;
592 clip
[i
].y2
= rect
->y2
;
595 /* TODO query connector property to see if this is needed */
596 ret
= drmModeDirtyFB(ms
->fd
, ms
->fb_id
, clip
, num_cliprects
);
598 debug_printf("%s: failed to send dirty (%i, %s)\n",
599 __func__
, ret
, strerror(-ret
));
602 DamageEmpty(ms
->damage
);
608 static void drv_block_handler(int i
, pointer blockData
, pointer pTimeout
,
611 ScreenPtr pScreen
= screenInfo
.screens
[i
];
612 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
614 pScreen
->BlockHandler
= ms
->blockHandler
;
615 pScreen
->BlockHandler(i
, blockData
, pTimeout
, pReadmask
);
616 pScreen
->BlockHandler
= drv_block_handler
;
622 drv_create_screen_resources(ScreenPtr pScreen
)
624 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
625 modesettingPtr ms
= modesettingPTR(pScrn
);
626 PixmapPtr rootPixmap
;
631 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
632 ret
= pScreen
->CreateScreenResources(pScreen
);
633 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
635 ms
->bind_front_buffer(pScrn
);
639 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
641 #ifdef DRM_MODE_FEATURE_DIRTYFB
642 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
643 ms
->damage
= DamageCreate(NULL
, NULL
, DamageReportNone
, TRUE
,
644 pScreen
, rootPixmap
);
647 DamageRegister(&rootPixmap
->drawable
, ms
->damage
);
649 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Damage tracking initialized\n");
651 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
652 "Failed to create screen damage record\n");
663 drv_set_master(ScrnInfoPtr pScrn
)
665 modesettingPtr ms
= modesettingPTR(pScrn
);
667 if (!ms
->isMaster
&& drmSetMaster(ms
->fd
) != 0) {
668 if (errno
== EINVAL
) {
669 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
670 "drmSetMaster failed: 2.6.29 or newer kernel required for "
671 "multi-server DRI\n");
673 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
674 "drmSetMaster failed: %s\n", strerror(errno
));
684 static void drv_load_palette(ScrnInfoPtr pScrn
, int numColors
,
685 int *indices
, LOCO
*colors
, VisualPtr pVisual
)
687 xf86CrtcConfigPtr xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
688 modesettingPtr ms
= modesettingPTR(pScrn
);
692 switch(pScrn
->depth
) {
694 for (i
= 0; i
< numColors
; i
++) {
696 for (j
= 0; j
< 8; j
++) {
697 ms
->lut_r
[index
* 8 + j
] = colors
[index
].red
<< 8;
698 ms
->lut_g
[index
* 8 + j
] = colors
[index
].green
<< 8;
699 ms
->lut_b
[index
* 8 + j
] = colors
[index
].blue
<< 8;
704 for (i
= 0; i
< numColors
; i
++) {
708 for (j
= 0; j
< 8; j
++) {
709 ms
->lut_r
[index
* 8 + j
] = colors
[index
].red
<< 8;
710 ms
->lut_b
[index
* 8 + j
] = colors
[index
].blue
<< 8;
714 for (j
= 0; j
< 4; j
++) {
715 ms
->lut_g
[index
* 4 + j
] = colors
[index
].green
<< 8;
720 for (i
= 0; i
< numColors
; i
++) {
722 ms
->lut_r
[index
] = colors
[index
].red
<< 8;
723 ms
->lut_g
[index
] = colors
[index
].green
<< 8;
724 ms
->lut_b
[index
] = colors
[index
].blue
<< 8;
729 for (c
= 0; c
< xf86_config
->num_crtc
; c
++) {
730 xf86CrtcPtr crtc
= xf86_config
->crtc
[c
];
732 /* Make the change through RandR */
733 #ifdef RANDR_12_INTERFACE
734 if (crtc
->randr_crtc
)
735 RRCrtcGammaSet(crtc
->randr_crtc
, ms
->lut_r
, ms
->lut_g
, ms
->lut_b
);
738 crtc
->funcs
->gamma_set(crtc
, ms
->lut_r
, ms
->lut_g
, ms
->lut_b
, 256);
744 drv_screen_init(int scrnIndex
, ScreenPtr pScreen
, int argc
, char **argv
)
746 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
747 modesettingPtr ms
= modesettingPTR(pScrn
);
749 CustomizerPtr cust
= ms
->cust
;
753 if (!drv_set_master(pScrn
))
756 if (!drv_init_front_buffer_functions(pScrn
)) {
757 FatalError("Could not init front buffer manager");
761 pScrn
->pScreen
= pScreen
;
763 /* HW dependent - FIXME */
764 pScrn
->displayWidth
= pScrn
->virtualX
;
766 miClearVisualTypes();
768 if (!miSetVisualTypes(pScrn
->depth
,
769 miGetDefaultVisualMask(pScrn
->depth
),
770 pScrn
->rgbBits
, pScrn
->defaultVisual
))
773 if (!miSetPixmapDepths())
776 pScrn
->memPhysBase
= 0;
779 if (!fbScreenInit(pScreen
, NULL
,
780 pScrn
->virtualX
, pScrn
->virtualY
,
781 pScrn
->xDpi
, pScrn
->yDpi
,
782 pScrn
->displayWidth
, pScrn
->bitsPerPixel
))
785 if (pScrn
->bitsPerPixel
> 8) {
786 /* Fixup RGB ordering */
787 visual
= pScreen
->visuals
+ pScreen
->numVisuals
;
788 while (--visual
>= pScreen
->visuals
) {
789 if ((visual
->class | DynamicClass
) == DirectColor
) {
790 visual
->offsetRed
= pScrn
->offset
.red
;
791 visual
->offsetGreen
= pScrn
->offset
.green
;
792 visual
->offsetBlue
= pScrn
->offset
.blue
;
793 visual
->redMask
= pScrn
->mask
.red
;
794 visual
->greenMask
= pScrn
->mask
.green
;
795 visual
->blueMask
= pScrn
->mask
.blue
;
800 fbPictureInit(pScreen
, NULL
, 0);
802 ms
->blockHandler
= pScreen
->BlockHandler
;
803 pScreen
->BlockHandler
= drv_block_handler
;
804 ms
->createScreenResources
= pScreen
->CreateScreenResources
;
805 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
807 xf86SetBlackWhitePixels(pScreen
);
809 ms
->accelerate_2d
= xf86ReturnOptValBool(ms
->Options
, OPTION_2D_ACCEL
, FALSE
);
810 ms
->debug_fallback
= xf86ReturnOptValBool(ms
->Options
, OPTION_DEBUG_FALLBACK
, ms
->accelerate_2d
);
812 if (cust
&& cust
->winsys_screen_init
)
813 cust
->winsys_screen_init(cust
);
815 ms
->swapThrottling
= cust
? cust
->swap_throttling
: TRUE
;
816 from_st
= xf86GetOptValBool(ms
->Options
, OPTION_THROTTLE_SWAP
,
817 &ms
->swapThrottling
) ?
818 X_CONFIG
: X_DEFAULT
;
820 ms
->dirtyThrottling
= cust
? cust
->dirty_throttling
: TRUE
;
821 from_dt
= xf86GetOptValBool(ms
->Options
, OPTION_THROTTLE_DIRTY
,
822 &ms
->dirtyThrottling
) ?
823 X_CONFIG
: X_DEFAULT
;
826 ms
->exa
= xorg_exa_init(pScrn
, ms
->accelerate_2d
);
828 xorg_xv_init(pScreen
);
830 xorg_dri2_init(pScreen
);
834 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
835 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "# Usefull debugging info follows #\n");
836 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
837 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Using %s backend\n",
838 ms
->screen
? "Gallium3D" : "libkms");
839 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "2D Acceleration is %s\n",
840 ms
->screen
&& ms
->accelerate_2d
? "enabled" : "disabled");
841 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Fallback debugging is %s\n",
842 ms
->debug_fallback
? "enabled" : "disabled");
844 xf86DrvMsg(pScrn
->scrnIndex
, ms
->from_3D
, "3D Acceleration is %s\n",
845 ms
->screen
? "enabled" : "disabled");
847 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "3D Acceleration is disabled\n");
849 xf86DrvMsg(pScrn
->scrnIndex
, from_st
, "Swap Throttling is %s.\n",
850 ms
->swapThrottling
? "enabled" : "disabled");
851 xf86DrvMsg(pScrn
->scrnIndex
, from_dt
, "Dirty Throttling is %s.\n",
852 ms
->dirtyThrottling
? "enabled" : "disabled");
854 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "##################################\n");
856 miInitializeBackingStore(pScreen
);
857 xf86SetBackingStore(pScreen
);
858 xf86SetSilkenMouse(pScreen
);
859 miDCInitialize(pScreen
, xf86GetPointerScreenFuncs());
861 /* Need to extend HWcursor support to handle mask interleave */
863 xf86_cursors_init(pScreen
, 64, 64,
864 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64
|
865 HARDWARE_CURSOR_ARGB
|
866 ((cust
&& cust
->unhidden_hw_cursor_update
) ?
867 HARDWARE_CURSOR_UPDATE_UNHIDDEN
: 0));
869 /* Must force it before EnterVT, so we are in control of VT and
870 * later memory should be bound when allocating, e.g rotate_mem */
871 pScrn
->vtSema
= TRUE
;
873 pScreen
->SaveScreen
= xf86SaveScreen
;
874 ms
->CloseScreen
= pScreen
->CloseScreen
;
875 pScreen
->CloseScreen
= drv_close_screen
;
877 if (!xf86CrtcScreenInit(pScreen
))
880 if (!miCreateDefColormap(pScreen
))
882 if (!xf86HandleColormaps(pScreen
, 256, 8, drv_load_palette
, NULL
,
883 CMAP_PALETTED_TRUECOLOR
|
884 CMAP_RELOAD_ON_MODE_SWITCH
))
887 xf86DPMSInit(pScreen
, xf86DPMSSet
, 0);
889 if (serverGeneration
== 1)
890 xf86ShowUnusedOptions(pScrn
->scrnIndex
, pScrn
->options
);
892 return drv_enter_vt(scrnIndex
, 1);
896 drv_adjust_frame(int scrnIndex
, int x
, int y
, int flags
)
898 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
899 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
900 xf86OutputPtr output
= config
->output
[config
->compat_output
];
901 xf86CrtcPtr crtc
= output
->crtc
;
903 if (crtc
&& crtc
->enabled
) {
904 crtc
->funcs
->set_mode_major(crtc
, pScrn
->currentMode
,
906 crtc
->x
= output
->initial_x
+ x
;
907 crtc
->y
= output
->initial_y
+ y
;
912 drv_free_screen(int scrnIndex
, int flags
)
914 drv_free_rec(xf86Screens
[scrnIndex
]);
918 drv_leave_vt(int scrnIndex
, int flags
)
920 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
921 modesettingPtr ms
= modesettingPTR(pScrn
);
922 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
923 CustomizerPtr cust
= ms
->cust
;
926 if (cust
&& cust
->winsys_leave_vt
)
927 cust
->winsys_leave_vt(cust
);
929 for (o
= 0; o
< config
->num_crtc
; o
++) {
930 xf86CrtcPtr crtc
= config
->crtc
[o
];
932 xorg_crtc_cursor_destroy(crtc
);
934 if (crtc
->rotatedPixmap
|| crtc
->rotatedData
) {
935 crtc
->funcs
->shadow_destroy(crtc
, crtc
->rotatedPixmap
,
937 crtc
->rotatedPixmap
= NULL
;
938 crtc
->rotatedData
= NULL
;
942 if (ms
->fb_id
!= -1) {
943 drmModeRmFB(ms
->fd
, ms
->fb_id
);
949 drv_cleanup_fences(pScrn
);
951 if (drmDropMaster(ms
->fd
))
952 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
953 "drmDropMaster failed: %s\n", strerror(errno
));
955 ms
->isMaster
= FALSE
;
956 pScrn
->vtSema
= FALSE
;
960 * This gets called when gaining control of the VT, and from ScreenInit().
963 drv_enter_vt(int scrnIndex
, int flags
)
965 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
966 modesettingPtr ms
= modesettingPTR(pScrn
);
967 CustomizerPtr cust
= ms
->cust
;
969 if (!drv_set_master(pScrn
))
972 if (!ms
->create_front_buffer(pScrn
))
975 if (!flags
&& !ms
->bind_front_buffer(pScrn
))
978 if (!xf86SetDesiredModes(pScrn
))
981 if (cust
&& cust
->winsys_enter_vt
)
982 cust
->winsys_enter_vt(cust
);
988 drv_switch_mode(int scrnIndex
, DisplayModePtr mode
, int flags
)
990 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
992 return xf86SetSingleMode(pScrn
, mode
, RR_Rotate_0
);
996 drv_close_screen(int scrnIndex
, ScreenPtr pScreen
)
998 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
999 modesettingPtr ms
= modesettingPTR(pScrn
);
1000 CustomizerPtr cust
= ms
->cust
;
1003 FreeCursor(ms
->cursor
, None
);
1007 if (cust
&& cust
->winsys_screen_close
)
1008 cust
->winsys_screen_close(cust
);
1012 xorg_dri2_close(pScreen
);
1015 pScreen
->BlockHandler
= ms
->blockHandler
;
1016 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
1018 #ifdef DRM_MODE_FEATURE_DIRTYFB
1020 DamageUnregister(&pScreen
->GetScreenPixmap(pScreen
)->drawable
, ms
->damage
);
1021 DamageDestroy(ms
->damage
);
1026 ms
->destroy_front_buffer(pScrn
);
1029 xorg_exa_close(pScrn
);
1032 /* calls drop master make sure we don't talk to 3D HW after that */
1033 if (pScrn
->vtSema
) {
1034 drv_leave_vt(scrnIndex
, 0);
1037 pScrn
->vtSema
= FALSE
;
1038 pScreen
->CloseScreen
= ms
->CloseScreen
;
1040 return (*pScreen
->CloseScreen
) (scrnIndex
, pScreen
);
1044 drv_valid_mode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
, int flags
)
1051 * Front buffer backing store functions.
1055 drv_destroy_front_buffer_ga3d(ScrnInfoPtr pScrn
)
1057 modesettingPtr ms
= modesettingPTR(pScrn
);
1059 if (!ms
->root_texture
)
1062 if (ms
->fb_id
!= -1) {
1063 drmModeRmFB(ms
->fd
, ms
->fb_id
);
1067 pipe_resource_reference(&ms
->root_texture
, NULL
);
1072 drv_create_front_buffer_ga3d(ScrnInfoPtr pScrn
)
1074 modesettingPtr ms
= modesettingPTR(pScrn
);
1075 struct pipe_resource
*tex
;
1076 struct winsys_handle whandle
;
1082 tex
= xorg_exa_create_root_texture(pScrn
, pScrn
->virtualX
, pScrn
->virtualY
,
1083 pScrn
->depth
, pScrn
->bitsPerPixel
);
1088 memset(&whandle
, 0, sizeof(whandle
));
1089 whandle
.type
= DRM_API_HANDLE_TYPE_KMS
;
1091 if (!ms
->screen
->resource_get_handle(ms
->screen
, tex
, &whandle
))
1094 ret
= drmModeAddFB(ms
->fd
,
1098 pScrn
->bitsPerPixel
,
1103 debug_printf("%s: failed to create framebuffer (%i, %s)\n",
1104 __func__
, ret
, strerror(-ret
));
1108 if (!drv_destroy_front_buffer_ga3d(pScrn
))
1109 FatalError("%s: failed to take down old framebuffer\n", __func__
);
1113 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
1115 pipe_resource_reference(&ms
->root_texture
, tex
);
1116 pipe_resource_reference(&tex
, NULL
);
1122 pipe_resource_reference(&tex
, NULL
);
1127 drv_bind_front_buffer_ga3d(ScrnInfoPtr pScrn
)
1129 modesettingPtr ms
= modesettingPTR(pScrn
);
1130 ScreenPtr pScreen
= pScrn
->pScreen
;
1131 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1132 struct pipe_resource
*check
;
1134 xorg_exa_set_displayed_usage(rootPixmap
);
1135 xorg_exa_set_shared_usage(rootPixmap
);
1136 xorg_exa_set_texture(rootPixmap
, ms
->root_texture
);
1137 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, -1, -1, -1, -1, -1, NULL
))
1138 FatalError("Couldn't adjust screen pixmap\n");
1140 check
= xorg_exa_get_texture(rootPixmap
);
1141 if (ms
->root_texture
!= check
)
1142 FatalError("Created new root texture\n");
1144 pipe_resource_reference(&check
, NULL
);
1150 drv_destroy_front_buffer_kms(ScrnInfoPtr pScrn
)
1152 modesettingPtr ms
= modesettingPTR(pScrn
);
1153 ScreenPtr pScreen
= pScrn
->pScreen
;
1154 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1156 /* XXX Do something with the rootPixmap.
1157 * This currently works fine but if we are getting crashes in
1158 * the fb functions after VT switches maybe look more into it.
1165 if (ms
->fb_id
!= -1) {
1166 drmModeRmFB(ms
->fd
, ms
->fb_id
);
1170 kms_bo_unmap(ms
->root_bo
);
1171 kms_bo_destroy(&ms
->root_bo
);
1176 drv_create_front_buffer_kms(ScrnInfoPtr pScrn
)
1178 modesettingPtr ms
= modesettingPTR(pScrn
);
1179 unsigned handle
, stride
;
1185 attr
[0] = KMS_BO_TYPE
;
1186 #ifdef KMS_BO_TYPE_SCANOUT_X8R8G8B8
1187 attr
[1] = KMS_BO_TYPE_SCANOUT_X8R8G8B8
;
1189 attr
[1] = KMS_BO_TYPE_SCANOUT
;
1191 attr
[2] = KMS_WIDTH
;
1192 attr
[3] = pScrn
->virtualX
;
1193 attr
[4] = KMS_HEIGHT
;
1194 attr
[5] = pScrn
->virtualY
;
1197 if (kms_bo_create(ms
->kms
, attr
, &bo
))
1200 if (kms_bo_get_prop(bo
, KMS_PITCH
, &stride
))
1203 if (kms_bo_get_prop(bo
, KMS_HANDLE
, &handle
))
1206 ret
= drmModeAddFB(ms
->fd
,
1210 pScrn
->bitsPerPixel
,
1215 debug_printf("%s: failed to create framebuffer (%i, %s)",
1216 __func__
, ret
, strerror(-ret
));
1220 if (!drv_destroy_front_buffer_kms(pScrn
))
1221 FatalError("%s: could not takedown old bo", __func__
);
1225 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
1232 kms_bo_destroy(&bo
);
1237 drv_bind_front_buffer_kms(ScrnInfoPtr pScrn
)
1239 modesettingPtr ms
= modesettingPTR(pScrn
);
1240 ScreenPtr pScreen
= pScrn
->pScreen
;
1241 PixmapPtr rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
1245 if (kms_bo_get_prop(ms
->root_bo
, KMS_PITCH
, &stride
))
1248 if (kms_bo_map(ms
->root_bo
, &ptr
))
1251 pScreen
->ModifyPixmapHeader(rootPixmap
,
1255 pScrn
->bitsPerPixel
,
1259 #if (XORG_VERSION_CURRENT < XORG_VERSION_NUMERIC(1, 9, 99, 1, 0))
1261 /* This a hack to work around EnableDisableFBAccess setting the pointer
1262 * the real fix would be to replace pScrn->EnableDisableFBAccess hook
1263 * and set the rootPixmap->devPrivate.ptr to something valid before that.
1265 * But in its infinit visdome something uses either this some times before
1266 * that, so our hook doesn't get called before the crash happens.
1268 pScrn
->pixmapPrivate
.ptr
= ptr
;
1275 kms_bo_destroy(&ms
->root_bo
);
1278 #endif /* HAVE_LIBKMS */
1280 static Bool
drv_init_front_buffer_functions(ScrnInfoPtr pScrn
)
1282 modesettingPtr ms
= modesettingPTR(pScrn
);
1284 ms
->destroy_front_buffer
= drv_destroy_front_buffer_ga3d
;
1285 ms
->create_front_buffer
= drv_create_front_buffer_ga3d
;
1286 ms
->bind_front_buffer
= drv_bind_front_buffer_ga3d
;
1288 } else if (ms
->kms
) {
1289 ms
->destroy_front_buffer
= drv_destroy_front_buffer_kms
;
1290 ms
->create_front_buffer
= drv_create_front_buffer_kms
;
1291 ms
->bind_front_buffer
= drv_bind_front_buffer_kms
;
1299 CustomizerPtr
xorg_customizer(ScrnInfoPtr pScrn
)
1301 return modesettingPTR(pScrn
)->cust
;
1304 Bool
xorg_has_gallium(ScrnInfoPtr pScrn
)
1306 return modesettingPTR(pScrn
)->screen
!= NULL
;
1309 /* vim: set sw=4 ts=8 sts=4: */