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 = ERR_PTR(-ENODEV); 145 if (size * 2 < dev_priv->stolen_usable_size) 146 obj = i915_gem_object_create_stolen(dev_priv, size); 147 if (IS_ERR(obj)) 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 wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm); 208 209 /* Pin the GGTT vma for our access via info->screen_base. 210 * This also validates that any existing fb inherited from the 211 * BIOS is suitable for own access. 212 */ 213 vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, 214 &view, false, &flags); 215 if (IS_ERR(vma)) { 216 ret = PTR_ERR(vma); 217 goto out_unlock; 218 } 219 220 intel_frontbuffer_flush(to_frontbuffer(ifbdev), ORIGIN_DIRTYFB); 221 222 info = drm_fb_helper_alloc_fbi(helper); 223 if (IS_ERR(info)) { 224 DRM_ERROR("Failed to allocate fb_info\n"); 225 ret = PTR_ERR(info); 226 goto out_unpin; 227 } 228 229 ifbdev->helper.fb = &ifbdev->fb->base; 230 231 info->fbops = &intelfb_ops; 232 233 /* setup aperture base/size for vesafb takeover */ 234 info->apertures->ranges[0].base = ggtt->gmadr.start; 235 info->apertures->ranges[0].size = ggtt->mappable_end; 236 237 vaddr = i915_vma_pin_iomap(vma); 238 if (IS_ERR(vaddr)) { 239 DRM_ERROR("Failed to remap framebuffer into virtual memory\n"); 240 ret = PTR_ERR(vaddr); 241 goto out_unpin; 242 } 243 info->screen_base = vaddr; 244 info->screen_size = vma->node.size; 245 246 /* Our framebuffer is the entirety of fbdev's system memory */ 247 info->fix.smem_start = (unsigned long)info->screen_base; 248 info->fix.smem_len = info->screen_size; 249 250 drm_fb_helper_fill_info(info, &ifbdev->helper, sizes); 251 252 /* If the object is shmemfs backed, it will have given us zeroed pages. 253 * If the object is stolen however, it will be full of whatever 254 * garbage was left in there. 255 */ 256 if (vma->obj->stolen && !prealloc) 257 memset_io(info->screen_base, 0, info->screen_size); 258 259 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 260 261 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x\n", 262 ifbdev->fb->base.width, ifbdev->fb->base.height, 263 i915_ggtt_offset(vma)); 264 ifbdev->vma = vma; 265 ifbdev->vma_flags = flags; 266 267 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref); 268 vga_switcheroo_client_fb_set(pdev, info); 269 return 0; 270 271 out_unpin: 272 intel_unpin_fb_vma(vma, flags); 273 out_unlock: 274 intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref); 275 return ret; 276 } 277 278 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = { 279 .fb_probe = intelfb_create, 280 }; 281 282 static void intel_fbdev_destroy(struct intel_fbdev *ifbdev) 283 { 284 /* We rely on the object-free to release the VMA pinning for 285 * the info->screen_base mmaping. Leaking the VMA is simpler than 286 * trying to rectify all the possible error paths leading here. 287 */ 288 289 drm_fb_helper_fini(&ifbdev->helper); 290 291 if (ifbdev->vma) 292 intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags); 293 294 if (ifbdev->fb) 295 drm_framebuffer_remove(&ifbdev->fb->base); 296 297 kfree(ifbdev); 298 } 299 300 /* 301 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible. 302 * The core display code will have read out the current plane configuration, 303 * so we use that to figure out if there's an object for us to use as the 304 * fb, and if so, we re-use it for the fbdev configuration. 305 * 306 * Note we only support a single fb shared across pipes for boot (mostly for 307 * fbcon), so we just find the biggest and use that. 308 */ 309 static bool intel_fbdev_init_bios(struct drm_device *dev, 310 struct intel_fbdev *ifbdev) 311 { 312 struct intel_framebuffer *fb = NULL; 313 struct drm_crtc *crtc; 314 struct intel_crtc *intel_crtc; 315 unsigned int max_size = 0; 316 317 /* Find the largest fb */ 318 for_each_crtc(dev, crtc) { 319 struct drm_i915_gem_object *obj = 320 intel_fb_obj(crtc->primary->state->fb); 321 intel_crtc = to_intel_crtc(crtc); 322 323 if (!crtc->state->active || !obj) { 324 DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n", 325 pipe_name(intel_crtc->pipe)); 326 continue; 327 } 328 329 if (obj->base.size > max_size) { 330 DRM_DEBUG_KMS("found possible fb from plane %c\n", 331 pipe_name(intel_crtc->pipe)); 332 fb = to_intel_framebuffer(crtc->primary->state->fb); 333 max_size = obj->base.size; 334 } 335 } 336 337 if (!fb) { 338 DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n"); 339 goto out; 340 } 341 342 /* Now make sure all the pipes will fit into it */ 343 for_each_crtc(dev, crtc) { 344 unsigned int cur_size; 345 346 intel_crtc = to_intel_crtc(crtc); 347 348 if (!crtc->state->active) { 349 DRM_DEBUG_KMS("pipe %c not active, skipping\n", 350 pipe_name(intel_crtc->pipe)); 351 continue; 352 } 353 354 DRM_DEBUG_KMS("checking plane %c for BIOS fb\n", 355 pipe_name(intel_crtc->pipe)); 356 357 /* 358 * See if the plane fb we found above will fit on this 359 * pipe. Note we need to use the selected fb's pitch and bpp 360 * rather than the current pipe's, since they differ. 361 */ 362 cur_size = crtc->state->adjusted_mode.crtc_hdisplay; 363 cur_size = cur_size * fb->base.format->cpp[0]; 364 if (fb->base.pitches[0] < cur_size) { 365 DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n", 366 pipe_name(intel_crtc->pipe), 367 cur_size, fb->base.pitches[0]); 368 fb = NULL; 369 break; 370 } 371 372 cur_size = crtc->state->adjusted_mode.crtc_vdisplay; 373 cur_size = intel_fb_align_height(&fb->base, 0, cur_size); 374 cur_size *= fb->base.pitches[0]; 375 DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n", 376 pipe_name(intel_crtc->pipe), 377 crtc->state->adjusted_mode.crtc_hdisplay, 378 crtc->state->adjusted_mode.crtc_vdisplay, 379 fb->base.format->cpp[0] * 8, 380 cur_size); 381 382 if (cur_size > max_size) { 383 DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n", 384 pipe_name(intel_crtc->pipe), 385 cur_size, max_size); 386 fb = NULL; 387 break; 388 } 389 390 DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n", 391 pipe_name(intel_crtc->pipe), 392 max_size, cur_size); 393 } 394 395 if (!fb) { 396 DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n"); 397 goto out; 398 } 399 400 ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8; 401 ifbdev->fb = fb; 402 403 drm_framebuffer_get(&ifbdev->fb->base); 404 405 /* Final pass to check if any active pipes don't have fbs */ 406 for_each_crtc(dev, crtc) { 407 intel_crtc = to_intel_crtc(crtc); 408 409 if (!crtc->state->active) 410 continue; 411 412 WARN(!crtc->primary->state->fb, 413 "re-used BIOS config but lost an fb on crtc %d\n", 414 crtc->base.id); 415 } 416 417 418 DRM_DEBUG_KMS("using BIOS fb for initial console\n"); 419 return true; 420 421 out: 422 423 return false; 424 } 425 426 static void intel_fbdev_suspend_worker(struct work_struct *work) 427 { 428 intel_fbdev_set_suspend(&container_of(work, 429 struct drm_i915_private, 430 fbdev_suspend_work)->drm, 431 FBINFO_STATE_RUNNING, 432 true); 433 } 434 435 int intel_fbdev_init(struct drm_device *dev) 436 { 437 struct drm_i915_private *dev_priv = to_i915(dev); 438 struct intel_fbdev *ifbdev; 439 int ret; 440 441 if (WARN_ON(!HAS_DISPLAY(dev_priv) || !INTEL_DISPLAY_ENABLED(dev_priv))) 442 return -ENODEV; 443 444 ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL); 445 if (ifbdev == NULL) 446 return -ENOMEM; 447 448 mutex_init(&ifbdev->hpd_lock); 449 drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs); 450 451 if (!intel_fbdev_init_bios(dev, ifbdev)) 452 ifbdev->preferred_bpp = 32; 453 454 ret = drm_fb_helper_init(dev, &ifbdev->helper, 4); 455 if (ret) { 456 kfree(ifbdev); 457 return ret; 458 } 459 460 dev_priv->fbdev = ifbdev; 461 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker); 462 463 drm_fb_helper_single_add_all_connectors(&ifbdev->helper); 464 465 return 0; 466 } 467 468 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie) 469 { 470 struct intel_fbdev *ifbdev = data; 471 472 /* Due to peculiar init order wrt to hpd handling this is separate. */ 473 if (drm_fb_helper_initial_config(&ifbdev->helper, 474 ifbdev->preferred_bpp)) 475 intel_fbdev_unregister(to_i915(ifbdev->helper.dev)); 476 } 477 478 void intel_fbdev_initial_config_async(struct drm_device *dev) 479 { 480 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev; 481 482 if (!ifbdev) 483 return; 484 485 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev); 486 } 487 488 static void intel_fbdev_sync(struct intel_fbdev *ifbdev) 489 { 490 if (!ifbdev->cookie) 491 return; 492 493 /* Only serialises with all preceding async calls, hence +1 */ 494 async_synchronize_cookie(ifbdev->cookie + 1); 495 ifbdev->cookie = 0; 496 } 497 498 void intel_fbdev_unregister(struct drm_i915_private *dev_priv) 499 { 500 struct intel_fbdev *ifbdev = dev_priv->fbdev; 501 502 if (!ifbdev) 503 return; 504 505 cancel_work_sync(&dev_priv->fbdev_suspend_work); 506 if (!current_is_async()) 507 intel_fbdev_sync(ifbdev); 508 509 drm_fb_helper_unregister_fbi(&ifbdev->helper); 510 } 511 512 void intel_fbdev_fini(struct drm_i915_private *dev_priv) 513 { 514 struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev); 515 516 if (!ifbdev) 517 return; 518 519 intel_fbdev_destroy(ifbdev); 520 } 521 522 /* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD 523 * processing, fbdev will perform a full connector reprobe if a hotplug event 524 * was received while HPD was suspended. 525 */ 526 static void intel_fbdev_hpd_set_suspend(struct intel_fbdev *ifbdev, int state) 527 { 528 bool send_hpd = false; 529 530 mutex_lock(&ifbdev->hpd_lock); 531 ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED; 532 send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting; 533 ifbdev->hpd_waiting = false; 534 mutex_unlock(&ifbdev->hpd_lock); 535 536 if (send_hpd) { 537 DRM_DEBUG_KMS("Handling delayed fbcon HPD event\n"); 538 drm_fb_helper_hotplug_event(&ifbdev->helper); 539 } 540 } 541 542 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous) 543 { 544 struct drm_i915_private *dev_priv = to_i915(dev); 545 struct intel_fbdev *ifbdev = dev_priv->fbdev; 546 struct fb_info *info; 547 548 if (!ifbdev || !ifbdev->vma) 549 return; 550 551 info = ifbdev->helper.fbdev; 552 553 if (synchronous) { 554 /* Flush any pending work to turn the console on, and then 555 * wait to turn it off. It must be synchronous as we are 556 * about to suspend or unload the driver. 557 * 558 * Note that from within the work-handler, we cannot flush 559 * ourselves, so only flush outstanding work upon suspend! 560 */ 561 if (state != FBINFO_STATE_RUNNING) 562 flush_work(&dev_priv->fbdev_suspend_work); 563 564 console_lock(); 565 } else { 566 /* 567 * The console lock can be pretty contented on resume due 568 * to all the printk activity. Try to keep it out of the hot 569 * path of resume if possible. 570 */ 571 WARN_ON(state != FBINFO_STATE_RUNNING); 572 if (!console_trylock()) { 573 /* Don't block our own workqueue as this can 574 * be run in parallel with other i915.ko tasks. 575 */ 576 schedule_work(&dev_priv->fbdev_suspend_work); 577 return; 578 } 579 } 580 581 /* On resume from hibernation: If the object is shmemfs backed, it has 582 * been restored from swap. If the object is stolen however, it will be 583 * full of whatever garbage was left in there. 584 */ 585 if (state == FBINFO_STATE_RUNNING && 586 intel_fb_obj(&ifbdev->fb->base)->stolen) 587 memset_io(info->screen_base, 0, info->screen_size); 588 589 drm_fb_helper_set_suspend(&ifbdev->helper, state); 590 console_unlock(); 591 592 intel_fbdev_hpd_set_suspend(ifbdev, state); 593 } 594 595 void intel_fbdev_output_poll_changed(struct drm_device *dev) 596 { 597 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev; 598 bool send_hpd; 599 600 if (!ifbdev) 601 return; 602 603 intel_fbdev_sync(ifbdev); 604 605 mutex_lock(&ifbdev->hpd_lock); 606 send_hpd = !ifbdev->hpd_suspended; 607 ifbdev->hpd_waiting = true; 608 mutex_unlock(&ifbdev->hpd_lock); 609 610 if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup)) 611 drm_fb_helper_hotplug_event(&ifbdev->helper); 612 } 613 614 void intel_fbdev_restore_mode(struct drm_device *dev) 615 { 616 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev; 617 618 if (!ifbdev) 619 return; 620 621 intel_fbdev_sync(ifbdev); 622 if (!ifbdev->vma) 623 return; 624 625 if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0) 626 intel_fbdev_invalidate(ifbdev); 627 } 628