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/console.h> 28 #include <linux/delay.h> 29 #include <linux/errno.h> 30 #include <linux/fb.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/clients/drm_client_setup.h> 41 #include <drm/drm_crtc.h> 42 #include <drm/drm_crtc_helper.h> 43 #include <drm/drm_fb_helper.h> 44 #include <drm/drm_fourcc.h> 45 #include <drm/drm_gem.h> 46 #include <drm/drm_gem_framebuffer_helper.h> 47 #include <drm/drm_managed.h> 48 #include <drm/drm_print.h> 49 50 #include "i915_drv.h" 51 #include "i915_vma.h" 52 #include "intel_bo.h" 53 #include "intel_display_rpm.h" 54 #include "intel_display_types.h" 55 #include "intel_fb.h" 56 #include "intel_fb_pin.h" 57 #include "intel_fbdev.h" 58 #include "intel_fbdev_fb.h" 59 #include "intel_frontbuffer.h" 60 61 struct intel_fbdev { 62 struct intel_framebuffer *fb; 63 struct i915_vma *vma; 64 unsigned long vma_flags; 65 }; 66 67 static struct intel_fbdev *to_intel_fbdev(struct drm_fb_helper *fb_helper) 68 { 69 struct drm_i915_private *i915 = to_i915(fb_helper->client.dev); 70 71 return i915->display.fbdev.fbdev; 72 } 73 74 static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev) 75 { 76 return ifbdev->fb->frontbuffer; 77 } 78 79 static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev) 80 { 81 intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU); 82 } 83 84 FB_GEN_DEFAULT_DEFERRED_IOMEM_OPS(intel_fbdev, 85 drm_fb_helper_damage_range, 86 drm_fb_helper_damage_area) 87 88 static int intel_fbdev_set_par(struct fb_info *info) 89 { 90 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par); 91 int ret; 92 93 ret = drm_fb_helper_set_par(info); 94 if (ret == 0) 95 intel_fbdev_invalidate(ifbdev); 96 97 return ret; 98 } 99 100 static int intel_fbdev_blank(int blank, struct fb_info *info) 101 { 102 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par); 103 int ret; 104 105 ret = drm_fb_helper_blank(blank, info); 106 if (ret == 0) 107 intel_fbdev_invalidate(ifbdev); 108 109 return ret; 110 } 111 112 static int intel_fbdev_pan_display(struct fb_var_screeninfo *var, 113 struct fb_info *info) 114 { 115 struct intel_fbdev *ifbdev = to_intel_fbdev(info->par); 116 int ret; 117 118 ret = drm_fb_helper_pan_display(var, info); 119 if (ret == 0) 120 intel_fbdev_invalidate(ifbdev); 121 122 return ret; 123 } 124 125 static int intel_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma) 126 { 127 struct drm_fb_helper *fb_helper = info->par; 128 struct drm_gem_object *obj = drm_gem_fb_get_obj(fb_helper->fb, 0); 129 130 return intel_bo_fb_mmap(obj, vma); 131 } 132 133 static void intel_fbdev_fb_destroy(struct fb_info *info) 134 { 135 struct drm_fb_helper *fb_helper = info->par; 136 struct intel_fbdev *ifbdev = to_intel_fbdev(fb_helper); 137 138 drm_fb_helper_fini(fb_helper); 139 140 /* 141 * We rely on the object-free to release the VMA pinning for 142 * the info->screen_base mmaping. Leaking the VMA is simpler than 143 * trying to rectify all the possible error paths leading here. 144 */ 145 intel_fb_unpin_vma(ifbdev->vma, ifbdev->vma_flags); 146 drm_framebuffer_remove(fb_helper->fb); 147 148 drm_client_release(&fb_helper->client); 149 drm_fb_helper_unprepare(fb_helper); 150 kfree(fb_helper); 151 } 152 153 __diag_push(); 154 __diag_ignore_all("-Woverride-init", "Allow field initialization overrides for fb ops"); 155 156 static const struct fb_ops intelfb_ops = { 157 .owner = THIS_MODULE, 158 __FB_DEFAULT_DEFERRED_OPS_RDWR(intel_fbdev), 159 DRM_FB_HELPER_DEFAULT_OPS, 160 .fb_set_par = intel_fbdev_set_par, 161 .fb_blank = intel_fbdev_blank, 162 .fb_pan_display = intel_fbdev_pan_display, 163 __FB_DEFAULT_DEFERRED_OPS_DRAW(intel_fbdev), 164 .fb_mmap = intel_fbdev_mmap, 165 .fb_destroy = intel_fbdev_fb_destroy, 166 }; 167 168 __diag_pop(); 169 170 static int intelfb_dirty(struct drm_fb_helper *helper, struct drm_clip_rect *clip) 171 { 172 if (!(clip->x1 < clip->x2 && clip->y1 < clip->y2)) 173 return 0; 174 175 if (helper->fb->funcs->dirty) 176 return helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, clip, 1); 177 178 return 0; 179 } 180 181 static void intelfb_restore(struct drm_fb_helper *fb_helper) 182 { 183 struct intel_fbdev *ifbdev = to_intel_fbdev(fb_helper); 184 185 intel_fbdev_invalidate(ifbdev); 186 } 187 188 static void intelfb_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) 189 { 190 struct fb_info *info = fb_helper->info; 191 192 /* 193 * When resuming from hibernation, Linux restores the object's 194 * content from swap if the buffer is backed by shmemfs. If the 195 * object is stolen however, it will be full of whatever garbage 196 * was left in there. Clear it to zero in this case. 197 */ 198 if (!suspend && !intel_bo_is_shmem(intel_fb_bo(fb_helper->fb))) 199 memset_io(info->screen_base, 0, info->screen_size); 200 201 fb_set_suspend(info, suspend); 202 } 203 204 static const struct drm_fb_helper_funcs intel_fb_helper_funcs = { 205 .fb_dirty = intelfb_dirty, 206 .fb_restore = intelfb_restore, 207 .fb_set_suspend = intelfb_set_suspend, 208 }; 209 210 int intel_fbdev_driver_fbdev_probe(struct drm_fb_helper *helper, 211 struct drm_fb_helper_surface_size *sizes) 212 { 213 struct intel_fbdev *ifbdev = to_intel_fbdev(helper); 214 struct intel_framebuffer *fb = ifbdev->fb; 215 struct drm_device *dev = helper->dev; 216 struct drm_i915_private *dev_priv = to_i915(dev); 217 struct intel_display *display = to_intel_display(dev); 218 struct ref_tracker *wakeref; 219 struct fb_info *info; 220 struct i915_vma *vma; 221 unsigned long flags = 0; 222 bool prealloc = false; 223 struct drm_gem_object *obj; 224 int ret; 225 226 ifbdev->fb = NULL; 227 228 if (fb && 229 (sizes->fb_width > fb->base.width || 230 sizes->fb_height > fb->base.height)) { 231 drm_dbg_kms(&dev_priv->drm, 232 "BIOS fb too small (%dx%d), we require (%dx%d)," 233 " releasing it\n", 234 fb->base.width, fb->base.height, 235 sizes->fb_width, sizes->fb_height); 236 drm_framebuffer_put(&fb->base); 237 fb = NULL; 238 } 239 if (!fb || drm_WARN_ON(dev, !intel_fb_bo(&fb->base))) { 240 drm_dbg_kms(&dev_priv->drm, 241 "no BIOS fb, allocating a new one\n"); 242 fb = intel_fbdev_fb_alloc(helper, sizes); 243 if (IS_ERR(fb)) 244 return PTR_ERR(fb); 245 } else { 246 drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n"); 247 prealloc = true; 248 sizes->fb_width = fb->base.width; 249 sizes->fb_height = fb->base.height; 250 } 251 252 wakeref = intel_display_rpm_get(display); 253 254 /* Pin the GGTT vma for our access via info->screen_base. 255 * This also validates that any existing fb inherited from the 256 * BIOS is suitable for own access. 257 */ 258 vma = intel_fb_pin_to_ggtt(&fb->base, &fb->normal_view.gtt, 259 fb->min_alignment, 0, 260 intel_fb_view_vtd_guard(&fb->base, &fb->normal_view, 261 DRM_MODE_ROTATE_0), 262 false, &flags); 263 if (IS_ERR(vma)) { 264 ret = PTR_ERR(vma); 265 goto out_unlock; 266 } 267 268 info = drm_fb_helper_alloc_info(helper); 269 if (IS_ERR(info)) { 270 drm_err(&dev_priv->drm, "Failed to allocate fb_info (%pe)\n", info); 271 ret = PTR_ERR(info); 272 goto out_unpin; 273 } 274 275 helper->funcs = &intel_fb_helper_funcs; 276 helper->fb = &fb->base; 277 278 info->fbops = &intelfb_ops; 279 280 obj = intel_fb_bo(&fb->base); 281 282 ret = intel_fbdev_fb_fill_info(dev_priv, info, obj, vma); 283 if (ret) 284 goto out_unpin; 285 286 drm_fb_helper_fill_info(info, dev->fb_helper, sizes); 287 288 /* If the object is shmemfs backed, it will have given us zeroed pages. 289 * If the object is stolen however, it will be full of whatever 290 * garbage was left in there. 291 */ 292 if (!intel_bo_is_shmem(obj) && !prealloc) 293 memset_io(info->screen_base, 0, info->screen_size); 294 295 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 296 297 drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n", 298 fb->base.width, fb->base.height, 299 i915_ggtt_offset(vma)); 300 ifbdev->fb = fb; 301 ifbdev->vma = vma; 302 ifbdev->vma_flags = flags; 303 304 intel_display_rpm_put(display, wakeref); 305 306 return 0; 307 308 out_unpin: 309 intel_fb_unpin_vma(vma, flags); 310 out_unlock: 311 intel_display_rpm_put(display, wakeref); 312 313 return ret; 314 } 315 316 /* 317 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible. 318 * The core display code will have read out the current plane configuration, 319 * so we use that to figure out if there's an object for us to use as the 320 * fb, and if so, we re-use it for the fbdev configuration. 321 * 322 * Note we only support a single fb shared across pipes for boot (mostly for 323 * fbcon), so we just find the biggest and use that. 324 */ 325 static bool intel_fbdev_init_bios(struct drm_device *dev, 326 struct intel_fbdev *ifbdev) 327 { 328 struct drm_i915_private *i915 = to_i915(dev); 329 struct intel_framebuffer *fb = NULL; 330 struct intel_crtc *crtc; 331 unsigned int max_size = 0; 332 333 /* Find the largest fb */ 334 for_each_intel_crtc(dev, crtc) { 335 struct intel_crtc_state *crtc_state = 336 to_intel_crtc_state(crtc->base.state); 337 struct intel_plane *plane = 338 to_intel_plane(crtc->base.primary); 339 struct intel_plane_state *plane_state = 340 to_intel_plane_state(plane->base.state); 341 struct drm_gem_object *obj = intel_fb_bo(plane_state->uapi.fb); 342 343 if (!crtc_state->uapi.active) { 344 drm_dbg_kms(&i915->drm, 345 "[CRTC:%d:%s] not active, skipping\n", 346 crtc->base.base.id, crtc->base.name); 347 continue; 348 } 349 350 if (!obj) { 351 drm_dbg_kms(&i915->drm, 352 "[PLANE:%d:%s] no fb, skipping\n", 353 plane->base.base.id, plane->base.name); 354 continue; 355 } 356 357 if (obj->size > max_size) { 358 drm_dbg_kms(&i915->drm, 359 "found possible fb from [PLANE:%d:%s]\n", 360 plane->base.base.id, plane->base.name); 361 fb = to_intel_framebuffer(plane_state->uapi.fb); 362 max_size = obj->size; 363 } 364 } 365 366 if (!fb) { 367 drm_dbg_kms(&i915->drm, 368 "no active fbs found, not using BIOS config\n"); 369 goto out; 370 } 371 372 /* Now make sure all the pipes will fit into it */ 373 for_each_intel_crtc(dev, crtc) { 374 struct intel_crtc_state *crtc_state = 375 to_intel_crtc_state(crtc->base.state); 376 struct intel_plane *plane = 377 to_intel_plane(crtc->base.primary); 378 unsigned int cur_size; 379 380 if (!crtc_state->uapi.active) { 381 drm_dbg_kms(&i915->drm, 382 "[CRTC:%d:%s] not active, skipping\n", 383 crtc->base.base.id, crtc->base.name); 384 continue; 385 } 386 387 drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n", 388 plane->base.base.id, plane->base.name); 389 390 /* 391 * See if the plane fb we found above will fit on this 392 * pipe. Note we need to use the selected fb's pitch and bpp 393 * rather than the current pipe's, since they differ. 394 */ 395 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay; 396 cur_size = cur_size * fb->base.format->cpp[0]; 397 if (fb->base.pitches[0] < cur_size) { 398 drm_dbg_kms(&i915->drm, 399 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n", 400 plane->base.base.id, plane->base.name, 401 cur_size, fb->base.pitches[0]); 402 fb = NULL; 403 break; 404 } 405 406 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay; 407 cur_size = intel_fb_align_height(&fb->base, 0, cur_size); 408 cur_size *= fb->base.pitches[0]; 409 drm_dbg_kms(&i915->drm, 410 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n", 411 crtc->base.base.id, crtc->base.name, 412 crtc_state->uapi.adjusted_mode.crtc_hdisplay, 413 crtc_state->uapi.adjusted_mode.crtc_vdisplay, 414 fb->base.format->cpp[0] * 8, 415 cur_size); 416 417 if (cur_size > max_size) { 418 drm_dbg_kms(&i915->drm, 419 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n", 420 plane->base.base.id, plane->base.name, 421 cur_size, max_size); 422 fb = NULL; 423 break; 424 } 425 426 drm_dbg_kms(&i915->drm, 427 "fb big enough [PLANE:%d:%s] (%d >= %d)\n", 428 plane->base.base.id, plane->base.name, 429 max_size, cur_size); 430 } 431 432 if (!fb) { 433 drm_dbg_kms(&i915->drm, 434 "BIOS fb not suitable for all pipes, not using\n"); 435 goto out; 436 } 437 438 ifbdev->fb = fb; 439 440 drm_framebuffer_get(&ifbdev->fb->base); 441 442 /* Final pass to check if any active pipes don't have fbs */ 443 for_each_intel_crtc(dev, crtc) { 444 struct intel_crtc_state *crtc_state = 445 to_intel_crtc_state(crtc->base.state); 446 struct intel_plane *plane = 447 to_intel_plane(crtc->base.primary); 448 struct intel_plane_state *plane_state = 449 to_intel_plane_state(plane->base.state); 450 451 if (!crtc_state->uapi.active) 452 continue; 453 454 drm_WARN(dev, !plane_state->uapi.fb, 455 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n", 456 plane->base.base.id, plane->base.name); 457 } 458 459 460 drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n"); 461 return true; 462 463 out: 464 465 return false; 466 } 467 468 static unsigned int intel_fbdev_color_mode(const struct drm_format_info *info) 469 { 470 unsigned int bpp; 471 472 if (!info->depth || info->num_planes != 1 || info->has_alpha || info->is_yuv) 473 return 0; 474 475 bpp = drm_format_info_bpp(info, 0); 476 477 switch (bpp) { 478 case 16: 479 return info->depth; // 15 or 16 480 default: 481 return bpp; 482 } 483 } 484 485 void intel_fbdev_setup(struct drm_i915_private *i915) 486 { 487 struct drm_device *dev = &i915->drm; 488 struct intel_fbdev *ifbdev; 489 unsigned int preferred_bpp = 0; 490 491 if (!HAS_DISPLAY(i915)) 492 return; 493 494 ifbdev = drmm_kzalloc(dev, sizeof(*ifbdev), GFP_KERNEL); 495 if (!ifbdev) 496 return; 497 498 i915->display.fbdev.fbdev = ifbdev; 499 if (intel_fbdev_init_bios(dev, ifbdev)) 500 preferred_bpp = intel_fbdev_color_mode(ifbdev->fb->base.format); 501 if (!preferred_bpp) 502 preferred_bpp = 32; 503 504 drm_client_setup_with_color_mode(dev, preferred_bpp); 505 } 506 507 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev) 508 { 509 if (!fbdev) 510 return NULL; 511 512 return fbdev->fb; 513 } 514 515 struct i915_vma *intel_fbdev_vma_pointer(struct intel_fbdev *fbdev) 516 { 517 return fbdev ? fbdev->vma : NULL; 518 } 519