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 /* XXX: DragonFly-specific */ 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 /* XXX: DragonFly-specific */ 1010 static void 1011 sc_restore_fbdev_mode(void *cookie) 1012 { 1013 struct drm_fb_helper *fb_helper = cookie; 1014 1015 if (!fb_helper->fb) 1016 return; 1017 1018 taskqueue_enqueue(taskqueue_thread[0], &fb_helper->fb_mode_task); 1019 } 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 int kms_console = 1; /* XXX: DragonFly-specific */ 1036 1037 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1038 sizes.surface_depth = 24; 1039 sizes.surface_bpp = 32; 1040 sizes.fb_width = (unsigned)-1; 1041 sizes.fb_height = (unsigned)-1; 1042 1043 /* if driver picks 8 or 16 by default use that 1044 for both depth/bpp */ 1045 if (preferred_bpp != sizes.surface_bpp) 1046 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1047 1048 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1049 for (i = 0; i < fb_helper->connector_count; i++) { 1050 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 1051 struct drm_cmdline_mode *cmdline_mode; 1052 1053 cmdline_mode = &fb_helper_conn->cmdline_mode; 1054 1055 if (cmdline_mode->bpp_specified) { 1056 switch (cmdline_mode->bpp) { 1057 case 8: 1058 sizes.surface_depth = sizes.surface_bpp = 8; 1059 break; 1060 case 15: 1061 sizes.surface_depth = 15; 1062 sizes.surface_bpp = 16; 1063 break; 1064 case 16: 1065 sizes.surface_depth = sizes.surface_bpp = 16; 1066 break; 1067 case 24: 1068 sizes.surface_depth = sizes.surface_bpp = 24; 1069 break; 1070 case 32: 1071 sizes.surface_depth = 24; 1072 sizes.surface_bpp = 32; 1073 break; 1074 } 1075 break; 1076 } 1077 } 1078 1079 crtc_count = 0; 1080 for (i = 0; i < fb_helper->crtc_count; i++) { 1081 struct drm_display_mode *desired_mode; 1082 desired_mode = fb_helper->crtc_info[i].desired_mode; 1083 1084 if (desired_mode) { 1085 if (gamma_size == 0) 1086 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; 1087 if (desired_mode->hdisplay < sizes.fb_width) 1088 sizes.fb_width = desired_mode->hdisplay; 1089 if (desired_mode->vdisplay < sizes.fb_height) 1090 sizes.fb_height = desired_mode->vdisplay; 1091 if (desired_mode->hdisplay > sizes.surface_width) 1092 sizes.surface_width = desired_mode->hdisplay; 1093 if (desired_mode->vdisplay > sizes.surface_height) 1094 sizes.surface_height = desired_mode->vdisplay; 1095 crtc_count++; 1096 } 1097 } 1098 1099 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1100 /* hmm everyone went away - assume VGA cable just fell out 1101 and will come back later. */ 1102 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); 1103 sizes.fb_width = sizes.surface_width = 1024; 1104 sizes.fb_height = sizes.surface_height = 768; 1105 } 1106 1107 /* push down into drivers */ 1108 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1109 if (ret < 0) 1110 return ret; 1111 1112 info = fb_helper->fbdev; 1113 1114 /* 1115 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug 1116 * events, but at init time drm_setup_crtcs needs to be called before 1117 * the fb is allocated (since we need to figure out the desired size of 1118 * the fb before we can allocate it ...). Hence we need to fix things up 1119 * here again. 1120 */ 1121 for (i = 0; i < fb_helper->crtc_count; i++) 1122 if (fb_helper->crtc_info[i].mode_set.num_connectors) 1123 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; 1124 1125 1126 /* XXX: DragonFly-specific */ 1127 TUNABLE_INT_FETCH("kern.kms_console", &kms_console); 1128 if (kms_console) { 1129 TASK_INIT(&fb_helper->fb_mode_task, 0, do_restore_fbdev_mode, 1130 fb_helper); 1131 info->cookie = fb_helper; 1132 info->restore = (void *)&sc_restore_fbdev_mode; 1133 if (register_framebuffer(info) < 0) 1134 return -EINVAL; 1135 } 1136 1137 #if 0 1138 info->var.pixclock = 0; 1139 if (register_framebuffer(info) < 0) 1140 return -EINVAL; 1141 1142 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", 1143 info->node, info->fix.id); 1144 1145 /* Switch back to kernel console on panic */ 1146 /* multi card linked list maybe */ 1147 if (list_empty(&kernel_fb_helper_list)) { 1148 dev_info(fb_helper->dev->dev, "registered panic notifier\n"); 1149 atomic_notifier_chain_register(&panic_notifier_list, 1150 &paniced); 1151 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 1152 } 1153 1154 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1155 #endif 1156 1157 return 0; 1158 } 1159 1160 #if 0 1161 /** 1162 * drm_fb_helper_fill_fix - initializes fixed fbdev information 1163 * @info: fbdev registered by the helper 1164 * @pitch: desired pitch 1165 * @depth: desired depth 1166 * 1167 * Helper to fill in the fixed fbdev information useful for a non-accelerated 1168 * fbdev emulations. Drivers which support acceleration methods which impose 1169 * additional constraints need to set up their own limits. 1170 * 1171 * Drivers should call this (or their equivalent setup code) from their 1172 * ->fb_probe callback. 1173 */ 1174 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1175 uint32_t depth) 1176 { 1177 info->fix.type = FB_TYPE_PACKED_PIXELS; 1178 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1179 FB_VISUAL_TRUECOLOR; 1180 info->fix.mmio_start = 0; 1181 info->fix.mmio_len = 0; 1182 info->fix.type_aux = 0; 1183 info->fix.xpanstep = 1; /* doing it in hw */ 1184 info->fix.ypanstep = 1; /* doing it in hw */ 1185 info->fix.ywrapstep = 0; 1186 info->fix.accel = FB_ACCEL_NONE; 1187 1188 info->fix.line_length = pitch; 1189 return; 1190 } 1191 EXPORT_SYMBOL(drm_fb_helper_fill_fix); 1192 1193 /** 1194 * drm_fb_helper_fill_var - initalizes variable fbdev information 1195 * @info: fbdev instance to set up 1196 * @fb_helper: fb helper instance to use as template 1197 * @fb_width: desired fb width 1198 * @fb_height: desired fb height 1199 * 1200 * Sets up the variable fbdev metainformation from the given fb helper instance 1201 * and the drm framebuffer allocated in fb_helper->fb. 1202 * 1203 * Drivers should call this (or their equivalent setup code) from their 1204 * ->fb_probe callback after having allocated the fbdev backing 1205 * storage framebuffer. 1206 */ 1207 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 1208 uint32_t fb_width, uint32_t fb_height) 1209 { 1210 struct drm_framebuffer *fb = fb_helper->fb; 1211 info->pseudo_palette = fb_helper->pseudo_palette; 1212 info->var.xres_virtual = fb->width; 1213 info->var.yres_virtual = fb->height; 1214 info->var.bits_per_pixel = fb->bits_per_pixel; 1215 info->var.accel_flags = FB_ACCELF_TEXT; 1216 info->var.xoffset = 0; 1217 info->var.yoffset = 0; 1218 info->var.activate = FB_ACTIVATE_NOW; 1219 info->var.height = -1; 1220 info->var.width = -1; 1221 1222 switch (fb->depth) { 1223 case 8: 1224 info->var.red.offset = 0; 1225 info->var.green.offset = 0; 1226 info->var.blue.offset = 0; 1227 info->var.red.length = 8; /* 8bit DAC */ 1228 info->var.green.length = 8; 1229 info->var.blue.length = 8; 1230 info->var.transp.offset = 0; 1231 info->var.transp.length = 0; 1232 break; 1233 case 15: 1234 info->var.red.offset = 10; 1235 info->var.green.offset = 5; 1236 info->var.blue.offset = 0; 1237 info->var.red.length = 5; 1238 info->var.green.length = 5; 1239 info->var.blue.length = 5; 1240 info->var.transp.offset = 15; 1241 info->var.transp.length = 1; 1242 break; 1243 case 16: 1244 info->var.red.offset = 11; 1245 info->var.green.offset = 5; 1246 info->var.blue.offset = 0; 1247 info->var.red.length = 5; 1248 info->var.green.length = 6; 1249 info->var.blue.length = 5; 1250 info->var.transp.offset = 0; 1251 break; 1252 case 24: 1253 info->var.red.offset = 16; 1254 info->var.green.offset = 8; 1255 info->var.blue.offset = 0; 1256 info->var.red.length = 8; 1257 info->var.green.length = 8; 1258 info->var.blue.length = 8; 1259 info->var.transp.offset = 0; 1260 info->var.transp.length = 0; 1261 break; 1262 case 32: 1263 info->var.red.offset = 16; 1264 info->var.green.offset = 8; 1265 info->var.blue.offset = 0; 1266 info->var.red.length = 8; 1267 info->var.green.length = 8; 1268 info->var.blue.length = 8; 1269 info->var.transp.offset = 24; 1270 info->var.transp.length = 8; 1271 break; 1272 default: 1273 break; 1274 } 1275 1276 info->var.xres = fb_width; 1277 info->var.yres = fb_height; 1278 } 1279 EXPORT_SYMBOL(drm_fb_helper_fill_var); 1280 #endif 1281 1282 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1283 uint32_t maxX, 1284 uint32_t maxY) 1285 { 1286 struct drm_connector *connector; 1287 int count = 0; 1288 int i; 1289 1290 for (i = 0; i < fb_helper->connector_count; i++) { 1291 connector = fb_helper->connector_info[i]->connector; 1292 count += connector->funcs->fill_modes(connector, maxX, maxY); 1293 } 1294 1295 return count; 1296 } 1297 1298 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1299 { 1300 struct drm_display_mode *mode; 1301 1302 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1303 if (mode->hdisplay > width || 1304 mode->vdisplay > height) 1305 continue; 1306 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1307 return mode; 1308 } 1309 return NULL; 1310 } 1311 EXPORT_SYMBOL(drm_has_preferred_mode); 1312 1313 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1314 { 1315 struct drm_cmdline_mode *cmdline_mode; 1316 cmdline_mode = &fb_connector->cmdline_mode; 1317 return cmdline_mode->specified; 1318 } 1319 1320 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, 1321 int width, int height) 1322 { 1323 struct drm_cmdline_mode *cmdline_mode; 1324 struct drm_display_mode *mode = NULL; 1325 bool prefer_non_interlace; 1326 1327 cmdline_mode = &fb_helper_conn->cmdline_mode; 1328 if (cmdline_mode->specified == false) 1329 return mode; 1330 1331 /* attempt to find a matching mode in the list of modes 1332 * we have gotten so far, if not add a CVT mode that conforms 1333 */ 1334 if (cmdline_mode->rb || cmdline_mode->margins) 1335 goto create_mode; 1336 1337 prefer_non_interlace = !cmdline_mode->interlace; 1338 again: 1339 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1340 /* check width/height */ 1341 if (mode->hdisplay != cmdline_mode->xres || 1342 mode->vdisplay != cmdline_mode->yres) 1343 continue; 1344 1345 if (cmdline_mode->refresh_specified) { 1346 if (mode->vrefresh != cmdline_mode->refresh) 1347 continue; 1348 } 1349 1350 if (cmdline_mode->interlace) { 1351 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1352 continue; 1353 } else if (prefer_non_interlace) { 1354 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1355 continue; 1356 } 1357 return mode; 1358 } 1359 1360 if (prefer_non_interlace) { 1361 prefer_non_interlace = false; 1362 goto again; 1363 } 1364 1365 create_mode: 1366 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 1367 cmdline_mode); 1368 list_add(&mode->head, &fb_helper_conn->connector->modes); 1369 return mode; 1370 } 1371 EXPORT_SYMBOL(drm_pick_cmdline_mode); 1372 1373 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1374 { 1375 bool enable; 1376 1377 if (strict) 1378 enable = connector->status == connector_status_connected; 1379 else 1380 enable = connector->status != connector_status_disconnected; 1381 1382 return enable; 1383 } 1384 1385 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1386 bool *enabled) 1387 { 1388 bool any_enabled = false; 1389 struct drm_connector *connector; 1390 int i = 0; 1391 1392 for (i = 0; i < fb_helper->connector_count; i++) { 1393 connector = fb_helper->connector_info[i]->connector; 1394 enabled[i] = drm_connector_enabled(connector, true); 1395 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 1396 enabled[i] ? "yes" : "no"); 1397 any_enabled |= enabled[i]; 1398 } 1399 1400 if (any_enabled) 1401 return; 1402 1403 for (i = 0; i < fb_helper->connector_count; i++) { 1404 connector = fb_helper->connector_info[i]->connector; 1405 enabled[i] = drm_connector_enabled(connector, false); 1406 } 1407 } 1408 1409 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 1410 struct drm_display_mode **modes, 1411 bool *enabled, int width, int height) 1412 { 1413 int count, i, j; 1414 bool can_clone = false; 1415 struct drm_fb_helper_connector *fb_helper_conn; 1416 struct drm_display_mode *dmt_mode, *mode; 1417 1418 /* only contemplate cloning in the single crtc case */ 1419 if (fb_helper->crtc_count > 1) 1420 return false; 1421 1422 count = 0; 1423 for (i = 0; i < fb_helper->connector_count; i++) { 1424 if (enabled[i]) 1425 count++; 1426 } 1427 1428 /* only contemplate cloning if more than one connector is enabled */ 1429 if (count <= 1) 1430 return false; 1431 1432 /* check the command line or if nothing common pick 1024x768 */ 1433 can_clone = true; 1434 for (i = 0; i < fb_helper->connector_count; i++) { 1435 if (!enabled[i]) 1436 continue; 1437 fb_helper_conn = fb_helper->connector_info[i]; 1438 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1439 if (!modes[i]) { 1440 can_clone = false; 1441 break; 1442 } 1443 for (j = 0; j < i; j++) { 1444 if (!enabled[j]) 1445 continue; 1446 if (!drm_mode_equal(modes[j], modes[i])) 1447 can_clone = false; 1448 } 1449 } 1450 1451 if (can_clone) { 1452 DRM_DEBUG_KMS("can clone using command line\n"); 1453 return true; 1454 } 1455 1456 /* try and find a 1024x768 mode on each connector */ 1457 can_clone = true; 1458 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 1459 1460 for (i = 0; i < fb_helper->connector_count; i++) { 1461 1462 if (!enabled[i]) 1463 continue; 1464 1465 fb_helper_conn = fb_helper->connector_info[i]; 1466 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1467 if (drm_mode_equal(mode, dmt_mode)) 1468 modes[i] = mode; 1469 } 1470 if (!modes[i]) 1471 can_clone = false; 1472 } 1473 1474 if (can_clone) { 1475 DRM_DEBUG_KMS("can clone using 1024x768\n"); 1476 return true; 1477 } 1478 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 1479 return false; 1480 } 1481 1482 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 1483 struct drm_display_mode **modes, 1484 bool *enabled, int width, int height) 1485 { 1486 struct drm_fb_helper_connector *fb_helper_conn; 1487 int i; 1488 1489 for (i = 0; i < fb_helper->connector_count; i++) { 1490 fb_helper_conn = fb_helper->connector_info[i]; 1491 1492 if (enabled[i] == false) 1493 continue; 1494 1495 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 1496 fb_helper_conn->connector->base.id); 1497 1498 /* got for command line mode first */ 1499 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1500 if (!modes[i]) { 1501 DRM_DEBUG_KMS("looking for preferred mode on connector %d\n", 1502 fb_helper_conn->connector->base.id); 1503 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 1504 } 1505 /* No preferred modes, pick one off the list */ 1506 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 1507 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 1508 break; 1509 } 1510 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 1511 "none"); 1512 } 1513 return true; 1514 } 1515 1516 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 1517 struct drm_fb_helper_crtc **best_crtcs, 1518 struct drm_display_mode **modes, 1519 int n, int width, int height) 1520 { 1521 int c, o; 1522 struct drm_device *dev = fb_helper->dev; 1523 struct drm_connector *connector; 1524 struct drm_connector_helper_funcs *connector_funcs; 1525 struct drm_encoder *encoder; 1526 int my_score, best_score, score; 1527 struct drm_fb_helper_crtc **crtcs, *crtc; 1528 struct drm_fb_helper_connector *fb_helper_conn; 1529 1530 if (n == fb_helper->connector_count) 1531 return 0; 1532 1533 fb_helper_conn = fb_helper->connector_info[n]; 1534 connector = fb_helper_conn->connector; 1535 1536 best_crtcs[n] = NULL; 1537 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 1538 if (modes[n] == NULL) 1539 return best_score; 1540 1541 crtcs = kzalloc(dev->mode_config.num_connector * 1542 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 1543 if (!crtcs) 1544 return best_score; 1545 1546 my_score = 1; 1547 if (connector->status == connector_status_connected) 1548 my_score++; 1549 if (drm_has_cmdline_mode(fb_helper_conn)) 1550 my_score++; 1551 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 1552 my_score++; 1553 1554 connector_funcs = connector->helper_private; 1555 encoder = connector_funcs->best_encoder(connector); 1556 if (!encoder) 1557 goto out; 1558 1559 /* select a crtc for this connector and then attempt to configure 1560 remaining connectors */ 1561 for (c = 0; c < fb_helper->crtc_count; c++) { 1562 crtc = &fb_helper->crtc_info[c]; 1563 1564 if ((encoder->possible_crtcs & (1 << c)) == 0) 1565 continue; 1566 1567 for (o = 0; o < n; o++) 1568 if (best_crtcs[o] == crtc) 1569 break; 1570 1571 if (o < n) { 1572 /* ignore cloning unless only a single crtc */ 1573 if (fb_helper->crtc_count > 1) 1574 continue; 1575 1576 if (!drm_mode_equal(modes[o], modes[n])) 1577 continue; 1578 } 1579 1580 crtcs[n] = crtc; 1581 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 1582 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 1583 width, height); 1584 if (score > best_score) { 1585 best_score = score; 1586 memcpy(best_crtcs, crtcs, 1587 dev->mode_config.num_connector * 1588 sizeof(struct drm_fb_helper_crtc *)); 1589 } 1590 } 1591 out: 1592 kfree(crtcs); 1593 return best_score; 1594 } 1595 1596 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) 1597 { 1598 struct drm_device *dev = fb_helper->dev; 1599 struct drm_fb_helper_crtc **crtcs; 1600 struct drm_display_mode **modes; 1601 struct drm_mode_set *modeset; 1602 bool *enabled; 1603 int width, height; 1604 int i; 1605 1606 DRM_DEBUG_KMS("\n"); 1607 1608 width = dev->mode_config.max_width; 1609 height = dev->mode_config.max_height; 1610 1611 crtcs = kcalloc(dev->mode_config.num_connector, 1612 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 1613 modes = kcalloc(dev->mode_config.num_connector, 1614 sizeof(struct drm_display_mode *), GFP_KERNEL); 1615 enabled = kcalloc(dev->mode_config.num_connector, 1616 sizeof(bool), GFP_KERNEL); 1617 if (!crtcs || !modes || !enabled) { 1618 DRM_ERROR("Memory allocation failed\n"); 1619 goto out; 1620 } 1621 1622 1623 drm_enable_connectors(fb_helper, enabled); 1624 1625 if (!(fb_helper->funcs->initial_config && 1626 fb_helper->funcs->initial_config(fb_helper, crtcs, modes, 1627 enabled, width, height))) { 1628 memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0])); 1629 memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0])); 1630 1631 if (!drm_target_cloned(fb_helper, 1632 modes, enabled, width, height) && 1633 !drm_target_preferred(fb_helper, 1634 modes, enabled, width, height)) 1635 DRM_ERROR("Unable to find initial modes\n"); 1636 1637 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 1638 width, height); 1639 1640 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 1641 } 1642 1643 /* need to set the modesets up here for use later */ 1644 /* fill out the connector<->crtc mappings into the modesets */ 1645 for (i = 0; i < fb_helper->crtc_count; i++) { 1646 modeset = &fb_helper->crtc_info[i].mode_set; 1647 modeset->num_connectors = 0; 1648 modeset->fb = NULL; 1649 } 1650 1651 for (i = 0; i < fb_helper->connector_count; i++) { 1652 struct drm_display_mode *mode = modes[i]; 1653 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 1654 modeset = &fb_crtc->mode_set; 1655 1656 if (mode && fb_crtc) { 1657 DRM_DEBUG_KMS("desired mode %s set on crtc %d\n", 1658 mode->name, fb_crtc->mode_set.crtc->base.id); 1659 fb_crtc->desired_mode = mode; 1660 if (modeset->mode) 1661 drm_mode_destroy(dev, modeset->mode); 1662 modeset->mode = drm_mode_duplicate(dev, 1663 fb_crtc->desired_mode); 1664 modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; 1665 modeset->fb = fb_helper->fb; 1666 } 1667 } 1668 1669 /* Clear out any old modes if there are no more connected outputs. */ 1670 for (i = 0; i < fb_helper->crtc_count; i++) { 1671 modeset = &fb_helper->crtc_info[i].mode_set; 1672 if (modeset->num_connectors == 0) { 1673 BUG_ON(modeset->fb); 1674 BUG_ON(modeset->num_connectors); 1675 if (modeset->mode) 1676 drm_mode_destroy(dev, modeset->mode); 1677 modeset->mode = NULL; 1678 } 1679 } 1680 out: 1681 kfree(crtcs); 1682 kfree(modes); 1683 kfree(enabled); 1684 } 1685 1686 /** 1687 * drm_fb_helper_initial_config - setup a sane initial connector configuration 1688 * @fb_helper: fb_helper device struct 1689 * @bpp_sel: bpp value to use for the framebuffer configuration 1690 * 1691 * Scans the CRTCs and connectors and tries to put together an initial setup. 1692 * At the moment, this is a cloned configuration across all heads with 1693 * a new framebuffer object as the backing store. 1694 * 1695 * Note that this also registers the fbdev and so allows userspace to call into 1696 * the driver through the fbdev interfaces. 1697 * 1698 * This function will call down into the ->fb_probe callback to let 1699 * the driver allocate and initialize the fbdev info structure and the drm 1700 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and 1701 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default 1702 * values for the fbdev info structure. 1703 * 1704 * RETURNS: 1705 * Zero if everything went ok, nonzero otherwise. 1706 */ 1707 bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 1708 { 1709 struct drm_device *dev = fb_helper->dev; 1710 int count = 0; 1711 1712 drm_fb_helper_parse_command_line(fb_helper); 1713 1714 mutex_lock(&dev->mode_config.mutex); 1715 count = drm_fb_helper_probe_connector_modes(fb_helper, 1716 dev->mode_config.max_width, 1717 dev->mode_config.max_height); 1718 mutex_unlock(&dev->mode_config.mutex); 1719 /* 1720 * we shouldn't end up with no modes here. 1721 */ 1722 if (count == 0) 1723 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); 1724 1725 drm_setup_crtcs(fb_helper); 1726 1727 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 1728 } 1729 EXPORT_SYMBOL(drm_fb_helper_initial_config); 1730 1731 /** 1732 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 1733 * probing all the outputs attached to the fb 1734 * @fb_helper: the drm_fb_helper 1735 * 1736 * Scan the connectors attached to the fb_helper and try to put together a 1737 * setup after *notification of a change in output configuration. 1738 * 1739 * Called at runtime, takes the mode config locks to be able to check/change the 1740 * modeset configuration. Must be run from process context (which usually means 1741 * either the output polling work or a work item launched from the driver's 1742 * hotplug interrupt). 1743 * 1744 * Note that drivers may call this even before calling 1745 * drm_fb_helper_initial_config but only aftert drm_fb_helper_init. This allows 1746 * for a race-free fbcon setup and will make sure that the fbdev emulation will 1747 * not miss any hotplug events. 1748 * 1749 * RETURNS: 1750 * 0 on success and a non-zero error code otherwise. 1751 */ 1752 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 1753 { 1754 struct drm_device *dev = fb_helper->dev; 1755 u32 max_width, max_height; 1756 1757 mutex_lock(&fb_helper->dev->mode_config.mutex); 1758 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { 1759 fb_helper->delayed_hotplug = true; 1760 mutex_unlock(&fb_helper->dev->mode_config.mutex); 1761 return 0; 1762 } 1763 DRM_DEBUG_KMS("\n"); 1764 1765 max_width = fb_helper->fb->width; 1766 max_height = fb_helper->fb->height; 1767 1768 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); 1769 mutex_unlock(&fb_helper->dev->mode_config.mutex); 1770 1771 drm_modeset_lock_all(dev); 1772 drm_setup_crtcs(fb_helper); 1773 drm_modeset_unlock_all(dev); 1774 #if 0 1775 drm_fb_helper_set_par(fb_helper->fbdev); 1776 #endif 1777 1778 return 0; 1779 } 1780 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 1781 1782 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 1783 * but the module doesn't depend on any fb console symbols. At least 1784 * attempt to load fbcon to avoid leaving the system without a usable console. 1785 */ 1786 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 1787 static int __init drm_fb_helper_modinit(void) 1788 { 1789 const char *name = "fbcon"; 1790 struct module *fbcon; 1791 1792 mutex_lock(&module_mutex); 1793 fbcon = find_module(name); 1794 mutex_unlock(&module_mutex); 1795 1796 if (!fbcon) 1797 request_module_nowait(name); 1798 return 0; 1799 } 1800 1801 module_init(drm_fb_helper_modinit); 1802 #endif 1803