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