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