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