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/console.h> 32 #include <linux/kernel.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 #include <drm/drm_atomic.h> 39 #include <drm/drm_atomic_helper.h> 40 41 #include "drm_crtc_helper_internal.h" 42 43 static bool drm_fbdev_emulation = true; 44 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); 45 MODULE_PARM_DESC(fbdev_emulation, 46 "Enable legacy fbdev emulation [default=true]"); 47 48 static LINUX_LIST_HEAD(kernel_fb_helper_list); 49 50 /** 51 * DOC: fbdev helpers 52 * 53 * The fb helper functions are useful to provide an fbdev on top of a drm kernel 54 * mode setting driver. They can be used mostly independently from the crtc 55 * helper functions used by many drivers to implement the kernel mode setting 56 * interfaces. 57 * 58 * Initialization is done as a four-step process with drm_fb_helper_prepare(), 59 * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and 60 * drm_fb_helper_initial_config(). Drivers with fancier requirements than the 61 * default behaviour can override the third step with their own code. 62 * Teardown is done with drm_fb_helper_fini(). 63 * 64 * At runtime drivers should restore the fbdev console by calling 65 * drm_fb_helper_restore_fbdev_mode_unlocked() from their ->lastclose callback. 66 * They should also notify the fb helper code from updates to the output 67 * configuration by calling drm_fb_helper_hotplug_event(). For easier 68 * integration with the output polling code in drm_crtc_helper.c the modeset 69 * code provides a ->output_poll_changed callback. 70 * 71 * All other functions exported by the fb helper library can be used to 72 * implement the fbdev driver interface by the driver. 73 * 74 * It is possible, though perhaps somewhat tricky, to implement race-free 75 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() 76 * helper must be called first to initialize the minimum required to make 77 * hotplug detection work. Drivers also need to make sure to properly set up 78 * the dev->mode_config.funcs member. After calling drm_kms_helper_poll_init() 79 * it is safe to enable interrupts and start processing hotplug events. At the 80 * same time, drivers should initialize all modeset objects such as CRTCs, 81 * encoders and connectors. To finish up the fbdev helper initialization, the 82 * drm_fb_helper_init() function is called. To probe for all attached displays 83 * and set up an initial configuration using the detected hardware, drivers 84 * should call drm_fb_helper_single_add_all_connectors() followed by 85 * drm_fb_helper_initial_config(). 86 * 87 * If &drm_framebuffer_funcs ->dirty is set, the 88 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will 89 * accumulate changes and schedule &drm_fb_helper ->dirty_work to run right 90 * away. This worker then calls the dirty() function ensuring that it will 91 * always run in process context since the fb_*() function could be running in 92 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io 93 * callback it will also schedule dirty_work with the damage collected from the 94 * mmap page writes. 95 */ 96 97 /** 98 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev 99 * emulation helper 100 * @fb_helper: fbdev initialized with drm_fb_helper_init 101 * 102 * This functions adds all the available connectors for use with the given 103 * fb_helper. This is a separate step to allow drivers to freely assign 104 * connectors to the fbdev, e.g. if some are reserved for special purposes or 105 * not adequate to be used for the fbcon. 106 * 107 * This function is protected against concurrent connector hotadds/removals 108 * using drm_fb_helper_add_one_connector() and 109 * drm_fb_helper_remove_one_connector(). 110 */ 111 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) 112 { 113 struct drm_device *dev = fb_helper->dev; 114 struct drm_connector *connector; 115 int i, ret; 116 117 if (!drm_fbdev_emulation) 118 return 0; 119 120 mutex_lock(&dev->mode_config.mutex); 121 drm_for_each_connector(connector, dev) { 122 ret = drm_fb_helper_add_one_connector(fb_helper, connector); 123 124 if (ret) 125 goto fail; 126 } 127 mutex_unlock(&dev->mode_config.mutex); 128 return 0; 129 fail: 130 for (i = 0; i < fb_helper->connector_count; i++) { 131 struct drm_fb_helper_connector *fb_helper_connector = 132 fb_helper->connector_info[i]; 133 134 drm_connector_unreference(fb_helper_connector->connector); 135 136 kfree(fb_helper_connector); 137 fb_helper->connector_info[i] = NULL; 138 } 139 fb_helper->connector_count = 0; 140 mutex_unlock(&dev->mode_config.mutex); 141 142 return ret; 143 } 144 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); 145 146 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector) 147 { 148 struct drm_fb_helper_connector **temp; 149 struct drm_fb_helper_connector *fb_helper_connector; 150 151 if (!drm_fbdev_emulation) 152 return 0; 153 154 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); 155 if (fb_helper->connector_count + 1 > fb_helper->connector_info_alloc_count) { 156 temp = krealloc(fb_helper->connector_info, sizeof(struct drm_fb_helper_connector *) * (fb_helper->connector_count + 1), M_DRM, M_WAITOK); 157 if (!temp) 158 return -ENOMEM; 159 160 fb_helper->connector_info_alloc_count = fb_helper->connector_count + 1; 161 fb_helper->connector_info = temp; 162 } 163 164 165 fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); 166 if (!fb_helper_connector) 167 return -ENOMEM; 168 169 drm_connector_reference(connector); 170 fb_helper_connector->connector = connector; 171 fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; 172 return 0; 173 } 174 EXPORT_SYMBOL(drm_fb_helper_add_one_connector); 175 176 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 177 struct drm_connector *connector) 178 { 179 struct drm_fb_helper_connector *fb_helper_connector; 180 int i, j; 181 182 if (!drm_fbdev_emulation) 183 return 0; 184 185 WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex)); 186 187 for (i = 0; i < fb_helper->connector_count; i++) { 188 if (fb_helper->connector_info[i]->connector == connector) 189 break; 190 } 191 192 if (i == fb_helper->connector_count) 193 return -EINVAL; 194 fb_helper_connector = fb_helper->connector_info[i]; 195 drm_connector_unreference(fb_helper_connector->connector); 196 197 for (j = i + 1; j < fb_helper->connector_count; j++) { 198 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; 199 } 200 fb_helper->connector_count--; 201 kfree(fb_helper_connector); 202 203 return 0; 204 } 205 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); 206 207 #if 0 208 static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper) 209 { 210 uint16_t *r_base, *g_base, *b_base; 211 int i; 212 213 if (helper->funcs->gamma_get == NULL) 214 return; 215 216 r_base = crtc->gamma_store; 217 g_base = r_base + crtc->gamma_size; 218 b_base = g_base + crtc->gamma_size; 219 220 for (i = 0; i < crtc->gamma_size; i++) 221 helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i); 222 } 223 224 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) 225 { 226 uint16_t *r_base, *g_base, *b_base; 227 228 if (crtc->funcs->gamma_set == NULL) 229 return; 230 231 r_base = crtc->gamma_store; 232 g_base = r_base + crtc->gamma_size; 233 b_base = g_base + crtc->gamma_size; 234 235 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size); 236 } 237 #endif 238 239 /** 240 * drm_fb_helper_debug_enter - implementation for ->fb_debug_enter 241 * @info: fbdev registered by the helper 242 */ 243 int drm_fb_helper_debug_enter(struct fb_info *info) 244 { 245 struct drm_fb_helper *helper = info->par; 246 const struct drm_crtc_helper_funcs *funcs; 247 int i; 248 249 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 250 for (i = 0; i < helper->crtc_count; i++) { 251 struct drm_mode_set *mode_set = 252 &helper->crtc_info[i].mode_set; 253 254 if (!mode_set->crtc->enabled) 255 continue; 256 257 funcs = mode_set->crtc->helper_private; 258 #if 0 259 drm_fb_helper_save_lut_atomic(mode_set->crtc, helper); 260 #endif 261 funcs->mode_set_base_atomic(mode_set->crtc, 262 mode_set->fb, 263 mode_set->x, 264 mode_set->y, 265 ENTER_ATOMIC_MODE_SET); 266 } 267 } 268 269 return 0; 270 } 271 EXPORT_SYMBOL(drm_fb_helper_debug_enter); 272 273 #if 0 274 /* Find the real fb for a given fb helper CRTC */ 275 static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc) 276 { 277 struct drm_device *dev = crtc->dev; 278 struct drm_crtc *c; 279 280 drm_for_each_crtc(c, dev) { 281 if (crtc->base.id == c->base.id) 282 return c->primary->fb; 283 } 284 285 return NULL; 286 } 287 288 /** 289 * drm_fb_helper_debug_leave - implementation for ->fb_debug_leave 290 * @info: fbdev registered by the helper 291 */ 292 int drm_fb_helper_debug_leave(struct fb_info *info) 293 { 294 struct drm_fb_helper *helper = info->par; 295 struct drm_crtc *crtc; 296 const struct drm_crtc_helper_funcs *funcs; 297 struct drm_framebuffer *fb; 298 int i; 299 300 for (i = 0; i < helper->crtc_count; i++) { 301 struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; 302 crtc = mode_set->crtc; 303 funcs = crtc->helper_private; 304 fb = drm_mode_config_fb(crtc); 305 306 if (!crtc->enabled) 307 continue; 308 309 if (!fb) { 310 DRM_ERROR("no fb to restore??\n"); 311 continue; 312 } 313 314 drm_fb_helper_restore_lut_atomic(mode_set->crtc); 315 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, 316 crtc->y, LEAVE_ATOMIC_MODE_SET); 317 } 318 319 return 0; 320 } 321 EXPORT_SYMBOL(drm_fb_helper_debug_leave); 322 #endif 323 324 static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper) 325 { 326 struct drm_device *dev = fb_helper->dev; 327 struct drm_plane *plane; 328 struct drm_atomic_state *state; 329 int i, ret; 330 unsigned plane_mask; 331 332 state = drm_atomic_state_alloc(dev); 333 if (!state) 334 return -ENOMEM; 335 336 state->acquire_ctx = dev->mode_config.acquire_ctx; 337 retry: 338 plane_mask = 0; 339 drm_for_each_plane(plane, dev) { 340 struct drm_plane_state *plane_state; 341 342 plane_state = drm_atomic_get_plane_state(state, plane); 343 if (IS_ERR(plane_state)) { 344 ret = PTR_ERR(plane_state); 345 goto fail; 346 } 347 348 plane_state->rotation = DRM_ROTATE_0; 349 350 plane->old_fb = plane->fb; 351 plane_mask |= 1 << drm_plane_index(plane); 352 353 /* disable non-primary: */ 354 if (plane->type == DRM_PLANE_TYPE_PRIMARY) 355 continue; 356 357 ret = __drm_atomic_helper_disable_plane(plane, plane_state); 358 if (ret != 0) 359 goto fail; 360 } 361 362 for(i = 0; i < fb_helper->crtc_count; i++) { 363 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 364 365 ret = __drm_atomic_helper_set_config(mode_set, state); 366 if (ret != 0) 367 goto fail; 368 } 369 370 ret = drm_atomic_commit(state); 371 372 fail: 373 drm_atomic_clean_old_fb(dev, plane_mask, ret); 374 375 if (ret == -EDEADLK) 376 goto backoff; 377 378 if (ret != 0) 379 drm_atomic_state_free(state); 380 381 return ret; 382 383 backoff: 384 drm_atomic_state_clear(state); 385 drm_atomic_legacy_backoff(state); 386 387 goto retry; 388 } 389 390 static bool restore_fbdev_mode(struct drm_fb_helper *fb_helper) 391 { 392 struct drm_device *dev = fb_helper->dev; 393 struct drm_plane *plane; 394 bool error = false; 395 int i; 396 397 drm_warn_on_modeset_not_all_locked(dev); 398 399 if (dev->mode_config.funcs->atomic_commit) 400 return restore_fbdev_mode_atomic(fb_helper); 401 402 drm_for_each_plane(plane, dev) { 403 if (plane->type != DRM_PLANE_TYPE_PRIMARY) 404 drm_plane_force_disable(plane); 405 406 if (dev->mode_config.rotation_property) { 407 drm_mode_plane_set_obj_prop(plane, 408 dev->mode_config.rotation_property, 409 DRM_ROTATE_0); 410 } 411 } 412 413 for (i = 0; i < fb_helper->crtc_count; i++) { 414 struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set; 415 struct drm_crtc *crtc = mode_set->crtc; 416 int ret; 417 418 if (crtc->funcs->cursor_set) { 419 ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0); 420 if (ret) 421 error = true; 422 } 423 424 ret = drm_mode_set_config_internal(mode_set); 425 if (ret) 426 error = true; 427 } 428 return error; 429 } 430 431 /** 432 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration 433 * @fb_helper: fbcon to restore 434 * 435 * This should be called from driver's drm ->lastclose callback 436 * when implementing an fbcon on top of kms using this helper. This ensures that 437 * the user isn't greeted with a black screen when e.g. X dies. 438 * 439 * RETURNS: 440 * Zero if everything went ok, negative error code otherwise. 441 */ 442 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 443 { 444 struct drm_device *dev = fb_helper->dev; 445 bool do_delayed; 446 int ret; 447 448 if (!drm_fbdev_emulation) 449 return -ENODEV; 450 451 drm_modeset_lock_all(dev); 452 ret = restore_fbdev_mode(fb_helper); 453 454 do_delayed = fb_helper->delayed_hotplug; 455 if (do_delayed) 456 fb_helper->delayed_hotplug = false; 457 drm_modeset_unlock_all(dev); 458 459 if (do_delayed) 460 drm_fb_helper_hotplug_event(fb_helper); 461 return ret; 462 } 463 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); 464 465 static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper) 466 { 467 struct drm_device *dev = fb_helper->dev; 468 struct drm_crtc *crtc; 469 int bound = 0, crtcs_bound = 0; 470 471 /* Sometimes user space wants everything disabled, so don't steal the 472 * display if there's a master. */ 473 if (READ_ONCE(dev->master)) 474 return false; 475 476 drm_for_each_crtc(crtc, dev) { 477 if (crtc->primary->fb) 478 crtcs_bound++; 479 if (crtc->primary->fb == fb_helper->fb) 480 bound++; 481 } 482 483 if (bound < crtcs_bound) 484 return false; 485 486 return true; 487 } 488 489 #ifdef CONFIG_MAGIC_SYSRQ 490 /* 491 * restore fbcon display for all kms driver's using this helper, used for sysrq 492 * and panic handling. 493 */ 494 static bool drm_fb_helper_force_kernel_mode(void) 495 { 496 bool ret, error = false; 497 struct drm_fb_helper *helper; 498 499 if (list_empty(&kernel_fb_helper_list)) 500 return false; 501 502 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 503 struct drm_device *dev = helper->dev; 504 505 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 506 continue; 507 508 drm_modeset_lock_all(dev); 509 ret = restore_fbdev_mode(helper); 510 if (ret) 511 error = true; 512 drm_modeset_unlock_all(dev); 513 } 514 return error; 515 } 516 517 #if 0 518 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) 519 { 520 bool ret; 521 ret = drm_fb_helper_force_kernel_mode(); 522 if (ret == true) 523 DRM_ERROR("Failed to restore crtc configuration\n"); 524 } 525 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); 526 527 static void drm_fb_helper_sysrq(int dummy1) 528 { 529 schedule_work(&drm_fb_helper_restore_work); 530 } 531 532 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { 533 .handler = drm_fb_helper_sysrq, 534 .help_msg = "force-fb(V)", 535 .action_msg = "Restore framebuffer console", 536 }; 537 #else 538 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; 539 #endif 540 #endif 541 542 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) 543 { 544 struct drm_fb_helper *fb_helper = info->par; 545 struct drm_device *dev = fb_helper->dev; 546 struct drm_crtc *crtc; 547 struct drm_connector *connector; 548 int i, j; 549 550 /* 551 * For each CRTC in this fb, turn the connectors on/off. 552 */ 553 drm_modeset_lock_all(dev); 554 if (!drm_fb_helper_is_bound(fb_helper)) { 555 drm_modeset_unlock_all(dev); 556 return; 557 } 558 559 for (i = 0; i < fb_helper->crtc_count; i++) { 560 crtc = fb_helper->crtc_info[i].mode_set.crtc; 561 562 if (!crtc->enabled) 563 continue; 564 565 /* Walk the connectors & encoders on this fb turning them on/off */ 566 for (j = 0; j < fb_helper->connector_count; j++) { 567 connector = fb_helper->connector_info[j]->connector; 568 connector->funcs->dpms(connector, dpms_mode); 569 drm_object_property_set_value(&connector->base, 570 dev->mode_config.dpms_property, dpms_mode); 571 } 572 } 573 drm_modeset_unlock_all(dev); 574 } 575 576 /** 577 * drm_fb_helper_blank - implementation for ->fb_blank 578 * @blank: desired blanking state 579 * @info: fbdev registered by the helper 580 */ 581 int drm_fb_helper_blank(int blank, struct fb_info *info) 582 { 583 #ifdef __DragonFly__ 584 if (panicstr) 585 return -EBUSY; 586 #else 587 if (oops_in_progress) 588 return -EBUSY; 589 #endif 590 591 switch (blank) { 592 /* Display: On; HSync: On, VSync: On */ 593 case FB_BLANK_UNBLANK: 594 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); 595 break; 596 #if 0 597 /* Display: Off; HSync: On, VSync: On */ 598 case FB_BLANK_NORMAL: 599 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 600 break; 601 /* Display: Off; HSync: Off, VSync: On */ 602 case FB_BLANK_HSYNC_SUSPEND: 603 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 604 break; 605 /* Display: Off; HSync: On, VSync: Off */ 606 case FB_BLANK_VSYNC_SUSPEND: 607 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); 608 break; 609 #endif 610 /* Display: Off; HSync: Off, VSync: Off */ 611 case FB_BLANK_POWERDOWN: 612 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); 613 break; 614 } 615 return 0; 616 } 617 EXPORT_SYMBOL(drm_fb_helper_blank); 618 619 static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper, 620 struct drm_mode_set *modeset) 621 { 622 int i; 623 624 for (i = 0; i < modeset->num_connectors; i++) { 625 drm_connector_unreference(modeset->connectors[i]); 626 modeset->connectors[i] = NULL; 627 } 628 modeset->num_connectors = 0; 629 630 drm_mode_destroy(helper->dev, modeset->mode); 631 modeset->mode = NULL; 632 633 /* FIXME should hold a ref? */ 634 modeset->fb = NULL; 635 } 636 637 static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) 638 { 639 int i; 640 641 for (i = 0; i < helper->connector_count; i++) { 642 drm_connector_unreference(helper->connector_info[i]->connector); 643 kfree(helper->connector_info[i]); 644 } 645 kfree(helper->connector_info); 646 647 for (i = 0; i < helper->crtc_count; i++) { 648 struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set; 649 650 drm_fb_helper_modeset_release(helper, modeset); 651 kfree(modeset->connectors); 652 } 653 kfree(helper->crtc_info); 654 } 655 656 static void drm_fb_helper_resume_worker(struct work_struct *work) 657 { 658 #if 0 659 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 660 resume_work); 661 662 console_lock(); 663 fb_set_suspend(helper->fbdev, 0); 664 console_unlock(); 665 #endif 666 } 667 668 static void drm_fb_helper_dirty_work(struct work_struct *work) 669 { 670 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 671 dirty_work); 672 struct drm_clip_rect *clip = &helper->dirty_clip; 673 struct drm_clip_rect clip_copy; 674 unsigned long flags; 675 676 spin_lock_irqsave(&helper->dirty_lock, flags); 677 clip_copy = *clip; 678 clip->x1 = clip->y1 = ~0; 679 clip->x2 = clip->y2 = 0; 680 spin_unlock_irqrestore(&helper->dirty_lock, flags); 681 682 /* call dirty callback only when it has been really touched */ 683 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) 684 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1); 685 } 686 687 /** 688 * drm_fb_helper_prepare - setup a drm_fb_helper structure 689 * @dev: DRM device 690 * @helper: driver-allocated fbdev helper structure to set up 691 * @funcs: pointer to structure of functions associate with this helper 692 * 693 * Sets up the bare minimum to make the framebuffer helper usable. This is 694 * useful to implement race-free initialization of the polling helpers. 695 */ 696 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 697 const struct drm_fb_helper_funcs *funcs) 698 { 699 INIT_LIST_HEAD(&helper->kernel_fb_list); 700 lockinit(&helper->dirty_lock, "drm_fb_helper dirty_lock", 0, LK_CANRECURSE); 701 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); 702 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); 703 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; 704 helper->funcs = funcs; 705 helper->dev = dev; 706 } 707 EXPORT_SYMBOL(drm_fb_helper_prepare); 708 709 /** 710 * drm_fb_helper_init - initialize a drm_fb_helper structure 711 * @dev: drm device 712 * @fb_helper: driver-allocated fbdev helper structure to initialize 713 * @crtc_count: maximum number of crtcs to support in this fbdev emulation 714 * @max_conn_count: max connector count 715 * 716 * This allocates the structures for the fbdev helper with the given limits. 717 * Note that this won't yet touch the hardware (through the driver interfaces) 718 * nor register the fbdev. This is only done in drm_fb_helper_initial_config() 719 * to allow driver writes more control over the exact init sequence. 720 * 721 * Drivers must call drm_fb_helper_prepare() before calling this function. 722 * 723 * RETURNS: 724 * Zero if everything went ok, nonzero otherwise. 725 */ 726 int drm_fb_helper_init(struct drm_device *dev, 727 struct drm_fb_helper *fb_helper, 728 int crtc_count, int max_conn_count) 729 { 730 struct drm_crtc *crtc; 731 int i; 732 733 if (!drm_fbdev_emulation) 734 return 0; 735 736 if (!max_conn_count) 737 return -EINVAL; 738 739 fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); 740 if (!fb_helper->crtc_info) 741 return -ENOMEM; 742 743 fb_helper->crtc_count = crtc_count; 744 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); 745 if (!fb_helper->connector_info) { 746 kfree(fb_helper->crtc_info); 747 return -ENOMEM; 748 } 749 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; 750 fb_helper->connector_count = 0; 751 752 for (i = 0; i < crtc_count; i++) { 753 fb_helper->crtc_info[i].mode_set.connectors = 754 kcalloc(max_conn_count, 755 sizeof(struct drm_connector *), 756 GFP_KERNEL); 757 758 if (!fb_helper->crtc_info[i].mode_set.connectors) 759 goto out_free; 760 fb_helper->crtc_info[i].mode_set.num_connectors = 0; 761 } 762 763 i = 0; 764 drm_for_each_crtc(crtc, dev) { 765 fb_helper->crtc_info[i].mode_set.crtc = crtc; 766 i++; 767 } 768 769 return 0; 770 out_free: 771 drm_fb_helper_crtc_free(fb_helper); 772 return -ENOMEM; 773 } 774 EXPORT_SYMBOL(drm_fb_helper_init); 775 776 /** 777 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members 778 * @fb_helper: driver-allocated fbdev helper 779 * 780 * A helper to alloc fb_info and the members cmap and apertures. Called 781 * by the driver within the fb_probe fb_helper callback function. 782 * 783 * RETURNS: 784 * fb_info pointer if things went okay, pointer containing error code 785 * otherwise 786 */ 787 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) 788 { 789 struct device *dev = fb_helper->dev->dev; 790 struct fb_info *info; 791 792 info = framebuffer_alloc(0, dev); 793 if (!info) 794 return ERR_PTR(-ENOMEM); 795 796 #if 0 797 ret = fb_alloc_cmap(&info->cmap, 256, 0); 798 if (ret) 799 goto err_release; 800 801 info->apertures = alloc_apertures(1); 802 if (!info->apertures) { 803 ret = -ENOMEM; 804 goto err_free_cmap; 805 } 806 #endif 807 808 fb_helper->fbdev = info; 809 810 return info; 811 812 #if 0 813 err_free_cmap: 814 fb_dealloc_cmap(&info->cmap); 815 err_release: 816 framebuffer_release(info); 817 return ERR_PTR(ret); 818 #endif 819 } 820 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); 821 822 /** 823 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device 824 * @fb_helper: driver-allocated fbdev helper 825 * 826 * A wrapper around unregister_framebuffer, to release the fb_info 827 * framebuffer device 828 */ 829 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 830 { 831 if (fb_helper && fb_helper->fbdev) 832 unregister_framebuffer(fb_helper->fbdev); 833 } 834 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); 835 836 /** 837 * drm_fb_helper_release_fbi - dealloc fb_info and its members 838 * @fb_helper: driver-allocated fbdev helper 839 * 840 * A helper to free memory taken by fb_info and the members cmap and 841 * apertures 842 */ 843 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper) 844 { 845 if (fb_helper) { 846 struct fb_info *info = fb_helper->fbdev; 847 848 if (info) { 849 #if 0 850 if (info->cmap.len) 851 fb_dealloc_cmap(&info->cmap); 852 #endif 853 framebuffer_release(info); 854 } 855 856 fb_helper->fbdev = NULL; 857 } 858 } 859 EXPORT_SYMBOL(drm_fb_helper_release_fbi); 860 861 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 862 { 863 if (!drm_fbdev_emulation) 864 return; 865 866 if (!list_empty(&fb_helper->kernel_fb_list)) { 867 list_del(&fb_helper->kernel_fb_list); 868 if (list_empty(&kernel_fb_helper_list)) { 869 #if 0 870 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 871 #endif 872 } 873 } 874 875 drm_fb_helper_crtc_free(fb_helper); 876 877 } 878 EXPORT_SYMBOL(drm_fb_helper_fini); 879 880 #if 0 881 /** 882 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer 883 * @fb_helper: driver-allocated fbdev helper 884 * 885 * A wrapper around unlink_framebuffer implemented by fbdev core 886 */ 887 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) 888 { 889 if (fb_helper && fb_helper->fbdev) 890 unlink_framebuffer(fb_helper->fbdev); 891 } 892 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); 893 894 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, 895 u32 width, u32 height) 896 { 897 struct drm_fb_helper *helper = info->par; 898 struct drm_clip_rect *clip = &helper->dirty_clip; 899 unsigned long flags; 900 901 if (!helper->fb->funcs->dirty) 902 return; 903 904 spin_lock_irqsave(&helper->dirty_lock, flags); 905 clip->x1 = min_t(u32, clip->x1, x); 906 clip->y1 = min_t(u32, clip->y1, y); 907 clip->x2 = max_t(u32, clip->x2, x + width); 908 clip->y2 = max_t(u32, clip->y2, y + height); 909 spin_unlock_irqrestore(&helper->dirty_lock, flags); 910 911 schedule_work(&helper->dirty_work); 912 } 913 914 /** 915 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function 916 * @info: fb_info struct pointer 917 * @pagelist: list of dirty mmap framebuffer pages 918 * 919 * This function is used as the &fb_deferred_io ->deferred_io 920 * callback function for flushing the fbdev mmap writes. 921 */ 922 void drm_fb_helper_deferred_io(struct fb_info *info, 923 struct list_head *pagelist) 924 { 925 unsigned long start, end, min, max; 926 struct page *page; 927 u32 y1, y2; 928 929 min = ULONG_MAX; 930 max = 0; 931 list_for_each_entry(page, pagelist, lru) { 932 start = page->index << PAGE_SHIFT; 933 end = start + PAGE_SIZE - 1; 934 min = min(min, start); 935 max = max(max, end); 936 } 937 938 if (min < max) { 939 y1 = min / info->fix.line_length; 940 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), 941 info->var.yres); 942 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); 943 } 944 } 945 EXPORT_SYMBOL(drm_fb_helper_deferred_io); 946 947 /** 948 * drm_fb_helper_sys_read - wrapper around fb_sys_read 949 * @info: fb_info struct pointer 950 * @buf: userspace buffer to read from framebuffer memory 951 * @count: number of bytes to read from framebuffer memory 952 * @ppos: read offset within framebuffer memory 953 * 954 * A wrapper around fb_sys_read implemented by fbdev core 955 */ 956 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 957 size_t count, loff_t *ppos) 958 { 959 return fb_sys_read(info, buf, count, ppos); 960 } 961 EXPORT_SYMBOL(drm_fb_helper_sys_read); 962 963 /** 964 * drm_fb_helper_sys_write - wrapper around fb_sys_write 965 * @info: fb_info struct pointer 966 * @buf: userspace buffer to write to framebuffer memory 967 * @count: number of bytes to write to framebuffer memory 968 * @ppos: write offset within framebuffer memory 969 * 970 * A wrapper around fb_sys_write implemented by fbdev core 971 */ 972 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 973 size_t count, loff_t *ppos) 974 { 975 ssize_t ret; 976 977 ret = fb_sys_write(info, buf, count, ppos); 978 if (ret > 0) 979 drm_fb_helper_dirty(info, 0, 0, info->var.xres, 980 info->var.yres); 981 982 return ret; 983 } 984 EXPORT_SYMBOL(drm_fb_helper_sys_write); 985 986 /** 987 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect 988 * @info: fbdev registered by the helper 989 * @rect: info about rectangle to fill 990 * 991 * A wrapper around sys_fillrect implemented by fbdev core 992 */ 993 void drm_fb_helper_sys_fillrect(struct fb_info *info, 994 const struct fb_fillrect *rect) 995 { 996 sys_fillrect(info, rect); 997 drm_fb_helper_dirty(info, rect->dx, rect->dy, 998 rect->width, rect->height); 999 } 1000 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); 1001 1002 /** 1003 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea 1004 * @info: fbdev registered by the helper 1005 * @area: info about area to copy 1006 * 1007 * A wrapper around sys_copyarea implemented by fbdev core 1008 */ 1009 void drm_fb_helper_sys_copyarea(struct fb_info *info, 1010 const struct fb_copyarea *area) 1011 { 1012 sys_copyarea(info, area); 1013 drm_fb_helper_dirty(info, area->dx, area->dy, 1014 area->width, area->height); 1015 } 1016 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); 1017 1018 /** 1019 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit 1020 * @info: fbdev registered by the helper 1021 * @image: info about image to blit 1022 * 1023 * A wrapper around sys_imageblit implemented by fbdev core 1024 */ 1025 void drm_fb_helper_sys_imageblit(struct fb_info *info, 1026 const struct fb_image *image) 1027 { 1028 sys_imageblit(info, image); 1029 drm_fb_helper_dirty(info, image->dx, image->dy, 1030 image->width, image->height); 1031 } 1032 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); 1033 1034 /** 1035 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect 1036 * @info: fbdev registered by the helper 1037 * @rect: info about rectangle to fill 1038 * 1039 * A wrapper around cfb_imageblit implemented by fbdev core 1040 */ 1041 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 1042 const struct fb_fillrect *rect) 1043 { 1044 cfb_fillrect(info, rect); 1045 drm_fb_helper_dirty(info, rect->dx, rect->dy, 1046 rect->width, rect->height); 1047 } 1048 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); 1049 1050 /** 1051 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea 1052 * @info: fbdev registered by the helper 1053 * @area: info about area to copy 1054 * 1055 * A wrapper around cfb_copyarea implemented by fbdev core 1056 */ 1057 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 1058 const struct fb_copyarea *area) 1059 { 1060 cfb_copyarea(info, area); 1061 drm_fb_helper_dirty(info, area->dx, area->dy, 1062 area->width, area->height); 1063 } 1064 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); 1065 1066 /** 1067 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit 1068 * @info: fbdev registered by the helper 1069 * @image: info about image to blit 1070 * 1071 * A wrapper around cfb_imageblit implemented by fbdev core 1072 */ 1073 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 1074 const struct fb_image *image) 1075 { 1076 cfb_imageblit(info, image); 1077 drm_fb_helper_dirty(info, image->dx, image->dy, 1078 image->width, image->height); 1079 } 1080 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); 1081 1082 /** 1083 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 1084 * @fb_helper: driver-allocated fbdev helper 1085 * @state: desired state, zero to resume, non-zero to suspend 1086 * 1087 * A wrapper around fb_set_suspend implemented by fbdev core. 1088 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take 1089 * the lock yourself 1090 */ 1091 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state) 1092 { 1093 #if 0 1094 if (fb_helper && fb_helper->fbdev) 1095 fb_set_suspend(fb_helper->fbdev, state); 1096 #endif 1097 } 1098 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 1099 1100 /** 1101 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also 1102 * takes the console lock 1103 * @fb_helper: driver-allocated fbdev helper 1104 * @suspend: whether to suspend or resume 1105 * 1106 * A wrapper around fb_set_suspend() that takes the console lock. If the lock 1107 * isn't available on resume, a worker is tasked with waiting for the lock 1108 * to become available. The console lock can be pretty contented on resume 1109 * due to all the printk activity. 1110 * 1111 * This function can be called multiple times with the same state since 1112 * &fb_info->state is checked to see if fbdev is running or not before locking. 1113 * 1114 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. 1115 */ 1116 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 1117 bool suspend) 1118 { 1119 if (!fb_helper || !fb_helper->fbdev) 1120 return; 1121 1122 /* make sure there's no pending/ongoing resume */ 1123 flush_work(&fb_helper->resume_work); 1124 1125 if (suspend) { 1126 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) 1127 return; 1128 1129 #if 0 1130 console_lock(); 1131 #endif 1132 1133 } else { 1134 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) 1135 return; 1136 1137 if (!console_trylock()) { 1138 schedule_work(&fb_helper->resume_work); 1139 return; 1140 } 1141 } 1142 1143 #if 0 1144 fb_set_suspend(fb_helper->fbdev, suspend); 1145 console_unlock(); 1146 #endif 1147 } 1148 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); 1149 1150 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, 1151 u16 blue, u16 regno, struct fb_info *info) 1152 { 1153 struct drm_fb_helper *fb_helper = info->par; 1154 struct drm_framebuffer *fb = fb_helper->fb; 1155 1156 if (info->fix.visual == FB_VISUAL_TRUECOLOR) { 1157 u32 *palette; 1158 u32 value; 1159 /* place color in psuedopalette */ 1160 if (regno > 16) 1161 return -EINVAL; 1162 palette = (u32 *)info->pseudo_palette; 1163 red >>= (16 - info->var.red.length); 1164 green >>= (16 - info->var.green.length); 1165 blue >>= (16 - info->var.blue.length); 1166 value = (red << info->var.red.offset) | 1167 (green << info->var.green.offset) | 1168 (blue << info->var.blue.offset); 1169 if (info->var.transp.length > 0) { 1170 u32 mask = (1 << info->var.transp.length) - 1; 1171 mask <<= info->var.transp.offset; 1172 value |= mask; 1173 } 1174 palette[regno] = value; 1175 return 0; 1176 } 1177 1178 /* 1179 * The driver really shouldn't advertise pseudo/directcolor 1180 * visuals if it can't deal with the palette. 1181 */ 1182 if (WARN_ON(!fb_helper->funcs->gamma_set || 1183 !fb_helper->funcs->gamma_get)) 1184 return -EINVAL; 1185 1186 WARN_ON(fb->bits_per_pixel != 8); 1187 1188 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno); 1189 1190 return 0; 1191 } 1192 1193 /** 1194 * drm_fb_helper_setcmap - implementation for ->fb_setcmap 1195 * @cmap: cmap to set 1196 * @info: fbdev registered by the helper 1197 */ 1198 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 1199 { 1200 struct drm_fb_helper *fb_helper = info->par; 1201 struct drm_device *dev = fb_helper->dev; 1202 const struct drm_crtc_helper_funcs *crtc_funcs; 1203 u16 *red, *green, *blue, *transp; 1204 struct drm_crtc *crtc; 1205 int i, j, rc = 0; 1206 int start; 1207 1208 if (oops_in_progress) 1209 return -EBUSY; 1210 1211 drm_modeset_lock_all(dev); 1212 if (!drm_fb_helper_is_bound(fb_helper)) { 1213 drm_modeset_unlock_all(dev); 1214 return -EBUSY; 1215 } 1216 1217 for (i = 0; i < fb_helper->crtc_count; i++) { 1218 crtc = fb_helper->crtc_info[i].mode_set.crtc; 1219 crtc_funcs = crtc->helper_private; 1220 1221 red = cmap->red; 1222 green = cmap->green; 1223 blue = cmap->blue; 1224 transp = cmap->transp; 1225 start = cmap->start; 1226 1227 for (j = 0; j < cmap->len; j++) { 1228 u16 hred, hgreen, hblue, htransp = 0xffff; 1229 1230 hred = *red++; 1231 hgreen = *green++; 1232 hblue = *blue++; 1233 1234 if (transp) 1235 htransp = *transp++; 1236 1237 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); 1238 if (rc) 1239 goto out; 1240 } 1241 if (crtc_funcs->load_lut) 1242 crtc_funcs->load_lut(crtc); 1243 } 1244 out: 1245 drm_modeset_unlock_all(dev); 1246 return rc; 1247 } 1248 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1249 1250 /** 1251 * drm_fb_helper_check_var - implementation for ->fb_check_var 1252 * @var: screeninfo to check 1253 * @info: fbdev registered by the helper 1254 */ 1255 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1256 struct fb_info *info) 1257 { 1258 struct drm_fb_helper *fb_helper = info->par; 1259 struct drm_framebuffer *fb = fb_helper->fb; 1260 int depth; 1261 1262 if (var->pixclock != 0 || in_dbg_master()) 1263 return -EINVAL; 1264 1265 /* Need to resize the fb object !!! */ 1266 if (var->bits_per_pixel > fb->bits_per_pixel || 1267 var->xres > fb->width || var->yres > fb->height || 1268 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1269 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " 1270 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1271 var->xres, var->yres, var->bits_per_pixel, 1272 var->xres_virtual, var->yres_virtual, 1273 fb->width, fb->height, fb->bits_per_pixel); 1274 return -EINVAL; 1275 } 1276 1277 switch (var->bits_per_pixel) { 1278 case 16: 1279 depth = (var->green.length == 6) ? 16 : 15; 1280 break; 1281 case 32: 1282 depth = (var->transp.length > 0) ? 32 : 24; 1283 break; 1284 default: 1285 depth = var->bits_per_pixel; 1286 break; 1287 } 1288 1289 switch (depth) { 1290 case 8: 1291 var->red.offset = 0; 1292 var->green.offset = 0; 1293 var->blue.offset = 0; 1294 var->red.length = 8; 1295 var->green.length = 8; 1296 var->blue.length = 8; 1297 var->transp.length = 0; 1298 var->transp.offset = 0; 1299 break; 1300 case 15: 1301 var->red.offset = 10; 1302 var->green.offset = 5; 1303 var->blue.offset = 0; 1304 var->red.length = 5; 1305 var->green.length = 5; 1306 var->blue.length = 5; 1307 var->transp.length = 1; 1308 var->transp.offset = 15; 1309 break; 1310 case 16: 1311 var->red.offset = 11; 1312 var->green.offset = 5; 1313 var->blue.offset = 0; 1314 var->red.length = 5; 1315 var->green.length = 6; 1316 var->blue.length = 5; 1317 var->transp.length = 0; 1318 var->transp.offset = 0; 1319 break; 1320 case 24: 1321 var->red.offset = 16; 1322 var->green.offset = 8; 1323 var->blue.offset = 0; 1324 var->red.length = 8; 1325 var->green.length = 8; 1326 var->blue.length = 8; 1327 var->transp.length = 0; 1328 var->transp.offset = 0; 1329 break; 1330 case 32: 1331 var->red.offset = 16; 1332 var->green.offset = 8; 1333 var->blue.offset = 0; 1334 var->red.length = 8; 1335 var->green.length = 8; 1336 var->blue.length = 8; 1337 var->transp.length = 8; 1338 var->transp.offset = 24; 1339 break; 1340 default: 1341 return -EINVAL; 1342 } 1343 return 0; 1344 } 1345 EXPORT_SYMBOL(drm_fb_helper_check_var); 1346 #endif 1347 1348 /** 1349 * drm_fb_helper_set_par - implementation for ->fb_set_par 1350 * @info: fbdev registered by the helper 1351 * 1352 * This will let fbcon do the mode init and is called at initialization time by 1353 * the fbdev core when registering the driver, and later on through the hotplug 1354 * callback. 1355 */ 1356 int drm_fb_helper_set_par(struct fb_info *info) 1357 { 1358 struct drm_fb_helper *fb_helper = info->par; 1359 #if 0 1360 struct fb_var_screeninfo *var = &info->var; 1361 #endif 1362 1363 #ifdef __DragonFly__ 1364 if (panicstr) 1365 return -EBUSY; 1366 #else 1367 if (oops_in_progress) 1368 return -EBUSY; 1369 #endif 1370 1371 #if 0 1372 if (var->pixclock != 0) { 1373 DRM_ERROR("PIXEL CLOCK SET\n"); 1374 return -EINVAL; 1375 } 1376 #endif 1377 1378 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 1379 1380 return 0; 1381 } 1382 EXPORT_SYMBOL(drm_fb_helper_set_par); 1383 1384 #if 0 1385 static int pan_display_atomic(struct fb_var_screeninfo *var, 1386 struct fb_info *info) 1387 { 1388 struct drm_fb_helper *fb_helper = info->par; 1389 struct drm_device *dev = fb_helper->dev; 1390 struct drm_atomic_state *state; 1391 struct drm_plane *plane; 1392 int i, ret; 1393 unsigned plane_mask; 1394 1395 state = drm_atomic_state_alloc(dev); 1396 if (!state) 1397 return -ENOMEM; 1398 1399 state->acquire_ctx = dev->mode_config.acquire_ctx; 1400 retry: 1401 plane_mask = 0; 1402 for(i = 0; i < fb_helper->crtc_count; i++) { 1403 struct drm_mode_set *mode_set; 1404 1405 mode_set = &fb_helper->crtc_info[i].mode_set; 1406 1407 mode_set->x = var->xoffset; 1408 mode_set->y = var->yoffset; 1409 1410 ret = __drm_atomic_helper_set_config(mode_set, state); 1411 if (ret != 0) 1412 goto fail; 1413 1414 plane = mode_set->crtc->primary; 1415 plane_mask |= (1 << drm_plane_index(plane)); 1416 plane->old_fb = plane->fb; 1417 } 1418 1419 ret = drm_atomic_commit(state); 1420 if (ret != 0) 1421 goto fail; 1422 1423 info->var.xoffset = var->xoffset; 1424 info->var.yoffset = var->yoffset; 1425 1426 1427 fail: 1428 drm_atomic_clean_old_fb(dev, plane_mask, ret); 1429 1430 if (ret == -EDEADLK) 1431 goto backoff; 1432 1433 if (ret != 0) 1434 drm_atomic_state_free(state); 1435 1436 return ret; 1437 1438 backoff: 1439 drm_atomic_state_clear(state); 1440 drm_atomic_legacy_backoff(state); 1441 1442 goto retry; 1443 } 1444 1445 /** 1446 * drm_fb_helper_pan_display - implementation for ->fb_pan_display 1447 * @var: updated screen information 1448 * @info: fbdev registered by the helper 1449 */ 1450 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1451 struct fb_info *info) 1452 { 1453 struct drm_fb_helper *fb_helper = info->par; 1454 struct drm_device *dev = fb_helper->dev; 1455 struct drm_mode_set *modeset; 1456 int ret = 0; 1457 int i; 1458 1459 if (oops_in_progress) 1460 return -EBUSY; 1461 1462 drm_modeset_lock_all(dev); 1463 if (!drm_fb_helper_is_bound(fb_helper)) { 1464 drm_modeset_unlock_all(dev); 1465 return -EBUSY; 1466 } 1467 1468 if (dev->mode_config.funcs->atomic_commit) { 1469 ret = pan_display_atomic(var, info); 1470 goto unlock; 1471 } 1472 1473 for (i = 0; i < fb_helper->crtc_count; i++) { 1474 modeset = &fb_helper->crtc_info[i].mode_set; 1475 1476 modeset->x = var->xoffset; 1477 modeset->y = var->yoffset; 1478 1479 if (modeset->num_connectors) { 1480 ret = drm_mode_set_config_internal(modeset); 1481 if (!ret) { 1482 info->var.xoffset = var->xoffset; 1483 info->var.yoffset = var->yoffset; 1484 } 1485 } 1486 } 1487 unlock: 1488 drm_modeset_unlock_all(dev); 1489 return ret; 1490 } 1491 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1492 #endif 1493 1494 /* 1495 * Allocates the backing storage and sets up the fbdev info structure through 1496 * the ->fb_probe callback and then registers the fbdev and sets up the panic 1497 * notifier. 1498 */ 1499 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1500 int preferred_bpp) 1501 { 1502 int ret = 0; 1503 int crtc_count = 0; 1504 int i; 1505 struct fb_info *info; 1506 struct drm_fb_helper_surface_size sizes; 1507 int gamma_size = 0; 1508 #ifdef __DragonFly__ 1509 int kms_console = 1; 1510 #endif 1511 1512 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1513 sizes.surface_depth = 24; 1514 sizes.surface_bpp = 32; 1515 sizes.fb_width = (unsigned)-1; 1516 sizes.fb_height = (unsigned)-1; 1517 1518 /* if driver picks 8 or 16 by default use that 1519 for both depth/bpp */ 1520 if (preferred_bpp != sizes.surface_bpp) 1521 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1522 1523 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1524 for (i = 0; i < fb_helper->connector_count; i++) { 1525 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 1526 struct drm_cmdline_mode *cmdline_mode; 1527 1528 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1529 1530 if (cmdline_mode->bpp_specified) { 1531 switch (cmdline_mode->bpp) { 1532 case 8: 1533 sizes.surface_depth = sizes.surface_bpp = 8; 1534 break; 1535 case 15: 1536 sizes.surface_depth = 15; 1537 sizes.surface_bpp = 16; 1538 break; 1539 case 16: 1540 sizes.surface_depth = sizes.surface_bpp = 16; 1541 break; 1542 case 24: 1543 sizes.surface_depth = sizes.surface_bpp = 24; 1544 break; 1545 case 32: 1546 sizes.surface_depth = 24; 1547 sizes.surface_bpp = 32; 1548 break; 1549 } 1550 break; 1551 } 1552 } 1553 1554 crtc_count = 0; 1555 for (i = 0; i < fb_helper->crtc_count; i++) { 1556 struct drm_display_mode *desired_mode; 1557 struct drm_mode_set *mode_set; 1558 int x, y, j; 1559 /* in case of tile group, are we the last tile vert or horiz? 1560 * If no tile group you are always the last one both vertically 1561 * and horizontally 1562 */ 1563 bool lastv = true, lasth = true; 1564 1565 desired_mode = fb_helper->crtc_info[i].desired_mode; 1566 mode_set = &fb_helper->crtc_info[i].mode_set; 1567 1568 if (!desired_mode) 1569 continue; 1570 1571 crtc_count++; 1572 1573 x = fb_helper->crtc_info[i].x; 1574 y = fb_helper->crtc_info[i].y; 1575 1576 if (gamma_size == 0) 1577 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; 1578 1579 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); 1580 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); 1581 1582 for (j = 0; j < mode_set->num_connectors; j++) { 1583 struct drm_connector *connector = mode_set->connectors[j]; 1584 if (connector->has_tile) { 1585 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1586 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1587 /* cloning to multiple tiles is just crazy-talk, so: */ 1588 break; 1589 } 1590 } 1591 1592 if (lasth) 1593 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); 1594 if (lastv) 1595 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); 1596 } 1597 1598 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1599 /* hmm everyone went away - assume VGA cable just fell out 1600 and will come back later. */ 1601 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); 1602 sizes.fb_width = sizes.surface_width = 1024; 1603 sizes.fb_height = sizes.surface_height = 768; 1604 } 1605 1606 /* push down into drivers */ 1607 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1608 if (ret < 0) 1609 return ret; 1610 1611 info = fb_helper->fbdev; 1612 1613 /* 1614 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug 1615 * events, but at init time drm_setup_crtcs needs to be called before 1616 * the fb is allocated (since we need to figure out the desired size of 1617 * the fb before we can allocate it ...). Hence we need to fix things up 1618 * here again. 1619 */ 1620 for (i = 0; i < fb_helper->crtc_count; i++) 1621 if (fb_helper->crtc_info[i].mode_set.num_connectors) 1622 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; 1623 1624 #ifdef __DragonFly__ 1625 TUNABLE_INT_FETCH("kern.kms_console", &kms_console); 1626 if (kms_console) { 1627 if (register_framebuffer(info) < 0) 1628 return -EINVAL; 1629 1630 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1631 } 1632 #else 1633 info->var.pixclock = 0; 1634 if (register_framebuffer(info) < 0) 1635 return -EINVAL; 1636 1637 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", 1638 info->node, info->fix.id); 1639 1640 if (list_empty(&kernel_fb_helper_list)) { 1641 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 1642 } 1643 1644 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1645 #endif 1646 1647 return 0; 1648 } 1649 1650 #if 0 1651 /** 1652 * drm_fb_helper_fill_fix - initializes fixed fbdev information 1653 * @info: fbdev registered by the helper 1654 * @pitch: desired pitch 1655 * @depth: desired depth 1656 * 1657 * Helper to fill in the fixed fbdev information useful for a non-accelerated 1658 * fbdev emulations. Drivers which support acceleration methods which impose 1659 * additional constraints need to set up their own limits. 1660 * 1661 * Drivers should call this (or their equivalent setup code) from their 1662 * ->fb_probe callback. 1663 */ 1664 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1665 uint32_t depth) 1666 { 1667 info->fix.type = FB_TYPE_PACKED_PIXELS; 1668 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1669 FB_VISUAL_TRUECOLOR; 1670 info->fix.mmio_start = 0; 1671 info->fix.mmio_len = 0; 1672 info->fix.type_aux = 0; 1673 info->fix.xpanstep = 1; /* doing it in hw */ 1674 info->fix.ypanstep = 1; /* doing it in hw */ 1675 info->fix.ywrapstep = 0; 1676 info->fix.accel = FB_ACCEL_NONE; 1677 1678 info->fix.line_length = pitch; 1679 return; 1680 } 1681 EXPORT_SYMBOL(drm_fb_helper_fill_fix); 1682 1683 /** 1684 * drm_fb_helper_fill_var - initalizes variable fbdev information 1685 * @info: fbdev instance to set up 1686 * @fb_helper: fb helper instance to use as template 1687 * @fb_width: desired fb width 1688 * @fb_height: desired fb height 1689 * 1690 * Sets up the variable fbdev metainformation from the given fb helper instance 1691 * and the drm framebuffer allocated in fb_helper->fb. 1692 * 1693 * Drivers should call this (or their equivalent setup code) from their 1694 * ->fb_probe callback after having allocated the fbdev backing 1695 * storage framebuffer. 1696 */ 1697 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 1698 uint32_t fb_width, uint32_t fb_height) 1699 { 1700 struct drm_framebuffer *fb = fb_helper->fb; 1701 info->pseudo_palette = fb_helper->pseudo_palette; 1702 info->var.xres_virtual = fb->width; 1703 info->var.yres_virtual = fb->height; 1704 info->var.bits_per_pixel = fb->bits_per_pixel; 1705 info->var.accel_flags = FB_ACCELF_TEXT; 1706 info->var.xoffset = 0; 1707 info->var.yoffset = 0; 1708 info->var.activate = FB_ACTIVATE_NOW; 1709 info->var.height = -1; 1710 info->var.width = -1; 1711 1712 switch (fb->depth) { 1713 case 8: 1714 info->var.red.offset = 0; 1715 info->var.green.offset = 0; 1716 info->var.blue.offset = 0; 1717 info->var.red.length = 8; /* 8bit DAC */ 1718 info->var.green.length = 8; 1719 info->var.blue.length = 8; 1720 info->var.transp.offset = 0; 1721 info->var.transp.length = 0; 1722 break; 1723 case 15: 1724 info->var.red.offset = 10; 1725 info->var.green.offset = 5; 1726 info->var.blue.offset = 0; 1727 info->var.red.length = 5; 1728 info->var.green.length = 5; 1729 info->var.blue.length = 5; 1730 info->var.transp.offset = 15; 1731 info->var.transp.length = 1; 1732 break; 1733 case 16: 1734 info->var.red.offset = 11; 1735 info->var.green.offset = 5; 1736 info->var.blue.offset = 0; 1737 info->var.red.length = 5; 1738 info->var.green.length = 6; 1739 info->var.blue.length = 5; 1740 info->var.transp.offset = 0; 1741 break; 1742 case 24: 1743 info->var.red.offset = 16; 1744 info->var.green.offset = 8; 1745 info->var.blue.offset = 0; 1746 info->var.red.length = 8; 1747 info->var.green.length = 8; 1748 info->var.blue.length = 8; 1749 info->var.transp.offset = 0; 1750 info->var.transp.length = 0; 1751 break; 1752 case 32: 1753 info->var.red.offset = 16; 1754 info->var.green.offset = 8; 1755 info->var.blue.offset = 0; 1756 info->var.red.length = 8; 1757 info->var.green.length = 8; 1758 info->var.blue.length = 8; 1759 info->var.transp.offset = 24; 1760 info->var.transp.length = 8; 1761 break; 1762 default: 1763 break; 1764 } 1765 1766 info->var.xres = fb_width; 1767 info->var.yres = fb_height; 1768 } 1769 EXPORT_SYMBOL(drm_fb_helper_fill_var); 1770 #endif 1771 1772 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1773 uint32_t maxX, 1774 uint32_t maxY) 1775 { 1776 struct drm_connector *connector; 1777 int count = 0; 1778 int i; 1779 1780 for (i = 0; i < fb_helper->connector_count; i++) { 1781 connector = fb_helper->connector_info[i]->connector; 1782 count += connector->funcs->fill_modes(connector, maxX, maxY); 1783 } 1784 1785 return count; 1786 } 1787 1788 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1789 { 1790 struct drm_display_mode *mode; 1791 1792 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1793 if (mode->hdisplay > width || 1794 mode->vdisplay > height) 1795 continue; 1796 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1797 return mode; 1798 } 1799 return NULL; 1800 } 1801 EXPORT_SYMBOL(drm_has_preferred_mode); 1802 1803 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1804 { 1805 return fb_connector->connector->cmdline_mode.specified; 1806 } 1807 1808 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, 1809 int width, int height) 1810 { 1811 struct drm_cmdline_mode *cmdline_mode; 1812 struct drm_display_mode *mode; 1813 bool prefer_non_interlace; 1814 1815 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1816 if (cmdline_mode->specified == false) 1817 return NULL; 1818 1819 /* attempt to find a matching mode in the list of modes 1820 * we have gotten so far, if not add a CVT mode that conforms 1821 */ 1822 if (cmdline_mode->rb || cmdline_mode->margins) 1823 goto create_mode; 1824 1825 prefer_non_interlace = !cmdline_mode->interlace; 1826 again: 1827 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1828 /* check width/height */ 1829 if (mode->hdisplay != cmdline_mode->xres || 1830 mode->vdisplay != cmdline_mode->yres) 1831 continue; 1832 1833 if (cmdline_mode->refresh_specified) { 1834 if (mode->vrefresh != cmdline_mode->refresh) 1835 continue; 1836 } 1837 1838 if (cmdline_mode->interlace) { 1839 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1840 continue; 1841 } else if (prefer_non_interlace) { 1842 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1843 continue; 1844 } 1845 return mode; 1846 } 1847 1848 if (prefer_non_interlace) { 1849 prefer_non_interlace = false; 1850 goto again; 1851 } 1852 1853 create_mode: 1854 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 1855 cmdline_mode); 1856 list_add(&mode->head, &fb_helper_conn->connector->modes); 1857 return mode; 1858 } 1859 EXPORT_SYMBOL(drm_pick_cmdline_mode); 1860 1861 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1862 { 1863 bool enable; 1864 1865 if (strict) 1866 enable = connector->status == connector_status_connected; 1867 else 1868 enable = connector->status != connector_status_disconnected; 1869 1870 return enable; 1871 } 1872 1873 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1874 bool *enabled) 1875 { 1876 bool any_enabled = false; 1877 struct drm_connector *connector; 1878 int i = 0; 1879 1880 for (i = 0; i < fb_helper->connector_count; i++) { 1881 connector = fb_helper->connector_info[i]->connector; 1882 enabled[i] = drm_connector_enabled(connector, true); 1883 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 1884 enabled[i] ? "yes" : "no"); 1885 any_enabled |= enabled[i]; 1886 } 1887 1888 if (any_enabled) 1889 return; 1890 1891 for (i = 0; i < fb_helper->connector_count; i++) { 1892 connector = fb_helper->connector_info[i]->connector; 1893 enabled[i] = drm_connector_enabled(connector, false); 1894 } 1895 } 1896 1897 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 1898 struct drm_display_mode **modes, 1899 struct drm_fb_offset *offsets, 1900 bool *enabled, int width, int height) 1901 { 1902 int count, i, j; 1903 bool can_clone = false; 1904 struct drm_fb_helper_connector *fb_helper_conn; 1905 struct drm_display_mode *dmt_mode, *mode; 1906 1907 /* only contemplate cloning in the single crtc case */ 1908 if (fb_helper->crtc_count > 1) 1909 return false; 1910 1911 count = 0; 1912 for (i = 0; i < fb_helper->connector_count; i++) { 1913 if (enabled[i]) 1914 count++; 1915 } 1916 1917 /* only contemplate cloning if more than one connector is enabled */ 1918 if (count <= 1) 1919 return false; 1920 1921 /* check the command line or if nothing common pick 1024x768 */ 1922 can_clone = true; 1923 for (i = 0; i < fb_helper->connector_count; i++) { 1924 if (!enabled[i]) 1925 continue; 1926 fb_helper_conn = fb_helper->connector_info[i]; 1927 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1928 if (!modes[i]) { 1929 can_clone = false; 1930 break; 1931 } 1932 for (j = 0; j < i; j++) { 1933 if (!enabled[j]) 1934 continue; 1935 if (!drm_mode_equal(modes[j], modes[i])) 1936 can_clone = false; 1937 } 1938 } 1939 1940 if (can_clone) { 1941 DRM_DEBUG_KMS("can clone using command line\n"); 1942 return true; 1943 } 1944 1945 /* try and find a 1024x768 mode on each connector */ 1946 can_clone = true; 1947 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 1948 1949 for (i = 0; i < fb_helper->connector_count; i++) { 1950 1951 if (!enabled[i]) 1952 continue; 1953 1954 fb_helper_conn = fb_helper->connector_info[i]; 1955 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1956 if (drm_mode_equal(mode, dmt_mode)) 1957 modes[i] = mode; 1958 } 1959 if (!modes[i]) 1960 can_clone = false; 1961 } 1962 1963 if (can_clone) { 1964 DRM_DEBUG_KMS("can clone using 1024x768\n"); 1965 return true; 1966 } 1967 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 1968 return false; 1969 } 1970 1971 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, 1972 struct drm_display_mode **modes, 1973 struct drm_fb_offset *offsets, 1974 int idx, 1975 int h_idx, int v_idx) 1976 { 1977 struct drm_fb_helper_connector *fb_helper_conn; 1978 int i; 1979 int hoffset = 0, voffset = 0; 1980 1981 for (i = 0; i < fb_helper->connector_count; i++) { 1982 fb_helper_conn = fb_helper->connector_info[i]; 1983 if (!fb_helper_conn->connector->has_tile) 1984 continue; 1985 1986 if (!modes[i] && (h_idx || v_idx)) { 1987 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, 1988 fb_helper_conn->connector->base.id); 1989 continue; 1990 } 1991 if (fb_helper_conn->connector->tile_h_loc < h_idx) 1992 hoffset += modes[i]->hdisplay; 1993 1994 if (fb_helper_conn->connector->tile_v_loc < v_idx) 1995 voffset += modes[i]->vdisplay; 1996 } 1997 offsets[idx].x = hoffset; 1998 offsets[idx].y = voffset; 1999 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); 2000 return 0; 2001 } 2002 2003 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 2004 struct drm_display_mode **modes, 2005 struct drm_fb_offset *offsets, 2006 bool *enabled, int width, int height) 2007 { 2008 struct drm_fb_helper_connector *fb_helper_conn; 2009 int i; 2010 uint64_t conn_configured = 0, mask; 2011 int tile_pass = 0; 2012 mask = (1 << fb_helper->connector_count) - 1; 2013 retry: 2014 for (i = 0; i < fb_helper->connector_count; i++) { 2015 fb_helper_conn = fb_helper->connector_info[i]; 2016 2017 if (conn_configured & (1 << i)) 2018 continue; 2019 2020 if (enabled[i] == false) { 2021 conn_configured |= (1 << i); 2022 continue; 2023 } 2024 2025 /* first pass over all the untiled connectors */ 2026 if (tile_pass == 0 && fb_helper_conn->connector->has_tile) 2027 continue; 2028 2029 if (tile_pass == 1) { 2030 if (fb_helper_conn->connector->tile_h_loc != 0 || 2031 fb_helper_conn->connector->tile_v_loc != 0) 2032 continue; 2033 2034 } else { 2035 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 && 2036 fb_helper_conn->connector->tile_v_loc != tile_pass - 1) 2037 /* if this tile_pass doesn't cover any of the tiles - keep going */ 2038 continue; 2039 2040 /* find the tile offsets for this pass - need 2041 to find all tiles left and above */ 2042 drm_get_tile_offsets(fb_helper, modes, offsets, 2043 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); 2044 } 2045 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 2046 fb_helper_conn->connector->base.id); 2047 2048 /* got for command line mode first */ 2049 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 2050 if (!modes[i]) { 2051 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", 2052 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); 2053 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 2054 } 2055 /* No preferred modes, pick one off the list */ 2056 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 2057 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 2058 break; 2059 } 2060 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 2061 "none"); 2062 conn_configured |= (1 << i); 2063 } 2064 2065 if ((conn_configured & mask) != mask) { 2066 tile_pass++; 2067 goto retry; 2068 } 2069 return true; 2070 } 2071 2072 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 2073 struct drm_fb_helper_crtc **best_crtcs, 2074 struct drm_display_mode **modes, 2075 int n, int width, int height) 2076 { 2077 int c, o; 2078 struct drm_connector *connector; 2079 const struct drm_connector_helper_funcs *connector_funcs; 2080 struct drm_encoder *encoder; 2081 int my_score, best_score, score; 2082 struct drm_fb_helper_crtc **crtcs, *crtc; 2083 struct drm_fb_helper_connector *fb_helper_conn; 2084 2085 if (n == fb_helper->connector_count) 2086 return 0; 2087 2088 fb_helper_conn = fb_helper->connector_info[n]; 2089 connector = fb_helper_conn->connector; 2090 2091 best_crtcs[n] = NULL; 2092 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 2093 if (modes[n] == NULL) 2094 return best_score; 2095 2096 crtcs = kzalloc(fb_helper->connector_count * 2097 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2098 if (!crtcs) 2099 return best_score; 2100 2101 my_score = 1; 2102 if (connector->status == connector_status_connected) 2103 my_score++; 2104 if (drm_has_cmdline_mode(fb_helper_conn)) 2105 my_score++; 2106 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 2107 my_score++; 2108 2109 connector_funcs = connector->helper_private; 2110 2111 /* 2112 * If the DRM device implements atomic hooks and ->best_encoder() is 2113 * NULL we fallback to the default drm_atomic_helper_best_encoder() 2114 * helper. 2115 */ 2116 if (fb_helper->dev->mode_config.funcs->atomic_commit && 2117 !connector_funcs->best_encoder) 2118 encoder = drm_atomic_helper_best_encoder(connector); 2119 else 2120 encoder = connector_funcs->best_encoder(connector); 2121 2122 if (!encoder) 2123 goto out; 2124 2125 /* select a crtc for this connector and then attempt to configure 2126 remaining connectors */ 2127 for (c = 0; c < fb_helper->crtc_count; c++) { 2128 crtc = &fb_helper->crtc_info[c]; 2129 2130 if ((encoder->possible_crtcs & (1 << c)) == 0) 2131 continue; 2132 2133 for (o = 0; o < n; o++) 2134 if (best_crtcs[o] == crtc) 2135 break; 2136 2137 if (o < n) { 2138 /* ignore cloning unless only a single crtc */ 2139 if (fb_helper->crtc_count > 1) 2140 continue; 2141 2142 if (!drm_mode_equal(modes[o], modes[n])) 2143 continue; 2144 } 2145 2146 crtcs[n] = crtc; 2147 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 2148 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 2149 width, height); 2150 if (score > best_score) { 2151 best_score = score; 2152 memcpy(best_crtcs, crtcs, 2153 fb_helper->connector_count * 2154 sizeof(struct drm_fb_helper_crtc *)); 2155 } 2156 } 2157 out: 2158 kfree(crtcs); 2159 return best_score; 2160 } 2161 2162 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) 2163 { 2164 struct drm_device *dev = fb_helper->dev; 2165 struct drm_fb_helper_crtc **crtcs; 2166 struct drm_display_mode **modes; 2167 struct drm_fb_offset *offsets; 2168 bool *enabled; 2169 int width, height; 2170 int i; 2171 2172 DRM_DEBUG_KMS("\n"); 2173 2174 width = dev->mode_config.max_width; 2175 height = dev->mode_config.max_height; 2176 2177 crtcs = kcalloc(fb_helper->connector_count, 2178 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2179 modes = kcalloc(fb_helper->connector_count, 2180 sizeof(struct drm_display_mode *), GFP_KERNEL); 2181 offsets = kcalloc(fb_helper->connector_count, 2182 sizeof(struct drm_fb_offset), GFP_KERNEL); 2183 enabled = kcalloc(fb_helper->connector_count, 2184 sizeof(bool), GFP_KERNEL); 2185 if (!crtcs || !modes || !enabled || !offsets) { 2186 DRM_ERROR("Memory allocation failed\n"); 2187 goto out; 2188 } 2189 2190 2191 drm_enable_connectors(fb_helper, enabled); 2192 2193 if (!(fb_helper->funcs->initial_config && 2194 fb_helper->funcs->initial_config(fb_helper, crtcs, modes, 2195 offsets, 2196 enabled, width, height))) { 2197 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); 2198 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); 2199 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); 2200 2201 if (!drm_target_cloned(fb_helper, modes, offsets, 2202 enabled, width, height) && 2203 !drm_target_preferred(fb_helper, modes, offsets, 2204 enabled, width, height)) 2205 DRM_ERROR("Unable to find initial modes\n"); 2206 2207 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 2208 width, height); 2209 2210 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 2211 } 2212 2213 /* need to set the modesets up here for use later */ 2214 /* fill out the connector<->crtc mappings into the modesets */ 2215 for (i = 0; i < fb_helper->crtc_count; i++) 2216 drm_fb_helper_modeset_release(fb_helper, 2217 &fb_helper->crtc_info[i].mode_set); 2218 2219 for (i = 0; i < fb_helper->connector_count; i++) { 2220 struct drm_display_mode *mode = modes[i]; 2221 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 2222 struct drm_fb_offset *offset = &offsets[i]; 2223 struct drm_mode_set *modeset = &fb_crtc->mode_set; 2224 2225 if (mode && fb_crtc) { 2226 struct drm_connector *connector = 2227 fb_helper->connector_info[i]->connector; 2228 2229 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", 2230 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); 2231 2232 fb_crtc->desired_mode = mode; 2233 fb_crtc->x = offset->x; 2234 fb_crtc->y = offset->y; 2235 modeset->mode = drm_mode_duplicate(dev, 2236 fb_crtc->desired_mode); 2237 drm_connector_reference(connector); 2238 modeset->connectors[modeset->num_connectors++] = connector; 2239 modeset->fb = fb_helper->fb; 2240 modeset->x = offset->x; 2241 modeset->y = offset->y; 2242 } 2243 } 2244 out: 2245 kfree(crtcs); 2246 kfree(modes); 2247 kfree(offsets); 2248 kfree(enabled); 2249 } 2250 2251 /** 2252 * drm_fb_helper_initial_config - setup a sane initial connector configuration 2253 * @fb_helper: fb_helper device struct 2254 * @bpp_sel: bpp value to use for the framebuffer configuration 2255 * 2256 * Scans the CRTCs and connectors and tries to put together an initial setup. 2257 * At the moment, this is a cloned configuration across all heads with 2258 * a new framebuffer object as the backing store. 2259 * 2260 * Note that this also registers the fbdev and so allows userspace to call into 2261 * the driver through the fbdev interfaces. 2262 * 2263 * This function will call down into the ->fb_probe callback to let 2264 * the driver allocate and initialize the fbdev info structure and the drm 2265 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and 2266 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default 2267 * values for the fbdev info structure. 2268 * 2269 * HANG DEBUGGING: 2270 * 2271 * When you have fbcon support built-in or already loaded, this function will do 2272 * a full modeset to setup the fbdev console. Due to locking misdesign in the 2273 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 2274 * console_lock. Until console_unlock is called no dmesg lines will be sent out 2275 * to consoles, not even serial console. This means when your driver crashes, 2276 * you will see absolutely nothing else but a system stuck in this function, 2277 * with no further output. Any kind of printk() you place within your own driver 2278 * or in the drm core modeset code will also never show up. 2279 * 2280 * Standard debug practice is to run the fbcon setup without taking the 2281 * console_lock as a hack, to be able to see backtraces and crashes on the 2282 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 2283 * cmdline option. 2284 * 2285 * The other option is to just disable fbdev emulation since very likely the 2286 * first modeset from userspace will crash in the same way, and is even easier 2287 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 2288 * kernel cmdline option. 2289 * 2290 * RETURNS: 2291 * Zero if everything went ok, nonzero otherwise. 2292 */ 2293 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 2294 { 2295 struct drm_device *dev = fb_helper->dev; 2296 int count = 0; 2297 2298 if (!drm_fbdev_emulation) 2299 return 0; 2300 2301 mutex_lock(&dev->mode_config.mutex); 2302 count = drm_fb_helper_probe_connector_modes(fb_helper, 2303 dev->mode_config.max_width, 2304 dev->mode_config.max_height); 2305 mutex_unlock(&dev->mode_config.mutex); 2306 /* 2307 * we shouldn't end up with no modes here. 2308 */ 2309 if (count == 0) 2310 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); 2311 2312 drm_setup_crtcs(fb_helper); 2313 2314 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 2315 } 2316 EXPORT_SYMBOL(drm_fb_helper_initial_config); 2317 2318 /** 2319 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 2320 * probing all the outputs attached to the fb 2321 * @fb_helper: the drm_fb_helper 2322 * 2323 * Scan the connectors attached to the fb_helper and try to put together a 2324 * setup after notification of a change in output configuration. 2325 * 2326 * Called at runtime, takes the mode config locks to be able to check/change the 2327 * modeset configuration. Must be run from process context (which usually means 2328 * either the output polling work or a work item launched from the driver's 2329 * hotplug interrupt). 2330 * 2331 * Note that drivers may call this even before calling 2332 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 2333 * for a race-free fbcon setup and will make sure that the fbdev emulation will 2334 * not miss any hotplug events. 2335 * 2336 * RETURNS: 2337 * 0 on success and a non-zero error code otherwise. 2338 */ 2339 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 2340 { 2341 struct drm_device *dev = fb_helper->dev; 2342 u32 max_width, max_height; 2343 2344 if (!drm_fbdev_emulation) 2345 return 0; 2346 2347 mutex_lock(&fb_helper->dev->mode_config.mutex); 2348 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { 2349 fb_helper->delayed_hotplug = true; 2350 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2351 return 0; 2352 } 2353 DRM_DEBUG_KMS("\n"); 2354 2355 max_width = fb_helper->fb->width; 2356 max_height = fb_helper->fb->height; 2357 2358 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); 2359 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2360 2361 drm_modeset_lock_all(dev); 2362 drm_setup_crtcs(fb_helper); 2363 drm_modeset_unlock_all(dev); 2364 drm_fb_helper_set_par(fb_helper->fbdev); 2365 2366 return 0; 2367 } 2368 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2369 2370 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 2371 * but the module doesn't depend on any fb console symbols. At least 2372 * attempt to load fbcon to avoid leaving the system without a usable console. 2373 */ 2374 int __init drm_fb_helper_modinit(void) 2375 { 2376 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 2377 const char *name = "fbcon"; 2378 struct module *fbcon; 2379 2380 mutex_lock(&module_mutex); 2381 fbcon = find_module(name); 2382 mutex_unlock(&module_mutex); 2383 2384 if (!fbcon) 2385 request_module_nowait(name); 2386 #endif 2387 return 0; 2388 } 2389 EXPORT_SYMBOL(drm_fb_helper_modinit); 2390