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