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