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