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/console.h> 29 #include <linux/delay.h> 30 #include <linux/errno.h> 31 #include <linux/init.h> 32 #include <linux/kernel.h> 33 #include <linux/mm.h> 34 #include <linux/module.h> 35 #include <linux/string.h> 36 #include <linux/sysrq.h> 37 #include <linux/tty.h> 38 #include <linux/vga_switcheroo.h> 39 40 #include <drm/drm_crtc.h> 41 #include <drm/drm_fb_helper.h> 42 #include <drm/drm_fourcc.h> 43 #include <drm/drm_gem_framebuffer_helper.h> 44 45 #include "gem/i915_gem_lmem.h" 46 #include "gem/i915_gem_mman.h" 47 48 #include "i915_drv.h" 49 #include "intel_display_types.h" 50 #include "intel_fb.h" 51 #include "intel_fb_pin.h" 52 #include "intel_fbdev.h" 53 #include "intel_frontbuffer.h" 54 55 struct intel_fbdev { 56 struct drm_fb_helper helper; 57 struct intel_framebuffer *fb; 58 struct i915_vma *vma; 59 unsigned long vma_flags; 60 async_cookie_t cookie; 61 int preferred_bpp; 62 63 /* Whether or not fbdev hpd processing is temporarily suspended */ 64 bool hpd_suspended: 1; 65 /* Set when a hotplug was received while HPD processing was suspended */ 66 bool hpd_waiting: 1; 67 68 /* Protects hpd_suspended */ 69 struct mutex hpd_lock; 70 }; 71 72 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper) 73 { 74 return container_of(fb_helper, struct intel_fbdev, helper); 75 } 76 77 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev) 78 { 79 return ifbdev->fb->frontbuffer; 80 } 81 82 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev) 83 { 84 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU); 85 } 86 87 static int intel_fbdev_set_par(struct fb_info *info) 88 { 89 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par); 90 int ret; 91 92 ret = drm_fb_helper_set_par(info); 93 if (ret == 0) 94 intel_fbdev_invalidate(ifbdev); 95 96 return ret; 97 } 98 99 static int intel_fbdev_blank(int blank, struct fb_info *info) 100 { 101 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par); 102 int ret; 103 104 ret = drm_fb_helper_blank(blank, info); 105 if (ret == 0) 106 intel_fbdev_invalidate(ifbdev); 107 108 return ret; 109 } 110 111 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var, 112 struct fb_info *info) 113 { 114 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par); 115 int ret; 116 117 ret = drm_fb_helper_pan_display(var, info); 118 if (ret == 0) 119 intel_fbdev_invalidate(ifbdev); 120 121 return ret; 122 } 123 124 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma) 125 { 126 struct intel_fbdev *fbdev = to_intel_fbdev(info->par); 127 struct drm_gem_object *bo = drm_gem_fb_get_obj(&fbdev->fb->base, 0); 128 struct drm_i915_gem_object *obj = to_intel_bo(bo); 129 130 return i915_gem_fb_mmap(obj, vma); 131 } 132 133 static const struct fb_ops intelfb_ops = { 134 .owner = THIS_MODULE, 135 DRM_FB_HELPER_DEFAULT_OPS, 136 .fb_set_par = intel_fbdev_set_par, 137 .fb_read = drm_fb_helper_cfb_read, 138 .fb_write = drm_fb_helper_cfb_write, 139 .fb_fillrect = drm_fb_helper_cfb_fillrect, 140 .fb_copyarea = drm_fb_helper_cfb_copyarea, 141 .fb_imageblit = drm_fb_helper_cfb_imageblit, 142 .fb_pan_display = intel_fbdev_pan_display, 143 .fb_blank = intel_fbdev_blank, 144 .fb_mmap = intel_fbdev_mmap, 145 }; 146 147 static int intelfb_alloc(struct drm_fb_helper *helper, 148 struct drm_fb_helper_surface_size *sizes) 149 { 150 struct intel_fbdev *ifbdev = to_intel_fbdev(helper); 151 struct drm_framebuffer *fb; 152 struct drm_device *dev = helper->dev; 153 struct drm_i915_private *dev_priv = to_i915(dev); 154 struct drm_mode_fb_cmd2 mode_cmd = {}; 155 struct drm_i915_gem_object *obj; 156 int size; 157 158 /* we don't do packed 24bpp */ 159 if (sizes->surface_bpp == 24) 160 sizes->surface_bpp = 32; 161 162 mode_cmd.width = sizes->surface_width; 163 mode_cmd.height = sizes->surface_height; 164 165 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * 166 DIV_ROUND_UP(sizes->surface_bpp, 8), 64); 167 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, 168 sizes->surface_depth); 169 170 size = mode_cmd.pitches[0] * mode_cmd.height; 171 size = PAGE_ALIGN(size); 172 173 obj = ERR_PTR(-ENODEV); 174 if (HAS_LMEM(dev_priv)) { 175 obj = i915_gem_object_create_lmem(dev_priv, size, 176 I915_BO_ALLOC_CONTIGUOUS | 177 I915_BO_ALLOC_USER); 178 } else { 179 /* 180 * If the FB is too big, just don't use it since fbdev is not very 181 * important and we should probably use that space with FBC or other 182 * features. 183 */ 184 if (size * 2 < dev_priv->dsm.usable_size) 185 obj = i915_gem_object_create_stolen(dev_priv, size); 186 if (IS_ERR(obj)) 187 obj = i915_gem_object_create_shmem(dev_priv, size); 188 } 189 190 if (IS_ERR(obj)) { 191 drm_err(&dev_priv->drm, "failed to allocate framebuffer (%pe)\n", obj); 192 return PTR_ERR(obj); 193 } 194 195 fb = intel_framebuffer_create(obj, &mode_cmd); 196 i915_gem_object_put(obj); 197 if (IS_ERR(fb)) 198 return PTR_ERR(fb); 199 200 ifbdev->fb = to_intel_framebuffer(fb); 201 return 0; 202 } 203 204 static int intelfb_create(struct drm_fb_helper *helper, 205 struct drm_fb_helper_surface_size *sizes) 206 { 207 struct intel_fbdev *ifbdev = to_intel_fbdev(helper); 208 struct intel_framebuffer *intel_fb = ifbdev->fb; 209 struct drm_device *dev = helper->dev; 210 struct drm_i915_private *dev_priv = to_i915(dev); 211 struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev); 212 struct i915_ggtt *ggtt = to_gt(dev_priv)->ggtt; 213 const struct i915_gtt_view view = { 214 .type = I915_GTT_VIEW_NORMAL, 215 }; 216 intel_wakeref_t wakeref; 217 struct fb_info *info; 218 struct i915_vma *vma; 219 unsigned long flags = 0; 220 bool prealloc = false; 221 void __iomem *vaddr; 222 struct drm_i915_gem_object *obj; 223 struct i915_gem_ww_ctx ww; 224 int ret; 225 226 mutex_lock(&ifbdev->hpd_lock); 227 ret = ifbdev->hpd_suspended ? -EAGAIN : 0; 228 mutex_unlock(&ifbdev->hpd_lock); 229 if (ret) 230 return ret; 231 232 if (intel_fb && 233 (sizes->fb_width > intel_fb->base.width || 234 sizes->fb_height > intel_fb->base.height)) { 235 drm_dbg_kms(&dev_priv->drm, 236 "BIOS fb too small (%dx%d), we require (%dx%d)," 237 " releasing it\n", 238 intel_fb->base.width, intel_fb->base.height, 239 sizes->fb_width, sizes->fb_height); 240 drm_framebuffer_put(&intel_fb->base); 241 intel_fb = ifbdev->fb = NULL; 242 } 243 if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) { 244 drm_dbg_kms(&dev_priv->drm, 245 "no BIOS fb, allocating a new one\n"); 246 ret = intelfb_alloc(helper, sizes); 247 if (ret) 248 return ret; 249 intel_fb = ifbdev->fb; 250 } else { 251 drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n"); 252 prealloc = true; 253 sizes->fb_width = intel_fb->base.width; 254 sizes->fb_height = intel_fb->base.height; 255 } 256 257 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm); 258 259 /* Pin the GGTT vma for our access via info->screen_base. 260 * This also validates that any existing fb inherited from the 261 * BIOS is suitable for own access. 262 */ 263 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false, 264 &view, false, &flags); 265 if (IS_ERR(vma)) { 266 ret = PTR_ERR(vma); 267 goto out_unlock; 268 } 269 270 info = drm_fb_helper_alloc_info(helper); 271 if (IS_ERR(info)) { 272 drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info); 273 ret = PTR_ERR(info); 274 goto out_unpin; 275 } 276 277 ifbdev->helper.fb = &ifbdev->fb->base; 278 279 info->fbops = &intelfb_ops; 280 281 obj = intel_fb_obj(&intel_fb->base); 282 if (i915_gem_object_is_lmem(obj)) { 283 struct intel_memory_region *mem = obj->mm.region; 284 285 /* Use fbdev's framebuffer from lmem for discrete */ 286 info->fix.smem_start = 287 (unsigned long)(mem->io_start + 288 i915_gem_object_get_dma_address(obj, 0)); 289 info->fix.smem_len = obj->base.size; 290 } else { 291 /* Our framebuffer is the entirety of fbdev's system memory */ 292 info->fix.smem_start = 293 (unsigned long)(ggtt->gmadr.start + i915_ggtt_offset(vma)); 294 info->fix.smem_len = vma->size; 295 } 296 297 for_i915_gem_ww(&ww, ret, false) { 298 ret = i915_gem_object_lock(vma->obj, &ww); 299 300 if (ret) 301 continue; 302 303 vaddr = i915_vma_pin_iomap(vma); 304 if (IS_ERR(vaddr)) { 305 drm_err(&dev_priv->drm, 306 "Failed to remap framebuffer into virtual memory (%pe)\n", vaddr); 307 ret = PTR_ERR(vaddr); 308 continue; 309 } 310 } 311 312 if (ret) 313 goto out_unpin; 314 315 info->screen_base = vaddr; 316 info->screen_size = vma->size; 317 318 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes); 319 320 /* If the object is shmemfs backed, it will have given us zeroed pages. 321 * If the object is stolen however, it will be full of whatever 322 * garbage was left in there. 323 */ 324 if (!i915_gem_object_is_shmem(vma->obj) && !prealloc) 325 memset_io(info->screen_base, 0, info->screen_size); 326 327 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 328 329 drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n", 330 ifbdev->fb->base.width, ifbdev->fb->base.height, 331 i915_ggtt_offset(vma)); 332 ifbdev->vma = vma; 333 ifbdev->vma_flags = flags; 334 335 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref); 336 vga_switcheroo_client_fb_set(pdev, info); 337 return 0; 338 339 out_unpin: 340 intel_unpin_fb_vma(vma, flags); 341 out_unlock: 342 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref); 343 return ret; 344 } 345 346 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip) 347 { 348 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2)) 349 return 0; 350 351 if (helper->fb->funcs->dirty) 352 return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1); 353 354 return 0; 355 } 356 357 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = { 358 .fb_probe = intelfb_create, 359 .fb_dirty = intelfb_dirty, 360 }; 361 362 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev) 363 { 364 /* We rely on the object-free to release the VMA pinning for 365 * the info->screen_base mmaping. Leaking the VMA is simpler than 366 * trying to rectify all the possible error paths leading here. 367 */ 368 369 drm_fb_helper_fini(&ifbdev->helper); 370 371 if (ifbdev->vma) 372 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags); 373 374 if (ifbdev->fb) 375 drm_framebuffer_remove(&ifbdev->fb->base); 376 377 drm_fb_helper_unprepare(&ifbdev->helper); 378 kfree(ifbdev); 379 } 380 381 /* 382 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible. 383 * The core display code will have read out the current plane configuration, 384 * so we use that to figure out if there's an object for us to use as the 385 * fb, and if so, we re-use it for the fbdev configuration. 386 * 387 * Note we only support a single fb shared across pipes for boot (mostly for 388 * fbcon), so we just find the biggest and use that. 389 */ 390 static bool intel_fbdev_init_bios(struct drm_device *dev, 391 struct intel_fbdev *ifbdev) 392 { 393 struct drm_i915_private *i915 = to_i915(dev); 394 struct intel_framebuffer *fb = NULL; 395 struct intel_crtc *crtc; 396 unsigned int max_size = 0; 397 398 /* Find the largest fb */ 399 for_each_intel_crtc(dev, crtc) { 400 struct intel_crtc_state *crtc_state = 401 to_intel_crtc_state(crtc->base.state); 402 struct intel_plane *plane = 403 to_intel_plane(crtc->base.primary); 404 struct intel_plane_state *plane_state = 405 to_intel_plane_state(plane->base.state); 406 struct drm_i915_gem_object *obj = 407 intel_fb_obj(plane_state->uapi.fb); 408 409 if (!crtc_state->uapi.active) { 410 drm_dbg_kms(&i915->drm, 411 "[CRTC:%d:%s] not active, skipping\n", 412 crtc->base.base.id, crtc->base.name); 413 continue; 414 } 415 416 if (!obj) { 417 drm_dbg_kms(&i915->drm, 418 "[PLANE:%d:%s] no fb, skipping\n", 419 plane->base.base.id, plane->base.name); 420 continue; 421 } 422 423 if (obj->base.size > max_size) { 424 drm_dbg_kms(&i915->drm, 425 "found possible fb from [PLANE:%d:%s]\n", 426 plane->base.base.id, plane->base.name); 427 fb = to_intel_framebuffer(plane_state->uapi.fb); 428 max_size = obj->base.size; 429 } 430 } 431 432 if (!fb) { 433 drm_dbg_kms(&i915->drm, 434 "no active fbs found, not using BIOS config\n"); 435 goto out; 436 } 437 438 /* Now make sure all the pipes will fit into it */ 439 for_each_intel_crtc(dev, crtc) { 440 struct intel_crtc_state *crtc_state = 441 to_intel_crtc_state(crtc->base.state); 442 struct intel_plane *plane = 443 to_intel_plane(crtc->base.primary); 444 unsigned int cur_size; 445 446 if (!crtc_state->uapi.active) { 447 drm_dbg_kms(&i915->drm, 448 "[CRTC:%d:%s] not active, skipping\n", 449 crtc->base.base.id, crtc->base.name); 450 continue; 451 } 452 453 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n", 454 plane->base.base.id, plane->base.name); 455 456 /* 457 * See if the plane fb we found above will fit on this 458 * pipe. Note we need to use the selected fb's pitch and bpp 459 * rather than the current pipe's, since they differ. 460 */ 461 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay; 462 cur_size = cur_size * fb->base.format->cpp[0]; 463 if (fb->base.pitches[0] < cur_size) { 464 drm_dbg_kms(&i915->drm, 465 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n", 466 plane->base.base.id, plane->base.name, 467 cur_size, fb->base.pitches[0]); 468 fb = NULL; 469 break; 470 } 471 472 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay; 473 cur_size = intel_fb_align_height(&fb->base, 0, cur_size); 474 cur_size *= fb->base.pitches[0]; 475 drm_dbg_kms(&i915->drm, 476 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n", 477 crtc->base.base.id, crtc->base.name, 478 crtc_state->uapi.adjusted_mode.crtc_hdisplay, 479 crtc_state->uapi.adjusted_mode.crtc_vdisplay, 480 fb->base.format->cpp[0] * 8, 481 cur_size); 482 483 if (cur_size > max_size) { 484 drm_dbg_kms(&i915->drm, 485 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n", 486 plane->base.base.id, plane->base.name, 487 cur_size, max_size); 488 fb = NULL; 489 break; 490 } 491 492 drm_dbg_kms(&i915->drm, 493 "fb big enough [PLANE:%d:%s] (%d >= %d)\n", 494 plane->base.base.id, plane->base.name, 495 max_size, cur_size); 496 } 497 498 if (!fb) { 499 drm_dbg_kms(&i915->drm, 500 "BIOS fb not suitable for all pipes, not using\n"); 501 goto out; 502 } 503 504 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8; 505 ifbdev->fb = fb; 506 507 drm_framebuffer_get(&ifbdev->fb->base); 508 509 /* Final pass to check if any active pipes don't have fbs */ 510 for_each_intel_crtc(dev, crtc) { 511 struct intel_crtc_state *crtc_state = 512 to_intel_crtc_state(crtc->base.state); 513 struct intel_plane *plane = 514 to_intel_plane(crtc->base.primary); 515 struct intel_plane_state *plane_state = 516 to_intel_plane_state(plane->base.state); 517 518 if (!crtc_state->uapi.active) 519 continue; 520 521 drm_WARN(dev, !plane_state->uapi.fb, 522 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n", 523 plane->base.base.id, plane->base.name); 524 } 525 526 527 drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n"); 528 return true; 529 530 out: 531 532 return false; 533 } 534 535 static void intel_fbdev_suspend_worker(struct work_struct *work) 536 { 537 intel_fbdev_set_suspend(&container_of(work, 538 struct drm_i915_private, 539 display.fbdev.suspend_work)->drm, 540 FBINFO_STATE_RUNNING, 541 true); 542 } 543 544 int intel_fbdev_init(struct drm_device *dev) 545 { 546 struct drm_i915_private *dev_priv = to_i915(dev); 547 struct intel_fbdev *ifbdev; 548 int ret; 549 550 if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv))) 551 return -ENODEV; 552 553 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL); 554 if (ifbdev == NULL) 555 return -ENOMEM; 556 557 mutex_init(&ifbdev->hpd_lock); 558 drm_fb_helper_prepare(dev, &ifbdev->helper, 32, &intel_fb_helper_funcs); 559 560 if (intel_fbdev_init_bios(dev, ifbdev)) 561 ifbdev->helper.preferred_bpp = ifbdev->preferred_bpp; 562 else 563 ifbdev->preferred_bpp = ifbdev->helper.preferred_bpp; 564 565 ret = drm_fb_helper_init(dev, &ifbdev->helper); 566 if (ret) { 567 kfree(ifbdev); 568 return ret; 569 } 570 571 dev_priv->display.fbdev.fbdev = ifbdev; 572 INIT_WORK(&dev_priv->display.fbdev.suspend_work, intel_fbdev_suspend_worker); 573 574 return 0; 575 } 576 577 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie) 578 { 579 struct intel_fbdev *ifbdev = data; 580 581 /* Due to peculiar init order wrt to hpd handling this is separate. */ 582 if (drm_fb_helper_initial_config(&ifbdev->helper)) 583 intel_fbdev_unregister(to_i915(ifbdev->helper.dev)); 584 } 585 586 void intel_fbdev_initial_config_async(struct drm_i915_private *dev_priv) 587 { 588 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev; 589 590 if (!ifbdev) 591 return; 592 593 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev); 594 } 595 596 static void intel_fbdev_sync(struct intel_fbdev *ifbdev) 597 { 598 if (!ifbdev->cookie) 599 return; 600 601 /* Only serialises with all preceding async calls, hence +1 */ 602 async_synchronize_cookie(ifbdev->cookie + 1); 603 ifbdev->cookie = 0; 604 } 605 606 void intel_fbdev_unregister(struct drm_i915_private *dev_priv) 607 { 608 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev; 609 610 if (!ifbdev) 611 return; 612 613 intel_fbdev_set_suspend(&dev_priv->drm, FBINFO_STATE_SUSPENDED, true); 614 615 if (!current_is_async()) 616 intel_fbdev_sync(ifbdev); 617 618 drm_fb_helper_unregister_info(&ifbdev->helper); 619 } 620 621 void intel_fbdev_fini(struct drm_i915_private *dev_priv) 622 { 623 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->display.fbdev.fbdev); 624 625 if (!ifbdev) 626 return; 627 628 intel_fbdev_destroy(ifbdev); 629 } 630 631 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD 632 * processing, fbdev will perform a full connector reprobe if a hotplug event 633 * was received while HPD was suspended. 634 */ 635 static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state) 636 { 637 struct intel_fbdev *ifbdev = i915->display.fbdev.fbdev; 638 bool send_hpd = false; 639 640 mutex_lock(&ifbdev->hpd_lock); 641 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED; 642 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting; 643 ifbdev->hpd_waiting = false; 644 mutex_unlock(&ifbdev->hpd_lock); 645 646 if (send_hpd) { 647 drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n"); 648 drm_fb_helper_hotplug_event(&ifbdev->helper); 649 } 650 } 651 652 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous) 653 { 654 struct drm_i915_private *dev_priv = to_i915(dev); 655 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev; 656 struct fb_info *info; 657 658 if (!ifbdev) 659 return; 660 661 if (drm_WARN_ON(&dev_priv->drm, !HAS_DISPLAY(dev_priv))) 662 return; 663 664 if (!ifbdev->vma) 665 goto set_suspend; 666 667 info = ifbdev->helper.info; 668 669 if (synchronous) { 670 /* Flush any pending work to turn the console on, and then 671 * wait to turn it off. It must be synchronous as we are 672 * about to suspend or unload the driver. 673 * 674 * Note that from within the work-handler, we cannot flush 675 * ourselves, so only flush outstanding work upon suspend! 676 */ 677 if (state != FBINFO_STATE_RUNNING) 678 flush_work(&dev_priv->display.fbdev.suspend_work); 679 680 console_lock(); 681 } else { 682 /* 683 * The console lock can be pretty contented on resume due 684 * to all the printk activity. Try to keep it out of the hot 685 * path of resume if possible. 686 */ 687 drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING); 688 if (!console_trylock()) { 689 /* Don't block our own workqueue as this can 690 * be run in parallel with other i915.ko tasks. 691 */ 692 schedule_work(&dev_priv->display.fbdev.suspend_work); 693 return; 694 } 695 } 696 697 /* On resume from hibernation: If the object is shmemfs backed, it has 698 * been restored from swap. If the object is stolen however, it will be 699 * full of whatever garbage was left in there. 700 */ 701 if (state == FBINFO_STATE_RUNNING && 702 !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base))) 703 memset_io(info->screen_base, 0, info->screen_size); 704 705 drm_fb_helper_set_suspend(&ifbdev->helper, state); 706 console_unlock(); 707 708 set_suspend: 709 intel_fbdev_hpd_set_suspend(dev_priv, state); 710 } 711 712 void intel_fbdev_output_poll_changed(struct drm_device *dev) 713 { 714 struct intel_fbdev *ifbdev = to_i915(dev)->display.fbdev.fbdev; 715 bool send_hpd; 716 717 if (!ifbdev) 718 return; 719 720 intel_fbdev_sync(ifbdev); 721 722 mutex_lock(&ifbdev->hpd_lock); 723 send_hpd = !ifbdev->hpd_suspended; 724 ifbdev->hpd_waiting = true; 725 mutex_unlock(&ifbdev->hpd_lock); 726 727 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup)) 728 drm_fb_helper_hotplug_event(&ifbdev->helper); 729 } 730 731 void intel_fbdev_restore_mode(struct drm_i915_private *dev_priv) 732 { 733 struct intel_fbdev *ifbdev = dev_priv->display.fbdev.fbdev; 734 735 if (!ifbdev) 736 return; 737 738 intel_fbdev_sync(ifbdev); 739 if (!ifbdev->vma) 740 return; 741 742 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0) 743 intel_fbdev_invalidate(ifbdev); 744 } 745 746 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev) 747 { 748 if (!fbdev || !fbdev->helper.fb) 749 return NULL; 750 751 return to_intel_framebuffer(fbdev->helper.fb); 752 } 753