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