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