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 PreInit(ScrnInfoPtr pScrn
, int flags
)
266 xf86CrtcConfigPtr xf86_config
;
268 rgb defaultWeight
= { 0, 0, 0 };
272 int max_width
, max_height
;
274 if (pScrn
->numEntities
!= 1)
277 pEnt
= xf86GetEntityInfo(pScrn
->entityList
[0]);
279 if (flags
& PROBE_DETECT
) {
280 ProbeDDC(pScrn
, pEnt
->index
);
284 /* Allocate driverPrivate */
288 ms
= modesettingPTR(pScrn
);
289 ms
->SaveGeneration
= -1;
292 pScrn
->displayWidth
= 640; /* default it */
294 if (ms
->pEnt
->location
.type
!= BUS_PCI
)
297 ms
->PciInfo
= xf86GetPciInfoForEntity(ms
->pEnt
->index
);
299 /* Allocate an entity private if necessary */
300 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
301 FatalError("Entity");
303 msEnt
= xf86GetEntityPrivate(pScrn
->entityList
[0],
304 modesettingEntityIndex
)->ptr
;
305 ms
->entityPrivate
= msEnt
;
310 ms
->entityPrivate
= NULL
;
312 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
313 if (xf86IsPrimInitDone(pScrn
->entityList
[0])) {
316 xf86SetPrimInitDone(pScrn
->entityList
[0]);
321 sprintf(BusID
, "PCI:%d:%d:%d",
322 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
323 ms
->PciInfo
->dev
, ms
->PciInfo
->func
326 ms
->api
= drm_api_create();
327 ms
->fd
= drmOpen(NULL
, BusID
);
332 pScrn
->monitor
= pScrn
->confScreen
->monitor
;
333 pScrn
->progClock
= TRUE
;
338 PreferConvert24to32
| SupportConvert24to32
| Support32bppFb
))
341 switch (pScrn
->depth
) {
347 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
348 "Given depth (%d) is not supported by the driver\n",
352 xf86PrintDepthBpp(pScrn
);
354 if (!xf86SetWeight(pScrn
, defaultWeight
, defaultWeight
))
356 if (!xf86SetDefaultVisual(pScrn
, -1))
359 /* Process the options */
360 xf86CollectOptions(pScrn
, NULL
);
361 if (!(ms
->Options
= xalloc(sizeof(Options
))))
363 memcpy(ms
->Options
, Options
, sizeof(Options
));
364 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, ms
->Options
);
366 /* Allocate an xf86CrtcConfig */
367 xf86CrtcConfigInit(pScrn
, &crtc_config_funcs
);
368 xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
372 xf86CrtcSetSizeRange(pScrn
, 320, 200, max_width
, max_height
);
374 if (xf86ReturnOptValBool(ms
->Options
, OPTION_SW_CURSOR
, FALSE
)) {
383 if (!xf86InitialConfiguration(pScrn
, TRUE
)) {
384 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No valid modes.\n");
385 RestoreHWState(pScrn
);
389 RestoreHWState(pScrn
);
392 * If the driver can do gamma correction, it should call xf86SetGamma() here.
395 Gamma zeros
= { 0.0, 0.0, 0.0 };
397 if (!xf86SetGamma(pScrn
, zeros
)) {
402 if (pScrn
->modes
== NULL
) {
403 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No modes.\n");
407 pScrn
->currentMode
= pScrn
->modes
;
409 /* Set display resolution */
410 xf86SetDpi(pScrn
, 0, 0);
412 /* Load the required sub modules */
413 if (!xf86LoadSubModule(pScrn
, "fb")) {
417 xf86LoadSubModule(pScrn
, "exa");
420 xf86LoadSubModule(pScrn
, "dri2");
427 SaveHWState(ScrnInfoPtr pScrn
)
429 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
435 RestoreHWState(ScrnInfoPtr pScrn
)
437 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
442 static void xorgBlockHandler(int i
, pointer blockData
, pointer pTimeout
,
445 ScreenPtr pScreen
= screenInfo
.screens
[i
];
446 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
448 pScreen
->BlockHandler
= ms
->blockHandler
;
449 pScreen
->BlockHandler(i
, blockData
, pTimeout
, pReadmask
);
450 pScreen
->BlockHandler
= xorgBlockHandler
;
452 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
454 #ifdef DRM_MODE_FEATURE_DIRTYFB
456 RegionPtr dirty
= DamageRegion(ms
->damage
);
457 unsigned num_cliprects
= REGION_NUM_RECTS(dirty
);
460 drmModeClip
*clip
= alloca(num_cliprects
* sizeof(drmModeClip
));
461 BoxPtr rect
= REGION_RECTS(dirty
);
464 for (i
= 0; i
< num_cliprects
; i
++, rect
++) {
465 clip
[i
].x
= rect
->x1
;
466 clip
[i
].y
= rect
->y1
;
467 clip
[i
].width
= rect
->x2
- rect
->x1
;
468 clip
[i
].height
= rect
->y2
- rect
->y1
;
471 /* TODO query connector property to see if this is needed */
472 drmModeDirtyFB(ms
->fd
, ms
->fb_id
, clip
, num_cliprects
);
474 DamageEmpty(ms
->damage
);
481 CreateScreenResources(ScreenPtr pScreen
)
483 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
484 modesettingPtr ms
= modesettingPTR(pScrn
);
485 PixmapPtr rootPixmap
;
490 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
491 ret
= pScreen
->CreateScreenResources(pScreen
);
492 pScreen
->CreateScreenResources
= CreateScreenResources
;
494 BindTextureToRoot(pScrn
);
498 AdjustFrame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
500 #ifdef DRM_MODE_FEATURE_DIRTYFB
501 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
502 ms
->damage
= DamageCreate(NULL
, NULL
, DamageReportNone
, TRUE
,
503 pScreen
, rootPixmap
);
506 DamageRegister(&rootPixmap
->drawable
, ms
->damage
);
508 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Damage tracking initialized\n");
510 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
511 "Failed to create screen damage record\n");
522 ScreenInit(int scrnIndex
, ScreenPtr pScreen
, int argc
, char **argv
)
524 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
525 modesettingPtr ms
= modesettingPTR(pScrn
);
528 /* deal with server regeneration */
533 sprintf(BusID
, "PCI:%d:%d:%d",
534 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
535 ms
->PciInfo
->dev
, ms
->PciInfo
->func
538 ms
->fd
= drmOpen(NULL
, BusID
);
545 ms
->screen
= ms
->api
->create_screen(ms
->api
, ms
->fd
, NULL
);
548 FatalError("Could not init pipe_screen\n");
553 pScrn
->pScreen
= pScreen
;
555 /* HW dependent - FIXME */
556 pScrn
->displayWidth
= pScrn
->virtualX
;
558 miClearVisualTypes();
560 if (!miSetVisualTypes(pScrn
->depth
,
561 miGetDefaultVisualMask(pScrn
->depth
),
562 pScrn
->rgbBits
, pScrn
->defaultVisual
))
565 if (!miSetPixmapDepths())
568 pScrn
->memPhysBase
= 0;
571 if (!fbScreenInit(pScreen
, NULL
,
572 pScrn
->virtualX
, pScrn
->virtualY
,
573 pScrn
->xDpi
, pScrn
->yDpi
,
574 pScrn
->displayWidth
, pScrn
->bitsPerPixel
))
577 if (pScrn
->bitsPerPixel
> 8) {
578 /* Fixup RGB ordering */
579 visual
= pScreen
->visuals
+ pScreen
->numVisuals
;
580 while (--visual
>= pScreen
->visuals
) {
581 if ((visual
->class | DynamicClass
) == DirectColor
) {
582 visual
->offsetRed
= pScrn
->offset
.red
;
583 visual
->offsetGreen
= pScrn
->offset
.green
;
584 visual
->offsetBlue
= pScrn
->offset
.blue
;
585 visual
->redMask
= pScrn
->mask
.red
;
586 visual
->greenMask
= pScrn
->mask
.green
;
587 visual
->blueMask
= pScrn
->mask
.blue
;
592 fbPictureInit(pScreen
, NULL
, 0);
594 ms
->blockHandler
= pScreen
->BlockHandler
;
595 pScreen
->BlockHandler
= xorgBlockHandler
;
596 ms
->createScreenResources
= pScreen
->CreateScreenResources
;
597 pScreen
->CreateScreenResources
= CreateScreenResources
;
599 xf86SetBlackWhitePixels(pScreen
);
601 ms
->exa
= xorg_exa_init(pScrn
);
602 ms
->debug_fallback
= debug_get_bool_option("XORG_DEBUG_FALLBACK", TRUE
);
604 xorg_init_video(pScreen
);
606 miInitializeBackingStore(pScreen
);
607 xf86SetBackingStore(pScreen
);
608 xf86SetSilkenMouse(pScreen
);
609 miDCInitialize(pScreen
, xf86GetPointerScreenFuncs());
611 /* Need to extend HWcursor support to handle mask interleave */
613 xf86_cursors_init(pScreen
, 64, 64,
614 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64
|
615 HARDWARE_CURSOR_ARGB
);
617 /* Must force it before EnterVT, so we are in control of VT and
618 * later memory should be bound when allocating, e.g rotate_mem */
619 pScrn
->vtSema
= TRUE
;
621 pScreen
->SaveScreen
= xf86SaveScreen
;
622 ms
->CloseScreen
= pScreen
->CloseScreen
;
623 pScreen
->CloseScreen
= CloseScreen
;
625 if (!xf86CrtcScreenInit(pScreen
))
628 if (!miCreateDefColormap(pScreen
))
631 xf86DPMSInit(pScreen
, xf86DPMSSet
, 0);
633 if (serverGeneration
== 1)
634 xf86ShowUnusedOptions(pScrn
->scrnIndex
, pScrn
->options
);
638 driScreenInit(pScreen
);
642 return EnterVT(scrnIndex
, 1);
646 AdjustFrame(int scrnIndex
, int x
, int y
, int flags
)
648 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
649 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
650 xf86OutputPtr output
= config
->output
[config
->compat_output
];
651 xf86CrtcPtr crtc
= output
->crtc
;
653 if (crtc
&& crtc
->enabled
) {
654 crtc
->funcs
->set_mode_major(crtc
, pScrn
->currentMode
,
656 crtc
->x
= output
->initial_x
+ x
;
657 crtc
->y
= output
->initial_y
+ y
;
662 FreeScreen(int scrnIndex
, int flags
)
664 FreeRec(xf86Screens
[scrnIndex
]);
668 LeaveVT(int scrnIndex
, int flags
)
670 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
671 modesettingPtr ms
= modesettingPTR(pScrn
);
672 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
675 for (o
= 0; o
< config
->num_crtc
; o
++) {
676 xf86CrtcPtr crtc
= config
->crtc
[o
];
678 crtc_cursor_destroy(crtc
);
680 if (crtc
->rotatedPixmap
|| crtc
->rotatedData
) {
681 crtc
->funcs
->shadow_destroy(crtc
, crtc
->rotatedPixmap
,
683 crtc
->rotatedPixmap
= NULL
;
684 crtc
->rotatedData
= NULL
;
688 drmModeRmFB(ms
->fd
, ms
->fb_id
);
690 RestoreHWState(pScrn
);
692 if (drmDropMaster(ms
->fd
))
693 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
694 "drmDropMaster failed: %s\n", strerror(errno
));
696 pScrn
->vtSema
= FALSE
;
700 * This gets called when gaining control of the VT, and from ScreenInit().
703 EnterVT(int scrnIndex
, int flags
)
705 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
706 modesettingPtr ms
= modesettingPTR(pScrn
);
708 if (drmSetMaster(ms
->fd
)) {
709 if (errno
== EINVAL
) {
710 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
711 "drmSetMaster failed: 2.6.29 or newer kernel required for "
712 "multi-server DRI\n");
714 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
715 "drmSetMaster failed: %s\n", strerror(errno
));
720 * Only save state once per server generation since that's what most
721 * drivers do. Could change this to save state at each VT enter.
723 if (ms
->SaveGeneration
!= serverGeneration
) {
724 ms
->SaveGeneration
= serverGeneration
;
728 if (!CreateFrontBuffer(pScrn
))
731 if (!flags
&& !BindTextureToRoot(pScrn
))
734 if (!xf86SetDesiredModes(pScrn
))
741 SwitchMode(int scrnIndex
, DisplayModePtr mode
, int flags
)
743 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
745 return xf86SetSingleMode(pScrn
, mode
, RR_Rotate_0
);
749 CloseScreen(int scrnIndex
, ScreenPtr pScreen
)
751 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
752 modesettingPtr ms
= modesettingPTR(pScrn
);
755 LeaveVT(scrnIndex
, 0);
758 driCloseScreen(pScreen
);
761 pScreen
->BlockHandler
= ms
->blockHandler
;
762 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
764 #ifdef DRM_MODE_FEATURE_DIRTYFB
766 DamageUnregister(&pScreen
->GetScreenPixmap(pScreen
)->drawable
, ms
->damage
);
767 DamageDestroy(ms
->damage
);
772 pipe_texture_reference(&ms
->root_texture
, NULL
);
775 xorg_exa_close(pScrn
);
777 if (ms
->api
->destroy
)
778 ms
->api
->destroy(ms
->api
);
784 pScrn
->vtSema
= FALSE
;
785 pScreen
->CloseScreen
= ms
->CloseScreen
;
786 return (*pScreen
->CloseScreen
) (scrnIndex
, pScreen
);
790 ValidMode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
, int flags
)
795 /* vim: set sw=4 ts=8 sts=4: */