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