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/kernel.h> 34 #include <linux/sysrq.h> 35 #include <linux/slab.h> 36 #include <linux/module.h> 37 #include <drm/drmP.h> 38 #include <drm/drm_crtc.h> 39 #include <drm/drm_fb_helper.h> 40 #include <drm/drm_crtc_helper.h> 41 #include <drm/drm_atomic.h> 42 #include <drm/drm_atomic_helper.h> 43 44 #include "drm_crtc_helper_internal.h" 45 46 static bool drm_fbdev_emulation = true; 47 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); 48 MODULE_PARM_DESC(fbdev_emulation, 49 "Enable legacy fbdev emulation [default=true]"); 50 51 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; 52 module_param(drm_fbdev_overalloc, int, 0444); 53 MODULE_PARM_DESC(drm_fbdev_overalloc, 54 "Overallocation of the fbdev buffer (%) [default=" 55 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); 56 57 static LINUX_LIST_HEAD(kernel_fb_helper_list); 58 static DEFINE_MUTEX(kernel_fb_helper_lock); 59 60 /** 61 * DOC: fbdev helpers 62 * 63 * The fb helper functions are useful to provide an fbdev on top of a drm kernel 64 * mode setting driver. They can be used mostly independently from the crtc 65 * helper functions used by many drivers to implement the kernel mode setting 66 * interfaces. 67 * 68 * Initialization is done as a four-step process with drm_fb_helper_prepare(), 69 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and 70 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the 71 * default behaviour can override the third step with their own code. 72 * Teardown is done with drm_fb_helper_fini() after the fbdev device is 73 * unregisters using drm_fb_helper_unregister_fbi(). 74 * 75 * At runtime drivers should restore the fbdev console by calling 76 * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose 77 * callback. They should also notify the fb helper code from updates to the 78 * output configuration by calling drm_fb_helper_hotplug_event(). For easier 79 * integration with the output polling code in drm_crtc_helper.c the modeset 80 * code provides a &drm_mode_config_funcs.output_poll_changed callback. 81 * 82 * All other functions exported by the fb helper library can be used to 83 * implement the fbdev driver interface by the driver. 84 * 85 * It is possible, though perhaps somewhat tricky, to implement race-free 86 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() 87 * helper must be called first to initialize the minimum required to make 88 * hotplug detection work. Drivers also need to make sure to properly set up 89 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() 90 * it is safe to enable interrupts and start processing hotplug events. At the 91 * same time, drivers should initialize all modeset objects such as CRTCs, 92 * encoders and connectors. To finish up the fbdev helper initialization, the 93 * drm_fb_helper_init() function is called. To probe for all attached displays 94 * and set up an initial configuration using the detected hardware, drivers 95 * should call drm_fb_helper_single_add_all_connectors() followed by 96 * drm_fb_helper_initial_config(). 97 * 98 * If &drm_framebuffer_funcs.dirty is set, the 99 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will 100 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right 101 * away. This worker then calls the dirty() function ensuring that it will 102 * always run in process context since the fb_*() function could be running in 103 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io 104 * callback it will also schedule dirty_work with the damage collected from the 105 * mmap page writes. 106 */ 107 108 #define drm_fb_helper_for_each_connector(fbh, i__) \ 109 for (({ lockdep_assert_held(&(fbh)->dev->mode_config.mutex); }), \ 110 i__ = 0; i__ < (fbh)->connector_count; i__++) 111 112 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, 113 struct drm_connector *connector) 114 { 115 struct drm_fb_helper_connector *fb_conn; 116 struct drm_fb_helper_connector **temp; 117 unsigned int count; 118 119 if (!drm_fbdev_emulation) 120 return 0; 121 122 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); 123 124 count = fb_helper->connector_count + 1; 125 126 if (count > fb_helper->connector_info_alloc_count) { 127 size_t size = count * sizeof(fb_conn); 128 129 temp = krealloc(fb_helper->connector_info, size, M_DRM, 130 GFP_KERNEL); 131 if (!temp) 132 return -ENOMEM; 133 134 fb_helper->connector_info_alloc_count = count; 135 fb_helper->connector_info = temp; 136 } 137 138 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL); 139 if (!fb_conn) 140 return -ENOMEM; 141 142 drm_connector_get(connector); 143 fb_conn->connector = connector; 144 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn; 145 return 0; 146 } 147 EXPORT_SYMBOL(drm_fb_helper_add_one_connector); 148 149 /** 150 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev 151 * emulation helper 152 * @fb_helper: fbdev initialized with drm_fb_helper_init 153 * 154 * This functions adds all the available connectors for use with the given 155 * fb_helper. This is a separate step to allow drivers to freely assign 156 * connectors to the fbdev, e.g. if some are reserved for special purposes or 157 * not adequate to be used for the fbcon. 158 * 159 * This function is protected against concurrent connector hotadds/removals 160 * using drm_fb_helper_add_one_connector() and 161 * drm_fb_helper_remove_one_connector(). 162 */ 163 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) 164 { 165 struct drm_device *dev = fb_helper->dev; 166 struct drm_connector *connector; 167 struct drm_connector_list_iter conn_iter; 168 int i, ret = 0; 169 170 if (!drm_fbdev_emulation) 171 return 0; 172 173 mutex_lock(&dev->mode_config.mutex); 174 drm_connector_list_iter_begin(dev, &conn_iter); 175 drm_for_each_connector_iter(connector, &conn_iter) { 176 ret = drm_fb_helper_add_one_connector(fb_helper, connector); 177 178 if (ret) 179 goto fail; 180 } 181 goto out; 182 183 fail: 184 drm_fb_helper_for_each_connector(fb_helper, i) { 185 struct drm_fb_helper_connector *fb_helper_connector = 186 fb_helper->connector_info[i]; 187 188 drm_connector_put(fb_helper_connector->connector); 189 190 kfree(fb_helper_connector); 191 fb_helper->connector_info[i] = NULL; 192 } 193 fb_helper->connector_count = 0; 194 out: 195 drm_connector_list_iter_end(&conn_iter); 196 mutex_unlock(&dev->mode_config.mutex); 197 198 return ret; 199 } 200 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); 201 202 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 203 struct drm_connector *connector) 204 { 205 struct drm_fb_helper_connector *fb_helper_connector; 206 int i, j; 207 208 if (!drm_fbdev_emulation) 209 return 0; 210 211 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); 212 213 for (i = 0; i < fb_helper->connector_count; i++) { 214 if (fb_helper->connector_info[i]->connector == connector) 215 break; 216 } 217 218 if (i == fb_helper->connector_count) 219 return -EINVAL; 220 fb_helper_connector = fb_helper->connector_info[i]; 221 drm_connector_put(fb_helper_connector->connector); 222 223 for (j = i + 1; j < fb_helper->connector_count; j++) 224 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; 225 226 fb_helper->connector_count--; 227 kfree(fb_helper_connector); 228 229 return 0; 230 } 231 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); 232 233 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper) 234 { 235 uint16_t *r_base, *g_base, *b_base; 236 int i; 237 238 if (helper->funcs->gamma_get == NULL) 239 return; 240 241 r_base = crtc->gamma_store; 242 g_base = r_base + crtc->gamma_size; 243 b_base = g_base + crtc->gamma_size; 244 245 for (i = 0; i < crtc->gamma_size; i++) 246 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i); 247 } 248 249 #if 0 250 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) 251 { 252 uint16_t *r_base, *g_base, *b_base; 253 254 if (crtc->funcs->gamma_set == NULL) 255 return; 256 257 r_base = crtc->gamma_store; 258 g_base = r_base + crtc->gamma_size; 259 b_base = g_base + crtc->gamma_size; 260 261 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 262 crtc->gamma_size, NULL); 263 } 264 #endif 265 266 /** 267 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter 268 * @info: fbdev registered by the helper 269 */ 270 int drm_fb_helper_debug_enter(struct fb_info *info) 271 { 272 struct drm_fb_helper *helper = info->par; 273 const struct drm_crtc_helper_funcs *funcs; 274 int i; 275 276 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 277 for (i = 0; i < helper->crtc_count; i++) { 278 struct drm_mode_set *mode_set = 279 &helper->crtc_info[i].mode_set; 280 281 if (!mode_set->crtc->enabled) 282 continue; 283 284 funcs = mode_set->crtc->helper_private; 285 if (funcs->mode_set_base_atomic == NULL) 286 continue; 287 288 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) 289 continue; 290 291 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) 292 continue; 293 294 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper); 295 funcs->mode_set_base_atomic(mode_set->crtc, 296 mode_set->fb, 297 mode_set->x, 298 mode_set->y, 299 ENTER_ATOMIC_MODE_SET); 300 } 301 } 302 303 return 0; 304 } 305 EXPORT_SYMBOL(drm_fb_helper_debug_enter); 306 307 #if 0 308 /* Find the real fb for a given fb helper CRTC */ 309 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc) 310 { 311 struct drm_device *dev = crtc->dev; 312 struct drm_crtc *c; 313 314 drm_for_each_crtc(c, dev) { 315 if (crtc->base.id == c->base.id) 316 return c->primary->fb; 317 } 318 319 return NULL; 320 } 321 #endif 322 323 /** 324 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave 325 * @info: fbdev registered by the helper 326 */ 327 int drm_fb_helper_debug_leave(struct fb_info *info) 328 { 329 #if 0 330 struct drm_fb_helper *helper = info->par; 331 struct drm_crtc *crtc; 332 const struct drm_crtc_helper_funcs *funcs; 333 struct drm_framebuffer *fb; 334 int i; 335 336 for (i = 0; i < helper->crtc_count; i++) { 337 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; 338 339 crtc = mode_set->crtc; 340 funcs = crtc->helper_private; 341 fb = drm_mode_config_fb(crtc); 342 343 if (!crtc->enabled) 344 continue; 345 346 if (!fb) { 347 DRM_ERROR("no fb to restore??\n"); 348 continue; 349 } 350 351 if (funcs->mode_set_base_atomic == NULL) 352 continue; 353 354 if (drm_drv_uses_atomic_modeset(crtc->dev)) 355 continue; 356 357 if (drm_drv_uses_atomic_modeset(crtc->dev)) 358 continue; 359 360 drm_fb_helper_restore_lut_atomic(mode_set->crtc); 361 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, 362 crtc->y, LEAVE_ATOMIC_MODE_SET); 363 } 364 #endif 365 366 return 0; 367 } 368 EXPORT_SYMBOL(drm_fb_helper_debug_leave); 369 370 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper) 371 { 372 struct drm_device *dev = fb_helper->dev; 373 struct drm_plane *plane; 374 struct drm_atomic_state *state; 375 int i, ret; 376 unsigned int plane_mask; 377 378 state = drm_atomic_state_alloc(dev); 379 if (!state) 380 return -ENOMEM; 381 382 state->acquire_ctx = dev->mode_config.acquire_ctx; 383 retry: 384 plane_mask = 0; 385 drm_for_each_plane(plane, dev) { 386 struct drm_plane_state *plane_state; 387 388 plane_state = drm_atomic_get_plane_state(state, plane); 389 if (IS_ERR(plane_state)) { 390 ret = PTR_ERR(plane_state); 391 goto fail; 392 } 393 394 plane_state->rotation = DRM_ROTATE_0; 395 396 plane->old_fb = plane->fb; 397 plane_mask |= 1 << drm_plane_index(plane); 398 399 /* disable non-primary: */ 400 if (plane->type == DRM_PLANE_TYPE_PRIMARY) 401 continue; 402 403 ret = __drm_atomic_helper_disable_plane(plane, plane_state); 404 if (ret != 0) 405 goto fail; 406 } 407 408 for (i = 0; i < fb_helper->crtc_count; i++) { 409 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 410 411 ret = __drm_atomic_helper_set_config(mode_set, state); 412 if (ret != 0) 413 goto fail; 414 } 415 416 ret = drm_atomic_commit(state); 417 418 fail: 419 drm_atomic_clean_old_fb(dev, plane_mask, ret); 420 421 if (ret == -EDEADLK) 422 goto backoff; 423 424 drm_atomic_state_put(state); 425 return ret; 426 427 backoff: 428 drm_atomic_state_clear(state); 429 drm_atomic_legacy_backoff(state); 430 431 goto retry; 432 } 433 434 static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper) 435 { 436 struct drm_device *dev = fb_helper->dev; 437 struct drm_plane *plane; 438 int i; 439 440 drm_for_each_plane(plane, dev) { 441 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 442 drm_plane_force_disable(plane); 443 444 if (plane->rotation_property) 445 drm_mode_plane_set_obj_prop(plane, 446 plane->rotation_property, 447 DRM_ROTATE_0); 448 } 449 450 for (i = 0; i < fb_helper->crtc_count; i++) { 451 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 452 struct drm_crtc *crtc = mode_set->crtc; 453 int ret; 454 455 if (crtc->funcs->cursor_set2) { 456 ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0); 457 if (ret) 458 return ret; 459 } else if (crtc->funcs->cursor_set) { 460 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); 461 if (ret) 462 return ret; 463 } 464 465 ret = drm_mode_set_config_internal(mode_set); 466 if (ret) 467 return ret; 468 } 469 470 return 0; 471 } 472 473 static int restore_fbdev_mode(struct drm_fb_helper *fb_helper) 474 { 475 struct drm_device *dev = fb_helper->dev; 476 477 drm_warn_on_modeset_not_all_locked(dev); 478 479 if (drm_drv_uses_atomic_modeset(dev)) 480 return restore_fbdev_mode_atomic(fb_helper); 481 else 482 return restore_fbdev_mode_legacy(fb_helper); 483 } 484 485 /** 486 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration 487 * @fb_helper: fbcon to restore 488 * 489 * This should be called from driver's drm &drm_driver.lastclose callback 490 * when implementing an fbcon on top of kms using this helper. This ensures that 491 * the user isn't greeted with a black screen when e.g. X dies. 492 * 493 * RETURNS: 494 * Zero if everything went ok, negative error code otherwise. 495 */ 496 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 497 { 498 struct drm_device *dev = fb_helper->dev; 499 bool do_delayed; 500 int ret; 501 502 if (!drm_fbdev_emulation) 503 return -ENODEV; 504 505 drm_modeset_lock_all(dev); 506 ret = restore_fbdev_mode(fb_helper); 507 508 do_delayed = fb_helper->delayed_hotplug; 509 if (do_delayed) 510 fb_helper->delayed_hotplug = false; 511 drm_modeset_unlock_all(dev); 512 513 if (do_delayed) 514 drm_fb_helper_hotplug_event(fb_helper); 515 return ret; 516 } 517 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); 518 519 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) 520 { 521 struct drm_device *dev = fb_helper->dev; 522 struct drm_crtc *crtc; 523 int bound = 0, crtcs_bound = 0; 524 525 /* 526 * Sometimes user space wants everything disabled, so don't steal the 527 * display if there's a master. 528 */ 529 if (READ_ONCE(dev->master)) 530 return false; 531 532 drm_for_each_crtc(crtc, dev) { 533 if (crtc->primary->fb) 534 crtcs_bound++; 535 if (crtc->primary->fb == fb_helper->fb) 536 bound++; 537 } 538 539 if (bound < crtcs_bound) 540 return false; 541 542 return true; 543 } 544 545 #ifdef CONFIG_MAGIC_SYSRQ 546 /* 547 * restore fbcon display for all kms driver's using this helper, used for sysrq 548 * and panic handling. 549 */ 550 static bool drm_fb_helper_force_kernel_mode(void) 551 { 552 bool ret, error = false; 553 struct drm_fb_helper *helper; 554 555 if (list_empty(&kernel_fb_helper_list)) 556 return false; 557 558 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 559 struct drm_device *dev = helper->dev; 560 561 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 562 continue; 563 564 drm_modeset_lock_all(dev); 565 ret = restore_fbdev_mode(helper); 566 if (ret) 567 error = true; 568 drm_modeset_unlock_all(dev); 569 } 570 return error; 571 } 572 573 #if 0 574 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) 575 { 576 bool ret; 577 578 ret = drm_fb_helper_force_kernel_mode(); 579 if (ret == true) 580 DRM_ERROR("Failed to restore crtc configuration\n"); 581 } 582 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); 583 584 static void drm_fb_helper_sysrq(int dummy1) 585 { 586 schedule_work(&drm_fb_helper_restore_work); 587 } 588 589 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { 590 .handler = drm_fb_helper_sysrq, 591 .help_msg = "force-fb(V)", 592 .action_msg = "Restore framebuffer console", 593 }; 594 #else 595 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; 596 #endif 597 #endif 598 599 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) 600 { 601 struct drm_fb_helper *fb_helper = info->par; 602 struct drm_device *dev = fb_helper->dev; 603 struct drm_crtc *crtc; 604 struct drm_connector *connector; 605 int i, j; 606 607 /* 608 * For each CRTC in this fb, turn the connectors on/off. 609 */ 610 drm_modeset_lock_all(dev); 611 if (!drm_fb_helper_is_bound(fb_helper)) { 612 drm_modeset_unlock_all(dev); 613 return; 614 } 615 616 for (i = 0; i < fb_helper->crtc_count; i++) { 617 crtc = fb_helper->crtc_info[i].mode_set.crtc; 618 619 if (!crtc->enabled) 620 continue; 621 622 /* Walk the connectors & encoders on this fb turning them on/off */ 623 drm_fb_helper_for_each_connector(fb_helper, j) { 624 connector = fb_helper->connector_info[j]->connector; 625 connector->funcs->dpms(connector, dpms_mode); 626 drm_object_property_set_value(&connector->base, 627 dev->mode_config.dpms_property, dpms_mode); 628 } 629 } 630 drm_modeset_unlock_all(dev); 631 } 632 633 /** 634 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank 635 * @blank: desired blanking state 636 * @info: fbdev registered by the helper 637 */ 638 int drm_fb_helper_blank(int blank, struct fb_info *info) 639 { 640 if (oops_in_progress) 641 return -EBUSY; 642 643 switch (blank) { 644 /* Display: On; HSync: On, VSync: On */ 645 case FB_BLANK_UNBLANK: 646 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); 647 break; 648 #if 0 649 /* Display: Off; HSync: On, VSync: On */ 650 case FB_BLANK_NORMAL: 651 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 652 break; 653 /* Display: Off; HSync: Off, VSync: On */ 654 case FB_BLANK_HSYNC_SUSPEND: 655 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 656 break; 657 /* Display: Off; HSync: On, VSync: Off */ 658 case FB_BLANK_VSYNC_SUSPEND: 659 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); 660 break; 661 #endif 662 /* Display: Off; HSync: Off, VSync: Off */ 663 case FB_BLANK_POWERDOWN: 664 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); 665 break; 666 } 667 return 0; 668 } 669 EXPORT_SYMBOL(drm_fb_helper_blank); 670 671 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper, 672 struct drm_mode_set *modeset) 673 { 674 int i; 675 676 for (i = 0; i < modeset->num_connectors; i++) { 677 drm_connector_put(modeset->connectors[i]); 678 modeset->connectors[i] = NULL; 679 } 680 modeset->num_connectors = 0; 681 682 drm_mode_destroy(helper->dev, modeset->mode); 683 modeset->mode = NULL; 684 685 /* FIXME should hold a ref? */ 686 modeset->fb = NULL; 687 } 688 689 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) 690 { 691 int i; 692 693 for (i = 0; i < helper->connector_count; i++) { 694 drm_connector_put(helper->connector_info[i]->connector); 695 kfree(helper->connector_info[i]); 696 } 697 kfree(helper->connector_info); 698 699 for (i = 0; i < helper->crtc_count; i++) { 700 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set; 701 702 drm_fb_helper_modeset_release(helper, modeset); 703 kfree(modeset->connectors); 704 } 705 kfree(helper->crtc_info); 706 } 707 708 static void drm_fb_helper_resume_worker(struct work_struct *work) 709 { 710 #if 0 711 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 712 resume_work); 713 714 console_lock(); 715 fb_set_suspend(helper->fbdev, 0); 716 console_unlock(); 717 #endif 718 } 719 720 static void drm_fb_helper_dirty_work(struct work_struct *work) 721 { 722 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 723 dirty_work); 724 struct drm_clip_rect *clip = &helper->dirty_clip; 725 struct drm_clip_rect clip_copy; 726 unsigned long flags; 727 728 spin_lock_irqsave(&helper->dirty_lock, flags); 729 clip_copy = *clip; 730 clip->x1 = clip->y1 = ~0; 731 clip->x2 = clip->y2 = 0; 732 spin_unlock_irqrestore(&helper->dirty_lock, flags); 733 734 /* call dirty callback only when it has been really touched */ 735 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) 736 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1); 737 } 738 739 /** 740 * drm_fb_helper_prepare - setup a drm_fb_helper structure 741 * @dev: DRM device 742 * @helper: driver-allocated fbdev helper structure to set up 743 * @funcs: pointer to structure of functions associate with this helper 744 * 745 * Sets up the bare minimum to make the framebuffer helper usable. This is 746 * useful to implement race-free initialization of the polling helpers. 747 */ 748 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 749 const struct drm_fb_helper_funcs *funcs) 750 { 751 INIT_LIST_HEAD(&helper->kernel_fb_list); 752 lockinit(&helper->dirty_lock, "drm_fb_helper dirty_lock", 0, 0); 753 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); 754 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); 755 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; 756 helper->funcs = funcs; 757 helper->dev = dev; 758 } 759 EXPORT_SYMBOL(drm_fb_helper_prepare); 760 761 /** 762 * drm_fb_helper_init - initialize a &struct drm_fb_helper 763 * @dev: drm device 764 * @fb_helper: driver-allocated fbdev helper structure to initialize 765 * @max_conn_count: max connector count 766 * 767 * This allocates the structures for the fbdev helper with the given limits. 768 * Note that this won't yet touch the hardware (through the driver interfaces) 769 * nor register the fbdev. This is only done in drm_fb_helper_initial_config() 770 * to allow driver writes more control over the exact init sequence. 771 * 772 * Drivers must call drm_fb_helper_prepare() before calling this function. 773 * 774 * RETURNS: 775 * Zero if everything went ok, nonzero otherwise. 776 */ 777 int drm_fb_helper_init(struct drm_device *dev, 778 struct drm_fb_helper *fb_helper, 779 int max_conn_count) 780 { 781 struct drm_crtc *crtc; 782 struct drm_mode_config *config = &dev->mode_config; 783 int i; 784 785 if (!drm_fbdev_emulation) 786 return 0; 787 788 if (!max_conn_count) 789 return -EINVAL; 790 791 fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); 792 if (!fb_helper->crtc_info) 793 return -ENOMEM; 794 795 fb_helper->crtc_count = config->num_crtc; 796 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); 797 if (!fb_helper->connector_info) { 798 kfree(fb_helper->crtc_info); 799 return -ENOMEM; 800 } 801 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; 802 fb_helper->connector_count = 0; 803 804 for (i = 0; i < fb_helper->crtc_count; i++) { 805 fb_helper->crtc_info[i].mode_set.connectors = 806 kcalloc(max_conn_count, 807 sizeof(struct drm_connector *), 808 GFP_KERNEL); 809 810 if (!fb_helper->crtc_info[i].mode_set.connectors) 811 goto out_free; 812 fb_helper->crtc_info[i].mode_set.num_connectors = 0; 813 } 814 815 i = 0; 816 drm_for_each_crtc(crtc, dev) { 817 fb_helper->crtc_info[i].mode_set.crtc = crtc; 818 i++; 819 } 820 821 return 0; 822 out_free: 823 drm_fb_helper_crtc_free(fb_helper); 824 return -ENOMEM; 825 } 826 EXPORT_SYMBOL(drm_fb_helper_init); 827 828 /** 829 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members 830 * @fb_helper: driver-allocated fbdev helper 831 * 832 * A helper to alloc fb_info and the members cmap and apertures. Called 833 * by the driver within the fb_probe fb_helper callback function. Drivers do not 834 * need to release the allocated fb_info structure themselves, this is 835 * automatically done when calling drm_fb_helper_fini(). 836 * 837 * RETURNS: 838 * fb_info pointer if things went okay, pointer containing error code 839 * otherwise 840 */ 841 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) 842 { 843 struct device *dev = fb_helper->dev->dev; 844 struct fb_info *info; 845 #if 0 846 int ret; 847 #endif 848 849 info = framebuffer_alloc(0, dev); 850 if (!info) 851 return ERR_PTR(-ENOMEM); 852 853 #if 0 854 ret = fb_alloc_cmap(&info->cmap, 256, 0); 855 if (ret) 856 goto err_release; 857 858 info->apertures = alloc_apertures(1); 859 if (!info->apertures) { 860 ret = -ENOMEM; 861 goto err_free_cmap; 862 } 863 #endif 864 865 fb_helper->fbdev = info; 866 867 return info; 868 869 #if 0 870 err_free_cmap: 871 fb_dealloc_cmap(&info->cmap); 872 err_release: 873 framebuffer_release(info); 874 return ERR_PTR(ret); 875 #endif 876 } 877 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); 878 879 /** 880 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device 881 * @fb_helper: driver-allocated fbdev helper 882 * 883 * A wrapper around unregister_framebuffer, to release the fb_info 884 * framebuffer device. This must be called before releasing all resources for 885 * @fb_helper by calling drm_fb_helper_fini(). 886 */ 887 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 888 { 889 if (fb_helper && fb_helper->fbdev) 890 unregister_framebuffer(fb_helper->fbdev); 891 } 892 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); 893 894 /** 895 * drm_fb_helper_fini - finialize a &struct drm_fb_helper 896 * @fb_helper: driver-allocated fbdev helper 897 * 898 * This cleans up all remaining resources associated with @fb_helper. Must be 899 * called after drm_fb_helper_unlink_fbi() was called. 900 */ 901 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 902 { 903 struct fb_info *info; 904 905 if (!drm_fbdev_emulation || !fb_helper) 906 return; 907 908 info = fb_helper->fbdev; 909 if (info) { 910 #if 0 911 if (info->cmap.len) 912 fb_dealloc_cmap(&info->cmap); 913 #endif 914 framebuffer_release(info); 915 } 916 fb_helper->fbdev = NULL; 917 918 cancel_work_sync(&fb_helper->resume_work); 919 cancel_work_sync(&fb_helper->dirty_work); 920 921 mutex_lock(&kernel_fb_helper_lock); 922 if (!list_empty(&fb_helper->kernel_fb_list)) { 923 list_del(&fb_helper->kernel_fb_list); 924 #if 0 925 if (list_empty(&kernel_fb_helper_list)) 926 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 927 #endif 928 } 929 mutex_unlock(&kernel_fb_helper_lock); 930 931 drm_fb_helper_crtc_free(fb_helper); 932 933 } 934 EXPORT_SYMBOL(drm_fb_helper_fini); 935 936 #if 0 937 /** 938 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer 939 * @fb_helper: driver-allocated fbdev helper 940 * 941 * A wrapper around unlink_framebuffer implemented by fbdev core 942 */ 943 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) 944 { 945 if (fb_helper && fb_helper->fbdev) 946 unlink_framebuffer(fb_helper->fbdev); 947 } 948 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); 949 950 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, 951 u32 width, u32 height) 952 { 953 struct drm_fb_helper *helper = info->par; 954 struct drm_clip_rect *clip = &helper->dirty_clip; 955 unsigned long flags; 956 957 if (!helper->fb->funcs->dirty) 958 return; 959 960 spin_lock_irqsave(&helper->dirty_lock, flags); 961 clip->x1 = min_t(u32, clip->x1, x); 962 clip->y1 = min_t(u32, clip->y1, y); 963 clip->x2 = max_t(u32, clip->x2, x + width); 964 clip->y2 = max_t(u32, clip->y2, y + height); 965 spin_unlock_irqrestore(&helper->dirty_lock, flags); 966 967 schedule_work(&helper->dirty_work); 968 } 969 970 /** 971 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function 972 * @info: fb_info struct pointer 973 * @pagelist: list of dirty mmap framebuffer pages 974 * 975 * This function is used as the &fb_deferred_io.deferred_io 976 * callback function for flushing the fbdev mmap writes. 977 */ 978 void drm_fb_helper_deferred_io(struct fb_info *info, 979 struct list_head *pagelist) 980 { 981 unsigned long start, end, min, max; 982 struct page *page; 983 u32 y1, y2; 984 985 min = ULONG_MAX; 986 max = 0; 987 list_for_each_entry(page, pagelist, lru) { 988 start = page->index << PAGE_SHIFT; 989 end = start + PAGE_SIZE - 1; 990 min = min(min, start); 991 max = max(max, end); 992 } 993 994 if (min < max) { 995 y1 = min / info->fix.line_length; 996 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), 997 info->var.yres); 998 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); 999 } 1000 } 1001 EXPORT_SYMBOL(drm_fb_helper_deferred_io); 1002 1003 /** 1004 * drm_fb_helper_sys_read - wrapper around fb_sys_read 1005 * @info: fb_info struct pointer 1006 * @buf: userspace buffer to read from framebuffer memory 1007 * @count: number of bytes to read from framebuffer memory 1008 * @ppos: read offset within framebuffer memory 1009 * 1010 * A wrapper around fb_sys_read implemented by fbdev core 1011 */ 1012 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 1013 size_t count, loff_t *ppos) 1014 { 1015 return fb_sys_read(info, buf, count, ppos); 1016 } 1017 EXPORT_SYMBOL(drm_fb_helper_sys_read); 1018 1019 /** 1020 * drm_fb_helper_sys_write - wrapper around fb_sys_write 1021 * @info: fb_info struct pointer 1022 * @buf: userspace buffer to write to framebuffer memory 1023 * @count: number of bytes to write to framebuffer memory 1024 * @ppos: write offset within framebuffer memory 1025 * 1026 * A wrapper around fb_sys_write implemented by fbdev core 1027 */ 1028 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 1029 size_t count, loff_t *ppos) 1030 { 1031 ssize_t ret; 1032 1033 ret = fb_sys_write(info, buf, count, ppos); 1034 if (ret > 0) 1035 drm_fb_helper_dirty(info, 0, 0, info->var.xres, 1036 info->var.yres); 1037 1038 return ret; 1039 } 1040 EXPORT_SYMBOL(drm_fb_helper_sys_write); 1041 1042 /** 1043 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect 1044 * @info: fbdev registered by the helper 1045 * @rect: info about rectangle to fill 1046 * 1047 * A wrapper around sys_fillrect implemented by fbdev core 1048 */ 1049 void drm_fb_helper_sys_fillrect(struct fb_info *info, 1050 const struct fb_fillrect *rect) 1051 { 1052 sys_fillrect(info, rect); 1053 drm_fb_helper_dirty(info, rect->dx, rect->dy, 1054 rect->width, rect->height); 1055 } 1056 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); 1057 1058 /** 1059 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea 1060 * @info: fbdev registered by the helper 1061 * @area: info about area to copy 1062 * 1063 * A wrapper around sys_copyarea implemented by fbdev core 1064 */ 1065 void drm_fb_helper_sys_copyarea(struct fb_info *info, 1066 const struct fb_copyarea *area) 1067 { 1068 sys_copyarea(info, area); 1069 drm_fb_helper_dirty(info, area->dx, area->dy, 1070 area->width, area->height); 1071 } 1072 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); 1073 1074 /** 1075 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit 1076 * @info: fbdev registered by the helper 1077 * @image: info about image to blit 1078 * 1079 * A wrapper around sys_imageblit implemented by fbdev core 1080 */ 1081 void drm_fb_helper_sys_imageblit(struct fb_info *info, 1082 const struct fb_image *image) 1083 { 1084 sys_imageblit(info, image); 1085 drm_fb_helper_dirty(info, image->dx, image->dy, 1086 image->width, image->height); 1087 } 1088 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); 1089 1090 /** 1091 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect 1092 * @info: fbdev registered by the helper 1093 * @rect: info about rectangle to fill 1094 * 1095 * A wrapper around cfb_imageblit implemented by fbdev core 1096 */ 1097 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 1098 const struct fb_fillrect *rect) 1099 { 1100 cfb_fillrect(info, rect); 1101 drm_fb_helper_dirty(info, rect->dx, rect->dy, 1102 rect->width, rect->height); 1103 } 1104 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); 1105 1106 /** 1107 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea 1108 * @info: fbdev registered by the helper 1109 * @area: info about area to copy 1110 * 1111 * A wrapper around cfb_copyarea implemented by fbdev core 1112 */ 1113 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 1114 const struct fb_copyarea *area) 1115 { 1116 cfb_copyarea(info, area); 1117 drm_fb_helper_dirty(info, area->dx, area->dy, 1118 area->width, area->height); 1119 } 1120 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); 1121 1122 /** 1123 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit 1124 * @info: fbdev registered by the helper 1125 * @image: info about image to blit 1126 * 1127 * A wrapper around cfb_imageblit implemented by fbdev core 1128 */ 1129 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 1130 const struct fb_image *image) 1131 { 1132 cfb_imageblit(info, image); 1133 drm_fb_helper_dirty(info, image->dx, image->dy, 1134 image->width, image->height); 1135 } 1136 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); 1137 1138 /** 1139 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 1140 * @fb_helper: driver-allocated fbdev helper 1141 * @suspend: whether to suspend or resume 1142 * 1143 * A wrapper around fb_set_suspend implemented by fbdev core. 1144 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take 1145 * the lock yourself 1146 */ 1147 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) 1148 { 1149 if (fb_helper && fb_helper->fbdev) 1150 fb_set_suspend(fb_helper->fbdev, suspend); 1151 } 1152 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 1153 1154 /** 1155 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also 1156 * takes the console lock 1157 * @fb_helper: driver-allocated fbdev helper 1158 * @suspend: whether to suspend or resume 1159 * 1160 * A wrapper around fb_set_suspend() that takes the console lock. If the lock 1161 * isn't available on resume, a worker is tasked with waiting for the lock 1162 * to become available. The console lock can be pretty contented on resume 1163 * due to all the printk activity. 1164 * 1165 * This function can be called multiple times with the same state since 1166 * &fb_info.state is checked to see if fbdev is running or not before locking. 1167 * 1168 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. 1169 */ 1170 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 1171 bool suspend) 1172 { 1173 if (!fb_helper || !fb_helper->fbdev) 1174 return; 1175 1176 /* make sure there's no pending/ongoing resume */ 1177 flush_work(&fb_helper->resume_work); 1178 1179 if (suspend) { 1180 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) 1181 return; 1182 1183 console_lock(); 1184 1185 } else { 1186 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) 1187 return; 1188 1189 if (!console_trylock()) { 1190 schedule_work(&fb_helper->resume_work); 1191 return; 1192 } 1193 } 1194 1195 fb_set_suspend(fb_helper->fbdev, suspend); 1196 console_unlock(); 1197 } 1198 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); 1199 1200 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, 1201 u16 blue, u16 regno, struct fb_info *info) 1202 { 1203 struct drm_fb_helper *fb_helper = info->par; 1204 struct drm_framebuffer *fb = fb_helper->fb; 1205 1206 if (info->fix.visual == FB_VISUAL_TRUECOLOR) { 1207 u32 *palette; 1208 u32 value; 1209 /* place color in psuedopalette */ 1210 if (regno > 16) 1211 return -EINVAL; 1212 palette = (u32 *)info->pseudo_palette; 1213 red >>= (16 - info->var.red.length); 1214 green >>= (16 - info->var.green.length); 1215 blue >>= (16 - info->var.blue.length); 1216 value = (red << info->var.red.offset) | 1217 (green << info->var.green.offset) | 1218 (blue << info->var.blue.offset); 1219 if (info->var.transp.length > 0) { 1220 u32 mask = (1 << info->var.transp.length) - 1; 1221 1222 mask <<= info->var.transp.offset; 1223 value |= mask; 1224 } 1225 palette[regno] = value; 1226 return 0; 1227 } 1228 1229 /* 1230 * The driver really shouldn't advertise pseudo/directcolor 1231 * visuals if it can't deal with the palette. 1232 */ 1233 if (WARN_ON(!fb_helper->funcs->gamma_set || 1234 !fb_helper->funcs->gamma_get)) 1235 return -EINVAL; 1236 1237 WARN_ON(fb->format->cpp[0] != 1); 1238 1239 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno); 1240 1241 return 0; 1242 } 1243 #endif 1244 1245 /** 1246 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap 1247 * @cmap: cmap to set 1248 * @info: fbdev registered by the helper 1249 */ 1250 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 1251 { 1252 #if 0 1253 struct drm_fb_helper *fb_helper = info->par; 1254 struct drm_device *dev = fb_helper->dev; 1255 const struct drm_crtc_helper_funcs *crtc_funcs; 1256 u16 *red, *green, *blue, *transp; 1257 struct drm_crtc *crtc; 1258 int i, j, rc = 0; 1259 int start; 1260 1261 if (oops_in_progress) 1262 return -EBUSY; 1263 1264 drm_modeset_lock_all(dev); 1265 if (!drm_fb_helper_is_bound(fb_helper)) { 1266 drm_modeset_unlock_all(dev); 1267 return -EBUSY; 1268 } 1269 1270 for (i = 0; i < fb_helper->crtc_count; i++) { 1271 crtc = fb_helper->crtc_info[i].mode_set.crtc; 1272 crtc_funcs = crtc->helper_private; 1273 1274 red = cmap->red; 1275 green = cmap->green; 1276 blue = cmap->blue; 1277 transp = cmap->transp; 1278 start = cmap->start; 1279 1280 for (j = 0; j < cmap->len; j++) { 1281 u16 hred, hgreen, hblue, htransp = 0xffff; 1282 1283 hred = *red++; 1284 hgreen = *green++; 1285 hblue = *blue++; 1286 1287 if (transp) 1288 htransp = *transp++; 1289 1290 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); 1291 if (rc) 1292 goto out; 1293 } 1294 if (crtc_funcs->load_lut) 1295 crtc_funcs->load_lut(crtc); 1296 } 1297 out: 1298 drm_modeset_unlock_all(dev); 1299 return rc; 1300 #endif 1301 return 0; 1302 } 1303 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1304 1305 /** 1306 * drm_fb_helper_ioctl - legacy ioctl implementation 1307 * @info: fbdev registered by the helper 1308 * @cmd: ioctl command 1309 * @arg: ioctl argument 1310 * 1311 * A helper to implement the standard fbdev ioctl. Only 1312 * FBIO_WAITFORVSYNC is implemented for now. 1313 */ 1314 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 1315 unsigned long arg) 1316 { 1317 struct drm_fb_helper *fb_helper = info->par; 1318 struct drm_device *dev = fb_helper->dev; 1319 #if 0 1320 struct drm_mode_set *mode_set; 1321 struct drm_crtc *crtc; 1322 #endif 1323 int ret = 0; 1324 1325 mutex_lock(&dev->mode_config.mutex); 1326 if (!drm_fb_helper_is_bound(fb_helper)) { 1327 ret = -EBUSY; 1328 goto unlock; 1329 } 1330 1331 switch (cmd) { 1332 #if 0 1333 case FBIO_WAITFORVSYNC: 1334 /* 1335 * Only consider the first CRTC. 1336 * 1337 * This ioctl is supposed to take the CRTC number as 1338 * an argument, but in fbdev times, what that number 1339 * was supposed to be was quite unclear, different 1340 * drivers were passing that argument differently 1341 * (some by reference, some by value), and most of the 1342 * userspace applications were just hardcoding 0 as an 1343 * argument. 1344 * 1345 * The first CRTC should be the integrated panel on 1346 * most drivers, so this is the best choice we can 1347 * make. If we're not smart enough here, one should 1348 * just consider switch the userspace to KMS. 1349 */ 1350 mode_set = &fb_helper->crtc_info[0].mode_set; 1351 crtc = mode_set->crtc; 1352 1353 /* 1354 * Only wait for a vblank event if the CRTC is 1355 * enabled, otherwise just don't do anythintg, 1356 * not even report an error. 1357 */ 1358 ret = drm_crtc_vblank_get(crtc); 1359 if (!ret) { 1360 drm_crtc_wait_one_vblank(crtc); 1361 drm_crtc_vblank_put(crtc); 1362 } 1363 1364 ret = 0; 1365 goto unlock; 1366 #endif 1367 default: 1368 ret = -ENOTTY; 1369 } 1370 1371 unlock: 1372 mutex_unlock(&dev->mode_config.mutex); 1373 return ret; 1374 } 1375 EXPORT_SYMBOL(drm_fb_helper_ioctl); 1376 1377 /** 1378 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var 1379 * @var: screeninfo to check 1380 * @info: fbdev registered by the helper 1381 */ 1382 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1383 struct fb_info *info) 1384 { 1385 #if 0 1386 struct drm_fb_helper *fb_helper = info->par; 1387 struct drm_framebuffer *fb = fb_helper->fb; 1388 int depth; 1389 1390 if (var->pixclock != 0 || in_dbg_master()) 1391 return -EINVAL; 1392 1393 /* 1394 * Changes struct fb_var_screeninfo are currently not pushed back 1395 * to KMS, hence fail if different settings are requested. 1396 */ 1397 if (var->bits_per_pixel != fb->format->cpp[0] * 8 || 1398 var->xres > fb->width || var->yres > fb->height || 1399 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1400 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb " 1401 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1402 var->xres, var->yres, var->bits_per_pixel, 1403 var->xres_virtual, var->yres_virtual, 1404 fb->width, fb->height, fb->format->cpp[0] * 8); 1405 return -EINVAL; 1406 } 1407 1408 switch (var->bits_per_pixel) { 1409 case 16: 1410 depth = (var->green.length == 6) ? 16 : 15; 1411 break; 1412 case 32: 1413 depth = (var->transp.length > 0) ? 32 : 24; 1414 break; 1415 default: 1416 depth = var->bits_per_pixel; 1417 break; 1418 } 1419 1420 switch (depth) { 1421 case 8: 1422 var->red.offset = 0; 1423 var->green.offset = 0; 1424 var->blue.offset = 0; 1425 var->red.length = 8; 1426 var->green.length = 8; 1427 var->blue.length = 8; 1428 var->transp.length = 0; 1429 var->transp.offset = 0; 1430 break; 1431 case 15: 1432 var->red.offset = 10; 1433 var->green.offset = 5; 1434 var->blue.offset = 0; 1435 var->red.length = 5; 1436 var->green.length = 5; 1437 var->blue.length = 5; 1438 var->transp.length = 1; 1439 var->transp.offset = 15; 1440 break; 1441 case 16: 1442 var->red.offset = 11; 1443 var->green.offset = 5; 1444 var->blue.offset = 0; 1445 var->red.length = 5; 1446 var->green.length = 6; 1447 var->blue.length = 5; 1448 var->transp.length = 0; 1449 var->transp.offset = 0; 1450 break; 1451 case 24: 1452 var->red.offset = 16; 1453 var->green.offset = 8; 1454 var->blue.offset = 0; 1455 var->red.length = 8; 1456 var->green.length = 8; 1457 var->blue.length = 8; 1458 var->transp.length = 0; 1459 var->transp.offset = 0; 1460 break; 1461 case 32: 1462 var->red.offset = 16; 1463 var->green.offset = 8; 1464 var->blue.offset = 0; 1465 var->red.length = 8; 1466 var->green.length = 8; 1467 var->blue.length = 8; 1468 var->transp.length = 8; 1469 var->transp.offset = 24; 1470 break; 1471 default: 1472 return -EINVAL; 1473 } 1474 #endif 1475 return 0; 1476 } 1477 EXPORT_SYMBOL(drm_fb_helper_check_var); 1478 1479 /** 1480 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par 1481 * @info: fbdev registered by the helper 1482 * 1483 * This will let fbcon do the mode init and is called at initialization time by 1484 * the fbdev core when registering the driver, and later on through the hotplug 1485 * callback. 1486 */ 1487 int drm_fb_helper_set_par(struct fb_info *info) 1488 { 1489 struct drm_fb_helper *fb_helper = info->par; 1490 #if 0 1491 struct fb_var_screeninfo *var = &info->var; 1492 #endif 1493 1494 if (oops_in_progress) 1495 return -EBUSY; 1496 1497 #if 0 1498 if (var->pixclock != 0) { 1499 DRM_ERROR("PIXEL CLOCK SET\n"); 1500 return -EINVAL; 1501 } 1502 #endif 1503 1504 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 1505 1506 return 0; 1507 } 1508 EXPORT_SYMBOL(drm_fb_helper_set_par); 1509 1510 #if 0 1511 static int pan_display_atomic(struct fb_var_screeninfo *var, 1512 struct fb_info *info) 1513 { 1514 struct drm_fb_helper *fb_helper = info->par; 1515 struct drm_device *dev = fb_helper->dev; 1516 struct drm_atomic_state *state; 1517 struct drm_plane *plane; 1518 int i, ret; 1519 unsigned int plane_mask; 1520 1521 state = drm_atomic_state_alloc(dev); 1522 if (!state) 1523 return -ENOMEM; 1524 1525 state->acquire_ctx = dev->mode_config.acquire_ctx; 1526 retry: 1527 plane_mask = 0; 1528 for (i = 0; i < fb_helper->crtc_count; i++) { 1529 struct drm_mode_set *mode_set; 1530 1531 mode_set = &fb_helper->crtc_info[i].mode_set; 1532 1533 mode_set->x = var->xoffset; 1534 mode_set->y = var->yoffset; 1535 1536 ret = __drm_atomic_helper_set_config(mode_set, state); 1537 if (ret != 0) 1538 goto fail; 1539 1540 plane = mode_set->crtc->primary; 1541 plane_mask |= (1 << drm_plane_index(plane)); 1542 plane->old_fb = plane->fb; 1543 } 1544 1545 ret = drm_atomic_commit(state); 1546 if (ret != 0) 1547 goto fail; 1548 1549 info->var.xoffset = var->xoffset; 1550 info->var.yoffset = var->yoffset; 1551 1552 fail: 1553 drm_atomic_clean_old_fb(dev, plane_mask, ret); 1554 1555 if (ret == -EDEADLK) 1556 goto backoff; 1557 1558 drm_atomic_state_put(state); 1559 return ret; 1560 1561 backoff: 1562 drm_atomic_state_clear(state); 1563 drm_atomic_legacy_backoff(state); 1564 1565 goto retry; 1566 } 1567 1568 static int pan_display_legacy(struct fb_var_screeninfo *var, 1569 struct fb_info *info) 1570 { 1571 struct drm_fb_helper *fb_helper = info->par; 1572 struct drm_mode_set *modeset; 1573 int ret = 0; 1574 int i; 1575 1576 for (i = 0; i < fb_helper->crtc_count; i++) { 1577 modeset = &fb_helper->crtc_info[i].mode_set; 1578 1579 modeset->x = var->xoffset; 1580 modeset->y = var->yoffset; 1581 1582 if (modeset->num_connectors) { 1583 ret = drm_mode_set_config_internal(modeset); 1584 if (!ret) { 1585 info->var.xoffset = var->xoffset; 1586 info->var.yoffset = var->yoffset; 1587 } 1588 } 1589 } 1590 1591 return ret; 1592 } 1593 #endif 1594 1595 /** 1596 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display 1597 * @var: updated screen information 1598 * @info: fbdev registered by the helper 1599 */ 1600 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1601 struct fb_info *info) 1602 { 1603 #if 0 1604 struct drm_fb_helper *fb_helper = info->par; 1605 struct drm_device *dev = fb_helper->dev; 1606 int ret; 1607 1608 if (oops_in_progress) 1609 return -EBUSY; 1610 1611 drm_modeset_lock_all(dev); 1612 if (!drm_fb_helper_is_bound(fb_helper)) { 1613 drm_modeset_unlock_all(dev); 1614 return -EBUSY; 1615 } 1616 1617 if (drm_drv_uses_atomic_modeset(dev)) 1618 ret = pan_display_atomic(var, info); 1619 else 1620 ret = pan_display_legacy(var, info); 1621 drm_modeset_unlock_all(dev); 1622 1623 return ret; 1624 #endif 1625 return 0; 1626 } 1627 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1628 1629 /* 1630 * Allocates the backing storage and sets up the fbdev info structure through 1631 * the ->fb_probe callback and then registers the fbdev and sets up the panic 1632 * notifier. 1633 */ 1634 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1635 int preferred_bpp) 1636 { 1637 int ret = 0; 1638 int crtc_count = 0; 1639 int i; 1640 struct drm_fb_helper_surface_size sizes; 1641 int gamma_size = 0; 1642 #ifdef __DragonFly__ 1643 int kms_console = 1; 1644 #endif 1645 1646 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1647 sizes.surface_depth = 24; 1648 sizes.surface_bpp = 32; 1649 sizes.fb_width = (u32)-1; 1650 sizes.fb_height = (u32)-1; 1651 1652 /* if driver picks 8 or 16 by default use that for both depth/bpp */ 1653 if (preferred_bpp != sizes.surface_bpp) 1654 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1655 1656 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1657 drm_fb_helper_for_each_connector(fb_helper, i) { 1658 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 1659 struct drm_cmdline_mode *cmdline_mode; 1660 1661 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1662 1663 if (cmdline_mode->bpp_specified) { 1664 switch (cmdline_mode->bpp) { 1665 case 8: 1666 sizes.surface_depth = sizes.surface_bpp = 8; 1667 break; 1668 case 15: 1669 sizes.surface_depth = 15; 1670 sizes.surface_bpp = 16; 1671 break; 1672 case 16: 1673 sizes.surface_depth = sizes.surface_bpp = 16; 1674 break; 1675 case 24: 1676 sizes.surface_depth = sizes.surface_bpp = 24; 1677 break; 1678 case 32: 1679 sizes.surface_depth = 24; 1680 sizes.surface_bpp = 32; 1681 break; 1682 } 1683 break; 1684 } 1685 } 1686 1687 crtc_count = 0; 1688 for (i = 0; i < fb_helper->crtc_count; i++) { 1689 struct drm_display_mode *desired_mode; 1690 struct drm_mode_set *mode_set; 1691 int x, y, j; 1692 /* in case of tile group, are we the last tile vert or horiz? 1693 * If no tile group you are always the last one both vertically 1694 * and horizontally 1695 */ 1696 bool lastv = true, lasth = true; 1697 1698 desired_mode = fb_helper->crtc_info[i].desired_mode; 1699 mode_set = &fb_helper->crtc_info[i].mode_set; 1700 1701 if (!desired_mode) 1702 continue; 1703 1704 crtc_count++; 1705 1706 x = fb_helper->crtc_info[i].x; 1707 y = fb_helper->crtc_info[i].y; 1708 1709 if (gamma_size == 0) 1710 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; 1711 1712 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); 1713 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); 1714 1715 for (j = 0; j < mode_set->num_connectors; j++) { 1716 struct drm_connector *connector = mode_set->connectors[j]; 1717 1718 if (connector->has_tile) { 1719 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1720 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1721 /* cloning to multiple tiles is just crazy-talk, so: */ 1722 break; 1723 } 1724 } 1725 1726 if (lasth) 1727 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); 1728 if (lastv) 1729 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); 1730 } 1731 1732 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1733 /* 1734 * hmm everyone went away - assume VGA cable just fell out 1735 * and will come back later. 1736 */ 1737 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); 1738 sizes.fb_width = sizes.surface_width = 1024; 1739 sizes.fb_height = sizes.surface_height = 768; 1740 } 1741 1742 /* Handle our overallocation */ 1743 sizes.surface_height *= drm_fbdev_overalloc; 1744 sizes.surface_height /= 100; 1745 1746 /* push down into drivers */ 1747 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1748 if (ret < 0) 1749 return ret; 1750 1751 /* 1752 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug 1753 * events, but at init time drm_setup_crtcs needs to be called before 1754 * the fb is allocated (since we need to figure out the desired size of 1755 * the fb before we can allocate it ...). Hence we need to fix things up 1756 * here again. 1757 */ 1758 for (i = 0; i < fb_helper->crtc_count; i++) 1759 if (fb_helper->crtc_info[i].mode_set.num_connectors) 1760 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; 1761 1762 #ifdef __DragonFly__ 1763 TUNABLE_INT_FETCH("kern.kms_console", &kms_console); 1764 if (kms_console) { 1765 if (register_framebuffer(fb_helper->fbdev) < 0) 1766 return -EINVAL; 1767 } 1768 #endif 1769 1770 return 0; 1771 } 1772 1773 #if 0 1774 /** 1775 * drm_fb_helper_fill_fix - initializes fixed fbdev information 1776 * @info: fbdev registered by the helper 1777 * @pitch: desired pitch 1778 * @depth: desired depth 1779 * 1780 * Helper to fill in the fixed fbdev information useful for a non-accelerated 1781 * fbdev emulations. Drivers which support acceleration methods which impose 1782 * additional constraints need to set up their own limits. 1783 * 1784 * Drivers should call this (or their equivalent setup code) from their 1785 * &drm_fb_helper_funcs.fb_probe callback. 1786 */ 1787 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1788 uint32_t depth) 1789 { 1790 info->fix.type = FB_TYPE_PACKED_PIXELS; 1791 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1792 FB_VISUAL_TRUECOLOR; 1793 info->fix.mmio_start = 0; 1794 info->fix.mmio_len = 0; 1795 info->fix.type_aux = 0; 1796 info->fix.xpanstep = 1; /* doing it in hw */ 1797 info->fix.ypanstep = 1; /* doing it in hw */ 1798 info->fix.ywrapstep = 0; 1799 info->fix.accel = FB_ACCEL_NONE; 1800 1801 info->fix.line_length = pitch; 1802 } 1803 EXPORT_SYMBOL(drm_fb_helper_fill_fix); 1804 1805 /** 1806 * drm_fb_helper_fill_var - initalizes variable fbdev information 1807 * @info: fbdev instance to set up 1808 * @fb_helper: fb helper instance to use as template 1809 * @fb_width: desired fb width 1810 * @fb_height: desired fb height 1811 * 1812 * Sets up the variable fbdev metainformation from the given fb helper instance 1813 * and the drm framebuffer allocated in &drm_fb_helper.fb. 1814 * 1815 * Drivers should call this (or their equivalent setup code) from their 1816 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev 1817 * backing storage framebuffer. 1818 */ 1819 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 1820 uint32_t fb_width, uint32_t fb_height) 1821 { 1822 struct drm_framebuffer *fb = fb_helper->fb; 1823 1824 info->pseudo_palette = fb_helper->pseudo_palette; 1825 info->var.xres_virtual = fb->width; 1826 info->var.yres_virtual = fb->height; 1827 info->var.bits_per_pixel = fb->format->cpp[0] * 8; 1828 info->var.accel_flags = FB_ACCELF_TEXT; 1829 info->var.xoffset = 0; 1830 info->var.yoffset = 0; 1831 info->var.activate = FB_ACTIVATE_NOW; 1832 info->var.height = -1; 1833 info->var.width = -1; 1834 1835 switch (fb->format->depth) { 1836 case 8: 1837 info->var.red.offset = 0; 1838 info->var.green.offset = 0; 1839 info->var.blue.offset = 0; 1840 info->var.red.length = 8; /* 8bit DAC */ 1841 info->var.green.length = 8; 1842 info->var.blue.length = 8; 1843 info->var.transp.offset = 0; 1844 info->var.transp.length = 0; 1845 break; 1846 case 15: 1847 info->var.red.offset = 10; 1848 info->var.green.offset = 5; 1849 info->var.blue.offset = 0; 1850 info->var.red.length = 5; 1851 info->var.green.length = 5; 1852 info->var.blue.length = 5; 1853 info->var.transp.offset = 15; 1854 info->var.transp.length = 1; 1855 break; 1856 case 16: 1857 info->var.red.offset = 11; 1858 info->var.green.offset = 5; 1859 info->var.blue.offset = 0; 1860 info->var.red.length = 5; 1861 info->var.green.length = 6; 1862 info->var.blue.length = 5; 1863 info->var.transp.offset = 0; 1864 break; 1865 case 24: 1866 info->var.red.offset = 16; 1867 info->var.green.offset = 8; 1868 info->var.blue.offset = 0; 1869 info->var.red.length = 8; 1870 info->var.green.length = 8; 1871 info->var.blue.length = 8; 1872 info->var.transp.offset = 0; 1873 info->var.transp.length = 0; 1874 break; 1875 case 32: 1876 info->var.red.offset = 16; 1877 info->var.green.offset = 8; 1878 info->var.blue.offset = 0; 1879 info->var.red.length = 8; 1880 info->var.green.length = 8; 1881 info->var.blue.length = 8; 1882 info->var.transp.offset = 24; 1883 info->var.transp.length = 8; 1884 break; 1885 default: 1886 break; 1887 } 1888 1889 info->var.xres = fb_width; 1890 info->var.yres = fb_height; 1891 } 1892 EXPORT_SYMBOL(drm_fb_helper_fill_var); 1893 #endif 1894 1895 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1896 uint32_t maxX, 1897 uint32_t maxY) 1898 { 1899 struct drm_connector *connector; 1900 int count = 0; 1901 int i; 1902 1903 drm_fb_helper_for_each_connector(fb_helper, i) { 1904 connector = fb_helper->connector_info[i]->connector; 1905 count += connector->funcs->fill_modes(connector, maxX, maxY); 1906 } 1907 1908 return count; 1909 } 1910 1911 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1912 { 1913 struct drm_display_mode *mode; 1914 1915 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1916 if (mode->hdisplay > width || 1917 mode->vdisplay > height) 1918 continue; 1919 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1920 return mode; 1921 } 1922 return NULL; 1923 } 1924 EXPORT_SYMBOL(drm_has_preferred_mode); 1925 1926 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1927 { 1928 return fb_connector->connector->cmdline_mode.specified; 1929 } 1930 1931 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn) 1932 { 1933 struct drm_cmdline_mode *cmdline_mode; 1934 struct drm_display_mode *mode; 1935 bool prefer_non_interlace; 1936 1937 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1938 if (cmdline_mode->specified == false) 1939 return NULL; 1940 1941 /* attempt to find a matching mode in the list of modes 1942 * we have gotten so far, if not add a CVT mode that conforms 1943 */ 1944 if (cmdline_mode->rb || cmdline_mode->margins) 1945 goto create_mode; 1946 1947 prefer_non_interlace = !cmdline_mode->interlace; 1948 again: 1949 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1950 /* check width/height */ 1951 if (mode->hdisplay != cmdline_mode->xres || 1952 mode->vdisplay != cmdline_mode->yres) 1953 continue; 1954 1955 if (cmdline_mode->refresh_specified) { 1956 if (mode->vrefresh != cmdline_mode->refresh) 1957 continue; 1958 } 1959 1960 if (cmdline_mode->interlace) { 1961 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1962 continue; 1963 } else if (prefer_non_interlace) { 1964 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1965 continue; 1966 } 1967 return mode; 1968 } 1969 1970 if (prefer_non_interlace) { 1971 prefer_non_interlace = false; 1972 goto again; 1973 } 1974 1975 create_mode: 1976 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 1977 cmdline_mode); 1978 list_add(&mode->head, &fb_helper_conn->connector->modes); 1979 return mode; 1980 } 1981 EXPORT_SYMBOL(drm_pick_cmdline_mode); 1982 1983 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1984 { 1985 bool enable; 1986 1987 if (strict) 1988 enable = connector->status == connector_status_connected; 1989 else 1990 enable = connector->status != connector_status_disconnected; 1991 1992 return enable; 1993 } 1994 1995 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1996 bool *enabled) 1997 { 1998 bool any_enabled = false; 1999 struct drm_connector *connector; 2000 int i = 0; 2001 2002 drm_fb_helper_for_each_connector(fb_helper, i) { 2003 connector = fb_helper->connector_info[i]->connector; 2004 enabled[i] = drm_connector_enabled(connector, true); 2005 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 2006 enabled[i] ? "yes" : "no"); 2007 any_enabled |= enabled[i]; 2008 } 2009 2010 if (any_enabled) 2011 return; 2012 2013 drm_fb_helper_for_each_connector(fb_helper, i) { 2014 connector = fb_helper->connector_info[i]->connector; 2015 enabled[i] = drm_connector_enabled(connector, false); 2016 } 2017 } 2018 2019 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 2020 struct drm_display_mode **modes, 2021 struct drm_fb_offset *offsets, 2022 bool *enabled, int width, int height) 2023 { 2024 int count, i, j; 2025 bool can_clone = false; 2026 struct drm_fb_helper_connector *fb_helper_conn; 2027 struct drm_display_mode *dmt_mode, *mode; 2028 2029 /* only contemplate cloning in the single crtc case */ 2030 if (fb_helper->crtc_count > 1) 2031 return false; 2032 2033 count = 0; 2034 drm_fb_helper_for_each_connector(fb_helper, i) { 2035 if (enabled[i]) 2036 count++; 2037 } 2038 2039 /* only contemplate cloning if more than one connector is enabled */ 2040 if (count <= 1) 2041 return false; 2042 2043 /* check the command line or if nothing common pick 1024x768 */ 2044 can_clone = true; 2045 drm_fb_helper_for_each_connector(fb_helper, i) { 2046 if (!enabled[i]) 2047 continue; 2048 fb_helper_conn = fb_helper->connector_info[i]; 2049 modes[i] = drm_pick_cmdline_mode(fb_helper_conn); 2050 if (!modes[i]) { 2051 can_clone = false; 2052 break; 2053 } 2054 for (j = 0; j < i; j++) { 2055 if (!enabled[j]) 2056 continue; 2057 if (!drm_mode_equal(modes[j], modes[i])) 2058 can_clone = false; 2059 } 2060 } 2061 2062 if (can_clone) { 2063 DRM_DEBUG_KMS("can clone using command line\n"); 2064 return true; 2065 } 2066 2067 /* try and find a 1024x768 mode on each connector */ 2068 can_clone = true; 2069 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 2070 2071 drm_fb_helper_for_each_connector(fb_helper, i) { 2072 if (!enabled[i]) 2073 continue; 2074 2075 fb_helper_conn = fb_helper->connector_info[i]; 2076 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 2077 if (drm_mode_equal(mode, dmt_mode)) 2078 modes[i] = mode; 2079 } 2080 if (!modes[i]) 2081 can_clone = false; 2082 } 2083 2084 if (can_clone) { 2085 DRM_DEBUG_KMS("can clone using 1024x768\n"); 2086 return true; 2087 } 2088 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 2089 return false; 2090 } 2091 2092 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, 2093 struct drm_display_mode **modes, 2094 struct drm_fb_offset *offsets, 2095 int idx, 2096 int h_idx, int v_idx) 2097 { 2098 struct drm_fb_helper_connector *fb_helper_conn; 2099 int i; 2100 int hoffset = 0, voffset = 0; 2101 2102 drm_fb_helper_for_each_connector(fb_helper, i) { 2103 fb_helper_conn = fb_helper->connector_info[i]; 2104 if (!fb_helper_conn->connector->has_tile) 2105 continue; 2106 2107 if (!modes[i] && (h_idx || v_idx)) { 2108 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, 2109 fb_helper_conn->connector->base.id); 2110 continue; 2111 } 2112 if (fb_helper_conn->connector->tile_h_loc < h_idx) 2113 hoffset += modes[i]->hdisplay; 2114 2115 if (fb_helper_conn->connector->tile_v_loc < v_idx) 2116 voffset += modes[i]->vdisplay; 2117 } 2118 offsets[idx].x = hoffset; 2119 offsets[idx].y = voffset; 2120 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); 2121 return 0; 2122 } 2123 2124 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 2125 struct drm_display_mode **modes, 2126 struct drm_fb_offset *offsets, 2127 bool *enabled, int width, int height) 2128 { 2129 struct drm_fb_helper_connector *fb_helper_conn; 2130 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1; 2131 u64 conn_configured = 0; 2132 int tile_pass = 0; 2133 int i; 2134 2135 retry: 2136 drm_fb_helper_for_each_connector(fb_helper, i) { 2137 fb_helper_conn = fb_helper->connector_info[i]; 2138 2139 if (conn_configured & BIT_ULL(i)) 2140 continue; 2141 2142 if (enabled[i] == false) { 2143 conn_configured |= BIT_ULL(i); 2144 continue; 2145 } 2146 2147 /* first pass over all the untiled connectors */ 2148 if (tile_pass == 0 && fb_helper_conn->connector->has_tile) 2149 continue; 2150 2151 if (tile_pass == 1) { 2152 if (fb_helper_conn->connector->tile_h_loc != 0 || 2153 fb_helper_conn->connector->tile_v_loc != 0) 2154 continue; 2155 2156 } else { 2157 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 && 2158 fb_helper_conn->connector->tile_v_loc != tile_pass - 1) 2159 /* if this tile_pass doesn't cover any of the tiles - keep going */ 2160 continue; 2161 2162 /* 2163 * find the tile offsets for this pass - need to find 2164 * all tiles left and above 2165 */ 2166 drm_get_tile_offsets(fb_helper, modes, offsets, 2167 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); 2168 } 2169 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 2170 fb_helper_conn->connector->base.id); 2171 2172 /* got for command line mode first */ 2173 modes[i] = drm_pick_cmdline_mode(fb_helper_conn); 2174 if (!modes[i]) { 2175 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", 2176 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); 2177 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 2178 } 2179 /* No preferred modes, pick one off the list */ 2180 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 2181 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 2182 break; 2183 } 2184 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 2185 "none"); 2186 conn_configured |= BIT_ULL(i); 2187 } 2188 2189 if ((conn_configured & mask) != mask) { 2190 tile_pass++; 2191 goto retry; 2192 } 2193 return true; 2194 } 2195 2196 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 2197 struct drm_fb_helper_crtc **best_crtcs, 2198 struct drm_display_mode **modes, 2199 int n, int width, int height) 2200 { 2201 int c, o; 2202 struct drm_connector *connector; 2203 const struct drm_connector_helper_funcs *connector_funcs; 2204 struct drm_encoder *encoder; 2205 int my_score, best_score, score; 2206 struct drm_fb_helper_crtc **crtcs, *crtc; 2207 struct drm_fb_helper_connector *fb_helper_conn; 2208 2209 if (n == fb_helper->connector_count) 2210 return 0; 2211 2212 fb_helper_conn = fb_helper->connector_info[n]; 2213 connector = fb_helper_conn->connector; 2214 2215 best_crtcs[n] = NULL; 2216 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 2217 if (modes[n] == NULL) 2218 return best_score; 2219 2220 crtcs = kzalloc(fb_helper->connector_count * 2221 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2222 if (!crtcs) 2223 return best_score; 2224 2225 my_score = 1; 2226 if (connector->status == connector_status_connected) 2227 my_score++; 2228 if (drm_has_cmdline_mode(fb_helper_conn)) 2229 my_score++; 2230 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 2231 my_score++; 2232 2233 connector_funcs = connector->helper_private; 2234 2235 /* 2236 * If the DRM device implements atomic hooks and ->best_encoder() is 2237 * NULL we fallback to the default drm_atomic_helper_best_encoder() 2238 * helper. 2239 */ 2240 if (drm_drv_uses_atomic_modeset(fb_helper->dev) && 2241 !connector_funcs->best_encoder) 2242 encoder = drm_atomic_helper_best_encoder(connector); 2243 else 2244 encoder = connector_funcs->best_encoder(connector); 2245 2246 if (!encoder) 2247 goto out; 2248 2249 /* 2250 * select a crtc for this connector and then attempt to configure 2251 * remaining connectors 2252 */ 2253 for (c = 0; c < fb_helper->crtc_count; c++) { 2254 crtc = &fb_helper->crtc_info[c]; 2255 2256 if ((encoder->possible_crtcs & (1 << c)) == 0) 2257 continue; 2258 2259 for (o = 0; o < n; o++) 2260 if (best_crtcs[o] == crtc) 2261 break; 2262 2263 if (o < n) { 2264 /* ignore cloning unless only a single crtc */ 2265 if (fb_helper->crtc_count > 1) 2266 continue; 2267 2268 if (!drm_mode_equal(modes[o], modes[n])) 2269 continue; 2270 } 2271 2272 crtcs[n] = crtc; 2273 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 2274 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 2275 width, height); 2276 if (score > best_score) { 2277 best_score = score; 2278 memcpy(best_crtcs, crtcs, 2279 fb_helper->connector_count * 2280 sizeof(struct drm_fb_helper_crtc *)); 2281 } 2282 } 2283 out: 2284 kfree(crtcs); 2285 return best_score; 2286 } 2287 2288 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper, 2289 u32 width, u32 height) 2290 { 2291 struct drm_device *dev = fb_helper->dev; 2292 struct drm_fb_helper_crtc **crtcs; 2293 struct drm_display_mode **modes; 2294 struct drm_fb_offset *offsets; 2295 bool *enabled; 2296 int i; 2297 2298 DRM_DEBUG_KMS("\n"); 2299 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0) 2300 DRM_DEBUG_KMS("No connectors reported connected with modes\n"); 2301 2302 /* prevent concurrent modification of connector_count by hotplug */ 2303 lockdep_assert_held(&fb_helper->dev->mode_config.mutex); 2304 2305 crtcs = kcalloc(fb_helper->connector_count, 2306 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2307 modes = kcalloc(fb_helper->connector_count, 2308 sizeof(struct drm_display_mode *), GFP_KERNEL); 2309 offsets = kcalloc(fb_helper->connector_count, 2310 sizeof(struct drm_fb_offset), GFP_KERNEL); 2311 enabled = kcalloc(fb_helper->connector_count, 2312 sizeof(bool), GFP_KERNEL); 2313 if (!crtcs || !modes || !enabled || !offsets) { 2314 DRM_ERROR("Memory allocation failed\n"); 2315 goto out; 2316 } 2317 2318 drm_enable_connectors(fb_helper, enabled); 2319 2320 if (!(fb_helper->funcs->initial_config && 2321 fb_helper->funcs->initial_config(fb_helper, crtcs, modes, 2322 offsets, 2323 enabled, width, height))) { 2324 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); 2325 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); 2326 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); 2327 2328 if (!drm_target_cloned(fb_helper, modes, offsets, 2329 enabled, width, height) && 2330 !drm_target_preferred(fb_helper, modes, offsets, 2331 enabled, width, height)) 2332 DRM_ERROR("Unable to find initial modes\n"); 2333 2334 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 2335 width, height); 2336 2337 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 2338 } 2339 2340 /* need to set the modesets up here for use later */ 2341 /* fill out the connector<->crtc mappings into the modesets */ 2342 for (i = 0; i < fb_helper->crtc_count; i++) 2343 drm_fb_helper_modeset_release(fb_helper, 2344 &fb_helper->crtc_info[i].mode_set); 2345 2346 drm_fb_helper_for_each_connector(fb_helper, i) { 2347 struct drm_display_mode *mode = modes[i]; 2348 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 2349 struct drm_fb_offset *offset = &offsets[i]; 2350 struct drm_mode_set *modeset = &fb_crtc->mode_set; 2351 2352 if (mode && fb_crtc) { 2353 struct drm_connector *connector = 2354 fb_helper->connector_info[i]->connector; 2355 2356 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", 2357 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); 2358 2359 fb_crtc->desired_mode = mode; 2360 fb_crtc->x = offset->x; 2361 fb_crtc->y = offset->y; 2362 modeset->mode = drm_mode_duplicate(dev, 2363 fb_crtc->desired_mode); 2364 drm_connector_get(connector); 2365 modeset->connectors[modeset->num_connectors++] = connector; 2366 modeset->fb = fb_helper->fb; 2367 modeset->x = offset->x; 2368 modeset->y = offset->y; 2369 } 2370 } 2371 out: 2372 kfree(crtcs); 2373 kfree(modes); 2374 kfree(offsets); 2375 kfree(enabled); 2376 } 2377 2378 /** 2379 * drm_fb_helper_initial_config - setup a sane initial connector configuration 2380 * @fb_helper: fb_helper device struct 2381 * @bpp_sel: bpp value to use for the framebuffer configuration 2382 * 2383 * Scans the CRTCs and connectors and tries to put together an initial setup. 2384 * At the moment, this is a cloned configuration across all heads with 2385 * a new framebuffer object as the backing store. 2386 * 2387 * Note that this also registers the fbdev and so allows userspace to call into 2388 * the driver through the fbdev interfaces. 2389 * 2390 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback 2391 * to let the driver allocate and initialize the fbdev info structure and the 2392 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and 2393 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default 2394 * values for the fbdev info structure. 2395 * 2396 * HANG DEBUGGING: 2397 * 2398 * When you have fbcon support built-in or already loaded, this function will do 2399 * a full modeset to setup the fbdev console. Due to locking misdesign in the 2400 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 2401 * console_lock. Until console_unlock is called no dmesg lines will be sent out 2402 * to consoles, not even serial console. This means when your driver crashes, 2403 * you will see absolutely nothing else but a system stuck in this function, 2404 * with no further output. Any kind of printk() you place within your own driver 2405 * or in the drm core modeset code will also never show up. 2406 * 2407 * Standard debug practice is to run the fbcon setup without taking the 2408 * console_lock as a hack, to be able to see backtraces and crashes on the 2409 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 2410 * cmdline option. 2411 * 2412 * The other option is to just disable fbdev emulation since very likely the 2413 * first modeset from userspace will crash in the same way, and is even easier 2414 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 2415 * kernel cmdline option. 2416 * 2417 * RETURNS: 2418 * Zero if everything went ok, nonzero otherwise. 2419 */ 2420 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 2421 { 2422 struct drm_device *dev = fb_helper->dev; 2423 struct fb_info *info; 2424 int ret; 2425 2426 if (!drm_fbdev_emulation) 2427 return 0; 2428 2429 mutex_lock(&dev->mode_config.mutex); 2430 drm_setup_crtcs(fb_helper, 2431 dev->mode_config.max_width, 2432 dev->mode_config.max_height); 2433 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 2434 mutex_unlock(&dev->mode_config.mutex); 2435 if (ret) 2436 return ret; 2437 2438 info = fb_helper->fbdev; 2439 #if 0 2440 info->var.pixclock = 0; 2441 #endif 2442 ret = register_framebuffer(info); 2443 if (ret < 0) 2444 return ret; 2445 2446 #if 0 2447 dev_info(dev->dev, "fb%d: %s frame buffer device\n", 2448 info->node, info->fix.id); 2449 #endif 2450 2451 mutex_lock(&kernel_fb_helper_lock); 2452 #if 0 2453 if (list_empty(&kernel_fb_helper_list)) 2454 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 2455 #endif 2456 2457 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 2458 mutex_unlock(&kernel_fb_helper_lock); 2459 2460 return 0; 2461 } 2462 EXPORT_SYMBOL(drm_fb_helper_initial_config); 2463 2464 /** 2465 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 2466 * probing all the outputs attached to the fb 2467 * @fb_helper: the drm_fb_helper 2468 * 2469 * Scan the connectors attached to the fb_helper and try to put together a 2470 * setup after notification of a change in output configuration. 2471 * 2472 * Called at runtime, takes the mode config locks to be able to check/change the 2473 * modeset configuration. Must be run from process context (which usually means 2474 * either the output polling work or a work item launched from the driver's 2475 * hotplug interrupt). 2476 * 2477 * Note that drivers may call this even before calling 2478 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 2479 * for a race-free fbcon setup and will make sure that the fbdev emulation will 2480 * not miss any hotplug events. 2481 * 2482 * RETURNS: 2483 * 0 on success and a non-zero error code otherwise. 2484 */ 2485 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 2486 { 2487 struct drm_device *dev = fb_helper->dev; 2488 2489 if (!drm_fbdev_emulation) 2490 return 0; 2491 2492 mutex_lock(&dev->mode_config.mutex); 2493 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { 2494 fb_helper->delayed_hotplug = true; 2495 mutex_unlock(&dev->mode_config.mutex); 2496 return 0; 2497 } 2498 DRM_DEBUG_KMS("\n"); 2499 2500 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height); 2501 2502 mutex_unlock(&dev->mode_config.mutex); 2503 2504 drm_fb_helper_set_par(fb_helper->fbdev); 2505 2506 return 0; 2507 } 2508 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2509 2510 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 2511 * but the module doesn't depend on any fb console symbols. At least 2512 * attempt to load fbcon to avoid leaving the system without a usable console. 2513 */ 2514 int __init drm_fb_helper_modinit(void) 2515 { 2516 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 2517 const char name[] = "fbcon"; 2518 struct module *fbcon; 2519 2520 mutex_lock(&module_mutex); 2521 fbcon = find_module(name); 2522 mutex_unlock(&module_mutex); 2523 2524 if (!fbcon) 2525 request_module_nowait(name); 2526 #endif 2527 return 0; 2528 } 2529 EXPORT_SYMBOL(drm_fb_helper_modinit); 2530