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