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
);
80 static const OptionInfoRec Options
[] = {
81 {OPTION_SW_CURSOR
, "SWcursor", OPTV_BOOLEAN
, {0}, FALSE
},
82 {-1, NULL
, OPTV_NONE
, {0}, FALSE
}
86 * Exported Xorg driver functions to winsys
90 xorg_tracker_available_options(int chipid
, int busid
)
96 xorg_tracker_set_functions(ScrnInfoPtr scrn
)
98 scrn
->PreInit
= PreInit
;
99 scrn
->ScreenInit
= ScreenInit
;
100 scrn
->SwitchMode
= SwitchMode
;
101 scrn
->AdjustFrame
= AdjustFrame
;
102 scrn
->EnterVT
= EnterVT
;
103 scrn
->LeaveVT
= LeaveVT
;
104 scrn
->FreeScreen
= FreeScreen
;
105 scrn
->ValidMode
= ValidMode
;
109 * Static Xorg funtctions
113 GetRec(ScrnInfoPtr pScrn
)
115 if (pScrn
->driverPrivate
)
118 pScrn
->driverPrivate
= xnfcalloc(sizeof(modesettingRec
), 1);
124 FreeRec(ScrnInfoPtr pScrn
)
129 if (!pScrn
->driverPrivate
)
132 xfree(pScrn
->driverPrivate
);
134 pScrn
->driverPrivate
= NULL
;
138 ProbeDDC(ScrnInfoPtr pScrn
, int index
)
140 ConfiguredMonitor
= NULL
;
144 CreateFrontBuffer(ScrnInfoPtr pScrn
)
146 modesettingPtr ms
= modesettingPTR(pScrn
);
147 unsigned handle
, stride
;
148 struct pipe_texture
*tex
;
152 tex
= xorg_exa_create_root_texture(pScrn
, pScrn
->virtualX
, pScrn
->virtualY
,
153 pScrn
->depth
, pScrn
->bitsPerPixel
);
158 if (!ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
,
175 AdjustFrame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
177 pipe_texture_reference(&ms
->root_texture
, tex
);
178 pipe_texture_reference(&tex
, NULL
);
183 BindTextureToRoot(ScrnInfoPtr pScrn
)
185 modesettingPtr ms
= modesettingPTR(pScrn
);
186 ScreenPtr pScreen
= pScrn
->pScreen
;
187 struct pipe_texture
*check
;
188 PixmapPtr rootPixmap
;
190 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
192 xorg_exa_set_displayed_usage(rootPixmap
);
193 xorg_exa_set_shared_usage(rootPixmap
);
194 xorg_exa_set_texture(rootPixmap
, ms
->root_texture
);
195 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, -1, -1, -1, -1, -1, NULL
))
196 FatalError("Couldn't adjust screen pixmap\n");
198 check
= xorg_exa_get_texture(rootPixmap
);
199 if (ms
->root_texture
!= check
)
200 FatalError("Created new root texture\n");
202 pipe_texture_reference(&check
, NULL
);
208 crtc_resize(ScrnInfoPtr pScrn
, int width
, int height
)
210 modesettingPtr ms
= modesettingPTR(pScrn
);
211 unsigned handle
, stride
;
212 PixmapPtr rootPixmap
;
213 ScreenPtr pScreen
= pScrn
->pScreen
;
215 if (width
== pScrn
->virtualX
&& height
== pScrn
->virtualY
)
218 ErrorF("RESIZING TO %dx%d\n", width
, height
);
220 pScrn
->virtualX
= width
;
221 pScrn
->virtualY
= height
;
224 * Remove the old framebuffer & texture.
226 drmModeRmFB(ms
->fd
, ms
->fb_id
);
227 pipe_texture_reference(&ms
->root_texture
, NULL
);
230 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
231 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, width
, height
, -1, -1, -1, NULL
))
235 ms
->root_texture
= xorg_exa_get_texture(rootPixmap
);
237 if (!ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
,
241 FatalError("Could not get handle and stride from texture\n");
252 /* HW dependent - FIXME */
253 pScrn
->displayWidth
= pScrn
->virtualX
;
255 /* now create new frontbuffer */
256 return CreateFrontBuffer(pScrn
) && BindTextureToRoot(pScrn
);
259 static const xf86CrtcConfigFuncsRec crtc_config_funcs
= {
264 InitDRM(ScrnInfoPtr pScrn
)
266 modesettingPtr ms
= modesettingPTR(pScrn
);
268 /* deal with server regeneration */
273 sprintf(BusID
, "PCI:%d:%d:%d",
274 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
275 ms
->PciInfo
->dev
, ms
->PciInfo
->func
278 ms
->fd
= drmOpen(NULL
, BusID
);
285 ms
->api
= drm_api_create();
295 PreInit(ScrnInfoPtr pScrn
, int flags
)
297 xf86CrtcConfigPtr xf86_config
;
299 rgb defaultWeight
= { 0, 0, 0 };
302 int max_width
, max_height
;
304 if (pScrn
->numEntities
!= 1)
307 pEnt
= xf86GetEntityInfo(pScrn
->entityList
[0]);
309 if (flags
& PROBE_DETECT
) {
310 ProbeDDC(pScrn
, pEnt
->index
);
314 /* Allocate driverPrivate */
318 ms
= modesettingPTR(pScrn
);
319 ms
->SaveGeneration
= -1;
322 pScrn
->displayWidth
= 640; /* default it */
324 if (ms
->pEnt
->location
.type
!= BUS_PCI
)
327 ms
->PciInfo
= xf86GetPciInfoForEntity(ms
->pEnt
->index
);
329 /* Allocate an entity private if necessary */
330 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
331 FatalError("Entity");
333 msEnt
= xf86GetEntityPrivate(pScrn
->entityList
[0],
334 modesettingEntityIndex
)->ptr
;
335 ms
->entityPrivate
= msEnt
;
340 ms
->entityPrivate
= NULL
;
342 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
343 if (xf86IsPrimInitDone(pScrn
->entityList
[0])) {
346 xf86SetPrimInitDone(pScrn
->entityList
[0]);
355 pScrn
->monitor
= pScrn
->confScreen
->monitor
;
356 pScrn
->progClock
= TRUE
;
361 PreferConvert24to32
| SupportConvert24to32
| Support32bppFb
))
364 switch (pScrn
->depth
) {
370 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
371 "Given depth (%d) is not supported by the driver\n",
375 xf86PrintDepthBpp(pScrn
);
377 if (!xf86SetWeight(pScrn
, defaultWeight
, defaultWeight
))
379 if (!xf86SetDefaultVisual(pScrn
, -1))
382 /* Process the options */
383 xf86CollectOptions(pScrn
, NULL
);
384 if (!(ms
->Options
= xalloc(sizeof(Options
))))
386 memcpy(ms
->Options
, Options
, sizeof(Options
));
387 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, ms
->Options
);
389 /* Allocate an xf86CrtcConfig */
390 xf86CrtcConfigInit(pScrn
, &crtc_config_funcs
);
391 xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
395 xf86CrtcSetSizeRange(pScrn
, 320, 200, max_width
, max_height
);
397 if (xf86ReturnOptValBool(ms
->Options
, OPTION_SW_CURSOR
, FALSE
)) {
406 if (!xf86InitialConfiguration(pScrn
, TRUE
)) {
407 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No valid modes.\n");
408 RestoreHWState(pScrn
);
412 RestoreHWState(pScrn
);
415 * If the driver can do gamma correction, it should call xf86SetGamma() here.
418 Gamma zeros
= { 0.0, 0.0, 0.0 };
420 if (!xf86SetGamma(pScrn
, zeros
)) {
425 if (pScrn
->modes
== NULL
) {
426 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No modes.\n");
430 pScrn
->currentMode
= pScrn
->modes
;
432 /* Set display resolution */
433 xf86SetDpi(pScrn
, 0, 0);
435 /* Load the required sub modules */
436 if (!xf86LoadSubModule(pScrn
, "fb")) {
440 xf86LoadSubModule(pScrn
, "exa");
443 xf86LoadSubModule(pScrn
, "dri2");
450 SaveHWState(ScrnInfoPtr pScrn
)
452 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
458 RestoreHWState(ScrnInfoPtr pScrn
)
460 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
465 static void xorgBlockHandler(int i
, pointer blockData
, pointer pTimeout
,
468 ScreenPtr pScreen
= screenInfo
.screens
[i
];
469 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
471 pScreen
->BlockHandler
= ms
->blockHandler
;
472 pScreen
->BlockHandler(i
, blockData
, pTimeout
, pReadmask
);
473 pScreen
->BlockHandler
= xorgBlockHandler
;
475 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
477 #ifdef DRM_MODE_FEATURE_DIRTYFB
479 RegionPtr dirty
= DamageRegion(ms
->damage
);
480 unsigned num_cliprects
= REGION_NUM_RECTS(dirty
);
483 drmModeClip
*clip
= alloca(num_cliprects
* sizeof(drmModeClip
));
484 BoxPtr rect
= REGION_RECTS(dirty
);
487 /* XXX no need for copy? */
488 for (i
= 0; i
< num_cliprects
; i
++, rect
++) {
489 clip
[i
].x1
= rect
->x1
;
490 clip
[i
].y1
= rect
->y1
;
491 clip
[i
].x2
= rect
->x2
;
492 clip
[i
].y2
= rect
->y2
;
495 /* TODO query connector property to see if this is needed */
496 drmModeDirtyFB(ms
->fd
, ms
->fb_id
, clip
, num_cliprects
);
498 DamageEmpty(ms
->damage
);
505 CreateScreenResources(ScreenPtr pScreen
)
507 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
508 modesettingPtr ms
= modesettingPTR(pScrn
);
509 PixmapPtr rootPixmap
;
514 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
515 ret
= pScreen
->CreateScreenResources(pScreen
);
516 pScreen
->CreateScreenResources
= CreateScreenResources
;
518 BindTextureToRoot(pScrn
);
522 AdjustFrame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
524 #ifdef DRM_MODE_FEATURE_DIRTYFB
525 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
526 ms
->damage
= DamageCreate(NULL
, NULL
, DamageReportNone
, TRUE
,
527 pScreen
, rootPixmap
);
530 DamageRegister(&rootPixmap
->drawable
, ms
->damage
);
532 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Damage tracking initialized\n");
534 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
535 "Failed to create screen damage record\n");
546 ScreenInit(int scrnIndex
, ScreenPtr pScreen
, int argc
, char **argv
)
548 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
549 modesettingPtr ms
= modesettingPTR(pScrn
);
556 ms
->screen
= ms
->api
->create_screen(ms
->api
, ms
->fd
, NULL
);
559 FatalError("Could not init pipe_screen\n");
564 pScrn
->pScreen
= pScreen
;
566 /* HW dependent - FIXME */
567 pScrn
->displayWidth
= pScrn
->virtualX
;
569 miClearVisualTypes();
571 if (!miSetVisualTypes(pScrn
->depth
,
572 miGetDefaultVisualMask(pScrn
->depth
),
573 pScrn
->rgbBits
, pScrn
->defaultVisual
))
576 if (!miSetPixmapDepths())
579 pScrn
->memPhysBase
= 0;
582 if (!fbScreenInit(pScreen
, NULL
,
583 pScrn
->virtualX
, pScrn
->virtualY
,
584 pScrn
->xDpi
, pScrn
->yDpi
,
585 pScrn
->displayWidth
, pScrn
->bitsPerPixel
))
588 if (pScrn
->bitsPerPixel
> 8) {
589 /* Fixup RGB ordering */
590 visual
= pScreen
->visuals
+ pScreen
->numVisuals
;
591 while (--visual
>= pScreen
->visuals
) {
592 if ((visual
->class | DynamicClass
) == DirectColor
) {
593 visual
->offsetRed
= pScrn
->offset
.red
;
594 visual
->offsetGreen
= pScrn
->offset
.green
;
595 visual
->offsetBlue
= pScrn
->offset
.blue
;
596 visual
->redMask
= pScrn
->mask
.red
;
597 visual
->greenMask
= pScrn
->mask
.green
;
598 visual
->blueMask
= pScrn
->mask
.blue
;
603 fbPictureInit(pScreen
, NULL
, 0);
605 ms
->blockHandler
= pScreen
->BlockHandler
;
606 pScreen
->BlockHandler
= xorgBlockHandler
;
607 ms
->createScreenResources
= pScreen
->CreateScreenResources
;
608 pScreen
->CreateScreenResources
= CreateScreenResources
;
610 xf86SetBlackWhitePixels(pScreen
);
612 ms
->exa
= xorg_exa_init(pScrn
);
613 ms
->debug_fallback
= debug_get_bool_option("XORG_DEBUG_FALLBACK", TRUE
);
615 xorg_init_video(pScreen
);
617 miInitializeBackingStore(pScreen
);
618 xf86SetBackingStore(pScreen
);
619 xf86SetSilkenMouse(pScreen
);
620 miDCInitialize(pScreen
, xf86GetPointerScreenFuncs());
622 /* Need to extend HWcursor support to handle mask interleave */
624 xf86_cursors_init(pScreen
, 64, 64,
625 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64
|
626 HARDWARE_CURSOR_ARGB
);
628 /* Must force it before EnterVT, so we are in control of VT and
629 * later memory should be bound when allocating, e.g rotate_mem */
630 pScrn
->vtSema
= TRUE
;
632 pScreen
->SaveScreen
= xf86SaveScreen
;
633 ms
->CloseScreen
= pScreen
->CloseScreen
;
634 pScreen
->CloseScreen
= CloseScreen
;
636 if (!xf86CrtcScreenInit(pScreen
))
639 if (!miCreateDefColormap(pScreen
))
642 xf86DPMSInit(pScreen
, xf86DPMSSet
, 0);
644 if (serverGeneration
== 1)
645 xf86ShowUnusedOptions(pScrn
->scrnIndex
, pScrn
->options
);
649 driScreenInit(pScreen
);
653 return EnterVT(scrnIndex
, 1);
657 AdjustFrame(int scrnIndex
, int x
, int y
, int flags
)
659 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
660 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
661 xf86OutputPtr output
= config
->output
[config
->compat_output
];
662 xf86CrtcPtr crtc
= output
->crtc
;
664 if (crtc
&& crtc
->enabled
) {
665 crtc
->funcs
->set_mode_major(crtc
, pScrn
->currentMode
,
667 crtc
->x
= output
->initial_x
+ x
;
668 crtc
->y
= output
->initial_y
+ y
;
673 FreeScreen(int scrnIndex
, int flags
)
675 FreeRec(xf86Screens
[scrnIndex
]);
679 LeaveVT(int scrnIndex
, int flags
)
681 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
682 modesettingPtr ms
= modesettingPTR(pScrn
);
683 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
686 for (o
= 0; o
< config
->num_crtc
; o
++) {
687 xf86CrtcPtr crtc
= config
->crtc
[o
];
689 crtc_cursor_destroy(crtc
);
691 if (crtc
->rotatedPixmap
|| crtc
->rotatedData
) {
692 crtc
->funcs
->shadow_destroy(crtc
, crtc
->rotatedPixmap
,
694 crtc
->rotatedPixmap
= NULL
;
695 crtc
->rotatedData
= NULL
;
699 drmModeRmFB(ms
->fd
, ms
->fb_id
);
701 RestoreHWState(pScrn
);
703 if (drmDropMaster(ms
->fd
))
704 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
705 "drmDropMaster failed: %s\n", strerror(errno
));
707 pScrn
->vtSema
= FALSE
;
711 * This gets called when gaining control of the VT, and from ScreenInit().
714 EnterVT(int scrnIndex
, int flags
)
716 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
717 modesettingPtr ms
= modesettingPTR(pScrn
);
719 if (drmSetMaster(ms
->fd
)) {
720 if (errno
== EINVAL
) {
721 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
722 "drmSetMaster failed: 2.6.29 or newer kernel required for "
723 "multi-server DRI\n");
725 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
726 "drmSetMaster failed: %s\n", strerror(errno
));
731 * Only save state once per server generation since that's what most
732 * drivers do. Could change this to save state at each VT enter.
734 if (ms
->SaveGeneration
!= serverGeneration
) {
735 ms
->SaveGeneration
= serverGeneration
;
739 if (!CreateFrontBuffer(pScrn
))
742 if (!flags
&& !BindTextureToRoot(pScrn
))
745 if (!xf86SetDesiredModes(pScrn
))
752 SwitchMode(int scrnIndex
, DisplayModePtr mode
, int flags
)
754 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
756 return xf86SetSingleMode(pScrn
, mode
, RR_Rotate_0
);
760 CloseScreen(int scrnIndex
, ScreenPtr pScreen
)
762 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
763 modesettingPtr ms
= modesettingPTR(pScrn
);
766 LeaveVT(scrnIndex
, 0);
769 driCloseScreen(pScreen
);
772 pScreen
->BlockHandler
= ms
->blockHandler
;
773 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
775 #ifdef DRM_MODE_FEATURE_DIRTYFB
777 DamageUnregister(&pScreen
->GetScreenPixmap(pScreen
)->drawable
, ms
->damage
);
778 DamageDestroy(ms
->damage
);
783 pipe_texture_reference(&ms
->root_texture
, NULL
);
786 xorg_exa_close(pScrn
);
788 if (ms
->api
&& ms
->api
->destroy
)
789 ms
->api
->destroy(ms
->api
);
795 pScrn
->vtSema
= FALSE
;
796 pScreen
->CloseScreen
= ms
->CloseScreen
;
797 return (*pScreen
->CloseScreen
) (scrnIndex
, pScreen
);
801 ValidMode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
, int flags
)
806 /* vim: set sw=4 ts=8 sts=4: */