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"
60 * Functions and symbols exported to Xorg via pointers.
63 static Bool
drv_pre_init(ScrnInfoPtr pScrn
, int flags
);
64 static Bool
drv_screen_init(int scrnIndex
, ScreenPtr pScreen
, int argc
,
66 static Bool
drv_switch_mode(int scrnIndex
, DisplayModePtr mode
, int flags
);
67 static void drv_adjust_frame(int scrnIndex
, int x
, int y
, int flags
);
68 static Bool
drv_enter_vt(int scrnIndex
, int flags
);
69 static void drv_leave_vt(int scrnIndex
, int flags
);
70 static void drv_free_screen(int scrnIndex
, int flags
);
71 static ModeStatus
drv_valid_mode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
,
80 static const OptionInfoRec drv_options
[] = {
81 {OPTION_SW_CURSOR
, "SWcursor", OPTV_BOOLEAN
, {0}, FALSE
},
82 {OPTION_2D_ACCEL
, "2DAccel", OPTV_BOOLEAN
, {0}, FALSE
},
83 {-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
= drv_pre_init
;
101 scrn
->ScreenInit
= drv_screen_init
;
102 scrn
->SwitchMode
= drv_switch_mode
;
103 scrn
->AdjustFrame
= drv_adjust_frame
;
104 scrn
->EnterVT
= drv_enter_vt
;
105 scrn
->LeaveVT
= drv_leave_vt
;
106 scrn
->FreeScreen
= drv_free_screen
;
107 scrn
->ValidMode
= drv_valid_mode
;
112 * Internal function definitions
115 static Bool
drv_close_screen(int scrnIndex
, ScreenPtr pScreen
);
116 static Bool
drv_save_hw_state(ScrnInfoPtr pScrn
);
117 static Bool
drv_restore_hw_state(ScrnInfoPtr pScrn
);
125 drv_get_rec(ScrnInfoPtr pScrn
)
127 if (pScrn
->driverPrivate
)
130 pScrn
->driverPrivate
= xnfcalloc(sizeof(modesettingRec
), 1);
136 drv_free_rec(ScrnInfoPtr pScrn
)
141 if (!pScrn
->driverPrivate
)
144 xfree(pScrn
->driverPrivate
);
146 pScrn
->driverPrivate
= NULL
;
150 drv_probe_ddc(ScrnInfoPtr pScrn
, int index
)
152 ConfiguredMonitor
= NULL
;
156 drv_create_front_buffer(ScrnInfoPtr pScrn
)
158 modesettingPtr ms
= modesettingPTR(pScrn
);
159 unsigned handle
, stride
;
160 struct pipe_texture
*tex
;
164 tex
= xorg_exa_create_root_texture(pScrn
, pScrn
->virtualX
, pScrn
->virtualY
,
165 pScrn
->depth
, pScrn
->bitsPerPixel
);
170 if (!ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
,
187 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
189 pipe_texture_reference(&ms
->root_texture
, tex
);
190 pipe_texture_reference(&tex
, NULL
);
195 drv_bind_texture_to_root(ScrnInfoPtr pScrn
)
197 modesettingPtr ms
= modesettingPTR(pScrn
);
198 ScreenPtr pScreen
= pScrn
->pScreen
;
199 struct pipe_texture
*check
;
200 PixmapPtr rootPixmap
;
202 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
204 xorg_exa_set_displayed_usage(rootPixmap
);
205 xorg_exa_set_shared_usage(rootPixmap
);
206 xorg_exa_set_texture(rootPixmap
, ms
->root_texture
);
207 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, -1, -1, -1, -1, -1, NULL
))
208 FatalError("Couldn't adjust screen pixmap\n");
210 check
= xorg_exa_get_texture(rootPixmap
);
211 if (ms
->root_texture
!= check
)
212 FatalError("Created new root texture\n");
214 pipe_texture_reference(&check
, NULL
);
220 drv_crtc_resize(ScrnInfoPtr pScrn
, int width
, int height
)
222 modesettingPtr ms
= modesettingPTR(pScrn
);
223 unsigned handle
, stride
;
224 PixmapPtr rootPixmap
;
225 ScreenPtr pScreen
= pScrn
->pScreen
;
227 if (width
== pScrn
->virtualX
&& height
== pScrn
->virtualY
)
230 pScrn
->virtualX
= width
;
231 pScrn
->virtualY
= height
;
234 * Remove the old framebuffer & texture.
236 drmModeRmFB(ms
->fd
, ms
->fb_id
);
237 pipe_texture_reference(&ms
->root_texture
, NULL
);
240 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
241 if (!pScreen
->ModifyPixmapHeader(rootPixmap
, width
, height
, -1, -1, -1, NULL
))
245 ms
->root_texture
= xorg_exa_get_texture(rootPixmap
);
247 if (!ms
->api
->local_handle_from_texture(ms
->api
, ms
->screen
,
251 FatalError("Could not get handle and stride from texture\n");
262 /* HW dependent - FIXME */
263 pScrn
->displayWidth
= pScrn
->virtualX
;
265 /* now create new frontbuffer */
266 return drv_create_front_buffer(pScrn
) && drv_bind_texture_to_root(pScrn
);
269 static const xf86CrtcConfigFuncsRec crtc_config_funcs
= {
270 .resize
= drv_crtc_resize
274 drv_init_drm(ScrnInfoPtr pScrn
)
276 modesettingPtr ms
= modesettingPTR(pScrn
);
278 /* deal with server regeneration */
283 sprintf(BusID
, "PCI:%d:%d:%d",
284 ((ms
->PciInfo
->domain
<< 8) | ms
->PciInfo
->bus
),
285 ms
->PciInfo
->dev
, ms
->PciInfo
->func
288 ms
->fd
= drmOpen(NULL
, BusID
);
295 ms
->api
= drm_api_create();
305 drv_pre_init(ScrnInfoPtr pScrn
, int flags
)
307 xf86CrtcConfigPtr xf86_config
;
309 rgb defaultWeight
= { 0, 0, 0 };
312 int max_width
, max_height
;
314 if (pScrn
->numEntities
!= 1)
317 pEnt
= xf86GetEntityInfo(pScrn
->entityList
[0]);
319 if (flags
& PROBE_DETECT
) {
320 drv_probe_ddc(pScrn
, pEnt
->index
);
324 /* Allocate driverPrivate */
325 if (!drv_get_rec(pScrn
))
328 ms
= modesettingPTR(pScrn
);
329 ms
->SaveGeneration
= -1;
332 pScrn
->displayWidth
= 640; /* default it */
334 if (ms
->pEnt
->location
.type
!= BUS_PCI
)
337 ms
->PciInfo
= xf86GetPciInfoForEntity(ms
->pEnt
->index
);
339 /* Allocate an entity private if necessary */
340 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
341 FatalError("Entity");
343 msEnt
= xf86GetEntityPrivate(pScrn
->entityList
[0],
344 modesettingEntityIndex
)->ptr
;
345 ms
->entityPrivate
= msEnt
;
350 ms
->entityPrivate
= NULL
;
352 if (xf86IsEntityShared(pScrn
->entityList
[0])) {
353 if (xf86IsPrimInitDone(pScrn
->entityList
[0])) {
356 xf86SetPrimInitDone(pScrn
->entityList
[0]);
362 if (!drv_init_drm(pScrn
))
365 pScrn
->monitor
= pScrn
->confScreen
->monitor
;
366 pScrn
->progClock
= TRUE
;
371 PreferConvert24to32
| SupportConvert24to32
| Support32bppFb
))
374 switch (pScrn
->depth
) {
380 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
381 "Given depth (%d) is not supported by the driver\n",
385 xf86PrintDepthBpp(pScrn
);
387 if (!xf86SetWeight(pScrn
, defaultWeight
, defaultWeight
))
389 if (!xf86SetDefaultVisual(pScrn
, -1))
392 /* Process the options */
393 xf86CollectOptions(pScrn
, NULL
);
394 if (!(ms
->Options
= xalloc(sizeof(drv_options
))))
396 memcpy(ms
->Options
, drv_options
, sizeof(drv_options
));
397 xf86ProcessOptions(pScrn
->scrnIndex
, pScrn
->options
, ms
->Options
);
399 /* Allocate an xf86CrtcConfig */
400 xf86CrtcConfigInit(pScrn
, &crtc_config_funcs
);
401 xf86_config
= XF86_CRTC_CONFIG_PTR(pScrn
);
405 xf86CrtcSetSizeRange(pScrn
, 320, 200, max_width
, max_height
);
407 if (xf86ReturnOptValBool(ms
->Options
, OPTION_SW_CURSOR
, FALSE
)) {
411 drv_save_hw_state(pScrn
);
413 xorg_crtc_init(pScrn
);
414 xorg_output_init(pScrn
);
416 if (!xf86InitialConfiguration(pScrn
, TRUE
)) {
417 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No valid modes.\n");
418 drv_restore_hw_state(pScrn
);
422 drv_restore_hw_state(pScrn
);
425 * If the driver can do gamma correction, it should call xf86SetGamma() here.
428 Gamma zeros
= { 0.0, 0.0, 0.0 };
430 if (!xf86SetGamma(pScrn
, zeros
)) {
435 if (pScrn
->modes
== NULL
) {
436 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
, "No modes.\n");
440 pScrn
->currentMode
= pScrn
->modes
;
442 /* Set display resolution */
443 xf86SetDpi(pScrn
, 0, 0);
445 /* Load the required sub modules */
446 if (!xf86LoadSubModule(pScrn
, "fb")) {
450 xf86LoadSubModule(pScrn
, "exa");
453 xf86LoadSubModule(pScrn
, "dri2");
460 drv_save_hw_state(ScrnInfoPtr pScrn
)
462 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
468 drv_restore_hw_state(ScrnInfoPtr pScrn
)
470 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
475 static void drv_block_handler(int i
, pointer blockData
, pointer pTimeout
,
478 ScreenPtr pScreen
= screenInfo
.screens
[i
];
479 modesettingPtr ms
= modesettingPTR(xf86Screens
[pScreen
->myNum
]);
481 pScreen
->BlockHandler
= ms
->blockHandler
;
482 pScreen
->BlockHandler(i
, blockData
, pTimeout
, pReadmask
);
483 pScreen
->BlockHandler
= drv_block_handler
;
485 ms
->ctx
->flush(ms
->ctx
, PIPE_FLUSH_RENDER_CACHE
, NULL
);
487 #ifdef DRM_MODE_FEATURE_DIRTYFB
489 RegionPtr dirty
= DamageRegion(ms
->damage
);
490 unsigned num_cliprects
= REGION_NUM_RECTS(dirty
);
493 drmModeClip
*clip
= alloca(num_cliprects
* sizeof(drmModeClip
));
494 BoxPtr rect
= REGION_RECTS(dirty
);
497 /* XXX no need for copy? */
498 for (i
= 0; i
< num_cliprects
; i
++, rect
++) {
499 clip
[i
].x1
= rect
->x1
;
500 clip
[i
].y1
= rect
->y1
;
501 clip
[i
].x2
= rect
->x2
;
502 clip
[i
].y2
= rect
->y2
;
505 /* TODO query connector property to see if this is needed */
506 drmModeDirtyFB(ms
->fd
, ms
->fb_id
, clip
, num_cliprects
);
508 DamageEmpty(ms
->damage
);
515 drv_create_screen_resources(ScreenPtr pScreen
)
517 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
518 modesettingPtr ms
= modesettingPTR(pScrn
);
519 PixmapPtr rootPixmap
;
524 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
525 ret
= pScreen
->CreateScreenResources(pScreen
);
526 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
528 drv_bind_texture_to_root(pScrn
);
532 drv_adjust_frame(pScrn
->scrnIndex
, pScrn
->frameX0
, pScrn
->frameY0
, 0);
534 #ifdef DRM_MODE_FEATURE_DIRTYFB
535 rootPixmap
= pScreen
->GetScreenPixmap(pScreen
);
536 ms
->damage
= DamageCreate(NULL
, NULL
, DamageReportNone
, TRUE
,
537 pScreen
, rootPixmap
);
540 DamageRegister(&rootPixmap
->drawable
, ms
->damage
);
542 xf86DrvMsg(pScrn
->scrnIndex
, X_INFO
, "Damage tracking initialized\n");
544 xf86DrvMsg(pScrn
->scrnIndex
, X_ERROR
,
545 "Failed to create screen damage record\n");
556 drv_screen_init(int scrnIndex
, ScreenPtr pScreen
, int argc
, char **argv
)
558 ScrnInfoPtr pScrn
= xf86Screens
[pScreen
->myNum
];
559 modesettingPtr ms
= modesettingPTR(pScrn
);
562 if (!drv_init_drm(pScrn
))
566 ms
->screen
= ms
->api
->create_screen(ms
->api
, ms
->fd
, NULL
);
569 FatalError("Could not init pipe_screen\n");
574 pScrn
->pScreen
= pScreen
;
576 /* HW dependent - FIXME */
577 pScrn
->displayWidth
= pScrn
->virtualX
;
579 miClearVisualTypes();
581 if (!miSetVisualTypes(pScrn
->depth
,
582 miGetDefaultVisualMask(pScrn
->depth
),
583 pScrn
->rgbBits
, pScrn
->defaultVisual
))
586 if (!miSetPixmapDepths())
589 pScrn
->memPhysBase
= 0;
592 if (!fbScreenInit(pScreen
, NULL
,
593 pScrn
->virtualX
, pScrn
->virtualY
,
594 pScrn
->xDpi
, pScrn
->yDpi
,
595 pScrn
->displayWidth
, pScrn
->bitsPerPixel
))
598 if (pScrn
->bitsPerPixel
> 8) {
599 /* Fixup RGB ordering */
600 visual
= pScreen
->visuals
+ pScreen
->numVisuals
;
601 while (--visual
>= pScreen
->visuals
) {
602 if ((visual
->class | DynamicClass
) == DirectColor
) {
603 visual
->offsetRed
= pScrn
->offset
.red
;
604 visual
->offsetGreen
= pScrn
->offset
.green
;
605 visual
->offsetBlue
= pScrn
->offset
.blue
;
606 visual
->redMask
= pScrn
->mask
.red
;
607 visual
->greenMask
= pScrn
->mask
.green
;
608 visual
->blueMask
= pScrn
->mask
.blue
;
613 fbPictureInit(pScreen
, NULL
, 0);
615 ms
->blockHandler
= pScreen
->BlockHandler
;
616 pScreen
->BlockHandler
= drv_block_handler
;
617 ms
->createScreenResources
= pScreen
->CreateScreenResources
;
618 pScreen
->CreateScreenResources
= drv_create_screen_resources
;
620 xf86SetBlackWhitePixels(pScreen
);
622 ms
->exa
= xorg_exa_init(pScrn
, xf86ReturnOptValBool(ms
->Options
,
623 OPTION_2D_ACCEL
, TRUE
));
624 ms
->debug_fallback
= debug_get_bool_option("XORG_DEBUG_FALLBACK", TRUE
);
626 xorg_xv_init(pScreen
);
628 miInitializeBackingStore(pScreen
);
629 xf86SetBackingStore(pScreen
);
630 xf86SetSilkenMouse(pScreen
);
631 miDCInitialize(pScreen
, xf86GetPointerScreenFuncs());
633 /* Need to extend HWcursor support to handle mask interleave */
635 xf86_cursors_init(pScreen
, 64, 64,
636 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64
|
637 HARDWARE_CURSOR_ARGB
);
639 /* Must force it before EnterVT, so we are in control of VT and
640 * later memory should be bound when allocating, e.g rotate_mem */
641 pScrn
->vtSema
= TRUE
;
643 pScreen
->SaveScreen
= xf86SaveScreen
;
644 ms
->CloseScreen
= pScreen
->CloseScreen
;
645 pScreen
->CloseScreen
= drv_close_screen
;
647 if (!xf86CrtcScreenInit(pScreen
))
650 if (!miCreateDefColormap(pScreen
))
653 xf86DPMSInit(pScreen
, xf86DPMSSet
, 0);
655 if (serverGeneration
== 1)
656 xf86ShowUnusedOptions(pScrn
->scrnIndex
, pScrn
->options
);
659 xorg_dri2_init(pScreen
);
662 return drv_enter_vt(scrnIndex
, 1);
666 drv_adjust_frame(int scrnIndex
, int x
, int y
, int flags
)
668 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
669 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
670 xf86OutputPtr output
= config
->output
[config
->compat_output
];
671 xf86CrtcPtr crtc
= output
->crtc
;
673 if (crtc
&& crtc
->enabled
) {
674 crtc
->funcs
->set_mode_major(crtc
, pScrn
->currentMode
,
676 crtc
->x
= output
->initial_x
+ x
;
677 crtc
->y
= output
->initial_y
+ y
;
682 drv_free_screen(int scrnIndex
, int flags
)
684 drv_free_rec(xf86Screens
[scrnIndex
]);
688 drv_leave_vt(int scrnIndex
, int flags
)
690 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
691 modesettingPtr ms
= modesettingPTR(pScrn
);
692 xf86CrtcConfigPtr config
= XF86_CRTC_CONFIG_PTR(pScrn
);
695 for (o
= 0; o
< config
->num_crtc
; o
++) {
696 xf86CrtcPtr crtc
= config
->crtc
[o
];
698 xorg_crtc_cursor_destroy(crtc
);
700 if (crtc
->rotatedPixmap
|| crtc
->rotatedData
) {
701 crtc
->funcs
->shadow_destroy(crtc
, crtc
->rotatedPixmap
,
703 crtc
->rotatedPixmap
= NULL
;
704 crtc
->rotatedData
= NULL
;
708 drmModeRmFB(ms
->fd
, ms
->fb_id
);
710 drv_restore_hw_state(pScrn
);
712 if (drmDropMaster(ms
->fd
))
713 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
714 "drmDropMaster failed: %s\n", strerror(errno
));
716 pScrn
->vtSema
= FALSE
;
720 * This gets called when gaining control of the VT, and from ScreenInit().
723 drv_enter_vt(int scrnIndex
, int flags
)
725 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
726 modesettingPtr ms
= modesettingPTR(pScrn
);
728 if (drmSetMaster(ms
->fd
)) {
729 if (errno
== EINVAL
) {
730 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
731 "drmSetMaster failed: 2.6.29 or newer kernel required for "
732 "multi-server DRI\n");
734 xf86DrvMsg(pScrn
->scrnIndex
, X_WARNING
,
735 "drmSetMaster failed: %s\n", strerror(errno
));
740 * Only save state once per server generation since that's what most
741 * drivers do. Could change this to save state at each VT enter.
743 if (ms
->SaveGeneration
!= serverGeneration
) {
744 ms
->SaveGeneration
= serverGeneration
;
745 drv_save_hw_state(pScrn
);
748 if (!drv_create_front_buffer(pScrn
))
751 if (!flags
&& !drv_bind_texture_to_root(pScrn
))
754 if (!xf86SetDesiredModes(pScrn
))
761 drv_switch_mode(int scrnIndex
, DisplayModePtr mode
, int flags
)
763 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
765 return xf86SetSingleMode(pScrn
, mode
, RR_Rotate_0
);
769 drv_close_screen(int scrnIndex
, ScreenPtr pScreen
)
771 ScrnInfoPtr pScrn
= xf86Screens
[scrnIndex
];
772 modesettingPtr ms
= modesettingPTR(pScrn
);
775 drv_leave_vt(scrnIndex
, 0);
778 xorg_dri2_close(pScreen
);
781 pScreen
->BlockHandler
= ms
->blockHandler
;
782 pScreen
->CreateScreenResources
= ms
->createScreenResources
;
784 #ifdef DRM_MODE_FEATURE_DIRTYFB
786 DamageUnregister(&pScreen
->GetScreenPixmap(pScreen
)->drawable
, ms
->damage
);
787 DamageDestroy(ms
->damage
);
792 pipe_texture_reference(&ms
->root_texture
, NULL
);
795 xorg_exa_close(pScrn
);
797 if (ms
->api
&& ms
->api
->destroy
)
798 ms
->api
->destroy(ms
->api
);
804 pScrn
->vtSema
= FALSE
;
805 pScreen
->CloseScreen
= ms
->CloseScreen
;
806 return (*pScreen
->CloseScreen
) (scrnIndex
, pScreen
);
810 drv_valid_mode(int scrnIndex
, DisplayModePtr mode
, Bool verbose
, int flags
)
815 /* vim: set sw=4 ts=8 sts=4: */