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