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