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