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 /* 1432 * Workaround for SDL 1.2, which is known to be setting all pixel format 1433 * fields values to zero in some cases. We treat this situation as a 1434 * kind of "use some reasonable autodetected values". 1435 */ 1436 if (!var->red.offset && !var->green.offset && 1437 !var->blue.offset && !var->transp.offset && 1438 !var->red.length && !var->green.length && 1439 !var->blue.length && !var->transp.length && 1440 !var->red.msb_right && !var->green.msb_right && 1441 !var->blue.msb_right && !var->transp.msb_right) { 1442 drm_fb_helper_fill_pixel_fmt(var, format); 1443 } 1444 1445 /* 1446 * Likewise, bits_per_pixel should be rounded up to a supported value. 1447 */ 1448 var->bits_per_pixel = bpp; 1449 1450 /* 1451 * drm fbdev emulation doesn't support changing the pixel format at all, 1452 * so reject all pixel format changing requests. 1453 */ 1454 if (!drm_fb_pixel_format_equal(var, &info->var)) { 1455 drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n"); 1456 return -EINVAL; 1457 } 1458 1459 return 0; 1460 } 1461 EXPORT_SYMBOL(drm_fb_helper_check_var); 1462 1463 #endif /* __linux__ */ 1464 1465 /** 1466 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par 1467 * @info: fbdev registered by the helper 1468 * 1469 * This will let fbcon do the mode init and is called at initialization time by 1470 * the fbdev core when registering the driver, and later on through the hotplug 1471 * callback. 1472 */ 1473 int drm_fb_helper_set_par(struct fb_info *info) 1474 { 1475 struct drm_fb_helper *fb_helper = info->par; 1476 struct fb_var_screeninfo *var = &info->var; 1477 bool force; 1478 1479 if (oops_in_progress) 1480 return -EBUSY; 1481 1482 if (var->pixclock != 0) { 1483 drm_err(fb_helper->dev, "PIXEL CLOCK SET\n"); 1484 return -EINVAL; 1485 } 1486 1487 /* 1488 * Normally we want to make sure that a kms master takes precedence over 1489 * fbdev, to avoid fbdev flickering and occasionally stealing the 1490 * display status. But Xorg first sets the vt back to text mode using 1491 * the KDSET IOCTL with KD_TEXT, and only after that drops the master 1492 * status when exiting. 1493 * 1494 * In the past this was caught by drm_fb_helper_lastclose(), but on 1495 * modern systems where logind always keeps a drm fd open to orchestrate 1496 * the vt switching, this doesn't work. 1497 * 1498 * To not break the userspace ABI we have this special case here, which 1499 * is only used for the above case. Everything else uses the normal 1500 * commit function, which ensures that we never steal the display from 1501 * an active drm master. 1502 */ 1503 #ifdef __linux__ 1504 force = var->activate & FB_ACTIVATE_KD_TEXT; 1505 #else 1506 force = true; 1507 #endif 1508 1509 __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force); 1510 1511 return 0; 1512 } 1513 EXPORT_SYMBOL(drm_fb_helper_set_par); 1514 1515 #ifdef notyet 1516 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y) 1517 { 1518 struct drm_mode_set *mode_set; 1519 1520 mutex_lock(&fb_helper->client.modeset_mutex); 1521 drm_client_for_each_modeset(mode_set, &fb_helper->client) { 1522 mode_set->x = x; 1523 mode_set->y = y; 1524 } 1525 mutex_unlock(&fb_helper->client.modeset_mutex); 1526 } 1527 #endif 1528 1529 static int pan_display_atomic(struct fb_var_screeninfo *var, 1530 struct fb_info *info) 1531 { 1532 STUB(); 1533 return -ENOSYS; 1534 #ifdef notyet 1535 struct drm_fb_helper *fb_helper = info->par; 1536 int ret; 1537 1538 pan_set(fb_helper, var->xoffset, var->yoffset); 1539 1540 ret = drm_client_modeset_commit_locked(&fb_helper->client); 1541 if (!ret) { 1542 info->var.xoffset = var->xoffset; 1543 info->var.yoffset = var->yoffset; 1544 } else 1545 pan_set(fb_helper, info->var.xoffset, info->var.yoffset); 1546 1547 return ret; 1548 #endif 1549 } 1550 1551 static int pan_display_legacy(struct fb_var_screeninfo *var, 1552 struct fb_info *info) 1553 { 1554 STUB(); 1555 return -ENOSYS; 1556 #ifdef notyet 1557 struct drm_fb_helper *fb_helper = info->par; 1558 struct drm_client_dev *client = &fb_helper->client; 1559 struct drm_mode_set *modeset; 1560 int ret = 0; 1561 1562 mutex_lock(&client->modeset_mutex); 1563 drm_modeset_lock_all(fb_helper->dev); 1564 drm_client_for_each_modeset(modeset, client) { 1565 modeset->x = var->xoffset; 1566 modeset->y = var->yoffset; 1567 1568 if (modeset->num_connectors) { 1569 ret = drm_mode_set_config_internal(modeset); 1570 if (!ret) { 1571 info->var.xoffset = var->xoffset; 1572 info->var.yoffset = var->yoffset; 1573 } 1574 } 1575 } 1576 drm_modeset_unlock_all(fb_helper->dev); 1577 mutex_unlock(&client->modeset_mutex); 1578 1579 return ret; 1580 #endif 1581 } 1582 1583 /** 1584 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display 1585 * @var: updated screen information 1586 * @info: fbdev registered by the helper 1587 */ 1588 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1589 struct fb_info *info) 1590 { 1591 struct drm_fb_helper *fb_helper = info->par; 1592 struct drm_device *dev = fb_helper->dev; 1593 int ret; 1594 1595 if (oops_in_progress) 1596 return -EBUSY; 1597 1598 mutex_lock(&fb_helper->lock); 1599 if (!drm_master_internal_acquire(dev)) { 1600 ret = -EBUSY; 1601 goto unlock; 1602 } 1603 1604 if (drm_drv_uses_atomic_modeset(dev)) 1605 ret = pan_display_atomic(var, info); 1606 else 1607 ret = pan_display_legacy(var, info); 1608 1609 drm_master_internal_release(dev); 1610 unlock: 1611 mutex_unlock(&fb_helper->lock); 1612 1613 return ret; 1614 } 1615 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1616 1617 /* 1618 * Allocates the backing storage and sets up the fbdev info structure through 1619 * the ->fb_probe callback. 1620 */ 1621 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1622 int preferred_bpp) 1623 { 1624 struct drm_client_dev *client = &fb_helper->client; 1625 struct drm_device *dev = fb_helper->dev; 1626 struct drm_mode_config *config = &dev->mode_config; 1627 int ret = 0; 1628 int crtc_count = 0; 1629 struct drm_connector_list_iter conn_iter; 1630 struct drm_fb_helper_surface_size sizes; 1631 struct drm_connector *connector; 1632 struct drm_mode_set *mode_set; 1633 int best_depth = 0; 1634 1635 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1636 sizes.surface_depth = 24; 1637 sizes.surface_bpp = 32; 1638 sizes.fb_width = (u32)-1; 1639 sizes.fb_height = (u32)-1; 1640 1641 /* 1642 * If driver picks 8 or 16 by default use that for both depth/bpp 1643 * to begin with 1644 */ 1645 if (preferred_bpp != sizes.surface_bpp) 1646 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1647 1648 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); 1649 drm_client_for_each_connector_iter(connector, &conn_iter) { 1650 struct drm_cmdline_mode *cmdline_mode; 1651 1652 cmdline_mode = &connector->cmdline_mode; 1653 1654 if (cmdline_mode->bpp_specified) { 1655 switch (cmdline_mode->bpp) { 1656 case 8: 1657 sizes.surface_depth = sizes.surface_bpp = 8; 1658 break; 1659 case 15: 1660 sizes.surface_depth = 15; 1661 sizes.surface_bpp = 16; 1662 break; 1663 case 16: 1664 sizes.surface_depth = sizes.surface_bpp = 16; 1665 break; 1666 case 24: 1667 sizes.surface_depth = sizes.surface_bpp = 24; 1668 break; 1669 case 32: 1670 sizes.surface_depth = 24; 1671 sizes.surface_bpp = 32; 1672 break; 1673 } 1674 break; 1675 } 1676 } 1677 drm_connector_list_iter_end(&conn_iter); 1678 1679 /* 1680 * If we run into a situation where, for example, the primary plane 1681 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth 1682 * 16) we need to scale down the depth of the sizes we request. 1683 */ 1684 mutex_lock(&client->modeset_mutex); 1685 drm_client_for_each_modeset(mode_set, client) { 1686 struct drm_crtc *crtc = mode_set->crtc; 1687 struct drm_plane *plane = crtc->primary; 1688 int j; 1689 1690 drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc)); 1691 1692 for (j = 0; j < plane->format_count; j++) { 1693 const struct drm_format_info *fmt; 1694 1695 fmt = drm_format_info(plane->format_types[j]); 1696 1697 /* 1698 * Do not consider YUV or other complicated formats 1699 * for framebuffers. This means only legacy formats 1700 * are supported (fmt->depth is a legacy field) but 1701 * the framebuffer emulation can only deal with such 1702 * formats, specifically RGB/BGA formats. 1703 */ 1704 if (fmt->depth == 0) 1705 continue; 1706 1707 /* We found a perfect fit, great */ 1708 if (fmt->depth == sizes.surface_depth) { 1709 best_depth = fmt->depth; 1710 break; 1711 } 1712 1713 /* Skip depths above what we're looking for */ 1714 if (fmt->depth > sizes.surface_depth) 1715 continue; 1716 1717 /* Best depth found so far */ 1718 if (fmt->depth > best_depth) 1719 best_depth = fmt->depth; 1720 } 1721 } 1722 if (sizes.surface_depth != best_depth && best_depth) { 1723 drm_info(dev, "requested bpp %d, scaled depth down to %d", 1724 sizes.surface_bpp, best_depth); 1725 sizes.surface_depth = best_depth; 1726 } 1727 1728 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1729 crtc_count = 0; 1730 drm_client_for_each_modeset(mode_set, client) { 1731 struct drm_display_mode *desired_mode; 1732 int x, y, j; 1733 /* in case of tile group, are we the last tile vert or horiz? 1734 * If no tile group you are always the last one both vertically 1735 * and horizontally 1736 */ 1737 bool lastv = true, lasth = true; 1738 1739 desired_mode = mode_set->mode; 1740 1741 if (!desired_mode) 1742 continue; 1743 1744 crtc_count++; 1745 1746 x = mode_set->x; 1747 y = mode_set->y; 1748 1749 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); 1750 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); 1751 1752 for (j = 0; j < mode_set->num_connectors; j++) { 1753 struct drm_connector *connector = mode_set->connectors[j]; 1754 1755 if (connector->has_tile && 1756 desired_mode->hdisplay == connector->tile_h_size && 1757 desired_mode->vdisplay == connector->tile_v_size) { 1758 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1759 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1760 /* cloning to multiple tiles is just crazy-talk, so: */ 1761 break; 1762 } 1763 } 1764 1765 if (lasth) 1766 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); 1767 if (lastv) 1768 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); 1769 } 1770 mutex_unlock(&client->modeset_mutex); 1771 1772 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1773 #ifdef __linux__ 1774 drm_info(dev, "Cannot find any crtc or sizes\n"); 1775 1776 /* First time: disable all crtc's.. */ 1777 if (!fb_helper->deferred_setup) 1778 drm_client_modeset_commit(client); 1779 return -EAGAIN; 1780 #else 1781 drm_info(dev, "Cannot find any crtc or sizes - going 1024x768\n"); 1782 sizes.fb_width = sizes.surface_width = 1024; 1783 sizes.fb_height = sizes.surface_height = 768; 1784 #endif 1785 } 1786 1787 /* Handle our overallocation */ 1788 sizes.surface_height *= drm_fbdev_overalloc; 1789 sizes.surface_height /= 100; 1790 if (sizes.surface_height > config->max_height) { 1791 drm_dbg_kms(dev, "Fbdev over-allocation too large; clamping height to %d\n", 1792 config->max_height); 1793 sizes.surface_height = config->max_height; 1794 } 1795 1796 /* push down into drivers */ 1797 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1798 if (ret < 0) 1799 return ret; 1800 1801 #ifdef __linux__ 1802 strcpy(fb_helper->fb->comm, "[fbcon]"); 1803 #else 1804 strlcpy(fb_helper->fb->comm, "[fbcon]", sizeof(fb_helper->fb->comm)); 1805 #endif 1806 return 0; 1807 } 1808 1809 #ifdef __linux__ 1810 1811 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1812 bool is_color_indexed) 1813 { 1814 info->fix.type = FB_TYPE_PACKED_PIXELS; 1815 info->fix.visual = is_color_indexed ? FB_VISUAL_PSEUDOCOLOR 1816 : FB_VISUAL_TRUECOLOR; 1817 info->fix.mmio_start = 0; 1818 info->fix.mmio_len = 0; 1819 info->fix.type_aux = 0; 1820 info->fix.xpanstep = 1; /* doing it in hw */ 1821 info->fix.ypanstep = 1; /* doing it in hw */ 1822 info->fix.ywrapstep = 0; 1823 info->fix.accel = FB_ACCEL_NONE; 1824 1825 info->fix.line_length = pitch; 1826 } 1827 1828 #endif /* __linux__ */ 1829 1830 static void drm_fb_helper_fill_var(struct fb_info *info, 1831 struct drm_fb_helper *fb_helper, 1832 uint32_t fb_width, uint32_t fb_height) 1833 { 1834 struct drm_framebuffer *fb = fb_helper->fb; 1835 const struct drm_format_info *format = fb->format; 1836 1837 switch (format->format) { 1838 case DRM_FORMAT_C1: 1839 case DRM_FORMAT_C2: 1840 case DRM_FORMAT_C4: 1841 /* supported format with sub-byte pixels */ 1842 break; 1843 1844 default: 1845 WARN_ON((drm_format_info_block_width(format, 0) > 1) || 1846 (drm_format_info_block_height(format, 0) > 1)); 1847 break; 1848 } 1849 1850 #ifdef notyet 1851 info->pseudo_palette = fb_helper->pseudo_palette; 1852 info->var.xres_virtual = fb->width; 1853 info->var.yres_virtual = fb->height; 1854 info->var.bits_per_pixel = drm_format_info_bpp(format, 0); 1855 info->var.accel_flags = FB_ACCELF_TEXT; 1856 info->var.xoffset = 0; 1857 info->var.yoffset = 0; 1858 info->var.activate = FB_ACTIVATE_NOW; 1859 1860 drm_fb_helper_fill_pixel_fmt(&info->var, format); 1861 #endif 1862 1863 info->var.xres = fb_width; 1864 info->var.yres = fb_height; 1865 } 1866 1867 /** 1868 * drm_fb_helper_fill_info - initializes fbdev information 1869 * @info: fbdev instance to set up 1870 * @fb_helper: fb helper instance to use as template 1871 * @sizes: describes fbdev size and scanout surface size 1872 * 1873 * Sets up the variable and fixed fbdev metainformation from the given fb helper 1874 * instance and the drm framebuffer allocated in &drm_fb_helper.fb. 1875 * 1876 * Drivers should call this (or their equivalent setup code) from their 1877 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev 1878 * backing storage framebuffer. 1879 */ 1880 void drm_fb_helper_fill_info(struct fb_info *info, 1881 struct drm_fb_helper *fb_helper, 1882 struct drm_fb_helper_surface_size *sizes) 1883 { 1884 #ifdef __linux__ 1885 struct drm_framebuffer *fb = fb_helper->fb; 1886 1887 drm_fb_helper_fill_fix(info, fb->pitches[0], 1888 fb->format->is_color_indexed); 1889 #endif 1890 drm_fb_helper_fill_var(info, fb_helper, 1891 sizes->fb_width, sizes->fb_height); 1892 1893 info->par = fb_helper; 1894 #ifdef __linux__ 1895 /* 1896 * The DRM drivers fbdev emulation device name can be confusing if the 1897 * driver name also has a "drm" suffix on it. Leading to names such as 1898 * "simpledrmdrmfb" in /proc/fb. Unfortunately, it's an uAPI and can't 1899 * be changed due user-space tools (e.g: pm-utils) matching against it. 1900 */ 1901 snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb", 1902 fb_helper->dev->driver->name); 1903 #endif 1904 } 1905 EXPORT_SYMBOL(drm_fb_helper_fill_info); 1906 1907 /* 1908 * This is a continuation of drm_setup_crtcs() that sets up anything related 1909 * to the framebuffer. During initialization, drm_setup_crtcs() is called before 1910 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). 1911 * So, any setup that touches those fields needs to be done here instead of in 1912 * drm_setup_crtcs(). 1913 */ 1914 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) 1915 { 1916 struct drm_client_dev *client = &fb_helper->client; 1917 struct drm_connector_list_iter conn_iter; 1918 struct fb_info *info = fb_helper->fbdev; 1919 unsigned int rotation, sw_rotations = 0; 1920 struct drm_connector *connector; 1921 struct drm_mode_set *modeset; 1922 1923 mutex_lock(&client->modeset_mutex); 1924 drm_client_for_each_modeset(modeset, client) { 1925 if (!modeset->num_connectors) 1926 continue; 1927 1928 modeset->fb = fb_helper->fb; 1929 1930 if (drm_client_rotation(modeset, &rotation)) 1931 /* Rotating in hardware, fbcon should not rotate */ 1932 sw_rotations |= DRM_MODE_ROTATE_0; 1933 else 1934 sw_rotations |= rotation; 1935 } 1936 mutex_unlock(&client->modeset_mutex); 1937 1938 drm_connector_list_iter_begin(fb_helper->dev, &conn_iter); 1939 drm_client_for_each_connector_iter(connector, &conn_iter) { 1940 1941 /* use first connected connector for the physical dimensions */ 1942 if (connector->status == connector_status_connected) { 1943 info->var.width = connector->display_info.width_mm; 1944 info->var.height = connector->display_info.height_mm; 1945 break; 1946 } 1947 } 1948 drm_connector_list_iter_end(&conn_iter); 1949 1950 switch (sw_rotations) { 1951 case DRM_MODE_ROTATE_0: 1952 info->fbcon_rotate_hint = FB_ROTATE_UR; 1953 break; 1954 case DRM_MODE_ROTATE_90: 1955 info->fbcon_rotate_hint = FB_ROTATE_CCW; 1956 break; 1957 case DRM_MODE_ROTATE_180: 1958 info->fbcon_rotate_hint = FB_ROTATE_UD; 1959 break; 1960 case DRM_MODE_ROTATE_270: 1961 info->fbcon_rotate_hint = FB_ROTATE_CW; 1962 break; 1963 default: 1964 /* 1965 * Multiple bits are set / multiple rotations requested 1966 * fbcon cannot handle separate rotation settings per 1967 * output, so fallback to unrotated. 1968 */ 1969 info->fbcon_rotate_hint = FB_ROTATE_UR; 1970 } 1971 } 1972 1973 /* Note: Drops fb_helper->lock before returning. */ 1974 static int 1975 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, 1976 int bpp_sel) 1977 { 1978 struct drm_device *dev = fb_helper->dev; 1979 struct fb_info *info; 1980 unsigned int width, height; 1981 int ret; 1982 1983 width = dev->mode_config.max_width; 1984 height = dev->mode_config.max_height; 1985 1986 drm_client_modeset_probe(&fb_helper->client, width, height); 1987 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 1988 if (ret < 0) { 1989 if (ret == -EAGAIN) { 1990 fb_helper->preferred_bpp = bpp_sel; 1991 fb_helper->deferred_setup = true; 1992 ret = 0; 1993 } 1994 mutex_unlock(&fb_helper->lock); 1995 1996 return ret; 1997 } 1998 drm_setup_crtcs_fb(fb_helper); 1999 2000 fb_helper->deferred_setup = false; 2001 2002 info = fb_helper->fbdev; 2003 info->var.pixclock = 0; 2004 /* Shamelessly allow physical address leaking to userspace */ 2005 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 2006 if (!drm_leak_fbdev_smem) 2007 #endif 2008 /* don't leak any physical addresses to userspace */ 2009 info->flags |= FBINFO_HIDE_SMEM_START; 2010 2011 /* Need to drop locks to avoid recursive deadlock in 2012 * register_framebuffer. This is ok because the only thing left to do is 2013 * register the fbdev emulation instance in kernel_fb_helper_list. */ 2014 mutex_unlock(&fb_helper->lock); 2015 2016 ret = register_framebuffer(info); 2017 if (ret < 0) 2018 return ret; 2019 2020 #ifdef __linux__ 2021 drm_info(dev, "fb%d: %s frame buffer device\n", 2022 info->node, info->fix.id); 2023 #endif 2024 2025 mutex_lock(&kernel_fb_helper_lock); 2026 if (list_empty(&kernel_fb_helper_list)) 2027 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 2028 2029 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 2030 mutex_unlock(&kernel_fb_helper_lock); 2031 2032 return 0; 2033 } 2034 2035 /** 2036 * drm_fb_helper_initial_config - setup a sane initial connector configuration 2037 * @fb_helper: fb_helper device struct 2038 * @bpp_sel: bpp value to use for the framebuffer configuration 2039 * 2040 * Scans the CRTCs and connectors and tries to put together an initial setup. 2041 * At the moment, this is a cloned configuration across all heads with 2042 * a new framebuffer object as the backing store. 2043 * 2044 * Note that this also registers the fbdev and so allows userspace to call into 2045 * the driver through the fbdev interfaces. 2046 * 2047 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback 2048 * to let the driver allocate and initialize the fbdev info structure and the 2049 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided 2050 * as a helper to setup simple default values for the fbdev info structure. 2051 * 2052 * HANG DEBUGGING: 2053 * 2054 * When you have fbcon support built-in or already loaded, this function will do 2055 * a full modeset to setup the fbdev console. Due to locking misdesign in the 2056 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 2057 * console_lock. Until console_unlock is called no dmesg lines will be sent out 2058 * to consoles, not even serial console. This means when your driver crashes, 2059 * you will see absolutely nothing else but a system stuck in this function, 2060 * with no further output. Any kind of printk() you place within your own driver 2061 * or in the drm core modeset code will also never show up. 2062 * 2063 * Standard debug practice is to run the fbcon setup without taking the 2064 * console_lock as a hack, to be able to see backtraces and crashes on the 2065 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 2066 * cmdline option. 2067 * 2068 * The other option is to just disable fbdev emulation since very likely the 2069 * first modeset from userspace will crash in the same way, and is even easier 2070 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 2071 * kernel cmdline option. 2072 * 2073 * RETURNS: 2074 * Zero if everything went ok, nonzero otherwise. 2075 */ 2076 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 2077 { 2078 int ret; 2079 2080 if (!drm_fbdev_emulation) 2081 return 0; 2082 2083 mutex_lock(&fb_helper->lock); 2084 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); 2085 2086 return ret; 2087 } 2088 EXPORT_SYMBOL(drm_fb_helper_initial_config); 2089 2090 /** 2091 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 2092 * probing all the outputs attached to the fb 2093 * @fb_helper: driver-allocated fbdev helper, can be NULL 2094 * 2095 * Scan the connectors attached to the fb_helper and try to put together a 2096 * setup after notification of a change in output configuration. 2097 * 2098 * Called at runtime, takes the mode config locks to be able to check/change the 2099 * modeset configuration. Must be run from process context (which usually means 2100 * either the output polling work or a work item launched from the driver's 2101 * hotplug interrupt). 2102 * 2103 * Note that drivers may call this even before calling 2104 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 2105 * for a race-free fbcon setup and will make sure that the fbdev emulation will 2106 * not miss any hotplug events. 2107 * 2108 * RETURNS: 2109 * 0 on success and a non-zero error code otherwise. 2110 */ 2111 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 2112 { 2113 struct fb_info *fbi; 2114 int err = 0; 2115 2116 if (!drm_fbdev_emulation || !fb_helper) 2117 return 0; 2118 2119 mutex_lock(&fb_helper->lock); 2120 if (fb_helper->deferred_setup) { 2121 err = __drm_fb_helper_initial_config_and_unlock(fb_helper, 2122 fb_helper->preferred_bpp); 2123 return err; 2124 } 2125 2126 if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) { 2127 fb_helper->delayed_hotplug = true; 2128 mutex_unlock(&fb_helper->lock); 2129 return err; 2130 } 2131 2132 drm_master_internal_release(fb_helper->dev); 2133 2134 drm_dbg_kms(fb_helper->dev, "\n"); 2135 2136 drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height); 2137 drm_setup_crtcs_fb(fb_helper); 2138 mutex_unlock(&fb_helper->lock); 2139 2140 fbi = fb_helper->fbdev; 2141 if (fbi->fbops && fbi->fbops->fb_set_par) 2142 fbi->fbops->fb_set_par(fbi); 2143 else 2144 drm_fb_helper_set_par(fb_helper->fbdev); 2145 2146 return 0; 2147 } 2148 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2149 2150 /** 2151 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation 2152 * @dev: DRM device 2153 * 2154 * This function can be used as the &drm_driver->lastclose callback for drivers 2155 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked(). 2156 */ 2157 void drm_fb_helper_lastclose(struct drm_device *dev) 2158 { 2159 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper); 2160 } 2161 EXPORT_SYMBOL(drm_fb_helper_lastclose); 2162 2163 /** 2164 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed 2165 * helper for fbdev emulation 2166 * @dev: DRM device 2167 * 2168 * This function can be used as the 2169 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only 2170 * need to call drm_fb_helper_hotplug_event(). 2171 */ 2172 void drm_fb_helper_output_poll_changed(struct drm_device *dev) 2173 { 2174 drm_fb_helper_hotplug_event(dev->fb_helper); 2175 } 2176 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed); 2177 2178 #ifdef __linux__ 2179 /* @user: 1=userspace, 0=fbcon */ 2180 static int drm_fbdev_fb_open(struct fb_info *info, int user) 2181 { 2182 struct drm_fb_helper *fb_helper = info->par; 2183 2184 /* No need to take a ref for fbcon because it unbinds on unregister */ 2185 if (user && !try_module_get(fb_helper->dev->driver->fops->owner)) 2186 return -ENODEV; 2187 2188 return 0; 2189 } 2190 2191 static int drm_fbdev_fb_release(struct fb_info *info, int user) 2192 { 2193 struct drm_fb_helper *fb_helper = info->par; 2194 2195 if (user) 2196 module_put(fb_helper->dev->driver->fops->owner); 2197 2198 return 0; 2199 } 2200 2201 #endif /* __linux__ */ 2202 2203 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper) 2204 { 2205 struct fb_info *fbi = fb_helper->fbdev; 2206 void *shadow = NULL; 2207 2208 if (!fb_helper->dev) 2209 return; 2210 2211 if (fbi) { 2212 #ifdef notyet 2213 if (fbi->fbdefio) 2214 fb_deferred_io_cleanup(fbi); 2215 #endif 2216 if (drm_fbdev_use_shadow_fb(fb_helper)) 2217 shadow = fbi->screen_buffer; 2218 } 2219 2220 drm_fb_helper_fini(fb_helper); 2221 2222 if (shadow) 2223 vfree(shadow); 2224 else if (fb_helper->buffer) 2225 drm_client_buffer_vunmap(fb_helper->buffer); 2226 2227 drm_client_framebuffer_delete(fb_helper->buffer); 2228 } 2229 2230 static void drm_fbdev_release(struct drm_fb_helper *fb_helper) 2231 { 2232 drm_fbdev_cleanup(fb_helper); 2233 drm_client_release(&fb_helper->client); 2234 kfree(fb_helper); 2235 } 2236 2237 #ifdef __linux__ 2238 2239 /* 2240 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of 2241 * unregister_framebuffer() or fb_release(). 2242 */ 2243 static void drm_fbdev_fb_destroy(struct fb_info *info) 2244 { 2245 drm_fbdev_release(info->par); 2246 } 2247 2248 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) 2249 { 2250 struct drm_fb_helper *fb_helper = info->par; 2251 2252 if (drm_fbdev_use_shadow_fb(fb_helper)) 2253 return fb_deferred_io_mmap(info, vma); 2254 else if (fb_helper->dev->driver->gem_prime_mmap) 2255 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma); 2256 else 2257 return -ENODEV; 2258 } 2259 2260 static bool drm_fbdev_use_iomem(struct fb_info *info) 2261 { 2262 struct drm_fb_helper *fb_helper = info->par; 2263 struct drm_client_buffer *buffer = fb_helper->buffer; 2264 2265 return !drm_fbdev_use_shadow_fb(fb_helper) && buffer->map.is_iomem; 2266 } 2267 2268 static ssize_t fb_read_screen_base(struct fb_info *info, char __user *buf, size_t count, 2269 loff_t pos) 2270 { 2271 const char __iomem *src = info->screen_base + pos; 2272 size_t alloc_size = min_t(size_t, count, PAGE_SIZE); 2273 ssize_t ret = 0; 2274 int err = 0; 2275 char *tmp; 2276 2277 tmp = kmalloc(alloc_size, GFP_KERNEL); 2278 if (!tmp) 2279 return -ENOMEM; 2280 2281 while (count) { 2282 size_t c = min_t(size_t, count, alloc_size); 2283 2284 memcpy_fromio(tmp, src, c); 2285 if (copy_to_user(buf, tmp, c)) { 2286 err = -EFAULT; 2287 break; 2288 } 2289 2290 src += c; 2291 buf += c; 2292 ret += c; 2293 count -= c; 2294 } 2295 2296 kfree(tmp); 2297 2298 return ret ? ret : err; 2299 } 2300 2301 static ssize_t fb_read_screen_buffer(struct fb_info *info, char __user *buf, size_t count, 2302 loff_t pos) 2303 { 2304 const char *src = info->screen_buffer + pos; 2305 2306 if (copy_to_user(buf, src, count)) 2307 return -EFAULT; 2308 2309 return count; 2310 } 2311 2312 static ssize_t drm_fbdev_fb_read(struct fb_info *info, char __user *buf, 2313 size_t count, loff_t *ppos) 2314 { 2315 loff_t pos = *ppos; 2316 size_t total_size; 2317 ssize_t ret; 2318 2319 if (info->screen_size) 2320 total_size = info->screen_size; 2321 else 2322 total_size = info->fix.smem_len; 2323 2324 if (pos >= total_size) 2325 return 0; 2326 if (count >= total_size) 2327 count = total_size; 2328 if (total_size - count < pos) 2329 count = total_size - pos; 2330 2331 if (drm_fbdev_use_iomem(info)) 2332 ret = fb_read_screen_base(info, buf, count, pos); 2333 else 2334 ret = fb_read_screen_buffer(info, buf, count, pos); 2335 2336 if (ret > 0) 2337 *ppos += ret; 2338 2339 return ret; 2340 } 2341 2342 static ssize_t fb_write_screen_base(struct fb_info *info, const char __user *buf, size_t count, 2343 loff_t pos) 2344 { 2345 char __iomem *dst = info->screen_base + pos; 2346 size_t alloc_size = min_t(size_t, count, PAGE_SIZE); 2347 ssize_t ret = 0; 2348 int err = 0; 2349 u8 *tmp; 2350 2351 tmp = kmalloc(alloc_size, GFP_KERNEL); 2352 if (!tmp) 2353 return -ENOMEM; 2354 2355 while (count) { 2356 size_t c = min_t(size_t, count, alloc_size); 2357 2358 if (copy_from_user(tmp, buf, c)) { 2359 err = -EFAULT; 2360 break; 2361 } 2362 memcpy_toio(dst, tmp, c); 2363 2364 dst += c; 2365 buf += c; 2366 ret += c; 2367 count -= c; 2368 } 2369 2370 kfree(tmp); 2371 2372 return ret ? ret : err; 2373 } 2374 2375 static ssize_t fb_write_screen_buffer(struct fb_info *info, const char __user *buf, size_t count, 2376 loff_t pos) 2377 { 2378 char *dst = info->screen_buffer + pos; 2379 2380 if (copy_from_user(dst, buf, count)) 2381 return -EFAULT; 2382 2383 return count; 2384 } 2385 2386 static ssize_t drm_fbdev_fb_write(struct fb_info *info, const char __user *buf, 2387 size_t count, loff_t *ppos) 2388 { 2389 loff_t pos = *ppos; 2390 size_t total_size; 2391 ssize_t ret; 2392 struct drm_rect damage_area; 2393 int err = 0; 2394 2395 if (info->screen_size) 2396 total_size = info->screen_size; 2397 else 2398 total_size = info->fix.smem_len; 2399 2400 if (pos > total_size) 2401 return -EFBIG; 2402 if (count > total_size) { 2403 err = -EFBIG; 2404 count = total_size; 2405 } 2406 if (total_size - count < pos) { 2407 if (!err) 2408 err = -ENOSPC; 2409 count = total_size - pos; 2410 } 2411 2412 /* 2413 * Copy to framebuffer even if we already logged an error. Emulates 2414 * the behavior of the original fbdev implementation. 2415 */ 2416 if (drm_fbdev_use_iomem(info)) 2417 ret = fb_write_screen_base(info, buf, count, pos); 2418 else 2419 ret = fb_write_screen_buffer(info, buf, count, pos); 2420 2421 if (ret < 0) 2422 return ret; /* return last error, if any */ 2423 else if (!ret) 2424 return err; /* return previous error, if any */ 2425 2426 *ppos += ret; 2427 2428 drm_fb_helper_memory_range_to_clip(info, pos, ret, &damage_area); 2429 drm_fb_helper_damage(info, damage_area.x1, damage_area.y1, 2430 drm_rect_width(&damage_area), 2431 drm_rect_height(&damage_area)); 2432 2433 return ret; 2434 } 2435 2436 static void drm_fbdev_fb_fillrect(struct fb_info *info, 2437 const struct fb_fillrect *rect) 2438 { 2439 if (drm_fbdev_use_iomem(info)) 2440 drm_fb_helper_cfb_fillrect(info, rect); 2441 else 2442 drm_fb_helper_sys_fillrect(info, rect); 2443 } 2444 2445 static void drm_fbdev_fb_copyarea(struct fb_info *info, 2446 const struct fb_copyarea *area) 2447 { 2448 if (drm_fbdev_use_iomem(info)) 2449 drm_fb_helper_cfb_copyarea(info, area); 2450 else 2451 drm_fb_helper_sys_copyarea(info, area); 2452 } 2453 2454 static void drm_fbdev_fb_imageblit(struct fb_info *info, 2455 const struct fb_image *image) 2456 { 2457 if (drm_fbdev_use_iomem(info)) 2458 drm_fb_helper_cfb_imageblit(info, image); 2459 else 2460 drm_fb_helper_sys_imageblit(info, image); 2461 } 2462 2463 #endif /* __linux__ */ 2464 2465 static const struct fb_ops drm_fbdev_fb_ops = { 2466 #ifdef notyet 2467 .owner = THIS_MODULE, 2468 DRM_FB_HELPER_DEFAULT_OPS, 2469 .fb_open = drm_fbdev_fb_open, 2470 .fb_release = drm_fbdev_fb_release, 2471 .fb_destroy = drm_fbdev_fb_destroy, 2472 .fb_mmap = drm_fbdev_fb_mmap, 2473 .fb_read = drm_fbdev_fb_read, 2474 .fb_write = drm_fbdev_fb_write, 2475 .fb_fillrect = drm_fbdev_fb_fillrect, 2476 .fb_copyarea = drm_fbdev_fb_copyarea, 2477 .fb_imageblit = drm_fbdev_fb_imageblit, 2478 #else 2479 DRM_FB_HELPER_DEFAULT_OPS, 2480 #endif 2481 }; 2482 2483 #ifdef notyet 2484 static struct fb_deferred_io drm_fbdev_defio = { 2485 .delay = HZ / 20, 2486 .deferred_io = drm_fb_helper_deferred_io, 2487 }; 2488 #endif 2489 2490 /* 2491 * This function uses the client API to create a framebuffer backed by a dumb buffer. 2492 * 2493 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect, 2494 * fb_copyarea, fb_imageblit. 2495 */ 2496 static int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, 2497 struct drm_fb_helper_surface_size *sizes) 2498 { 2499 struct drm_client_dev *client = &fb_helper->client; 2500 struct drm_device *dev = fb_helper->dev; 2501 struct drm_client_buffer *buffer; 2502 struct drm_framebuffer *fb; 2503 struct fb_info *fbi; 2504 u32 format; 2505 struct iosys_map map; 2506 int ret; 2507 2508 drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n", 2509 sizes->surface_width, sizes->surface_height, 2510 sizes->surface_bpp); 2511 2512 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); 2513 buffer = drm_client_framebuffer_create(client, sizes->surface_width, 2514 sizes->surface_height, format); 2515 if (IS_ERR(buffer)) 2516 return PTR_ERR(buffer); 2517 2518 fb_helper->buffer = buffer; 2519 fb_helper->fb = buffer->fb; 2520 fb = buffer->fb; 2521 2522 fbi = drm_fb_helper_alloc_fbi(fb_helper); 2523 if (IS_ERR(fbi)) 2524 return PTR_ERR(fbi); 2525 2526 fbi->fbops = &drm_fbdev_fb_ops; 2527 fbi->screen_size = sizes->surface_height * fb->pitches[0]; 2528 #ifdef __linux__ 2529 fbi->fix.smem_len = fbi->screen_size; 2530 #endif 2531 fbi->flags = FBINFO_DEFAULT; 2532 2533 drm_fb_helper_fill_info(fbi, fb_helper, sizes); 2534 2535 if (drm_fbdev_use_shadow_fb(fb_helper)) { 2536 fbi->screen_buffer = vzalloc(fbi->screen_size); 2537 if (!fbi->screen_buffer) 2538 return -ENOMEM; 2539 fbi->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST; 2540 2541 #ifdef notyet 2542 fbi->fbdefio = &drm_fbdev_defio; 2543 fb_deferred_io_init(fbi); 2544 #endif 2545 } else { 2546 /* buffer is mapped for HW framebuffer */ 2547 ret = drm_client_buffer_vmap(fb_helper->buffer, &map); 2548 if (ret) 2549 return ret; 2550 if (map.is_iomem) { 2551 fbi->screen_base = map.vaddr_iomem; 2552 } else { 2553 fbi->screen_buffer = map.vaddr; 2554 fbi->flags |= FBINFO_VIRTFB; 2555 } 2556 2557 /* 2558 * Shamelessly leak the physical address to user-space. As 2559 * page_to_phys() is undefined for I/O memory, warn in this 2560 * case. 2561 */ 2562 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 2563 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0 && 2564 !drm_WARN_ON_ONCE(dev, map.is_iomem)) 2565 fbi->fix.smem_start = 2566 page_to_phys(virt_to_page(fbi->screen_buffer)); 2567 #endif 2568 } 2569 2570 return 0; 2571 } 2572 2573 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { 2574 .fb_probe = drm_fb_helper_generic_probe, 2575 }; 2576 2577 static void drm_fbdev_client_unregister(struct drm_client_dev *client) 2578 { 2579 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 2580 2581 if (fb_helper->fbdev) 2582 /* drm_fbdev_fb_destroy() takes care of cleanup */ 2583 drm_fb_helper_unregister_fbi(fb_helper); 2584 else 2585 drm_fbdev_release(fb_helper); 2586 } 2587 2588 static int drm_fbdev_client_restore(struct drm_client_dev *client) 2589 { 2590 drm_fb_helper_lastclose(client->dev); 2591 2592 return 0; 2593 } 2594 2595 static int drm_fbdev_client_hotplug(struct drm_client_dev *client) 2596 { 2597 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 2598 struct drm_device *dev = client->dev; 2599 int ret; 2600 2601 /* Setup is not retried if it has failed */ 2602 if (!fb_helper->dev && fb_helper->funcs) 2603 return 0; 2604 2605 if (dev->fb_helper) 2606 return drm_fb_helper_hotplug_event(dev->fb_helper); 2607 2608 if (!dev->mode_config.num_connector) { 2609 drm_dbg_kms(dev, "No connectors found, will not create framebuffer!\n"); 2610 return 0; 2611 } 2612 2613 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs); 2614 2615 ret = drm_fb_helper_init(dev, fb_helper); 2616 if (ret) 2617 goto err; 2618 2619 if (!drm_drv_uses_atomic_modeset(dev)) 2620 drm_helper_disable_unused_functions(dev); 2621 2622 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp); 2623 if (ret) 2624 goto err_cleanup; 2625 2626 return 0; 2627 2628 err_cleanup: 2629 drm_fbdev_cleanup(fb_helper); 2630 err: 2631 fb_helper->dev = NULL; 2632 fb_helper->fbdev = NULL; 2633 2634 drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret); 2635 2636 return ret; 2637 } 2638 2639 static const struct drm_client_funcs drm_fbdev_client_funcs = { 2640 .owner = THIS_MODULE, 2641 .unregister = drm_fbdev_client_unregister, 2642 .restore = drm_fbdev_client_restore, 2643 .hotplug = drm_fbdev_client_hotplug, 2644 }; 2645 2646 /** 2647 * drm_fbdev_generic_setup() - Setup generic fbdev emulation 2648 * @dev: DRM device 2649 * @preferred_bpp: Preferred bits per pixel for the device. 2650 * @dev->mode_config.preferred_depth is used if this is zero. 2651 * 2652 * This function sets up generic fbdev emulation for drivers that supports 2653 * dumb buffers with a virtual address and that can be mmap'ed. 2654 * drm_fbdev_generic_setup() shall be called after the DRM driver registered 2655 * the new DRM device with drm_dev_register(). 2656 * 2657 * Restore, hotplug events and teardown are all taken care of. Drivers that do 2658 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. 2659 * Simple drivers might use drm_mode_config_helper_suspend(). 2660 * 2661 * Drivers that set the dirty callback on their framebuffer will get a shadow 2662 * fbdev buffer that is blitted onto the real buffer. This is done in order to 2663 * make deferred I/O work with all kinds of buffers. A shadow buffer can be 2664 * requested explicitly by setting struct drm_mode_config.prefer_shadow or 2665 * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is 2666 * required to use generic fbdev emulation with SHMEM helpers. 2667 * 2668 * This function is safe to call even when there are no connectors present. 2669 * Setup will be retried on the next hotplug event. 2670 * 2671 * The fbdev is destroyed by drm_dev_unregister(). 2672 */ 2673 void drm_fbdev_generic_setup(struct drm_device *dev, 2674 unsigned int preferred_bpp) 2675 { 2676 struct drm_fb_helper *fb_helper; 2677 int ret; 2678 2679 drm_WARN(dev, !dev->registered, "Device has not been registered.\n"); 2680 drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n"); 2681 2682 if (!drm_fbdev_emulation) 2683 return; 2684 2685 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 2686 if (!fb_helper) { 2687 drm_err(dev, "Failed to allocate fb_helper\n"); 2688 return; 2689 } 2690 2691 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); 2692 if (ret) { 2693 kfree(fb_helper); 2694 drm_err(dev, "Failed to register client: %d\n", ret); 2695 return; 2696 } 2697 2698 /* 2699 * FIXME: This mixes up depth with bpp, which results in a glorious 2700 * mess, resulting in some drivers picking wrong fbdev defaults and 2701 * others wrong preferred_depth defaults. 2702 */ 2703 if (!preferred_bpp) 2704 preferred_bpp = dev->mode_config.preferred_depth; 2705 if (!preferred_bpp) 2706 preferred_bpp = 32; 2707 fb_helper->preferred_bpp = preferred_bpp; 2708 2709 ret = drm_fbdev_client_hotplug(&fb_helper->client); 2710 if (ret) 2711 drm_dbg_kms(dev, "client hotplug ret=%d\n", ret); 2712 2713 drm_client_register(&fb_helper->client); 2714 } 2715 EXPORT_SYMBOL(drm_fbdev_generic_setup); 2716