1 /* 2 * Copyright (c) 2006-2009 Red Hat Inc. 3 * Copyright (c) 2006-2008 Intel Corporation 4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 5 * 6 * DRM framebuffer helper functions 7 * 8 * Permission to use, copy, modify, distribute, and sell this software and its 9 * documentation for any purpose is hereby granted without fee, provided that 10 * the above copyright notice appear in all copies and that both that copyright 11 * notice and this permission notice appear in supporting documentation, and 12 * that the name of the copyright holders not be used in advertising or 13 * publicity pertaining to distribution of the software without specific, 14 * written prior permission. The copyright holders make no representations 15 * about the suitability of this software for any purpose. It is provided "as 16 * is" without express or implied warranty. 17 * 18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 24 * OF THIS SOFTWARE. 25 * 26 * Authors: 27 * Dave Airlie <airlied@linux.ie> 28 * Jesse Barnes <jesse.barnes@intel.com> 29 */ 30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 31 32 #include <linux/console.h> 33 #include <linux/dma-buf.h> 34 #include <linux/kernel.h> 35 #include <linux/module.h> 36 #include <linux/slab.h> 37 #include <linux/sysrq.h> 38 #include <linux/vmalloc.h> 39 40 #include <drm/drm_atomic.h> 41 #include <drm/drm_crtc.h> 42 #include <drm/drm_crtc_helper.h> 43 #include <drm/drm_drv.h> 44 #include <drm/drm_fb_helper.h> 45 #include <drm/drm_fourcc.h> 46 #include <drm/drm_print.h> 47 #include <drm/drm_vblank.h> 48 49 #include "drm_crtc_helper_internal.h" 50 #include "drm_internal.h" 51 52 static bool drm_fbdev_emulation = true; 53 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); 54 MODULE_PARM_DESC(fbdev_emulation, 55 "Enable legacy fbdev emulation [default=true]"); 56 57 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; 58 module_param(drm_fbdev_overalloc, int, 0444); 59 MODULE_PARM_DESC(drm_fbdev_overalloc, 60 "Overallocation of the fbdev buffer (%) [default=" 61 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); 62 63 /* 64 * In order to keep user-space compatibility, we want in certain use-cases 65 * to keep leaking the fbdev physical address to the user-space program 66 * handling the fbdev buffer. 67 * This is a bad habit essentially kept into closed source opengl driver 68 * that should really be moved into open-source upstream projects instead 69 * of using legacy physical addresses in user space to communicate with 70 * other out-of-tree kernel modules. 71 * 72 * This module_param *should* be removed as soon as possible and be 73 * considered as a broken and legacy behaviour from a modern fbdev device. 74 */ 75 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 76 static bool drm_leak_fbdev_smem = false; 77 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600); 78 MODULE_PARM_DESC(drm_leak_fbdev_smem, 79 "Allow unsafe leaking fbdev physical smem address [default=false]"); 80 #endif 81 82 static DRM_LIST_HEAD(kernel_fb_helper_list); 83 static DEFINE_MUTEX(kernel_fb_helper_lock); 84 85 /** 86 * DOC: fbdev helpers 87 * 88 * The fb helper functions are useful to provide an fbdev on top of a drm kernel 89 * mode setting driver. They can be used mostly independently from the crtc 90 * helper functions used by many drivers to implement the kernel mode setting 91 * interfaces. 92 * 93 * Drivers that support a dumb buffer with a virtual address and mmap support, 94 * should try out the generic fbdev emulation using drm_fbdev_generic_setup(). 95 * It will automatically set up deferred I/O if the driver requires a shadow 96 * buffer. 97 * 98 * At runtime drivers should restore the fbdev console by using 99 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback. 100 * They should also notify the fb helper code from updates to the output 101 * configuration by using drm_fb_helper_output_poll_changed() as their 102 * &drm_mode_config_funcs.output_poll_changed callback. 103 * 104 * For suspend/resume consider using drm_mode_config_helper_suspend() and 105 * drm_mode_config_helper_resume() which takes care of fbdev as well. 106 * 107 * All other functions exported by the fb helper library can be used to 108 * implement the fbdev driver interface by the driver. 109 * 110 * It is possible, though perhaps somewhat tricky, to implement race-free 111 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() 112 * helper must be called first to initialize the minimum required to make 113 * hotplug detection work. Drivers also need to make sure to properly set up 114 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() 115 * it is safe to enable interrupts and start processing hotplug events. At the 116 * same time, drivers should initialize all modeset objects such as CRTCs, 117 * encoders and connectors. To finish up the fbdev helper initialization, the 118 * drm_fb_helper_init() function is called. To probe for all attached displays 119 * and set up an initial configuration using the detected hardware, drivers 120 * should call drm_fb_helper_initial_config(). 121 * 122 * If &drm_framebuffer_funcs.dirty is set, the 123 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will 124 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right 125 * away. This worker then calls the dirty() function ensuring that it will 126 * always run in process context since the fb_*() function could be running in 127 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io 128 * callback it will also schedule dirty_work with the damage collected from the 129 * mmap page writes. 130 * 131 * Deferred I/O is not compatible with SHMEM. Such drivers should request an 132 * fbdev shadow buffer and call drm_fbdev_generic_setup() instead. 133 */ 134 135 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) 136 { 137 uint16_t *r_base, *g_base, *b_base; 138 139 if (crtc->funcs->gamma_set == NULL) 140 return; 141 142 r_base = crtc->gamma_store; 143 g_base = r_base + crtc->gamma_size; 144 b_base = g_base + crtc->gamma_size; 145 146 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 147 crtc->gamma_size, NULL); 148 } 149 150 /** 151 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter 152 * @info: fbdev registered by the helper 153 */ 154 int drm_fb_helper_debug_enter(struct fb_info *info) 155 { 156 struct drm_fb_helper *helper = info->par; 157 const struct drm_crtc_helper_funcs *funcs; 158 struct drm_mode_set *mode_set; 159 160 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 161 mutex_lock(&helper->client.modeset_mutex); 162 drm_client_for_each_modeset(mode_set, &helper->client) { 163 if (!mode_set->crtc->enabled) 164 continue; 165 166 funcs = mode_set->crtc->helper_private; 167 if (funcs->mode_set_base_atomic == NULL) 168 continue; 169 170 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) 171 continue; 172 173 funcs->mode_set_base_atomic(mode_set->crtc, 174 mode_set->fb, 175 mode_set->x, 176 mode_set->y, 177 ENTER_ATOMIC_MODE_SET); 178 } 179 mutex_unlock(&helper->client.modeset_mutex); 180 } 181 182 return 0; 183 } 184 EXPORT_SYMBOL(drm_fb_helper_debug_enter); 185 186 /** 187 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave 188 * @info: fbdev registered by the helper 189 */ 190 int drm_fb_helper_debug_leave(struct fb_info *info) 191 { 192 struct drm_fb_helper *helper = info->par; 193 struct drm_client_dev *client = &helper->client; 194 #ifdef notyet 195 struct drm_device *dev = helper->dev; 196 #endif 197 struct drm_crtc *crtc; 198 const struct drm_crtc_helper_funcs *funcs; 199 struct drm_mode_set *mode_set; 200 struct drm_framebuffer *fb; 201 202 mutex_lock(&client->modeset_mutex); 203 drm_client_for_each_modeset(mode_set, client) { 204 crtc = mode_set->crtc; 205 if (drm_drv_uses_atomic_modeset(crtc->dev)) 206 continue; 207 208 funcs = crtc->helper_private; 209 fb = crtc->primary->fb; 210 211 if (!crtc->enabled) 212 continue; 213 214 if (!fb) { 215 drm_err(dev, "no fb to restore?\n"); 216 continue; 217 } 218 219 if (funcs->mode_set_base_atomic == NULL) 220 continue; 221 222 drm_fb_helper_restore_lut_atomic(mode_set->crtc); 223 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, 224 crtc->y, LEAVE_ATOMIC_MODE_SET); 225 } 226 mutex_unlock(&client->modeset_mutex); 227 228 return 0; 229 } 230 EXPORT_SYMBOL(drm_fb_helper_debug_leave); 231 232 static int 233 __drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper, 234 bool force) 235 { 236 bool do_delayed; 237 int ret; 238 239 if (!drm_fbdev_emulation || !fb_helper) 240 return -ENODEV; 241 242 if (READ_ONCE(fb_helper->deferred_setup)) 243 return 0; 244 245 #ifdef __OpenBSD__ 246 force = true; 247 #endif 248 249 mutex_lock(&fb_helper->lock); 250 if (force) { 251 /* 252 * Yes this is the _locked version which expects the master lock 253 * to be held. But for forced restores we're intentionally 254 * racing here, see drm_fb_helper_set_par(). 255 */ 256 ret = drm_client_modeset_commit_locked(&fb_helper->client); 257 } else { 258 ret = drm_client_modeset_commit(&fb_helper->client); 259 } 260 261 do_delayed = fb_helper->delayed_hotplug; 262 if (do_delayed) 263 fb_helper->delayed_hotplug = false; 264 mutex_unlock(&fb_helper->lock); 265 266 if (do_delayed) 267 drm_fb_helper_hotplug_event(fb_helper); 268 269 return ret; 270 } 271 272 /** 273 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration 274 * @fb_helper: driver-allocated fbdev helper, can be NULL 275 * 276 * This should be called from driver's drm &drm_driver.lastclose callback 277 * when implementing an fbcon on top of kms using this helper. This ensures that 278 * the user isn't greeted with a black screen when e.g. X dies. 279 * 280 * RETURNS: 281 * Zero if everything went ok, negative error code otherwise. 282 */ 283 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 284 { 285 return __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, false); 286 } 287 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); 288 289 #ifdef CONFIG_MAGIC_SYSRQ 290 /* 291 * restore fbcon display for all kms driver's using this helper, used for sysrq 292 * and panic handling. 293 */ 294 static bool drm_fb_helper_force_kernel_mode(void) 295 { 296 bool ret, error = false; 297 struct drm_fb_helper *helper; 298 299 if (list_empty(&kernel_fb_helper_list)) 300 return false; 301 302 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 303 struct drm_device *dev = helper->dev; 304 305 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 306 continue; 307 308 mutex_lock(&helper->lock); 309 ret = drm_client_modeset_commit_locked(&helper->client); 310 if (ret) 311 error = true; 312 mutex_unlock(&helper->lock); 313 } 314 return error; 315 } 316 317 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) 318 { 319 bool ret; 320 321 ret = drm_fb_helper_force_kernel_mode(); 322 if (ret == true) 323 DRM_ERROR("Failed to restore crtc configuration\n"); 324 } 325 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); 326 327 static void drm_fb_helper_sysrq(int dummy1) 328 { 329 schedule_work(&drm_fb_helper_restore_work); 330 } 331 332 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { 333 .handler = drm_fb_helper_sysrq, 334 .help_msg = "force-fb(V)", 335 .action_msg = "Restore framebuffer console", 336 }; 337 #else 338 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; 339 #endif 340 341 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) 342 { 343 struct drm_fb_helper *fb_helper = info->par; 344 345 mutex_lock(&fb_helper->lock); 346 drm_client_modeset_dpms(&fb_helper->client, dpms_mode); 347 mutex_unlock(&fb_helper->lock); 348 } 349 350 /** 351 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank 352 * @blank: desired blanking state 353 * @info: fbdev registered by the helper 354 */ 355 int drm_fb_helper_blank(int blank, struct fb_info *info) 356 { 357 if (oops_in_progress) 358 return -EBUSY; 359 360 switch (blank) { 361 /* Display: On; HSync: On, VSync: On */ 362 case FB_BLANK_UNBLANK: 363 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); 364 break; 365 /* Display: Off; HSync: On, VSync: On */ 366 case FB_BLANK_NORMAL: 367 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 368 break; 369 /* Display: Off; HSync: Off, VSync: On */ 370 case FB_BLANK_HSYNC_SUSPEND: 371 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 372 break; 373 /* Display: Off; HSync: On, VSync: Off */ 374 case FB_BLANK_VSYNC_SUSPEND: 375 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); 376 break; 377 /* Display: Off; HSync: Off, VSync: Off */ 378 case FB_BLANK_POWERDOWN: 379 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); 380 break; 381 } 382 return 0; 383 } 384 EXPORT_SYMBOL(drm_fb_helper_blank); 385 386 static void drm_fb_helper_resume_worker(struct work_struct *work) 387 { 388 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 389 resume_work); 390 391 console_lock(); 392 fb_set_suspend(helper->fbdev, 0); 393 console_unlock(); 394 } 395 396 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper, 397 struct drm_clip_rect *clip) 398 { 399 struct drm_framebuffer *fb = fb_helper->fb; 400 unsigned int cpp = fb->format->cpp[0]; 401 size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp; 402 void *src = fb_helper->fbdev->screen_buffer + offset; 403 void *dst = fb_helper->buffer->vaddr + offset; 404 size_t len = (clip->x2 - clip->x1) * cpp; 405 unsigned int y; 406 407 for (y = clip->y1; y < clip->y2; y++) { 408 if (!fb_helper->dev->mode_config.fbdev_use_iomem) 409 memcpy(dst, src, len); 410 else 411 memcpy_toio((void __iomem *)dst, src, len); 412 413 src += fb->pitches[0]; 414 dst += fb->pitches[0]; 415 } 416 } 417 418 static void drm_fb_helper_dirty_work(struct work_struct *work) 419 { 420 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 421 dirty_work); 422 struct drm_clip_rect *clip = &helper->dirty_clip; 423 struct drm_clip_rect clip_copy; 424 unsigned long flags; 425 void *vaddr; 426 427 spin_lock_irqsave(&helper->dirty_lock, flags); 428 clip_copy = *clip; 429 clip->x1 = clip->y1 = ~0; 430 clip->x2 = clip->y2 = 0; 431 spin_unlock_irqrestore(&helper->dirty_lock, flags); 432 433 /* call dirty callback only when it has been really touched */ 434 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) { 435 436 /* Generic fbdev uses a shadow buffer */ 437 if (helper->buffer) { 438 vaddr = drm_client_buffer_vmap(helper->buffer); 439 if (IS_ERR(vaddr)) 440 return; 441 drm_fb_helper_dirty_blit_real(helper, &clip_copy); 442 } 443 if (helper->fb->funcs->dirty) 444 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, 445 &clip_copy, 1); 446 447 if (helper->buffer) 448 drm_client_buffer_vunmap(helper->buffer); 449 } 450 } 451 452 /** 453 * drm_fb_helper_prepare - setup a drm_fb_helper structure 454 * @dev: DRM device 455 * @helper: driver-allocated fbdev helper structure to set up 456 * @funcs: pointer to structure of functions associate with this helper 457 * 458 * Sets up the bare minimum to make the framebuffer helper usable. This is 459 * useful to implement race-free initialization of the polling helpers. 460 */ 461 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 462 const struct drm_fb_helper_funcs *funcs) 463 { 464 INIT_LIST_HEAD(&helper->kernel_fb_list); 465 mtx_init(&helper->dirty_lock, IPL_TTY); 466 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); 467 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); 468 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; 469 rw_init(&helper->lock, "fbhlk"); 470 helper->funcs = funcs; 471 helper->dev = dev; 472 } 473 EXPORT_SYMBOL(drm_fb_helper_prepare); 474 475 /** 476 * drm_fb_helper_init - initialize a &struct drm_fb_helper 477 * @dev: drm device 478 * @fb_helper: driver-allocated fbdev helper structure to initialize 479 * 480 * This allocates the structures for the fbdev helper with the given limits. 481 * Note that this won't yet touch the hardware (through the driver interfaces) 482 * nor register the fbdev. This is only done in drm_fb_helper_initial_config() 483 * to allow driver writes more control over the exact init sequence. 484 * 485 * Drivers must call drm_fb_helper_prepare() before calling this function. 486 * 487 * RETURNS: 488 * Zero if everything went ok, nonzero otherwise. 489 */ 490 int drm_fb_helper_init(struct drm_device *dev, 491 struct drm_fb_helper *fb_helper) 492 { 493 int ret; 494 495 if (!drm_fbdev_emulation) { 496 dev->fb_helper = fb_helper; 497 return 0; 498 } 499 500 /* 501 * If this is not the generic fbdev client, initialize a drm_client 502 * without callbacks so we can use the modesets. 503 */ 504 if (!fb_helper->client.funcs) { 505 ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL); 506 if (ret) 507 return ret; 508 } 509 510 dev->fb_helper = fb_helper; 511 512 return 0; 513 } 514 EXPORT_SYMBOL(drm_fb_helper_init); 515 516 /** 517 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members 518 * @fb_helper: driver-allocated fbdev helper 519 * 520 * A helper to alloc fb_info and the members cmap and apertures. Called 521 * by the driver within the fb_probe fb_helper callback function. Drivers do not 522 * need to release the allocated fb_info structure themselves, this is 523 * automatically done when calling drm_fb_helper_fini(). 524 * 525 * RETURNS: 526 * fb_info pointer if things went okay, pointer containing error code 527 * otherwise 528 */ 529 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) 530 { 531 struct device *dev = fb_helper->dev->dev; 532 struct fb_info *info; 533 #ifdef __linux__ 534 int ret; 535 #endif 536 537 info = framebuffer_alloc(0, dev); 538 if (!info) 539 return ERR_PTR(-ENOMEM); 540 541 #ifdef __linux__ 542 ret = fb_alloc_cmap(&info->cmap, 256, 0); 543 if (ret) 544 goto err_release; 545 546 info->apertures = alloc_apertures(1); 547 if (!info->apertures) { 548 ret = -ENOMEM; 549 goto err_free_cmap; 550 } 551 #endif 552 553 fb_helper->fbdev = info; 554 info->skip_vt_switch = true; 555 556 return info; 557 558 #ifdef __linux__ 559 err_free_cmap: 560 fb_dealloc_cmap(&info->cmap); 561 err_release: 562 framebuffer_release(info); 563 return ERR_PTR(ret); 564 #endif 565 } 566 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); 567 568 /** 569 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device 570 * @fb_helper: driver-allocated fbdev helper, can be NULL 571 * 572 * A wrapper around unregister_framebuffer, to release the fb_info 573 * framebuffer device. This must be called before releasing all resources for 574 * @fb_helper by calling drm_fb_helper_fini(). 575 */ 576 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 577 { 578 if (fb_helper && fb_helper->fbdev) 579 unregister_framebuffer(fb_helper->fbdev); 580 } 581 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); 582 583 /** 584 * drm_fb_helper_fini - finialize a &struct drm_fb_helper 585 * @fb_helper: driver-allocated fbdev helper, can be NULL 586 * 587 * This cleans up all remaining resources associated with @fb_helper. 588 */ 589 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 590 { 591 struct fb_info *info; 592 593 if (!fb_helper) 594 return; 595 596 fb_helper->dev->fb_helper = NULL; 597 598 if (!drm_fbdev_emulation) 599 return; 600 601 cancel_work_sync(&fb_helper->resume_work); 602 cancel_work_sync(&fb_helper->dirty_work); 603 604 info = fb_helper->fbdev; 605 if (info) { 606 #ifdef __linux__ 607 if (info->cmap.len) 608 fb_dealloc_cmap(&info->cmap); 609 #endif 610 framebuffer_release(info); 611 } 612 fb_helper->fbdev = NULL; 613 614 mutex_lock(&kernel_fb_helper_lock); 615 if (!list_empty(&fb_helper->kernel_fb_list)) { 616 list_del(&fb_helper->kernel_fb_list); 617 if (list_empty(&kernel_fb_helper_list)) 618 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 619 } 620 mutex_unlock(&kernel_fb_helper_lock); 621 622 mutex_destroy(&fb_helper->lock); 623 624 if (!fb_helper->client.funcs) 625 drm_client_release(&fb_helper->client); 626 } 627 EXPORT_SYMBOL(drm_fb_helper_fini); 628 629 #ifdef __linux__ 630 631 static bool drm_fbdev_use_shadow_fb(struct drm_fb_helper *fb_helper) 632 { 633 struct drm_device *dev = fb_helper->dev; 634 struct drm_framebuffer *fb = fb_helper->fb; 635 636 return dev->mode_config.prefer_shadow_fbdev || 637 dev->mode_config.prefer_shadow || 638 fb->funcs->dirty; 639 } 640 641 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, 642 u32 width, u32 height) 643 { 644 struct drm_fb_helper *helper = info->par; 645 struct drm_clip_rect *clip = &helper->dirty_clip; 646 unsigned long flags; 647 648 if (!drm_fbdev_use_shadow_fb(helper)) 649 return; 650 651 spin_lock_irqsave(&helper->dirty_lock, flags); 652 clip->x1 = min_t(u32, clip->x1, x); 653 clip->y1 = min_t(u32, clip->y1, y); 654 clip->x2 = max_t(u32, clip->x2, x + width); 655 clip->y2 = max_t(u32, clip->y2, y + height); 656 spin_unlock_irqrestore(&helper->dirty_lock, flags); 657 658 schedule_work(&helper->dirty_work); 659 } 660 661 /** 662 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function 663 * @info: fb_info struct pointer 664 * @pagelist: list of dirty mmap framebuffer pages 665 * 666 * This function is used as the &fb_deferred_io.deferred_io 667 * callback function for flushing the fbdev mmap writes. 668 */ 669 void drm_fb_helper_deferred_io(struct fb_info *info, 670 struct list_head *pagelist) 671 { 672 unsigned long start, end, min, max; 673 struct vm_page *page; 674 u32 y1, y2; 675 676 min = ULONG_MAX; 677 max = 0; 678 list_for_each_entry(page, pagelist, lru) { 679 start = page->index << PAGE_SHIFT; 680 end = start + PAGE_SIZE - 1; 681 min = min(min, start); 682 max = max(max, end); 683 } 684 685 if (min < max) { 686 y1 = min / info->fix.line_length; 687 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), 688 info->var.yres); 689 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); 690 } 691 } 692 EXPORT_SYMBOL(drm_fb_helper_deferred_io); 693 694 /** 695 * drm_fb_helper_sys_read - wrapper around fb_sys_read 696 * @info: fb_info struct pointer 697 * @buf: userspace buffer to read from framebuffer memory 698 * @count: number of bytes to read from framebuffer memory 699 * @ppos: read offset within framebuffer memory 700 * 701 * A wrapper around fb_sys_read implemented by fbdev core 702 */ 703 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 704 size_t count, loff_t *ppos) 705 { 706 return fb_sys_read(info, buf, count, ppos); 707 } 708 EXPORT_SYMBOL(drm_fb_helper_sys_read); 709 710 /** 711 * drm_fb_helper_sys_write - wrapper around fb_sys_write 712 * @info: fb_info struct pointer 713 * @buf: userspace buffer to write to framebuffer memory 714 * @count: number of bytes to write to framebuffer memory 715 * @ppos: write offset within framebuffer memory 716 * 717 * A wrapper around fb_sys_write implemented by fbdev core 718 */ 719 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 720 size_t count, loff_t *ppos) 721 { 722 ssize_t ret; 723 724 ret = fb_sys_write(info, buf, count, ppos); 725 if (ret > 0) 726 drm_fb_helper_dirty(info, 0, 0, info->var.xres, 727 info->var.yres); 728 729 return ret; 730 } 731 EXPORT_SYMBOL(drm_fb_helper_sys_write); 732 733 /** 734 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect 735 * @info: fbdev registered by the helper 736 * @rect: info about rectangle to fill 737 * 738 * A wrapper around sys_fillrect implemented by fbdev core 739 */ 740 void drm_fb_helper_sys_fillrect(struct fb_info *info, 741 const struct fb_fillrect *rect) 742 { 743 sys_fillrect(info, rect); 744 drm_fb_helper_dirty(info, rect->dx, rect->dy, 745 rect->width, rect->height); 746 } 747 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); 748 749 /** 750 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea 751 * @info: fbdev registered by the helper 752 * @area: info about area to copy 753 * 754 * A wrapper around sys_copyarea implemented by fbdev core 755 */ 756 void drm_fb_helper_sys_copyarea(struct fb_info *info, 757 const struct fb_copyarea *area) 758 { 759 sys_copyarea(info, area); 760 drm_fb_helper_dirty(info, area->dx, area->dy, 761 area->width, area->height); 762 } 763 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); 764 765 /** 766 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit 767 * @info: fbdev registered by the helper 768 * @image: info about image to blit 769 * 770 * A wrapper around sys_imageblit implemented by fbdev core 771 */ 772 void drm_fb_helper_sys_imageblit(struct fb_info *info, 773 const struct fb_image *image) 774 { 775 sys_imageblit(info, image); 776 drm_fb_helper_dirty(info, image->dx, image->dy, 777 image->width, image->height); 778 } 779 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); 780 781 /** 782 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect 783 * @info: fbdev registered by the helper 784 * @rect: info about rectangle to fill 785 * 786 * A wrapper around cfb_fillrect implemented by fbdev core 787 */ 788 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 789 const struct fb_fillrect *rect) 790 { 791 cfb_fillrect(info, rect); 792 drm_fb_helper_dirty(info, rect->dx, rect->dy, 793 rect->width, rect->height); 794 } 795 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); 796 797 /** 798 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea 799 * @info: fbdev registered by the helper 800 * @area: info about area to copy 801 * 802 * A wrapper around cfb_copyarea implemented by fbdev core 803 */ 804 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 805 const struct fb_copyarea *area) 806 { 807 cfb_copyarea(info, area); 808 drm_fb_helper_dirty(info, area->dx, area->dy, 809 area->width, area->height); 810 } 811 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); 812 813 /** 814 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit 815 * @info: fbdev registered by the helper 816 * @image: info about image to blit 817 * 818 * A wrapper around cfb_imageblit implemented by fbdev core 819 */ 820 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 821 const struct fb_image *image) 822 { 823 cfb_imageblit(info, image); 824 drm_fb_helper_dirty(info, image->dx, image->dy, 825 image->width, image->height); 826 } 827 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); 828 829 #endif /* __linux__ */ 830 831 /** 832 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 833 * @fb_helper: driver-allocated fbdev helper, can be NULL 834 * @suspend: whether to suspend or resume 835 * 836 * A wrapper around fb_set_suspend implemented by fbdev core. 837 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take 838 * the lock yourself 839 */ 840 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) 841 { 842 if (fb_helper && fb_helper->fbdev) 843 fb_set_suspend(fb_helper->fbdev, suspend); 844 } 845 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 846 847 /** 848 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also 849 * takes the console lock 850 * @fb_helper: driver-allocated fbdev helper, can be NULL 851 * @suspend: whether to suspend or resume 852 * 853 * A wrapper around fb_set_suspend() that takes the console lock. If the lock 854 * isn't available on resume, a worker is tasked with waiting for the lock 855 * to become available. The console lock can be pretty contented on resume 856 * due to all the printk activity. 857 * 858 * This function can be called multiple times with the same state since 859 * &fb_info.state is checked to see if fbdev is running or not before locking. 860 * 861 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. 862 */ 863 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 864 bool suspend) 865 { 866 #ifdef __linux__ 867 if (!fb_helper || !fb_helper->fbdev) 868 return; 869 870 /* make sure there's no pending/ongoing resume */ 871 flush_work(&fb_helper->resume_work); 872 873 if (suspend) { 874 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) 875 return; 876 877 console_lock(); 878 879 } else { 880 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) 881 return; 882 883 if (!console_trylock()) { 884 schedule_work(&fb_helper->resume_work); 885 return; 886 } 887 } 888 889 fb_set_suspend(fb_helper->fbdev, suspend); 890 console_unlock(); 891 #endif 892 } 893 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); 894 895 #ifdef __linux__ 896 897 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) 898 { 899 u32 *palette = (u32 *)info->pseudo_palette; 900 int i; 901 902 if (cmap->start + cmap->len > 16) 903 return -EINVAL; 904 905 for (i = 0; i < cmap->len; ++i) { 906 u16 red = cmap->red[i]; 907 u16 green = cmap->green[i]; 908 u16 blue = cmap->blue[i]; 909 u32 value; 910 911 red >>= 16 - info->var.red.length; 912 green >>= 16 - info->var.green.length; 913 blue >>= 16 - info->var.blue.length; 914 value = (red << info->var.red.offset) | 915 (green << info->var.green.offset) | 916 (blue << info->var.blue.offset); 917 if (info->var.transp.length > 0) { 918 u32 mask = (1 << info->var.transp.length) - 1; 919 920 mask <<= info->var.transp.offset; 921 value |= mask; 922 } 923 palette[cmap->start + i] = value; 924 } 925 926 return 0; 927 } 928 929 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) 930 { 931 struct drm_fb_helper *fb_helper = info->par; 932 struct drm_mode_set *modeset; 933 struct drm_crtc *crtc; 934 u16 *r, *g, *b; 935 int ret = 0; 936 937 drm_modeset_lock_all(fb_helper->dev); 938 drm_client_for_each_modeset(modeset, &fb_helper->client) { 939 crtc = modeset->crtc; 940 if (!crtc->funcs->gamma_set || !crtc->gamma_size) 941 return -EINVAL; 942 943 if (cmap->start + cmap->len > crtc->gamma_size) 944 return -EINVAL; 945 946 r = crtc->gamma_store; 947 g = r + crtc->gamma_size; 948 b = g + crtc->gamma_size; 949 950 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 951 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 952 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 953 954 ret = crtc->funcs->gamma_set(crtc, r, g, b, 955 crtc->gamma_size, NULL); 956 if (ret) 957 return ret; 958 } 959 drm_modeset_unlock_all(fb_helper->dev); 960 961 return ret; 962 } 963 964 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, 965 struct fb_cmap *cmap) 966 { 967 struct drm_device *dev = crtc->dev; 968 struct drm_property_blob *gamma_lut; 969 struct drm_color_lut *lut; 970 int size = crtc->gamma_size; 971 int i; 972 973 if (!size || cmap->start + cmap->len > size) 974 return ERR_PTR(-EINVAL); 975 976 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); 977 if (IS_ERR(gamma_lut)) 978 return gamma_lut; 979 980 lut = gamma_lut->data; 981 if (cmap->start || cmap->len != size) { 982 u16 *r = crtc->gamma_store; 983 u16 *g = r + crtc->gamma_size; 984 u16 *b = g + crtc->gamma_size; 985 986 for (i = 0; i < cmap->start; i++) { 987 lut[i].red = r[i]; 988 lut[i].green = g[i]; 989 lut[i].blue = b[i]; 990 } 991 for (i = cmap->start + cmap->len; i < size; i++) { 992 lut[i].red = r[i]; 993 lut[i].green = g[i]; 994 lut[i].blue = b[i]; 995 } 996 } 997 998 for (i = 0; i < cmap->len; i++) { 999 lut[cmap->start + i].red = cmap->red[i]; 1000 lut[cmap->start + i].green = cmap->green[i]; 1001 lut[cmap->start + i].blue = cmap->blue[i]; 1002 } 1003 1004 return gamma_lut; 1005 } 1006 1007 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) 1008 { 1009 struct drm_fb_helper *fb_helper = info->par; 1010 struct drm_device *dev = fb_helper->dev; 1011 struct drm_property_blob *gamma_lut = NULL; 1012 struct drm_modeset_acquire_ctx ctx; 1013 struct drm_crtc_state *crtc_state; 1014 struct drm_atomic_state *state; 1015 struct drm_mode_set *modeset; 1016 struct drm_crtc *crtc; 1017 u16 *r, *g, *b; 1018 bool replaced; 1019 int ret = 0; 1020 1021 drm_modeset_acquire_init(&ctx, 0); 1022 1023 state = drm_atomic_state_alloc(dev); 1024 if (!state) { 1025 ret = -ENOMEM; 1026 goto out_ctx; 1027 } 1028 1029 state->acquire_ctx = &ctx; 1030 retry: 1031 drm_client_for_each_modeset(modeset, &fb_helper->client) { 1032 crtc = modeset->crtc; 1033 1034 if (!gamma_lut) 1035 gamma_lut = setcmap_new_gamma_lut(crtc, cmap); 1036 if (IS_ERR(gamma_lut)) { 1037 ret = PTR_ERR(gamma_lut); 1038 gamma_lut = NULL; 1039 goto out_state; 1040 } 1041 1042 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1043 if (IS_ERR(crtc_state)) { 1044 ret = PTR_ERR(crtc_state); 1045 goto out_state; 1046 } 1047 1048 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, 1049 NULL); 1050 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); 1051 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, 1052 gamma_lut); 1053 crtc_state->color_mgmt_changed |= replaced; 1054 } 1055 1056 ret = drm_atomic_commit(state); 1057 if (ret) 1058 goto out_state; 1059 1060 drm_client_for_each_modeset(modeset, &fb_helper->client) { 1061 crtc = modeset->crtc; 1062 1063 r = crtc->gamma_store; 1064 g = r + crtc->gamma_size; 1065 b = g + crtc->gamma_size; 1066 1067 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 1068 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 1069 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 1070 } 1071 1072 out_state: 1073 if (ret == -EDEADLK) 1074 goto backoff; 1075 1076 drm_property_blob_put(gamma_lut); 1077 drm_atomic_state_put(state); 1078 out_ctx: 1079 drm_modeset_drop_locks(&ctx); 1080 drm_modeset_acquire_fini(&ctx); 1081 1082 return ret; 1083 1084 backoff: 1085 drm_atomic_state_clear(state); 1086 drm_modeset_backoff(&ctx); 1087 goto retry; 1088 } 1089 1090 /** 1091 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap 1092 * @cmap: cmap to set 1093 * @info: fbdev registered by the helper 1094 */ 1095 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 1096 { 1097 struct drm_fb_helper *fb_helper = info->par; 1098 struct drm_device *dev = fb_helper->dev; 1099 int ret; 1100 1101 if (oops_in_progress) 1102 return -EBUSY; 1103 1104 mutex_lock(&fb_helper->lock); 1105 1106 if (!drm_master_internal_acquire(dev)) { 1107 ret = -EBUSY; 1108 goto unlock; 1109 } 1110 1111 mutex_lock(&fb_helper->client.modeset_mutex); 1112 if (info->fix.visual == FB_VISUAL_TRUECOLOR) 1113 ret = setcmap_pseudo_palette(cmap, info); 1114 else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) 1115 ret = setcmap_atomic(cmap, info); 1116 else 1117 ret = setcmap_legacy(cmap, info); 1118 mutex_unlock(&fb_helper->client.modeset_mutex); 1119 1120 drm_master_internal_release(dev); 1121 unlock: 1122 mutex_unlock(&fb_helper->lock); 1123 1124 return ret; 1125 } 1126 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1127 1128 /** 1129 * drm_fb_helper_ioctl - legacy ioctl implementation 1130 * @info: fbdev registered by the helper 1131 * @cmd: ioctl command 1132 * @arg: ioctl argument 1133 * 1134 * A helper to implement the standard fbdev ioctl. Only 1135 * FBIO_WAITFORVSYNC is implemented for now. 1136 */ 1137 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 1138 unsigned long arg) 1139 { 1140 struct drm_fb_helper *fb_helper = info->par; 1141 struct drm_device *dev = fb_helper->dev; 1142 struct drm_crtc *crtc; 1143 int ret = 0; 1144 1145 mutex_lock(&fb_helper->lock); 1146 if (!drm_master_internal_acquire(dev)) { 1147 ret = -EBUSY; 1148 goto unlock; 1149 } 1150 1151 switch (cmd) { 1152 case FBIO_WAITFORVSYNC: 1153 /* 1154 * Only consider the first CRTC. 1155 * 1156 * This ioctl is supposed to take the CRTC number as 1157 * an argument, but in fbdev times, what that number 1158 * was supposed to be was quite unclear, different 1159 * drivers were passing that argument differently 1160 * (some by reference, some by value), and most of the 1161 * userspace applications were just hardcoding 0 as an 1162 * argument. 1163 * 1164 * The first CRTC should be the integrated panel on 1165 * most drivers, so this is the best choice we can 1166 * make. If we're not smart enough here, one should 1167 * just consider switch the userspace to KMS. 1168 */ 1169 crtc = fb_helper->client.modesets[0].crtc; 1170 1171 /* 1172 * Only wait for a vblank event if the CRTC is 1173 * enabled, otherwise just don't do anythintg, 1174 * not even report an error. 1175 */ 1176 ret = drm_crtc_vblank_get(crtc); 1177 if (!ret) { 1178 drm_crtc_wait_one_vblank(crtc); 1179 drm_crtc_vblank_put(crtc); 1180 } 1181 1182 ret = 0; 1183 break; 1184 default: 1185 ret = -ENOTTY; 1186 } 1187 1188 drm_master_internal_release(dev); 1189 unlock: 1190 mutex_unlock(&fb_helper->lock); 1191 return ret; 1192 } 1193 EXPORT_SYMBOL(drm_fb_helper_ioctl); 1194 1195 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1, 1196 const struct fb_var_screeninfo *var_2) 1197 { 1198 return var_1->bits_per_pixel == var_2->bits_per_pixel && 1199 var_1->grayscale == var_2->grayscale && 1200 var_1->red.offset == var_2->red.offset && 1201 var_1->red.length == var_2->red.length && 1202 var_1->red.msb_right == var_2->red.msb_right && 1203 var_1->green.offset == var_2->green.offset && 1204 var_1->green.length == var_2->green.length && 1205 var_1->green.msb_right == var_2->green.msb_right && 1206 var_1->blue.offset == var_2->blue.offset && 1207 var_1->blue.length == var_2->blue.length && 1208 var_1->blue.msb_right == var_2->blue.msb_right && 1209 var_1->transp.offset == var_2->transp.offset && 1210 var_1->transp.length == var_2->transp.length && 1211 var_1->transp.msb_right == var_2->transp.msb_right; 1212 } 1213 1214 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var, 1215 u8 depth) 1216 { 1217 switch (depth) { 1218 case 8: 1219 var->red.offset = 0; 1220 var->green.offset = 0; 1221 var->blue.offset = 0; 1222 var->red.length = 8; /* 8bit DAC */ 1223 var->green.length = 8; 1224 var->blue.length = 8; 1225 var->transp.offset = 0; 1226 var->transp.length = 0; 1227 break; 1228 case 15: 1229 var->red.offset = 10; 1230 var->green.offset = 5; 1231 var->blue.offset = 0; 1232 var->red.length = 5; 1233 var->green.length = 5; 1234 var->blue.length = 5; 1235 var->transp.offset = 15; 1236 var->transp.length = 1; 1237 break; 1238 case 16: 1239 var->red.offset = 11; 1240 var->green.offset = 5; 1241 var->blue.offset = 0; 1242 var->red.length = 5; 1243 var->green.length = 6; 1244 var->blue.length = 5; 1245 var->transp.offset = 0; 1246 break; 1247 case 24: 1248 var->red.offset = 16; 1249 var->green.offset = 8; 1250 var->blue.offset = 0; 1251 var->red.length = 8; 1252 var->green.length = 8; 1253 var->blue.length = 8; 1254 var->transp.offset = 0; 1255 var->transp.length = 0; 1256 break; 1257 case 32: 1258 var->red.offset = 16; 1259 var->green.offset = 8; 1260 var->blue.offset = 0; 1261 var->red.length = 8; 1262 var->green.length = 8; 1263 var->blue.length = 8; 1264 var->transp.offset = 24; 1265 var->transp.length = 8; 1266 break; 1267 default: 1268 break; 1269 } 1270 } 1271 1272 /** 1273 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var 1274 * @var: screeninfo to check 1275 * @info: fbdev registered by the helper 1276 */ 1277 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1278 struct fb_info *info) 1279 { 1280 struct drm_fb_helper *fb_helper = info->par; 1281 struct drm_framebuffer *fb = fb_helper->fb; 1282 struct drm_device *dev = fb_helper->dev; 1283 1284 if (in_dbg_master()) 1285 return -EINVAL; 1286 1287 if (var->pixclock != 0) { 1288 drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n"); 1289 var->pixclock = 0; 1290 } 1291 1292 if ((drm_format_info_block_width(fb->format, 0) > 1) || 1293 (drm_format_info_block_height(fb->format, 0) > 1)) 1294 return -EINVAL; 1295 1296 /* 1297 * Changes struct fb_var_screeninfo are currently not pushed back 1298 * to KMS, hence fail if different settings are requested. 1299 */ 1300 if (var->bits_per_pixel > fb->format->cpp[0] * 8 || 1301 var->xres > fb->width || var->yres > fb->height || 1302 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1303 drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb " 1304 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1305 var->xres, var->yres, var->bits_per_pixel, 1306 var->xres_virtual, var->yres_virtual, 1307 fb->width, fb->height, fb->format->cpp[0] * 8); 1308 return -EINVAL; 1309 } 1310 1311 /* 1312 * Workaround for SDL 1.2, which is known to be setting all pixel format 1313 * fields values to zero in some cases. We treat this situation as a 1314 * kind of "use some reasonable autodetected values". 1315 */ 1316 if (!var->red.offset && !var->green.offset && 1317 !var->blue.offset && !var->transp.offset && 1318 !var->red.length && !var->green.length && 1319 !var->blue.length && !var->transp.length && 1320 !var->red.msb_right && !var->green.msb_right && 1321 !var->blue.msb_right && !var->transp.msb_right) { 1322 drm_fb_helper_fill_pixel_fmt(var, fb->format->depth); 1323 } 1324 1325 /* 1326 * Likewise, bits_per_pixel should be rounded up to a supported value. 1327 */ 1328 var->bits_per_pixel = fb->format->cpp[0] * 8; 1329 1330 /* 1331 * drm fbdev emulation doesn't support changing the pixel format at all, 1332 * so reject all pixel format changing requests. 1333 */ 1334 if (!drm_fb_pixel_format_equal(var, &info->var)) { 1335 drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n"); 1336 return -EINVAL; 1337 } 1338 1339 return 0; 1340 } 1341 EXPORT_SYMBOL(drm_fb_helper_check_var); 1342 1343 #endif /* __linux__ */ 1344 1345 /** 1346 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par 1347 * @info: fbdev registered by the helper 1348 * 1349 * This will let fbcon do the mode init and is called at initialization time by 1350 * the fbdev core when registering the driver, and later on through the hotplug 1351 * callback. 1352 */ 1353 int drm_fb_helper_set_par(struct fb_info *info) 1354 { 1355 struct drm_fb_helper *fb_helper = info->par; 1356 struct fb_var_screeninfo *var = &info->var; 1357 bool force; 1358 1359 if (oops_in_progress) 1360 return -EBUSY; 1361 1362 if (var->pixclock != 0) { 1363 drm_err(fb_helper->dev, "PIXEL CLOCK SET\n"); 1364 return -EINVAL; 1365 } 1366 1367 /* 1368 * Normally we want to make sure that a kms master takes precedence over 1369 * fbdev, to avoid fbdev flickering and occasionally stealing the 1370 * display status. But Xorg first sets the vt back to text mode using 1371 * the KDSET IOCTL with KD_TEXT, and only after that drops the master 1372 * status when exiting. 1373 * 1374 * In the past this was caught by drm_fb_helper_lastclose(), but on 1375 * modern systems where logind always keeps a drm fd open to orchestrate 1376 * the vt switching, this doesn't work. 1377 * 1378 * To not break the userspace ABI we have this special case here, which 1379 * is only used for the above case. Everything else uses the normal 1380 * commit function, which ensures that we never steal the display from 1381 * an active drm master. 1382 */ 1383 #ifdef __linux__ 1384 force = var->activate & FB_ACTIVATE_KD_TEXT; 1385 #else 1386 force = true; 1387 #endif 1388 1389 __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force); 1390 1391 return 0; 1392 } 1393 EXPORT_SYMBOL(drm_fb_helper_set_par); 1394 1395 #ifdef notyet 1396 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y) 1397 { 1398 struct drm_mode_set *mode_set; 1399 1400 mutex_lock(&fb_helper->client.modeset_mutex); 1401 drm_client_for_each_modeset(mode_set, &fb_helper->client) { 1402 mode_set->x = x; 1403 mode_set->y = y; 1404 } 1405 mutex_unlock(&fb_helper->client.modeset_mutex); 1406 } 1407 #endif 1408 1409 static int pan_display_atomic(struct fb_var_screeninfo *var, 1410 struct fb_info *info) 1411 { 1412 STUB(); 1413 return -ENOSYS; 1414 #ifdef notyet 1415 struct drm_fb_helper *fb_helper = info->par; 1416 int ret; 1417 1418 pan_set(fb_helper, var->xoffset, var->yoffset); 1419 1420 ret = drm_client_modeset_commit_locked(&fb_helper->client); 1421 if (!ret) { 1422 info->var.xoffset = var->xoffset; 1423 info->var.yoffset = var->yoffset; 1424 } else 1425 pan_set(fb_helper, info->var.xoffset, info->var.yoffset); 1426 1427 return ret; 1428 #endif 1429 } 1430 1431 static int pan_display_legacy(struct fb_var_screeninfo *var, 1432 struct fb_info *info) 1433 { 1434 STUB(); 1435 return -ENOSYS; 1436 #ifdef notyet 1437 struct drm_fb_helper *fb_helper = info->par; 1438 struct drm_client_dev *client = &fb_helper->client; 1439 struct drm_mode_set *modeset; 1440 int ret = 0; 1441 1442 mutex_lock(&client->modeset_mutex); 1443 drm_modeset_lock_all(fb_helper->dev); 1444 drm_client_for_each_modeset(modeset, client) { 1445 modeset->x = var->xoffset; 1446 modeset->y = var->yoffset; 1447 1448 if (modeset->num_connectors) { 1449 ret = drm_mode_set_config_internal(modeset); 1450 if (!ret) { 1451 info->var.xoffset = var->xoffset; 1452 info->var.yoffset = var->yoffset; 1453 } 1454 } 1455 } 1456 drm_modeset_unlock_all(fb_helper->dev); 1457 mutex_unlock(&client->modeset_mutex); 1458 1459 return ret; 1460 #endif 1461 } 1462 1463 /** 1464 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display 1465 * @var: updated screen information 1466 * @info: fbdev registered by the helper 1467 */ 1468 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1469 struct fb_info *info) 1470 { 1471 struct drm_fb_helper *fb_helper = info->par; 1472 struct drm_device *dev = fb_helper->dev; 1473 int ret; 1474 1475 if (oops_in_progress) 1476 return -EBUSY; 1477 1478 mutex_lock(&fb_helper->lock); 1479 if (!drm_master_internal_acquire(dev)) { 1480 ret = -EBUSY; 1481 goto unlock; 1482 } 1483 1484 if (drm_drv_uses_atomic_modeset(dev)) 1485 ret = pan_display_atomic(var, info); 1486 else 1487 ret = pan_display_legacy(var, info); 1488 1489 drm_master_internal_release(dev); 1490 unlock: 1491 mutex_unlock(&fb_helper->lock); 1492 1493 return ret; 1494 } 1495 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1496 1497 /* 1498 * Allocates the backing storage and sets up the fbdev info structure through 1499 * the ->fb_probe callback. 1500 */ 1501 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1502 int preferred_bpp) 1503 { 1504 struct drm_client_dev *client = &fb_helper->client; 1505 struct drm_device *dev = fb_helper->dev; 1506 int ret = 0; 1507 int crtc_count = 0; 1508 struct drm_connector_list_iter conn_iter; 1509 struct drm_fb_helper_surface_size sizes; 1510 struct drm_connector *connector; 1511 struct drm_mode_set *mode_set; 1512 int best_depth = 0; 1513 1514 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1515 sizes.surface_depth = 24; 1516 sizes.surface_bpp = 32; 1517 sizes.fb_width = (u32)-1; 1518 sizes.fb_height = (u32)-1; 1519 1520 /* 1521 * If driver picks 8 or 16 by default use that for both depth/bpp 1522 * to begin with 1523 */ 1524 if (preferred_bpp != sizes.surface_bpp) 1525 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1526 1527 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); 1528 drm_client_for_each_connector_iter(connector, &conn_iter) { 1529 struct drm_cmdline_mode *cmdline_mode; 1530 1531 cmdline_mode = &connector->cmdline_mode; 1532 1533 if (cmdline_mode->bpp_specified) { 1534 switch (cmdline_mode->bpp) { 1535 case 8: 1536 sizes.surface_depth = sizes.surface_bpp = 8; 1537 break; 1538 case 15: 1539 sizes.surface_depth = 15; 1540 sizes.surface_bpp = 16; 1541 break; 1542 case 16: 1543 sizes.surface_depth = sizes.surface_bpp = 16; 1544 break; 1545 case 24: 1546 sizes.surface_depth = sizes.surface_bpp = 24; 1547 break; 1548 case 32: 1549 sizes.surface_depth = 24; 1550 sizes.surface_bpp = 32; 1551 break; 1552 } 1553 break; 1554 } 1555 } 1556 drm_connector_list_iter_end(&conn_iter); 1557 1558 /* 1559 * If we run into a situation where, for example, the primary plane 1560 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth 1561 * 16) we need to scale down the depth of the sizes we request. 1562 */ 1563 mutex_lock(&client->modeset_mutex); 1564 drm_client_for_each_modeset(mode_set, client) { 1565 struct drm_crtc *crtc = mode_set->crtc; 1566 struct drm_plane *plane = crtc->primary; 1567 int j; 1568 1569 drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc)); 1570 1571 for (j = 0; j < plane->format_count; j++) { 1572 const struct drm_format_info *fmt; 1573 1574 fmt = drm_format_info(plane->format_types[j]); 1575 1576 /* 1577 * Do not consider YUV or other complicated formats 1578 * for framebuffers. This means only legacy formats 1579 * are supported (fmt->depth is a legacy field) but 1580 * the framebuffer emulation can only deal with such 1581 * formats, specifically RGB/BGA formats. 1582 */ 1583 if (fmt->depth == 0) 1584 continue; 1585 1586 /* We found a perfect fit, great */ 1587 if (fmt->depth == sizes.surface_depth) { 1588 best_depth = fmt->depth; 1589 break; 1590 } 1591 1592 /* Skip depths above what we're looking for */ 1593 if (fmt->depth > sizes.surface_depth) 1594 continue; 1595 1596 /* Best depth found so far */ 1597 if (fmt->depth > best_depth) 1598 best_depth = fmt->depth; 1599 } 1600 } 1601 if (sizes.surface_depth != best_depth && best_depth) { 1602 drm_info(dev, "requested bpp %d, scaled depth down to %d", 1603 sizes.surface_bpp, best_depth); 1604 sizes.surface_depth = best_depth; 1605 } 1606 1607 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1608 crtc_count = 0; 1609 drm_client_for_each_modeset(mode_set, client) { 1610 struct drm_display_mode *desired_mode; 1611 int x, y, j; 1612 /* in case of tile group, are we the last tile vert or horiz? 1613 * If no tile group you are always the last one both vertically 1614 * and horizontally 1615 */ 1616 bool lastv = true, lasth = true; 1617 1618 desired_mode = mode_set->mode; 1619 1620 if (!desired_mode) 1621 continue; 1622 1623 crtc_count++; 1624 1625 x = mode_set->x; 1626 y = mode_set->y; 1627 1628 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); 1629 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); 1630 1631 for (j = 0; j < mode_set->num_connectors; j++) { 1632 struct drm_connector *connector = mode_set->connectors[j]; 1633 1634 if (connector->has_tile && 1635 desired_mode->hdisplay == connector->tile_h_size && 1636 desired_mode->vdisplay == connector->tile_v_size) { 1637 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1638 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1639 /* cloning to multiple tiles is just crazy-talk, so: */ 1640 break; 1641 } 1642 } 1643 1644 if (lasth) 1645 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); 1646 if (lastv) 1647 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); 1648 } 1649 mutex_unlock(&client->modeset_mutex); 1650 1651 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1652 #ifdef __linux__ 1653 drm_info(dev, "Cannot find any crtc or sizes\n"); 1654 1655 /* First time: disable all crtc's.. */ 1656 if (!fb_helper->deferred_setup) 1657 drm_client_modeset_commit(client); 1658 return -EAGAIN; 1659 #else 1660 drm_info(dev, "Cannot find any crtc or sizes - going 1024x768\n"); 1661 sizes.fb_width = sizes.surface_width = 1024; 1662 sizes.fb_height = sizes.surface_height = 768; 1663 #endif 1664 } 1665 1666 /* Handle our overallocation */ 1667 sizes.surface_height *= drm_fbdev_overalloc; 1668 sizes.surface_height /= 100; 1669 1670 /* push down into drivers */ 1671 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1672 if (ret < 0) 1673 return ret; 1674 1675 #ifdef __linux__ 1676 strcpy(fb_helper->fb->comm, "[fbcon]"); 1677 #endif 1678 return 0; 1679 } 1680 1681 #ifdef __linux__ 1682 1683 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1684 uint32_t depth) 1685 { 1686 info->fix.type = FB_TYPE_PACKED_PIXELS; 1687 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1688 FB_VISUAL_TRUECOLOR; 1689 info->fix.mmio_start = 0; 1690 info->fix.mmio_len = 0; 1691 info->fix.type_aux = 0; 1692 info->fix.xpanstep = 1; /* doing it in hw */ 1693 info->fix.ypanstep = 1; /* doing it in hw */ 1694 info->fix.ywrapstep = 0; 1695 info->fix.accel = FB_ACCEL_NONE; 1696 1697 info->fix.line_length = pitch; 1698 } 1699 1700 static void drm_fb_helper_fill_var(struct fb_info *info, 1701 struct drm_fb_helper *fb_helper, 1702 uint32_t fb_width, uint32_t fb_height) 1703 { 1704 struct drm_framebuffer *fb = fb_helper->fb; 1705 1706 WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) || 1707 (drm_format_info_block_height(fb->format, 0) > 1)); 1708 info->pseudo_palette = fb_helper->pseudo_palette; 1709 info->var.xres_virtual = fb->width; 1710 info->var.yres_virtual = fb->height; 1711 info->var.bits_per_pixel = fb->format->cpp[0] * 8; 1712 info->var.accel_flags = FB_ACCELF_TEXT; 1713 info->var.xoffset = 0; 1714 info->var.yoffset = 0; 1715 info->var.activate = FB_ACTIVATE_NOW; 1716 1717 drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth); 1718 1719 info->var.xres = fb_width; 1720 info->var.yres = fb_height; 1721 } 1722 1723 #endif /* __linux__ */ 1724 1725 /** 1726 * drm_fb_helper_fill_info - initializes fbdev information 1727 * @info: fbdev instance to set up 1728 * @fb_helper: fb helper instance to use as template 1729 * @sizes: describes fbdev size and scanout surface size 1730 * 1731 * Sets up the variable and fixed fbdev metainformation from the given fb helper 1732 * instance and the drm framebuffer allocated in &drm_fb_helper.fb. 1733 * 1734 * Drivers should call this (or their equivalent setup code) from their 1735 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev 1736 * backing storage framebuffer. 1737 */ 1738 void drm_fb_helper_fill_info(struct fb_info *info, 1739 struct drm_fb_helper *fb_helper, 1740 struct drm_fb_helper_surface_size *sizes) 1741 { 1742 #ifdef __linux__ 1743 struct drm_framebuffer *fb = fb_helper->fb; 1744 1745 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth); 1746 drm_fb_helper_fill_var(info, fb_helper, 1747 sizes->fb_width, sizes->fb_height); 1748 #endif 1749 1750 info->par = fb_helper; 1751 #ifdef __linux__ 1752 snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb", 1753 fb_helper->dev->driver->name); 1754 #endif 1755 1756 } 1757 EXPORT_SYMBOL(drm_fb_helper_fill_info); 1758 1759 /* 1760 * This is a continuation of drm_setup_crtcs() that sets up anything related 1761 * to the framebuffer. During initialization, drm_setup_crtcs() is called before 1762 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). 1763 * So, any setup that touches those fields needs to be done here instead of in 1764 * drm_setup_crtcs(). 1765 */ 1766 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) 1767 { 1768 struct drm_client_dev *client = &fb_helper->client; 1769 struct drm_connector_list_iter conn_iter; 1770 struct fb_info *info = fb_helper->fbdev; 1771 unsigned int rotation, sw_rotations = 0; 1772 struct drm_connector *connector; 1773 struct drm_mode_set *modeset; 1774 1775 mutex_lock(&client->modeset_mutex); 1776 drm_client_for_each_modeset(modeset, client) { 1777 if (!modeset->num_connectors) 1778 continue; 1779 1780 modeset->fb = fb_helper->fb; 1781 1782 if (drm_client_rotation(modeset, &rotation)) 1783 /* Rotating in hardware, fbcon should not rotate */ 1784 sw_rotations |= DRM_MODE_ROTATE_0; 1785 else 1786 sw_rotations |= rotation; 1787 } 1788 mutex_unlock(&client->modeset_mutex); 1789 1790 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); 1791 drm_client_for_each_connector_iter(connector, &conn_iter) { 1792 1793 /* use first connected connector for the physical dimensions */ 1794 if (connector->status == connector_status_connected) { 1795 info->var.width = connector->display_info.width_mm; 1796 info->var.height = connector->display_info.height_mm; 1797 break; 1798 } 1799 } 1800 drm_connector_list_iter_end(&conn_iter); 1801 1802 switch (sw_rotations) { 1803 case DRM_MODE_ROTATE_0: 1804 info->fbcon_rotate_hint = FB_ROTATE_UR; 1805 break; 1806 case DRM_MODE_ROTATE_90: 1807 info->fbcon_rotate_hint = FB_ROTATE_CCW; 1808 break; 1809 case DRM_MODE_ROTATE_180: 1810 info->fbcon_rotate_hint = FB_ROTATE_UD; 1811 break; 1812 case DRM_MODE_ROTATE_270: 1813 info->fbcon_rotate_hint = FB_ROTATE_CW; 1814 break; 1815 default: 1816 /* 1817 * Multiple bits are set / multiple rotations requested 1818 * fbcon cannot handle separate rotation settings per 1819 * output, so fallback to unrotated. 1820 */ 1821 info->fbcon_rotate_hint = FB_ROTATE_UR; 1822 } 1823 } 1824 1825 /* Note: Drops fb_helper->lock before returning. */ 1826 static int 1827 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, 1828 int bpp_sel) 1829 { 1830 struct drm_device *dev = fb_helper->dev; 1831 struct fb_info *info; 1832 unsigned int width, height; 1833 int ret; 1834 1835 width = dev->mode_config.max_width; 1836 height = dev->mode_config.max_height; 1837 1838 drm_client_modeset_probe(&fb_helper->client, width, height); 1839 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 1840 if (ret < 0) { 1841 if (ret == -EAGAIN) { 1842 fb_helper->preferred_bpp = bpp_sel; 1843 fb_helper->deferred_setup = true; 1844 ret = 0; 1845 } 1846 mutex_unlock(&fb_helper->lock); 1847 1848 return ret; 1849 } 1850 drm_setup_crtcs_fb(fb_helper); 1851 1852 fb_helper->deferred_setup = false; 1853 1854 info = fb_helper->fbdev; 1855 info->var.pixclock = 0; 1856 /* Shamelessly allow physical address leaking to userspace */ 1857 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 1858 if (!drm_leak_fbdev_smem) 1859 #endif 1860 /* don't leak any physical addresses to userspace */ 1861 info->flags |= FBINFO_HIDE_SMEM_START; 1862 1863 /* Need to drop locks to avoid recursive deadlock in 1864 * register_framebuffer. This is ok because the only thing left to do is 1865 * register the fbdev emulation instance in kernel_fb_helper_list. */ 1866 mutex_unlock(&fb_helper->lock); 1867 1868 ret = register_framebuffer(info); 1869 if (ret < 0) 1870 return ret; 1871 1872 #ifdef __linux__ 1873 dev_info(dev->dev, "fb%d: %s frame buffer device\n", 1874 info->node, info->fix.id); 1875 #endif 1876 1877 mutex_lock(&kernel_fb_helper_lock); 1878 if (list_empty(&kernel_fb_helper_list)) 1879 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 1880 1881 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1882 mutex_unlock(&kernel_fb_helper_lock); 1883 1884 return 0; 1885 } 1886 1887 /** 1888 * drm_fb_helper_initial_config - setup a sane initial connector configuration 1889 * @fb_helper: fb_helper device struct 1890 * @bpp_sel: bpp value to use for the framebuffer configuration 1891 * 1892 * Scans the CRTCs and connectors and tries to put together an initial setup. 1893 * At the moment, this is a cloned configuration across all heads with 1894 * a new framebuffer object as the backing store. 1895 * 1896 * Note that this also registers the fbdev and so allows userspace to call into 1897 * the driver through the fbdev interfaces. 1898 * 1899 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback 1900 * to let the driver allocate and initialize the fbdev info structure and the 1901 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided 1902 * as a helper to setup simple default values for the fbdev info structure. 1903 * 1904 * HANG DEBUGGING: 1905 * 1906 * When you have fbcon support built-in or already loaded, this function will do 1907 * a full modeset to setup the fbdev console. Due to locking misdesign in the 1908 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 1909 * console_lock. Until console_unlock is called no dmesg lines will be sent out 1910 * to consoles, not even serial console. This means when your driver crashes, 1911 * you will see absolutely nothing else but a system stuck in this function, 1912 * with no further output. Any kind of printk() you place within your own driver 1913 * or in the drm core modeset code will also never show up. 1914 * 1915 * Standard debug practice is to run the fbcon setup without taking the 1916 * console_lock as a hack, to be able to see backtraces and crashes on the 1917 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 1918 * cmdline option. 1919 * 1920 * The other option is to just disable fbdev emulation since very likely the 1921 * first modeset from userspace will crash in the same way, and is even easier 1922 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 1923 * kernel cmdline option. 1924 * 1925 * RETURNS: 1926 * Zero if everything went ok, nonzero otherwise. 1927 */ 1928 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 1929 { 1930 int ret; 1931 1932 if (!drm_fbdev_emulation) 1933 return 0; 1934 1935 mutex_lock(&fb_helper->lock); 1936 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); 1937 1938 return ret; 1939 } 1940 EXPORT_SYMBOL(drm_fb_helper_initial_config); 1941 1942 /** 1943 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 1944 * probing all the outputs attached to the fb 1945 * @fb_helper: driver-allocated fbdev helper, can be NULL 1946 * 1947 * Scan the connectors attached to the fb_helper and try to put together a 1948 * setup after notification of a change in output configuration. 1949 * 1950 * Called at runtime, takes the mode config locks to be able to check/change the 1951 * modeset configuration. Must be run from process context (which usually means 1952 * either the output polling work or a work item launched from the driver's 1953 * hotplug interrupt). 1954 * 1955 * Note that drivers may call this even before calling 1956 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 1957 * for a race-free fbcon setup and will make sure that the fbdev emulation will 1958 * not miss any hotplug events. 1959 * 1960 * RETURNS: 1961 * 0 on success and a non-zero error code otherwise. 1962 */ 1963 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 1964 { 1965 struct fb_info *fbi; 1966 int err = 0; 1967 1968 if (!drm_fbdev_emulation || !fb_helper) 1969 return 0; 1970 1971 mutex_lock(&fb_helper->lock); 1972 if (fb_helper->deferred_setup) { 1973 err = __drm_fb_helper_initial_config_and_unlock(fb_helper, 1974 fb_helper->preferred_bpp); 1975 return err; 1976 } 1977 1978 if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) { 1979 fb_helper->delayed_hotplug = true; 1980 mutex_unlock(&fb_helper->lock); 1981 return err; 1982 } 1983 1984 drm_master_internal_release(fb_helper->dev); 1985 1986 drm_dbg_kms(fb_helper->dev, "\n"); 1987 1988 drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height); 1989 drm_setup_crtcs_fb(fb_helper); 1990 mutex_unlock(&fb_helper->lock); 1991 1992 fbi = fb_helper->fbdev; 1993 if (fbi->fbops && fbi->fbops->fb_set_par) 1994 fbi->fbops->fb_set_par(fbi); 1995 else 1996 drm_fb_helper_set_par(fb_helper->fbdev); 1997 1998 return 0; 1999 } 2000 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2001 2002 /** 2003 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation 2004 * @dev: DRM device 2005 * 2006 * This function can be used as the &drm_driver->lastclose callback for drivers 2007 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked(). 2008 */ 2009 void drm_fb_helper_lastclose(struct drm_device *dev) 2010 { 2011 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper); 2012 } 2013 EXPORT_SYMBOL(drm_fb_helper_lastclose); 2014 2015 /** 2016 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed 2017 * helper for fbdev emulation 2018 * @dev: DRM device 2019 * 2020 * This function can be used as the 2021 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only 2022 * need to call drm_fb_helper_hotplug_event(). 2023 */ 2024 void drm_fb_helper_output_poll_changed(struct drm_device *dev) 2025 { 2026 drm_fb_helper_hotplug_event(dev->fb_helper); 2027 } 2028 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed); 2029 2030 #ifdef __linux__ 2031 /* @user: 1=userspace, 0=fbcon */ 2032 static int drm_fbdev_fb_open(struct fb_info *info, int user) 2033 { 2034 struct drm_fb_helper *fb_helper = info->par; 2035 2036 /* No need to take a ref for fbcon because it unbinds on unregister */ 2037 if (user && !try_module_get(fb_helper->dev->driver->fops->owner)) 2038 return -ENODEV; 2039 2040 return 0; 2041 } 2042 2043 static int drm_fbdev_fb_release(struct fb_info *info, int user) 2044 { 2045 struct drm_fb_helper *fb_helper = info->par; 2046 2047 if (user) 2048 module_put(fb_helper->dev->driver->fops->owner); 2049 2050 return 0; 2051 } 2052 2053 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper) 2054 { 2055 struct fb_info *fbi = fb_helper->fbdev; 2056 void *shadow = NULL; 2057 2058 if (!fb_helper->dev) 2059 return; 2060 2061 if (fbi && fbi->fbdefio) { 2062 fb_deferred_io_cleanup(fbi); 2063 shadow = fbi->screen_buffer; 2064 } 2065 2066 drm_fb_helper_fini(fb_helper); 2067 2068 vfree(shadow); 2069 2070 drm_client_framebuffer_delete(fb_helper->buffer); 2071 } 2072 2073 static void drm_fbdev_release(struct drm_fb_helper *fb_helper) 2074 { 2075 drm_fbdev_cleanup(fb_helper); 2076 drm_client_release(&fb_helper->client); 2077 kfree(fb_helper); 2078 } 2079 2080 /* 2081 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of 2082 * unregister_framebuffer() or fb_release(). 2083 */ 2084 static void drm_fbdev_fb_destroy(struct fb_info *info) 2085 { 2086 drm_fbdev_release(info->par); 2087 } 2088 2089 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) 2090 { 2091 struct drm_fb_helper *fb_helper = info->par; 2092 2093 if (fb_helper->dev->driver->gem_prime_mmap) 2094 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma); 2095 else 2096 return -ENODEV; 2097 } 2098 2099 static const struct fb_ops drm_fbdev_fb_ops = { 2100 .owner = THIS_MODULE, 2101 DRM_FB_HELPER_DEFAULT_OPS, 2102 .fb_open = drm_fbdev_fb_open, 2103 .fb_release = drm_fbdev_fb_release, 2104 .fb_destroy = drm_fbdev_fb_destroy, 2105 .fb_mmap = drm_fbdev_fb_mmap, 2106 .fb_read = drm_fb_helper_sys_read, 2107 .fb_write = drm_fb_helper_sys_write, 2108 .fb_fillrect = drm_fb_helper_sys_fillrect, 2109 .fb_copyarea = drm_fb_helper_sys_copyarea, 2110 .fb_imageblit = drm_fb_helper_sys_imageblit, 2111 }; 2112 2113 static struct fb_deferred_io drm_fbdev_defio = { 2114 .delay = HZ / 20, 2115 .deferred_io = drm_fb_helper_deferred_io, 2116 }; 2117 2118 /* 2119 * This function uses the client API to create a framebuffer backed by a dumb buffer. 2120 * 2121 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect, 2122 * fb_copyarea, fb_imageblit. 2123 */ 2124 static int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, 2125 struct drm_fb_helper_surface_size *sizes) 2126 { 2127 struct drm_client_dev *client = &fb_helper->client; 2128 struct drm_device *dev = fb_helper->dev; 2129 struct drm_client_buffer *buffer; 2130 struct drm_framebuffer *fb; 2131 struct fb_info *fbi; 2132 u32 format; 2133 void *vaddr; 2134 2135 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n", 2136 sizes->surface_width, sizes->surface_height, 2137 sizes->surface_bpp); 2138 2139 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); 2140 buffer = drm_client_framebuffer_create(client, sizes->surface_width, 2141 sizes->surface_height, format); 2142 if (IS_ERR(buffer)) 2143 return PTR_ERR(buffer); 2144 2145 fb_helper->buffer = buffer; 2146 fb_helper->fb = buffer->fb; 2147 fb = buffer->fb; 2148 2149 fbi = drm_fb_helper_alloc_fbi(fb_helper); 2150 if (IS_ERR(fbi)) 2151 return PTR_ERR(fbi); 2152 2153 fbi->fbops = &drm_fbdev_fb_ops; 2154 fbi->screen_size = fb->height * fb->pitches[0]; 2155 fbi->fix.smem_len = fbi->screen_size; 2156 2157 drm_fb_helper_fill_info(fbi, fb_helper, sizes); 2158 2159 if (drm_fbdev_use_shadow_fb(fb_helper)) { 2160 fbi->screen_buffer = vzalloc(fbi->screen_size); 2161 if (!fbi->screen_buffer) 2162 return -ENOMEM; 2163 2164 fbi->fbdefio = &drm_fbdev_defio; 2165 2166 fb_deferred_io_init(fbi); 2167 } else { 2168 /* buffer is mapped for HW framebuffer */ 2169 vaddr = drm_client_buffer_vmap(fb_helper->buffer); 2170 if (IS_ERR(vaddr)) 2171 return PTR_ERR(vaddr); 2172 2173 fbi->screen_buffer = vaddr; 2174 /* Shamelessly leak the physical address to user-space */ 2175 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 2176 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0) 2177 fbi->fix.smem_start = 2178 page_to_phys(virt_to_page(fbi->screen_buffer)); 2179 #endif 2180 } 2181 2182 return 0; 2183 } 2184 2185 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { 2186 .fb_probe = drm_fb_helper_generic_probe, 2187 }; 2188 2189 static void drm_fbdev_client_unregister(struct drm_client_dev *client) 2190 { 2191 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 2192 2193 if (fb_helper->fbdev) 2194 /* drm_fbdev_fb_destroy() takes care of cleanup */ 2195 drm_fb_helper_unregister_fbi(fb_helper); 2196 else 2197 drm_fbdev_release(fb_helper); 2198 } 2199 2200 static int drm_fbdev_client_restore(struct drm_client_dev *client) 2201 { 2202 drm_fb_helper_lastclose(client->dev); 2203 2204 return 0; 2205 } 2206 2207 static int drm_fbdev_client_hotplug(struct drm_client_dev *client) 2208 { 2209 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 2210 struct drm_device *dev = client->dev; 2211 int ret; 2212 2213 /* Setup is not retried if it has failed */ 2214 if (!fb_helper->dev && fb_helper->funcs) 2215 return 0; 2216 2217 if (dev->fb_helper) 2218 return drm_fb_helper_hotplug_event(dev->fb_helper); 2219 2220 if (!dev->mode_config.num_connector) { 2221 drm_dbg_kms(dev, "No connectors found, will not create framebuffer!\n"); 2222 return 0; 2223 } 2224 2225 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs); 2226 2227 ret = drm_fb_helper_init(dev, fb_helper); 2228 if (ret) 2229 goto err; 2230 2231 if (!drm_drv_uses_atomic_modeset(dev)) 2232 drm_helper_disable_unused_functions(dev); 2233 2234 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp); 2235 if (ret) 2236 goto err_cleanup; 2237 2238 return 0; 2239 2240 err_cleanup: 2241 drm_fbdev_cleanup(fb_helper); 2242 err: 2243 fb_helper->dev = NULL; 2244 fb_helper->fbdev = NULL; 2245 2246 drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret); 2247 2248 return ret; 2249 } 2250 2251 static const struct drm_client_funcs drm_fbdev_client_funcs = { 2252 .owner = THIS_MODULE, 2253 .unregister = drm_fbdev_client_unregister, 2254 .restore = drm_fbdev_client_restore, 2255 .hotplug = drm_fbdev_client_hotplug, 2256 }; 2257 2258 /** 2259 * drm_fbdev_generic_setup() - Setup generic fbdev emulation 2260 * @dev: DRM device 2261 * @preferred_bpp: Preferred bits per pixel for the device. 2262 * @dev->mode_config.preferred_depth is used if this is zero. 2263 * 2264 * This function sets up generic fbdev emulation for drivers that supports 2265 * dumb buffers with a virtual address and that can be mmap'ed. 2266 * 2267 * Restore, hotplug events and teardown are all taken care of. Drivers that do 2268 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. 2269 * Simple drivers might use drm_mode_config_helper_suspend(). 2270 * 2271 * Drivers that set the dirty callback on their framebuffer will get a shadow 2272 * fbdev buffer that is blitted onto the real buffer. This is done in order to 2273 * make deferred I/O work with all kinds of buffers. A shadow buffer can be 2274 * requested explicitly by setting struct drm_mode_config.prefer_shadow or 2275 * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is 2276 * required to use generic fbdev emulation with SHMEM helpers. 2277 * 2278 * This function is safe to call even when there are no connectors present. 2279 * Setup will be retried on the next hotplug event. 2280 * 2281 * The fbdev is destroyed by drm_dev_unregister(). 2282 * 2283 * Returns: 2284 * Zero on success or negative error code on failure. 2285 */ 2286 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) 2287 { 2288 struct drm_fb_helper *fb_helper; 2289 int ret; 2290 2291 WARN(dev->fb_helper, "fb_helper is already set!\n"); 2292 2293 if (!drm_fbdev_emulation) 2294 return 0; 2295 2296 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 2297 if (!fb_helper) 2298 return -ENOMEM; 2299 2300 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); 2301 if (ret) { 2302 kfree(fb_helper); 2303 drm_err(dev, "Failed to register client: %d\n", ret); 2304 return ret; 2305 } 2306 2307 if (!preferred_bpp) 2308 preferred_bpp = dev->mode_config.preferred_depth; 2309 if (!preferred_bpp) 2310 preferred_bpp = 32; 2311 fb_helper->preferred_bpp = preferred_bpp; 2312 2313 ret = drm_fbdev_client_hotplug(&fb_helper->client); 2314 if (ret) 2315 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret); 2316 2317 drm_client_register(&fb_helper->client); 2318 2319 return 0; 2320 } 2321 EXPORT_SYMBOL(drm_fbdev_generic_setup); 2322 2323 #endif /* __linux__ */ 2324 2325 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 2326 * but the module doesn't depend on any fb console symbols. At least 2327 * attempt to load fbcon to avoid leaving the system without a usable console. 2328 */ 2329 int __init drm_fb_helper_modinit(void) 2330 { 2331 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 2332 const char name[] = "fbcon"; 2333 struct module *fbcon; 2334 2335 mutex_lock(&module_mutex); 2336 fbcon = find_module(name); 2337 mutex_unlock(&module_mutex); 2338 2339 if (!fbcon) 2340 request_module_nowait(name); 2341 #endif 2342 return 0; 2343 } 2344 EXPORT_SYMBOL(drm_fb_helper_modinit); 2345