Merge branch 'mesa_7_7_branch'
[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 #ifdef HAVE_LIBKMS
60 #include "libkms.h"
61 #endif
62
63 /*
64 * Functions and symbols exported to Xorg via pointers.
65 */
66
67 static Bool drv_pre_init(ScrnInfoPtr pScrn, int flags);
68 static Bool drv_screen_init(int scrnIndex, ScreenPtr pScreen, int argc,
69 char **argv);
70 static Bool drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags);
71 static void drv_adjust_frame(int scrnIndex, int x, int y, int flags);
72 static Bool drv_enter_vt(int scrnIndex, int flags);
73 static void drv_leave_vt(int scrnIndex, int flags);
74 static void drv_free_screen(int scrnIndex, int flags);
75 static ModeStatus drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose,
76 int flags);
77
78 typedef enum
79 {
80 OPTION_SW_CURSOR,
81 OPTION_2D_ACCEL,
82 } drv_option_enums;
83
84 static const OptionInfoRec drv_options[] = {
85 {OPTION_SW_CURSOR, "SWcursor", OPTV_BOOLEAN, {0}, FALSE},
86 {OPTION_2D_ACCEL, "2DAccel", OPTV_BOOLEAN, {0}, FALSE},
87 {-1, NULL, OPTV_NONE, {0}, FALSE}
88 };
89
90
91 /*
92 * Exported Xorg driver functions to winsys
93 */
94
95 const OptionInfoRec *
96 xorg_tracker_available_options(int chipid, int busid)
97 {
98 return drv_options;
99 }
100
101 void
102 xorg_tracker_set_functions(ScrnInfoPtr scrn)
103 {
104 scrn->PreInit = drv_pre_init;
105 scrn->ScreenInit = drv_screen_init;
106 scrn->SwitchMode = drv_switch_mode;
107 scrn->AdjustFrame = drv_adjust_frame;
108 scrn->EnterVT = drv_enter_vt;
109 scrn->LeaveVT = drv_leave_vt;
110 scrn->FreeScreen = drv_free_screen;
111 scrn->ValidMode = drv_valid_mode;
112 }
113
114
115 /*
116 * Internal function definitions
117 */
118
119 static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn);
120 static Bool drv_close_screen(int scrnIndex, ScreenPtr pScreen);
121 static Bool drv_save_hw_state(ScrnInfoPtr pScrn);
122 static Bool drv_restore_hw_state(ScrnInfoPtr pScrn);
123
124
125 /*
126 * Internal functions
127 */
128
129 static Bool
130 drv_get_rec(ScrnInfoPtr pScrn)
131 {
132 if (pScrn->driverPrivate)
133 return TRUE;
134
135 pScrn->driverPrivate = xnfcalloc(sizeof(modesettingRec), 1);
136
137 return TRUE;
138 }
139
140 static void
141 drv_free_rec(ScrnInfoPtr pScrn)
142 {
143 if (!pScrn)
144 return;
145
146 if (!pScrn->driverPrivate)
147 return;
148
149 xfree(pScrn->driverPrivate);
150
151 pScrn->driverPrivate = NULL;
152 }
153
154 static void
155 drv_probe_ddc(ScrnInfoPtr pScrn, int index)
156 {
157 ConfiguredMonitor = NULL;
158 }
159
160 static Bool
161 drv_crtc_resize(ScrnInfoPtr pScrn, int width, int height)
162 {
163 modesettingPtr ms = modesettingPTR(pScrn);
164 PixmapPtr rootPixmap;
165 ScreenPtr pScreen = pScrn->pScreen;
166
167 if (width == pScrn->virtualX && height == pScrn->virtualY)
168 return TRUE;
169
170 pScrn->virtualX = width;
171 pScrn->virtualY = height;
172
173 /*
174 * Remove the old framebuffer & texture.
175 */
176 drmModeRmFB(ms->fd, ms->fb_id);
177 if (!ms->destroy_front_buffer(pScrn))
178 FatalError("failed to destroy front buffer\n");
179
180 rootPixmap = pScreen->GetScreenPixmap(pScreen);
181 if (!pScreen->ModifyPixmapHeader(rootPixmap, width, height, -1, -1, -1, NULL))
182 return FALSE;
183
184 /* HW dependent - FIXME */
185 pScrn->displayWidth = pScrn->virtualX;
186
187 /* now create new frontbuffer */
188 return ms->create_front_buffer(pScrn) && ms->bind_front_buffer(pScrn);
189 }
190
191 static const xf86CrtcConfigFuncsRec crtc_config_funcs = {
192 .resize = drv_crtc_resize
193 };
194
195 static Bool
196 drv_init_drm(ScrnInfoPtr pScrn)
197 {
198 modesettingPtr ms = modesettingPTR(pScrn);
199
200 /* deal with server regeneration */
201 if (ms->fd < 0) {
202 char *BusID;
203
204 BusID = xalloc(64);
205 sprintf(BusID, "PCI:%d:%d:%d",
206 ((ms->PciInfo->domain << 8) | ms->PciInfo->bus),
207 ms->PciInfo->dev, ms->PciInfo->func
208 );
209
210 ms->fd = drmOpen(NULL, BusID);
211
212 if (ms->fd < 0)
213 return FALSE;
214 }
215
216 return TRUE;
217 }
218
219 static Bool
220 drv_init_resource_management(ScrnInfoPtr pScrn)
221 {
222 modesettingPtr ms = modesettingPTR(pScrn);
223
224 if (ms->screen || ms->kms)
225 return TRUE;
226
227 ms->api = drm_api_create();
228 if (ms->api) {
229 ms->screen = ms->api->create_screen(ms->api, ms->fd, NULL);
230
231 if (ms->screen)
232 return TRUE;
233
234 if (ms->api->destroy)
235 ms->api->destroy(ms->api);
236
237 ms->api = NULL;
238 }
239
240 #ifdef HAVE_LIBKMS
241 if (!kms_create(ms->fd, &ms->kms))
242 return TRUE;
243 #endif
244
245 return FALSE;
246 }
247
248 static Bool
249 drv_close_resource_management(ScrnInfoPtr pScrn)
250 {
251 modesettingPtr ms = modesettingPTR(pScrn);
252
253 if (ms->screen)
254 ms->screen->destroy(ms->screen);
255 ms->screen = NULL;
256
257 if (ms->api && ms->api->destroy)
258 ms->api->destroy(ms->api);
259 ms->api = NULL;
260
261 #ifdef HAVE_LIBKMS
262 if (ms->kms)
263 kms_destroy(&ms->kms);
264 #endif
265
266 return TRUE;
267 }
268
269 static Bool
270 drv_pre_init(ScrnInfoPtr pScrn, int flags)
271 {
272 xf86CrtcConfigPtr xf86_config;
273 modesettingPtr ms;
274 rgb defaultWeight = { 0, 0, 0 };
275 EntityInfoPtr pEnt;
276 EntPtr msEnt = NULL;
277 int max_width, max_height;
278
279 if (pScrn->numEntities != 1)
280 return FALSE;
281
282 pEnt = xf86GetEntityInfo(pScrn->entityList[0]);
283
284 if (flags & PROBE_DETECT) {
285 drv_probe_ddc(pScrn, pEnt->index);
286 return TRUE;
287 }
288
289 /* Allocate driverPrivate */
290 if (!drv_get_rec(pScrn))
291 return FALSE;
292
293 ms = modesettingPTR(pScrn);
294 ms->SaveGeneration = -1;
295 ms->pEnt = pEnt;
296
297 pScrn->displayWidth = 640; /* default it */
298
299 if (ms->pEnt->location.type != BUS_PCI)
300 return FALSE;
301
302 ms->PciInfo = xf86GetPciInfoForEntity(ms->pEnt->index);
303
304 /* Allocate an entity private if necessary */
305 if (xf86IsEntityShared(pScrn->entityList[0])) {
306 FatalError("Entity");
307 #if 0
308 msEnt = xf86GetEntityPrivate(pScrn->entityList[0],
309 modesettingEntityIndex)->ptr;
310 ms->entityPrivate = msEnt;
311 #else
312 (void)msEnt;
313 #endif
314 } else
315 ms->entityPrivate = NULL;
316
317 if (xf86IsEntityShared(pScrn->entityList[0])) {
318 if (xf86IsPrimInitDone(pScrn->entityList[0])) {
319 /* do something */
320 } else {
321 xf86SetPrimInitDone(pScrn->entityList[0]);
322 }
323 }
324
325 ms->fd = -1;
326 ms->api = NULL;
327 if (!drv_init_drm(pScrn))
328 return FALSE;
329
330 pScrn->monitor = pScrn->confScreen->monitor;
331 pScrn->progClock = TRUE;
332 pScrn->rgbBits = 8;
333
334 if (!xf86SetDepthBpp
335 (pScrn, 0, 0, 0,
336 PreferConvert24to32 | SupportConvert24to32 | Support32bppFb))
337 return FALSE;
338
339 switch (pScrn->depth) {
340 case 15:
341 case 16:
342 case 24:
343 break;
344 default:
345 xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
346 "Given depth (%d) is not supported by the driver\n",
347 pScrn->depth);
348 return FALSE;
349 }
350 xf86PrintDepthBpp(pScrn);
351
352 if (!xf86SetWeight(pScrn, defaultWeight, defaultWeight))
353 return FALSE;
354 if (!xf86SetDefaultVisual(pScrn, -1))
355 return FALSE;
356
357 /* Process the options */
358 xf86CollectOptions(pScrn, NULL);
359 if (!(ms->Options = xalloc(sizeof(drv_options))))
360 return FALSE;
361 memcpy(ms->Options, drv_options, sizeof(drv_options));
362 xf86ProcessOptions(pScrn->scrnIndex, pScrn->options, ms->Options);
363
364 /* Allocate an xf86CrtcConfig */
365 xf86CrtcConfigInit(pScrn, &crtc_config_funcs);
366 xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);
367
368 max_width = 8192;
369 max_height = 8192;
370 xf86CrtcSetSizeRange(pScrn, 320, 200, max_width, max_height);
371
372 if (xf86ReturnOptValBool(ms->Options, OPTION_SW_CURSOR, FALSE)) {
373 ms->SWCursor = TRUE;
374 }
375
376 drv_save_hw_state(pScrn);
377
378 xorg_crtc_init(pScrn);
379 xorg_output_init(pScrn);
380
381 if (!xf86InitialConfiguration(pScrn, TRUE)) {
382 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No valid modes.\n");
383 drv_restore_hw_state(pScrn);
384 return FALSE;
385 }
386
387 drv_restore_hw_state(pScrn);
388
389 /*
390 * If the driver can do gamma correction, it should call xf86SetGamma() here.
391 */
392 {
393 Gamma zeros = { 0.0, 0.0, 0.0 };
394
395 if (!xf86SetGamma(pScrn, zeros)) {
396 return FALSE;
397 }
398 }
399
400 if (pScrn->modes == NULL) {
401 xf86DrvMsg(pScrn->scrnIndex, X_ERROR, "No modes.\n");
402 return FALSE;
403 }
404
405 pScrn->currentMode = pScrn->modes;
406
407 /* Set display resolution */
408 xf86SetDpi(pScrn, 0, 0);
409
410 /* Load the required sub modules */
411 if (!xf86LoadSubModule(pScrn, "fb"))
412 return FALSE;
413
414 /* XXX: these aren't needed when we are using libkms */
415 if (!xf86LoadSubModule(pScrn, "exa"))
416 return FALSE;
417
418 #ifdef DRI2
419 if (!xf86LoadSubModule(pScrn, "dri2"))
420 return FALSE;
421 #endif
422
423 return TRUE;
424 }
425
426 static Bool
427 drv_save_hw_state(ScrnInfoPtr pScrn)
428 {
429 /*xf86CrtcConfigPtr xf86_config = XF86_CRTC_CONFIG_PTR(pScrn);*/
430
431 return TRUE;
432 }
433
434 static Bool
435 drv_restore_hw_state(ScrnInfoPtr pScrn)
436 {
437 /*xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);*/
438
439 return TRUE;
440 }
441
442 static void drv_block_handler(int i, pointer blockData, pointer pTimeout,
443 pointer pReadmask)
444 {
445 ScreenPtr pScreen = screenInfo.screens[i];
446 modesettingPtr ms = modesettingPTR(xf86Screens[pScreen->myNum]);
447
448 pScreen->BlockHandler = ms->blockHandler;
449 pScreen->BlockHandler(i, blockData, pTimeout, pReadmask);
450 pScreen->BlockHandler = drv_block_handler;
451
452 if (ms->ctx) {
453 int j;
454
455 ms->ctx->flush(ms->ctx, PIPE_FLUSH_RENDER_CACHE, &ms->fence[XORG_NR_FENCES-1]);
456
457 if (ms->fence[0])
458 ms->ctx->screen->fence_finish(ms->ctx->screen, ms->fence[0], 0);
459
460 /* The amount of rendering generated by a block handler can be
461 * quite small. Let us get a fair way ahead of hardware before
462 * throttling.
463 */
464 for (j = 0; j < XORG_NR_FENCES; j++)
465 ms->screen->fence_reference(ms->screen,
466 &ms->fence[j],
467 ms->fence[j+1]);
468
469 ms->screen->fence_reference(ms->screen,
470 &ms->fence[XORG_NR_FENCES-1],
471 NULL);
472 }
473
474
475 #ifdef DRM_MODE_FEATURE_DIRTYFB
476 {
477 RegionPtr dirty = DamageRegion(ms->damage);
478 unsigned num_cliprects = REGION_NUM_RECTS(dirty);
479
480 if (num_cliprects) {
481 drmModeClip *clip = alloca(num_cliprects * sizeof(drmModeClip));
482 BoxPtr rect = REGION_RECTS(dirty);
483 int i, ret;
484
485 /* XXX no need for copy? */
486 for (i = 0; i < num_cliprects; i++, rect++) {
487 clip[i].x1 = rect->x1;
488 clip[i].y1 = rect->y1;
489 clip[i].x2 = rect->x2;
490 clip[i].y2 = rect->y2;
491 }
492
493 /* TODO query connector property to see if this is needed */
494 ret = drmModeDirtyFB(ms->fd, ms->fb_id, clip, num_cliprects);
495 if (ret) {
496 debug_printf("%s: failed to send dirty (%i, %s)\n",
497 __func__, ret, strerror(-ret));
498 }
499
500 DamageEmpty(ms->damage);
501 }
502 }
503 #endif
504 }
505
506 static Bool
507 drv_create_screen_resources(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 = drv_create_screen_resources;
519
520 ms->bind_front_buffer(pScrn);
521
522 ms->noEvict = FALSE;
523
524 drv_adjust_frame(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 drv_screen_init(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 (!drv_init_drm(pScrn)) {
555 FatalError("Could not init DRM");
556 return FALSE;
557 }
558
559 if (!drv_init_resource_management(pScrn)) {
560 FatalError("Could not init resource management (!pipe_screen && !libkms)");
561 return FALSE;
562 }
563
564 if (!drv_init_front_buffer_functions(pScrn)) {
565 FatalError("Could not init front buffer manager");
566 return FALSE;
567 }
568
569 pScrn->pScreen = pScreen;
570
571 /* HW dependent - FIXME */
572 pScrn->displayWidth = pScrn->virtualX;
573
574 miClearVisualTypes();
575
576 if (!miSetVisualTypes(pScrn->depth,
577 miGetDefaultVisualMask(pScrn->depth),
578 pScrn->rgbBits, pScrn->defaultVisual))
579 return FALSE;
580
581 if (!miSetPixmapDepths())
582 return FALSE;
583
584 pScrn->memPhysBase = 0;
585 pScrn->fbOffset = 0;
586
587 if (!fbScreenInit(pScreen, NULL,
588 pScrn->virtualX, pScrn->virtualY,
589 pScrn->xDpi, pScrn->yDpi,
590 pScrn->displayWidth, pScrn->bitsPerPixel))
591 return FALSE;
592
593 if (pScrn->bitsPerPixel > 8) {
594 /* Fixup RGB ordering */
595 visual = pScreen->visuals + pScreen->numVisuals;
596 while (--visual >= pScreen->visuals) {
597 if ((visual->class | DynamicClass) == DirectColor) {
598 visual->offsetRed = pScrn->offset.red;
599 visual->offsetGreen = pScrn->offset.green;
600 visual->offsetBlue = pScrn->offset.blue;
601 visual->redMask = pScrn->mask.red;
602 visual->greenMask = pScrn->mask.green;
603 visual->blueMask = pScrn->mask.blue;
604 }
605 }
606 }
607
608 fbPictureInit(pScreen, NULL, 0);
609
610 ms->blockHandler = pScreen->BlockHandler;
611 pScreen->BlockHandler = drv_block_handler;
612 ms->createScreenResources = pScreen->CreateScreenResources;
613 pScreen->CreateScreenResources = drv_create_screen_resources;
614
615 xf86SetBlackWhitePixels(pScreen);
616
617 if (ms->screen) {
618 ms->exa = xorg_exa_init(pScrn, xf86ReturnOptValBool(ms->Options,
619 OPTION_2D_ACCEL, TRUE));
620 ms->debug_fallback = debug_get_bool_option("XORG_DEBUG_FALLBACK", TRUE);
621
622 xorg_xv_init(pScreen);
623 #ifdef DRI2
624 xorg_dri2_init(pScreen);
625 #endif
626 }
627
628 miInitializeBackingStore(pScreen);
629 xf86SetBackingStore(pScreen);
630 xf86SetSilkenMouse(pScreen);
631 miDCInitialize(pScreen, xf86GetPointerScreenFuncs());
632
633 /* Need to extend HWcursor support to handle mask interleave */
634 if (!ms->SWCursor)
635 xf86_cursors_init(pScreen, 64, 64,
636 HARDWARE_CURSOR_SOURCE_MASK_INTERLEAVE_64 |
637 HARDWARE_CURSOR_ARGB);
638
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;
642
643 pScreen->SaveScreen = xf86SaveScreen;
644 ms->CloseScreen = pScreen->CloseScreen;
645 pScreen->CloseScreen = drv_close_screen;
646
647 if (!xf86CrtcScreenInit(pScreen))
648 return FALSE;
649
650 if (!miCreateDefColormap(pScreen))
651 return FALSE;
652
653 xf86DPMSInit(pScreen, xf86DPMSSet, 0);
654
655 if (serverGeneration == 1)
656 xf86ShowUnusedOptions(pScrn->scrnIndex, pScrn->options);
657
658 if (ms->winsys_screen_init)
659 ms->winsys_screen_init(pScrn);
660
661 return drv_enter_vt(scrnIndex, 1);
662 }
663
664 static void
665 drv_adjust_frame(int scrnIndex, int x, int y, int flags)
666 {
667 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
668 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
669 xf86OutputPtr output = config->output[config->compat_output];
670 xf86CrtcPtr crtc = output->crtc;
671
672 if (crtc && crtc->enabled) {
673 crtc->funcs->set_mode_major(crtc, pScrn->currentMode,
674 RR_Rotate_0, x, y);
675 crtc->x = output->initial_x + x;
676 crtc->y = output->initial_y + y;
677 }
678 }
679
680 static void
681 drv_free_screen(int scrnIndex, int flags)
682 {
683 drv_free_rec(xf86Screens[scrnIndex]);
684 }
685
686 static void
687 drv_leave_vt(int scrnIndex, int flags)
688 {
689 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
690 modesettingPtr ms = modesettingPTR(pScrn);
691 xf86CrtcConfigPtr config = XF86_CRTC_CONFIG_PTR(pScrn);
692 int o;
693
694 if (ms->winsys_leave_vt)
695 ms->winsys_leave_vt(pScrn);
696
697 for (o = 0; o < config->num_crtc; o++) {
698 xf86CrtcPtr crtc = config->crtc[o];
699
700 xorg_crtc_cursor_destroy(crtc);
701
702 if (crtc->rotatedPixmap || crtc->rotatedData) {
703 crtc->funcs->shadow_destroy(crtc, crtc->rotatedPixmap,
704 crtc->rotatedData);
705 crtc->rotatedPixmap = NULL;
706 crtc->rotatedData = NULL;
707 }
708 }
709
710 drmModeRmFB(ms->fd, ms->fb_id);
711
712 drv_restore_hw_state(pScrn);
713
714 if (drmDropMaster(ms->fd))
715 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
716 "drmDropMaster failed: %s\n", strerror(errno));
717
718 pScrn->vtSema = FALSE;
719 }
720
721 /*
722 * This gets called when gaining control of the VT, and from ScreenInit().
723 */
724 static Bool
725 drv_enter_vt(int scrnIndex, int flags)
726 {
727 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
728 modesettingPtr ms = modesettingPTR(pScrn);
729
730 if (drmSetMaster(ms->fd)) {
731 if (errno == EINVAL) {
732 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
733 "drmSetMaster failed: 2.6.29 or newer kernel required for "
734 "multi-server DRI\n");
735 } else {
736 xf86DrvMsg(pScrn->scrnIndex, X_WARNING,
737 "drmSetMaster failed: %s\n", strerror(errno));
738 }
739 }
740
741 /*
742 * Only save state once per server generation since that's what most
743 * drivers do. Could change this to save state at each VT enter.
744 */
745 if (ms->SaveGeneration != serverGeneration) {
746 ms->SaveGeneration = serverGeneration;
747 drv_save_hw_state(pScrn);
748 }
749
750 if (!ms->create_front_buffer(pScrn))
751 return FALSE;
752
753 if (!flags && !ms->bind_front_buffer(pScrn))
754 return FALSE;
755
756 if (!xf86SetDesiredModes(pScrn))
757 return FALSE;
758
759 if (ms->winsys_enter_vt)
760 ms->winsys_enter_vt(pScrn);
761
762 return TRUE;
763 }
764
765 static Bool
766 drv_switch_mode(int scrnIndex, DisplayModePtr mode, int flags)
767 {
768 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
769
770 return xf86SetSingleMode(pScrn, mode, RR_Rotate_0);
771 }
772
773 static Bool
774 drv_close_screen(int scrnIndex, ScreenPtr pScreen)
775 {
776 ScrnInfoPtr pScrn = xf86Screens[scrnIndex];
777 modesettingPtr ms = modesettingPTR(pScrn);
778
779 if (pScrn->vtSema) {
780 drv_leave_vt(scrnIndex, 0);
781 }
782
783 if (ms->winsys_screen_close)
784 ms->winsys_screen_close(pScrn);
785
786 #ifdef DRI2
787 if (ms->screen)
788 xorg_dri2_close(pScreen);
789 #endif
790
791 pScreen->BlockHandler = ms->blockHandler;
792 pScreen->CreateScreenResources = ms->createScreenResources;
793
794 #ifdef DRM_MODE_FEATURE_DIRTYFB
795 if (ms->damage) {
796 DamageUnregister(&pScreen->GetScreenPixmap(pScreen)->drawable, ms->damage);
797 DamageDestroy(ms->damage);
798 ms->damage = NULL;
799 }
800 #endif
801
802 drmModeRmFB(ms->fd, ms->fb_id);
803 ms->destroy_front_buffer(pScrn);
804
805 if (ms->exa)
806 xorg_exa_close(pScrn);
807 ms->exa = NULL;
808
809 drv_close_resource_management(pScrn);
810
811 drmClose(ms->fd);
812 ms->fd = -1;
813
814 pScrn->vtSema = FALSE;
815 pScreen->CloseScreen = ms->CloseScreen;
816 return (*pScreen->CloseScreen) (scrnIndex, pScreen);
817 }
818
819 static ModeStatus
820 drv_valid_mode(int scrnIndex, DisplayModePtr mode, Bool verbose, int flags)
821 {
822 return MODE_OK;
823 }
824
825
826 /*
827 * Front buffer backing store functions.
828 */
829
830 static Bool
831 drv_destroy_front_buffer_ga3d(ScrnInfoPtr pScrn)
832 {
833 modesettingPtr ms = modesettingPTR(pScrn);
834 pipe_texture_reference(&ms->root_texture, NULL);
835 return TRUE;
836 }
837
838 static Bool
839 drv_create_front_buffer_ga3d(ScrnInfoPtr pScrn)
840 {
841 modesettingPtr ms = modesettingPTR(pScrn);
842 unsigned handle, stride;
843 struct pipe_texture *tex;
844 int ret;
845
846 ms->noEvict = TRUE;
847
848 tex = xorg_exa_create_root_texture(pScrn, pScrn->virtualX, pScrn->virtualY,
849 pScrn->depth, pScrn->bitsPerPixel);
850
851 if (!tex)
852 return FALSE;
853
854 if (!ms->api->local_handle_from_texture(ms->api, ms->screen,
855 tex,
856 &stride,
857 &handle))
858 goto err_destroy;
859
860 ret = drmModeAddFB(ms->fd,
861 pScrn->virtualX,
862 pScrn->virtualY,
863 pScrn->depth,
864 pScrn->bitsPerPixel,
865 stride,
866 handle,
867 &ms->fb_id);
868 if (ret) {
869 debug_printf("%s: failed to create framebuffer (%i, %s)",
870 __func__, ret, strerror(-ret));
871 goto err_destroy;
872 }
873
874 pScrn->frameX0 = 0;
875 pScrn->frameY0 = 0;
876 drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
877
878 pipe_texture_reference(&ms->root_texture, tex);
879 pipe_texture_reference(&tex, NULL);
880
881 return TRUE;
882
883 err_destroy:
884 pipe_texture_reference(&tex, NULL);
885 return FALSE;
886 }
887
888 static Bool
889 drv_bind_front_buffer_ga3d(ScrnInfoPtr pScrn)
890 {
891 modesettingPtr ms = modesettingPTR(pScrn);
892 ScreenPtr pScreen = pScrn->pScreen;
893 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
894 struct pipe_texture *check;
895
896 xorg_exa_set_displayed_usage(rootPixmap);
897 xorg_exa_set_shared_usage(rootPixmap);
898 xorg_exa_set_texture(rootPixmap, ms->root_texture);
899 if (!pScreen->ModifyPixmapHeader(rootPixmap, -1, -1, -1, -1, -1, NULL))
900 FatalError("Couldn't adjust screen pixmap\n");
901
902 check = xorg_exa_get_texture(rootPixmap);
903 if (ms->root_texture != check)
904 FatalError("Created new root texture\n");
905
906 pipe_texture_reference(&check, NULL);
907 return TRUE;
908 }
909
910 #ifdef HAVE_LIBKMS
911 static Bool
912 drv_destroy_front_buffer_kms(ScrnInfoPtr pScrn)
913 {
914 modesettingPtr ms = modesettingPTR(pScrn);
915 ScreenPtr pScreen = pScrn->pScreen;
916 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
917
918 if (!ms->root_bo)
919 return TRUE;
920
921 kms_bo_unmap(ms->root_bo);
922 kms_bo_destroy(&ms->root_bo);
923 return TRUE;
924 }
925
926 static Bool
927 drv_create_front_buffer_kms(ScrnInfoPtr pScrn)
928 {
929 modesettingPtr ms = modesettingPTR(pScrn);
930 unsigned handle, stride;
931 struct kms_bo *bo;
932 unsigned attr[8];
933 int ret;
934
935 attr[0] = KMS_BO_TYPE;
936 attr[1] = KMS_BO_TYPE_SCANOUT;
937 attr[2] = KMS_WIDTH;
938 attr[3] = pScrn->virtualX;
939 attr[4] = KMS_HEIGHT;
940 attr[5] = pScrn->virtualY;
941 attr[6] = 0;
942
943 if (kms_bo_create(ms->kms, attr, &bo))
944 return FALSE;
945
946 if (kms_bo_get_prop(bo, KMS_PITCH, &stride))
947 goto err_destroy;
948
949 if (kms_bo_get_prop(bo, KMS_HANDLE, &handle))
950 goto err_destroy;
951
952 ret = drmModeAddFB(ms->fd,
953 pScrn->virtualX,
954 pScrn->virtualY,
955 pScrn->depth,
956 pScrn->bitsPerPixel,
957 stride,
958 handle,
959 &ms->fb_id);
960 if (ret) {
961 debug_printf("%s: failed to create framebuffer (%i, %s)",
962 __func__, ret, strerror(-ret));
963 goto err_destroy;
964 }
965
966 pScrn->frameX0 = 0;
967 pScrn->frameY0 = 0;
968 drv_adjust_frame(pScrn->scrnIndex, pScrn->frameX0, pScrn->frameY0, 0);
969 ms->root_bo = bo;
970
971 return TRUE;
972
973 err_destroy:
974 kms_bo_destroy(&bo);
975 return FALSE;
976 }
977
978 static Bool
979 drv_bind_front_buffer_kms(ScrnInfoPtr pScrn)
980 {
981 modesettingPtr ms = modesettingPTR(pScrn);
982 ScreenPtr pScreen = pScrn->pScreen;
983 PixmapPtr rootPixmap = pScreen->GetScreenPixmap(pScreen);
984 unsigned stride;
985 void *ptr;
986
987 if (kms_bo_get_prop(ms->root_bo, KMS_PITCH, &stride))
988 return FALSE;
989
990 if (kms_bo_map(ms->root_bo, &ptr))
991 goto err_destroy;
992
993 pScreen->ModifyPixmapHeader(rootPixmap,
994 pScreen->width,
995 pScreen->height,
996 pScreen->rootDepth,
997 pScrn->bitsPerPixel,
998 stride,
999 ptr);
1000 return TRUE;
1001
1002 err_destroy:
1003 kms_bo_destroy(&ms->root_bo);
1004 return FALSE;
1005 }
1006 #endif /* HAVE_LIBKMS */
1007
1008 static Bool drv_init_front_buffer_functions(ScrnInfoPtr pScrn)
1009 {
1010 modesettingPtr ms = modesettingPTR(pScrn);
1011 if (ms->screen) {
1012 ms->destroy_front_buffer = drv_destroy_front_buffer_ga3d;
1013 ms->create_front_buffer = drv_create_front_buffer_ga3d;
1014 ms->bind_front_buffer = drv_bind_front_buffer_ga3d;
1015 #ifdef HAVE_LIBKMS
1016 } else if (ms->kms) {
1017 ms->destroy_front_buffer = drv_destroy_front_buffer_kms;
1018 ms->create_front_buffer = drv_create_front_buffer_kms;
1019 ms->bind_front_buffer = drv_bind_front_buffer_kms;
1020 #endif
1021 } else
1022 return FALSE;
1023
1024 return TRUE;
1025 }
1026
1027 /* vim: set sw=4 ts=8 sts=4: */