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