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