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