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 #include <X11/extensions/Xv.h>
49 #ifndef XSERVER_LIBPCIACCESS
50 #error "libpciaccess needed"
53 #include <pciaccess.h>
55 #include "pipe/p_context.h"
56 #include "xorg_tracker.h"
57 #include "xorg_winsys.h"
59 static void AdjustFrame(int scrnIndex
, int x
, int y
, int flags
);
60 static Bool
CloseScreen(int scrnIndex
, ScreenPtr pScreen
);
61 static Bool
EnterVT(int scrnIndex
, int flags
);
62 static Bool
SaveHWState(ScrnInfoPtr pScrn
);
63 static Bool
RestoreHWState(ScrnInfoPtr pScrn
);
66 static ModeStatus
ValidMode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
,
68 static void FreeScreen(int scrnIndex
, int flags
);
69 static void LeaveVT(int scrnIndex
, int flags
);
70 static Bool
SwitchMode(int scrnIndex
, DisplayModePtr mode
, int flags
);
71 static Bool
ScreenInit(int scrnIndex
, ScreenPtr pScreen
, int argc
,
73 static Bool
PreInit(ScrnInfoPtr pScrn
, int flags
);
81 static const OptionInfoRec Options
[] = {
82 {OPTION_SW_CURSOR
, "SWcursor", OPTV_BOOLEAN
, {0}, FALSE
},
83 {OPTION_2D_ACCEL
, "2DAccel", OPTV_BOOLEAN
, {0}, FALSE
},
84 {-1, NULL
, OPTV_NONE
, {0}, FALSE
}
88 * Exported Xorg driver functions to winsys
92 xorg_tracker_available_options(int chipid
, int busid
)
98 xorg_tracker_set_functions(ScrnInfoPtr scrn
)
100 scrn
->PreInit
= PreInit
;
101 scrn
->ScreenInit
= ScreenInit
;
102 scrn
->SwitchMode
= SwitchMode
;
103 scrn
->AdjustFrame
= AdjustFrame
;
104 scrn
->EnterVT
= EnterVT
;
105 scrn
->LeaveVT
= LeaveVT
;
106 scrn
->FreeScreen
= FreeScreen
;
107 scrn
->ValidMode
= ValidMode
;
111 * Static Xorg funtctions
115 GetRec(ScrnInfoPtr pScrn
)
117 if (pScrn
->driverPrivate
)
120 pScrn
->driverPrivate
= xnfcalloc(sizeof(modesettingRec
), 1);
126 FreeRec(ScrnInfoPtr pScrn
)
131 if (!pScrn
->driverPrivate
)
134 xfree(pScrn
->driverPrivate
);
136 pScrn
->driverPrivate
= NULL
;
140 ProbeDDC(ScrnInfoPtr pScrn
, int index
)
142 ConfiguredMonitor
= NULL
;
146 CreateFrontBuffer(ScrnInfoPtr pScrn
)
148 modesettingPtr ms
= modesettingPTR(pScrn
);
149 unsigned handle
, stride
;
150 struct pipe_texture
*tex
;
154 tex
= xorg_exa_create_root_texture(pScrn
, pScrn
->virtualX
, pScrn
->virtualY
,
155 pScrn
->depth
, pScrn
->bitsPerPixel
);
160 if (!ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
,
177 AdjustFrame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
179 pipe_texture_reference(&ms
->root_texture
, tex
);
180 pipe_texture_reference(&tex
, NULL
);
185 BindTextureToRoot(ScrnInfoPtr pScrn
)
187 modesettingPtr ms
= modesettingPTR(pScrn
);
188 ScreenPtr pScreen
= pScrn
->pScreen
;
189 struct pipe_texture
*check
;
190 PixmapPtr rootPixmap
;
192 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
194 xorg_exa_set_displayed_usage(rootPixmap
);
195 xorg_exa_set_shared_usage(rootPixmap
);
196 xorg_exa_set_texture(rootPixmap
, ms
->root_texture
);
197 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, -1, -1, -1, -1, -1, NULL
))
198 FatalError("Couldn't adjust screen pixmap\n");
200 check
= xorg_exa_get_texture(rootPixmap
);
201 if (ms
->root_texture
!= check
)
202 FatalError("Created new root texture\n");
204 pipe_texture_reference(&check
, NULL
);
210 crtc_resize(ScrnInfoPtr pScrn
, int width
, int height
)
212 modesettingPtr ms
= modesettingPTR(pScrn
);
213 unsigned handle
, stride
;
214 PixmapPtr rootPixmap
;
215 ScreenPtr pScreen
= pScrn
->pScreen
;
217 if (width
== pScrn
->virtualX
&& height
== pScrn
->virtualY
)
220 ErrorF("RESIZING TO %dx%d\n", width
, height
);
222 pScrn
->virtualX
= width
;
223 pScrn
->virtualY
= height
;
226 * Remove the old framebuffer & texture.
228 drmModeRmFB(ms
->fd
, ms
->fb_id
);
229 pipe_texture_reference(&ms
->root_texture
, NULL
);
232 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
233 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, width
, height
, -1, -1, -1, NULL
))
237 ms
->root_texture
= xorg_exa_get_texture(rootPixmap
);
239 if (!ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
,
243 FatalError("Could not get handle and stride from texture\n");
254 /* HW dependent - FIXME */
255 pScrn
->displayWidth
= pScrn
->virtualX
;
257 /* now create new frontbuffer */
258 return CreateFrontBuffer(pScrn
) && BindTextureToRoot(pScrn
);
261 static const xf86CrtcConfigFuncsRec crtc_config_funcs
= {
266 InitDRM(ScrnInfoPtr pScrn
)
268 modesettingPtr ms
= modesettingPTR(pScrn
);
270 /* deal with server regeneration */
275 sprintf(BusID
, "PCI:%d:%d:%d",
276 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
277 ms
->PciInfo
->dev
, ms
->PciInfo
->func
280 ms
->fd
= drmOpen(NULL
, BusID
);
287 ms
->api
= drm_api_create();
297 PreInit(ScrnInfoPtr pScrn
, int flags
)
299 xf86CrtcConfigPtr xf86_config
;
301 rgb defaultWeight
= { 0, 0, 0 };
304 int max_width
, max_height
;
306 if (pScrn
->numEntities
!= 1)
309 pEnt
= xf86GetEntityInfo(pScrn
->entityList
[0]);
311 if (flags
& PROBE_DETECT
) {
312 ProbeDDC(pScrn
, pEnt
->index
);
316 /* Allocate driverPrivate */
320 ms
= modesettingPTR(pScrn
);
321 ms
->SaveGeneration
= -1;
324 pScrn
->displayWidth
= 640; /* default it */
326 if (ms
->pEnt
->location
.type
!= BUS_PCI
)
329 ms
->PciInfo
= xf86GetPciInfoForEntity(ms
->pEnt
->index
);
331 /* Allocate an entity private if necessary */
332 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
333 FatalError("Entity");
335 msEnt
= xf86GetEntityPrivate(pScrn
->entityList
[0],
336 modesettingEntityIndex
)->ptr
;
337 ms
->entityPrivate
= msEnt
;
342 ms
->entityPrivate
= NULL
;
344 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
345 if (xf86IsPrimInitDone(pScrn
->entityList
[0])) {
348 xf86SetPrimInitDone(pScrn
->entityList
[0]);
357 pScrn
->monitor
= pScrn
->confScreen
->monitor
;
358 pScrn
->progClock
= TRUE
;
363 PreferConvert24to32
| SupportConvert24to32
| Support32bppFb
))
366 switch (pScrn
->depth
) {
372 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
373 "Given depth (%d) is not supported by the driver\n",
377 xf86PrintDepthBpp(pScrn
);
379 if (!xf86SetWeight(pScrn
, defaultWeight
, defaultWeight
))
381 if (!xf86SetDefaultVisual(pScrn
, -1))
384 /* Process the options */
385 xf86CollectOptions(pScrn
, NULL
);
386 if (!(ms
->Options
= xalloc(sizeof(Options
))))
388 memcpy(ms
->Options
, Options
, sizeof(Options
));
389 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, ms
->Options
);
391 /* Allocate an xf86CrtcConfig */
392 xf86CrtcConfigInit(pScrn
, &crtc_config_funcs
);
393 xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
397 xf86CrtcSetSizeRange(pScrn
, 320, 200, max_width
, max_height
);
399 if (xf86ReturnOptValBool(ms
->Options
, OPTION_SW_CURSOR
, FALSE
)) {
408 if (!xf86InitialConfiguration(pScrn
, TRUE
)) {
409 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No valid modes.\n");
410 RestoreHWState(pScrn
);
414 RestoreHWState(pScrn
);
417 * If the driver can do gamma correction, it should call xf86SetGamma() here.
420 Gamma zeros
= { 0.0, 0.0, 0.0 };
422 if (!xf86SetGamma(pScrn
, zeros
)) {
427 if (pScrn
->modes
== NULL
) {
428 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No modes.\n");
432 pScrn
->currentMode
= pScrn
->modes
;
434 /* Set display resolution */
435 xf86SetDpi(pScrn
, 0, 0);
437 /* Load the required sub modules */
438 if (!xf86LoadSubModule(pScrn
, "fb")) {
442 xf86LoadSubModule(pScrn
, "exa");
445 xf86LoadSubModule(pScrn
, "dri2");
452 SaveHWState(ScrnInfoPtr pScrn
)
454 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
460 RestoreHWState(ScrnInfoPtr pScrn
)
462 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
467 static void xorgBlockHandler(int i
, pointer blockData
, pointer pTimeout
,
470 ScreenPtr pScreen
= screenInfo
.screens
[i
];
471 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
473 pScreen
->BlockHandler
= ms
->blockHandler
;
474 pScreen
->BlockHandler(i
, blockData
, pTimeout
, pReadmask
);
475 pScreen
->BlockHandler
= xorgBlockHandler
;
477 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
479 #ifdef DRM_MODE_FEATURE_DIRTYFB
481 RegionPtr dirty
= DamageRegion(ms
->damage
);
482 unsigned num_cliprects
= REGION_NUM_RECTS(dirty
);
485 drmModeClip
*clip
= alloca(num_cliprects
* sizeof(drmModeClip
));
486 BoxPtr rect
= REGION_RECTS(dirty
);
489 /* XXX no need for copy? */
490 for (i
= 0; i
< num_cliprects
; i
++, rect
++) {
491 clip
[i
].x1
= rect
->x1
;
492 clip
[i
].y1
= rect
->y1
;
493 clip
[i
].x2
= rect
->x2
;
494 clip
[i
].y2
= rect
->y2
;
497 /* TODO query connector property to see if this is needed */
498 drmModeDirtyFB(ms
->fd
, ms
->fb_id
, clip
, num_cliprects
);
500 DamageEmpty(ms
->damage
);
507 CreateScreenResources(ScreenPtr pScreen
)
509 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
510 modesettingPtr ms
= modesettingPTR(pScrn
);
511 PixmapPtr rootPixmap
;
516 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
517 ret
= pScreen
->CreateScreenResources(pScreen
);
518 pScreen
->CreateScreenResources
= CreateScreenResources
;
520 BindTextureToRoot(pScrn
);
524 AdjustFrame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
526 #ifdef DRM_MODE_FEATURE_DIRTYFB
527 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
528 ms
->damage
= DamageCreate(NULL
, NULL
, DamageReportNone
, TRUE
,
529 pScreen
, rootPixmap
);
532 DamageRegister(&rootPixmap
->drawable
, ms
->damage
);
534 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Damage tracking initialized\n");
536 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
537 "Failed to create screen damage record\n");
548 ScreenInit(int scrnIndex
, ScreenPtr pScreen
, int argc
, char **argv
)
550 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
551 modesettingPtr ms
= modesettingPTR(pScrn
);
558 ms
->screen
= ms
->api
->create_screen(ms
->api
, ms
->fd
, NULL
);
561 FatalError("Could not init pipe_screen\n");
566 pScrn
->pScreen
= pScreen
;
568 /* HW dependent - FIXME */
569 pScrn
->displayWidth
= pScrn
->virtualX
;
571 miClearVisualTypes();
573 if (!miSetVisualTypes(pScrn
->depth
,
574 miGetDefaultVisualMask(pScrn
->depth
),
575 pScrn
->rgbBits
, pScrn
->defaultVisual
))
578 if (!miSetPixmapDepths())
581 pScrn
->memPhysBase
= 0;
584 if (!fbScreenInit(pScreen
, NULL
,
585 pScrn
->virtualX
, pScrn
->virtualY
,
586 pScrn
->xDpi
, pScrn
->yDpi
,
587 pScrn
->displayWidth
, pScrn
->bitsPerPixel
))
590 if (pScrn
->bitsPerPixel
> 8) {
591 /* Fixup RGB ordering */
592 visual
= pScreen
->visuals
+ pScreen
->numVisuals
;
593 while (--visual
>= pScreen
->visuals
) {
594 if ((visual
->class | DynamicClass
) == DirectColor
) {
595 visual
->offsetRed
= pScrn
->offset
.red
;
596 visual
->offsetGreen
= pScrn
->offset
.green
;
597 visual
->offsetBlue
= pScrn
->offset
.blue
;
598 visual
->redMask
= pScrn
->mask
.red
;
599 visual
->greenMask
= pScrn
->mask
.green
;
600 visual
->blueMask
= pScrn
->mask
.blue
;
605 fbPictureInit(pScreen
, NULL
, 0);
607 ms
->blockHandler
= pScreen
->BlockHandler
;
608 pScreen
->BlockHandler
= xorgBlockHandler
;
609 ms
->createScreenResources
= pScreen
->CreateScreenResources
;
610 pScreen
->CreateScreenResources
= CreateScreenResources
;
612 xf86SetBlackWhitePixels(pScreen
);
614 ms
->exa
= xorg_exa_init(pScrn
, xf86ReturnOptValBool(ms
->Options
,
615 OPTION_2D_ACCEL
, TRUE
));
616 ms
->debug_fallback
= debug_get_bool_option("XORG_DEBUG_FALLBACK", TRUE
);
618 xorg_init_video(pScreen
);
620 miInitializeBackingStore(pScreen
);
621 xf86SetBackingStore(pScreen
);
622 xf86SetSilkenMouse(pScreen
);
623 miDCInitialize(pScreen
, xf86GetPointerScreenFuncs());
625 /* Need to extend HWcursor support to handle mask interleave */
627 xf86_cursors_init(pScreen
, 64, 64,
628 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64
|
629 HARDWARE_CURSOR_ARGB
);
631 /* Must force it before EnterVT, so we are in control of VT and
632 * later memory should be bound when allocating, e.g rotate_mem */
633 pScrn
->vtSema
= TRUE
;
635 pScreen
->SaveScreen
= xf86SaveScreen
;
636 ms
->CloseScreen
= pScreen
->CloseScreen
;
637 pScreen
->CloseScreen
= CloseScreen
;
639 if (!xf86CrtcScreenInit(pScreen
))
642 if (!miCreateDefColormap(pScreen
))
645 xf86DPMSInit(pScreen
, xf86DPMSSet
, 0);
647 if (serverGeneration
== 1)
648 xf86ShowUnusedOptions(pScrn
->scrnIndex
, pScrn
->options
);
652 driScreenInit(pScreen
);
656 return EnterVT(scrnIndex
, 1);
660 AdjustFrame(int scrnIndex
, int x
, int y
, int flags
)
662 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
663 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
664 xf86OutputPtr output
= config
->output
[config
->compat_output
];
665 xf86CrtcPtr crtc
= output
->crtc
;
667 if (crtc
&& crtc
->enabled
) {
668 crtc
->funcs
->set_mode_major(crtc
, pScrn
->currentMode
,
670 crtc
->x
= output
->initial_x
+ x
;
671 crtc
->y
= output
->initial_y
+ y
;
676 FreeScreen(int scrnIndex
, int flags
)
678 FreeRec(xf86Screens
[scrnIndex
]);
682 LeaveVT(int scrnIndex
, int flags
)
684 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
685 modesettingPtr ms
= modesettingPTR(pScrn
);
686 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
689 for (o
= 0; o
< config
->num_crtc
; o
++) {
690 xf86CrtcPtr crtc
= config
->crtc
[o
];
692 crtc_cursor_destroy(crtc
);
694 if (crtc
->rotatedPixmap
|| crtc
->rotatedData
) {
695 crtc
->funcs
->shadow_destroy(crtc
, crtc
->rotatedPixmap
,
697 crtc
->rotatedPixmap
= NULL
;
698 crtc
->rotatedData
= NULL
;
702 drmModeRmFB(ms
->fd
, ms
->fb_id
);
704 RestoreHWState(pScrn
);
706 if (drmDropMaster(ms
->fd
))
707 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
708 "drmDropMaster failed: %s\n", strerror(errno
));
710 pScrn
->vtSema
= FALSE
;
714 * This gets called when gaining control of the VT, and from ScreenInit().
717 EnterVT(int scrnIndex
, int flags
)
719 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
720 modesettingPtr ms
= modesettingPTR(pScrn
);
722 if (drmSetMaster(ms
->fd
)) {
723 if (errno
== EINVAL
) {
724 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
725 "drmSetMaster failed: 2.6.29 or newer kernel required for "
726 "multi-server DRI\n");
728 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
729 "drmSetMaster failed: %s\n", strerror(errno
));
734 * Only save state once per server generation since that's what most
735 * drivers do. Could change this to save state at each VT enter.
737 if (ms
->SaveGeneration
!= serverGeneration
) {
738 ms
->SaveGeneration
= serverGeneration
;
742 if (!CreateFrontBuffer(pScrn
))
745 if (!flags
&& !BindTextureToRoot(pScrn
))
748 if (!xf86SetDesiredModes(pScrn
))
755 SwitchMode(int scrnIndex
, DisplayModePtr mode
, int flags
)
757 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
759 return xf86SetSingleMode(pScrn
, mode
, RR_Rotate_0
);
763 CloseScreen(int scrnIndex
, ScreenPtr pScreen
)
765 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
766 modesettingPtr ms
= modesettingPTR(pScrn
);
769 LeaveVT(scrnIndex
, 0);
772 driCloseScreen(pScreen
);
775 pScreen
->BlockHandler
= ms
->blockHandler
;
776 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
778 #ifdef DRM_MODE_FEATURE_DIRTYFB
780 DamageUnregister(&pScreen
->GetScreenPixmap(pScreen
)->drawable
, ms
->damage
);
781 DamageDestroy(ms
->damage
);
786 pipe_texture_reference(&ms
->root_texture
, NULL
);
789 xorg_exa_close(pScrn
);
791 if (ms
->api
&& ms
->api
->destroy
)
792 ms
->api
->destroy(ms
->api
);
798 pScrn
->vtSema
= FALSE
;
799 pScreen
->CloseScreen
= ms
->CloseScreen
;
800 return (*pScreen
->CloseScreen
) (scrnIndex
, pScreen
);
804 ValidMode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
, int flags
)
809 /* vim: set sw=4 ts=8 sts=4: */