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