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