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/export.h> 34 35 #include <drm/drm_atomic.h> 36 #include <drm/drm_drv.h> 37 #include <drm/drm_fb_helper.h> 38 #include <drm/drm_fourcc.h> 39 #include <drm/drm_framebuffer.h> 40 #include <drm/drm_modeset_helper_vtables.h> 41 #include <drm/drm_print.h> 42 #include <drm/drm_vblank.h> 43 44 #include "drm_internal.h" 45 #include "drm_crtc_internal.h" 46 47 static bool drm_fbdev_emulation = true; 48 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); 49 MODULE_PARM_DESC(fbdev_emulation, 50 "Enable legacy fbdev emulation [default=true]"); 51 52 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; 53 module_param(drm_fbdev_overalloc, int, 0444); 54 MODULE_PARM_DESC(drm_fbdev_overalloc, 55 "Overallocation of the fbdev buffer (%) [default=" 56 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); 57 58 /* 59 * In order to keep user-space compatibility, we want in certain use-cases 60 * to keep leaking the fbdev physical address to the user-space program 61 * handling the fbdev buffer. 62 * 63 * This is a bad habit, essentially kept to support closed-source OpenGL 64 * drivers that should really be moved into open-source upstream projects 65 * instead of using legacy physical addresses in user space to communicate 66 * with other out-of-tree kernel modules. 67 * 68 * This module_param *should* be removed as soon as possible and be 69 * considered as a broken and legacy behaviour from a modern fbdev device. 70 */ 71 static bool drm_leak_fbdev_smem; 72 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 73 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600); 74 MODULE_PARM_DESC(drm_leak_fbdev_smem, 75 "Allow unsafe leaking fbdev physical smem address [default=false]"); 76 #endif 77 78 static LIST_HEAD(kernel_fb_helper_list); 79 static DEFINE_MUTEX(kernel_fb_helper_lock); 80 81 /** 82 * DOC: fbdev helpers 83 * 84 * The fb helper functions are useful to provide an fbdev on top of a drm kernel 85 * mode setting driver. They can be used mostly independently from the crtc 86 * helper functions used by many drivers to implement the kernel mode setting 87 * interfaces. Drivers that use one of the shared memory managers, TTM, SHMEM, 88 * DMA, should instead use the corresponding fbdev emulation. 89 * 90 * For suspend/resume consider using drm_mode_config_helper_suspend() and 91 * drm_mode_config_helper_resume() which takes care of fbdev as well. 92 * 93 * All other functions exported by the fb helper library can be used to 94 * implement the fbdev driver interface by the driver. 95 * 96 * It is possible, though perhaps somewhat tricky, to implement race-free 97 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() 98 * helper must be called first to initialize the minimum required to make 99 * hotplug detection work. Drivers also need to make sure to properly set up 100 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() 101 * it is safe to enable interrupts and start processing hotplug events. At the 102 * same time, drivers should initialize all modeset objects such as CRTCs, 103 * encoders and connectors. To finish up the fbdev helper initialization, the 104 * drm_fb_helper_init() function is called. To probe for all attached displays 105 * and set up an initial configuration using the detected hardware, drivers 106 * should call drm_fb_helper_initial_config(). 107 * 108 * If &drm_framebuffer_funcs.dirty is set, the 109 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will 110 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right 111 * away. This worker then calls the dirty() function ensuring that it will 112 * always run in process context since the fb_*() function could be running in 113 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io 114 * callback it will also schedule dirty_work with the damage collected from the 115 * mmap page writes. 116 */ 117 118 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) 119 { 120 uint16_t *r_base, *g_base, *b_base; 121 122 if (crtc->funcs->gamma_set == NULL) 123 return; 124 125 r_base = crtc->gamma_store; 126 g_base = r_base + crtc->gamma_size; 127 b_base = g_base + crtc->gamma_size; 128 129 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 130 crtc->gamma_size, NULL); 131 } 132 133 /** 134 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter 135 * @info: fbdev registered by the helper 136 */ 137 int drm_fb_helper_debug_enter(struct fb_info *info) 138 { 139 struct drm_fb_helper *helper = info->par; 140 const struct drm_crtc_helper_funcs *funcs; 141 struct drm_mode_set *mode_set; 142 143 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 144 mutex_lock(&helper->client.modeset_mutex); 145 drm_client_for_each_modeset(mode_set, &helper->client) { 146 if (!mode_set->crtc->enabled) 147 continue; 148 149 funcs = mode_set->crtc->helper_private; 150 if (funcs->mode_set_base_atomic == NULL) 151 continue; 152 153 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) 154 continue; 155 156 funcs->mode_set_base_atomic(mode_set->crtc, 157 mode_set->fb, 158 mode_set->x, 159 mode_set->y, 160 ENTER_ATOMIC_MODE_SET); 161 } 162 mutex_unlock(&helper->client.modeset_mutex); 163 } 164 165 return 0; 166 } 167 EXPORT_SYMBOL(drm_fb_helper_debug_enter); 168 169 /** 170 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave 171 * @info: fbdev registered by the helper 172 */ 173 int drm_fb_helper_debug_leave(struct fb_info *info) 174 { 175 struct drm_fb_helper *helper = info->par; 176 struct drm_client_dev *client = &helper->client; 177 struct drm_device *dev = helper->dev; 178 struct drm_crtc *crtc; 179 const struct drm_crtc_helper_funcs *funcs; 180 struct drm_mode_set *mode_set; 181 struct drm_framebuffer *fb; 182 183 mutex_lock(&client->modeset_mutex); 184 drm_client_for_each_modeset(mode_set, client) { 185 crtc = mode_set->crtc; 186 if (drm_drv_uses_atomic_modeset(crtc->dev)) 187 continue; 188 189 funcs = crtc->helper_private; 190 fb = crtc->primary->fb; 191 192 if (!crtc->enabled) 193 continue; 194 195 if (!fb) { 196 drm_err(dev, "no fb to restore?\n"); 197 continue; 198 } 199 200 if (funcs->mode_set_base_atomic == NULL) 201 continue; 202 203 drm_fb_helper_restore_lut_atomic(mode_set->crtc); 204 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, 205 crtc->y, LEAVE_ATOMIC_MODE_SET); 206 } 207 mutex_unlock(&client->modeset_mutex); 208 209 return 0; 210 } 211 EXPORT_SYMBOL(drm_fb_helper_debug_leave); 212 213 static int 214 __drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper, 215 bool force) 216 { 217 bool do_delayed; 218 int ret; 219 220 if (!drm_fbdev_emulation || !fb_helper) 221 return -ENODEV; 222 223 if (READ_ONCE(fb_helper->deferred_setup)) 224 return 0; 225 226 mutex_lock(&fb_helper->lock); 227 if (force) { 228 /* 229 * Yes this is the _locked version which expects the master lock 230 * to be held. But for forced restores we're intentionally 231 * racing here, see drm_fb_helper_set_par(). 232 */ 233 ret = drm_client_modeset_commit_locked(&fb_helper->client); 234 } else { 235 ret = drm_client_modeset_commit(&fb_helper->client); 236 } 237 238 do_delayed = fb_helper->delayed_hotplug; 239 if (do_delayed) 240 fb_helper->delayed_hotplug = false; 241 mutex_unlock(&fb_helper->lock); 242 243 if (do_delayed) 244 drm_fb_helper_hotplug_event(fb_helper); 245 246 if (fb_helper->funcs->fb_restore) 247 fb_helper->funcs->fb_restore(fb_helper); 248 249 return ret; 250 } 251 252 /** 253 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration 254 * @fb_helper: driver-allocated fbdev helper, can be NULL 255 * @force: ignore present DRM master 256 * 257 * This helper should be called from fbdev emulation's &drm_client_funcs.restore 258 * callback. It ensures that the user isn't greeted with a black screen when the 259 * userspace compositor releases the display device. 260 * 261 * Returns: 262 * 0 on success, or a negative errno code otherwise. 263 */ 264 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper, bool force) 265 { 266 return __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force); 267 } 268 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); 269 270 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) 271 { 272 struct drm_fb_helper *fb_helper = info->par; 273 274 mutex_lock(&fb_helper->lock); 275 drm_client_modeset_dpms(&fb_helper->client, dpms_mode); 276 mutex_unlock(&fb_helper->lock); 277 } 278 279 /** 280 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank 281 * @blank: desired blanking state 282 * @info: fbdev registered by the helper 283 */ 284 int drm_fb_helper_blank(int blank, struct fb_info *info) 285 { 286 if (oops_in_progress) 287 return -EBUSY; 288 289 switch (blank) { 290 /* Display: On; HSync: On, VSync: On */ 291 case FB_BLANK_UNBLANK: 292 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); 293 break; 294 /* Display: Off; HSync: On, VSync: On */ 295 case FB_BLANK_NORMAL: 296 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 297 break; 298 /* Display: Off; HSync: Off, VSync: On */ 299 case FB_BLANK_HSYNC_SUSPEND: 300 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 301 break; 302 /* Display: Off; HSync: On, VSync: Off */ 303 case FB_BLANK_VSYNC_SUSPEND: 304 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); 305 break; 306 /* Display: Off; HSync: Off, VSync: Off */ 307 case FB_BLANK_POWERDOWN: 308 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); 309 break; 310 } 311 return 0; 312 } 313 EXPORT_SYMBOL(drm_fb_helper_blank); 314 315 static void drm_fb_helper_resume_worker(struct work_struct *work) 316 { 317 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 318 resume_work); 319 320 console_lock(); 321 fb_set_suspend(helper->info, 0); 322 console_unlock(); 323 } 324 325 static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper) 326 { 327 struct drm_device *dev = helper->dev; 328 struct drm_clip_rect *clip = &helper->damage_clip; 329 struct drm_clip_rect clip_copy; 330 unsigned long flags; 331 int ret; 332 333 mutex_lock(&helper->lock); 334 drm_client_modeset_wait_for_vblank(&helper->client, 0); 335 mutex_unlock(&helper->lock); 336 337 if (drm_WARN_ON_ONCE(dev, !helper->funcs->fb_dirty)) 338 return; 339 340 spin_lock_irqsave(&helper->damage_lock, flags); 341 clip_copy = *clip; 342 clip->x1 = clip->y1 = ~0; 343 clip->x2 = clip->y2 = 0; 344 spin_unlock_irqrestore(&helper->damage_lock, flags); 345 346 ret = helper->funcs->fb_dirty(helper, &clip_copy); 347 if (ret) 348 goto err; 349 350 return; 351 352 err: 353 /* 354 * Restore damage clip rectangle on errors. The next run 355 * of the damage worker will perform the update. 356 */ 357 spin_lock_irqsave(&helper->damage_lock, flags); 358 clip->x1 = min_t(u32, clip->x1, clip_copy.x1); 359 clip->y1 = min_t(u32, clip->y1, clip_copy.y1); 360 clip->x2 = max_t(u32, clip->x2, clip_copy.x2); 361 clip->y2 = max_t(u32, clip->y2, clip_copy.y2); 362 spin_unlock_irqrestore(&helper->damage_lock, flags); 363 } 364 365 static void drm_fb_helper_damage_work(struct work_struct *work) 366 { 367 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, damage_work); 368 369 if (helper->info->state != FBINFO_STATE_RUNNING) 370 return; 371 372 drm_fb_helper_fb_dirty(helper); 373 } 374 375 /** 376 * drm_fb_helper_prepare - setup a drm_fb_helper structure 377 * @dev: DRM device 378 * @helper: driver-allocated fbdev helper structure to set up 379 * @preferred_bpp: Preferred bits per pixel for the device. 380 * @funcs: pointer to structure of functions associate with this helper 381 * 382 * Sets up the bare minimum to make the framebuffer helper usable. This is 383 * useful to implement race-free initialization of the polling helpers. 384 */ 385 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 386 unsigned int preferred_bpp, 387 const struct drm_fb_helper_funcs *funcs) 388 { 389 /* 390 * Pick a preferred bpp of 32 if no value has been given. This 391 * will select XRGB8888 for the framebuffer formats. All drivers 392 * have to support XRGB8888 for backwards compatibility with legacy 393 * userspace, so it's the safe choice here. 394 * 395 * TODO: Replace struct drm_mode_config.preferred_depth and this 396 * bpp value with a preferred format that is given as struct 397 * drm_format_info. Then derive all other values from the 398 * format. 399 */ 400 if (!preferred_bpp) 401 preferred_bpp = 32; 402 403 INIT_LIST_HEAD(&helper->kernel_fb_list); 404 spin_lock_init(&helper->damage_lock); 405 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); 406 INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work); 407 helper->damage_clip.x1 = helper->damage_clip.y1 = ~0; 408 mutex_init(&helper->lock); 409 helper->funcs = funcs; 410 helper->dev = dev; 411 helper->preferred_bpp = preferred_bpp; 412 } 413 EXPORT_SYMBOL(drm_fb_helper_prepare); 414 415 /** 416 * drm_fb_helper_unprepare - clean up a drm_fb_helper structure 417 * @fb_helper: driver-allocated fbdev helper structure to set up 418 * 419 * Cleans up the framebuffer helper. Inverse of drm_fb_helper_prepare(). 420 */ 421 void drm_fb_helper_unprepare(struct drm_fb_helper *fb_helper) 422 { 423 mutex_destroy(&fb_helper->lock); 424 } 425 EXPORT_SYMBOL(drm_fb_helper_unprepare); 426 427 /** 428 * drm_fb_helper_init - initialize a &struct drm_fb_helper 429 * @dev: drm device 430 * @fb_helper: driver-allocated fbdev helper structure to initialize 431 * 432 * This allocates the structures for the fbdev helper with the given limits. 433 * Note that this won't yet touch the hardware (through the driver interfaces) 434 * nor register the fbdev. This is only done in drm_fb_helper_initial_config() 435 * to allow driver writes more control over the exact init sequence. 436 * 437 * Drivers must call drm_fb_helper_prepare() before calling this function. 438 * 439 * RETURNS: 440 * Zero if everything went ok, nonzero otherwise. 441 */ 442 int drm_fb_helper_init(struct drm_device *dev, 443 struct drm_fb_helper *fb_helper) 444 { 445 int ret; 446 447 /* 448 * If this is not the generic fbdev client, initialize a drm_client 449 * without callbacks so we can use the modesets. 450 */ 451 if (!fb_helper->client.funcs) { 452 ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL); 453 if (ret) 454 return ret; 455 } 456 457 dev->fb_helper = fb_helper; 458 459 return 0; 460 } 461 EXPORT_SYMBOL(drm_fb_helper_init); 462 463 static struct fb_info *drm_fb_helper_alloc_info(struct drm_fb_helper *fb_helper) 464 { 465 struct device *dev = fb_helper->dev->dev; 466 struct fb_info *info; 467 int ret; 468 469 info = framebuffer_alloc(0, dev); 470 if (!info) 471 return ERR_PTR(-ENOMEM); 472 473 if (!drm_leak_fbdev_smem) 474 info->flags |= FBINFO_HIDE_SMEM_START; 475 476 ret = fb_alloc_cmap(&info->cmap, 256, 0); 477 if (ret) 478 goto err_release; 479 480 fb_helper->info = info; 481 info->skip_vt_switch = true; 482 483 info->skip_panic = drm_panic_is_enabled(fb_helper->dev); 484 return info; 485 486 err_release: 487 framebuffer_release(info); 488 return ERR_PTR(ret); 489 } 490 491 static void drm_fb_helper_release_info(struct drm_fb_helper *fb_helper) 492 { 493 struct fb_info *info = fb_helper->info; 494 495 if (!info) 496 return; 497 498 fb_helper->info = NULL; 499 500 if (info->cmap.len) 501 fb_dealloc_cmap(&info->cmap); 502 framebuffer_release(info); 503 } 504 505 /** 506 * drm_fb_helper_unregister_info - unregister fb_info framebuffer device 507 * @fb_helper: driver-allocated fbdev helper, must not be NULL 508 * 509 * A wrapper around unregister_framebuffer, to release the fb_info 510 * framebuffer device. This must be called before releasing all resources for 511 * @fb_helper by calling drm_fb_helper_fini(). 512 */ 513 void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper) 514 { 515 unregister_framebuffer(fb_helper->info); 516 } 517 EXPORT_SYMBOL(drm_fb_helper_unregister_info); 518 519 /** 520 * drm_fb_helper_fini - finialize a &struct drm_fb_helper 521 * @fb_helper: driver-allocated fbdev helper, can be NULL 522 * 523 * This cleans up all remaining resources associated with @fb_helper. 524 */ 525 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 526 { 527 if (!fb_helper) 528 return; 529 530 fb_helper->dev->fb_helper = NULL; 531 532 if (!drm_fbdev_emulation) 533 return; 534 535 cancel_work_sync(&fb_helper->resume_work); 536 cancel_work_sync(&fb_helper->damage_work); 537 538 drm_fb_helper_release_info(fb_helper); 539 540 mutex_lock(&kernel_fb_helper_lock); 541 if (!list_empty(&fb_helper->kernel_fb_list)) 542 list_del(&fb_helper->kernel_fb_list); 543 mutex_unlock(&kernel_fb_helper_lock); 544 545 if (!fb_helper->client.funcs) 546 drm_client_release(&fb_helper->client); 547 } 548 EXPORT_SYMBOL(drm_fb_helper_fini); 549 550 static void drm_fb_helper_add_damage_clip(struct drm_fb_helper *helper, u32 x, u32 y, 551 u32 width, u32 height) 552 { 553 struct drm_clip_rect *clip = &helper->damage_clip; 554 unsigned long flags; 555 556 spin_lock_irqsave(&helper->damage_lock, flags); 557 clip->x1 = min_t(u32, clip->x1, x); 558 clip->y1 = min_t(u32, clip->y1, y); 559 clip->x2 = max_t(u32, clip->x2, x + width); 560 clip->y2 = max_t(u32, clip->y2, y + height); 561 spin_unlock_irqrestore(&helper->damage_lock, flags); 562 } 563 564 static void drm_fb_helper_damage(struct drm_fb_helper *helper, u32 x, u32 y, 565 u32 width, u32 height) 566 { 567 /* 568 * This function may be invoked by panic() to flush the frame 569 * buffer, where all CPUs except the panic CPU are stopped. 570 * During the following schedule_work(), the panic CPU needs 571 * the worker_pool lock, which might be held by a stopped CPU, 572 * causing schedule_work() and panic() to block. Return early on 573 * oops_in_progress to prevent this blocking. 574 */ 575 if (oops_in_progress) 576 return; 577 578 drm_fb_helper_add_damage_clip(helper, x, y, width, height); 579 580 schedule_work(&helper->damage_work); 581 } 582 583 /* 584 * Convert memory region into area of scanlines and pixels per 585 * scanline. The parameters off and len must not reach beyond 586 * the end of the framebuffer. 587 */ 588 static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len, 589 struct drm_rect *clip) 590 { 591 u32 line_length = info->fix.line_length; 592 u32 fb_height = info->var.yres; 593 off_t end = off + len; 594 u32 x1 = 0; 595 u32 y1 = off / line_length; 596 u32 x2 = info->var.xres; 597 u32 y2 = DIV_ROUND_UP(end, line_length); 598 599 /* Don't allow any of them beyond the bottom bound of display area */ 600 if (y1 > fb_height) 601 y1 = fb_height; 602 if (y2 > fb_height) 603 y2 = fb_height; 604 605 if ((y2 - y1) == 1) { 606 /* 607 * We've only written to a single scanline. Try to reduce 608 * the number of horizontal pixels that need an update. 609 */ 610 off_t bit_off = (off % line_length) * 8; 611 off_t bit_end = (end % line_length) * 8; 612 613 x1 = bit_off / info->var.bits_per_pixel; 614 x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel); 615 } 616 617 drm_rect_init(clip, x1, y1, x2 - x1, y2 - y1); 618 } 619 620 /* Don't use in new code. */ 621 void drm_fb_helper_damage_range(struct fb_info *info, off_t off, size_t len) 622 { 623 struct drm_fb_helper *fb_helper = info->par; 624 struct drm_rect damage_area; 625 626 drm_fb_helper_memory_range_to_clip(info, off, len, &damage_area); 627 drm_fb_helper_damage(fb_helper, damage_area.x1, damage_area.y1, 628 drm_rect_width(&damage_area), 629 drm_rect_height(&damage_area)); 630 } 631 EXPORT_SYMBOL(drm_fb_helper_damage_range); 632 633 /* Don't use in new code. */ 634 void drm_fb_helper_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height) 635 { 636 struct drm_fb_helper *fb_helper = info->par; 637 638 drm_fb_helper_damage(fb_helper, x, y, width, height); 639 } 640 EXPORT_SYMBOL(drm_fb_helper_damage_area); 641 642 #ifdef CONFIG_FB_DEFERRED_IO 643 /** 644 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function 645 * @info: fb_info struct pointer 646 * @pagereflist: list of mmap framebuffer pages that have to be flushed 647 * 648 * This function is used as the &fb_deferred_io.deferred_io 649 * callback function for flushing the fbdev mmap writes. 650 */ 651 void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagereflist) 652 { 653 struct drm_fb_helper *helper = info->par; 654 unsigned long start, end, min_off, max_off, total_size; 655 struct fb_deferred_io_pageref *pageref; 656 struct drm_rect damage_area; 657 658 min_off = ULONG_MAX; 659 max_off = 0; 660 list_for_each_entry(pageref, pagereflist, list) { 661 start = pageref->offset; 662 end = start + PAGE_SIZE; 663 min_off = min(min_off, start); 664 max_off = max(max_off, end); 665 } 666 667 /* 668 * As we can only track pages, we might reach beyond the end 669 * of the screen and account for non-existing scanlines. Hence, 670 * keep the covered memory area within the screen buffer. 671 */ 672 if (info->screen_size) 673 total_size = info->screen_size; 674 else 675 total_size = info->fix.smem_len; 676 max_off = min(max_off, total_size); 677 678 if (min_off < max_off) { 679 drm_fb_helper_memory_range_to_clip(info, min_off, max_off - min_off, &damage_area); 680 drm_fb_helper_damage(helper, damage_area.x1, damage_area.y1, 681 drm_rect_width(&damage_area), 682 drm_rect_height(&damage_area)); 683 } 684 } 685 EXPORT_SYMBOL(drm_fb_helper_deferred_io); 686 #endif 687 688 /** 689 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 690 * @fb_helper: driver-allocated fbdev helper, can be NULL 691 * @suspend: whether to suspend or resume 692 * 693 * A wrapper around fb_set_suspend implemented by fbdev core. 694 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take 695 * the lock yourself 696 */ 697 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) 698 { 699 if (!fb_helper || !fb_helper->info) 700 return; 701 702 if (fb_helper->funcs->fb_set_suspend) 703 fb_helper->funcs->fb_set_suspend(fb_helper, suspend); 704 else 705 fb_set_suspend(fb_helper->info, suspend); 706 } 707 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 708 709 /** 710 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also 711 * takes the console lock 712 * @fb_helper: driver-allocated fbdev helper, can be NULL 713 * @suspend: whether to suspend or resume 714 * 715 * A wrapper around fb_set_suspend() that takes the console lock. If the lock 716 * isn't available on resume, a worker is tasked with waiting for the lock 717 * to become available. The console lock can be pretty contented on resume 718 * due to all the printk activity. 719 * 720 * This function can be called multiple times with the same state since 721 * &fb_info.state is checked to see if fbdev is running or not before locking. 722 * 723 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. 724 */ 725 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 726 bool suspend) 727 { 728 if (!fb_helper || !fb_helper->info) 729 return; 730 731 /* make sure there's no pending/ongoing resume */ 732 flush_work(&fb_helper->resume_work); 733 734 if (suspend) { 735 if (fb_helper->info->state != FBINFO_STATE_RUNNING) 736 return; 737 738 /* 739 * Cancel pending damage work. During GPU reset, VBlank 740 * interrupts are disabled and drm_fb_helper_fb_dirty() 741 * would wait for VBlank timeout otherwise. 742 */ 743 cancel_work_sync(&fb_helper->damage_work); 744 745 console_lock(); 746 747 } else { 748 if (fb_helper->info->state == FBINFO_STATE_RUNNING) 749 return; 750 751 if (!console_trylock()) { 752 schedule_work(&fb_helper->resume_work); 753 return; 754 } 755 } 756 757 drm_fb_helper_set_suspend(fb_helper, suspend); 758 console_unlock(); 759 } 760 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); 761 762 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) 763 { 764 u32 *palette = (u32 *)info->pseudo_palette; 765 int i; 766 767 if (cmap->start + cmap->len > 16) 768 return -EINVAL; 769 770 for (i = 0; i < cmap->len; ++i) { 771 u16 red = cmap->red[i]; 772 u16 green = cmap->green[i]; 773 u16 blue = cmap->blue[i]; 774 u32 value; 775 776 red >>= 16 - info->var.red.length; 777 green >>= 16 - info->var.green.length; 778 blue >>= 16 - info->var.blue.length; 779 value = (red << info->var.red.offset) | 780 (green << info->var.green.offset) | 781 (blue << info->var.blue.offset); 782 if (info->var.transp.length > 0) { 783 u32 mask = (1 << info->var.transp.length) - 1; 784 785 mask <<= info->var.transp.offset; 786 value |= mask; 787 } 788 palette[cmap->start + i] = value; 789 } 790 791 return 0; 792 } 793 794 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) 795 { 796 struct drm_fb_helper *fb_helper = info->par; 797 struct drm_mode_set *modeset; 798 struct drm_crtc *crtc; 799 u16 *r, *g, *b; 800 int ret = 0; 801 802 drm_modeset_lock_all(fb_helper->dev); 803 drm_client_for_each_modeset(modeset, &fb_helper->client) { 804 crtc = modeset->crtc; 805 if (!crtc->funcs->gamma_set || !crtc->gamma_size) { 806 ret = -EINVAL; 807 goto out; 808 } 809 810 if (cmap->start + cmap->len > crtc->gamma_size) { 811 ret = -EINVAL; 812 goto out; 813 } 814 815 r = crtc->gamma_store; 816 g = r + crtc->gamma_size; 817 b = g + crtc->gamma_size; 818 819 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 820 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 821 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 822 823 ret = crtc->funcs->gamma_set(crtc, r, g, b, 824 crtc->gamma_size, NULL); 825 if (ret) 826 goto out; 827 } 828 out: 829 drm_modeset_unlock_all(fb_helper->dev); 830 831 return ret; 832 } 833 834 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, 835 struct fb_cmap *cmap) 836 { 837 struct drm_device *dev = crtc->dev; 838 struct drm_property_blob *gamma_lut; 839 struct drm_color_lut *lut; 840 int size = crtc->gamma_size; 841 int i; 842 843 if (!size || cmap->start + cmap->len > size) 844 return ERR_PTR(-EINVAL); 845 846 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); 847 if (IS_ERR(gamma_lut)) 848 return gamma_lut; 849 850 lut = gamma_lut->data; 851 if (cmap->start || cmap->len != size) { 852 u16 *r = crtc->gamma_store; 853 u16 *g = r + crtc->gamma_size; 854 u16 *b = g + crtc->gamma_size; 855 856 for (i = 0; i < cmap->start; i++) { 857 lut[i].red = r[i]; 858 lut[i].green = g[i]; 859 lut[i].blue = b[i]; 860 } 861 for (i = cmap->start + cmap->len; i < size; i++) { 862 lut[i].red = r[i]; 863 lut[i].green = g[i]; 864 lut[i].blue = b[i]; 865 } 866 } 867 868 for (i = 0; i < cmap->len; i++) { 869 lut[cmap->start + i].red = cmap->red[i]; 870 lut[cmap->start + i].green = cmap->green[i]; 871 lut[cmap->start + i].blue = cmap->blue[i]; 872 } 873 874 return gamma_lut; 875 } 876 877 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) 878 { 879 struct drm_fb_helper *fb_helper = info->par; 880 struct drm_device *dev = fb_helper->dev; 881 struct drm_property_blob *gamma_lut = NULL; 882 struct drm_modeset_acquire_ctx ctx; 883 struct drm_crtc_state *crtc_state; 884 struct drm_atomic_state *state; 885 struct drm_mode_set *modeset; 886 struct drm_crtc *crtc; 887 u16 *r, *g, *b; 888 bool replaced; 889 int ret = 0; 890 891 drm_modeset_acquire_init(&ctx, 0); 892 893 state = drm_atomic_state_alloc(dev); 894 if (!state) { 895 ret = -ENOMEM; 896 goto out_ctx; 897 } 898 899 state->acquire_ctx = &ctx; 900 retry: 901 drm_client_for_each_modeset(modeset, &fb_helper->client) { 902 crtc = modeset->crtc; 903 904 if (!gamma_lut) 905 gamma_lut = setcmap_new_gamma_lut(crtc, cmap); 906 if (IS_ERR(gamma_lut)) { 907 ret = PTR_ERR(gamma_lut); 908 gamma_lut = NULL; 909 goto out_state; 910 } 911 912 crtc_state = drm_atomic_get_crtc_state(state, crtc); 913 if (IS_ERR(crtc_state)) { 914 ret = PTR_ERR(crtc_state); 915 goto out_state; 916 } 917 918 /* 919 * FIXME: This always uses gamma_lut. Some HW have only 920 * degamma_lut, in which case we should reset gamma_lut and set 921 * degamma_lut. See drm_crtc_legacy_gamma_set(). 922 */ 923 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, 924 NULL); 925 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); 926 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, 927 gamma_lut); 928 crtc_state->color_mgmt_changed |= replaced; 929 } 930 931 ret = drm_atomic_commit(state); 932 if (ret) 933 goto out_state; 934 935 drm_client_for_each_modeset(modeset, &fb_helper->client) { 936 crtc = modeset->crtc; 937 938 r = crtc->gamma_store; 939 g = r + crtc->gamma_size; 940 b = g + crtc->gamma_size; 941 942 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 943 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 944 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 945 } 946 947 out_state: 948 if (ret == -EDEADLK) 949 goto backoff; 950 951 drm_property_blob_put(gamma_lut); 952 drm_atomic_state_put(state); 953 out_ctx: 954 drm_modeset_drop_locks(&ctx); 955 drm_modeset_acquire_fini(&ctx); 956 957 return ret; 958 959 backoff: 960 drm_atomic_state_clear(state); 961 drm_modeset_backoff(&ctx); 962 goto retry; 963 } 964 965 /** 966 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap 967 * @cmap: cmap to set 968 * @info: fbdev registered by the helper 969 */ 970 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 971 { 972 struct drm_fb_helper *fb_helper = info->par; 973 struct drm_device *dev = fb_helper->dev; 974 int ret; 975 976 if (oops_in_progress) 977 return -EBUSY; 978 979 mutex_lock(&fb_helper->lock); 980 981 if (!drm_master_internal_acquire(dev)) { 982 ret = -EBUSY; 983 goto unlock; 984 } 985 986 mutex_lock(&fb_helper->client.modeset_mutex); 987 if (info->fix.visual == FB_VISUAL_TRUECOLOR) 988 ret = setcmap_pseudo_palette(cmap, info); 989 else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) 990 ret = setcmap_atomic(cmap, info); 991 else 992 ret = setcmap_legacy(cmap, info); 993 mutex_unlock(&fb_helper->client.modeset_mutex); 994 995 drm_master_internal_release(dev); 996 unlock: 997 mutex_unlock(&fb_helper->lock); 998 999 return ret; 1000 } 1001 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1002 1003 /** 1004 * drm_fb_helper_ioctl - legacy ioctl implementation 1005 * @info: fbdev registered by the helper 1006 * @cmd: ioctl command 1007 * @arg: ioctl argument 1008 * 1009 * A helper to implement the standard fbdev ioctl. Only 1010 * FBIO_WAITFORVSYNC is implemented for now. 1011 */ 1012 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 1013 unsigned long arg) 1014 { 1015 struct drm_fb_helper *fb_helper = info->par; 1016 int ret = 0; 1017 1018 guard(mutex)(&fb_helper->lock); 1019 1020 switch (cmd) { 1021 case FBIO_WAITFORVSYNC: 1022 /* 1023 * Only consider the first CRTC. 1024 * 1025 * This ioctl is supposed to take the CRTC number as 1026 * an argument, but in fbdev times, what that number 1027 * was supposed to be was quite unclear, different 1028 * drivers were passing that argument differently 1029 * (some by reference, some by value), and most of the 1030 * userspace applications were just hardcoding 0 as an 1031 * argument. 1032 * 1033 * The first CRTC should be the integrated panel on 1034 * most drivers, so this is the best choice we can 1035 * make. If we're not smart enough here, one should 1036 * just consider switch the userspace to KMS. 1037 */ 1038 ret = drm_client_modeset_wait_for_vblank(&fb_helper->client, 0); 1039 break; 1040 default: 1041 ret = -ENOTTY; 1042 } 1043 1044 return ret; 1045 } 1046 EXPORT_SYMBOL(drm_fb_helper_ioctl); 1047 1048 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1, 1049 const struct fb_var_screeninfo *var_2) 1050 { 1051 return var_1->bits_per_pixel == var_2->bits_per_pixel && 1052 var_1->grayscale == var_2->grayscale && 1053 var_1->red.offset == var_2->red.offset && 1054 var_1->red.length == var_2->red.length && 1055 var_1->red.msb_right == var_2->red.msb_right && 1056 var_1->green.offset == var_2->green.offset && 1057 var_1->green.length == var_2->green.length && 1058 var_1->green.msb_right == var_2->green.msb_right && 1059 var_1->blue.offset == var_2->blue.offset && 1060 var_1->blue.length == var_2->blue.length && 1061 var_1->blue.msb_right == var_2->blue.msb_right && 1062 var_1->transp.offset == var_2->transp.offset && 1063 var_1->transp.length == var_2->transp.length && 1064 var_1->transp.msb_right == var_2->transp.msb_right; 1065 } 1066 1067 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var, 1068 const struct drm_format_info *format) 1069 { 1070 u8 depth = format->depth; 1071 1072 if (format->is_color_indexed) { 1073 var->red.offset = 0; 1074 var->green.offset = 0; 1075 var->blue.offset = 0; 1076 var->red.length = depth; 1077 var->green.length = depth; 1078 var->blue.length = depth; 1079 var->transp.offset = 0; 1080 var->transp.length = 0; 1081 return; 1082 } 1083 1084 switch (depth) { 1085 case 15: 1086 var->red.offset = 10; 1087 var->green.offset = 5; 1088 var->blue.offset = 0; 1089 var->red.length = 5; 1090 var->green.length = 5; 1091 var->blue.length = 5; 1092 var->transp.offset = 15; 1093 var->transp.length = 1; 1094 break; 1095 case 16: 1096 var->red.offset = 11; 1097 var->green.offset = 5; 1098 var->blue.offset = 0; 1099 var->red.length = 5; 1100 var->green.length = 6; 1101 var->blue.length = 5; 1102 var->transp.offset = 0; 1103 break; 1104 case 24: 1105 var->red.offset = 16; 1106 var->green.offset = 8; 1107 var->blue.offset = 0; 1108 var->red.length = 8; 1109 var->green.length = 8; 1110 var->blue.length = 8; 1111 var->transp.offset = 0; 1112 var->transp.length = 0; 1113 break; 1114 case 32: 1115 var->red.offset = 16; 1116 var->green.offset = 8; 1117 var->blue.offset = 0; 1118 var->red.length = 8; 1119 var->green.length = 8; 1120 var->blue.length = 8; 1121 var->transp.offset = 24; 1122 var->transp.length = 8; 1123 break; 1124 default: 1125 break; 1126 } 1127 } 1128 1129 static void __fill_var(struct fb_var_screeninfo *var, struct fb_info *info, 1130 struct drm_framebuffer *fb) 1131 { 1132 int i; 1133 1134 var->xres_virtual = fb->width; 1135 var->yres_virtual = fb->height; 1136 var->accel_flags = 0; 1137 var->bits_per_pixel = drm_format_info_bpp(fb->format, 0); 1138 1139 var->height = info->var.height; 1140 var->width = info->var.width; 1141 1142 var->left_margin = var->right_margin = 0; 1143 var->upper_margin = var->lower_margin = 0; 1144 var->hsync_len = var->vsync_len = 0; 1145 var->sync = var->vmode = 0; 1146 var->rotate = 0; 1147 var->colorspace = 0; 1148 for (i = 0; i < 4; i++) 1149 var->reserved[i] = 0; 1150 } 1151 1152 /** 1153 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var 1154 * @var: screeninfo to check 1155 * @info: fbdev registered by the helper 1156 */ 1157 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1158 struct fb_info *info) 1159 { 1160 struct drm_fb_helper *fb_helper = info->par; 1161 struct drm_framebuffer *fb = fb_helper->fb; 1162 const struct drm_format_info *format = fb->format; 1163 struct drm_device *dev = fb_helper->dev; 1164 unsigned int bpp; 1165 1166 if (in_dbg_master()) 1167 return -EINVAL; 1168 1169 if (var->pixclock != 0) { 1170 drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n"); 1171 var->pixclock = 0; 1172 } 1173 1174 switch (format->format) { 1175 case DRM_FORMAT_C1: 1176 case DRM_FORMAT_C2: 1177 case DRM_FORMAT_C4: 1178 /* supported format with sub-byte pixels */ 1179 break; 1180 1181 default: 1182 if ((drm_format_info_block_width(format, 0) > 1) || 1183 (drm_format_info_block_height(format, 0) > 1)) 1184 return -EINVAL; 1185 break; 1186 } 1187 1188 /* 1189 * Changes struct fb_var_screeninfo are currently not pushed back 1190 * to KMS, hence fail if different settings are requested. 1191 */ 1192 bpp = drm_format_info_bpp(format, 0); 1193 if (var->bits_per_pixel > bpp || 1194 var->xres > fb->width || var->yres > fb->height || 1195 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1196 drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb " 1197 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1198 var->xres, var->yres, var->bits_per_pixel, 1199 var->xres_virtual, var->yres_virtual, 1200 fb->width, fb->height, bpp); 1201 return -EINVAL; 1202 } 1203 1204 __fill_var(var, info, fb); 1205 1206 /* 1207 * fb_pan_display() validates this, but fb_set_par() doesn't and just 1208 * falls over. Note that __fill_var above adjusts y/res_virtual. 1209 */ 1210 if (var->yoffset > var->yres_virtual - var->yres || 1211 var->xoffset > var->xres_virtual - var->xres) 1212 return -EINVAL; 1213 1214 /* We neither support grayscale nor FOURCC (also stored in here). */ 1215 if (var->grayscale > 0) 1216 return -EINVAL; 1217 1218 if (var->nonstd) 1219 return -EINVAL; 1220 1221 /* 1222 * Workaround for SDL 1.2, which is known to be setting all pixel format 1223 * fields values to zero in some cases. We treat this situation as a 1224 * kind of "use some reasonable autodetected values". 1225 */ 1226 if (!var->red.offset && !var->green.offset && 1227 !var->blue.offset && !var->transp.offset && 1228 !var->red.length && !var->green.length && 1229 !var->blue.length && !var->transp.length && 1230 !var->red.msb_right && !var->green.msb_right && 1231 !var->blue.msb_right && !var->transp.msb_right) { 1232 drm_fb_helper_fill_pixel_fmt(var, format); 1233 } 1234 1235 /* 1236 * drm fbdev emulation doesn't support changing the pixel format at all, 1237 * so reject all pixel format changing requests. 1238 */ 1239 if (!drm_fb_pixel_format_equal(var, &info->var)) { 1240 drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n"); 1241 return -EINVAL; 1242 } 1243 1244 return 0; 1245 } 1246 EXPORT_SYMBOL(drm_fb_helper_check_var); 1247 1248 /** 1249 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par 1250 * @info: fbdev registered by the helper 1251 * 1252 * This will let fbcon do the mode init and is called at initialization time by 1253 * the fbdev core when registering the driver, and later on through the hotplug 1254 * callback. 1255 */ 1256 int drm_fb_helper_set_par(struct fb_info *info) 1257 { 1258 struct drm_fb_helper *fb_helper = info->par; 1259 struct fb_var_screeninfo *var = &info->var; 1260 bool force; 1261 1262 if (oops_in_progress) 1263 return -EBUSY; 1264 1265 /* 1266 * Normally we want to make sure that a kms master takes precedence over 1267 * fbdev, to avoid fbdev flickering and occasionally stealing the 1268 * display status. But Xorg first sets the vt back to text mode using 1269 * the KDSET IOCTL with KD_TEXT, and only after that drops the master 1270 * status when exiting. 1271 * 1272 * In the past this was caught by drm_fb_helper_restore_fbdev_mode_unlocked(), 1273 * but on modern systems where logind always keeps a drm fd open to 1274 * orchestrate the vt switching, this doesn't work. 1275 * 1276 * To not break the userspace ABI we have this special case here, which 1277 * is only used for the above case. Everything else uses the normal 1278 * commit function, which ensures that we never steal the display from 1279 * an active drm master. 1280 */ 1281 force = var->activate & FB_ACTIVATE_KD_TEXT; 1282 1283 __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force); 1284 1285 return 0; 1286 } 1287 EXPORT_SYMBOL(drm_fb_helper_set_par); 1288 1289 static void pan_set(struct drm_fb_helper *fb_helper, int dx, int dy) 1290 { 1291 struct drm_mode_set *mode_set; 1292 1293 mutex_lock(&fb_helper->client.modeset_mutex); 1294 drm_client_for_each_modeset(mode_set, &fb_helper->client) { 1295 mode_set->x += dx; 1296 mode_set->y += dy; 1297 } 1298 mutex_unlock(&fb_helper->client.modeset_mutex); 1299 } 1300 1301 static int pan_display_atomic(struct fb_var_screeninfo *var, 1302 struct fb_info *info) 1303 { 1304 struct drm_fb_helper *fb_helper = info->par; 1305 int ret, dx, dy; 1306 1307 dx = var->xoffset - info->var.xoffset; 1308 dy = var->yoffset - info->var.yoffset; 1309 pan_set(fb_helper, dx, dy); 1310 1311 ret = drm_client_modeset_commit_locked(&fb_helper->client); 1312 if (!ret) { 1313 info->var.xoffset = var->xoffset; 1314 info->var.yoffset = var->yoffset; 1315 } else 1316 pan_set(fb_helper, -dx, -dy); 1317 1318 return ret; 1319 } 1320 1321 static int pan_display_legacy(struct fb_var_screeninfo *var, 1322 struct fb_info *info) 1323 { 1324 struct drm_fb_helper *fb_helper = info->par; 1325 struct drm_client_dev *client = &fb_helper->client; 1326 struct drm_mode_set *modeset; 1327 int ret = 0; 1328 1329 mutex_lock(&client->modeset_mutex); 1330 drm_modeset_lock_all(fb_helper->dev); 1331 drm_client_for_each_modeset(modeset, client) { 1332 modeset->x = var->xoffset; 1333 modeset->y = var->yoffset; 1334 1335 if (modeset->num_connectors) { 1336 ret = drm_mode_set_config_internal(modeset); 1337 if (!ret) { 1338 info->var.xoffset = var->xoffset; 1339 info->var.yoffset = var->yoffset; 1340 } 1341 } 1342 } 1343 drm_modeset_unlock_all(fb_helper->dev); 1344 mutex_unlock(&client->modeset_mutex); 1345 1346 return ret; 1347 } 1348 1349 /** 1350 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display 1351 * @var: updated screen information 1352 * @info: fbdev registered by the helper 1353 */ 1354 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1355 struct fb_info *info) 1356 { 1357 struct drm_fb_helper *fb_helper = info->par; 1358 struct drm_device *dev = fb_helper->dev; 1359 int ret; 1360 1361 if (oops_in_progress) 1362 return -EBUSY; 1363 1364 mutex_lock(&fb_helper->lock); 1365 if (!drm_master_internal_acquire(dev)) { 1366 ret = -EBUSY; 1367 goto unlock; 1368 } 1369 1370 if (drm_drv_uses_atomic_modeset(dev)) 1371 ret = pan_display_atomic(var, info); 1372 else 1373 ret = pan_display_legacy(var, info); 1374 1375 drm_master_internal_release(dev); 1376 unlock: 1377 mutex_unlock(&fb_helper->lock); 1378 1379 return ret; 1380 } 1381 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1382 1383 static uint32_t drm_fb_helper_find_format(struct drm_fb_helper *fb_helper, const uint32_t *formats, 1384 size_t format_count, unsigned int color_mode) 1385 { 1386 struct drm_device *dev = fb_helper->dev; 1387 uint32_t format; 1388 size_t i; 1389 1390 format = drm_driver_color_mode_format(dev, color_mode); 1391 if (!format) { 1392 drm_info(dev, "unsupported color mode of %d\n", color_mode); 1393 return DRM_FORMAT_INVALID; 1394 } 1395 1396 for (i = 0; i < format_count; ++i) { 1397 if (formats[i] == format) 1398 return format; 1399 } 1400 drm_warn(dev, "format %p4cc not supported\n", &format); 1401 1402 return DRM_FORMAT_INVALID; 1403 } 1404 1405 static int __drm_fb_helper_find_sizes(struct drm_fb_helper *fb_helper, 1406 struct drm_fb_helper_surface_size *sizes) 1407 { 1408 struct drm_client_dev *client = &fb_helper->client; 1409 struct drm_device *dev = fb_helper->dev; 1410 int crtc_count = 0; 1411 struct drm_connector_list_iter conn_iter; 1412 struct drm_connector *connector; 1413 struct drm_mode_set *mode_set; 1414 uint32_t surface_format = DRM_FORMAT_INVALID; 1415 const struct drm_format_info *info; 1416 1417 memset(sizes, 0, sizeof(*sizes)); 1418 sizes->fb_width = (u32)-1; 1419 sizes->fb_height = (u32)-1; 1420 1421 drm_client_for_each_modeset(mode_set, client) { 1422 struct drm_crtc *crtc = mode_set->crtc; 1423 struct drm_plane *plane = crtc->primary; 1424 1425 drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc)); 1426 1427 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); 1428 drm_client_for_each_connector_iter(connector, &conn_iter) { 1429 struct drm_cmdline_mode *cmdline_mode = &connector->cmdline_mode; 1430 1431 if (!cmdline_mode->bpp_specified) 1432 continue; 1433 1434 surface_format = drm_fb_helper_find_format(fb_helper, 1435 plane->format_types, 1436 plane->format_count, 1437 cmdline_mode->bpp); 1438 if (surface_format != DRM_FORMAT_INVALID) 1439 break; /* found supported format */ 1440 } 1441 drm_connector_list_iter_end(&conn_iter); 1442 1443 if (surface_format != DRM_FORMAT_INVALID) 1444 break; /* found supported format */ 1445 1446 /* try preferred color mode */ 1447 surface_format = drm_fb_helper_find_format(fb_helper, 1448 plane->format_types, 1449 plane->format_count, 1450 fb_helper->preferred_bpp); 1451 if (surface_format != DRM_FORMAT_INVALID) 1452 break; /* found supported format */ 1453 } 1454 1455 if (surface_format == DRM_FORMAT_INVALID) { 1456 /* 1457 * If none of the given color modes works, fall back 1458 * to XRGB8888. Drivers are expected to provide this 1459 * format for compatibility with legacy applications. 1460 */ 1461 drm_warn(dev, "No compatible format found\n"); 1462 surface_format = drm_driver_legacy_fb_format(dev, 32, 24); 1463 } 1464 1465 info = drm_format_info(surface_format); 1466 sizes->surface_bpp = drm_format_info_bpp(info, 0); 1467 sizes->surface_depth = info->depth; 1468 1469 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1470 crtc_count = 0; 1471 drm_client_for_each_modeset(mode_set, client) { 1472 struct drm_display_mode *desired_mode; 1473 int x, y, j; 1474 /* in case of tile group, are we the last tile vert or horiz? 1475 * If no tile group you are always the last one both vertically 1476 * and horizontally 1477 */ 1478 bool lastv = true, lasth = true; 1479 1480 desired_mode = mode_set->mode; 1481 1482 if (!desired_mode) 1483 continue; 1484 1485 crtc_count++; 1486 1487 x = mode_set->x; 1488 y = mode_set->y; 1489 1490 sizes->surface_width = 1491 max_t(u32, desired_mode->hdisplay + x, sizes->surface_width); 1492 sizes->surface_height = 1493 max_t(u32, desired_mode->vdisplay + y, sizes->surface_height); 1494 1495 for (j = 0; j < mode_set->num_connectors; j++) { 1496 struct drm_connector *connector = mode_set->connectors[j]; 1497 1498 if (connector->has_tile && 1499 desired_mode->hdisplay == connector->tile_h_size && 1500 desired_mode->vdisplay == connector->tile_v_size) { 1501 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1502 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1503 /* cloning to multiple tiles is just crazy-talk, so: */ 1504 break; 1505 } 1506 } 1507 1508 if (lasth) 1509 sizes->fb_width = min_t(u32, desired_mode->hdisplay + x, sizes->fb_width); 1510 if (lastv) 1511 sizes->fb_height = min_t(u32, desired_mode->vdisplay + y, sizes->fb_height); 1512 } 1513 1514 if (crtc_count == 0 || sizes->fb_width == -1 || sizes->fb_height == -1) { 1515 drm_info(dev, "Cannot find any crtc or sizes\n"); 1516 return -EAGAIN; 1517 } 1518 1519 return 0; 1520 } 1521 1522 static int drm_fb_helper_find_sizes(struct drm_fb_helper *fb_helper, 1523 struct drm_fb_helper_surface_size *sizes) 1524 { 1525 struct drm_client_dev *client = &fb_helper->client; 1526 struct drm_device *dev = fb_helper->dev; 1527 struct drm_mode_config *config = &dev->mode_config; 1528 int ret; 1529 1530 mutex_lock(&client->modeset_mutex); 1531 ret = __drm_fb_helper_find_sizes(fb_helper, sizes); 1532 mutex_unlock(&client->modeset_mutex); 1533 1534 if (ret) 1535 return ret; 1536 1537 /* Handle our overallocation */ 1538 sizes->surface_height *= drm_fbdev_overalloc; 1539 sizes->surface_height /= 100; 1540 if (sizes->surface_height > config->max_height) { 1541 drm_dbg_kms(dev, "Fbdev over-allocation too large; clamping height to %d\n", 1542 config->max_height); 1543 sizes->surface_height = config->max_height; 1544 } 1545 1546 return 0; 1547 } 1548 1549 /* 1550 * Allocates the backing storage and sets up the fbdev info structure through 1551 * the ->fbdev_probe callback. 1552 */ 1553 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper) 1554 { 1555 struct drm_client_dev *client = &fb_helper->client; 1556 struct drm_device *dev = fb_helper->dev; 1557 struct drm_fb_helper_surface_size sizes; 1558 int ret; 1559 1560 if (drm_WARN_ON(dev, !dev->driver->fbdev_probe)) 1561 return -EINVAL; 1562 1563 ret = drm_fb_helper_find_sizes(fb_helper, &sizes); 1564 if (ret) { 1565 /* First time: disable all crtc's.. */ 1566 if (!fb_helper->deferred_setup) 1567 drm_client_modeset_commit(client); 1568 return ret; 1569 } 1570 1571 /* push down into drivers */ 1572 ret = dev->driver->fbdev_probe(fb_helper, &sizes); 1573 if (ret < 0) 1574 return ret; 1575 1576 strcpy(fb_helper->fb->comm, "[fbcon]"); 1577 1578 return 0; 1579 } 1580 1581 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1582 bool is_color_indexed) 1583 { 1584 info->fix.type = FB_TYPE_PACKED_PIXELS; 1585 info->fix.visual = is_color_indexed ? FB_VISUAL_PSEUDOCOLOR 1586 : FB_VISUAL_TRUECOLOR; 1587 info->fix.mmio_start = 0; 1588 info->fix.mmio_len = 0; 1589 info->fix.type_aux = 0; 1590 info->fix.xpanstep = 1; /* doing it in hw */ 1591 info->fix.ypanstep = 1; /* doing it in hw */ 1592 info->fix.ywrapstep = 0; 1593 info->fix.accel = FB_ACCEL_NONE; 1594 1595 info->fix.line_length = pitch; 1596 } 1597 1598 static void drm_fb_helper_fill_var(struct fb_info *info, 1599 struct drm_fb_helper *fb_helper, 1600 uint32_t fb_width, uint32_t fb_height) 1601 { 1602 struct drm_framebuffer *fb = fb_helper->fb; 1603 const struct drm_format_info *format = fb->format; 1604 1605 switch (format->format) { 1606 case DRM_FORMAT_C1: 1607 case DRM_FORMAT_C2: 1608 case DRM_FORMAT_C4: 1609 /* supported format with sub-byte pixels */ 1610 break; 1611 1612 default: 1613 WARN_ON((drm_format_info_block_width(format, 0) > 1) || 1614 (drm_format_info_block_height(format, 0) > 1)); 1615 break; 1616 } 1617 1618 info->pseudo_palette = fb_helper->pseudo_palette; 1619 info->var.xoffset = 0; 1620 info->var.yoffset = 0; 1621 __fill_var(&info->var, info, fb); 1622 info->var.activate = FB_ACTIVATE_NOW; 1623 1624 drm_fb_helper_fill_pixel_fmt(&info->var, format); 1625 1626 info->var.xres = fb_width; 1627 info->var.yres = fb_height; 1628 } 1629 1630 /** 1631 * drm_fb_helper_fill_info - initializes fbdev information 1632 * @info: fbdev instance to set up 1633 * @fb_helper: fb helper instance to use as template 1634 * @sizes: describes fbdev size and scanout surface size 1635 * 1636 * Sets up the variable and fixed fbdev metainformation from the given fb helper 1637 * instance and the drm framebuffer allocated in &drm_fb_helper.fb. 1638 * 1639 * Drivers should call this (or their equivalent setup code) from their 1640 * &drm_driver.fbdev_probe callback after having allocated the fbdev 1641 * backing storage framebuffer. 1642 */ 1643 void drm_fb_helper_fill_info(struct fb_info *info, 1644 struct drm_fb_helper *fb_helper, 1645 struct drm_fb_helper_surface_size *sizes) 1646 { 1647 struct drm_framebuffer *fb = fb_helper->fb; 1648 1649 drm_fb_helper_fill_fix(info, fb->pitches[0], 1650 fb->format->is_color_indexed); 1651 drm_fb_helper_fill_var(info, fb_helper, 1652 sizes->fb_width, sizes->fb_height); 1653 1654 info->par = fb_helper; 1655 /* 1656 * The DRM drivers fbdev emulation device name can be confusing if the 1657 * driver name also has a "drm" suffix on it. Leading to names such as 1658 * "simpledrmdrmfb" in /proc/fb. Unfortunately, it's an uAPI and can't 1659 * be changed due user-space tools (e.g: pm-utils) matching against it. 1660 */ 1661 snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb", 1662 fb_helper->dev->driver->name); 1663 1664 } 1665 EXPORT_SYMBOL(drm_fb_helper_fill_info); 1666 1667 /* 1668 * This is a continuation of drm_setup_crtcs() that sets up anything related 1669 * to the framebuffer. During initialization, drm_setup_crtcs() is called before 1670 * the framebuffer has been allocated (fb_helper->fb and fb_helper->info). 1671 * So, any setup that touches those fields needs to be done here instead of in 1672 * drm_setup_crtcs(). 1673 */ 1674 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) 1675 { 1676 struct drm_client_dev *client = &fb_helper->client; 1677 struct drm_connector_list_iter conn_iter; 1678 struct fb_info *info = fb_helper->info; 1679 unsigned int rotation, sw_rotations = 0; 1680 struct drm_connector *connector; 1681 struct drm_mode_set *modeset; 1682 1683 mutex_lock(&client->modeset_mutex); 1684 drm_client_for_each_modeset(modeset, client) { 1685 if (!modeset->num_connectors) 1686 continue; 1687 1688 modeset->fb = fb_helper->fb; 1689 1690 if (drm_client_rotation(modeset, &rotation)) 1691 /* Rotating in hardware, fbcon should not rotate */ 1692 sw_rotations |= DRM_MODE_ROTATE_0; 1693 else 1694 sw_rotations |= rotation; 1695 } 1696 mutex_unlock(&client->modeset_mutex); 1697 1698 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); 1699 drm_client_for_each_connector_iter(connector, &conn_iter) { 1700 1701 /* use first connected connector for the physical dimensions */ 1702 if (connector->status == connector_status_connected) { 1703 info->var.width = connector->display_info.width_mm; 1704 info->var.height = connector->display_info.height_mm; 1705 break; 1706 } 1707 } 1708 drm_connector_list_iter_end(&conn_iter); 1709 1710 switch (sw_rotations) { 1711 case DRM_MODE_ROTATE_0: 1712 info->fbcon_rotate_hint = FB_ROTATE_UR; 1713 break; 1714 case DRM_MODE_ROTATE_90: 1715 info->fbcon_rotate_hint = FB_ROTATE_CCW; 1716 break; 1717 case DRM_MODE_ROTATE_180: 1718 info->fbcon_rotate_hint = FB_ROTATE_UD; 1719 break; 1720 case DRM_MODE_ROTATE_270: 1721 info->fbcon_rotate_hint = FB_ROTATE_CW; 1722 break; 1723 default: 1724 /* 1725 * Multiple bits are set / multiple rotations requested 1726 * fbcon cannot handle separate rotation settings per 1727 * output, so fallback to unrotated. 1728 */ 1729 info->fbcon_rotate_hint = FB_ROTATE_UR; 1730 } 1731 } 1732 1733 /* Note: Drops fb_helper->lock before returning. */ 1734 static int 1735 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper) 1736 { 1737 struct drm_device *dev = fb_helper->dev; 1738 struct fb_info *info; 1739 unsigned int width, height; 1740 int ret; 1741 1742 width = dev->mode_config.max_width; 1743 height = dev->mode_config.max_height; 1744 1745 drm_client_modeset_probe(&fb_helper->client, width, height); 1746 1747 info = drm_fb_helper_alloc_info(fb_helper); 1748 if (IS_ERR(info)) 1749 return PTR_ERR(info); 1750 1751 ret = drm_fb_helper_single_fb_probe(fb_helper); 1752 if (ret < 0) { 1753 if (ret == -EAGAIN) { 1754 fb_helper->deferred_setup = true; 1755 ret = 0; 1756 } 1757 mutex_unlock(&fb_helper->lock); 1758 1759 goto err_drm_fb_helper_release_info; 1760 } 1761 drm_setup_crtcs_fb(fb_helper); 1762 1763 fb_helper->deferred_setup = false; 1764 1765 info->var.pixclock = 0; 1766 1767 /* Need to drop locks to avoid recursive deadlock in 1768 * register_framebuffer. This is ok because the only thing left to do is 1769 * register the fbdev emulation instance in kernel_fb_helper_list. */ 1770 mutex_unlock(&fb_helper->lock); 1771 1772 ret = register_framebuffer(info); 1773 if (ret < 0) 1774 return ret; 1775 1776 drm_info(dev, "fb%d: %s frame buffer device\n", 1777 info->node, info->fix.id); 1778 1779 mutex_lock(&kernel_fb_helper_lock); 1780 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1781 mutex_unlock(&kernel_fb_helper_lock); 1782 1783 return 0; 1784 1785 err_drm_fb_helper_release_info: 1786 drm_fb_helper_release_info(fb_helper); 1787 return ret; 1788 } 1789 1790 /** 1791 * drm_fb_helper_initial_config - setup a sane initial connector configuration 1792 * @fb_helper: fb_helper device struct 1793 * 1794 * Scans the CRTCs and connectors and tries to put together an initial setup. 1795 * At the moment, this is a cloned configuration across all heads with 1796 * a new framebuffer object as the backing store. 1797 * 1798 * Note that this also registers the fbdev and so allows userspace to call into 1799 * the driver through the fbdev interfaces. 1800 * 1801 * This function will call down into the &drm_driver.fbdev_probe callback 1802 * to let the driver allocate and initialize the fbdev info structure and the 1803 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided 1804 * as a helper to setup simple default values for the fbdev info structure. 1805 * 1806 * HANG DEBUGGING: 1807 * 1808 * When you have fbcon support built-in or already loaded, this function will do 1809 * a full modeset to setup the fbdev console. Due to locking misdesign in the 1810 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 1811 * console_lock. Until console_unlock is called no dmesg lines will be sent out 1812 * to consoles, not even serial console. This means when your driver crashes, 1813 * you will see absolutely nothing else but a system stuck in this function, 1814 * with no further output. Any kind of printk() you place within your own driver 1815 * or in the drm core modeset code will also never show up. 1816 * 1817 * Standard debug practice is to run the fbcon setup without taking the 1818 * console_lock as a hack, to be able to see backtraces and crashes on the 1819 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 1820 * cmdline option. 1821 * 1822 * The other option is to just disable fbdev emulation since very likely the 1823 * first modeset from userspace will crash in the same way, and is even easier 1824 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 1825 * kernel cmdline option. 1826 * 1827 * RETURNS: 1828 * Zero if everything went ok, nonzero otherwise. 1829 */ 1830 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper) 1831 { 1832 int ret; 1833 1834 if (!drm_fbdev_emulation) 1835 return 0; 1836 1837 mutex_lock(&fb_helper->lock); 1838 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper); 1839 1840 return ret; 1841 } 1842 EXPORT_SYMBOL(drm_fb_helper_initial_config); 1843 1844 /** 1845 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 1846 * probing all the outputs attached to the fb 1847 * @fb_helper: driver-allocated fbdev helper, can be NULL 1848 * 1849 * Scan the connectors attached to the fb_helper and try to put together a 1850 * setup after notification of a change in output configuration. 1851 * 1852 * Called at runtime, takes the mode config locks to be able to check/change the 1853 * modeset configuration. Must be run from process context (which usually means 1854 * either the output polling work or a work item launched from the driver's 1855 * hotplug interrupt). 1856 * 1857 * Note that drivers may call this even before calling 1858 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 1859 * for a race-free fbcon setup and will make sure that the fbdev emulation will 1860 * not miss any hotplug events. 1861 * 1862 * RETURNS: 1863 * 0 on success and a non-zero error code otherwise. 1864 */ 1865 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 1866 { 1867 int err = 0; 1868 1869 if (!drm_fbdev_emulation || !fb_helper) 1870 return 0; 1871 1872 mutex_lock(&fb_helper->lock); 1873 if (fb_helper->deferred_setup) { 1874 err = __drm_fb_helper_initial_config_and_unlock(fb_helper); 1875 return err; 1876 } 1877 1878 if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) { 1879 fb_helper->delayed_hotplug = true; 1880 mutex_unlock(&fb_helper->lock); 1881 return err; 1882 } 1883 1884 drm_master_internal_release(fb_helper->dev); 1885 1886 drm_dbg_kms(fb_helper->dev, "\n"); 1887 1888 drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height); 1889 drm_setup_crtcs_fb(fb_helper); 1890 mutex_unlock(&fb_helper->lock); 1891 1892 drm_fb_helper_set_par(fb_helper->info); 1893 1894 return 0; 1895 } 1896 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 1897