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