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