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