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