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