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