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