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