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