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_fbdev_fb.h" 58 #include "intel_frontbuffer.h" 59 60 struct intel_fbdev { 61 struct intel_framebuffer *fb; 62 struct i915_vma *vma; 63 unsigned long vma_flags; 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, ifbdev->vma_flags); 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 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_fbdev_fb_pitch_align(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(sizes, &mode_cmd); 234 235 size = mode_cmd.pitches[0] * mode_cmd.height; 236 size = PAGE_ALIGN(size); 237 238 obj = intel_fbdev_fb_bo_create(display->drm, 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_fbdev_fb_bo_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_framebuffer *fb = ifbdev->fb; 269 struct fb_info *info = helper->info; 270 struct ref_tracker *wakeref; 271 struct i915_vma *vma; 272 unsigned long flags = 0; 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 /* Pin the GGTT vma for our access via info->screen_base. 310 * This also validates that any existing fb inherited from the 311 * BIOS is suitable for own access. 312 */ 313 vma = intel_fb_pin_to_ggtt(&fb->base, &fb->normal_view.gtt, 314 fb->min_alignment, 0, 315 intel_fb_view_vtd_guard(&fb->base, &fb->normal_view, 316 DRM_MODE_ROTATE_0), 317 false, &flags); 318 if (IS_ERR(vma)) { 319 ret = PTR_ERR(vma); 320 goto out_unlock; 321 } 322 323 helper->funcs = &intel_fb_helper_funcs; 324 helper->fb = &fb->base; 325 326 info->fbops = &intelfb_ops; 327 328 obj = intel_fb_bo(&fb->base); 329 330 ret = intel_fbdev_fb_fill_info(display->drm, info, obj, vma); 331 if (ret) 332 goto out_unpin; 333 334 drm_fb_helper_fill_info(info, display->drm->fb_helper, sizes); 335 336 /* If the object is shmemfs backed, it will have given us zeroed pages. 337 * If the object is stolen however, it will be full of whatever 338 * garbage was left in there. 339 */ 340 if (!intel_bo_is_shmem(obj) && !prealloc) 341 memset_io(info->screen_base, 0, info->screen_size); 342 343 /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ 344 345 drm_dbg_kms(display->drm, "allocated %dx%d fb\n", fb->base.width, fb->base.height); 346 ifbdev->fb = fb; 347 ifbdev->vma = vma; 348 ifbdev->vma_flags = flags; 349 350 intel_display_rpm_put(display, wakeref); 351 352 return 0; 353 354 out_unpin: 355 intel_fb_unpin_vma(vma, flags); 356 out_unlock: 357 intel_display_rpm_put(display, wakeref); 358 359 return ret; 360 } 361 362 /* 363 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible. 364 * The core display code will have read out the current plane configuration, 365 * so we use that to figure out if there's an object for us to use as the 366 * fb, and if so, we re-use it for the fbdev configuration. 367 * 368 * Note we only support a single fb shared across pipes for boot (mostly for 369 * fbcon), so we just find the biggest and use that. 370 */ 371 static bool intel_fbdev_init_bios(struct intel_display *display, 372 struct intel_fbdev *ifbdev) 373 { 374 struct intel_framebuffer *fb = NULL; 375 struct intel_crtc *crtc; 376 unsigned int max_size = 0; 377 378 /* Find the largest fb */ 379 for_each_intel_crtc(display->drm, crtc) { 380 struct intel_crtc_state *crtc_state = 381 to_intel_crtc_state(crtc->base.state); 382 struct intel_plane *plane = 383 to_intel_plane(crtc->base.primary); 384 struct intel_plane_state *plane_state = 385 to_intel_plane_state(plane->base.state); 386 struct drm_gem_object *obj = intel_fb_bo(plane_state->uapi.fb); 387 388 if (!crtc_state->uapi.active) { 389 drm_dbg_kms(display->drm, 390 "[CRTC:%d:%s] not active, skipping\n", 391 crtc->base.base.id, crtc->base.name); 392 continue; 393 } 394 395 if (!obj) { 396 drm_dbg_kms(display->drm, 397 "[PLANE:%d:%s] no fb, skipping\n", 398 plane->base.base.id, plane->base.name); 399 continue; 400 } 401 402 if (obj->size > max_size) { 403 drm_dbg_kms(display->drm, 404 "found possible fb from [PLANE:%d:%s]\n", 405 plane->base.base.id, plane->base.name); 406 fb = to_intel_framebuffer(plane_state->uapi.fb); 407 max_size = obj->size; 408 } 409 } 410 411 if (!fb) { 412 drm_dbg_kms(display->drm, 413 "no active fbs found, not using BIOS config\n"); 414 goto out; 415 } 416 417 /* Now make sure all the pipes will fit into it */ 418 for_each_intel_crtc(display->drm, crtc) { 419 struct intel_crtc_state *crtc_state = 420 to_intel_crtc_state(crtc->base.state); 421 struct intel_plane *plane = 422 to_intel_plane(crtc->base.primary); 423 unsigned int cur_size; 424 425 if (!crtc_state->uapi.active) { 426 drm_dbg_kms(display->drm, 427 "[CRTC:%d:%s] not active, skipping\n", 428 crtc->base.base.id, crtc->base.name); 429 continue; 430 } 431 432 drm_dbg_kms(display->drm, "checking [PLANE:%d:%s] for BIOS fb\n", 433 plane->base.base.id, plane->base.name); 434 435 /* 436 * See if the plane fb we found above will fit on this 437 * pipe. Note we need to use the selected fb's pitch and bpp 438 * rather than the current pipe's, since they differ. 439 */ 440 cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay; 441 cur_size = cur_size * fb->base.format->cpp[0]; 442 if (fb->base.pitches[0] < cur_size) { 443 drm_dbg_kms(display->drm, 444 "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n", 445 plane->base.base.id, plane->base.name, 446 cur_size, fb->base.pitches[0]); 447 fb = NULL; 448 break; 449 } 450 451 cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay; 452 cur_size = intel_fb_align_height(&fb->base, 0, cur_size); 453 cur_size *= fb->base.pitches[0]; 454 drm_dbg_kms(display->drm, 455 "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n", 456 crtc->base.base.id, crtc->base.name, 457 crtc_state->uapi.adjusted_mode.crtc_hdisplay, 458 crtc_state->uapi.adjusted_mode.crtc_vdisplay, 459 fb->base.format->cpp[0] * 8, 460 cur_size); 461 462 if (cur_size > max_size) { 463 drm_dbg_kms(display->drm, 464 "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n", 465 plane->base.base.id, plane->base.name, 466 cur_size, max_size); 467 fb = NULL; 468 break; 469 } 470 471 drm_dbg_kms(display->drm, 472 "fb big enough [PLANE:%d:%s] (%d >= %d)\n", 473 plane->base.base.id, plane->base.name, 474 max_size, cur_size); 475 } 476 477 if (!fb) { 478 drm_dbg_kms(display->drm, 479 "BIOS fb not suitable for all pipes, not using\n"); 480 goto out; 481 } 482 483 ifbdev->fb = fb; 484 485 drm_framebuffer_get(&ifbdev->fb->base); 486 487 /* Final pass to check if any active pipes don't have fbs */ 488 for_each_intel_crtc(display->drm, crtc) { 489 struct intel_crtc_state *crtc_state = 490 to_intel_crtc_state(crtc->base.state); 491 struct intel_plane *plane = 492 to_intel_plane(crtc->base.primary); 493 struct intel_plane_state *plane_state = 494 to_intel_plane_state(plane->base.state); 495 496 if (!crtc_state->uapi.active) 497 continue; 498 499 drm_WARN(display->drm, !plane_state->uapi.fb, 500 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n", 501 plane->base.base.id, plane->base.name); 502 } 503 504 505 drm_dbg_kms(display->drm, "using BIOS fb for initial console\n"); 506 return true; 507 508 out: 509 510 return false; 511 } 512 513 static unsigned int intel_fbdev_color_mode(const struct drm_format_info *info) 514 { 515 unsigned int bpp; 516 517 if (!info->depth || info->num_planes != 1 || info->has_alpha || info->is_yuv) 518 return 0; 519 520 bpp = drm_format_info_bpp(info, 0); 521 522 switch (bpp) { 523 case 16: 524 return info->depth; // 15 or 16 525 default: 526 return bpp; 527 } 528 } 529 530 void intel_fbdev_setup(struct intel_display *display) 531 { 532 struct intel_fbdev *ifbdev; 533 unsigned int preferred_bpp = 0; 534 535 if (!HAS_DISPLAY(display)) 536 return; 537 538 ifbdev = drmm_kzalloc(display->drm, sizeof(*ifbdev), GFP_KERNEL); 539 if (!ifbdev) 540 return; 541 542 display->fbdev.fbdev = ifbdev; 543 if (intel_fbdev_init_bios(display, ifbdev)) 544 preferred_bpp = intel_fbdev_color_mode(ifbdev->fb->base.format); 545 if (!preferred_bpp) 546 preferred_bpp = 32; 547 548 drm_client_setup_with_color_mode(display->drm, preferred_bpp); 549 } 550 551 struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev) 552 { 553 if (!fbdev) 554 return NULL; 555 556 return fbdev->fb; 557 } 558 559 struct i915_vma *intel_fbdev_vma_pointer(struct intel_fbdev *fbdev) 560 { 561 return fbdev ? fbdev->vma : NULL; 562 } 563 564 void intel_fbdev_get_map(struct intel_fbdev *fbdev, struct iosys_map *map) 565 { 566 intel_fb_get_map(fbdev->vma, map); 567 } 568