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 39 static LINUX_LIST_HEAD(kernel_fb_helper_list); 40 41 /** 42 * DOC: fbdev helpers 43 * 44 * The fb helper functions are useful to provide an fbdev on top of a drm kernel 45 * mode setting driver. They can be used mostly independently from the crtc 46 * helper functions used by many drivers to implement the kernel mode setting 47 * interfaces. 48 * 49 * Initialization is done as a four-step process with drm_fb_helper_prepare(), 50 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and 51 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the 52 * default behaviour can override the third step with their own code. 53 * Teardown is done with drm_fb_helper_fini(). 54 * 55 * At runtime drivers should restore the fbdev console by calling 56 * drm_fb_helper_restore_fbdev_mode() from their ->lastclose callback. They 57 * should also notify the fb helper code from updates to the output 58 * configuration by calling drm_fb_helper_hotplug_event(). For easier 59 * integration with the output polling code in drm_crtc_helper.c the modeset 60 * code provides a ->output_poll_changed callback. 61 * 62 * All other functions exported by the fb helper library can be used to 63 * implement the fbdev driver interface by the driver. 64 * 65 * It is possible, though perhaps somewhat tricky, to implement race-free 66 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() 67 * helper must be called first to initialize the minimum required to make 68 * hotplug detection work. Drivers also need to make sure to properly set up 69 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init() 70 * it is safe to enable interrupts and start processing hotplug events. At the 71 * same time, drivers should initialize all modeset objects such as CRTCs, 72 * encoders and connectors. To finish up the fbdev helper initialization, the 73 * drm_fb_helper_init() function is called. To probe for all attached displays 74 * and set up an initial configuration using the detected hardware, drivers 75 * should call drm_fb_helper_single_add_all_connectors() followed by 76 * drm_fb_helper_initial_config(). 77 */ 78 79 /** 80 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev 81 * emulation helper 82 * @fb_helper: fbdev initialized with drm_fb_helper_init 83 * 84 * This functions adds all the available connectors for use with the given 85 * fb_helper. This is a separate step to allow drivers to freely assign 86 * connectors to the fbdev, e.g. if some are reserved for special purposes or 87 * not adequate to be used for the fbcon. 88 * 89 * Since this is part of the initial setup before the fbdev is published, no 90 * locking is required. 91 */ 92 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) 93 { 94 struct drm_device *dev = fb_helper->dev; 95 struct drm_connector *connector; 96 int i; 97 98 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 99 struct drm_fb_helper_connector *fb_helper_connector; 100 101 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); 102 if (!fb_helper_connector) 103 goto fail; 104 105 fb_helper_connector->connector = connector; 106 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; 107 } 108 return 0; 109 fail: 110 for (i = 0; i < fb_helper->connector_count; i++) { 111 kfree(fb_helper->connector_info[i]); 112 fb_helper->connector_info[i] = NULL; 113 } 114 fb_helper->connector_count = 0; 115 return -ENOMEM; 116 } 117 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); 118 119 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector) 120 { 121 struct drm_fb_helper_connector **temp; 122 struct drm_fb_helper_connector *fb_helper_connector; 123 124 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); 125 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) { 126 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector) * (fb_helper->connector_count + 1), M_DRM, M_WAITOK); 127 if (!temp) 128 return -ENOMEM; 129 130 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1; 131 fb_helper->connector_info = temp; 132 } 133 134 135 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); 136 if (!fb_helper_connector) 137 return -ENOMEM; 138 139 fb_helper_connector->connector = connector; 140 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; 141 return 0; 142 } 143 EXPORT_SYMBOL(drm_fb_helper_add_one_connector); 144 145 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 146 struct drm_connector *connector) 147 { 148 struct drm_fb_helper_connector *fb_helper_connector; 149 int i, j; 150 151 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); 152 153 for (i = 0; i < fb_helper->connector_count; i++) { 154 if (fb_helper->connector_info[i]->connector == connector) 155 break; 156 } 157 158 if (i == fb_helper->connector_count) 159 return -EINVAL; 160 fb_helper_connector = fb_helper->connector_info[i]; 161 162 for (j = i + 1; j < fb_helper->connector_count; j++) { 163 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; 164 } 165 fb_helper->connector_count--; 166 kfree(fb_helper_connector); 167 return 0; 168 } 169 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); 170 171 static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper) 172 { 173 struct drm_fb_helper_connector *fb_helper_conn; 174 int i; 175 176 for (i = 0; i < fb_helper->connector_count; i++) { 177 struct drm_cmdline_mode *mode; 178 struct drm_connector *connector; 179 char *option = NULL; 180 181 fb_helper_conn = fb_helper->connector_info[i]; 182 connector = fb_helper_conn->connector; 183 mode = &fb_helper_conn->cmdline_mode; 184 185 /* do something on return - turn off connector maybe */ 186 if (fb_get_options(connector->name, &option)) 187 continue; 188 189 if (drm_mode_parse_command_line_for_connector(option, 190 connector, 191 mode)) { 192 if (mode->force) { 193 const char *s; 194 switch (mode->force) { 195 case DRM_FORCE_OFF: 196 s = "OFF"; 197 break; 198 case DRM_FORCE_ON_DIGITAL: 199 s = "ON - dig"; 200 break; 201 default: 202 case DRM_FORCE_ON: 203 s = "ON"; 204 break; 205 } 206 207 DRM_INFO("forcing %s connector %s\n", 208 connector->name, s); 209 connector->force = mode->force; 210 } 211 212 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n", 213 connector->name, 214 mode->xres, mode->yres, 215 mode->refresh_specified ? mode->refresh : 60, 216 mode->rb ? " reduced blanking" : "", 217 mode->margins ? " with margins" : "", 218 mode->interlace ? " interlaced" : ""); 219 } 220 221 } 222 return 0; 223 } 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 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 list_for_each_entry(c, &dev->mode_config.crtc_list, head) { 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 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 bool restore_fbdev_mode(struct drm_fb_helper *fb_helper) 339 { 340 struct drm_device *dev = fb_helper->dev; 341 struct drm_plane *plane; 342 bool error = false; 343 int i; 344 345 drm_warn_on_modeset_not_all_locked(dev); 346 347 list_for_each_entry(plane, &dev->mode_config.plane_list, head) 348 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 349 drm_plane_force_disable(plane); 350 351 for (i = 0; i < fb_helper->crtc_count; i++) { 352 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 353 struct drm_crtc *crtc = mode_set->crtc; 354 int ret; 355 356 if (crtc->funcs->cursor_set) { 357 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); 358 if (ret) 359 error = true; 360 } 361 362 ret = drm_mode_set_config_internal(mode_set); 363 if (ret) 364 error = true; 365 } 366 return error; 367 } 368 /** 369 * drm_fb_helper_restore_fbdev_mode - restore fbdev configuration 370 * @fb_helper: fbcon to restore 371 * 372 * This should be called from driver's drm ->lastclose callback 373 * when implementing an fbcon on top of kms using this helper. This ensures that 374 * the user isn't greeted with a black screen when e.g. X dies. 375 * 376 * Use this variant if you need to bypass locking (panic), or already 377 * hold all modeset locks. Otherwise use drm_fb_helper_restore_fbdev_mode_unlocked() 378 */ 379 static bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper) 380 { 381 return restore_fbdev_mode(fb_helper); 382 } 383 384 /** 385 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration 386 * @fb_helper: fbcon to restore 387 * 388 * This should be called from driver's drm ->lastclose callback 389 * when implementing an fbcon on top of kms using this helper. This ensures that 390 * the user isn't greeted with a black screen when e.g. X dies. 391 */ 392 bool drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 393 { 394 struct drm_device *dev = fb_helper->dev; 395 bool ret; 396 drm_modeset_lock_all(dev); 397 ret = restore_fbdev_mode(fb_helper); 398 drm_modeset_unlock_all(dev); 399 return ret; 400 } 401 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); 402 403 #if 0 404 /* 405 * restore fbcon display for all kms driver's using this helper, used for sysrq 406 * and panic handling. 407 */ 408 static bool drm_fb_helper_force_kernel_mode(void) 409 { 410 bool ret, error = false; 411 struct drm_fb_helper *helper; 412 413 if (list_empty(&kernel_fb_helper_list)) 414 return false; 415 416 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 417 struct drm_device *dev = helper->dev; 418 419 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 420 continue; 421 422 /* NOTE: we use lockless flag below to avoid grabbing other 423 * modeset locks. So just trylock the underlying mutex 424 * directly: 425 */ 426 if (!mutex_trylock(&dev->mode_config.mutex)) { 427 error = true; 428 continue; 429 } 430 431 ret = drm_fb_helper_restore_fbdev_mode(helper); 432 if (ret) 433 error = true; 434 435 mutex_unlock(&dev->mode_config.mutex); 436 } 437 return error; 438 } 439 440 static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed, 441 void *panic_str) 442 { 443 /* 444 * It's a waste of time and effort to switch back to text console 445 * if the kernel should reboot before panic messages can be seen. 446 */ 447 if (panic_timeout < 0) 448 return 0; 449 450 pr_err("panic occurred, switching back to text console\n"); 451 return drm_fb_helper_force_kernel_mode(); 452 } 453 454 static struct notifier_block paniced = { 455 .notifier_call = drm_fb_helper_panic, 456 }; 457 #endif 458 459 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) 460 { 461 struct drm_device *dev = fb_helper->dev; 462 struct drm_crtc *crtc; 463 int bound = 0, crtcs_bound = 0; 464 465 #if 0 466 /* Sometimes user space wants everything disabled, so don't steal the 467 * display if there's a master. */ 468 if (dev->primary->master) 469 return false; 470 #endif 471 472 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 473 if (crtc->primary->fb) 474 crtcs_bound++; 475 if (crtc->primary->fb == fb_helper->fb) 476 bound++; 477 } 478 479 if (bound < crtcs_bound) 480 return false; 481 482 return true; 483 } 484 485 #if 0 486 #ifdef CONFIG_MAGIC_SYSRQ 487 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) 488 { 489 bool ret; 490 ret = drm_fb_helper_force_kernel_mode(); 491 if (ret == true) 492 DRM_ERROR("Failed to restore crtc configuration\n"); 493 } 494 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); 495 496 static void drm_fb_helper_sysrq(int dummy1) 497 { 498 schedule_work(&drm_fb_helper_restore_work); 499 } 500 501 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { 502 .handler = drm_fb_helper_sysrq, 503 .help_msg = "force-fb(V)", 504 .action_msg = "Restore framebuffer console", 505 }; 506 #else 507 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; 508 #endif 509 510 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) 511 { 512 struct drm_fb_helper *fb_helper = info->par; 513 struct drm_device *dev = fb_helper->dev; 514 struct drm_crtc *crtc; 515 struct drm_connector *connector; 516 int i, j; 517 518 /* 519 * fbdev->blank can be called from irq context in case of a panic. 520 * Since we already have our own special panic handler which will 521 * restore the fbdev console mode completely, just bail out early. 522 */ 523 if (oops_in_progress) 524 return; 525 526 /* 527 * For each CRTC in this fb, turn the connectors on/off. 528 */ 529 drm_modeset_lock_all(dev); 530 if (!drm_fb_helper_is_bound(fb_helper)) { 531 drm_modeset_unlock_all(dev); 532 return; 533 } 534 535 for (i = 0; i < fb_helper->crtc_count; i++) { 536 crtc = fb_helper->crtc_info[i].mode_set.crtc; 537 538 if (!crtc->enabled) 539 continue; 540 541 /* Walk the connectors & encoders on this fb turning them on/off */ 542 for (j = 0; j < fb_helper->connector_count; j++) { 543 connector = fb_helper->connector_info[j]->connector; 544 connector->funcs->dpms(connector, dpms_mode); 545 drm_object_property_set_value(&connector->base, 546 dev->mode_config.dpms_property, dpms_mode); 547 } 548 } 549 drm_modeset_unlock_all(dev); 550 } 551 552 /** 553 * drm_fb_helper_blank - implementation for ->fb_blank 554 * @blank: desired blanking state 555 * @info: fbdev registered by the helper 556 */ 557 int drm_fb_helper_blank(int blank, struct fb_info *info) 558 { 559 switch (blank) { 560 /* Display: On; HSync: On, VSync: On */ 561 case FB_BLANK_UNBLANK: 562 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); 563 break; 564 /* Display: Off; HSync: On, VSync: On */ 565 case FB_BLANK_NORMAL: 566 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 567 break; 568 /* Display: Off; HSync: Off, VSync: On */ 569 case FB_BLANK_HSYNC_SUSPEND: 570 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 571 break; 572 /* Display: Off; HSync: On, VSync: Off */ 573 case FB_BLANK_VSYNC_SUSPEND: 574 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); 575 break; 576 /* Display: Off; HSync: Off, VSync: Off */ 577 case FB_BLANK_POWERDOWN: 578 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); 579 break; 580 } 581 return 0; 582 } 583 EXPORT_SYMBOL(drm_fb_helper_blank); 584 #endif 585 586 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) 587 { 588 int i; 589 590 for (i = 0; i < helper->connector_count; i++) 591 kfree(helper->connector_info[i]); 592 kfree(helper->connector_info); 593 for (i = 0; i < helper->crtc_count; i++) { 594 kfree(helper->crtc_info[i].mode_set.connectors); 595 if (helper->crtc_info[i].mode_set.mode) 596 drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode); 597 } 598 kfree(helper->crtc_info); 599 } 600 601 /** 602 * drm_fb_helper_prepare - setup a drm_fb_helper structure 603 * @dev: DRM device 604 * @helper: driver-allocated fbdev helper structure to set up 605 * @funcs: pointer to structure of functions associate with this helper 606 * 607 * Sets up the bare minimum to make the framebuffer helper usable. This is 608 * useful to implement race-free initialization of the polling helpers. 609 */ 610 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 611 const struct drm_fb_helper_funcs *funcs) 612 { 613 INIT_LIST_HEAD(&helper->kernel_fb_list); 614 helper->funcs = funcs; 615 helper->dev = dev; 616 } 617 EXPORT_SYMBOL(drm_fb_helper_prepare); 618 619 /** 620 * drm_fb_helper_init - initialize a drm_fb_helper structure 621 * @dev: drm device 622 * @fb_helper: driver-allocated fbdev helper structure to initialize 623 * @crtc_count: maximum number of crtcs to support in this fbdev emulation 624 * @max_conn_count: max connector count 625 * 626 * This allocates the structures for the fbdev helper with the given limits. 627 * Note that this won't yet touch the hardware (through the driver interfaces) 628 * nor register the fbdev. This is only done in drm_fb_helper_initial_config() 629 * to allow driver writes more control over the exact init sequence. 630 * 631 * Drivers must call drm_fb_helper_prepare() before calling this function. 632 * 633 * RETURNS: 634 * Zero if everything went ok, nonzero otherwise. 635 */ 636 int drm_fb_helper_init(struct drm_device *dev, 637 struct drm_fb_helper *fb_helper, 638 int crtc_count, int max_conn_count) 639 { 640 struct drm_crtc *crtc; 641 int i; 642 643 if (!max_conn_count) 644 return -EINVAL; 645 646 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); 647 if (!fb_helper->crtc_info) 648 return -ENOMEM; 649 650 fb_helper->crtc_count = crtc_count; 651 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); 652 if (!fb_helper->connector_info) { 653 kfree(fb_helper->crtc_info); 654 return -ENOMEM; 655 } 656 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; 657 fb_helper->connector_count = 0; 658 659 for (i = 0; i < crtc_count; i++) { 660 fb_helper->crtc_info[i].mode_set.connectors = 661 kcalloc(max_conn_count, 662 sizeof(struct drm_connector *), 663 GFP_KERNEL); 664 665 if (!fb_helper->crtc_info[i].mode_set.connectors) 666 goto out_free; 667 fb_helper->crtc_info[i].mode_set.num_connectors = 0; 668 } 669 670 i = 0; 671 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 672 fb_helper->crtc_info[i].mode_set.crtc = crtc; 673 i++; 674 } 675 676 return 0; 677 out_free: 678 drm_fb_helper_crtc_free(fb_helper); 679 return -ENOMEM; 680 } 681 EXPORT_SYMBOL(drm_fb_helper_init); 682 683 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 684 { 685 if (!list_empty(&fb_helper->kernel_fb_list)) { 686 list_del(&fb_helper->kernel_fb_list); 687 if (list_empty(&kernel_fb_helper_list)) { 688 #if 0 689 pr_info("drm: unregistered panic notifier\n"); 690 atomic_notifier_chain_unregister(&panic_notifier_list, 691 &paniced); 692 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 693 #endif 694 } 695 } 696 697 drm_fb_helper_crtc_free(fb_helper); 698 699 } 700 EXPORT_SYMBOL(drm_fb_helper_fini); 701 702 #if 0 703 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, 704 u16 blue, u16 regno, struct fb_info *info) 705 { 706 struct drm_fb_helper *fb_helper = info->par; 707 struct drm_framebuffer *fb = fb_helper->fb; 708 int pindex; 709 710 if (info->fix.visual == FB_VISUAL_TRUECOLOR) { 711 u32 *palette; 712 u32 value; 713 /* place color in psuedopalette */ 714 if (regno > 16) 715 return -EINVAL; 716 palette = (u32 *)info->pseudo_palette; 717 red >>= (16 - info->var.red.length); 718 green >>= (16 - info->var.green.length); 719 blue >>= (16 - info->var.blue.length); 720 value = (red << info->var.red.offset) | 721 (green << info->var.green.offset) | 722 (blue << info->var.blue.offset); 723 if (info->var.transp.length > 0) { 724 u32 mask = (1 << info->var.transp.length) - 1; 725 mask <<= info->var.transp.offset; 726 value |= mask; 727 } 728 palette[regno] = value; 729 return 0; 730 } 731 732 /* 733 * The driver really shouldn't advertise pseudo/directcolor 734 * visuals if it can't deal with the palette. 735 */ 736 if (WARN_ON(!fb_helper->funcs->gamma_set || 737 !fb_helper->funcs->gamma_get)) 738 return -EINVAL; 739 740 pindex = regno; 741 742 if (fb->bits_per_pixel == 16) { 743 pindex = regno << 3; 744 745 if (fb->depth == 16 && regno > 63) 746 return -EINVAL; 747 if (fb->depth == 15 && regno > 31) 748 return -EINVAL; 749 750 if (fb->depth == 16) { 751 u16 r, g, b; 752 int i; 753 if (regno < 32) { 754 for (i = 0; i < 8; i++) 755 fb_helper->funcs->gamma_set(crtc, red, 756 green, blue, pindex + i); 757 } 758 759 fb_helper->funcs->gamma_get(crtc, &r, 760 &g, &b, 761 pindex >> 1); 762 763 for (i = 0; i < 4; i++) 764 fb_helper->funcs->gamma_set(crtc, r, 765 green, b, 766 (pindex >> 1) + i); 767 } 768 } 769 770 if (fb->depth != 16) 771 fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); 772 return 0; 773 } 774 775 /** 776 * drm_fb_helper_setcmap - implementation for ->fb_setcmap 777 * @cmap: cmap to set 778 * @info: fbdev registered by the helper 779 */ 780 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 781 { 782 struct drm_fb_helper *fb_helper = info->par; 783 struct drm_device *dev = fb_helper->dev; 784 struct drm_crtc_helper_funcs *crtc_funcs; 785 u16 *red, *green, *blue, *transp; 786 struct drm_crtc *crtc; 787 int i, j, rc = 0; 788 int start; 789 790 drm_modeset_lock_all(dev); 791 if (!drm_fb_helper_is_bound(fb_helper)) { 792 drm_modeset_unlock_all(dev); 793 return -EBUSY; 794 } 795 796 for (i = 0; i < fb_helper->crtc_count; i++) { 797 crtc = fb_helper->crtc_info[i].mode_set.crtc; 798 crtc_funcs = crtc->helper_private; 799 800 red = cmap->red; 801 green = cmap->green; 802 blue = cmap->blue; 803 transp = cmap->transp; 804 start = cmap->start; 805 806 for (j = 0; j < cmap->len; j++) { 807 u16 hred, hgreen, hblue, htransp = 0xffff; 808 809 hred = *red++; 810 hgreen = *green++; 811 hblue = *blue++; 812 813 if (transp) 814 htransp = *transp++; 815 816 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); 817 if (rc) 818 goto out; 819 } 820 if (crtc_funcs->load_lut) 821 crtc_funcs->load_lut(crtc); 822 } 823 out: 824 drm_modeset_unlock_all(dev); 825 return rc; 826 } 827 EXPORT_SYMBOL(drm_fb_helper_setcmap); 828 829 /** 830 * drm_fb_helper_check_var - implementation for ->fb_check_var 831 * @var: screeninfo to check 832 * @info: fbdev registered by the helper 833 */ 834 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 835 struct fb_info *info) 836 { 837 struct drm_fb_helper *fb_helper = info->par; 838 struct drm_framebuffer *fb = fb_helper->fb; 839 int depth; 840 841 if (var->pixclock != 0 || in_dbg_master()) 842 return -EINVAL; 843 844 /* Need to resize the fb object !!! */ 845 if (var->bits_per_pixel > fb->bits_per_pixel || 846 var->xres > fb->width || var->yres > fb->height || 847 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 848 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " 849 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 850 var->xres, var->yres, var->bits_per_pixel, 851 var->xres_virtual, var->yres_virtual, 852 fb->width, fb->height, fb->bits_per_pixel); 853 return -EINVAL; 854 } 855 856 switch (var->bits_per_pixel) { 857 case 16: 858 depth = (var->green.length == 6) ? 16 : 15; 859 break; 860 case 32: 861 depth = (var->transp.length > 0) ? 32 : 24; 862 break; 863 default: 864 depth = var->bits_per_pixel; 865 break; 866 } 867 868 switch (depth) { 869 case 8: 870 var->red.offset = 0; 871 var->green.offset = 0; 872 var->blue.offset = 0; 873 var->red.length = 8; 874 var->green.length = 8; 875 var->blue.length = 8; 876 var->transp.length = 0; 877 var->transp.offset = 0; 878 break; 879 case 15: 880 var->red.offset = 10; 881 var->green.offset = 5; 882 var->blue.offset = 0; 883 var->red.length = 5; 884 var->green.length = 5; 885 var->blue.length = 5; 886 var->transp.length = 1; 887 var->transp.offset = 15; 888 break; 889 case 16: 890 var->red.offset = 11; 891 var->green.offset = 5; 892 var->blue.offset = 0; 893 var->red.length = 5; 894 var->green.length = 6; 895 var->blue.length = 5; 896 var->transp.length = 0; 897 var->transp.offset = 0; 898 break; 899 case 24: 900 var->red.offset = 16; 901 var->green.offset = 8; 902 var->blue.offset = 0; 903 var->red.length = 8; 904 var->green.length = 8; 905 var->blue.length = 8; 906 var->transp.length = 0; 907 var->transp.offset = 0; 908 break; 909 case 32: 910 var->red.offset = 16; 911 var->green.offset = 8; 912 var->blue.offset = 0; 913 var->red.length = 8; 914 var->green.length = 8; 915 var->blue.length = 8; 916 var->transp.length = 8; 917 var->transp.offset = 24; 918 break; 919 default: 920 return -EINVAL; 921 } 922 return 0; 923 } 924 EXPORT_SYMBOL(drm_fb_helper_check_var); 925 926 /** 927 * drm_fb_helper_set_par - implementation for ->fb_set_par 928 * @info: fbdev registered by the helper 929 * 930 * This will let fbcon do the mode init and is called at initialization time by 931 * the fbdev core when registering the driver, and later on through the hotplug 932 * callback. 933 */ 934 int drm_fb_helper_set_par(struct fb_info *info) 935 { 936 struct drm_fb_helper *fb_helper = info->par; 937 struct fb_var_screeninfo *var = &info->var; 938 939 if (var->pixclock != 0) { 940 DRM_ERROR("PIXEL CLOCK SET\n"); 941 return -EINVAL; 942 } 943 944 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 945 946 if (fb_helper->delayed_hotplug) { 947 fb_helper->delayed_hotplug = false; 948 drm_fb_helper_hotplug_event(fb_helper); 949 } 950 return 0; 951 } 952 EXPORT_SYMBOL(drm_fb_helper_set_par); 953 954 /** 955 * drm_fb_helper_pan_display - implementation for ->fb_pan_display 956 * @var: updated screen information 957 * @info: fbdev registered by the helper 958 */ 959 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 960 struct fb_info *info) 961 { 962 struct drm_fb_helper *fb_helper = info->par; 963 struct drm_device *dev = fb_helper->dev; 964 struct drm_mode_set *modeset; 965 int ret = 0; 966 int i; 967 968 drm_modeset_lock_all(dev); 969 if (!drm_fb_helper_is_bound(fb_helper)) { 970 drm_modeset_unlock_all(dev); 971 return -EBUSY; 972 } 973 974 for (i = 0; i < fb_helper->crtc_count; i++) { 975 modeset = &fb_helper->crtc_info[i].mode_set; 976 977 modeset->x = var->xoffset; 978 modeset->y = var->yoffset; 979 980 if (modeset->num_connectors) { 981 ret = drm_mode_set_config_internal(modeset); 982 if (!ret) { 983 info->var.xoffset = var->xoffset; 984 info->var.yoffset = var->yoffset; 985 } 986 } 987 } 988 drm_modeset_unlock_all(dev); 989 return ret; 990 } 991 EXPORT_SYMBOL(drm_fb_helper_pan_display); 992 #endif 993 994 #ifdef __DragonFly__ 995 static void 996 do_restore_fbdev_mode(void *context, int pending) 997 { 998 struct drm_fb_helper *fb_helper = context; 999 struct drm_device *dev = fb_helper->dev; 1000 1001 if (!fb_helper->fb) 1002 return; 1003 1004 drm_modeset_lock_all(dev); 1005 drm_fb_helper_restore_fbdev_mode(fb_helper); 1006 drm_modeset_unlock_all(dev); 1007 } 1008 1009 static void 1010 sc_restore_fbdev_mode(void *cookie) 1011 { 1012 struct drm_fb_helper *fb_helper = cookie; 1013 1014 if (!fb_helper->fb) 1015 return; 1016 1017 taskqueue_enqueue(taskqueue_thread[0], &fb_helper->fb_mode_task); 1018 } 1019 #endif 1020 1021 /* 1022 * Allocates the backing storage and sets up the fbdev info structure through 1023 * the ->fb_probe callback and then registers the fbdev and sets up the panic 1024 * notifier. 1025 */ 1026 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1027 int preferred_bpp) 1028 { 1029 int ret = 0; 1030 int crtc_count = 0; 1031 int i; 1032 struct fb_info *info; 1033 struct drm_fb_helper_surface_size sizes; 1034 int gamma_size = 0; 1035 #ifdef __DragonFly__ 1036 int kms_console = 1; 1037 #endif 1038 1039 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1040 sizes.surface_depth = 24; 1041 sizes.surface_bpp = 32; 1042 sizes.fb_width = (unsigned)-1; 1043 sizes.fb_height = (unsigned)-1; 1044 1045 /* if driver picks 8 or 16 by default use that 1046 for both depth/bpp */ 1047 if (preferred_bpp != sizes.surface_bpp) 1048 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1049 1050 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1051 for (i = 0; i < fb_helper->connector_count; i++) { 1052 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 1053 struct drm_cmdline_mode *cmdline_mode; 1054 1055 cmdline_mode = &fb_helper_conn->cmdline_mode; 1056 1057 if (cmdline_mode->bpp_specified) { 1058 switch (cmdline_mode->bpp) { 1059 case 8: 1060 sizes.surface_depth = sizes.surface_bpp = 8; 1061 break; 1062 case 15: 1063 sizes.surface_depth = 15; 1064 sizes.surface_bpp = 16; 1065 break; 1066 case 16: 1067 sizes.surface_depth = sizes.surface_bpp = 16; 1068 break; 1069 case 24: 1070 sizes.surface_depth = sizes.surface_bpp = 24; 1071 break; 1072 case 32: 1073 sizes.surface_depth = 24; 1074 sizes.surface_bpp = 32; 1075 break; 1076 } 1077 break; 1078 } 1079 } 1080 1081 crtc_count = 0; 1082 for (i = 0; i < fb_helper->crtc_count; i++) { 1083 struct drm_display_mode *desired_mode; 1084 desired_mode = fb_helper->crtc_info[i].desired_mode; 1085 1086 if (desired_mode) { 1087 if (gamma_size == 0) 1088 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; 1089 if (desired_mode->hdisplay < sizes.fb_width) 1090 sizes.fb_width = desired_mode->hdisplay; 1091 if (desired_mode->vdisplay < sizes.fb_height) 1092 sizes.fb_height = desired_mode->vdisplay; 1093 if (desired_mode->hdisplay > sizes.surface_width) 1094 sizes.surface_width = desired_mode->hdisplay; 1095 if (desired_mode->vdisplay > sizes.surface_height) 1096 sizes.surface_height = desired_mode->vdisplay; 1097 crtc_count++; 1098 } 1099 } 1100 1101 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1102 /* hmm everyone went away - assume VGA cable just fell out 1103 and will come back later. */ 1104 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); 1105 sizes.fb_width = sizes.surface_width = 1024; 1106 sizes.fb_height = sizes.surface_height = 768; 1107 } 1108 1109 /* push down into drivers */ 1110 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1111 if (ret < 0) 1112 return ret; 1113 1114 info = fb_helper->fbdev; 1115 1116 /* 1117 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug 1118 * events, but at init time drm_setup_crtcs needs to be called before 1119 * the fb is allocated (since we need to figure out the desired size of 1120 * the fb before we can allocate it ...). Hence we need to fix things up 1121 * here again. 1122 */ 1123 for (i = 0; i < fb_helper->crtc_count; i++) 1124 if (fb_helper->crtc_info[i].mode_set.num_connectors) 1125 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; 1126 1127 #ifdef __DragonFly__ 1128 TUNABLE_INT_FETCH("kern.kms_console", &kms_console); 1129 if (kms_console) { 1130 TASK_INIT(&fb_helper->fb_mode_task, 0, do_restore_fbdev_mode, 1131 fb_helper); 1132 info->cookie = fb_helper; 1133 info->restore = (void *)&sc_restore_fbdev_mode; 1134 if (register_framebuffer(info) < 0) 1135 return -EINVAL; 1136 } 1137 #endif 1138 1139 #if 0 1140 info->var.pixclock = 0; 1141 if (register_framebuffer(info) < 0) 1142 return -EINVAL; 1143 1144 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", 1145 info->node, info->fix.id); 1146 1147 /* Switch back to kernel console on panic */ 1148 /* multi card linked list maybe */ 1149 if (list_empty(&kernel_fb_helper_list)) { 1150 dev_info(fb_helper->dev->dev, "registered panic notifier\n"); 1151 atomic_notifier_chain_register(&panic_notifier_list, 1152 &paniced); 1153 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 1154 } 1155 1156 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1157 #endif 1158 1159 return 0; 1160 } 1161 1162 #if 0 1163 /** 1164 * drm_fb_helper_fill_fix - initializes fixed fbdev information 1165 * @info: fbdev registered by the helper 1166 * @pitch: desired pitch 1167 * @depth: desired depth 1168 * 1169 * Helper to fill in the fixed fbdev information useful for a non-accelerated 1170 * fbdev emulations. Drivers which support acceleration methods which impose 1171 * additional constraints need to set up their own limits. 1172 * 1173 * Drivers should call this (or their equivalent setup code) from their 1174 * ->fb_probe callback. 1175 */ 1176 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1177 uint32_t depth) 1178 { 1179 info->fix.type = FB_TYPE_PACKED_PIXELS; 1180 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1181 FB_VISUAL_TRUECOLOR; 1182 info->fix.mmio_start = 0; 1183 info->fix.mmio_len = 0; 1184 info->fix.type_aux = 0; 1185 info->fix.xpanstep = 1; /* doing it in hw */ 1186 info->fix.ypanstep = 1; /* doing it in hw */ 1187 info->fix.ywrapstep = 0; 1188 info->fix.accel = FB_ACCEL_NONE; 1189 1190 info->fix.line_length = pitch; 1191 return; 1192 } 1193 EXPORT_SYMBOL(drm_fb_helper_fill_fix); 1194 1195 /** 1196 * drm_fb_helper_fill_var - initalizes variable fbdev information 1197 * @info: fbdev instance to set up 1198 * @fb_helper: fb helper instance to use as template 1199 * @fb_width: desired fb width 1200 * @fb_height: desired fb height 1201 * 1202 * Sets up the variable fbdev metainformation from the given fb helper instance 1203 * and the drm framebuffer allocated in fb_helper->fb. 1204 * 1205 * Drivers should call this (or their equivalent setup code) from their 1206 * ->fb_probe callback after having allocated the fbdev backing 1207 * storage framebuffer. 1208 */ 1209 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 1210 uint32_t fb_width, uint32_t fb_height) 1211 { 1212 struct drm_framebuffer *fb = fb_helper->fb; 1213 info->pseudo_palette = fb_helper->pseudo_palette; 1214 info->var.xres_virtual = fb->width; 1215 info->var.yres_virtual = fb->height; 1216 info->var.bits_per_pixel = fb->bits_per_pixel; 1217 info->var.accel_flags = FB_ACCELF_TEXT; 1218 info->var.xoffset = 0; 1219 info->var.yoffset = 0; 1220 info->var.activate = FB_ACTIVATE_NOW; 1221 info->var.height = -1; 1222 info->var.width = -1; 1223 1224 switch (fb->depth) { 1225 case 8: 1226 info->var.red.offset = 0; 1227 info->var.green.offset = 0; 1228 info->var.blue.offset = 0; 1229 info->var.red.length = 8; /* 8bit DAC */ 1230 info->var.green.length = 8; 1231 info->var.blue.length = 8; 1232 info->var.transp.offset = 0; 1233 info->var.transp.length = 0; 1234 break; 1235 case 15: 1236 info->var.red.offset = 10; 1237 info->var.green.offset = 5; 1238 info->var.blue.offset = 0; 1239 info->var.red.length = 5; 1240 info->var.green.length = 5; 1241 info->var.blue.length = 5; 1242 info->var.transp.offset = 15; 1243 info->var.transp.length = 1; 1244 break; 1245 case 16: 1246 info->var.red.offset = 11; 1247 info->var.green.offset = 5; 1248 info->var.blue.offset = 0; 1249 info->var.red.length = 5; 1250 info->var.green.length = 6; 1251 info->var.blue.length = 5; 1252 info->var.transp.offset = 0; 1253 break; 1254 case 24: 1255 info->var.red.offset = 16; 1256 info->var.green.offset = 8; 1257 info->var.blue.offset = 0; 1258 info->var.red.length = 8; 1259 info->var.green.length = 8; 1260 info->var.blue.length = 8; 1261 info->var.transp.offset = 0; 1262 info->var.transp.length = 0; 1263 break; 1264 case 32: 1265 info->var.red.offset = 16; 1266 info->var.green.offset = 8; 1267 info->var.blue.offset = 0; 1268 info->var.red.length = 8; 1269 info->var.green.length = 8; 1270 info->var.blue.length = 8; 1271 info->var.transp.offset = 24; 1272 info->var.transp.length = 8; 1273 break; 1274 default: 1275 break; 1276 } 1277 1278 info->var.xres = fb_width; 1279 info->var.yres = fb_height; 1280 } 1281 EXPORT_SYMBOL(drm_fb_helper_fill_var); 1282 #endif 1283 1284 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1285 uint32_t maxX, 1286 uint32_t maxY) 1287 { 1288 struct drm_connector *connector; 1289 int count = 0; 1290 int i; 1291 1292 for (i = 0; i < fb_helper->connector_count; i++) { 1293 connector = fb_helper->connector_info[i]->connector; 1294 count += connector->funcs->fill_modes(connector, maxX, maxY); 1295 } 1296 1297 return count; 1298 } 1299 1300 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1301 { 1302 struct drm_display_mode *mode; 1303 1304 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1305 if (mode->hdisplay > width || 1306 mode->vdisplay > height) 1307 continue; 1308 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1309 return mode; 1310 } 1311 return NULL; 1312 } 1313 EXPORT_SYMBOL(drm_has_preferred_mode); 1314 1315 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1316 { 1317 struct drm_cmdline_mode *cmdline_mode; 1318 cmdline_mode = &fb_connector->cmdline_mode; 1319 return cmdline_mode->specified; 1320 } 1321 1322 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, 1323 int width, int height) 1324 { 1325 struct drm_cmdline_mode *cmdline_mode; 1326 struct drm_display_mode *mode = NULL; 1327 bool prefer_non_interlace; 1328 1329 cmdline_mode = &fb_helper_conn->cmdline_mode; 1330 if (cmdline_mode->specified == false) 1331 return mode; 1332 1333 /* attempt to find a matching mode in the list of modes 1334 * we have gotten so far, if not add a CVT mode that conforms 1335 */ 1336 if (cmdline_mode->rb || cmdline_mode->margins) 1337 goto create_mode; 1338 1339 prefer_non_interlace = !cmdline_mode->interlace; 1340 again: 1341 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1342 /* check width/height */ 1343 if (mode->hdisplay != cmdline_mode->xres || 1344 mode->vdisplay != cmdline_mode->yres) 1345 continue; 1346 1347 if (cmdline_mode->refresh_specified) { 1348 if (mode->vrefresh != cmdline_mode->refresh) 1349 continue; 1350 } 1351 1352 if (cmdline_mode->interlace) { 1353 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1354 continue; 1355 } else if (prefer_non_interlace) { 1356 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1357 continue; 1358 } 1359 return mode; 1360 } 1361 1362 if (prefer_non_interlace) { 1363 prefer_non_interlace = false; 1364 goto again; 1365 } 1366 1367 create_mode: 1368 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 1369 cmdline_mode); 1370 list_add(&mode->head, &fb_helper_conn->connector->modes); 1371 return mode; 1372 } 1373 EXPORT_SYMBOL(drm_pick_cmdline_mode); 1374 1375 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1376 { 1377 bool enable; 1378 1379 if (strict) 1380 enable = connector->status == connector_status_connected; 1381 else 1382 enable = connector->status != connector_status_disconnected; 1383 1384 return enable; 1385 } 1386 1387 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1388 bool *enabled) 1389 { 1390 bool any_enabled = false; 1391 struct drm_connector *connector; 1392 int i = 0; 1393 1394 for (i = 0; i < fb_helper->connector_count; i++) { 1395 connector = fb_helper->connector_info[i]->connector; 1396 enabled[i] = drm_connector_enabled(connector, true); 1397 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 1398 enabled[i] ? "yes" : "no"); 1399 any_enabled |= enabled[i]; 1400 } 1401 1402 if (any_enabled) 1403 return; 1404 1405 for (i = 0; i < fb_helper->connector_count; i++) { 1406 connector = fb_helper->connector_info[i]->connector; 1407 enabled[i] = drm_connector_enabled(connector, false); 1408 } 1409 } 1410 1411 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 1412 struct drm_display_mode **modes, 1413 bool *enabled, int width, int height) 1414 { 1415 int count, i, j; 1416 bool can_clone = false; 1417 struct drm_fb_helper_connector *fb_helper_conn; 1418 struct drm_display_mode *dmt_mode, *mode; 1419 1420 /* only contemplate cloning in the single crtc case */ 1421 if (fb_helper->crtc_count > 1) 1422 return false; 1423 1424 count = 0; 1425 for (i = 0; i < fb_helper->connector_count; i++) { 1426 if (enabled[i]) 1427 count++; 1428 } 1429 1430 /* only contemplate cloning if more than one connector is enabled */ 1431 if (count <= 1) 1432 return false; 1433 1434 /* check the command line or if nothing common pick 1024x768 */ 1435 can_clone = true; 1436 for (i = 0; i < fb_helper->connector_count; i++) { 1437 if (!enabled[i]) 1438 continue; 1439 fb_helper_conn = fb_helper->connector_info[i]; 1440 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1441 if (!modes[i]) { 1442 can_clone = false; 1443 break; 1444 } 1445 for (j = 0; j < i; j++) { 1446 if (!enabled[j]) 1447 continue; 1448 if (!drm_mode_equal(modes[j], modes[i])) 1449 can_clone = false; 1450 } 1451 } 1452 1453 if (can_clone) { 1454 DRM_DEBUG_KMS("can clone using command line\n"); 1455 return true; 1456 } 1457 1458 /* try and find a 1024x768 mode on each connector */ 1459 can_clone = true; 1460 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 1461 1462 for (i = 0; i < fb_helper->connector_count; i++) { 1463 1464 if (!enabled[i]) 1465 continue; 1466 1467 fb_helper_conn = fb_helper->connector_info[i]; 1468 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1469 if (drm_mode_equal(mode, dmt_mode)) 1470 modes[i] = mode; 1471 } 1472 if (!modes[i]) 1473 can_clone = false; 1474 } 1475 1476 if (can_clone) { 1477 DRM_DEBUG_KMS("can clone using 1024x768\n"); 1478 return true; 1479 } 1480 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 1481 return false; 1482 } 1483 1484 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 1485 struct drm_display_mode **modes, 1486 bool *enabled, int width, int height) 1487 { 1488 struct drm_fb_helper_connector *fb_helper_conn; 1489 int i; 1490 1491 for (i = 0; i < fb_helper->connector_count; i++) { 1492 fb_helper_conn = fb_helper->connector_info[i]; 1493 1494 if (enabled[i] == false) 1495 continue; 1496 1497 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 1498 fb_helper_conn->connector->base.id); 1499 1500 /* got for command line mode first */ 1501 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1502 if (!modes[i]) { 1503 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n", 1504 fb_helper_conn->connector->base.id); 1505 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 1506 } 1507 /* No preferred modes, pick one off the list */ 1508 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 1509 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 1510 break; 1511 } 1512 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 1513 "none"); 1514 } 1515 return true; 1516 } 1517 1518 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 1519 struct drm_fb_helper_crtc **best_crtcs, 1520 struct drm_display_mode **modes, 1521 int n, int width, int height) 1522 { 1523 int c, o; 1524 struct drm_device *dev = fb_helper->dev; 1525 struct drm_connector *connector; 1526 struct drm_connector_helper_funcs *connector_funcs; 1527 struct drm_encoder *encoder; 1528 int my_score, best_score, score; 1529 struct drm_fb_helper_crtc **crtcs, *crtc; 1530 struct drm_fb_helper_connector *fb_helper_conn; 1531 1532 if (n == fb_helper->connector_count) 1533 return 0; 1534 1535 fb_helper_conn = fb_helper->connector_info[n]; 1536 connector = fb_helper_conn->connector; 1537 1538 best_crtcs[n] = NULL; 1539 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 1540 if (modes[n] == NULL) 1541 return best_score; 1542 1543 crtcs = kzalloc(dev->mode_config.num_connector * 1544 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 1545 if (!crtcs) 1546 return best_score; 1547 1548 my_score = 1; 1549 if (connector->status == connector_status_connected) 1550 my_score++; 1551 if (drm_has_cmdline_mode(fb_helper_conn)) 1552 my_score++; 1553 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 1554 my_score++; 1555 1556 connector_funcs = connector->helper_private; 1557 encoder = connector_funcs->best_encoder(connector); 1558 if (!encoder) 1559 goto out; 1560 1561 /* select a crtc for this connector and then attempt to configure 1562 remaining connectors */ 1563 for (c = 0; c < fb_helper->crtc_count; c++) { 1564 crtc = &fb_helper->crtc_info[c]; 1565 1566 if ((encoder->possible_crtcs & (1 << c)) == 0) 1567 continue; 1568 1569 for (o = 0; o < n; o++) 1570 if (best_crtcs[o] == crtc) 1571 break; 1572 1573 if (o < n) { 1574 /* ignore cloning unless only a single crtc */ 1575 if (fb_helper->crtc_count > 1) 1576 continue; 1577 1578 if (!drm_mode_equal(modes[o], modes[n])) 1579 continue; 1580 } 1581 1582 crtcs[n] = crtc; 1583 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 1584 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 1585 width, height); 1586 if (score > best_score) { 1587 best_score = score; 1588 memcpy(best_crtcs, crtcs, 1589 dev->mode_config.num_connector * 1590 sizeof(struct drm_fb_helper_crtc *)); 1591 } 1592 } 1593 out: 1594 kfree(crtcs); 1595 return best_score; 1596 } 1597 1598 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) 1599 { 1600 struct drm_device *dev = fb_helper->dev; 1601 struct drm_fb_helper_crtc **crtcs; 1602 struct drm_display_mode **modes; 1603 struct drm_mode_set *modeset; 1604 bool *enabled; 1605 int width, height; 1606 int i; 1607 1608 DRM_DEBUG_KMS("\n"); 1609 1610 width = dev->mode_config.max_width; 1611 height = dev->mode_config.max_height; 1612 1613 crtcs = kcalloc(dev->mode_config.num_connector, 1614 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 1615 modes = kcalloc(dev->mode_config.num_connector, 1616 sizeof(struct drm_display_mode *), GFP_KERNEL); 1617 enabled = kcalloc(dev->mode_config.num_connector, 1618 sizeof(bool), GFP_KERNEL); 1619 if (!crtcs || !modes || !enabled) { 1620 DRM_ERROR("Memory allocation failed\n"); 1621 goto out; 1622 } 1623 1624 1625 drm_enable_connectors(fb_helper, enabled); 1626 1627 if (!(fb_helper->funcs->initial_config && 1628 fb_helper->funcs->initial_config(fb_helper, crtcs, modes, 1629 enabled, width, height))) { 1630 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); 1631 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); 1632 1633 if (!drm_target_cloned(fb_helper, 1634 modes, enabled, width, height) && 1635 !drm_target_preferred(fb_helper, 1636 modes, enabled, width, height)) 1637 DRM_ERROR("Unable to find initial modes\n"); 1638 1639 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 1640 width, height); 1641 1642 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 1643 } 1644 1645 /* need to set the modesets up here for use later */ 1646 /* fill out the connector<->crtc mappings into the modesets */ 1647 for (i = 0; i < fb_helper->crtc_count; i++) { 1648 modeset = &fb_helper->crtc_info[i].mode_set; 1649 modeset->num_connectors = 0; 1650 modeset->fb = NULL; 1651 } 1652 1653 for (i = 0; i < fb_helper->connector_count; i++) { 1654 struct drm_display_mode *mode = modes[i]; 1655 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 1656 modeset = &fb_crtc->mode_set; 1657 1658 if (mode && fb_crtc) { 1659 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n", 1660 mode->name, fb_crtc->mode_set.crtc->base.id); 1661 fb_crtc->desired_mode = mode; 1662 if (modeset->mode) 1663 drm_mode_destroy(dev, modeset->mode); 1664 modeset->mode = drm_mode_duplicate(dev, 1665 fb_crtc->desired_mode); 1666 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; 1667 modeset->fb = fb_helper->fb; 1668 } 1669 } 1670 1671 /* Clear out any old modes if there are no more connected outputs. */ 1672 for (i = 0; i < fb_helper->crtc_count; i++) { 1673 modeset = &fb_helper->crtc_info[i].mode_set; 1674 if (modeset->num_connectors == 0) { 1675 BUG_ON(modeset->fb); 1676 BUG_ON(modeset->num_connectors); 1677 if (modeset->mode) 1678 drm_mode_destroy(dev, modeset->mode); 1679 modeset->mode = NULL; 1680 } 1681 } 1682 out: 1683 kfree(crtcs); 1684 kfree(modes); 1685 kfree(enabled); 1686 } 1687 1688 /** 1689 * drm_fb_helper_initial_config - setup a sane initial connector configuration 1690 * @fb_helper: fb_helper device struct 1691 * @bpp_sel: bpp value to use for the framebuffer configuration 1692 * 1693 * Scans the CRTCs and connectors and tries to put together an initial setup. 1694 * At the moment, this is a cloned configuration across all heads with 1695 * a new framebuffer object as the backing store. 1696 * 1697 * Note that this also registers the fbdev and so allows userspace to call into 1698 * the driver through the fbdev interfaces. 1699 * 1700 * This function will call down into the ->fb_probe callback to let 1701 * the driver allocate and initialize the fbdev info structure and the drm 1702 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and 1703 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default 1704 * values for the fbdev info structure. 1705 * 1706 * RETURNS: 1707 * Zero if everything went ok, nonzero otherwise. 1708 */ 1709 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 1710 { 1711 struct drm_device *dev = fb_helper->dev; 1712 int count = 0; 1713 1714 drm_fb_helper_parse_command_line(fb_helper); 1715 1716 mutex_lock(&dev->mode_config.mutex); 1717 count = drm_fb_helper_probe_connector_modes(fb_helper, 1718 dev->mode_config.max_width, 1719 dev->mode_config.max_height); 1720 mutex_unlock(&dev->mode_config.mutex); 1721 /* 1722 * we shouldn't end up with no modes here. 1723 */ 1724 if (count == 0) 1725 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); 1726 1727 drm_setup_crtcs(fb_helper); 1728 1729 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 1730 } 1731 EXPORT_SYMBOL(drm_fb_helper_initial_config); 1732 1733 /** 1734 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 1735 * probing all the outputs attached to the fb 1736 * @fb_helper: the drm_fb_helper 1737 * 1738 * Scan the connectors attached to the fb_helper and try to put together a 1739 * setup after *notification of a change in output configuration. 1740 * 1741 * Called at runtime, takes the mode config locks to be able to check/change the 1742 * modeset configuration. Must be run from process context (which usually means 1743 * either the output polling work or a work item launched from the driver's 1744 * hotplug interrupt). 1745 * 1746 * Note that drivers may call this even before calling 1747 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows 1748 * for a race-free fbcon setup and will make sure that the fbdev emulation will 1749 * not miss any hotplug events. 1750 * 1751 * RETURNS: 1752 * 0 on success and a non-zero error code otherwise. 1753 */ 1754 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 1755 { 1756 struct drm_device *dev = fb_helper->dev; 1757 u32 max_width, max_height; 1758 1759 mutex_lock(&fb_helper->dev->mode_config.mutex); 1760 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { 1761 fb_helper->delayed_hotplug = true; 1762 mutex_unlock(&fb_helper->dev->mode_config.mutex); 1763 return 0; 1764 } 1765 DRM_DEBUG_KMS("\n"); 1766 1767 max_width = fb_helper->fb->width; 1768 max_height = fb_helper->fb->height; 1769 1770 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); 1771 mutex_unlock(&fb_helper->dev->mode_config.mutex); 1772 1773 drm_modeset_lock_all(dev); 1774 drm_setup_crtcs(fb_helper); 1775 drm_modeset_unlock_all(dev); 1776 #if 0 1777 drm_fb_helper_set_par(fb_helper->fbdev); 1778 #endif 1779 1780 return 0; 1781 } 1782 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 1783 1784 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 1785 * but the module doesn't depend on any fb console symbols. At least 1786 * attempt to load fbcon to avoid leaving the system without a usable console. 1787 */ 1788 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 1789 static int __init drm_fb_helper_modinit(void) 1790 { 1791 const char *name = "fbcon"; 1792 struct module *fbcon; 1793 1794 mutex_lock(&module_mutex); 1795 fbcon = find_module(name); 1796 mutex_unlock(&module_mutex); 1797 1798 if (!fbcon) 1799 request_module_nowait(name); 1800 return 0; 1801 } 1802 1803 module_init(drm_fb_helper_modinit); 1804 #endif 1805