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/sysrq.h> 36 #include <linux/slab.h> 37 #include <linux/module.h> 38 #include <drm/drmP.h> 39 #include <drm/drm_crtc.h> 40 #include <drm/drm_fb_helper.h> 41 #include <drm/drm_crtc_helper.h> 42 #include <drm/drm_atomic.h> 43 #include <drm/drm_atomic_helper.h> 44 45 #include "drm_crtc_internal.h" 46 #include "drm_crtc_helper_internal.h" 47 48 static bool drm_fbdev_emulation = true; 49 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); 50 MODULE_PARM_DESC(fbdev_emulation, 51 "Enable legacy fbdev emulation [default=true]"); 52 53 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; 54 module_param(drm_fbdev_overalloc, int, 0444); 55 MODULE_PARM_DESC(drm_fbdev_overalloc, 56 "Overallocation of the fbdev buffer (%) [default=" 57 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); 58 59 /* 60 * In order to keep user-space compatibility, we want in certain use-cases 61 * to keep leaking the fbdev physical address to the user-space program 62 * handling the fbdev buffer. 63 * This is a bad habit essentially kept into closed source opengl driver 64 * that should really be moved into open-source upstream projects instead 65 * of using legacy physical addresses in user space to communicate with 66 * other out-of-tree kernel modules. 67 * 68 * This module_param *should* be removed as soon as possible and be 69 * considered as a broken and legacy behaviour from a modern fbdev device. 70 */ 71 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 72 static bool drm_leak_fbdev_smem = false; 73 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600); 74 MODULE_PARM_DESC(drm_leak_fbdev_smem, 75 "Allow unsafe leaking fbdev physical smem address [default=false]"); 76 #endif 77 78 static LIST_HEAD(kernel_fb_helper_list); 79 static DEFINE_MUTEX(kernel_fb_helper_lock); 80 81 /** 82 * DOC: fbdev helpers 83 * 84 * The fb helper functions are useful to provide an fbdev on top of a drm kernel 85 * mode setting driver. They can be used mostly independently from the crtc 86 * helper functions used by many drivers to implement the kernel mode setting 87 * interfaces. 88 * 89 * Drivers that support a dumb buffer with a virtual address and mmap support, 90 * should try out the generic fbdev emulation using drm_fbdev_generic_setup(). 91 * 92 * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it 93 * down by calling drm_fb_helper_fbdev_teardown(). 94 * 95 * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use 96 * the setup helper and will need to do the whole four-step setup process with 97 * drm_fb_helper_prepare(), drm_fb_helper_init(), 98 * drm_fb_helper_single_add_all_connectors(), enable hotplugging and 99 * drm_fb_helper_initial_config() to avoid a possible race window. 100 * 101 * At runtime drivers should restore the fbdev console by using 102 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback. 103 * They should also notify the fb helper code from updates to the output 104 * configuration by using drm_fb_helper_output_poll_changed() as their 105 * &drm_mode_config_funcs.output_poll_changed callback. 106 * 107 * For suspend/resume consider using drm_mode_config_helper_suspend() and 108 * drm_mode_config_helper_resume() which takes care of fbdev as well. 109 * 110 * All other functions exported by the fb helper library can be used to 111 * implement the fbdev driver interface by the driver. 112 * 113 * It is possible, though perhaps somewhat tricky, to implement race-free 114 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() 115 * helper must be called first to initialize the minimum required to make 116 * hotplug detection work. Drivers also need to make sure to properly set up 117 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() 118 * it is safe to enable interrupts and start processing hotplug events. At the 119 * same time, drivers should initialize all modeset objects such as CRTCs, 120 * encoders and connectors. To finish up the fbdev helper initialization, the 121 * drm_fb_helper_init() function is called. To probe for all attached displays 122 * and set up an initial configuration using the detected hardware, drivers 123 * should call drm_fb_helper_single_add_all_connectors() followed by 124 * drm_fb_helper_initial_config(). 125 * 126 * If &drm_framebuffer_funcs.dirty is set, the 127 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will 128 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right 129 * away. This worker then calls the dirty() function ensuring that it will 130 * always run in process context since the fb_*() function could be running in 131 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io 132 * callback it will also schedule dirty_work with the damage collected from the 133 * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup 134 * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()). 135 */ 136 137 #define drm_fb_helper_for_each_connector(fbh, i__) \ 138 for (({ lockdep_assert_held(&(fbh)->lock); }), \ 139 i__ = 0; i__ < (fbh)->connector_count; i__++) 140 141 static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, 142 struct drm_connector *connector) 143 { 144 struct drm_fb_helper_connector *fb_conn; 145 struct drm_fb_helper_connector **temp; 146 unsigned int count; 147 148 if (!drm_fbdev_emulation) 149 return 0; 150 151 lockdep_assert_held(&fb_helper->lock); 152 153 count = fb_helper->connector_count + 1; 154 155 if (count > fb_helper->connector_info_alloc_count) { 156 size_t size = count * sizeof(fb_conn); 157 158 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL); 159 if (!temp) 160 return -ENOMEM; 161 162 fb_helper->connector_info_alloc_count = count; 163 fb_helper->connector_info = temp; 164 } 165 166 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL); 167 if (!fb_conn) 168 return -ENOMEM; 169 170 drm_connector_get(connector); 171 fb_conn->connector = connector; 172 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn; 173 174 return 0; 175 } 176 177 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, 178 struct drm_connector *connector) 179 { 180 int err; 181 182 if (!fb_helper) 183 return 0; 184 185 mutex_lock(&fb_helper->lock); 186 err = __drm_fb_helper_add_one_connector(fb_helper, connector); 187 mutex_unlock(&fb_helper->lock); 188 189 return err; 190 } 191 EXPORT_SYMBOL(drm_fb_helper_add_one_connector); 192 193 /** 194 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev 195 * emulation helper 196 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL 197 * 198 * This functions adds all the available connectors for use with the given 199 * fb_helper. This is a separate step to allow drivers to freely assign 200 * connectors to the fbdev, e.g. if some are reserved for special purposes or 201 * not adequate to be used for the fbcon. 202 * 203 * This function is protected against concurrent connector hotadds/removals 204 * using drm_fb_helper_add_one_connector() and 205 * drm_fb_helper_remove_one_connector(). 206 */ 207 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) 208 { 209 struct drm_device *dev; 210 struct drm_connector *connector; 211 struct drm_connector_list_iter conn_iter; 212 int i, ret = 0; 213 214 if (!drm_fbdev_emulation || !fb_helper) 215 return 0; 216 217 dev = fb_helper->dev; 218 219 mutex_lock(&fb_helper->lock); 220 drm_connector_list_iter_begin(dev, &conn_iter); 221 drm_for_each_connector_iter(connector, &conn_iter) { 222 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 223 continue; 224 225 ret = __drm_fb_helper_add_one_connector(fb_helper, connector); 226 if (ret) 227 goto fail; 228 } 229 goto out; 230 231 fail: 232 drm_fb_helper_for_each_connector(fb_helper, i) { 233 struct drm_fb_helper_connector *fb_helper_connector = 234 fb_helper->connector_info[i]; 235 236 drm_connector_put(fb_helper_connector->connector); 237 238 kfree(fb_helper_connector); 239 fb_helper->connector_info[i] = NULL; 240 } 241 fb_helper->connector_count = 0; 242 out: 243 drm_connector_list_iter_end(&conn_iter); 244 mutex_unlock(&fb_helper->lock); 245 246 return ret; 247 } 248 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); 249 250 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 251 struct drm_connector *connector) 252 { 253 struct drm_fb_helper_connector *fb_helper_connector; 254 int i, j; 255 256 if (!drm_fbdev_emulation) 257 return 0; 258 259 lockdep_assert_held(&fb_helper->lock); 260 261 drm_fb_helper_for_each_connector(fb_helper, i) { 262 if (fb_helper->connector_info[i]->connector == connector) 263 break; 264 } 265 266 if (i == fb_helper->connector_count) 267 return -EINVAL; 268 fb_helper_connector = fb_helper->connector_info[i]; 269 drm_connector_put(fb_helper_connector->connector); 270 271 for (j = i + 1; j < fb_helper->connector_count; j++) 272 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; 273 274 fb_helper->connector_count--; 275 kfree(fb_helper_connector); 276 277 return 0; 278 } 279 280 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 281 struct drm_connector *connector) 282 { 283 int err; 284 285 if (!fb_helper) 286 return 0; 287 288 mutex_lock(&fb_helper->lock); 289 err = __drm_fb_helper_remove_one_connector(fb_helper, connector); 290 mutex_unlock(&fb_helper->lock); 291 292 return err; 293 } 294 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); 295 296 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) 297 { 298 uint16_t *r_base, *g_base, *b_base; 299 300 if (crtc->funcs->gamma_set == NULL) 301 return; 302 303 r_base = crtc->gamma_store; 304 g_base = r_base + crtc->gamma_size; 305 b_base = g_base + crtc->gamma_size; 306 307 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 308 crtc->gamma_size, NULL); 309 } 310 311 /** 312 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter 313 * @info: fbdev registered by the helper 314 */ 315 int drm_fb_helper_debug_enter(struct fb_info *info) 316 { 317 struct drm_fb_helper *helper = info->par; 318 const struct drm_crtc_helper_funcs *funcs; 319 int i; 320 321 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 322 for (i = 0; i < helper->crtc_count; i++) { 323 struct drm_mode_set *mode_set = 324 &helper->crtc_info[i].mode_set; 325 326 if (!mode_set->crtc->enabled) 327 continue; 328 329 funcs = mode_set->crtc->helper_private; 330 if (funcs->mode_set_base_atomic == NULL) 331 continue; 332 333 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) 334 continue; 335 336 funcs->mode_set_base_atomic(mode_set->crtc, 337 mode_set->fb, 338 mode_set->x, 339 mode_set->y, 340 ENTER_ATOMIC_MODE_SET); 341 } 342 } 343 344 return 0; 345 } 346 EXPORT_SYMBOL(drm_fb_helper_debug_enter); 347 348 /** 349 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave 350 * @info: fbdev registered by the helper 351 */ 352 int drm_fb_helper_debug_leave(struct fb_info *info) 353 { 354 struct drm_fb_helper *helper = info->par; 355 struct drm_crtc *crtc; 356 const struct drm_crtc_helper_funcs *funcs; 357 struct drm_framebuffer *fb; 358 int i; 359 360 for (i = 0; i < helper->crtc_count; i++) { 361 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; 362 363 crtc = mode_set->crtc; 364 if (drm_drv_uses_atomic_modeset(crtc->dev)) 365 continue; 366 367 funcs = crtc->helper_private; 368 fb = crtc->primary->fb; 369 370 if (!crtc->enabled) 371 continue; 372 373 if (!fb) { 374 DRM_ERROR("no fb to restore??\n"); 375 continue; 376 } 377 378 if (funcs->mode_set_base_atomic == NULL) 379 continue; 380 381 drm_fb_helper_restore_lut_atomic(mode_set->crtc); 382 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, 383 crtc->y, LEAVE_ATOMIC_MODE_SET); 384 } 385 386 return 0; 387 } 388 EXPORT_SYMBOL(drm_fb_helper_debug_leave); 389 390 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active) 391 { 392 struct drm_device *dev = fb_helper->dev; 393 struct drm_plane_state *plane_state; 394 struct drm_plane *plane; 395 struct drm_atomic_state *state; 396 int i, ret; 397 struct drm_modeset_acquire_ctx ctx; 398 399 drm_modeset_acquire_init(&ctx, 0); 400 401 state = drm_atomic_state_alloc(dev); 402 if (!state) { 403 ret = -ENOMEM; 404 goto out_ctx; 405 } 406 407 state->acquire_ctx = &ctx; 408 retry: 409 drm_for_each_plane(plane, dev) { 410 plane_state = drm_atomic_get_plane_state(state, plane); 411 if (IS_ERR(plane_state)) { 412 ret = PTR_ERR(plane_state); 413 goto out_state; 414 } 415 416 plane_state->rotation = DRM_MODE_ROTATE_0; 417 418 /* disable non-primary: */ 419 if (plane->type == DRM_PLANE_TYPE_PRIMARY) 420 continue; 421 422 ret = __drm_atomic_helper_disable_plane(plane, plane_state); 423 if (ret != 0) 424 goto out_state; 425 } 426 427 for (i = 0; i < fb_helper->crtc_count; i++) { 428 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 429 struct drm_plane *primary = mode_set->crtc->primary; 430 431 /* Cannot fail as we've already gotten the plane state above */ 432 plane_state = drm_atomic_get_new_plane_state(state, primary); 433 plane_state->rotation = fb_helper->crtc_info[i].rotation; 434 435 ret = __drm_atomic_helper_set_config(mode_set, state); 436 if (ret != 0) 437 goto out_state; 438 439 /* 440 * __drm_atomic_helper_set_config() sets active when a 441 * mode is set, unconditionally clear it if we force DPMS off 442 */ 443 if (!active) { 444 struct drm_crtc *crtc = mode_set->crtc; 445 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc); 446 447 crtc_state->active = false; 448 } 449 } 450 451 ret = drm_atomic_commit(state); 452 453 out_state: 454 if (ret == -EDEADLK) 455 goto backoff; 456 457 drm_atomic_state_put(state); 458 out_ctx: 459 drm_modeset_drop_locks(&ctx); 460 drm_modeset_acquire_fini(&ctx); 461 462 return ret; 463 464 backoff: 465 drm_atomic_state_clear(state); 466 drm_modeset_backoff(&ctx); 467 468 goto retry; 469 } 470 471 static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper) 472 { 473 struct drm_device *dev = fb_helper->dev; 474 struct drm_plane *plane; 475 int i, ret = 0; 476 477 drm_modeset_lock_all(fb_helper->dev); 478 drm_for_each_plane(plane, dev) { 479 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 480 drm_plane_force_disable(plane); 481 482 if (plane->rotation_property) 483 drm_mode_plane_set_obj_prop(plane, 484 plane->rotation_property, 485 DRM_MODE_ROTATE_0); 486 } 487 488 for (i = 0; i < fb_helper->crtc_count; i++) { 489 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 490 struct drm_crtc *crtc = mode_set->crtc; 491 492 if (crtc->funcs->cursor_set2) { 493 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0); 494 if (ret) 495 goto out; 496 } else if (crtc->funcs->cursor_set) { 497 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); 498 if (ret) 499 goto out; 500 } 501 502 ret = drm_mode_set_config_internal(mode_set); 503 if (ret) 504 goto out; 505 } 506 out: 507 drm_modeset_unlock_all(fb_helper->dev); 508 509 return ret; 510 } 511 512 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper) 513 { 514 struct drm_device *dev = fb_helper->dev; 515 516 if (drm_drv_uses_atomic_modeset(dev)) 517 return restore_fbdev_mode_atomic(fb_helper, true); 518 else 519 return restore_fbdev_mode_legacy(fb_helper); 520 } 521 522 /** 523 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration 524 * @fb_helper: driver-allocated fbdev helper, can be NULL 525 * 526 * This should be called from driver's drm &drm_driver.lastclose callback 527 * when implementing an fbcon on top of kms using this helper. This ensures that 528 * the user isn't greeted with a black screen when e.g. X dies. 529 * 530 * RETURNS: 531 * Zero if everything went ok, negative error code otherwise. 532 */ 533 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 534 { 535 bool do_delayed; 536 int ret; 537 538 if (!drm_fbdev_emulation || !fb_helper) 539 return -ENODEV; 540 541 if (READ_ONCE(fb_helper->deferred_setup)) 542 return 0; 543 544 mutex_lock(&fb_helper->lock); 545 ret = restore_fbdev_mode(fb_helper); 546 547 do_delayed = fb_helper->delayed_hotplug; 548 if (do_delayed) 549 fb_helper->delayed_hotplug = false; 550 mutex_unlock(&fb_helper->lock); 551 552 if (do_delayed) 553 drm_fb_helper_hotplug_event(fb_helper); 554 555 return ret; 556 } 557 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); 558 559 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) 560 { 561 struct drm_device *dev = fb_helper->dev; 562 struct drm_crtc *crtc; 563 int bound = 0, crtcs_bound = 0; 564 565 /* 566 * Sometimes user space wants everything disabled, so don't steal the 567 * display if there's a master. 568 */ 569 if (READ_ONCE(dev->master)) 570 return false; 571 572 drm_for_each_crtc(crtc, dev) { 573 drm_modeset_lock(&crtc->mutex, NULL); 574 if (crtc->primary->fb) 575 crtcs_bound++; 576 if (crtc->primary->fb == fb_helper->fb) 577 bound++; 578 drm_modeset_unlock(&crtc->mutex); 579 } 580 581 if (bound < crtcs_bound) 582 return false; 583 584 return true; 585 } 586 587 #ifdef CONFIG_MAGIC_SYSRQ 588 /* 589 * restore fbcon display for all kms driver's using this helper, used for sysrq 590 * and panic handling. 591 */ 592 static bool drm_fb_helper_force_kernel_mode(void) 593 { 594 bool ret, error = false; 595 struct drm_fb_helper *helper; 596 597 if (list_empty(&kernel_fb_helper_list)) 598 return false; 599 600 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 601 struct drm_device *dev = helper->dev; 602 603 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 604 continue; 605 606 mutex_lock(&helper->lock); 607 ret = restore_fbdev_mode(helper); 608 if (ret) 609 error = true; 610 mutex_unlock(&helper->lock); 611 } 612 return error; 613 } 614 615 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) 616 { 617 bool ret; 618 619 ret = drm_fb_helper_force_kernel_mode(); 620 if (ret == true) 621 DRM_ERROR("Failed to restore crtc configuration\n"); 622 } 623 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); 624 625 static void drm_fb_helper_sysrq(int dummy1) 626 { 627 schedule_work(&drm_fb_helper_restore_work); 628 } 629 630 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { 631 .handler = drm_fb_helper_sysrq, 632 .help_msg = "force-fb(V)", 633 .action_msg = "Restore framebuffer console", 634 }; 635 #else 636 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; 637 #endif 638 639 static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode) 640 { 641 struct drm_device *dev = fb_helper->dev; 642 struct drm_crtc *crtc; 643 struct drm_connector *connector; 644 int i, j; 645 646 drm_modeset_lock_all(dev); 647 for (i = 0; i < fb_helper->crtc_count; i++) { 648 crtc = fb_helper->crtc_info[i].mode_set.crtc; 649 650 if (!crtc->enabled) 651 continue; 652 653 /* Walk the connectors & encoders on this fb turning them on/off */ 654 drm_fb_helper_for_each_connector(fb_helper, j) { 655 connector = fb_helper->connector_info[j]->connector; 656 connector->funcs->dpms(connector, dpms_mode); 657 drm_object_property_set_value(&connector->base, 658 dev->mode_config.dpms_property, dpms_mode); 659 } 660 } 661 drm_modeset_unlock_all(dev); 662 } 663 664 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) 665 { 666 struct drm_fb_helper *fb_helper = info->par; 667 668 /* 669 * For each CRTC in this fb, turn the connectors on/off. 670 */ 671 mutex_lock(&fb_helper->lock); 672 if (!drm_fb_helper_is_bound(fb_helper)) { 673 mutex_unlock(&fb_helper->lock); 674 return; 675 } 676 677 if (drm_drv_uses_atomic_modeset(fb_helper->dev)) 678 restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON); 679 else 680 dpms_legacy(fb_helper, dpms_mode); 681 mutex_unlock(&fb_helper->lock); 682 } 683 684 /** 685 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank 686 * @blank: desired blanking state 687 * @info: fbdev registered by the helper 688 */ 689 int drm_fb_helper_blank(int blank, struct fb_info *info) 690 { 691 if (oops_in_progress) 692 return -EBUSY; 693 694 switch (blank) { 695 /* Display: On; HSync: On, VSync: On */ 696 case FB_BLANK_UNBLANK: 697 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); 698 break; 699 /* Display: Off; HSync: On, VSync: On */ 700 case FB_BLANK_NORMAL: 701 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 702 break; 703 /* Display: Off; HSync: Off, VSync: On */ 704 case FB_BLANK_HSYNC_SUSPEND: 705 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 706 break; 707 /* Display: Off; HSync: On, VSync: Off */ 708 case FB_BLANK_VSYNC_SUSPEND: 709 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); 710 break; 711 /* Display: Off; HSync: Off, VSync: Off */ 712 case FB_BLANK_POWERDOWN: 713 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); 714 break; 715 } 716 return 0; 717 } 718 EXPORT_SYMBOL(drm_fb_helper_blank); 719 720 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper, 721 struct drm_mode_set *modeset) 722 { 723 int i; 724 725 for (i = 0; i < modeset->num_connectors; i++) { 726 drm_connector_put(modeset->connectors[i]); 727 modeset->connectors[i] = NULL; 728 } 729 modeset->num_connectors = 0; 730 731 drm_mode_destroy(helper->dev, modeset->mode); 732 modeset->mode = NULL; 733 734 /* FIXME should hold a ref? */ 735 modeset->fb = NULL; 736 } 737 738 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) 739 { 740 int i; 741 742 for (i = 0; i < helper->connector_count; i++) { 743 drm_connector_put(helper->connector_info[i]->connector); 744 kfree(helper->connector_info[i]); 745 } 746 kfree(helper->connector_info); 747 748 for (i = 0; i < helper->crtc_count; i++) { 749 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set; 750 751 drm_fb_helper_modeset_release(helper, modeset); 752 kfree(modeset->connectors); 753 } 754 kfree(helper->crtc_info); 755 } 756 757 static void drm_fb_helper_resume_worker(struct work_struct *work) 758 { 759 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 760 resume_work); 761 762 console_lock(); 763 fb_set_suspend(helper->fbdev, 0); 764 console_unlock(); 765 } 766 767 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper, 768 struct drm_clip_rect *clip) 769 { 770 struct drm_framebuffer *fb = fb_helper->fb; 771 unsigned int cpp = drm_format_plane_cpp(fb->format->format, 0); 772 size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp; 773 void *src = fb_helper->fbdev->screen_buffer + offset; 774 void *dst = fb_helper->buffer->vaddr + offset; 775 size_t len = (clip->x2 - clip->x1) * cpp; 776 unsigned int y; 777 778 for (y = clip->y1; y < clip->y2; y++) { 779 memcpy(dst, src, len); 780 src += fb->pitches[0]; 781 dst += fb->pitches[0]; 782 } 783 } 784 785 static void drm_fb_helper_dirty_work(struct work_struct *work) 786 { 787 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 788 dirty_work); 789 struct drm_clip_rect *clip = &helper->dirty_clip; 790 struct drm_clip_rect clip_copy; 791 unsigned long flags; 792 793 spin_lock_irqsave(&helper->dirty_lock, flags); 794 clip_copy = *clip; 795 clip->x1 = clip->y1 = ~0; 796 clip->x2 = clip->y2 = 0; 797 spin_unlock_irqrestore(&helper->dirty_lock, flags); 798 799 /* call dirty callback only when it has been really touched */ 800 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) { 801 /* Generic fbdev uses a shadow buffer */ 802 if (helper->buffer) 803 drm_fb_helper_dirty_blit_real(helper, &clip_copy); 804 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1); 805 } 806 } 807 808 /** 809 * drm_fb_helper_prepare - setup a drm_fb_helper structure 810 * @dev: DRM device 811 * @helper: driver-allocated fbdev helper structure to set up 812 * @funcs: pointer to structure of functions associate with this helper 813 * 814 * Sets up the bare minimum to make the framebuffer helper usable. This is 815 * useful to implement race-free initialization of the polling helpers. 816 */ 817 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 818 const struct drm_fb_helper_funcs *funcs) 819 { 820 INIT_LIST_HEAD(&helper->kernel_fb_list); 821 spin_lock_init(&helper->dirty_lock); 822 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); 823 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); 824 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; 825 mutex_init(&helper->lock); 826 helper->funcs = funcs; 827 helper->dev = dev; 828 } 829 EXPORT_SYMBOL(drm_fb_helper_prepare); 830 831 /** 832 * drm_fb_helper_init - initialize a &struct drm_fb_helper 833 * @dev: drm device 834 * @fb_helper: driver-allocated fbdev helper structure to initialize 835 * @max_conn_count: max connector count 836 * 837 * This allocates the structures for the fbdev helper with the given limits. 838 * Note that this won't yet touch the hardware (through the driver interfaces) 839 * nor register the fbdev. This is only done in drm_fb_helper_initial_config() 840 * to allow driver writes more control over the exact init sequence. 841 * 842 * Drivers must call drm_fb_helper_prepare() before calling this function. 843 * 844 * RETURNS: 845 * Zero if everything went ok, nonzero otherwise. 846 */ 847 int drm_fb_helper_init(struct drm_device *dev, 848 struct drm_fb_helper *fb_helper, 849 int max_conn_count) 850 { 851 struct drm_crtc *crtc; 852 struct drm_mode_config *config = &dev->mode_config; 853 int i; 854 855 if (!drm_fbdev_emulation) { 856 dev->fb_helper = fb_helper; 857 return 0; 858 } 859 860 if (!max_conn_count) 861 return -EINVAL; 862 863 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); 864 if (!fb_helper->crtc_info) 865 return -ENOMEM; 866 867 fb_helper->crtc_count = config->num_crtc; 868 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); 869 if (!fb_helper->connector_info) { 870 kfree(fb_helper->crtc_info); 871 return -ENOMEM; 872 } 873 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; 874 fb_helper->connector_count = 0; 875 876 for (i = 0; i < fb_helper->crtc_count; i++) { 877 fb_helper->crtc_info[i].mode_set.connectors = 878 kcalloc(max_conn_count, 879 sizeof(struct drm_connector *), 880 GFP_KERNEL); 881 882 if (!fb_helper->crtc_info[i].mode_set.connectors) 883 goto out_free; 884 fb_helper->crtc_info[i].mode_set.num_connectors = 0; 885 fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0; 886 } 887 888 i = 0; 889 drm_for_each_crtc(crtc, dev) { 890 fb_helper->crtc_info[i].mode_set.crtc = crtc; 891 i++; 892 } 893 894 dev->fb_helper = fb_helper; 895 896 return 0; 897 out_free: 898 drm_fb_helper_crtc_free(fb_helper); 899 return -ENOMEM; 900 } 901 EXPORT_SYMBOL(drm_fb_helper_init); 902 903 /** 904 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members 905 * @fb_helper: driver-allocated fbdev helper 906 * 907 * A helper to alloc fb_info and the members cmap and apertures. Called 908 * by the driver within the fb_probe fb_helper callback function. Drivers do not 909 * need to release the allocated fb_info structure themselves, this is 910 * automatically done when calling drm_fb_helper_fini(). 911 * 912 * RETURNS: 913 * fb_info pointer if things went okay, pointer containing error code 914 * otherwise 915 */ 916 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) 917 { 918 struct device *dev = fb_helper->dev->dev; 919 struct fb_info *info; 920 int ret; 921 922 info = framebuffer_alloc(0, dev); 923 if (!info) 924 return ERR_PTR(-ENOMEM); 925 926 ret = fb_alloc_cmap(&info->cmap, 256, 0); 927 if (ret) 928 goto err_release; 929 930 info->apertures = alloc_apertures(1); 931 if (!info->apertures) { 932 ret = -ENOMEM; 933 goto err_free_cmap; 934 } 935 936 fb_helper->fbdev = info; 937 938 return info; 939 940 err_free_cmap: 941 fb_dealloc_cmap(&info->cmap); 942 err_release: 943 framebuffer_release(info); 944 return ERR_PTR(ret); 945 } 946 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); 947 948 /** 949 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device 950 * @fb_helper: driver-allocated fbdev helper, can be NULL 951 * 952 * A wrapper around unregister_framebuffer, to release the fb_info 953 * framebuffer device. This must be called before releasing all resources for 954 * @fb_helper by calling drm_fb_helper_fini(). 955 */ 956 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 957 { 958 if (fb_helper && fb_helper->fbdev) 959 unregister_framebuffer(fb_helper->fbdev); 960 } 961 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); 962 963 /** 964 * drm_fb_helper_fini - finialize a &struct drm_fb_helper 965 * @fb_helper: driver-allocated fbdev helper, can be NULL 966 * 967 * This cleans up all remaining resources associated with @fb_helper. Must be 968 * called after drm_fb_helper_unlink_fbi() was called. 969 */ 970 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 971 { 972 struct fb_info *info; 973 974 if (!fb_helper) 975 return; 976 977 fb_helper->dev->fb_helper = NULL; 978 979 if (!drm_fbdev_emulation) 980 return; 981 982 cancel_work_sync(&fb_helper->resume_work); 983 cancel_work_sync(&fb_helper->dirty_work); 984 985 info = fb_helper->fbdev; 986 if (info) { 987 if (info->cmap.len) 988 fb_dealloc_cmap(&info->cmap); 989 framebuffer_release(info); 990 } 991 fb_helper->fbdev = NULL; 992 993 mutex_lock(&kernel_fb_helper_lock); 994 if (!list_empty(&fb_helper->kernel_fb_list)) { 995 list_del(&fb_helper->kernel_fb_list); 996 if (list_empty(&kernel_fb_helper_list)) 997 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 998 } 999 mutex_unlock(&kernel_fb_helper_lock); 1000 1001 mutex_destroy(&fb_helper->lock); 1002 drm_fb_helper_crtc_free(fb_helper); 1003 1004 } 1005 EXPORT_SYMBOL(drm_fb_helper_fini); 1006 1007 /** 1008 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer 1009 * @fb_helper: driver-allocated fbdev helper, can be NULL 1010 * 1011 * A wrapper around unlink_framebuffer implemented by fbdev core 1012 */ 1013 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) 1014 { 1015 if (fb_helper && fb_helper->fbdev) 1016 unlink_framebuffer(fb_helper->fbdev); 1017 } 1018 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); 1019 1020 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, 1021 u32 width, u32 height) 1022 { 1023 struct drm_fb_helper *helper = info->par; 1024 struct drm_clip_rect *clip = &helper->dirty_clip; 1025 unsigned long flags; 1026 1027 if (!helper->fb->funcs->dirty) 1028 return; 1029 1030 spin_lock_irqsave(&helper->dirty_lock, flags); 1031 clip->x1 = min_t(u32, clip->x1, x); 1032 clip->y1 = min_t(u32, clip->y1, y); 1033 clip->x2 = max_t(u32, clip->x2, x + width); 1034 clip->y2 = max_t(u32, clip->y2, y + height); 1035 spin_unlock_irqrestore(&helper->dirty_lock, flags); 1036 1037 schedule_work(&helper->dirty_work); 1038 } 1039 1040 /** 1041 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function 1042 * @info: fb_info struct pointer 1043 * @pagelist: list of dirty mmap framebuffer pages 1044 * 1045 * This function is used as the &fb_deferred_io.deferred_io 1046 * callback function for flushing the fbdev mmap writes. 1047 */ 1048 void drm_fb_helper_deferred_io(struct fb_info *info, 1049 struct list_head *pagelist) 1050 { 1051 unsigned long start, end, min, max; 1052 struct page *page; 1053 u32 y1, y2; 1054 1055 min = ULONG_MAX; 1056 max = 0; 1057 list_for_each_entry(page, pagelist, lru) { 1058 start = page->index << PAGE_SHIFT; 1059 end = start + PAGE_SIZE - 1; 1060 min = min(min, start); 1061 max = max(max, end); 1062 } 1063 1064 if (min < max) { 1065 y1 = min / info->fix.line_length; 1066 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), 1067 info->var.yres); 1068 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); 1069 } 1070 } 1071 EXPORT_SYMBOL(drm_fb_helper_deferred_io); 1072 1073 /** 1074 * drm_fb_helper_defio_init - fbdev deferred I/O initialization 1075 * @fb_helper: driver-allocated fbdev helper 1076 * 1077 * This function allocates &fb_deferred_io, sets callback to 1078 * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init(). 1079 * It should be called from the &drm_fb_helper_funcs->fb_probe callback. 1080 * drm_fb_helper_fbdev_teardown() cleans up deferred I/O. 1081 * 1082 * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done 1083 * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby 1084 * affect other instances of that &fb_ops. 1085 * 1086 * Returns: 1087 * 0 on success or a negative error code on failure. 1088 */ 1089 int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper) 1090 { 1091 struct fb_info *info = fb_helper->fbdev; 1092 struct fb_deferred_io *fbdefio; 1093 struct fb_ops *fbops; 1094 1095 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL); 1096 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); 1097 if (!fbdefio || !fbops) { 1098 kfree(fbdefio); 1099 kfree(fbops); 1100 return -ENOMEM; 1101 } 1102 1103 info->fbdefio = fbdefio; 1104 fbdefio->delay = msecs_to_jiffies(50); 1105 fbdefio->deferred_io = drm_fb_helper_deferred_io; 1106 1107 *fbops = *info->fbops; 1108 info->fbops = fbops; 1109 1110 fb_deferred_io_init(info); 1111 1112 return 0; 1113 } 1114 EXPORT_SYMBOL(drm_fb_helper_defio_init); 1115 1116 /** 1117 * drm_fb_helper_sys_read - wrapper around fb_sys_read 1118 * @info: fb_info struct pointer 1119 * @buf: userspace buffer to read from framebuffer memory 1120 * @count: number of bytes to read from framebuffer memory 1121 * @ppos: read offset within framebuffer memory 1122 * 1123 * A wrapper around fb_sys_read implemented by fbdev core 1124 */ 1125 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 1126 size_t count, loff_t *ppos) 1127 { 1128 return fb_sys_read(info, buf, count, ppos); 1129 } 1130 EXPORT_SYMBOL(drm_fb_helper_sys_read); 1131 1132 /** 1133 * drm_fb_helper_sys_write - wrapper around fb_sys_write 1134 * @info: fb_info struct pointer 1135 * @buf: userspace buffer to write to framebuffer memory 1136 * @count: number of bytes to write to framebuffer memory 1137 * @ppos: write offset within framebuffer memory 1138 * 1139 * A wrapper around fb_sys_write implemented by fbdev core 1140 */ 1141 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 1142 size_t count, loff_t *ppos) 1143 { 1144 ssize_t ret; 1145 1146 ret = fb_sys_write(info, buf, count, ppos); 1147 if (ret > 0) 1148 drm_fb_helper_dirty(info, 0, 0, info->var.xres, 1149 info->var.yres); 1150 1151 return ret; 1152 } 1153 EXPORT_SYMBOL(drm_fb_helper_sys_write); 1154 1155 /** 1156 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect 1157 * @info: fbdev registered by the helper 1158 * @rect: info about rectangle to fill 1159 * 1160 * A wrapper around sys_fillrect implemented by fbdev core 1161 */ 1162 void drm_fb_helper_sys_fillrect(struct fb_info *info, 1163 const struct fb_fillrect *rect) 1164 { 1165 sys_fillrect(info, rect); 1166 drm_fb_helper_dirty(info, rect->dx, rect->dy, 1167 rect->width, rect->height); 1168 } 1169 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); 1170 1171 /** 1172 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea 1173 * @info: fbdev registered by the helper 1174 * @area: info about area to copy 1175 * 1176 * A wrapper around sys_copyarea implemented by fbdev core 1177 */ 1178 void drm_fb_helper_sys_copyarea(struct fb_info *info, 1179 const struct fb_copyarea *area) 1180 { 1181 sys_copyarea(info, area); 1182 drm_fb_helper_dirty(info, area->dx, area->dy, 1183 area->width, area->height); 1184 } 1185 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); 1186 1187 /** 1188 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit 1189 * @info: fbdev registered by the helper 1190 * @image: info about image to blit 1191 * 1192 * A wrapper around sys_imageblit implemented by fbdev core 1193 */ 1194 void drm_fb_helper_sys_imageblit(struct fb_info *info, 1195 const struct fb_image *image) 1196 { 1197 sys_imageblit(info, image); 1198 drm_fb_helper_dirty(info, image->dx, image->dy, 1199 image->width, image->height); 1200 } 1201 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); 1202 1203 /** 1204 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect 1205 * @info: fbdev registered by the helper 1206 * @rect: info about rectangle to fill 1207 * 1208 * A wrapper around cfb_fillrect implemented by fbdev core 1209 */ 1210 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 1211 const struct fb_fillrect *rect) 1212 { 1213 cfb_fillrect(info, rect); 1214 drm_fb_helper_dirty(info, rect->dx, rect->dy, 1215 rect->width, rect->height); 1216 } 1217 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); 1218 1219 /** 1220 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea 1221 * @info: fbdev registered by the helper 1222 * @area: info about area to copy 1223 * 1224 * A wrapper around cfb_copyarea implemented by fbdev core 1225 */ 1226 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 1227 const struct fb_copyarea *area) 1228 { 1229 cfb_copyarea(info, area); 1230 drm_fb_helper_dirty(info, area->dx, area->dy, 1231 area->width, area->height); 1232 } 1233 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); 1234 1235 /** 1236 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit 1237 * @info: fbdev registered by the helper 1238 * @image: info about image to blit 1239 * 1240 * A wrapper around cfb_imageblit implemented by fbdev core 1241 */ 1242 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 1243 const struct fb_image *image) 1244 { 1245 cfb_imageblit(info, image); 1246 drm_fb_helper_dirty(info, image->dx, image->dy, 1247 image->width, image->height); 1248 } 1249 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); 1250 1251 /** 1252 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 1253 * @fb_helper: driver-allocated fbdev helper, can be NULL 1254 * @suspend: whether to suspend or resume 1255 * 1256 * A wrapper around fb_set_suspend implemented by fbdev core. 1257 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take 1258 * the lock yourself 1259 */ 1260 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) 1261 { 1262 if (fb_helper && fb_helper->fbdev) 1263 fb_set_suspend(fb_helper->fbdev, suspend); 1264 } 1265 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 1266 1267 /** 1268 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also 1269 * takes the console lock 1270 * @fb_helper: driver-allocated fbdev helper, can be NULL 1271 * @suspend: whether to suspend or resume 1272 * 1273 * A wrapper around fb_set_suspend() that takes the console lock. If the lock 1274 * isn't available on resume, a worker is tasked with waiting for the lock 1275 * to become available. The console lock can be pretty contented on resume 1276 * due to all the printk activity. 1277 * 1278 * This function can be called multiple times with the same state since 1279 * &fb_info.state is checked to see if fbdev is running or not before locking. 1280 * 1281 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. 1282 */ 1283 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 1284 bool suspend) 1285 { 1286 if (!fb_helper || !fb_helper->fbdev) 1287 return; 1288 1289 /* make sure there's no pending/ongoing resume */ 1290 flush_work(&fb_helper->resume_work); 1291 1292 if (suspend) { 1293 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) 1294 return; 1295 1296 console_lock(); 1297 1298 } else { 1299 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) 1300 return; 1301 1302 if (!console_trylock()) { 1303 schedule_work(&fb_helper->resume_work); 1304 return; 1305 } 1306 } 1307 1308 fb_set_suspend(fb_helper->fbdev, suspend); 1309 console_unlock(); 1310 } 1311 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); 1312 1313 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) 1314 { 1315 u32 *palette = (u32 *)info->pseudo_palette; 1316 int i; 1317 1318 if (cmap->start + cmap->len > 16) 1319 return -EINVAL; 1320 1321 for (i = 0; i < cmap->len; ++i) { 1322 u16 red = cmap->red[i]; 1323 u16 green = cmap->green[i]; 1324 u16 blue = cmap->blue[i]; 1325 u32 value; 1326 1327 red >>= 16 - info->var.red.length; 1328 green >>= 16 - info->var.green.length; 1329 blue >>= 16 - info->var.blue.length; 1330 value = (red << info->var.red.offset) | 1331 (green << info->var.green.offset) | 1332 (blue << info->var.blue.offset); 1333 if (info->var.transp.length > 0) { 1334 u32 mask = (1 << info->var.transp.length) - 1; 1335 1336 mask <<= info->var.transp.offset; 1337 value |= mask; 1338 } 1339 palette[cmap->start + i] = value; 1340 } 1341 1342 return 0; 1343 } 1344 1345 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) 1346 { 1347 struct drm_fb_helper *fb_helper = info->par; 1348 struct drm_crtc *crtc; 1349 u16 *r, *g, *b; 1350 int i, ret = 0; 1351 1352 drm_modeset_lock_all(fb_helper->dev); 1353 for (i = 0; i < fb_helper->crtc_count; i++) { 1354 crtc = fb_helper->crtc_info[i].mode_set.crtc; 1355 if (!crtc->funcs->gamma_set || !crtc->gamma_size) 1356 return -EINVAL; 1357 1358 if (cmap->start + cmap->len > crtc->gamma_size) 1359 return -EINVAL; 1360 1361 r = crtc->gamma_store; 1362 g = r + crtc->gamma_size; 1363 b = g + crtc->gamma_size; 1364 1365 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 1366 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 1367 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 1368 1369 ret = crtc->funcs->gamma_set(crtc, r, g, b, 1370 crtc->gamma_size, NULL); 1371 if (ret) 1372 return ret; 1373 } 1374 drm_modeset_unlock_all(fb_helper->dev); 1375 1376 return ret; 1377 } 1378 1379 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, 1380 struct fb_cmap *cmap) 1381 { 1382 struct drm_device *dev = crtc->dev; 1383 struct drm_property_blob *gamma_lut; 1384 struct drm_color_lut *lut; 1385 int size = crtc->gamma_size; 1386 int i; 1387 1388 if (!size || cmap->start + cmap->len > size) 1389 return ERR_PTR(-EINVAL); 1390 1391 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); 1392 if (IS_ERR(gamma_lut)) 1393 return gamma_lut; 1394 1395 lut = gamma_lut->data; 1396 if (cmap->start || cmap->len != size) { 1397 u16 *r = crtc->gamma_store; 1398 u16 *g = r + crtc->gamma_size; 1399 u16 *b = g + crtc->gamma_size; 1400 1401 for (i = 0; i < cmap->start; i++) { 1402 lut[i].red = r[i]; 1403 lut[i].green = g[i]; 1404 lut[i].blue = b[i]; 1405 } 1406 for (i = cmap->start + cmap->len; i < size; i++) { 1407 lut[i].red = r[i]; 1408 lut[i].green = g[i]; 1409 lut[i].blue = b[i]; 1410 } 1411 } 1412 1413 for (i = 0; i < cmap->len; i++) { 1414 lut[cmap->start + i].red = cmap->red[i]; 1415 lut[cmap->start + i].green = cmap->green[i]; 1416 lut[cmap->start + i].blue = cmap->blue[i]; 1417 } 1418 1419 return gamma_lut; 1420 } 1421 1422 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) 1423 { 1424 struct drm_fb_helper *fb_helper = info->par; 1425 struct drm_device *dev = fb_helper->dev; 1426 struct drm_property_blob *gamma_lut = NULL; 1427 struct drm_modeset_acquire_ctx ctx; 1428 struct drm_crtc_state *crtc_state; 1429 struct drm_atomic_state *state; 1430 struct drm_crtc *crtc; 1431 u16 *r, *g, *b; 1432 int i, ret = 0; 1433 bool replaced; 1434 1435 drm_modeset_acquire_init(&ctx, 0); 1436 1437 state = drm_atomic_state_alloc(dev); 1438 if (!state) { 1439 ret = -ENOMEM; 1440 goto out_ctx; 1441 } 1442 1443 state->acquire_ctx = &ctx; 1444 retry: 1445 for (i = 0; i < fb_helper->crtc_count; i++) { 1446 crtc = fb_helper->crtc_info[i].mode_set.crtc; 1447 1448 if (!gamma_lut) 1449 gamma_lut = setcmap_new_gamma_lut(crtc, cmap); 1450 if (IS_ERR(gamma_lut)) { 1451 ret = PTR_ERR(gamma_lut); 1452 gamma_lut = NULL; 1453 goto out_state; 1454 } 1455 1456 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1457 if (IS_ERR(crtc_state)) { 1458 ret = PTR_ERR(crtc_state); 1459 goto out_state; 1460 } 1461 1462 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, 1463 NULL); 1464 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); 1465 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, 1466 gamma_lut); 1467 crtc_state->color_mgmt_changed |= replaced; 1468 } 1469 1470 ret = drm_atomic_commit(state); 1471 if (ret) 1472 goto out_state; 1473 1474 for (i = 0; i < fb_helper->crtc_count; i++) { 1475 crtc = fb_helper->crtc_info[i].mode_set.crtc; 1476 1477 r = crtc->gamma_store; 1478 g = r + crtc->gamma_size; 1479 b = g + crtc->gamma_size; 1480 1481 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 1482 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 1483 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 1484 } 1485 1486 out_state: 1487 if (ret == -EDEADLK) 1488 goto backoff; 1489 1490 drm_property_blob_put(gamma_lut); 1491 drm_atomic_state_put(state); 1492 out_ctx: 1493 drm_modeset_drop_locks(&ctx); 1494 drm_modeset_acquire_fini(&ctx); 1495 1496 return ret; 1497 1498 backoff: 1499 drm_atomic_state_clear(state); 1500 drm_modeset_backoff(&ctx); 1501 goto retry; 1502 } 1503 1504 /** 1505 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap 1506 * @cmap: cmap to set 1507 * @info: fbdev registered by the helper 1508 */ 1509 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 1510 { 1511 struct drm_fb_helper *fb_helper = info->par; 1512 int ret; 1513 1514 if (oops_in_progress) 1515 return -EBUSY; 1516 1517 mutex_lock(&fb_helper->lock); 1518 1519 if (!drm_fb_helper_is_bound(fb_helper)) { 1520 ret = -EBUSY; 1521 goto out; 1522 } 1523 1524 if (info->fix.visual == FB_VISUAL_TRUECOLOR) 1525 ret = setcmap_pseudo_palette(cmap, info); 1526 else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) 1527 ret = setcmap_atomic(cmap, info); 1528 else 1529 ret = setcmap_legacy(cmap, info); 1530 1531 out: 1532 mutex_unlock(&fb_helper->lock); 1533 1534 return ret; 1535 } 1536 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1537 1538 /** 1539 * drm_fb_helper_ioctl - legacy ioctl implementation 1540 * @info: fbdev registered by the helper 1541 * @cmd: ioctl command 1542 * @arg: ioctl argument 1543 * 1544 * A helper to implement the standard fbdev ioctl. Only 1545 * FBIO_WAITFORVSYNC is implemented for now. 1546 */ 1547 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 1548 unsigned long arg) 1549 { 1550 struct drm_fb_helper *fb_helper = info->par; 1551 struct drm_mode_set *mode_set; 1552 struct drm_crtc *crtc; 1553 int ret = 0; 1554 1555 mutex_lock(&fb_helper->lock); 1556 if (!drm_fb_helper_is_bound(fb_helper)) { 1557 ret = -EBUSY; 1558 goto unlock; 1559 } 1560 1561 switch (cmd) { 1562 case FBIO_WAITFORVSYNC: 1563 /* 1564 * Only consider the first CRTC. 1565 * 1566 * This ioctl is supposed to take the CRTC number as 1567 * an argument, but in fbdev times, what that number 1568 * was supposed to be was quite unclear, different 1569 * drivers were passing that argument differently 1570 * (some by reference, some by value), and most of the 1571 * userspace applications were just hardcoding 0 as an 1572 * argument. 1573 * 1574 * The first CRTC should be the integrated panel on 1575 * most drivers, so this is the best choice we can 1576 * make. If we're not smart enough here, one should 1577 * just consider switch the userspace to KMS. 1578 */ 1579 mode_set = &fb_helper->crtc_info[0].mode_set; 1580 crtc = mode_set->crtc; 1581 1582 /* 1583 * Only wait for a vblank event if the CRTC is 1584 * enabled, otherwise just don't do anythintg, 1585 * not even report an error. 1586 */ 1587 ret = drm_crtc_vblank_get(crtc); 1588 if (!ret) { 1589 drm_crtc_wait_one_vblank(crtc); 1590 drm_crtc_vblank_put(crtc); 1591 } 1592 1593 ret = 0; 1594 goto unlock; 1595 default: 1596 ret = -ENOTTY; 1597 } 1598 1599 unlock: 1600 mutex_unlock(&fb_helper->lock); 1601 return ret; 1602 } 1603 EXPORT_SYMBOL(drm_fb_helper_ioctl); 1604 1605 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1, 1606 const struct fb_var_screeninfo *var_2) 1607 { 1608 return var_1->bits_per_pixel == var_2->bits_per_pixel && 1609 var_1->grayscale == var_2->grayscale && 1610 var_1->red.offset == var_2->red.offset && 1611 var_1->red.length == var_2->red.length && 1612 var_1->red.msb_right == var_2->red.msb_right && 1613 var_1->green.offset == var_2->green.offset && 1614 var_1->green.length == var_2->green.length && 1615 var_1->green.msb_right == var_2->green.msb_right && 1616 var_1->blue.offset == var_2->blue.offset && 1617 var_1->blue.length == var_2->blue.length && 1618 var_1->blue.msb_right == var_2->blue.msb_right && 1619 var_1->transp.offset == var_2->transp.offset && 1620 var_1->transp.length == var_2->transp.length && 1621 var_1->transp.msb_right == var_2->transp.msb_right; 1622 } 1623 1624 /** 1625 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var 1626 * @var: screeninfo to check 1627 * @info: fbdev registered by the helper 1628 */ 1629 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1630 struct fb_info *info) 1631 { 1632 struct drm_fb_helper *fb_helper = info->par; 1633 struct drm_framebuffer *fb = fb_helper->fb; 1634 1635 if (var->pixclock != 0 || in_dbg_master()) 1636 return -EINVAL; 1637 1638 if ((drm_format_info_block_width(fb->format, 0) > 1) || 1639 (drm_format_info_block_height(fb->format, 0) > 1)) 1640 return -EINVAL; 1641 1642 /* 1643 * Changes struct fb_var_screeninfo are currently not pushed back 1644 * to KMS, hence fail if different settings are requested. 1645 */ 1646 if (var->bits_per_pixel != fb->format->cpp[0] * 8 || 1647 var->xres > fb->width || var->yres > fb->height || 1648 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1649 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb " 1650 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1651 var->xres, var->yres, var->bits_per_pixel, 1652 var->xres_virtual, var->yres_virtual, 1653 fb->width, fb->height, fb->format->cpp[0] * 8); 1654 return -EINVAL; 1655 } 1656 1657 /* 1658 * drm fbdev emulation doesn't support changing the pixel format at all, 1659 * so reject all pixel format changing requests. 1660 */ 1661 if (!drm_fb_pixel_format_equal(var, &info->var)) { 1662 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n"); 1663 return -EINVAL; 1664 } 1665 1666 return 0; 1667 } 1668 EXPORT_SYMBOL(drm_fb_helper_check_var); 1669 1670 /** 1671 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par 1672 * @info: fbdev registered by the helper 1673 * 1674 * This will let fbcon do the mode init and is called at initialization time by 1675 * the fbdev core when registering the driver, and later on through the hotplug 1676 * callback. 1677 */ 1678 int drm_fb_helper_set_par(struct fb_info *info) 1679 { 1680 struct drm_fb_helper *fb_helper = info->par; 1681 struct fb_var_screeninfo *var = &info->var; 1682 1683 if (oops_in_progress) 1684 return -EBUSY; 1685 1686 if (var->pixclock != 0) { 1687 DRM_ERROR("PIXEL CLOCK SET\n"); 1688 return -EINVAL; 1689 } 1690 1691 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 1692 1693 return 0; 1694 } 1695 EXPORT_SYMBOL(drm_fb_helper_set_par); 1696 1697 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y) 1698 { 1699 int i; 1700 1701 for (i = 0; i < fb_helper->crtc_count; i++) { 1702 struct drm_mode_set *mode_set; 1703 1704 mode_set = &fb_helper->crtc_info[i].mode_set; 1705 1706 mode_set->x = x; 1707 mode_set->y = y; 1708 } 1709 } 1710 1711 static int pan_display_atomic(struct fb_var_screeninfo *var, 1712 struct fb_info *info) 1713 { 1714 struct drm_fb_helper *fb_helper = info->par; 1715 int ret; 1716 1717 pan_set(fb_helper, var->xoffset, var->yoffset); 1718 1719 ret = restore_fbdev_mode_atomic(fb_helper, true); 1720 if (!ret) { 1721 info->var.xoffset = var->xoffset; 1722 info->var.yoffset = var->yoffset; 1723 } else 1724 pan_set(fb_helper, info->var.xoffset, info->var.yoffset); 1725 1726 return ret; 1727 } 1728 1729 static int pan_display_legacy(struct fb_var_screeninfo *var, 1730 struct fb_info *info) 1731 { 1732 struct drm_fb_helper *fb_helper = info->par; 1733 struct drm_mode_set *modeset; 1734 int ret = 0; 1735 int i; 1736 1737 drm_modeset_lock_all(fb_helper->dev); 1738 for (i = 0; i < fb_helper->crtc_count; i++) { 1739 modeset = &fb_helper->crtc_info[i].mode_set; 1740 1741 modeset->x = var->xoffset; 1742 modeset->y = var->yoffset; 1743 1744 if (modeset->num_connectors) { 1745 ret = drm_mode_set_config_internal(modeset); 1746 if (!ret) { 1747 info->var.xoffset = var->xoffset; 1748 info->var.yoffset = var->yoffset; 1749 } 1750 } 1751 } 1752 drm_modeset_unlock_all(fb_helper->dev); 1753 1754 return ret; 1755 } 1756 1757 /** 1758 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display 1759 * @var: updated screen information 1760 * @info: fbdev registered by the helper 1761 */ 1762 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1763 struct fb_info *info) 1764 { 1765 struct drm_fb_helper *fb_helper = info->par; 1766 struct drm_device *dev = fb_helper->dev; 1767 int ret; 1768 1769 if (oops_in_progress) 1770 return -EBUSY; 1771 1772 mutex_lock(&fb_helper->lock); 1773 if (!drm_fb_helper_is_bound(fb_helper)) { 1774 mutex_unlock(&fb_helper->lock); 1775 return -EBUSY; 1776 } 1777 1778 if (drm_drv_uses_atomic_modeset(dev)) 1779 ret = pan_display_atomic(var, info); 1780 else 1781 ret = pan_display_legacy(var, info); 1782 mutex_unlock(&fb_helper->lock); 1783 1784 return ret; 1785 } 1786 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1787 1788 /* 1789 * Allocates the backing storage and sets up the fbdev info structure through 1790 * the ->fb_probe callback. 1791 */ 1792 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1793 int preferred_bpp) 1794 { 1795 int ret = 0; 1796 int crtc_count = 0; 1797 int i; 1798 struct drm_fb_helper_surface_size sizes; 1799 int gamma_size = 0; 1800 int best_depth = 0; 1801 1802 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1803 sizes.surface_depth = 24; 1804 sizes.surface_bpp = 32; 1805 sizes.fb_width = (u32)-1; 1806 sizes.fb_height = (u32)-1; 1807 1808 /* 1809 * If driver picks 8 or 16 by default use that for both depth/bpp 1810 * to begin with 1811 */ 1812 if (preferred_bpp != sizes.surface_bpp) 1813 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1814 1815 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1816 drm_fb_helper_for_each_connector(fb_helper, i) { 1817 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 1818 struct drm_cmdline_mode *cmdline_mode; 1819 1820 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1821 1822 if (cmdline_mode->bpp_specified) { 1823 switch (cmdline_mode->bpp) { 1824 case 8: 1825 sizes.surface_depth = sizes.surface_bpp = 8; 1826 break; 1827 case 15: 1828 sizes.surface_depth = 15; 1829 sizes.surface_bpp = 16; 1830 break; 1831 case 16: 1832 sizes.surface_depth = sizes.surface_bpp = 16; 1833 break; 1834 case 24: 1835 sizes.surface_depth = sizes.surface_bpp = 24; 1836 break; 1837 case 32: 1838 sizes.surface_depth = 24; 1839 sizes.surface_bpp = 32; 1840 break; 1841 } 1842 break; 1843 } 1844 } 1845 1846 /* 1847 * If we run into a situation where, for example, the primary plane 1848 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth 1849 * 16) we need to scale down the depth of the sizes we request. 1850 */ 1851 for (i = 0; i < fb_helper->crtc_count; i++) { 1852 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 1853 struct drm_crtc *crtc = mode_set->crtc; 1854 struct drm_plane *plane = crtc->primary; 1855 int j; 1856 1857 DRM_DEBUG("test CRTC %d primary plane\n", i); 1858 1859 for (j = 0; j < plane->format_count; j++) { 1860 const struct drm_format_info *fmt; 1861 1862 fmt = drm_format_info(plane->format_types[j]); 1863 1864 /* 1865 * Do not consider YUV or other complicated formats 1866 * for framebuffers. This means only legacy formats 1867 * are supported (fmt->depth is a legacy field) but 1868 * the framebuffer emulation can only deal with such 1869 * formats, specifically RGB/BGA formats. 1870 */ 1871 if (fmt->depth == 0) 1872 continue; 1873 1874 /* We found a perfect fit, great */ 1875 if (fmt->depth == sizes.surface_depth) { 1876 best_depth = fmt->depth; 1877 break; 1878 } 1879 1880 /* Skip depths above what we're looking for */ 1881 if (fmt->depth > sizes.surface_depth) 1882 continue; 1883 1884 /* Best depth found so far */ 1885 if (fmt->depth > best_depth) 1886 best_depth = fmt->depth; 1887 } 1888 } 1889 if (sizes.surface_depth != best_depth) { 1890 DRM_INFO("requested bpp %d, scaled depth down to %d", 1891 sizes.surface_bpp, best_depth); 1892 sizes.surface_depth = best_depth; 1893 } 1894 1895 crtc_count = 0; 1896 for (i = 0; i < fb_helper->crtc_count; i++) { 1897 struct drm_display_mode *desired_mode; 1898 struct drm_mode_set *mode_set; 1899 int x, y, j; 1900 /* in case of tile group, are we the last tile vert or horiz? 1901 * If no tile group you are always the last one both vertically 1902 * and horizontally 1903 */ 1904 bool lastv = true, lasth = true; 1905 1906 desired_mode = fb_helper->crtc_info[i].desired_mode; 1907 mode_set = &fb_helper->crtc_info[i].mode_set; 1908 1909 if (!desired_mode) 1910 continue; 1911 1912 crtc_count++; 1913 1914 x = fb_helper->crtc_info[i].x; 1915 y = fb_helper->crtc_info[i].y; 1916 1917 if (gamma_size == 0) 1918 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; 1919 1920 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); 1921 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); 1922 1923 for (j = 0; j < mode_set->num_connectors; j++) { 1924 struct drm_connector *connector = mode_set->connectors[j]; 1925 1926 if (connector->has_tile) { 1927 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1928 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1929 /* cloning to multiple tiles is just crazy-talk, so: */ 1930 break; 1931 } 1932 } 1933 1934 if (lasth) 1935 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); 1936 if (lastv) 1937 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); 1938 } 1939 1940 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1941 DRM_INFO("Cannot find any crtc or sizes\n"); 1942 1943 /* First time: disable all crtc's.. */ 1944 if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master)) 1945 restore_fbdev_mode(fb_helper); 1946 return -EAGAIN; 1947 } 1948 1949 /* Handle our overallocation */ 1950 sizes.surface_height *= drm_fbdev_overalloc; 1951 sizes.surface_height /= 100; 1952 1953 /* push down into drivers */ 1954 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1955 if (ret < 0) 1956 return ret; 1957 1958 strcpy(fb_helper->fb->comm, "[fbcon]"); 1959 return 0; 1960 } 1961 1962 /** 1963 * drm_fb_helper_fill_fix - initializes fixed fbdev information 1964 * @info: fbdev registered by the helper 1965 * @pitch: desired pitch 1966 * @depth: desired depth 1967 * 1968 * Helper to fill in the fixed fbdev information useful for a non-accelerated 1969 * fbdev emulations. Drivers which support acceleration methods which impose 1970 * additional constraints need to set up their own limits. 1971 * 1972 * Drivers should call this (or their equivalent setup code) from their 1973 * &drm_fb_helper_funcs.fb_probe callback. 1974 */ 1975 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1976 uint32_t depth) 1977 { 1978 info->fix.type = FB_TYPE_PACKED_PIXELS; 1979 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1980 FB_VISUAL_TRUECOLOR; 1981 info->fix.mmio_start = 0; 1982 info->fix.mmio_len = 0; 1983 info->fix.type_aux = 0; 1984 info->fix.xpanstep = 1; /* doing it in hw */ 1985 info->fix.ypanstep = 1; /* doing it in hw */ 1986 info->fix.ywrapstep = 0; 1987 info->fix.accel = FB_ACCEL_NONE; 1988 1989 info->fix.line_length = pitch; 1990 } 1991 EXPORT_SYMBOL(drm_fb_helper_fill_fix); 1992 1993 /** 1994 * drm_fb_helper_fill_var - initalizes variable fbdev information 1995 * @info: fbdev instance to set up 1996 * @fb_helper: fb helper instance to use as template 1997 * @fb_width: desired fb width 1998 * @fb_height: desired fb height 1999 * 2000 * Sets up the variable fbdev metainformation from the given fb helper instance 2001 * and the drm framebuffer allocated in &drm_fb_helper.fb. 2002 * 2003 * Drivers should call this (or their equivalent setup code) from their 2004 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev 2005 * backing storage framebuffer. 2006 */ 2007 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 2008 uint32_t fb_width, uint32_t fb_height) 2009 { 2010 struct drm_framebuffer *fb = fb_helper->fb; 2011 2012 WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) || 2013 (drm_format_info_block_height(fb->format, 0) > 1)); 2014 info->pseudo_palette = fb_helper->pseudo_palette; 2015 info->var.xres_virtual = fb->width; 2016 info->var.yres_virtual = fb->height; 2017 info->var.bits_per_pixel = fb->format->cpp[0] * 8; 2018 info->var.accel_flags = FB_ACCELF_TEXT; 2019 info->var.xoffset = 0; 2020 info->var.yoffset = 0; 2021 info->var.activate = FB_ACTIVATE_NOW; 2022 2023 switch (fb->format->depth) { 2024 case 8: 2025 info->var.red.offset = 0; 2026 info->var.green.offset = 0; 2027 info->var.blue.offset = 0; 2028 info->var.red.length = 8; /* 8bit DAC */ 2029 info->var.green.length = 8; 2030 info->var.blue.length = 8; 2031 info->var.transp.offset = 0; 2032 info->var.transp.length = 0; 2033 break; 2034 case 15: 2035 info->var.red.offset = 10; 2036 info->var.green.offset = 5; 2037 info->var.blue.offset = 0; 2038 info->var.red.length = 5; 2039 info->var.green.length = 5; 2040 info->var.blue.length = 5; 2041 info->var.transp.offset = 15; 2042 info->var.transp.length = 1; 2043 break; 2044 case 16: 2045 info->var.red.offset = 11; 2046 info->var.green.offset = 5; 2047 info->var.blue.offset = 0; 2048 info->var.red.length = 5; 2049 info->var.green.length = 6; 2050 info->var.blue.length = 5; 2051 info->var.transp.offset = 0; 2052 break; 2053 case 24: 2054 info->var.red.offset = 16; 2055 info->var.green.offset = 8; 2056 info->var.blue.offset = 0; 2057 info->var.red.length = 8; 2058 info->var.green.length = 8; 2059 info->var.blue.length = 8; 2060 info->var.transp.offset = 0; 2061 info->var.transp.length = 0; 2062 break; 2063 case 32: 2064 info->var.red.offset = 16; 2065 info->var.green.offset = 8; 2066 info->var.blue.offset = 0; 2067 info->var.red.length = 8; 2068 info->var.green.length = 8; 2069 info->var.blue.length = 8; 2070 info->var.transp.offset = 24; 2071 info->var.transp.length = 8; 2072 break; 2073 default: 2074 break; 2075 } 2076 2077 info->var.xres = fb_width; 2078 info->var.yres = fb_height; 2079 } 2080 EXPORT_SYMBOL(drm_fb_helper_fill_var); 2081 2082 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 2083 uint32_t maxX, 2084 uint32_t maxY) 2085 { 2086 struct drm_connector *connector; 2087 int i, count = 0; 2088 2089 drm_fb_helper_for_each_connector(fb_helper, i) { 2090 connector = fb_helper->connector_info[i]->connector; 2091 count += connector->funcs->fill_modes(connector, maxX, maxY); 2092 } 2093 2094 return count; 2095 } 2096 2097 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 2098 { 2099 struct drm_display_mode *mode; 2100 2101 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 2102 if (mode->hdisplay > width || 2103 mode->vdisplay > height) 2104 continue; 2105 if (mode->type & DRM_MODE_TYPE_PREFERRED) 2106 return mode; 2107 } 2108 return NULL; 2109 } 2110 EXPORT_SYMBOL(drm_has_preferred_mode); 2111 2112 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 2113 { 2114 return fb_connector->connector->cmdline_mode.specified; 2115 } 2116 2117 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn) 2118 { 2119 struct drm_cmdline_mode *cmdline_mode; 2120 struct drm_display_mode *mode; 2121 bool prefer_non_interlace; 2122 2123 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 2124 if (cmdline_mode->specified == false) 2125 return NULL; 2126 2127 /* attempt to find a matching mode in the list of modes 2128 * we have gotten so far, if not add a CVT mode that conforms 2129 */ 2130 if (cmdline_mode->rb || cmdline_mode->margins) 2131 goto create_mode; 2132 2133 prefer_non_interlace = !cmdline_mode->interlace; 2134 again: 2135 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 2136 /* check width/height */ 2137 if (mode->hdisplay != cmdline_mode->xres || 2138 mode->vdisplay != cmdline_mode->yres) 2139 continue; 2140 2141 if (cmdline_mode->refresh_specified) { 2142 if (mode->vrefresh != cmdline_mode->refresh) 2143 continue; 2144 } 2145 2146 if (cmdline_mode->interlace) { 2147 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 2148 continue; 2149 } else if (prefer_non_interlace) { 2150 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 2151 continue; 2152 } 2153 return mode; 2154 } 2155 2156 if (prefer_non_interlace) { 2157 prefer_non_interlace = false; 2158 goto again; 2159 } 2160 2161 create_mode: 2162 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 2163 cmdline_mode); 2164 list_add(&mode->head, &fb_helper_conn->connector->modes); 2165 return mode; 2166 } 2167 EXPORT_SYMBOL(drm_pick_cmdline_mode); 2168 2169 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 2170 { 2171 bool enable; 2172 2173 if (connector->display_info.non_desktop) 2174 return false; 2175 2176 if (strict) 2177 enable = connector->status == connector_status_connected; 2178 else 2179 enable = connector->status != connector_status_disconnected; 2180 2181 return enable; 2182 } 2183 2184 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 2185 bool *enabled) 2186 { 2187 bool any_enabled = false; 2188 struct drm_connector *connector; 2189 int i = 0; 2190 2191 drm_fb_helper_for_each_connector(fb_helper, i) { 2192 connector = fb_helper->connector_info[i]->connector; 2193 enabled[i] = drm_connector_enabled(connector, true); 2194 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 2195 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no"); 2196 2197 any_enabled |= enabled[i]; 2198 } 2199 2200 if (any_enabled) 2201 return; 2202 2203 drm_fb_helper_for_each_connector(fb_helper, i) { 2204 connector = fb_helper->connector_info[i]->connector; 2205 enabled[i] = drm_connector_enabled(connector, false); 2206 } 2207 } 2208 2209 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 2210 struct drm_display_mode **modes, 2211 struct drm_fb_offset *offsets, 2212 bool *enabled, int width, int height) 2213 { 2214 int count, i, j; 2215 bool can_clone = false; 2216 struct drm_fb_helper_connector *fb_helper_conn; 2217 struct drm_display_mode *dmt_mode, *mode; 2218 2219 /* only contemplate cloning in the single crtc case */ 2220 if (fb_helper->crtc_count > 1) 2221 return false; 2222 2223 count = 0; 2224 drm_fb_helper_for_each_connector(fb_helper, i) { 2225 if (enabled[i]) 2226 count++; 2227 } 2228 2229 /* only contemplate cloning if more than one connector is enabled */ 2230 if (count <= 1) 2231 return false; 2232 2233 /* check the command line or if nothing common pick 1024x768 */ 2234 can_clone = true; 2235 drm_fb_helper_for_each_connector(fb_helper, i) { 2236 if (!enabled[i]) 2237 continue; 2238 fb_helper_conn = fb_helper->connector_info[i]; 2239 modes[i] = drm_pick_cmdline_mode(fb_helper_conn); 2240 if (!modes[i]) { 2241 can_clone = false; 2242 break; 2243 } 2244 for (j = 0; j < i; j++) { 2245 if (!enabled[j]) 2246 continue; 2247 if (!drm_mode_match(modes[j], modes[i], 2248 DRM_MODE_MATCH_TIMINGS | 2249 DRM_MODE_MATCH_CLOCK | 2250 DRM_MODE_MATCH_FLAGS | 2251 DRM_MODE_MATCH_3D_FLAGS)) 2252 can_clone = false; 2253 } 2254 } 2255 2256 if (can_clone) { 2257 DRM_DEBUG_KMS("can clone using command line\n"); 2258 return true; 2259 } 2260 2261 /* try and find a 1024x768 mode on each connector */ 2262 can_clone = true; 2263 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 2264 2265 drm_fb_helper_for_each_connector(fb_helper, i) { 2266 if (!enabled[i]) 2267 continue; 2268 2269 fb_helper_conn = fb_helper->connector_info[i]; 2270 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 2271 if (drm_mode_match(mode, dmt_mode, 2272 DRM_MODE_MATCH_TIMINGS | 2273 DRM_MODE_MATCH_CLOCK | 2274 DRM_MODE_MATCH_FLAGS | 2275 DRM_MODE_MATCH_3D_FLAGS)) 2276 modes[i] = mode; 2277 } 2278 if (!modes[i]) 2279 can_clone = false; 2280 } 2281 2282 if (can_clone) { 2283 DRM_DEBUG_KMS("can clone using 1024x768\n"); 2284 return true; 2285 } 2286 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 2287 return false; 2288 } 2289 2290 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, 2291 struct drm_display_mode **modes, 2292 struct drm_fb_offset *offsets, 2293 int idx, 2294 int h_idx, int v_idx) 2295 { 2296 struct drm_fb_helper_connector *fb_helper_conn; 2297 int i; 2298 int hoffset = 0, voffset = 0; 2299 2300 drm_fb_helper_for_each_connector(fb_helper, i) { 2301 fb_helper_conn = fb_helper->connector_info[i]; 2302 if (!fb_helper_conn->connector->has_tile) 2303 continue; 2304 2305 if (!modes[i] && (h_idx || v_idx)) { 2306 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, 2307 fb_helper_conn->connector->base.id); 2308 continue; 2309 } 2310 if (fb_helper_conn->connector->tile_h_loc < h_idx) 2311 hoffset += modes[i]->hdisplay; 2312 2313 if (fb_helper_conn->connector->tile_v_loc < v_idx) 2314 voffset += modes[i]->vdisplay; 2315 } 2316 offsets[idx].x = hoffset; 2317 offsets[idx].y = voffset; 2318 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); 2319 return 0; 2320 } 2321 2322 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 2323 struct drm_display_mode **modes, 2324 struct drm_fb_offset *offsets, 2325 bool *enabled, int width, int height) 2326 { 2327 struct drm_fb_helper_connector *fb_helper_conn; 2328 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1; 2329 u64 conn_configured = 0; 2330 int tile_pass = 0; 2331 int i; 2332 2333 retry: 2334 drm_fb_helper_for_each_connector(fb_helper, i) { 2335 fb_helper_conn = fb_helper->connector_info[i]; 2336 2337 if (conn_configured & BIT_ULL(i)) 2338 continue; 2339 2340 if (enabled[i] == false) { 2341 conn_configured |= BIT_ULL(i); 2342 continue; 2343 } 2344 2345 /* first pass over all the untiled connectors */ 2346 if (tile_pass == 0 && fb_helper_conn->connector->has_tile) 2347 continue; 2348 2349 if (tile_pass == 1) { 2350 if (fb_helper_conn->connector->tile_h_loc != 0 || 2351 fb_helper_conn->connector->tile_v_loc != 0) 2352 continue; 2353 2354 } else { 2355 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 && 2356 fb_helper_conn->connector->tile_v_loc != tile_pass - 1) 2357 /* if this tile_pass doesn't cover any of the tiles - keep going */ 2358 continue; 2359 2360 /* 2361 * find the tile offsets for this pass - need to find 2362 * all tiles left and above 2363 */ 2364 drm_get_tile_offsets(fb_helper, modes, offsets, 2365 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); 2366 } 2367 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 2368 fb_helper_conn->connector->base.id); 2369 2370 /* got for command line mode first */ 2371 modes[i] = drm_pick_cmdline_mode(fb_helper_conn); 2372 if (!modes[i]) { 2373 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", 2374 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); 2375 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 2376 } 2377 /* No preferred modes, pick one off the list */ 2378 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 2379 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 2380 break; 2381 } 2382 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 2383 "none"); 2384 conn_configured |= BIT_ULL(i); 2385 } 2386 2387 if ((conn_configured & mask) != mask) { 2388 tile_pass++; 2389 goto retry; 2390 } 2391 return true; 2392 } 2393 2394 static bool connector_has_possible_crtc(struct drm_connector *connector, 2395 struct drm_crtc *crtc) 2396 { 2397 struct drm_encoder *encoder; 2398 int i; 2399 2400 drm_connector_for_each_possible_encoder(connector, encoder, i) { 2401 if (encoder->possible_crtcs & drm_crtc_mask(crtc)) 2402 return true; 2403 } 2404 2405 return false; 2406 } 2407 2408 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 2409 struct drm_fb_helper_crtc **best_crtcs, 2410 struct drm_display_mode **modes, 2411 int n, int width, int height) 2412 { 2413 int c, o; 2414 struct drm_connector *connector; 2415 int my_score, best_score, score; 2416 struct drm_fb_helper_crtc **crtcs, *crtc; 2417 struct drm_fb_helper_connector *fb_helper_conn; 2418 2419 if (n == fb_helper->connector_count) 2420 return 0; 2421 2422 fb_helper_conn = fb_helper->connector_info[n]; 2423 connector = fb_helper_conn->connector; 2424 2425 best_crtcs[n] = NULL; 2426 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 2427 if (modes[n] == NULL) 2428 return best_score; 2429 2430 crtcs = kcalloc(fb_helper->connector_count, 2431 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2432 if (!crtcs) 2433 return best_score; 2434 2435 my_score = 1; 2436 if (connector->status == connector_status_connected) 2437 my_score++; 2438 if (drm_has_cmdline_mode(fb_helper_conn)) 2439 my_score++; 2440 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 2441 my_score++; 2442 2443 /* 2444 * select a crtc for this connector and then attempt to configure 2445 * remaining connectors 2446 */ 2447 for (c = 0; c < fb_helper->crtc_count; c++) { 2448 crtc = &fb_helper->crtc_info[c]; 2449 2450 if (!connector_has_possible_crtc(connector, 2451 crtc->mode_set.crtc)) 2452 continue; 2453 2454 for (o = 0; o < n; o++) 2455 if (best_crtcs[o] == crtc) 2456 break; 2457 2458 if (o < n) { 2459 /* ignore cloning unless only a single crtc */ 2460 if (fb_helper->crtc_count > 1) 2461 continue; 2462 2463 if (!drm_mode_equal(modes[o], modes[n])) 2464 continue; 2465 } 2466 2467 crtcs[n] = crtc; 2468 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 2469 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 2470 width, height); 2471 if (score > best_score) { 2472 best_score = score; 2473 memcpy(best_crtcs, crtcs, 2474 fb_helper->connector_count * 2475 sizeof(struct drm_fb_helper_crtc *)); 2476 } 2477 } 2478 2479 kfree(crtcs); 2480 return best_score; 2481 } 2482 2483 /* 2484 * This function checks if rotation is necessary because of panel orientation 2485 * and if it is, if it is supported. 2486 * If rotation is necessary and supported, its gets set in fb_crtc.rotation. 2487 * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets 2488 * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only 2489 * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do 2490 * the unsupported rotation. 2491 */ 2492 static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper, 2493 struct drm_fb_helper_crtc *fb_crtc, 2494 struct drm_connector *connector) 2495 { 2496 struct drm_plane *plane = fb_crtc->mode_set.crtc->primary; 2497 uint64_t valid_mask = 0; 2498 int i, rotation; 2499 2500 fb_crtc->rotation = DRM_MODE_ROTATE_0; 2501 2502 switch (connector->display_info.panel_orientation) { 2503 case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP: 2504 rotation = DRM_MODE_ROTATE_180; 2505 break; 2506 case DRM_MODE_PANEL_ORIENTATION_LEFT_UP: 2507 rotation = DRM_MODE_ROTATE_90; 2508 break; 2509 case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP: 2510 rotation = DRM_MODE_ROTATE_270; 2511 break; 2512 default: 2513 rotation = DRM_MODE_ROTATE_0; 2514 } 2515 2516 /* 2517 * TODO: support 90 / 270 degree hardware rotation, 2518 * depending on the hardware this may require the framebuffer 2519 * to be in a specific tiling format. 2520 */ 2521 if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) { 2522 fb_helper->sw_rotations |= rotation; 2523 return; 2524 } 2525 2526 for (i = 0; i < plane->rotation_property->num_values; i++) 2527 valid_mask |= (1ULL << plane->rotation_property->values[i]); 2528 2529 if (!(rotation & valid_mask)) { 2530 fb_helper->sw_rotations |= rotation; 2531 return; 2532 } 2533 2534 fb_crtc->rotation = rotation; 2535 /* Rotating in hardware, fbcon should not rotate */ 2536 fb_helper->sw_rotations |= DRM_MODE_ROTATE_0; 2537 } 2538 2539 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper, 2540 u32 width, u32 height) 2541 { 2542 struct drm_device *dev = fb_helper->dev; 2543 struct drm_fb_helper_crtc **crtcs; 2544 struct drm_display_mode **modes; 2545 struct drm_fb_offset *offsets; 2546 bool *enabled; 2547 int i; 2548 2549 DRM_DEBUG_KMS("\n"); 2550 /* prevent concurrent modification of connector_count by hotplug */ 2551 lockdep_assert_held(&fb_helper->lock); 2552 2553 crtcs = kcalloc(fb_helper->connector_count, 2554 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2555 modes = kcalloc(fb_helper->connector_count, 2556 sizeof(struct drm_display_mode *), GFP_KERNEL); 2557 offsets = kcalloc(fb_helper->connector_count, 2558 sizeof(struct drm_fb_offset), GFP_KERNEL); 2559 enabled = kcalloc(fb_helper->connector_count, 2560 sizeof(bool), GFP_KERNEL); 2561 if (!crtcs || !modes || !enabled || !offsets) { 2562 DRM_ERROR("Memory allocation failed\n"); 2563 goto out; 2564 } 2565 2566 mutex_lock(&fb_helper->dev->mode_config.mutex); 2567 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0) 2568 DRM_DEBUG_KMS("No connectors reported connected with modes\n"); 2569 drm_enable_connectors(fb_helper, enabled); 2570 2571 if (!(fb_helper->funcs->initial_config && 2572 fb_helper->funcs->initial_config(fb_helper, crtcs, modes, 2573 offsets, 2574 enabled, width, height))) { 2575 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); 2576 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); 2577 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); 2578 2579 if (!drm_target_cloned(fb_helper, modes, offsets, 2580 enabled, width, height) && 2581 !drm_target_preferred(fb_helper, modes, offsets, 2582 enabled, width, height)) 2583 DRM_ERROR("Unable to find initial modes\n"); 2584 2585 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 2586 width, height); 2587 2588 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 2589 } 2590 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2591 2592 /* need to set the modesets up here for use later */ 2593 /* fill out the connector<->crtc mappings into the modesets */ 2594 for (i = 0; i < fb_helper->crtc_count; i++) 2595 drm_fb_helper_modeset_release(fb_helper, 2596 &fb_helper->crtc_info[i].mode_set); 2597 2598 fb_helper->sw_rotations = 0; 2599 drm_fb_helper_for_each_connector(fb_helper, i) { 2600 struct drm_display_mode *mode = modes[i]; 2601 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 2602 struct drm_fb_offset *offset = &offsets[i]; 2603 2604 if (mode && fb_crtc) { 2605 struct drm_mode_set *modeset = &fb_crtc->mode_set; 2606 struct drm_connector *connector = 2607 fb_helper->connector_info[i]->connector; 2608 2609 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", 2610 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); 2611 2612 fb_crtc->desired_mode = mode; 2613 fb_crtc->x = offset->x; 2614 fb_crtc->y = offset->y; 2615 modeset->mode = drm_mode_duplicate(dev, 2616 fb_crtc->desired_mode); 2617 drm_connector_get(connector); 2618 drm_setup_crtc_rotation(fb_helper, fb_crtc, connector); 2619 modeset->connectors[modeset->num_connectors++] = connector; 2620 modeset->x = offset->x; 2621 modeset->y = offset->y; 2622 } 2623 } 2624 out: 2625 kfree(crtcs); 2626 kfree(modes); 2627 kfree(offsets); 2628 kfree(enabled); 2629 } 2630 2631 /* 2632 * This is a continuation of drm_setup_crtcs() that sets up anything related 2633 * to the framebuffer. During initialization, drm_setup_crtcs() is called before 2634 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). 2635 * So, any setup that touches those fields needs to be done here instead of in 2636 * drm_setup_crtcs(). 2637 */ 2638 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) 2639 { 2640 struct fb_info *info = fb_helper->fbdev; 2641 int i; 2642 2643 for (i = 0; i < fb_helper->crtc_count; i++) 2644 if (fb_helper->crtc_info[i].mode_set.num_connectors) 2645 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; 2646 2647 mutex_lock(&fb_helper->dev->mode_config.mutex); 2648 drm_fb_helper_for_each_connector(fb_helper, i) { 2649 struct drm_connector *connector = 2650 fb_helper->connector_info[i]->connector; 2651 2652 /* use first connected connector for the physical dimensions */ 2653 if (connector->status == connector_status_connected) { 2654 info->var.width = connector->display_info.width_mm; 2655 info->var.height = connector->display_info.height_mm; 2656 break; 2657 } 2658 } 2659 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2660 2661 switch (fb_helper->sw_rotations) { 2662 case DRM_MODE_ROTATE_0: 2663 info->fbcon_rotate_hint = FB_ROTATE_UR; 2664 break; 2665 case DRM_MODE_ROTATE_90: 2666 info->fbcon_rotate_hint = FB_ROTATE_CCW; 2667 break; 2668 case DRM_MODE_ROTATE_180: 2669 info->fbcon_rotate_hint = FB_ROTATE_UD; 2670 break; 2671 case DRM_MODE_ROTATE_270: 2672 info->fbcon_rotate_hint = FB_ROTATE_CW; 2673 break; 2674 default: 2675 /* 2676 * Multiple bits are set / multiple rotations requested 2677 * fbcon cannot handle separate rotation settings per 2678 * output, so fallback to unrotated. 2679 */ 2680 info->fbcon_rotate_hint = FB_ROTATE_UR; 2681 } 2682 } 2683 2684 /* Note: Drops fb_helper->lock before returning. */ 2685 static int 2686 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, 2687 int bpp_sel) 2688 { 2689 struct drm_device *dev = fb_helper->dev; 2690 struct fb_info *info; 2691 unsigned int width, height; 2692 int ret; 2693 2694 width = dev->mode_config.max_width; 2695 height = dev->mode_config.max_height; 2696 2697 drm_setup_crtcs(fb_helper, width, height); 2698 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 2699 if (ret < 0) { 2700 if (ret == -EAGAIN) { 2701 fb_helper->preferred_bpp = bpp_sel; 2702 fb_helper->deferred_setup = true; 2703 ret = 0; 2704 } 2705 mutex_unlock(&fb_helper->lock); 2706 2707 return ret; 2708 } 2709 drm_setup_crtcs_fb(fb_helper); 2710 2711 fb_helper->deferred_setup = false; 2712 2713 info = fb_helper->fbdev; 2714 info->var.pixclock = 0; 2715 /* Shamelessly allow physical address leaking to userspace */ 2716 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 2717 if (!drm_leak_fbdev_smem) 2718 #endif 2719 /* don't leak any physical addresses to userspace */ 2720 info->flags |= FBINFO_HIDE_SMEM_START; 2721 2722 /* Need to drop locks to avoid recursive deadlock in 2723 * register_framebuffer. This is ok because the only thing left to do is 2724 * register the fbdev emulation instance in kernel_fb_helper_list. */ 2725 mutex_unlock(&fb_helper->lock); 2726 2727 ret = register_framebuffer(info); 2728 if (ret < 0) 2729 return ret; 2730 2731 dev_info(dev->dev, "fb%d: %s frame buffer device\n", 2732 info->node, info->fix.id); 2733 2734 mutex_lock(&kernel_fb_helper_lock); 2735 if (list_empty(&kernel_fb_helper_list)) 2736 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 2737 2738 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 2739 mutex_unlock(&kernel_fb_helper_lock); 2740 2741 return 0; 2742 } 2743 2744 /** 2745 * drm_fb_helper_initial_config - setup a sane initial connector configuration 2746 * @fb_helper: fb_helper device struct 2747 * @bpp_sel: bpp value to use for the framebuffer configuration 2748 * 2749 * Scans the CRTCs and connectors and tries to put together an initial setup. 2750 * At the moment, this is a cloned configuration across all heads with 2751 * a new framebuffer object as the backing store. 2752 * 2753 * Note that this also registers the fbdev and so allows userspace to call into 2754 * the driver through the fbdev interfaces. 2755 * 2756 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback 2757 * to let the driver allocate and initialize the fbdev info structure and the 2758 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and 2759 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default 2760 * values for the fbdev info structure. 2761 * 2762 * HANG DEBUGGING: 2763 * 2764 * When you have fbcon support built-in or already loaded, this function will do 2765 * a full modeset to setup the fbdev console. Due to locking misdesign in the 2766 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 2767 * console_lock. Until console_unlock is called no dmesg lines will be sent out 2768 * to consoles, not even serial console. This means when your driver crashes, 2769 * you will see absolutely nothing else but a system stuck in this function, 2770 * with no further output. Any kind of printk() you place within your own driver 2771 * or in the drm core modeset code will also never show up. 2772 * 2773 * Standard debug practice is to run the fbcon setup without taking the 2774 * console_lock as a hack, to be able to see backtraces and crashes on the 2775 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 2776 * cmdline option. 2777 * 2778 * The other option is to just disable fbdev emulation since very likely the 2779 * first modeset from userspace will crash in the same way, and is even easier 2780 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 2781 * kernel cmdline option. 2782 * 2783 * RETURNS: 2784 * Zero if everything went ok, nonzero otherwise. 2785 */ 2786 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 2787 { 2788 int ret; 2789 2790 if (!drm_fbdev_emulation) 2791 return 0; 2792 2793 mutex_lock(&fb_helper->lock); 2794 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); 2795 2796 return ret; 2797 } 2798 EXPORT_SYMBOL(drm_fb_helper_initial_config); 2799 2800 /** 2801 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 2802 * probing all the outputs attached to the fb 2803 * @fb_helper: driver-allocated fbdev helper, can be NULL 2804 * 2805 * Scan the connectors attached to the fb_helper and try to put together a 2806 * setup after notification of a change in output configuration. 2807 * 2808 * Called at runtime, takes the mode config locks to be able to check/change the 2809 * modeset configuration. Must be run from process context (which usually means 2810 * either the output polling work or a work item launched from the driver's 2811 * hotplug interrupt). 2812 * 2813 * Note that drivers may call this even before calling 2814 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 2815 * for a race-free fbcon setup and will make sure that the fbdev emulation will 2816 * not miss any hotplug events. 2817 * 2818 * RETURNS: 2819 * 0 on success and a non-zero error code otherwise. 2820 */ 2821 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 2822 { 2823 int err = 0; 2824 2825 if (!drm_fbdev_emulation || !fb_helper) 2826 return 0; 2827 2828 mutex_lock(&fb_helper->lock); 2829 if (fb_helper->deferred_setup) { 2830 err = __drm_fb_helper_initial_config_and_unlock(fb_helper, 2831 fb_helper->preferred_bpp); 2832 return err; 2833 } 2834 2835 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { 2836 fb_helper->delayed_hotplug = true; 2837 mutex_unlock(&fb_helper->lock); 2838 return err; 2839 } 2840 2841 DRM_DEBUG_KMS("\n"); 2842 2843 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height); 2844 drm_setup_crtcs_fb(fb_helper); 2845 mutex_unlock(&fb_helper->lock); 2846 2847 drm_fb_helper_set_par(fb_helper->fbdev); 2848 2849 return 0; 2850 } 2851 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2852 2853 /** 2854 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation 2855 * @dev: DRM device 2856 * @fb_helper: fbdev helper structure to set up 2857 * @funcs: fbdev helper functions 2858 * @preferred_bpp: Preferred bits per pixel for the device. 2859 * @dev->mode_config.preferred_depth is used if this is zero. 2860 * @max_conn_count: Maximum number of connectors. 2861 * @dev->mode_config.num_connector is used if this is zero. 2862 * 2863 * This function sets up fbdev emulation and registers fbdev for access by 2864 * userspace. If all connectors are disconnected, setup is deferred to the next 2865 * time drm_fb_helper_hotplug_event() is called. 2866 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback 2867 * function. 2868 * 2869 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev. 2870 * 2871 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup(). 2872 * 2873 * Returns: 2874 * Zero on success or negative error code on failure. 2875 */ 2876 int drm_fb_helper_fbdev_setup(struct drm_device *dev, 2877 struct drm_fb_helper *fb_helper, 2878 const struct drm_fb_helper_funcs *funcs, 2879 unsigned int preferred_bpp, 2880 unsigned int max_conn_count) 2881 { 2882 int ret; 2883 2884 if (!preferred_bpp) 2885 preferred_bpp = dev->mode_config.preferred_depth; 2886 if (!preferred_bpp) 2887 preferred_bpp = 32; 2888 2889 if (!max_conn_count) 2890 max_conn_count = dev->mode_config.num_connector; 2891 if (!max_conn_count) { 2892 DRM_DEV_ERROR(dev->dev, "fbdev: No connectors\n"); 2893 return -EINVAL; 2894 } 2895 2896 drm_fb_helper_prepare(dev, fb_helper, funcs); 2897 2898 ret = drm_fb_helper_init(dev, fb_helper, max_conn_count); 2899 if (ret < 0) { 2900 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret); 2901 return ret; 2902 } 2903 2904 ret = drm_fb_helper_single_add_all_connectors(fb_helper); 2905 if (ret < 0) { 2906 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret); 2907 goto err_drm_fb_helper_fini; 2908 } 2909 2910 if (!drm_drv_uses_atomic_modeset(dev)) 2911 drm_helper_disable_unused_functions(dev); 2912 2913 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp); 2914 if (ret < 0) { 2915 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret); 2916 goto err_drm_fb_helper_fini; 2917 } 2918 2919 return 0; 2920 2921 err_drm_fb_helper_fini: 2922 drm_fb_helper_fbdev_teardown(dev); 2923 2924 return ret; 2925 } 2926 EXPORT_SYMBOL(drm_fb_helper_fbdev_setup); 2927 2928 /** 2929 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation 2930 * @dev: DRM device 2931 * 2932 * This function unregisters fbdev if not already done and cleans up the 2933 * associated resources including the &drm_framebuffer. 2934 * The driver is responsible for freeing the &drm_fb_helper structure which is 2935 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared 2936 * when this function returns. 2937 * 2938 * In order to support device removal/unplug while file handles are still open, 2939 * drm_fb_helper_unregister_fbi() should be called on device removal and 2940 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when 2941 * file handles are closed. 2942 */ 2943 void drm_fb_helper_fbdev_teardown(struct drm_device *dev) 2944 { 2945 struct drm_fb_helper *fb_helper = dev->fb_helper; 2946 struct fb_ops *fbops = NULL; 2947 2948 if (!fb_helper) 2949 return; 2950 2951 /* Unregister if it hasn't been done already */ 2952 if (fb_helper->fbdev && fb_helper->fbdev->dev) 2953 drm_fb_helper_unregister_fbi(fb_helper); 2954 2955 if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) { 2956 fb_deferred_io_cleanup(fb_helper->fbdev); 2957 kfree(fb_helper->fbdev->fbdefio); 2958 fbops = fb_helper->fbdev->fbops; 2959 } 2960 2961 drm_fb_helper_fini(fb_helper); 2962 kfree(fbops); 2963 2964 if (fb_helper->fb) 2965 drm_framebuffer_remove(fb_helper->fb); 2966 } 2967 EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown); 2968 2969 /** 2970 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation 2971 * @dev: DRM device 2972 * 2973 * This function can be used as the &drm_driver->lastclose callback for drivers 2974 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked(). 2975 */ 2976 void drm_fb_helper_lastclose(struct drm_device *dev) 2977 { 2978 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper); 2979 } 2980 EXPORT_SYMBOL(drm_fb_helper_lastclose); 2981 2982 /** 2983 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed 2984 * helper for fbdev emulation 2985 * @dev: DRM device 2986 * 2987 * This function can be used as the 2988 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only 2989 * need to call drm_fb_helper_hotplug_event(). 2990 */ 2991 void drm_fb_helper_output_poll_changed(struct drm_device *dev) 2992 { 2993 drm_fb_helper_hotplug_event(dev->fb_helper); 2994 } 2995 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed); 2996 2997 /* @user: 1=userspace, 0=fbcon */ 2998 static int drm_fbdev_fb_open(struct fb_info *info, int user) 2999 { 3000 struct drm_fb_helper *fb_helper = info->par; 3001 3002 if (!try_module_get(fb_helper->dev->driver->fops->owner)) 3003 return -ENODEV; 3004 3005 return 0; 3006 } 3007 3008 static int drm_fbdev_fb_release(struct fb_info *info, int user) 3009 { 3010 struct drm_fb_helper *fb_helper = info->par; 3011 3012 module_put(fb_helper->dev->driver->fops->owner); 3013 3014 return 0; 3015 } 3016 3017 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper) 3018 { 3019 struct fb_info *fbi = fb_helper->fbdev; 3020 struct fb_ops *fbops = NULL; 3021 void *shadow = NULL; 3022 3023 if (!fb_helper->dev) 3024 return; 3025 3026 if (fbi && fbi->fbdefio) { 3027 fb_deferred_io_cleanup(fbi); 3028 shadow = fbi->screen_buffer; 3029 fbops = fbi->fbops; 3030 } 3031 3032 drm_fb_helper_fini(fb_helper); 3033 3034 if (shadow) { 3035 vfree(shadow); 3036 kfree(fbops); 3037 } 3038 3039 drm_client_framebuffer_delete(fb_helper->buffer); 3040 } 3041 3042 static void drm_fbdev_release(struct drm_fb_helper *fb_helper) 3043 { 3044 drm_fbdev_cleanup(fb_helper); 3045 drm_client_release(&fb_helper->client); 3046 kfree(fb_helper); 3047 } 3048 3049 /* 3050 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of 3051 * unregister_framebuffer() or fb_release(). 3052 */ 3053 static void drm_fbdev_fb_destroy(struct fb_info *info) 3054 { 3055 drm_fbdev_release(info->par); 3056 } 3057 3058 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) 3059 { 3060 struct drm_fb_helper *fb_helper = info->par; 3061 3062 if (fb_helper->dev->driver->gem_prime_mmap) 3063 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma); 3064 else 3065 return -ENODEV; 3066 } 3067 3068 static struct fb_ops drm_fbdev_fb_ops = { 3069 .owner = THIS_MODULE, 3070 DRM_FB_HELPER_DEFAULT_OPS, 3071 .fb_open = drm_fbdev_fb_open, 3072 .fb_release = drm_fbdev_fb_release, 3073 .fb_destroy = drm_fbdev_fb_destroy, 3074 .fb_mmap = drm_fbdev_fb_mmap, 3075 .fb_read = drm_fb_helper_sys_read, 3076 .fb_write = drm_fb_helper_sys_write, 3077 .fb_fillrect = drm_fb_helper_sys_fillrect, 3078 .fb_copyarea = drm_fb_helper_sys_copyarea, 3079 .fb_imageblit = drm_fb_helper_sys_imageblit, 3080 }; 3081 3082 static struct fb_deferred_io drm_fbdev_defio = { 3083 .delay = HZ / 20, 3084 .deferred_io = drm_fb_helper_deferred_io, 3085 }; 3086 3087 /** 3088 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper 3089 * @fb_helper: fbdev helper structure 3090 * @sizes: describes fbdev size and scanout surface size 3091 * 3092 * This function uses the client API to create a framebuffer backed by a dumb buffer. 3093 * 3094 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect, 3095 * fb_copyarea, fb_imageblit. 3096 * 3097 * Returns: 3098 * Zero on success or negative error code on failure. 3099 */ 3100 int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, 3101 struct drm_fb_helper_surface_size *sizes) 3102 { 3103 struct drm_client_dev *client = &fb_helper->client; 3104 struct drm_client_buffer *buffer; 3105 struct drm_framebuffer *fb; 3106 struct fb_info *fbi; 3107 u32 format; 3108 3109 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n", 3110 sizes->surface_width, sizes->surface_height, 3111 sizes->surface_bpp); 3112 3113 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); 3114 buffer = drm_client_framebuffer_create(client, sizes->surface_width, 3115 sizes->surface_height, format); 3116 if (IS_ERR(buffer)) 3117 return PTR_ERR(buffer); 3118 3119 fb_helper->buffer = buffer; 3120 fb_helper->fb = buffer->fb; 3121 fb = buffer->fb; 3122 3123 fbi = drm_fb_helper_alloc_fbi(fb_helper); 3124 if (IS_ERR(fbi)) 3125 return PTR_ERR(fbi); 3126 3127 fbi->par = fb_helper; 3128 fbi->fbops = &drm_fbdev_fb_ops; 3129 fbi->screen_size = fb->height * fb->pitches[0]; 3130 fbi->fix.smem_len = fbi->screen_size; 3131 fbi->screen_buffer = buffer->vaddr; 3132 /* Shamelessly leak the physical address to user-space */ 3133 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 3134 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0) 3135 fbi->fix.smem_start = 3136 page_to_phys(virt_to_page(fbi->screen_buffer)); 3137 #endif 3138 strcpy(fbi->fix.id, "DRM emulated"); 3139 3140 drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth); 3141 drm_fb_helper_fill_var(fbi, fb_helper, sizes->fb_width, sizes->fb_height); 3142 3143 if (fb->funcs->dirty) { 3144 struct fb_ops *fbops; 3145 void *shadow; 3146 3147 /* 3148 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per 3149 * instance version is necessary. 3150 */ 3151 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); 3152 shadow = vzalloc(fbi->screen_size); 3153 if (!fbops || !shadow) { 3154 kfree(fbops); 3155 vfree(shadow); 3156 return -ENOMEM; 3157 } 3158 3159 *fbops = *fbi->fbops; 3160 fbi->fbops = fbops; 3161 fbi->screen_buffer = shadow; 3162 fbi->fbdefio = &drm_fbdev_defio; 3163 3164 fb_deferred_io_init(fbi); 3165 } 3166 3167 return 0; 3168 } 3169 EXPORT_SYMBOL(drm_fb_helper_generic_probe); 3170 3171 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { 3172 .fb_probe = drm_fb_helper_generic_probe, 3173 }; 3174 3175 static void drm_fbdev_client_unregister(struct drm_client_dev *client) 3176 { 3177 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 3178 3179 if (fb_helper->fbdev) 3180 /* drm_fbdev_fb_destroy() takes care of cleanup */ 3181 drm_fb_helper_unregister_fbi(fb_helper); 3182 else 3183 drm_fbdev_release(fb_helper); 3184 } 3185 3186 static int drm_fbdev_client_restore(struct drm_client_dev *client) 3187 { 3188 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 3189 3190 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 3191 3192 return 0; 3193 } 3194 3195 static int drm_fbdev_client_hotplug(struct drm_client_dev *client) 3196 { 3197 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 3198 struct drm_device *dev = client->dev; 3199 int ret; 3200 3201 /* Setup is not retried if it has failed */ 3202 if (!fb_helper->dev && fb_helper->funcs) 3203 return 0; 3204 3205 if (dev->fb_helper) 3206 return drm_fb_helper_hotplug_event(dev->fb_helper); 3207 3208 if (!dev->mode_config.num_connector) { 3209 DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n"); 3210 return 0; 3211 } 3212 3213 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs); 3214 3215 ret = drm_fb_helper_init(dev, fb_helper, dev->mode_config.num_connector); 3216 if (ret) 3217 goto err; 3218 3219 ret = drm_fb_helper_single_add_all_connectors(fb_helper); 3220 if (ret) 3221 goto err_cleanup; 3222 3223 if (!drm_drv_uses_atomic_modeset(dev)) 3224 drm_helper_disable_unused_functions(dev); 3225 3226 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp); 3227 if (ret) 3228 goto err_cleanup; 3229 3230 return 0; 3231 3232 err_cleanup: 3233 drm_fbdev_cleanup(fb_helper); 3234 err: 3235 fb_helper->dev = NULL; 3236 fb_helper->fbdev = NULL; 3237 3238 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret); 3239 3240 return ret; 3241 } 3242 3243 static const struct drm_client_funcs drm_fbdev_client_funcs = { 3244 .owner = THIS_MODULE, 3245 .unregister = drm_fbdev_client_unregister, 3246 .restore = drm_fbdev_client_restore, 3247 .hotplug = drm_fbdev_client_hotplug, 3248 }; 3249 3250 /** 3251 * drm_fbdev_generic_setup() - Setup generic fbdev emulation 3252 * @dev: DRM device 3253 * @preferred_bpp: Preferred bits per pixel for the device. 3254 * @dev->mode_config.preferred_depth is used if this is zero. 3255 * 3256 * This function sets up generic fbdev emulation for drivers that supports 3257 * dumb buffers with a virtual address and that can be mmap'ed. If the driver 3258 * does not support these functions, it could use drm_fb_helper_fbdev_setup(). 3259 * 3260 * Restore, hotplug events and teardown are all taken care of. Drivers that do 3261 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. 3262 * Simple drivers might use drm_mode_config_helper_suspend(). 3263 * 3264 * Drivers that set the dirty callback on their framebuffer will get a shadow 3265 * fbdev buffer that is blitted onto the real buffer. This is done in order to 3266 * make deferred I/O work with all kinds of buffers. 3267 * 3268 * This function is safe to call even when there are no connectors present. 3269 * Setup will be retried on the next hotplug event. 3270 * 3271 * The fbdev is destroyed by drm_dev_unregister(). 3272 * 3273 * Returns: 3274 * Zero on success or negative error code on failure. 3275 */ 3276 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) 3277 { 3278 struct drm_fb_helper *fb_helper; 3279 int ret; 3280 3281 WARN(dev->fb_helper, "fb_helper is already set!\n"); 3282 3283 if (!drm_fbdev_emulation) 3284 return 0; 3285 3286 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 3287 if (!fb_helper) 3288 return -ENOMEM; 3289 3290 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); 3291 if (ret) { 3292 kfree(fb_helper); 3293 DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret); 3294 return ret; 3295 } 3296 3297 drm_client_add(&fb_helper->client); 3298 3299 if (!preferred_bpp) 3300 preferred_bpp = dev->mode_config.preferred_depth; 3301 if (!preferred_bpp) 3302 preferred_bpp = 32; 3303 fb_helper->preferred_bpp = preferred_bpp; 3304 3305 ret = drm_fbdev_client_hotplug(&fb_helper->client); 3306 if (ret) 3307 DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret); 3308 3309 return 0; 3310 } 3311 EXPORT_SYMBOL(drm_fbdev_generic_setup); 3312 3313 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 3314 * but the module doesn't depend on any fb console symbols. At least 3315 * attempt to load fbcon to avoid leaving the system without a usable console. 3316 */ 3317 int __init drm_fb_helper_modinit(void) 3318 { 3319 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 3320 const char name[] = "fbcon"; 3321 struct module *fbcon; 3322 3323 mutex_lock(&module_mutex); 3324 fbcon = find_module(name); 3325 mutex_unlock(&module_mutex); 3326 3327 if (!fbcon) 3328 request_module_nowait(name); 3329 #endif 3330 return 0; 3331 } 3332 EXPORT_SYMBOL(drm_fb_helper_modinit); 3333