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/fb.h> 38 #include <linux/init.h> 39 #include <linux/vga_switcheroo.h> 40 41 #include <drm/drmP.h> 42 #include <drm/drm_crtc.h> 43 #include <drm/drm_fb_helper.h> 44 #include "intel_drv.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 drm_gem_object_unreference(&obj->base); 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 i915_ggtt *ggtt = &dev_priv->ggtt; 197 struct fb_info *info; 198 struct drm_framebuffer *fb; 199 struct i915_vma *vma; 200 struct drm_i915_gem_object *obj; 201 bool prealloc = false; 202 void *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 obj = intel_fb->obj; 230 231 mutex_lock(&dev->struct_mutex); 232 233 /* Pin the GGTT vma for our access via info->screen_base. 234 * This also validates that any existing fb inherited from the 235 * BIOS is suitable for own access. 236 */ 237 ret = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, DRM_ROTATE_0); 238 if (ret) 239 goto out_unlock; 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 + i915_gem_obj_ggtt_offset(obj); 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 vma = i915_gem_obj_to_ggtt(obj); 273 274 /* setup aperture base/size for vesafb takeover */ 275 #ifndef __DragonFly__ 276 info->apertures->ranges[0].base = dev->mode_config.fb_base; 277 info->apertures->ranges[0].size = ggtt->mappable_end; 278 279 info->fix.smem_start = dev->mode_config.fb_base + vma->node.start; 280 info->fix.smem_len = vma->node.size; 281 #endif 282 283 vaddr = i915_vma_pin_iomap(vma); 284 if (IS_ERR(vaddr)) { 285 DRM_ERROR("Failed to remap framebuffer into virtual memory\n"); 286 ret = PTR_ERR(vaddr); 287 goto out_destroy_fbi; 288 } 289 #ifdef __DragonFly__ 290 info->vaddr = (vm_offset_t)vaddr; 291 #else 292 info->screen_base = vaddr; 293 info->screen_size = vma->node.size; 294 295 /* This driver doesn't need a VT switch to restore the mode on resume */ 296 info->skip_vt_switch = true; 297 298 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); 299 drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height); 300 301 /* If the object is shmemfs backed, it will have given us zeroed pages. 302 * If the object is stolen however, it will be full of whatever 303 * garbage was left in there. 304 */ 305 if (ifbdev->fb->obj->stolen && !prealloc) 306 memset_io(info->screen_base, 0, info->screen_size); 307 308 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 309 #endif 310 311 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08llx, bo %p\n", 312 fb->width, fb->height, 313 i915_gem_obj_ggtt_offset(obj), obj); 314 315 mutex_unlock(&dev->struct_mutex); 316 vga_switcheroo_client_fb_set(dev->pdev, info); 317 return 0; 318 319 out_destroy_fbi: 320 drm_fb_helper_release_fbi(helper); 321 out_unpin: 322 intel_unpin_fb_obj(&ifbdev->fb->base, BIT(DRM_ROTATE_0)); 323 out_unlock: 324 mutex_unlock(&dev->struct_mutex); 325 return ret; 326 } 327 328 /** Sets the color ramps on behalf of RandR */ 329 static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, 330 u16 blue, int regno) 331 { 332 struct intel_crtc *intel_crtc = to_intel_crtc(crtc); 333 334 intel_crtc->lut_r[regno] = red >> 8; 335 intel_crtc->lut_g[regno] = green >> 8; 336 intel_crtc->lut_b[regno] = blue >> 8; 337 } 338 339 static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green, 340 u16 *blue, int regno) 341 { 342 struct intel_crtc *intel_crtc = to_intel_crtc(crtc); 343 344 *red = intel_crtc->lut_r[regno] << 8; 345 *green = intel_crtc->lut_g[regno] << 8; 346 *blue = intel_crtc->lut_b[regno] << 8; 347 } 348 349 static struct drm_fb_helper_crtc * 350 intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc) 351 { 352 int i; 353 354 for (i = 0; i < fb_helper->crtc_count; i++) 355 if (fb_helper->crtc_info[i].mode_set.crtc == crtc) 356 return &fb_helper->crtc_info[i]; 357 358 return NULL; 359 } 360 361 /* 362 * Try to read the BIOS display configuration and use it for the initial 363 * fb configuration. 364 * 365 * The BIOS or boot loader will generally create an initial display 366 * configuration for us that includes some set of active pipes and displays. 367 * This routine tries to figure out which pipes and connectors are active 368 * and stuffs them into the crtcs and modes array given to us by the 369 * drm_fb_helper code. 370 * 371 * The overall sequence is: 372 * intel_fbdev_init - from driver load 373 * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data 374 * drm_fb_helper_init - build fb helper structs 375 * drm_fb_helper_single_add_all_connectors - more fb helper structs 376 * intel_fbdev_initial_config - apply the config 377 * drm_fb_helper_initial_config - call ->probe then register_framebuffer() 378 * drm_setup_crtcs - build crtc config for fbdev 379 * intel_fb_initial_config - find active connectors etc 380 * drm_fb_helper_single_fb_probe - set up fbdev 381 * intelfb_create - re-use or alloc fb, build out fbdev structs 382 * 383 * Note that we don't make special consideration whether we could actually 384 * switch to the selected modes without a full modeset. E.g. when the display 385 * is in VGA mode we need to recalculate watermarks and set a new high-res 386 * framebuffer anyway. 387 */ 388 static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper, 389 struct drm_fb_helper_crtc **crtcs, 390 struct drm_display_mode **modes, 391 struct drm_fb_offset *offsets, 392 bool *enabled, int width, int height) 393 { 394 struct drm_device *dev = fb_helper->dev; 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 uint64_t conn_configured = 0, mask; 401 int pass = 0; 402 403 save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool), 404 GFP_KERNEL); 405 if (!save_enabled) 406 return false; 407 408 memcpy(save_enabled, enabled, dev->mode_config.num_connector); 409 mask = (1 << fb_helper->connector_count) - 1; 410 retry: 411 for (i = 0; i < fb_helper->connector_count; i++) { 412 struct drm_fb_helper_connector *fb_conn; 413 struct drm_connector *connector; 414 struct drm_encoder *encoder; 415 struct drm_fb_helper_crtc *new_crtc; 416 struct intel_crtc *intel_crtc; 417 418 fb_conn = fb_helper->connector_info[i]; 419 connector = fb_conn->connector; 420 421 if (conn_configured & (1 << i)) 422 continue; 423 424 if (pass == 0 && !connector->has_tile) 425 continue; 426 427 if (connector->status == connector_status_connected) 428 num_connectors_detected++; 429 430 if (!enabled[i]) { 431 DRM_DEBUG_KMS("connector %s not enabled, skipping\n", 432 connector->name); 433 conn_configured |= (1 << i); 434 continue; 435 } 436 437 if (connector->force == DRM_FORCE_OFF) { 438 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n", 439 connector->name); 440 enabled[i] = false; 441 continue; 442 } 443 444 encoder = connector->state->best_encoder; 445 if (!encoder || WARN_ON(!connector->state->crtc)) { 446 if (connector->force > DRM_FORCE_OFF) 447 goto bail; 448 449 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n", 450 connector->name); 451 enabled[i] = false; 452 conn_configured |= (1 << i); 453 continue; 454 } 455 456 num_connectors_enabled++; 457 458 intel_crtc = to_intel_crtc(connector->state->crtc); 459 for (j = 0; j < 256; j++) { 460 intel_crtc->lut_r[j] = j; 461 intel_crtc->lut_g[j] = j; 462 intel_crtc->lut_b[j] = j; 463 } 464 465 new_crtc = intel_fb_helper_crtc(fb_helper, 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 < fb_helper->connector_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 |= (1 << 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, dev->mode_config.num_connector); 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, BIT(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)->dev, 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 = dev->dev_private; 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 #if 0 757 ifbdev->helper.atomic = true; 758 #endif 759 760 dev_priv->fbdev = ifbdev; 761 INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker); 762 763 drm_fb_helper_single_add_all_connectors(&ifbdev->helper); 764 765 return 0; 766 } 767 768 static void intel_fbdev_initial_config(void *data, async_cookie_t cookie) 769 { 770 struct intel_fbdev *ifbdev = data; 771 772 /* Due to peculiar init order wrt to hpd handling this is separate. */ 773 if (drm_fb_helper_initial_config(&ifbdev->helper, 774 ifbdev->preferred_bpp)) 775 intel_fbdev_fini(ifbdev->helper.dev); 776 } 777 778 void intel_fbdev_initial_config_async(struct drm_device *dev) 779 { 780 struct intel_fbdev *ifbdev = to_i915(dev)->fbdev; 781 782 ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev); 783 } 784 785 static void intel_fbdev_sync(struct intel_fbdev *ifbdev) 786 { 787 if (!ifbdev->cookie) 788 return; 789 790 /* Only serialises with all preceding async calls, hence +1 */ 791 async_synchronize_cookie(ifbdev->cookie + 1); 792 ifbdev->cookie = 0; 793 } 794 795 void intel_fbdev_fini(struct drm_device *dev) 796 { 797 struct drm_i915_private *dev_priv = dev->dev_private; 798 struct intel_fbdev *ifbdev = dev_priv->fbdev; 799 800 if (!ifbdev) 801 return; 802 803 flush_work(&dev_priv->fbdev_suspend_work); 804 805 #if 0 806 if (!current_is_async()) 807 #endif 808 intel_fbdev_sync(ifbdev); 809 810 intel_fbdev_destroy(ifbdev); 811 dev_priv->fbdev = NULL; 812 } 813 814 void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous) 815 { 816 #if 0 817 struct drm_i915_private *dev_priv = dev->dev_private; 818 struct intel_fbdev *ifbdev = dev_priv->fbdev; 819 struct fb_info *info; 820 821 if (!ifbdev) 822 return; 823 824 info = ifbdev->helper.fbdev; 825 826 if (synchronous) { 827 /* Flush any pending work to turn the console on, and then 828 * wait to turn it off. It must be synchronous as we are 829 * about to suspend or unload the driver. 830 * 831 * Note that from within the work-handler, we cannot flush 832 * ourselves, so only flush outstanding work upon suspend! 833 */ 834 if (state != FBINFO_STATE_RUNNING) 835 flush_work(&dev_priv->fbdev_suspend_work); 836 console_lock(); 837 } else { 838 /* 839 * The console lock can be pretty contented on resume due 840 * to all the printk activity. Try to keep it out of the hot 841 * path of resume if possible. 842 */ 843 WARN_ON(state != FBINFO_STATE_RUNNING); 844 if (!console_trylock()) { 845 /* Don't block our own workqueue as this can 846 * be run in parallel with other i915.ko tasks. 847 */ 848 schedule_work(&dev_priv->fbdev_suspend_work); 849 return; 850 } 851 } 852 853 /* On resume from hibernation: If the object is shmemfs backed, it has 854 * been restored from swap. If the object is stolen however, it will be 855 * full of whatever garbage was left in there. 856 */ 857 if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen) 858 memset_io(info->screen_base, 0, info->screen_size); 859 860 drm_fb_helper_set_suspend(&ifbdev->helper, state); 861 console_unlock(); 862 #endif 863 } 864 865 void intel_fbdev_output_poll_changed(struct drm_device *dev) 866 { 867 struct drm_i915_private *dev_priv = dev->dev_private; 868 if (dev_priv->fbdev) 869 drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper); 870 } 871 872 void intel_fbdev_restore_mode(struct drm_device *dev) 873 { 874 int ret; 875 struct drm_i915_private *dev_priv = dev->dev_private; 876 struct intel_fbdev *ifbdev = dev_priv->fbdev; 877 struct drm_fb_helper *fb_helper; 878 879 if (!ifbdev) 880 return; 881 882 intel_fbdev_sync(ifbdev); 883 884 fb_helper = &ifbdev->helper; 885 886 #ifdef __DragonFly__ 887 /* XXX: avoid dead-locking the Xorg on exit */ 888 if (mutex_is_locked(&dev->mode_config.mutex)) { 889 DRM_ERROR("fubar while trying to restore kms_console\n"); 890 return; /* drm_modeset_unlock_all(dev) ? */ 891 } 892 #endif 893 894 ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 895 if (ret) { 896 DRM_DEBUG("failed to restore crtc mode\n"); 897 } else { 898 mutex_lock(&fb_helper->dev->struct_mutex); 899 intel_fb_obj_invalidate(ifbdev->fb->obj, ORIGIN_GTT); 900 mutex_unlock(&fb_helper->dev->struct_mutex); 901 } 902 } 903