1 /* 2 * Copyright © 2007 David Airlie 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 * DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: 24 * David Airlie 25 */ 26 27 #include <linux/async.h> 28 #include <linux/module.h> 29 #include <linux/kernel.h> 30 #include <linux/console.h> 31 #include <linux/errno.h> 32 #include <linux/string.h> 33 #include <linux/mm.h> 34 #include <linux/tty.h> 35 #include <linux/sysrq.h> 36 #include <linux/delay.h> 37 #include <linux/init.h> 38 #include <linux/vga_switcheroo.h> 39 40 #include <drm/drmP.h> 41 #include <drm/drm_crtc.h> 42 #include <drm/drm_fb_helper.h> 43 #include "intel_drv.h" 44 #include "intel_frontbuffer.h" 45 #include <drm/i915_drm.h> 46 #include "i915_drv.h" 47 48 static int intel_fbdev_set_par(struct fb_info *info) 49 { 50 struct drm_fb_helper *fb_helper = info->par; 51 struct intel_fbdev *ifbdev = 52 container_of(fb_helper, struct intel_fbdev, helper); 53 int ret; 54 55 ret = drm_fb_helper_set_par(info); 56 57 if (ret == 0) { 58 mutex_lock(&fb_helper->dev->struct_mutex); 59 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT); 60 mutex_unlock(&fb_helper->dev->struct_mutex); 61 } 62 63 return ret; 64 } 65 66 static int intel_fbdev_blank(int blank, struct fb_info *info) 67 { 68 struct drm_fb_helper *fb_helper = info->par; 69 struct intel_fbdev *ifbdev = 70 container_of(fb_helper, struct intel_fbdev, helper); 71 int ret; 72 73 ret = drm_fb_helper_blank(blank, info); 74 75 if (ret == 0) { 76 mutex_lock(&fb_helper->dev->struct_mutex); 77 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT); 78 mutex_unlock(&fb_helper->dev->struct_mutex); 79 } 80 81 return ret; 82 } 83 84 #if 0 85 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var, 86 struct fb_info *info) 87 { 88 struct drm_fb_helper *fb_helper = info->par; 89 struct intel_fbdev *ifbdev = 90 container_of(fb_helper, struct intel_fbdev, helper); 91 92 int ret; 93 ret = drm_fb_helper_pan_display(var, info); 94 95 if (ret == 0) { 96 mutex_lock(&fb_helper->dev->struct_mutex); 97 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT); 98 mutex_unlock(&fb_helper->dev->struct_mutex); 99 } 100 101 return ret; 102 } 103 #endif 104 105 static struct fb_ops intelfb_ops = { 106 #if 0 107 .owner = THIS_MODULE, 108 .fb_check_var = drm_fb_helper_check_var, 109 #endif 110 .fb_set_par = intel_fbdev_set_par, 111 #if 0 112 .fb_fillrect = drm_fb_helper_cfb_fillrect, 113 .fb_copyarea = drm_fb_helper_cfb_copyarea, 114 .fb_imageblit = drm_fb_helper_cfb_imageblit, 115 .fb_pan_display = intel_fbdev_pan_display, 116 #endif 117 .fb_blank = intel_fbdev_blank, 118 #if 0 119 .fb_setcmap = drm_fb_helper_setcmap, 120 #endif 121 .fb_debug_enter = drm_fb_helper_debug_enter, 122 #if 0 123 .fb_debug_leave = drm_fb_helper_debug_leave, 124 #endif 125 }; 126 127 static int intelfb_alloc(struct drm_fb_helper *helper, 128 struct drm_fb_helper_surface_size *sizes) 129 { 130 struct intel_fbdev *ifbdev = 131 container_of(helper, struct intel_fbdev, helper); 132 struct drm_framebuffer *fb; 133 struct drm_device *dev = helper->dev; 134 struct drm_i915_private *dev_priv = to_i915(dev); 135 struct i915_ggtt *ggtt = &dev_priv->ggtt; 136 struct drm_mode_fb_cmd2 mode_cmd = {}; 137 struct drm_i915_gem_object *obj = NULL; 138 int size, ret; 139 140 /* we don't do packed 24bpp */ 141 if (sizes->surface_bpp == 24) 142 sizes->surface_bpp = 32; 143 144 mode_cmd.width = sizes->surface_width; 145 mode_cmd.height = sizes->surface_height; 146 147 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * 148 DIV_ROUND_UP(sizes->surface_bpp, 8), 64); 149 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 150 sizes->surface_depth); 151 152 mutex_lock(&dev->struct_mutex); 153 154 size = mode_cmd.pitches[0] * mode_cmd.height; 155 size = PAGE_ALIGN(size); 156 157 /* If the FB is too big, just don't use it since fbdev is not very 158 * important and we should probably use that space with FBC or other 159 * features. */ 160 if (size * 2 < ggtt->stolen_usable_size) 161 obj = i915_gem_object_create_stolen(dev, size); 162 if (obj == NULL) 163 obj = i915_gem_object_create(dev, size); 164 if (IS_ERR(obj)) { 165 DRM_ERROR("failed to allocate framebuffer\n"); 166 ret = PTR_ERR(obj); 167 goto out; 168 } 169 170 fb = __intel_framebuffer_create(dev, &mode_cmd, obj); 171 if (IS_ERR(fb)) { 172 i915_gem_object_put(obj); 173 ret = PTR_ERR(fb); 174 goto out; 175 } 176 177 mutex_unlock(&dev->struct_mutex); 178 179 ifbdev->fb = to_intel_framebuffer(fb); 180 181 return 0; 182 183 out: 184 mutex_unlock(&dev->struct_mutex); 185 return ret; 186 } 187 188 static int intelfb_create(struct drm_fb_helper *helper, 189 struct drm_fb_helper_surface_size *sizes) 190 { 191 struct intel_fbdev *ifbdev = 192 container_of(helper, struct intel_fbdev, helper); 193 struct intel_framebuffer *intel_fb = ifbdev->fb; 194 struct drm_device *dev = helper->dev; 195 struct drm_i915_private *dev_priv = to_i915(dev); 196 struct pci_dev *pdev = dev_priv->drm.pdev; 197 struct i915_ggtt *ggtt = &dev_priv->ggtt; 198 struct fb_info *info; 199 struct drm_framebuffer *fb; 200 struct i915_vma *vma; 201 bool prealloc = false; 202 void __iomem *vaddr; 203 int ret; 204 device_t vga_dev; 205 206 if (intel_fb && 207 (sizes->fb_width > intel_fb->base.width || 208 sizes->fb_height > intel_fb->base.height)) { 209 DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d)," 210 " releasing it\n", 211 intel_fb->base.width, intel_fb->base.height, 212 sizes->fb_width, sizes->fb_height); 213 drm_framebuffer_unreference(&intel_fb->base); 214 intel_fb = ifbdev->fb = NULL; 215 } 216 if (!intel_fb || WARN_ON(!intel_fb->obj)) { 217 DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n"); 218 ret = intelfb_alloc(helper, sizes); 219 if (ret) 220 return ret; 221 intel_fb = ifbdev->fb; 222 } else { 223 DRM_DEBUG_KMS("re-using BIOS fb\n"); 224 prealloc = true; 225 sizes->fb_width = intel_fb->base.width; 226 sizes->fb_height = intel_fb->base.height; 227 } 228 229 mutex_lock(&dev->struct_mutex); 230 231 /* Pin the GGTT vma for our access via info->screen_base. 232 * This also validates that any existing fb inherited from the 233 * BIOS is suitable for own access. 234 */ 235 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0); 236 if (IS_ERR(vma)) { 237 ret = PTR_ERR(vma); 238 goto out_unlock; 239 } 240 241 info = drm_fb_helper_alloc_fbi(helper); 242 if (IS_ERR(info)) { 243 DRM_ERROR("Failed to allocate fb_info\n"); 244 ret = PTR_ERR(info); 245 goto out_unpin; 246 } 247 248 info->par = helper; 249 250 fb = &ifbdev->fb->base; 251 252 ifbdev->helper.fb = fb; 253 254 #ifdef __DragonFly__ 255 vga_dev = device_get_parent(dev->dev->bsddev); 256 info->width = sizes->fb_width; 257 info->height = sizes->fb_height; 258 info->stride = fb->pitches[0]; 259 info->depth = sizes->surface_bpp; 260 info->paddr = ggtt->mappable_base + vma->node.start; 261 info->is_vga_boot_display = vga_pci_is_boot_display(vga_dev); 262 info->fbops = intelfb_ops; 263 #endif 264 265 #if 0 266 strcpy(info->fix.id, "inteldrmfb"); 267 268 info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT; 269 info->fbops = &intelfb_ops; 270 #endif 271 272 /* setup aperture base/size for vesafb takeover */ 273 #ifndef __DragonFly__ 274 info->apertures->ranges[0].base = dev->mode_config.fb_base; 275 info->apertures->ranges[0].size = ggtt->mappable_end; 276 277 info->fix.smem_start = dev->mode_config.fb_base + i915_ggtt_offset(vma); 278 info->fix.smem_len = vma->node.size; 279 #endif 280 281 vaddr = i915_vma_pin_iomap(vma); 282 if (IS_ERR(vaddr)) { 283 DRM_ERROR("Failed to remap framebuffer into virtual memory\n"); 284 ret = PTR_ERR(vaddr); 285 goto out_destroy_fbi; 286 } 287 #ifdef __DragonFly__ 288 info->vaddr = (vm_offset_t)vaddr; 289 #else 290 info->screen_base = vaddr; 291 info->screen_size = vma->node.size; 292 293 /* This driver doesn't need a VT switch to restore the mode on resume */ 294 info->skip_vt_switch = true; 295 296 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); 297 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height); 298 299 /* If the object is shmemfs backed, it will have given us zeroed pages. 300 * If the object is stolen however, it will be full of whatever 301 * garbage was left in there. 302 */ 303 if (intel_fb->obj->stolen && !prealloc) 304 memset_io(info->screen_base, 0, info->screen_size); 305 306 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 307 #endif 308 309 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n", 310 fb->width, fb->height, i915_ggtt_offset(vma)); 311 ifbdev->vma = vma; 312 313 mutex_unlock(&dev->struct_mutex); 314 vga_switcheroo_client_fb_set(pdev, info); 315 return 0; 316 317 out_destroy_fbi: 318 drm_fb_helper_release_fbi(helper); 319 out_unpin: 320 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0); 321 out_unlock: 322 mutex_unlock(&dev->struct_mutex); 323 return ret; 324 } 325 326 /** Sets the color ramps on behalf of RandR */ 327 static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, 328 u16 blue, int regno) 329 { 330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc); 331 332 intel_crtc->lut_r[regno] = red >> 8; 333 intel_crtc->lut_g[regno] = green >> 8; 334 intel_crtc->lut_b[regno] = blue >> 8; 335 } 336 337 static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green, 338 u16 *blue, int regno) 339 { 340 struct intel_crtc *intel_crtc = to_intel_crtc(crtc); 341 342 *red = intel_crtc->lut_r[regno] << 8; 343 *green = intel_crtc->lut_g[regno] << 8; 344 *blue = intel_crtc->lut_b[regno] << 8; 345 } 346 347 static struct drm_fb_helper_crtc * 348 intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc) 349 { 350 int i; 351 352 for (i = 0; i < fb_helper->crtc_count; i++) 353 if (fb_helper->crtc_info[i].mode_set.crtc == crtc) 354 return &fb_helper->crtc_info[i]; 355 356 return NULL; 357 } 358 359 /* 360 * Try to read the BIOS display configuration and use it for the initial 361 * fb configuration. 362 * 363 * The BIOS or boot loader will generally create an initial display 364 * configuration for us that includes some set of active pipes and displays. 365 * This routine tries to figure out which pipes and connectors are active 366 * and stuffs them into the crtcs and modes array given to us by the 367 * drm_fb_helper code. 368 * 369 * The overall sequence is: 370 * intel_fbdev_init - from driver load 371 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data 372 * drm_fb_helper_init - build fb helper structs 373 * drm_fb_helper_single_add_all_connectors - more fb helper structs 374 * intel_fbdev_initial_config - apply the config 375 * drm_fb_helper_initial_config - call ->probe then register_framebuffer() 376 * drm_setup_crtcs - build crtc config for fbdev 377 * intel_fb_initial_config - find active connectors etc 378 * drm_fb_helper_single_fb_probe - set up fbdev 379 * intelfb_create - re-use or alloc fb, build out fbdev structs 380 * 381 * Note that we don't make special consideration whether we could actually 382 * switch to the selected modes without a full modeset. E.g. when the display 383 * is in VGA mode we need to recalculate watermarks and set a new high-res 384 * framebuffer anyway. 385 */ 386 static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper, 387 struct drm_fb_helper_crtc **crtcs, 388 struct drm_display_mode **modes, 389 struct drm_fb_offset *offsets, 390 bool *enabled, int width, int height) 391 { 392 struct drm_device *dev = fb_helper->dev; 393 unsigned long conn_configured, mask; 394 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG); 395 int i, j; 396 bool *save_enabled; 397 bool fallback = true; 398 int num_connectors_enabled = 0; 399 int num_connectors_detected = 0; 400 int pass = 0; 401 402 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL); 403 if (!save_enabled) 404 return false; 405 406 memcpy(save_enabled, enabled, count); 407 mask = BIT(count) - 1; 408 conn_configured = 0; 409 retry: 410 for (i = 0; i < count; i++) { 411 struct drm_fb_helper_connector *fb_conn; 412 struct drm_connector *connector; 413 struct drm_encoder *encoder; 414 struct drm_fb_helper_crtc *new_crtc; 415 struct intel_crtc *intel_crtc; 416 417 fb_conn = fb_helper->connector_info[i]; 418 connector = fb_conn->connector; 419 420 if (conn_configured & BIT(i)) 421 continue; 422 423 if (pass == 0 && !connector->has_tile) 424 continue; 425 426 if (connector->status == connector_status_connected) 427 num_connectors_detected++; 428 429 if (!enabled[i]) { 430 DRM_DEBUG_KMS("connector %s not enabled, skipping\n", 431 connector->name); 432 conn_configured |= BIT(i); 433 continue; 434 } 435 436 if (connector->force == DRM_FORCE_OFF) { 437 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n", 438 connector->name); 439 enabled[i] = false; 440 continue; 441 } 442 443 encoder = connector->state->best_encoder; 444 if (!encoder || WARN_ON(!connector->state->crtc)) { 445 if (connector->force > DRM_FORCE_OFF) 446 goto bail; 447 448 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n", 449 connector->name); 450 enabled[i] = false; 451 conn_configured |= BIT(i); 452 continue; 453 } 454 455 num_connectors_enabled++; 456 457 intel_crtc = to_intel_crtc(connector->state->crtc); 458 for (j = 0; j < 256; j++) { 459 intel_crtc->lut_r[j] = j; 460 intel_crtc->lut_g[j] = j; 461 intel_crtc->lut_b[j] = j; 462 } 463 464 new_crtc = intel_fb_helper_crtc(fb_helper, 465 connector->state->crtc); 466 467 /* 468 * Make sure we're not trying to drive multiple connectors 469 * with a single CRTC, since our cloning support may not 470 * match the BIOS. 471 */ 472 for (j = 0; j < count; j++) { 473 if (crtcs[j] == new_crtc) { 474 DRM_DEBUG_KMS("fallback: cloned configuration\n"); 475 goto bail; 476 } 477 } 478 479 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n", 480 connector->name); 481 482 /* go for command line mode first */ 483 modes[i] = drm_pick_cmdline_mode(fb_conn, width, height); 484 485 /* try for preferred next */ 486 if (!modes[i]) { 487 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n", 488 connector->name, connector->has_tile); 489 modes[i] = drm_has_preferred_mode(fb_conn, width, 490 height); 491 } 492 493 /* No preferred mode marked by the EDID? Are there any modes? */ 494 if (!modes[i] && !list_empty(&connector->modes)) { 495 DRM_DEBUG_KMS("using first mode listed on connector %s\n", 496 connector->name); 497 modes[i] = list_first_entry(&connector->modes, 498 struct drm_display_mode, 499 head); 500 } 501 502 /* last resort: use current mode */ 503 if (!modes[i]) { 504 /* 505 * IMPORTANT: We want to use the adjusted mode (i.e. 506 * after the panel fitter upscaling) as the initial 507 * config, not the input mode, which is what crtc->mode 508 * usually contains. But since our current 509 * code puts a mode derived from the post-pfit timings 510 * into crtc->mode this works out correctly. 511 * 512 * This is crtc->mode and not crtc->state->mode for the 513 * fastboot check to work correctly. crtc_state->mode has 514 * I915_MODE_FLAG_INHERITED, which we clear to force check 515 * state. 516 */ 517 DRM_DEBUG_KMS("looking for current mode on connector %s\n", 518 connector->name); 519 modes[i] = &connector->state->crtc->mode; 520 } 521 crtcs[i] = new_crtc; 522 523 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n", 524 connector->name, 525 connector->state->crtc->base.id, 526 connector->state->crtc->name, 527 modes[i]->hdisplay, modes[i]->vdisplay, 528 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :""); 529 530 fallback = false; 531 conn_configured |= BIT(i); 532 } 533 534 if ((conn_configured & mask) != mask) { 535 pass++; 536 goto retry; 537 } 538 539 /* 540 * If the BIOS didn't enable everything it could, fall back to have the 541 * same user experiencing of lighting up as much as possible like the 542 * fbdev helper library. 543 */ 544 if (num_connectors_enabled != num_connectors_detected && 545 num_connectors_enabled < INTEL_INFO(dev)->num_pipes) { 546 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n"); 547 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled, 548 num_connectors_detected); 549 fallback = true; 550 } 551 552 if (fallback) { 553 bail: 554 DRM_DEBUG_KMS("Not using firmware configuration\n"); 555 memcpy(enabled, save_enabled, count); 556 kfree(save_enabled); 557 return false; 558 } 559 560 kfree(save_enabled); 561 return true; 562 } 563 564 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = { 565 .initial_config = intel_fb_initial_config, 566 .gamma_set = intel_crtc_fb_gamma_set, 567 .gamma_get = intel_crtc_fb_gamma_get, 568 .fb_probe = intelfb_create, 569 }; 570 571 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev) 572 { 573 /* We rely on the object-free to release the VMA pinning for 574 * the info->screen_base mmaping. Leaking the VMA is simpler than 575 * trying to rectify all the possible error paths leading here. 576 */ 577 578 drm_fb_helper_unregister_fbi(&ifbdev->helper); 579 drm_fb_helper_release_fbi(&ifbdev->helper); 580 581 drm_fb_helper_fini(&ifbdev->helper); 582 583 if (ifbdev->fb) { 584 mutex_lock(&ifbdev->helper.dev->struct_mutex); 585 intel_unpin_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0); 586 mutex_unlock(&ifbdev->helper.dev->struct_mutex); 587 588 drm_framebuffer_remove(&ifbdev->fb->base); 589 } 590 591 kfree(ifbdev); 592 } 593 594 /* 595 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible. 596 * The core display code will have read out the current plane configuration, 597 * so we use that to figure out if there's an object for us to use as the 598 * fb, and if so, we re-use it for the fbdev configuration. 599 * 600 * Note we only support a single fb shared across pipes for boot (mostly for 601 * fbcon), so we just find the biggest and use that. 602 */ 603 static bool intel_fbdev_init_bios(struct drm_device *dev, 604 struct intel_fbdev *ifbdev) 605 { 606 struct intel_framebuffer *fb = NULL; 607 struct drm_crtc *crtc; 608 struct intel_crtc *intel_crtc; 609 unsigned int max_size = 0; 610 611 /* Find the largest fb */ 612 for_each_crtc(dev, crtc) { 613 struct drm_i915_gem_object *obj = 614 intel_fb_obj(crtc->primary->state->fb); 615 intel_crtc = to_intel_crtc(crtc); 616 617 if (!crtc->state->active || !obj) { 618 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n", 619 pipe_name(intel_crtc->pipe)); 620 continue; 621 } 622 623 if (obj->base.size > max_size) { 624 DRM_DEBUG_KMS("found possible fb from plane %c\n", 625 pipe_name(intel_crtc->pipe)); 626 fb = to_intel_framebuffer(crtc->primary->state->fb); 627 max_size = obj->base.size; 628 } 629 } 630 631 if (!fb) { 632 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n"); 633 goto out; 634 } 635 636 /* Now make sure all the pipes will fit into it */ 637 for_each_crtc(dev, crtc) { 638 unsigned int cur_size; 639 640 intel_crtc = to_intel_crtc(crtc); 641 642 if (!crtc->state->active) { 643 DRM_DEBUG_KMS("pipe %c not active, skipping\n", 644 pipe_name(intel_crtc->pipe)); 645 continue; 646 } 647 648 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n", 649 pipe_name(intel_crtc->pipe)); 650 651 /* 652 * See if the plane fb we found above will fit on this 653 * pipe. Note we need to use the selected fb's pitch and bpp 654 * rather than the current pipe's, since they differ. 655 */ 656 cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay; 657 cur_size = cur_size * fb->base.bits_per_pixel / 8; 658 if (fb->base.pitches[0] < cur_size) { 659 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n", 660 pipe_name(intel_crtc->pipe), 661 cur_size, fb->base.pitches[0]); 662 fb = NULL; 663 break; 664 } 665 666 cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay; 667 cur_size = intel_fb_align_height(dev, cur_size, 668 fb->base.pixel_format, 669 fb->base.modifier[0]); 670 cur_size *= fb->base.pitches[0]; 671 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n", 672 pipe_name(intel_crtc->pipe), 673 intel_crtc->config->base.adjusted_mode.crtc_hdisplay, 674 intel_crtc->config->base.adjusted_mode.crtc_vdisplay, 675 fb->base.bits_per_pixel, 676 cur_size); 677 678 if (cur_size > max_size) { 679 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n", 680 pipe_name(intel_crtc->pipe), 681 cur_size, max_size); 682 fb = NULL; 683 break; 684 } 685 686 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n", 687 pipe_name(intel_crtc->pipe), 688 max_size, cur_size); 689 } 690 691 if (!fb) { 692 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n"); 693 goto out; 694 } 695 696 ifbdev->preferred_bpp = fb->base.bits_per_pixel; 697 ifbdev->fb = fb; 698 699 drm_framebuffer_reference(&ifbdev->fb->base); 700 701 /* Final pass to check if any active pipes don't have fbs */ 702 for_each_crtc(dev, crtc) { 703 intel_crtc = to_intel_crtc(crtc); 704 705 if (!crtc->state->active) 706 continue; 707 708 WARN(!crtc->primary->fb, 709 "re-used BIOS config but lost an fb on crtc %d\n", 710 crtc->base.id); 711 } 712 713 714 DRM_DEBUG_KMS("using BIOS fb for initial console\n"); 715 return true; 716 717 out: 718 719 return false; 720 } 721 722 static void intel_fbdev_suspend_worker(struct work_struct *work) 723 { 724 intel_fbdev_set_suspend(&container_of(work, 725 struct drm_i915_private, 726 fbdev_suspend_work)->drm, 727 FBINFO_STATE_RUNNING, 728 true); 729 } 730 731 int intel_fbdev_init(struct drm_device *dev) 732 { 733 struct intel_fbdev *ifbdev; 734 struct drm_i915_private *dev_priv = to_i915(dev); 735 int ret; 736 737 if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0)) 738 return -ENODEV; 739 740 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL); 741 if (ifbdev == NULL) 742 return -ENOMEM; 743 744 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs); 745 746 if (!intel_fbdev_init_bios(dev, ifbdev)) 747 ifbdev->preferred_bpp = 32; 748 749 ret = drm_fb_helper_init(dev, &ifbdev->helper, 750 INTEL_INFO(dev)->num_pipes, 4); 751 if (ret) { 752 kfree(ifbdev); 753 return ret; 754 } 755 756 dev_priv->fbdev = ifbdev; 757 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker); 758 759 drm_fb_helper_single_add_all_connectors(&ifbdev->helper); 760 761 return 0; 762 } 763 764 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie) 765 { 766 struct intel_fbdev *ifbdev = data; 767 768 /* Due to peculiar init order wrt to hpd handling this is separate. */ 769 if (drm_fb_helper_initial_config(&ifbdev->helper, 770 ifbdev->preferred_bpp)) 771 intel_fbdev_fini(ifbdev->helper.dev); 772 } 773 774 void intel_fbdev_initial_config_async(struct drm_device *dev) 775 { 776 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev; 777 778 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev); 779 } 780 781 static void intel_fbdev_sync(struct intel_fbdev *ifbdev) 782 { 783 if (!ifbdev->cookie) 784 return; 785 786 /* Only serialises with all preceding async calls, hence +1 */ 787 async_synchronize_cookie(ifbdev->cookie + 1); 788 ifbdev->cookie = 0; 789 } 790 791 void intel_fbdev_fini(struct drm_device *dev) 792 { 793 struct drm_i915_private *dev_priv = to_i915(dev); 794 struct intel_fbdev *ifbdev = dev_priv->fbdev; 795 796 if (!ifbdev) 797 return; 798 799 cancel_work_sync(&dev_priv->fbdev_suspend_work); 800 #if 0 801 if (!current_is_async()) 802 #endif 803 intel_fbdev_sync(ifbdev); 804 805 intel_fbdev_destroy(ifbdev); 806 dev_priv->fbdev = NULL; 807 } 808 809 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous) 810 { 811 #if 0 812 struct drm_i915_private *dev_priv = to_i915(dev); 813 struct intel_fbdev *ifbdev = dev_priv->fbdev; 814 struct fb_info *info; 815 816 if (!ifbdev || !ifbdev->fb) 817 return; 818 819 info = ifbdev->helper.fbdev; 820 821 if (synchronous) { 822 /* Flush any pending work to turn the console on, and then 823 * wait to turn it off. It must be synchronous as we are 824 * about to suspend or unload the driver. 825 * 826 * Note that from within the work-handler, we cannot flush 827 * ourselves, so only flush outstanding work upon suspend! 828 */ 829 if (state != FBINFO_STATE_RUNNING) 830 flush_work(&dev_priv->fbdev_suspend_work); 831 console_lock(); 832 } else { 833 /* 834 * The console lock can be pretty contented on resume due 835 * to all the printk activity. Try to keep it out of the hot 836 * path of resume if possible. 837 */ 838 WARN_ON(state != FBINFO_STATE_RUNNING); 839 if (!console_trylock()) { 840 /* Don't block our own workqueue as this can 841 * be run in parallel with other i915.ko tasks. 842 */ 843 schedule_work(&dev_priv->fbdev_suspend_work); 844 return; 845 } 846 } 847 848 /* On resume from hibernation: If the object is shmemfs backed, it has 849 * been restored from swap. If the object is stolen however, it will be 850 * full of whatever garbage was left in there. 851 */ 852 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen) 853 memset_io(info->screen_base, 0, info->screen_size); 854 855 drm_fb_helper_set_suspend(&ifbdev->helper, state); 856 console_unlock(); 857 #endif 858 } 859 860 void intel_fbdev_output_poll_changed(struct drm_device *dev) 861 { 862 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev; 863 864 if (ifbdev && ifbdev->fb) 865 drm_fb_helper_hotplug_event(&ifbdev->helper); 866 } 867 868 void intel_fbdev_restore_mode(struct drm_device *dev) 869 { 870 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev; 871 872 if (!ifbdev) 873 return; 874 875 intel_fbdev_sync(ifbdev); 876 if (!ifbdev->fb) 877 return; 878 879 #ifdef __DragonFly__ 880 /* XXX: avoid dead-locking the Xorg on exit */ 881 if (mutex_is_locked(&dev->mode_config.mutex)) { 882 DRM_ERROR("fubar while trying to restore kms_console\n"); 883 return; /* drm_modeset_unlock_all(dev) ? */ 884 } 885 #endif 886 887 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper)) { 888 DRM_DEBUG("failed to restore crtc mode\n"); 889 } else { 890 mutex_lock(&dev->struct_mutex); 891 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT); 892 mutex_unlock(&dev->struct_mutex); 893 } 894 } 895