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 info = kmalloc(sizeof(struct fb_info), M_DRM, M_WAITOK | M_ZERO); 746 if (!info) 747 return ERR_PTR(-ENOMEM); 748 749 #if 0 750 ret = fb_alloc_cmap(&info->cmap, 256, 0); 751 if (ret) 752 goto err_release; 753 754 info->apertures = alloc_apertures(1); 755 if (!info->apertures) { 756 ret = -ENOMEM; 757 goto err_free_cmap; 758 } 759 #endif 760 761 fb_helper->fbdev = info; 762 763 return info; 764 765 #if 0 766 err_free_cmap: 767 fb_dealloc_cmap(&info->cmap); 768 err_release: 769 framebuffer_release(info); 770 return ERR_PTR(ret); 771 #endif 772 } 773 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); 774 775 #if 0 776 /** 777 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device 778 * @fb_helper: driver-allocated fbdev helper 779 * 780 * A wrapper around unregister_framebuffer, to release the fb_info 781 * framebuffer device 782 */ 783 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 784 { 785 if (fb_helper && fb_helper->fbdev) 786 unregister_framebuffer(fb_helper->fbdev); 787 } 788 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); 789 790 /** 791 * drm_fb_helper_release_fbi - dealloc fb_info and its members 792 * @fb_helper: driver-allocated fbdev helper 793 * 794 * A helper to free memory taken by fb_info and the members cmap and 795 * apertures 796 */ 797 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper) 798 { 799 if (fb_helper) { 800 struct fb_info *info = fb_helper->fbdev; 801 802 if (info) { 803 if (info->cmap.len) 804 fb_dealloc_cmap(&info->cmap); 805 framebuffer_release(info); 806 } 807 808 fb_helper->fbdev = NULL; 809 } 810 } 811 EXPORT_SYMBOL(drm_fb_helper_release_fbi); 812 #endif 813 814 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 815 { 816 if (!drm_fbdev_emulation) 817 return; 818 819 if (!list_empty(&fb_helper->kernel_fb_list)) { 820 list_del(&fb_helper->kernel_fb_list); 821 if (list_empty(&kernel_fb_helper_list)) { 822 #if 0 823 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 824 #endif 825 } 826 } 827 828 drm_fb_helper_crtc_free(fb_helper); 829 830 } 831 EXPORT_SYMBOL(drm_fb_helper_fini); 832 833 #if 0 834 /** 835 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer 836 * @fb_helper: driver-allocated fbdev helper 837 * 838 * A wrapper around unlink_framebuffer implemented by fbdev core 839 */ 840 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) 841 { 842 if (fb_helper && fb_helper->fbdev) 843 unlink_framebuffer(fb_helper->fbdev); 844 } 845 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); 846 847 /** 848 * drm_fb_helper_sys_read - wrapper around fb_sys_read 849 * @info: fb_info struct pointer 850 * @buf: userspace buffer to read from framebuffer memory 851 * @count: number of bytes to read from framebuffer memory 852 * @ppos: read offset within framebuffer memory 853 * 854 * A wrapper around fb_sys_read implemented by fbdev core 855 */ 856 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 857 size_t count, loff_t *ppos) 858 { 859 return fb_sys_read(info, buf, count, ppos); 860 } 861 EXPORT_SYMBOL(drm_fb_helper_sys_read); 862 863 /** 864 * drm_fb_helper_sys_write - wrapper around fb_sys_write 865 * @info: fb_info struct pointer 866 * @buf: userspace buffer to write to framebuffer memory 867 * @count: number of bytes to write to framebuffer memory 868 * @ppos: write offset within framebuffer memory 869 * 870 * A wrapper around fb_sys_write implemented by fbdev core 871 */ 872 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 873 size_t count, loff_t *ppos) 874 { 875 return fb_sys_write(info, buf, count, ppos); 876 } 877 EXPORT_SYMBOL(drm_fb_helper_sys_write); 878 879 /** 880 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect 881 * @info: fbdev registered by the helper 882 * @rect: info about rectangle to fill 883 * 884 * A wrapper around sys_fillrect implemented by fbdev core 885 */ 886 void drm_fb_helper_sys_fillrect(struct fb_info *info, 887 const struct fb_fillrect *rect) 888 { 889 sys_fillrect(info, rect); 890 } 891 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); 892 893 /** 894 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea 895 * @info: fbdev registered by the helper 896 * @area: info about area to copy 897 * 898 * A wrapper around sys_copyarea implemented by fbdev core 899 */ 900 void drm_fb_helper_sys_copyarea(struct fb_info *info, 901 const struct fb_copyarea *area) 902 { 903 sys_copyarea(info, area); 904 } 905 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); 906 907 /** 908 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit 909 * @info: fbdev registered by the helper 910 * @image: info about image to blit 911 * 912 * A wrapper around sys_imageblit implemented by fbdev core 913 */ 914 void drm_fb_helper_sys_imageblit(struct fb_info *info, 915 const struct fb_image *image) 916 { 917 sys_imageblit(info, image); 918 } 919 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); 920 921 /** 922 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect 923 * @info: fbdev registered by the helper 924 * @rect: info about rectangle to fill 925 * 926 * A wrapper around cfb_imageblit implemented by fbdev core 927 */ 928 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 929 const struct fb_fillrect *rect) 930 { 931 cfb_fillrect(info, rect); 932 } 933 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); 934 935 /** 936 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea 937 * @info: fbdev registered by the helper 938 * @area: info about area to copy 939 * 940 * A wrapper around cfb_copyarea implemented by fbdev core 941 */ 942 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 943 const struct fb_copyarea *area) 944 { 945 cfb_copyarea(info, area); 946 } 947 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); 948 949 /** 950 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit 951 * @info: fbdev registered by the helper 952 * @image: info about image to blit 953 * 954 * A wrapper around cfb_imageblit implemented by fbdev core 955 */ 956 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 957 const struct fb_image *image) 958 { 959 cfb_imageblit(info, image); 960 } 961 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); 962 963 /** 964 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 965 * @fb_helper: driver-allocated fbdev helper 966 * @state: desired state, zero to resume, non-zero to suspend 967 * 968 * A wrapper around fb_set_suspend implemented by fbdev core 969 */ 970 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state) 971 { 972 if (fb_helper && fb_helper->fbdev) 973 fb_set_suspend(fb_helper->fbdev, state); 974 } 975 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 976 977 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, 978 u16 blue, u16 regno, struct fb_info *info) 979 { 980 struct drm_fb_helper *fb_helper = info->par; 981 struct drm_framebuffer *fb = fb_helper->fb; 982 int pindex; 983 984 if (info->fix.visual == FB_VISUAL_TRUECOLOR) { 985 u32 *palette; 986 u32 value; 987 /* place color in psuedopalette */ 988 if (regno > 16) 989 return -EINVAL; 990 palette = (u32 *)info->pseudo_palette; 991 red >>= (16 - info->var.red.length); 992 green >>= (16 - info->var.green.length); 993 blue >>= (16 - info->var.blue.length); 994 value = (red << info->var.red.offset) | 995 (green << info->var.green.offset) | 996 (blue << info->var.blue.offset); 997 if (info->var.transp.length > 0) { 998 u32 mask = (1 << info->var.transp.length) - 1; 999 mask <<= info->var.transp.offset; 1000 value |= mask; 1001 } 1002 palette[regno] = value; 1003 return 0; 1004 } 1005 1006 /* 1007 * The driver really shouldn't advertise pseudo/directcolor 1008 * visuals if it can't deal with the palette. 1009 */ 1010 if (WARN_ON(!fb_helper->funcs->gamma_set || 1011 !fb_helper->funcs->gamma_get)) 1012 return -EINVAL; 1013 1014 pindex = regno; 1015 1016 if (fb->bits_per_pixel == 16) { 1017 pindex = regno << 3; 1018 1019 if (fb->depth == 16 && regno > 63) 1020 return -EINVAL; 1021 if (fb->depth == 15 && regno > 31) 1022 return -EINVAL; 1023 1024 if (fb->depth == 16) { 1025 u16 r, g, b; 1026 int i; 1027 if (regno < 32) { 1028 for (i = 0; i < 8; i++) 1029 fb_helper->funcs->gamma_set(crtc, red, 1030 green, blue, pindex + i); 1031 } 1032 1033 fb_helper->funcs->gamma_get(crtc, &r, 1034 &g, &b, 1035 pindex >> 1); 1036 1037 for (i = 0; i < 4; i++) 1038 fb_helper->funcs->gamma_set(crtc, r, 1039 green, b, 1040 (pindex >> 1) + i); 1041 } 1042 } 1043 1044 if (fb->depth != 16) 1045 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); 1046 return 0; 1047 } 1048 1049 /** 1050 * drm_fb_helper_setcmap - implementation for ->fb_setcmap 1051 * @cmap: cmap to set 1052 * @info: fbdev registered by the helper 1053 */ 1054 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 1055 { 1056 struct drm_fb_helper *fb_helper = info->par; 1057 struct drm_device *dev = fb_helper->dev; 1058 const struct drm_crtc_helper_funcs *crtc_funcs; 1059 u16 *red, *green, *blue, *transp; 1060 struct drm_crtc *crtc; 1061 int i, j, rc = 0; 1062 int start; 1063 1064 if (oops_in_progress) 1065 return -EBUSY; 1066 1067 drm_modeset_lock_all(dev); 1068 if (!drm_fb_helper_is_bound(fb_helper)) { 1069 drm_modeset_unlock_all(dev); 1070 return -EBUSY; 1071 } 1072 1073 for (i = 0; i < fb_helper->crtc_count; i++) { 1074 crtc = fb_helper->crtc_info[i].mode_set.crtc; 1075 crtc_funcs = crtc->helper_private; 1076 1077 red = cmap->red; 1078 green = cmap->green; 1079 blue = cmap->blue; 1080 transp = cmap->transp; 1081 start = cmap->start; 1082 1083 for (j = 0; j < cmap->len; j++) { 1084 u16 hred, hgreen, hblue, htransp = 0xffff; 1085 1086 hred = *red++; 1087 hgreen = *green++; 1088 hblue = *blue++; 1089 1090 if (transp) 1091 htransp = *transp++; 1092 1093 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); 1094 if (rc) 1095 goto out; 1096 } 1097 if (crtc_funcs->load_lut) 1098 crtc_funcs->load_lut(crtc); 1099 } 1100 out: 1101 drm_modeset_unlock_all(dev); 1102 return rc; 1103 } 1104 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1105 1106 /** 1107 * drm_fb_helper_check_var - implementation for ->fb_check_var 1108 * @var: screeninfo to check 1109 * @info: fbdev registered by the helper 1110 */ 1111 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1112 struct fb_info *info) 1113 { 1114 struct drm_fb_helper *fb_helper = info->par; 1115 struct drm_framebuffer *fb = fb_helper->fb; 1116 int depth; 1117 1118 if (var->pixclock != 0 || in_dbg_master()) 1119 return -EINVAL; 1120 1121 /* Need to resize the fb object !!! */ 1122 if (var->bits_per_pixel > fb->bits_per_pixel || 1123 var->xres > fb->width || var->yres > fb->height || 1124 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1125 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " 1126 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1127 var->xres, var->yres, var->bits_per_pixel, 1128 var->xres_virtual, var->yres_virtual, 1129 fb->width, fb->height, fb->bits_per_pixel); 1130 return -EINVAL; 1131 } 1132 1133 switch (var->bits_per_pixel) { 1134 case 16: 1135 depth = (var->green.length == 6) ? 16 : 15; 1136 break; 1137 case 32: 1138 depth = (var->transp.length > 0) ? 32 : 24; 1139 break; 1140 default: 1141 depth = var->bits_per_pixel; 1142 break; 1143 } 1144 1145 switch (depth) { 1146 case 8: 1147 var->red.offset = 0; 1148 var->green.offset = 0; 1149 var->blue.offset = 0; 1150 var->red.length = 8; 1151 var->green.length = 8; 1152 var->blue.length = 8; 1153 var->transp.length = 0; 1154 var->transp.offset = 0; 1155 break; 1156 case 15: 1157 var->red.offset = 10; 1158 var->green.offset = 5; 1159 var->blue.offset = 0; 1160 var->red.length = 5; 1161 var->green.length = 5; 1162 var->blue.length = 5; 1163 var->transp.length = 1; 1164 var->transp.offset = 15; 1165 break; 1166 case 16: 1167 var->red.offset = 11; 1168 var->green.offset = 5; 1169 var->blue.offset = 0; 1170 var->red.length = 5; 1171 var->green.length = 6; 1172 var->blue.length = 5; 1173 var->transp.length = 0; 1174 var->transp.offset = 0; 1175 break; 1176 case 24: 1177 var->red.offset = 16; 1178 var->green.offset = 8; 1179 var->blue.offset = 0; 1180 var->red.length = 8; 1181 var->green.length = 8; 1182 var->blue.length = 8; 1183 var->transp.length = 0; 1184 var->transp.offset = 0; 1185 break; 1186 case 32: 1187 var->red.offset = 16; 1188 var->green.offset = 8; 1189 var->blue.offset = 0; 1190 var->red.length = 8; 1191 var->green.length = 8; 1192 var->blue.length = 8; 1193 var->transp.length = 8; 1194 var->transp.offset = 24; 1195 break; 1196 default: 1197 return -EINVAL; 1198 } 1199 return 0; 1200 } 1201 EXPORT_SYMBOL(drm_fb_helper_check_var); 1202 1203 /** 1204 * drm_fb_helper_set_par - implementation for ->fb_set_par 1205 * @info: fbdev registered by the helper 1206 * 1207 * This will let fbcon do the mode init and is called at initialization time by 1208 * the fbdev core when registering the driver, and later on through the hotplug 1209 * callback. 1210 */ 1211 int drm_fb_helper_set_par(struct fb_info *info) 1212 { 1213 struct drm_fb_helper *fb_helper = info->par; 1214 struct fb_var_screeninfo *var = &info->var; 1215 1216 if (oops_in_progress) 1217 return -EBUSY; 1218 1219 if (var->pixclock != 0) { 1220 DRM_ERROR("PIXEL CLOCK SET\n"); 1221 return -EINVAL; 1222 } 1223 1224 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 1225 1226 return 0; 1227 } 1228 EXPORT_SYMBOL(drm_fb_helper_set_par); 1229 1230 static int pan_display_atomic(struct fb_var_screeninfo *var, 1231 struct fb_info *info) 1232 { 1233 struct drm_fb_helper *fb_helper = info->par; 1234 struct drm_device *dev = fb_helper->dev; 1235 struct drm_atomic_state *state; 1236 struct drm_plane *plane; 1237 int i, ret; 1238 unsigned plane_mask; 1239 1240 state = drm_atomic_state_alloc(dev); 1241 if (!state) 1242 return -ENOMEM; 1243 1244 state->acquire_ctx = dev->mode_config.acquire_ctx; 1245 retry: 1246 plane_mask = 0; 1247 for(i = 0; i < fb_helper->crtc_count; i++) { 1248 struct drm_mode_set *mode_set; 1249 1250 mode_set = &fb_helper->crtc_info[i].mode_set; 1251 1252 mode_set->x = var->xoffset; 1253 mode_set->y = var->yoffset; 1254 1255 ret = __drm_atomic_helper_set_config(mode_set, state); 1256 if (ret != 0) 1257 goto fail; 1258 1259 plane = mode_set->crtc->primary; 1260 plane_mask |= 1 << drm_plane_index(plane); 1261 plane->old_fb = plane->fb; 1262 } 1263 1264 ret = drm_atomic_commit(state); 1265 if (ret != 0) 1266 goto fail; 1267 1268 info->var.xoffset = var->xoffset; 1269 info->var.yoffset = var->yoffset; 1270 1271 1272 fail: 1273 drm_atomic_clean_old_fb(dev, plane_mask, ret); 1274 1275 if (ret == -EDEADLK) 1276 goto backoff; 1277 1278 if (ret != 0) 1279 drm_atomic_state_free(state); 1280 1281 return ret; 1282 1283 backoff: 1284 drm_atomic_state_clear(state); 1285 drm_atomic_legacy_backoff(state); 1286 1287 goto retry; 1288 } 1289 1290 /** 1291 * drm_fb_helper_pan_display - implementation for ->fb_pan_display 1292 * @var: updated screen information 1293 * @info: fbdev registered by the helper 1294 */ 1295 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1296 struct fb_info *info) 1297 { 1298 struct drm_fb_helper *fb_helper = info->par; 1299 struct drm_device *dev = fb_helper->dev; 1300 struct drm_mode_set *modeset; 1301 int ret = 0; 1302 int i; 1303 1304 if (oops_in_progress) 1305 return -EBUSY; 1306 1307 drm_modeset_lock_all(dev); 1308 if (!drm_fb_helper_is_bound(fb_helper)) { 1309 drm_modeset_unlock_all(dev); 1310 return -EBUSY; 1311 } 1312 1313 if (fb_helper->atomic) { 1314 ret = pan_display_atomic(var, info); 1315 goto unlock; 1316 } 1317 1318 for (i = 0; i < fb_helper->crtc_count; i++) { 1319 modeset = &fb_helper->crtc_info[i].mode_set; 1320 1321 modeset->x = var->xoffset; 1322 modeset->y = var->yoffset; 1323 1324 if (modeset->num_connectors) { 1325 ret = drm_mode_set_config_internal(modeset); 1326 if (!ret) { 1327 info->var.xoffset = var->xoffset; 1328 info->var.yoffset = var->yoffset; 1329 } 1330 } 1331 } 1332 unlock: 1333 drm_modeset_unlock_all(dev); 1334 return ret; 1335 } 1336 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1337 #endif 1338 1339 #ifdef __DragonFly__ 1340 static void 1341 do_restore_fbdev_mode(void *context, int pending) 1342 { 1343 struct drm_fb_helper *fb_helper = context; 1344 1345 if (!fb_helper->fb) 1346 return; 1347 1348 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 1349 } 1350 1351 static void 1352 sc_restore_fbdev_mode(void *cookie) 1353 { 1354 struct drm_fb_helper *fb_helper = cookie; 1355 1356 if (!fb_helper->fb) 1357 return; 1358 1359 taskqueue_enqueue(taskqueue_thread[0], &fb_helper->fb_mode_task); 1360 } 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 TASK_INIT(&fb_helper->fb_mode_task, 0, do_restore_fbdev_mode, 1497 fb_helper); 1498 info->cookie = fb_helper; 1499 info->restore = (void *)&sc_restore_fbdev_mode; 1500 if (register_framebuffer(info) < 0) 1501 return -EINVAL; 1502 } 1503 #endif 1504 1505 #if 0 1506 info->var.pixclock = 0; 1507 if (register_framebuffer(info) < 0) 1508 return -EINVAL; 1509 1510 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", 1511 info->node, info->fix.id); 1512 1513 if (list_empty(&kernel_fb_helper_list)) { 1514 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 1515 } 1516 1517 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1518 #endif 1519 1520 return 0; 1521 } 1522 1523 #if 0 1524 /** 1525 * drm_fb_helper_fill_fix - initializes fixed fbdev information 1526 * @info: fbdev registered by the helper 1527 * @pitch: desired pitch 1528 * @depth: desired depth 1529 * 1530 * Helper to fill in the fixed fbdev information useful for a non-accelerated 1531 * fbdev emulations. Drivers which support acceleration methods which impose 1532 * additional constraints need to set up their own limits. 1533 * 1534 * Drivers should call this (or their equivalent setup code) from their 1535 * ->fb_probe callback. 1536 */ 1537 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1538 uint32_t depth) 1539 { 1540 info->fix.type = FB_TYPE_PACKED_PIXELS; 1541 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1542 FB_VISUAL_TRUECOLOR; 1543 info->fix.mmio_start = 0; 1544 info->fix.mmio_len = 0; 1545 info->fix.type_aux = 0; 1546 info->fix.xpanstep = 1; /* doing it in hw */ 1547 info->fix.ypanstep = 1; /* doing it in hw */ 1548 info->fix.ywrapstep = 0; 1549 info->fix.accel = FB_ACCEL_NONE; 1550 1551 info->fix.line_length = pitch; 1552 return; 1553 } 1554 EXPORT_SYMBOL(drm_fb_helper_fill_fix); 1555 1556 /** 1557 * drm_fb_helper_fill_var - initalizes variable fbdev information 1558 * @info: fbdev instance to set up 1559 * @fb_helper: fb helper instance to use as template 1560 * @fb_width: desired fb width 1561 * @fb_height: desired fb height 1562 * 1563 * Sets up the variable fbdev metainformation from the given fb helper instance 1564 * and the drm framebuffer allocated in fb_helper->fb. 1565 * 1566 * Drivers should call this (or their equivalent setup code) from their 1567 * ->fb_probe callback after having allocated the fbdev backing 1568 * storage framebuffer. 1569 */ 1570 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 1571 uint32_t fb_width, uint32_t fb_height) 1572 { 1573 struct drm_framebuffer *fb = fb_helper->fb; 1574 info->pseudo_palette = fb_helper->pseudo_palette; 1575 info->var.xres_virtual = fb->width; 1576 info->var.yres_virtual = fb->height; 1577 info->var.bits_per_pixel = fb->bits_per_pixel; 1578 info->var.accel_flags = FB_ACCELF_TEXT; 1579 info->var.xoffset = 0; 1580 info->var.yoffset = 0; 1581 info->var.activate = FB_ACTIVATE_NOW; 1582 info->var.height = -1; 1583 info->var.width = -1; 1584 1585 switch (fb->depth) { 1586 case 8: 1587 info->var.red.offset = 0; 1588 info->var.green.offset = 0; 1589 info->var.blue.offset = 0; 1590 info->var.red.length = 8; /* 8bit DAC */ 1591 info->var.green.length = 8; 1592 info->var.blue.length = 8; 1593 info->var.transp.offset = 0; 1594 info->var.transp.length = 0; 1595 break; 1596 case 15: 1597 info->var.red.offset = 10; 1598 info->var.green.offset = 5; 1599 info->var.blue.offset = 0; 1600 info->var.red.length = 5; 1601 info->var.green.length = 5; 1602 info->var.blue.length = 5; 1603 info->var.transp.offset = 15; 1604 info->var.transp.length = 1; 1605 break; 1606 case 16: 1607 info->var.red.offset = 11; 1608 info->var.green.offset = 5; 1609 info->var.blue.offset = 0; 1610 info->var.red.length = 5; 1611 info->var.green.length = 6; 1612 info->var.blue.length = 5; 1613 info->var.transp.offset = 0; 1614 break; 1615 case 24: 1616 info->var.red.offset = 16; 1617 info->var.green.offset = 8; 1618 info->var.blue.offset = 0; 1619 info->var.red.length = 8; 1620 info->var.green.length = 8; 1621 info->var.blue.length = 8; 1622 info->var.transp.offset = 0; 1623 info->var.transp.length = 0; 1624 break; 1625 case 32: 1626 info->var.red.offset = 16; 1627 info->var.green.offset = 8; 1628 info->var.blue.offset = 0; 1629 info->var.red.length = 8; 1630 info->var.green.length = 8; 1631 info->var.blue.length = 8; 1632 info->var.transp.offset = 24; 1633 info->var.transp.length = 8; 1634 break; 1635 default: 1636 break; 1637 } 1638 1639 info->var.xres = fb_width; 1640 info->var.yres = fb_height; 1641 } 1642 EXPORT_SYMBOL(drm_fb_helper_fill_var); 1643 #endif 1644 1645 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1646 uint32_t maxX, 1647 uint32_t maxY) 1648 { 1649 struct drm_connector *connector; 1650 int count = 0; 1651 int i; 1652 1653 for (i = 0; i < fb_helper->connector_count; i++) { 1654 connector = fb_helper->connector_info[i]->connector; 1655 count += connector->funcs->fill_modes(connector, maxX, maxY); 1656 } 1657 1658 return count; 1659 } 1660 1661 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1662 { 1663 struct drm_display_mode *mode; 1664 1665 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1666 if (mode->hdisplay > width || 1667 mode->vdisplay > height) 1668 continue; 1669 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1670 return mode; 1671 } 1672 return NULL; 1673 } 1674 EXPORT_SYMBOL(drm_has_preferred_mode); 1675 1676 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1677 { 1678 return fb_connector->connector->cmdline_mode.specified; 1679 } 1680 1681 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, 1682 int width, int height) 1683 { 1684 struct drm_cmdline_mode *cmdline_mode; 1685 struct drm_display_mode *mode; 1686 bool prefer_non_interlace; 1687 1688 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1689 if (cmdline_mode->specified == false) 1690 return NULL; 1691 1692 /* attempt to find a matching mode in the list of modes 1693 * we have gotten so far, if not add a CVT mode that conforms 1694 */ 1695 if (cmdline_mode->rb || cmdline_mode->margins) 1696 goto create_mode; 1697 1698 prefer_non_interlace = !cmdline_mode->interlace; 1699 again: 1700 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1701 /* check width/height */ 1702 if (mode->hdisplay != cmdline_mode->xres || 1703 mode->vdisplay != cmdline_mode->yres) 1704 continue; 1705 1706 if (cmdline_mode->refresh_specified) { 1707 if (mode->vrefresh != cmdline_mode->refresh) 1708 continue; 1709 } 1710 1711 if (cmdline_mode->interlace) { 1712 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1713 continue; 1714 } else if (prefer_non_interlace) { 1715 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1716 continue; 1717 } 1718 return mode; 1719 } 1720 1721 if (prefer_non_interlace) { 1722 prefer_non_interlace = false; 1723 goto again; 1724 } 1725 1726 create_mode: 1727 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 1728 cmdline_mode); 1729 list_add(&mode->head, &fb_helper_conn->connector->modes); 1730 return mode; 1731 } 1732 EXPORT_SYMBOL(drm_pick_cmdline_mode); 1733 1734 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1735 { 1736 bool enable; 1737 1738 if (strict) 1739 enable = connector->status == connector_status_connected; 1740 else 1741 enable = connector->status != connector_status_disconnected; 1742 1743 return enable; 1744 } 1745 1746 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1747 bool *enabled) 1748 { 1749 bool any_enabled = false; 1750 struct drm_connector *connector; 1751 int i = 0; 1752 1753 for (i = 0; i < fb_helper->connector_count; i++) { 1754 connector = fb_helper->connector_info[i]->connector; 1755 enabled[i] = drm_connector_enabled(connector, true); 1756 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 1757 enabled[i] ? "yes" : "no"); 1758 any_enabled |= enabled[i]; 1759 } 1760 1761 if (any_enabled) 1762 return; 1763 1764 for (i = 0; i < fb_helper->connector_count; i++) { 1765 connector = fb_helper->connector_info[i]->connector; 1766 enabled[i] = drm_connector_enabled(connector, false); 1767 } 1768 } 1769 1770 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 1771 struct drm_display_mode **modes, 1772 struct drm_fb_offset *offsets, 1773 bool *enabled, int width, int height) 1774 { 1775 int count, i, j; 1776 bool can_clone = false; 1777 struct drm_fb_helper_connector *fb_helper_conn; 1778 struct drm_display_mode *dmt_mode, *mode; 1779 1780 /* only contemplate cloning in the single crtc case */ 1781 if (fb_helper->crtc_count > 1) 1782 return false; 1783 1784 count = 0; 1785 for (i = 0; i < fb_helper->connector_count; i++) { 1786 if (enabled[i]) 1787 count++; 1788 } 1789 1790 /* only contemplate cloning if more than one connector is enabled */ 1791 if (count <= 1) 1792 return false; 1793 1794 /* check the command line or if nothing common pick 1024x768 */ 1795 can_clone = true; 1796 for (i = 0; i < fb_helper->connector_count; i++) { 1797 if (!enabled[i]) 1798 continue; 1799 fb_helper_conn = fb_helper->connector_info[i]; 1800 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1801 if (!modes[i]) { 1802 can_clone = false; 1803 break; 1804 } 1805 for (j = 0; j < i; j++) { 1806 if (!enabled[j]) 1807 continue; 1808 if (!drm_mode_equal(modes[j], modes[i])) 1809 can_clone = false; 1810 } 1811 } 1812 1813 if (can_clone) { 1814 DRM_DEBUG_KMS("can clone using command line\n"); 1815 return true; 1816 } 1817 1818 /* try and find a 1024x768 mode on each connector */ 1819 can_clone = true; 1820 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 1821 1822 for (i = 0; i < fb_helper->connector_count; i++) { 1823 1824 if (!enabled[i]) 1825 continue; 1826 1827 fb_helper_conn = fb_helper->connector_info[i]; 1828 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1829 if (drm_mode_equal(mode, dmt_mode)) 1830 modes[i] = mode; 1831 } 1832 if (!modes[i]) 1833 can_clone = false; 1834 } 1835 1836 if (can_clone) { 1837 DRM_DEBUG_KMS("can clone using 1024x768\n"); 1838 return true; 1839 } 1840 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 1841 return false; 1842 } 1843 1844 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, 1845 struct drm_display_mode **modes, 1846 struct drm_fb_offset *offsets, 1847 int idx, 1848 int h_idx, int v_idx) 1849 { 1850 struct drm_fb_helper_connector *fb_helper_conn; 1851 int i; 1852 int hoffset = 0, voffset = 0; 1853 1854 for (i = 0; i < fb_helper->connector_count; i++) { 1855 fb_helper_conn = fb_helper->connector_info[i]; 1856 if (!fb_helper_conn->connector->has_tile) 1857 continue; 1858 1859 if (!modes[i] && (h_idx || v_idx)) { 1860 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, 1861 fb_helper_conn->connector->base.id); 1862 continue; 1863 } 1864 if (fb_helper_conn->connector->tile_h_loc < h_idx) 1865 hoffset += modes[i]->hdisplay; 1866 1867 if (fb_helper_conn->connector->tile_v_loc < v_idx) 1868 voffset += modes[i]->vdisplay; 1869 } 1870 offsets[idx].x = hoffset; 1871 offsets[idx].y = voffset; 1872 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); 1873 return 0; 1874 } 1875 1876 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 1877 struct drm_display_mode **modes, 1878 struct drm_fb_offset *offsets, 1879 bool *enabled, int width, int height) 1880 { 1881 struct drm_fb_helper_connector *fb_helper_conn; 1882 int i; 1883 uint64_t conn_configured = 0, mask; 1884 int tile_pass = 0; 1885 mask = (1 << fb_helper->connector_count) - 1; 1886 retry: 1887 for (i = 0; i < fb_helper->connector_count; i++) { 1888 fb_helper_conn = fb_helper->connector_info[i]; 1889 1890 if (conn_configured & (1 << i)) 1891 continue; 1892 1893 if (enabled[i] == false) { 1894 conn_configured |= (1 << i); 1895 continue; 1896 } 1897 1898 /* first pass over all the untiled connectors */ 1899 if (tile_pass == 0 && fb_helper_conn->connector->has_tile) 1900 continue; 1901 1902 if (tile_pass == 1) { 1903 if (fb_helper_conn->connector->tile_h_loc != 0 || 1904 fb_helper_conn->connector->tile_v_loc != 0) 1905 continue; 1906 1907 } else { 1908 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 && 1909 fb_helper_conn->connector->tile_v_loc != tile_pass - 1) 1910 /* if this tile_pass doesn't cover any of the tiles - keep going */ 1911 continue; 1912 1913 /* find the tile offsets for this pass - need 1914 to find all tiles left and above */ 1915 drm_get_tile_offsets(fb_helper, modes, offsets, 1916 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); 1917 } 1918 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 1919 fb_helper_conn->connector->base.id); 1920 1921 /* got for command line mode first */ 1922 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1923 if (!modes[i]) { 1924 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", 1925 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); 1926 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 1927 } 1928 /* No preferred modes, pick one off the list */ 1929 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 1930 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 1931 break; 1932 } 1933 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 1934 "none"); 1935 conn_configured |= (1 << i); 1936 } 1937 1938 if ((conn_configured & mask) != mask) { 1939 tile_pass++; 1940 goto retry; 1941 } 1942 return true; 1943 } 1944 1945 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 1946 struct drm_fb_helper_crtc **best_crtcs, 1947 struct drm_display_mode **modes, 1948 int n, int width, int height) 1949 { 1950 int c, o; 1951 struct drm_device *dev = fb_helper->dev; 1952 struct drm_connector *connector; 1953 const struct drm_connector_helper_funcs *connector_funcs; 1954 struct drm_encoder *encoder; 1955 int my_score, best_score, score; 1956 struct drm_fb_helper_crtc **crtcs, *crtc; 1957 struct drm_fb_helper_connector *fb_helper_conn; 1958 1959 if (n == fb_helper->connector_count) 1960 return 0; 1961 1962 fb_helper_conn = fb_helper->connector_info[n]; 1963 connector = fb_helper_conn->connector; 1964 1965 best_crtcs[n] = NULL; 1966 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 1967 if (modes[n] == NULL) 1968 return best_score; 1969 1970 crtcs = kzalloc(dev->mode_config.num_connector * 1971 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 1972 if (!crtcs) 1973 return best_score; 1974 1975 my_score = 1; 1976 if (connector->status == connector_status_connected) 1977 my_score++; 1978 if (drm_has_cmdline_mode(fb_helper_conn)) 1979 my_score++; 1980 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 1981 my_score++; 1982 1983 connector_funcs = connector->helper_private; 1984 encoder = connector_funcs->best_encoder(connector); 1985 if (!encoder) 1986 goto out; 1987 1988 /* select a crtc for this connector and then attempt to configure 1989 remaining connectors */ 1990 for (c = 0; c < fb_helper->crtc_count; c++) { 1991 crtc = &fb_helper->crtc_info[c]; 1992 1993 if ((encoder->possible_crtcs & (1 << c)) == 0) 1994 continue; 1995 1996 for (o = 0; o < n; o++) 1997 if (best_crtcs[o] == crtc) 1998 break; 1999 2000 if (o < n) { 2001 /* ignore cloning unless only a single crtc */ 2002 if (fb_helper->crtc_count > 1) 2003 continue; 2004 2005 if (!drm_mode_equal(modes[o], modes[n])) 2006 continue; 2007 } 2008 2009 crtcs[n] = crtc; 2010 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 2011 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 2012 width, height); 2013 if (score > best_score) { 2014 best_score = score; 2015 memcpy(best_crtcs, crtcs, 2016 dev->mode_config.num_connector * 2017 sizeof(struct drm_fb_helper_crtc *)); 2018 } 2019 } 2020 out: 2021 kfree(crtcs); 2022 return best_score; 2023 } 2024 2025 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) 2026 { 2027 struct drm_device *dev = fb_helper->dev; 2028 struct drm_fb_helper_crtc **crtcs; 2029 struct drm_display_mode **modes; 2030 struct drm_fb_offset *offsets; 2031 struct drm_mode_set *modeset; 2032 bool *enabled; 2033 int width, height; 2034 int i; 2035 2036 DRM_DEBUG_KMS("\n"); 2037 2038 width = dev->mode_config.max_width; 2039 height = dev->mode_config.max_height; 2040 2041 crtcs = kcalloc(dev->mode_config.num_connector, 2042 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2043 modes = kcalloc(dev->mode_config.num_connector, 2044 sizeof(struct drm_display_mode *), GFP_KERNEL); 2045 offsets = kcalloc(dev->mode_config.num_connector, 2046 sizeof(struct drm_fb_offset), GFP_KERNEL); 2047 enabled = kcalloc(dev->mode_config.num_connector, 2048 sizeof(bool), GFP_KERNEL); 2049 if (!crtcs || !modes || !enabled || !offsets) { 2050 DRM_ERROR("Memory allocation failed\n"); 2051 goto out; 2052 } 2053 2054 2055 drm_enable_connectors(fb_helper, enabled); 2056 2057 if (!(fb_helper->funcs->initial_config && 2058 fb_helper->funcs->initial_config(fb_helper, crtcs, modes, 2059 offsets, 2060 enabled, width, height))) { 2061 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); 2062 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); 2063 memset(offsets, 0, dev->mode_config.num_connector*sizeof(offsets[0])); 2064 2065 if (!drm_target_cloned(fb_helper, modes, offsets, 2066 enabled, width, height) && 2067 !drm_target_preferred(fb_helper, modes, offsets, 2068 enabled, width, height)) 2069 DRM_ERROR("Unable to find initial modes\n"); 2070 2071 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 2072 width, height); 2073 2074 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 2075 } 2076 2077 /* need to set the modesets up here for use later */ 2078 /* fill out the connector<->crtc mappings into the modesets */ 2079 for (i = 0; i < fb_helper->crtc_count; i++) { 2080 modeset = &fb_helper->crtc_info[i].mode_set; 2081 modeset->num_connectors = 0; 2082 modeset->fb = NULL; 2083 } 2084 2085 for (i = 0; i < fb_helper->connector_count; i++) { 2086 struct drm_display_mode *mode = modes[i]; 2087 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 2088 struct drm_fb_offset *offset = &offsets[i]; 2089 modeset = &fb_crtc->mode_set; 2090 2091 if (mode && fb_crtc) { 2092 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", 2093 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); 2094 fb_crtc->desired_mode = mode; 2095 fb_crtc->x = offset->x; 2096 fb_crtc->y = offset->y; 2097 if (modeset->mode) 2098 drm_mode_destroy(dev, modeset->mode); 2099 modeset->mode = drm_mode_duplicate(dev, 2100 fb_crtc->desired_mode); 2101 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; 2102 modeset->fb = fb_helper->fb; 2103 modeset->x = offset->x; 2104 modeset->y = offset->y; 2105 } 2106 } 2107 2108 /* Clear out any old modes if there are no more connected outputs. */ 2109 for (i = 0; i < fb_helper->crtc_count; i++) { 2110 modeset = &fb_helper->crtc_info[i].mode_set; 2111 if (modeset->num_connectors == 0) { 2112 BUG_ON(modeset->fb); 2113 if (modeset->mode) 2114 drm_mode_destroy(dev, modeset->mode); 2115 modeset->mode = NULL; 2116 } 2117 } 2118 out: 2119 kfree(crtcs); 2120 kfree(modes); 2121 kfree(offsets); 2122 kfree(enabled); 2123 } 2124 2125 /** 2126 * drm_fb_helper_initial_config - setup a sane initial connector configuration 2127 * @fb_helper: fb_helper device struct 2128 * @bpp_sel: bpp value to use for the framebuffer configuration 2129 * 2130 * Scans the CRTCs and connectors and tries to put together an initial setup. 2131 * At the moment, this is a cloned configuration across all heads with 2132 * a new framebuffer object as the backing store. 2133 * 2134 * Note that this also registers the fbdev and so allows userspace to call into 2135 * the driver through the fbdev interfaces. 2136 * 2137 * This function will call down into the ->fb_probe callback to let 2138 * the driver allocate and initialize the fbdev info structure and the drm 2139 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and 2140 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default 2141 * values for the fbdev info structure. 2142 * 2143 * RETURNS: 2144 * Zero if everything went ok, nonzero otherwise. 2145 */ 2146 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 2147 { 2148 struct drm_device *dev = fb_helper->dev; 2149 int count = 0; 2150 2151 if (!drm_fbdev_emulation) 2152 return 0; 2153 2154 mutex_lock(&dev->mode_config.mutex); 2155 count = drm_fb_helper_probe_connector_modes(fb_helper, 2156 dev->mode_config.max_width, 2157 dev->mode_config.max_height); 2158 mutex_unlock(&dev->mode_config.mutex); 2159 /* 2160 * we shouldn't end up with no modes here. 2161 */ 2162 if (count == 0) 2163 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); 2164 2165 drm_setup_crtcs(fb_helper); 2166 2167 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 2168 } 2169 EXPORT_SYMBOL(drm_fb_helper_initial_config); 2170 2171 /** 2172 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 2173 * probing all the outputs attached to the fb 2174 * @fb_helper: the drm_fb_helper 2175 * 2176 * Scan the connectors attached to the fb_helper and try to put together a 2177 * setup after *notification of a change in output configuration. 2178 * 2179 * Called at runtime, takes the mode config locks to be able to check/change the 2180 * modeset configuration. Must be run from process context (which usually means 2181 * either the output polling work or a work item launched from the driver's 2182 * hotplug interrupt). 2183 * 2184 * Note that drivers may call this even before calling 2185 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows 2186 * for a race-free fbcon setup and will make sure that the fbdev emulation will 2187 * not miss any hotplug events. 2188 * 2189 * RETURNS: 2190 * 0 on success and a non-zero error code otherwise. 2191 */ 2192 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 2193 { 2194 struct drm_device *dev = fb_helper->dev; 2195 u32 max_width, max_height; 2196 2197 if (!drm_fbdev_emulation) 2198 return 0; 2199 2200 mutex_lock(&fb_helper->dev->mode_config.mutex); 2201 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { 2202 fb_helper->delayed_hotplug = true; 2203 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2204 return 0; 2205 } 2206 DRM_DEBUG_KMS("\n"); 2207 2208 max_width = fb_helper->fb->width; 2209 max_height = fb_helper->fb->height; 2210 2211 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); 2212 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2213 2214 drm_modeset_lock_all(dev); 2215 drm_setup_crtcs(fb_helper); 2216 drm_modeset_unlock_all(dev); 2217 #if 0 2218 drm_fb_helper_set_par(fb_helper->fbdev); 2219 #endif 2220 2221 return 0; 2222 } 2223 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2224 2225 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 2226 * but the module doesn't depend on any fb console symbols. At least 2227 * attempt to load fbcon to avoid leaving the system without a usable console. 2228 */ 2229 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 2230 static int __init drm_fb_helper_modinit(void) 2231 { 2232 const char *name = "fbcon"; 2233 struct module *fbcon; 2234 2235 mutex_lock(&module_mutex); 2236 fbcon = find_module(name); 2237 mutex_unlock(&module_mutex); 2238 2239 if (!fbcon) 2240 request_module_nowait(name); 2241 return 0; 2242 } 2243 2244 module_init(drm_fb_helper_modinit); 2245 #endif 2246