Merge commit 'origin/st-shader-varients'
[mesa.git] / src / gallium / state_trackers / xorg / xorg_driver.c
1 /*
2 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 * All Rights Reserved.
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 *
25 *
26 * Author: Alan Hourihane <alanh@tungstengraphics.com>
27 * Author: Jakob Bornecrantz <wallbraker@gmail.com>
28 *
29 */
30
31
32 #include "xorg-server.h"
33 #include "xf86.h"
34 #include "xf86_OSproc.h"
35 #include "compiler.h"
36 #include "xf86PciInfo.h"
37 #include "xf86Pci.h"
38 #include "mipointer.h"
39 #include "micmap.h"
40 #include <X11/extensions/randr.h>
41 #include "fb.h"
42 #include "edid.h"
43 #include "xf86i2c.h"
44 #include "xf86Crtc.h"
45 #include "miscstruct.h"
46 #include "dixstruct.h"
47 #include "xf86xv.h"
48 #include <X11/extensions/Xv.h>
49 #ifndef XSERVER_LIBPCIACCESS
50 #error "libpciaccess needed"
51 #endif
52
53 #include <pciaccess.h>
54
55 #include "pipe/p_context.h"
56 #include "xorg_tracker.h"
57 #include "xorg_winsys.h"
58
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);
64
65
66 static ModeStatus ValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose,
67 int flags);
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,
72 char **argv);
73 static Bool PreInit(ScrnInfoPtr pScrn, int flags);
74
75 typedef enum
76 {
77 OPTION_SW_CURSOR,
78 OPTION_2D_ACCEL,
79 } modesettingOpts;
80
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}
85 };
86
87 /*
88 * Exported Xorg driver functions to winsys
89 */
90
91 const OptionInfoRec *
92 xorg_tracker_available_options(int chipid, int busid)
93 {
94 return Options;
95 }
96
97 void
98 xorg_tracker_set_functions(ScrnInfoPtr scrn)
99 {
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;
108 }
109
110 /*
111 * Static Xorg funtctions
112 */
113
114 static Bool
115 GetRec(ScrnInfoPtr pScrn)
116 {
117 if (pScrn->driverPrivate)
118 return TRUE;
119
120 pScrn->driverPrivate = xnfcalloc(sizeof(modesettingRec), 1);
121
122 return TRUE;
123 }
124
125 static void
126 FreeRec(ScrnInfoPtr pScrn)
127 {
128 if (!pScrn)
129 return;
130
131 if (!pScrn->driverPrivate)
132 return;
133
134 xfree(pScrn->driverPrivate);
135
136 pScrn->driverPrivate = NULL;
137 }
138
139 static void
140 ProbeDDC(ScrnInfoPtr pScrn, int index)
141 {
142 ConfiguredMonitor = NULL;
143 }
144
145 static Bool
146 CreateFrontBuffer(ScrnInfoPtr pScrn)
147 {
148 modesettingPtr ms = modesettingPTR(pScrn);
149 unsigned handle, stride;
150 struct pipe_texture *tex;
151
152 ms->noEvict = TRUE;
153
154 tex = xorg_exa_create_root_texture(pScrn, pScrn->virtualX, pScrn->virtualY,
155 pScrn->depth, pScrn->bitsPerPixel);
156
157 if (!tex)
158 return FALSE;
159
160 if (!ms->api->local_handle_from_texture(ms->api, ms->screen,
161 tex,
162 &stride,
163 &handle))
164 return FALSE;
165
166 drmModeAddFB(ms->fd,
167 pScrn->virtualX,
168 pScrn->virtualY,
169 pScrn->depth,
170 pScrn->bitsPerPixel,
171 stride,
172 handle,
173 &ms->fb_id);
174
175 pScrn->frameX0 = 0;
176 pScrn->frameY0 = 0;
177 AdjustFrame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
178
179 pipe_texture_reference(&ms->root_texture, tex);
180 pipe_texture_reference(&tex, NULL);
181 return TRUE;
182 }
183
184 static Bool
185 BindTextureToRoot(ScrnInfoPtr pScrn)
186 {
187 modesettingPtr ms = modesettingPTR(pScrn);
188 ScreenPtr pScreen = pScrn->pScreen;
189 struct pipe_texture *check;
190 PixmapPtr rootPixmap;
191
192 rootPixmap = pScreen->GetScreenPixmap(pScreen);
193
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");
199
200 check = xorg_exa_get_texture(rootPixmap);
201 if (ms->root_texture != check)
202 FatalError("Created new root texture\n");
203
204 pipe_texture_reference(&check, NULL);
205
206 return TRUE;
207 }
208
209 static Bool
210 crtc_resize(ScrnInfoPtr pScrn, int width, int height)
211 {
212 modesettingPtr ms = modesettingPTR(pScrn);
213 unsigned handle, stride;
214 PixmapPtr rootPixmap;
215 ScreenPtr pScreen = pScrn->pScreen;
216
217 if (width == pScrn->virtualX && height == pScrn->virtualY)
218 return TRUE;
219
220 ErrorF("RESIZING TO %dx%d\n", width, height);
221
222 pScrn->virtualX = width;
223 pScrn->virtualY = height;
224
225 /*
226 * Remove the old framebuffer & texture.
227 */
228 drmModeRmFB(ms->fd, ms->fb_id);
229 pipe_texture_reference(&ms->root_texture, NULL);
230
231
232 rootPixmap = pScreen->GetScreenPixmap(pScreen);
233 if (!pScreen->ModifyPixmapHeader(rootPixmap, width, height, -1, -1, -1, NULL))
234 return FALSE;
235
236 /* takes one ref */
237 ms->root_texture = xorg_exa_get_texture(rootPixmap);
238
239 if (!ms->api->local_handle_from_texture(ms->api, ms->screen,
240 ms->root_texture,
241 &stride,
242 &handle))
243 FatalError("Could not get handle and stride from texture\n");
244
245 drmModeAddFB(ms->fd,
246 pScrn->virtualX,
247 pScrn->virtualY,
248 pScrn->depth,
249 pScrn->bitsPerPixel,
250 stride,
251 handle,
252 &ms->fb_id);
253
254 /* HW dependent - FIXME */
255 pScrn->displayWidth = pScrn->virtualX;
256
257 /* now create new frontbuffer */
258 return CreateFrontBuffer(pScrn) && BindTextureToRoot(pScrn);
259 }
260
261 static const xf86CrtcConfigFuncsRec crtc_config_funcs = {
262 crtc_resize
263 };
264
265 static Bool
266 InitDRM(ScrnInfoPtr pScrn)
267 {
268 modesettingPtr ms = modesettingPTR(pScrn);
269
270 /* deal with server regeneration */
271 if (ms->fd < 0) {
272 char *BusID;
273
274 BusID = xalloc(64);
275 sprintf(BusID, "PCI:%d:%d:%d",
276 ((ms->PciInfo->domain << 8) | ms->PciInfo->bus),
277 ms->PciInfo->dev, ms->PciInfo->func
278 );
279
280 ms->fd = drmOpen(NULL, BusID);
281
282 if (ms->fd < 0)
283 return FALSE;
284 }
285
286 if (!ms->api) {
287 ms->api = drm_api_create();
288
289 if (!ms->api)
290 return FALSE;
291 }
292
293 return TRUE;
294 }
295
296 static Bool
297 PreInit(ScrnInfoPtr pScrn, int flags)
298 {
299 xf86CrtcConfigPtr xf86_config;
300 modesettingPtr ms;
301 rgb defaultWeight = { 0, 0, 0 };
302 EntityInfoPtr pEnt;
303 EntPtr msEnt = NULL;
304 int max_width, max_height;
305
306 if (pScrn->numEntities != 1)
307 return FALSE;
308
309 pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
310
311 if (flags & PROBE_DETECT) {
312 ProbeDDC(pScrn, pEnt->index);
313 return TRUE;
314 }
315
316 /* Allocate driverPrivate */
317 if (!GetRec(pScrn))
318 return FALSE;
319
320 ms = modesettingPTR(pScrn);
321 ms->SaveGeneration = -1;
322 ms->pEnt = pEnt;
323
324 pScrn->displayWidth = 640; /* default it */
325
326 if (ms->pEnt->location.type != BUS_PCI)
327 return FALSE;
328
329 ms->PciInfo = xf86GetPciInfoForEntity(ms->pEnt->index);
330
331 /* Allocate an entity private if necessary */
332 if (xf86IsEntityShared(pScrn->entityList[0])) {
333 FatalError("Entity");
334 #if 0
335 msEnt = xf86GetEntityPrivate(pScrn->entityList[0],
336 modesettingEntityIndex)->ptr;
337 ms->entityPrivate = msEnt;
338 #else
339 (void)msEnt;
340 #endif
341 } else
342 ms->entityPrivate = NULL;
343
344 if (xf86IsEntityShared(pScrn->entityList[0])) {
345 if (xf86IsPrimInitDone(pScrn->entityList[0])) {
346 /* do something */
347 } else {
348 xf86SetPrimInitDone(pScrn->entityList[0]);
349 }
350 }
351
352 ms->fd = -1;
353 ms->api = NULL;
354 if (!InitDRM(pScrn))
355 return FALSE;
356
357 pScrn->monitor = pScrn->confScreen->monitor;
358 pScrn->progClock = TRUE;
359 pScrn->rgbBits = 8;
360
361 if (!xf86SetDepthBpp
362 (pScrn, 0, 0, 0,
363 PreferConvert24to32 | SupportConvert24to32 | Support32bppFb))
364 return FALSE;
365
366 switch (pScrn->depth) {
367 case 15:
368 case 16:
369 case 24:
370 break;
371 default:
372 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
373 "Given depth (%d) is not supported by the driver\n",
374 pScrn->depth);
375 return FALSE;
376 }
377 xf86PrintDepthBpp(pScrn);
378
379 if (!xf86SetWeight(pScrn, defaultWeight, defaultWeight))
380 return FALSE;
381 if (!xf86SetDefaultVisual(pScrn, -1))
382 return FALSE;
383
384 /* Process the options */
385 xf86CollectOptions(pScrn, NULL);
386 if (!(ms->Options = xalloc(sizeof(Options))))
387 return FALSE;
388 memcpy(ms->Options, Options, sizeof(Options));
389 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->Options);
390
391 /* Allocate an xf86CrtcConfig */
392 xf86CrtcConfigInit(pScrn, &crtc_config_funcs);
393 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
394
395 max_width = 8192;
396 max_height = 8192;
397 xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, max_height);
398
399 if (xf86ReturnOptValBool(ms->Options, OPTION_SW_CURSOR, FALSE)) {
400 ms->SWCursor = TRUE;
401 }
402
403 SaveHWState(pScrn);
404
405 crtc_init(pScrn);
406 output_init(pScrn);
407
408 if (!xf86InitialConfiguration(pScrn, TRUE)) {
409 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes.\n");
410 RestoreHWState(pScrn);
411 return FALSE;
412 }
413
414 RestoreHWState(pScrn);
415
416 /*
417 * If the driver can do gamma correction, it should call xf86SetGamma() here.
418 */
419 {
420 Gamma zeros = { 0.0, 0.0, 0.0 };
421
422 if (!xf86SetGamma(pScrn, zeros)) {
423 return FALSE;
424 }
425 }
426
427 if (pScrn->modes == NULL) {
428 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
429 return FALSE;
430 }
431
432 pScrn->currentMode = pScrn->modes;
433
434 /* Set display resolution */
435 xf86SetDpi(pScrn, 0, 0);
436
437 /* Load the required sub modules */
438 if (!xf86LoadSubModule(pScrn, "fb")) {
439 return FALSE;
440 }
441
442 xf86LoadSubModule(pScrn, "exa");
443
444 #ifdef DRI2
445 xf86LoadSubModule(pScrn, "dri2");
446 #endif
447
448 return TRUE;
449 }
450
451 static Bool
452 SaveHWState(ScrnInfoPtr pScrn)
453 {
454 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
455
456 return TRUE;
457 }
458
459 static Bool
460 RestoreHWState(ScrnInfoPtr pScrn)
461 {
462 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
463
464 return TRUE;
465 }
466
467 static void xorgBlockHandler(int i, pointer blockData, pointer pTimeout,
468 pointer pReadmask)
469 {
470 ScreenPtr pScreen = screenInfo.screens[i];
471 modesettingPtr ms = modesettingPTR(xf86Screens[pScreen->myNum]);
472
473 pScreen->BlockHandler = ms->blockHandler;
474 pScreen->BlockHandler(i, blockData, pTimeout, pReadmask);
475 pScreen->BlockHandler = xorgBlockHandler;
476
477 ms->ctx->flush(ms->ctx, PIPE_FLUSH_RENDER_CACHE, NULL);
478
479 #ifdef DRM_MODE_FEATURE_DIRTYFB
480 {
481 RegionPtr dirty = DamageRegion(ms->damage);
482 unsigned num_cliprects = REGION_NUM_RECTS(dirty);
483
484 if (num_cliprects) {
485 drmModeClip *clip = alloca(num_cliprects * sizeof(drmModeClip));
486 BoxPtr rect = REGION_RECTS(dirty);
487 int i;
488
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;
495 }
496
497 /* TODO query connector property to see if this is needed */
498 drmModeDirtyFB(ms->fd, ms->fb_id, clip, num_cliprects);
499
500 DamageEmpty(ms->damage);
501 }
502 }
503 #endif
504 }
505
506 static Bool
507 CreateScreenResources(ScreenPtr pScreen)
508 {
509 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
510 modesettingPtr ms = modesettingPTR(pScrn);
511 PixmapPtr rootPixmap;
512 Bool ret;
513
514 ms->noEvict = TRUE;
515
516 pScreen->CreateScreenResources = ms->createScreenResources;
517 ret = pScreen->CreateScreenResources(pScreen);
518 pScreen->CreateScreenResources = CreateScreenResources;
519
520 BindTextureToRoot(pScrn);
521
522 ms->noEvict = FALSE;
523
524 AdjustFrame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
525
526 #ifdef DRM_MODE_FEATURE_DIRTYFB
527 rootPixmap = pScreen->GetScreenPixmap(pScreen);
528 ms->damage = DamageCreate(NULL, NULL, DamageReportNone, TRUE,
529 pScreen, rootPixmap);
530
531 if (ms->damage) {
532 DamageRegister(&rootPixmap->drawable, ms->damage);
533
534 xf86DrvMsg(pScrn->scrnIndex, X_INFO, "Damage tracking initialized\n");
535 } else {
536 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
537 "Failed to create screen damage record\n");
538 return FALSE;
539 }
540 #else
541 (void)rootPixmap;
542 #endif
543
544 return ret;
545 }
546
547 static Bool
548 ScreenInit(int scrnIndex, ScreenPtr pScreen, int argc, char **argv)
549 {
550 ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
551 modesettingPtr ms = modesettingPTR(pScrn);
552 VisualPtr visual;
553
554 if (!InitDRM(pScrn))
555 return FALSE;
556
557 if (!ms->screen) {
558 ms->screen = ms->api->create_screen(ms->api, ms->fd, NULL);
559
560 if (!ms->screen) {
561 FatalError("Could not init pipe_screen\n");
562 return FALSE;
563 }
564 }
565
566 pScrn->pScreen = pScreen;
567
568 /* HW dependent - FIXME */
569 pScrn->displayWidth = pScrn->virtualX;
570
571 miClearVisualTypes();
572
573 if (!miSetVisualTypes(pScrn->depth,
574 miGetDefaultVisualMask(pScrn->depth),
575 pScrn->rgbBits, pScrn->defaultVisual))
576 return FALSE;
577
578 if (!miSetPixmapDepths())
579 return FALSE;
580
581 pScrn->memPhysBase = 0;
582 pScrn->fbOffset = 0;
583
584 if (!fbScreenInit(pScreen, NULL,
585 pScrn->virtualX, pScrn->virtualY,
586 pScrn->xDpi, pScrn->yDpi,
587 pScrn->displayWidth, pScrn->bitsPerPixel))
588 return FALSE;
589
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;
601 }
602 }
603 }
604
605 fbPictureInit(pScreen, NULL, 0);
606
607 ms->blockHandler = pScreen->BlockHandler;
608 pScreen->BlockHandler = xorgBlockHandler;
609 ms->createScreenResources = pScreen->CreateScreenResources;
610 pScreen->CreateScreenResources = CreateScreenResources;
611
612 xf86SetBlackWhitePixels(pScreen);
613
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);
617
618 xorg_init_video(pScreen);
619
620 miInitializeBackingStore(pScreen);
621 xf86SetBackingStore(pScreen);
622 xf86SetSilkenMouse(pScreen);
623 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
624
625 /* Need to extend HWcursor support to handle mask interleave */
626 if (!ms->SWCursor)
627 xf86_cursors_init(pScreen, 64, 64,
628 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 |
629 HARDWARE_CURSOR_ARGB);
630
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;
634
635 pScreen->SaveScreen = xf86SaveScreen;
636 ms->CloseScreen = pScreen->CloseScreen;
637 pScreen->CloseScreen = CloseScreen;
638
639 if (!xf86CrtcScreenInit(pScreen))
640 return FALSE;
641
642 if (!miCreateDefColormap(pScreen))
643 return FALSE;
644
645 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
646
647 if (serverGeneration == 1)
648 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
649
650 #if 1
651 #ifdef DRI2
652 driScreenInit(pScreen);
653 #endif
654 #endif
655
656 return EnterVT(scrnIndex, 1);
657 }
658
659 static void
660 AdjustFrame(int scrnIndex, int x, int y, int flags)
661 {
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;
666
667 if (crtc && crtc->enabled) {
668 crtc->funcs->set_mode_major(crtc, pScrn->currentMode,
669 RR_Rotate_0, x, y);
670 crtc->x = output->initial_x + x;
671 crtc->y = output->initial_y + y;
672 }
673 }
674
675 static void
676 FreeScreen(int scrnIndex, int flags)
677 {
678 FreeRec(xf86Screens[scrnIndex]);
679 }
680
681 static void
682 LeaveVT(int scrnIndex, int flags)
683 {
684 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
685 modesettingPtr ms = modesettingPTR(pScrn);
686 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
687 int o;
688
689 for (o = 0; o < config->num_crtc; o++) {
690 xf86CrtcPtr crtc = config->crtc[o];
691
692 crtc_cursor_destroy(crtc);
693
694 if (crtc->rotatedPixmap || crtc->rotatedData) {
695 crtc->funcs->shadow_destroy(crtc, crtc->rotatedPixmap,
696 crtc->rotatedData);
697 crtc->rotatedPixmap = NULL;
698 crtc->rotatedData = NULL;
699 }
700 }
701
702 drmModeRmFB(ms->fd, ms->fb_id);
703
704 RestoreHWState(pScrn);
705
706 if (drmDropMaster(ms->fd))
707 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
708 "drmDropMaster failed: %s\n", strerror(errno));
709
710 pScrn->vtSema = FALSE;
711 }
712
713 /*
714 * This gets called when gaining control of the VT, and from ScreenInit().
715 */
716 static Bool
717 EnterVT(int scrnIndex, int flags)
718 {
719 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
720 modesettingPtr ms = modesettingPTR(pScrn);
721
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");
727 } else {
728 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
729 "drmSetMaster failed: %s\n", strerror(errno));
730 }
731 }
732
733 /*
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.
736 */
737 if (ms->SaveGeneration != serverGeneration) {
738 ms->SaveGeneration = serverGeneration;
739 SaveHWState(pScrn);
740 }
741
742 if (!CreateFrontBuffer(pScrn))
743 return FALSE;
744
745 if (!flags && !BindTextureToRoot(pScrn))
746 return FALSE;
747
748 if (!xf86SetDesiredModes(pScrn))
749 return FALSE;
750
751 return TRUE;
752 }
753
754 static Bool
755 SwitchMode(int scrnIndex, DisplayModePtr mode, int flags)
756 {
757 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
758
759 return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
760 }
761
762 static Bool
763 CloseScreen(int scrnIndex, ScreenPtr pScreen)
764 {
765 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
766 modesettingPtr ms = modesettingPTR(pScrn);
767
768 if (pScrn->vtSema) {
769 LeaveVT(scrnIndex, 0);
770 }
771 #ifdef DRI2
772 driCloseScreen(pScreen);
773 #endif
774
775 pScreen->BlockHandler = ms->blockHandler;
776 pScreen->CreateScreenResources = ms->createScreenResources;
777
778 #ifdef DRM_MODE_FEATURE_DIRTYFB
779 if (ms->damage) {
780 DamageUnregister(&pScreen->GetScreenPixmap(pScreen)->drawable, ms->damage);
781 DamageDestroy(ms->damage);
782 ms->damage = NULL;
783 }
784 #endif
785
786 pipe_texture_reference(&ms->root_texture, NULL);
787
788 if (ms->exa)
789 xorg_exa_close(pScrn);
790
791 if (ms->api && ms->api->destroy)
792 ms->api->destroy(ms->api);
793 ms->api = NULL;
794
795 drmClose(ms->fd);
796 ms->fd = -1;
797
798 pScrn->vtSema = FALSE;
799 pScreen->CloseScreen = ms->CloseScreen;
800 return (*pScreen->CloseScreen) (scrnIndex, pScreen);
801 }
802
803 static ModeStatus
804 ValidMode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
805 {
806 return MODE_OK;
807 }
808
809 /* vim: set sw=4 ts=8 sts=4: */