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 fb_info *info; 790 791 #ifdef __DragonFly__ 792 info = kzalloc(sizeof(struct fb_info), GFP_KERNEL); 793 #else 794 info = framebuffer_alloc(0, dev); 795 #endif 796 if (!info) 797 return ERR_PTR(-ENOMEM); 798 799 #if 0 800 ret = fb_alloc_cmap(&info->cmap, 256, 0); 801 if (ret) 802 goto err_release; 803 804 info->apertures = alloc_apertures(1); 805 if (!info->apertures) { 806 ret = -ENOMEM; 807 goto err_free_cmap; 808 } 809 #endif 810 811 fb_helper->fbdev = info; 812 813 return info; 814 815 #if 0 816 err_free_cmap: 817 fb_dealloc_cmap(&info->cmap); 818 err_release: 819 framebuffer_release(info); 820 return ERR_PTR(ret); 821 #endif 822 } 823 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); 824 825 /** 826 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device 827 * @fb_helper: driver-allocated fbdev helper 828 * 829 * A wrapper around unregister_framebuffer, to release the fb_info 830 * framebuffer device 831 */ 832 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 833 { 834 if (fb_helper && fb_helper->fbdev) 835 unregister_framebuffer(fb_helper->fbdev); 836 } 837 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); 838 839 /** 840 * drm_fb_helper_release_fbi - dealloc fb_info and its members 841 * @fb_helper: driver-allocated fbdev helper 842 * 843 * A helper to free memory taken by fb_info and the members cmap and 844 * apertures 845 */ 846 void drm_fb_helper_release_fbi(struct drm_fb_helper *fb_helper) 847 { 848 if (fb_helper) { 849 struct fb_info *info = fb_helper->fbdev; 850 851 if (info) { 852 #ifdef __DragonFly__ 853 kfree(info); 854 #else 855 if (info->cmap.len) 856 fb_dealloc_cmap(&info->cmap); 857 framebuffer_release(info); 858 #endif 859 } 860 861 fb_helper->fbdev = NULL; 862 } 863 } 864 EXPORT_SYMBOL(drm_fb_helper_release_fbi); 865 866 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 867 { 868 if (!drm_fbdev_emulation) 869 return; 870 871 if (!list_empty(&fb_helper->kernel_fb_list)) { 872 list_del(&fb_helper->kernel_fb_list); 873 if (list_empty(&kernel_fb_helper_list)) { 874 #if 0 875 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 876 #endif 877 } 878 } 879 880 drm_fb_helper_crtc_free(fb_helper); 881 882 } 883 EXPORT_SYMBOL(drm_fb_helper_fini); 884 885 #if 0 886 /** 887 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer 888 * @fb_helper: driver-allocated fbdev helper 889 * 890 * A wrapper around unlink_framebuffer implemented by fbdev core 891 */ 892 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) 893 { 894 if (fb_helper && fb_helper->fbdev) 895 unlink_framebuffer(fb_helper->fbdev); 896 } 897 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); 898 899 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, 900 u32 width, u32 height) 901 { 902 struct drm_fb_helper *helper = info->par; 903 struct drm_clip_rect *clip = &helper->dirty_clip; 904 unsigned long flags; 905 906 if (!helper->fb->funcs->dirty) 907 return; 908 909 spin_lock_irqsave(&helper->dirty_lock, flags); 910 clip->x1 = min_t(u32, clip->x1, x); 911 clip->y1 = min_t(u32, clip->y1, y); 912 clip->x2 = max_t(u32, clip->x2, x + width); 913 clip->y2 = max_t(u32, clip->y2, y + height); 914 spin_unlock_irqrestore(&helper->dirty_lock, flags); 915 916 schedule_work(&helper->dirty_work); 917 } 918 919 /** 920 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function 921 * @info: fb_info struct pointer 922 * @pagelist: list of dirty mmap framebuffer pages 923 * 924 * This function is used as the &fb_deferred_io ->deferred_io 925 * callback function for flushing the fbdev mmap writes. 926 */ 927 void drm_fb_helper_deferred_io(struct fb_info *info, 928 struct list_head *pagelist) 929 { 930 unsigned long start, end, min, max; 931 struct page *page; 932 u32 y1, y2; 933 934 min = ULONG_MAX; 935 max = 0; 936 list_for_each_entry(page, pagelist, lru) { 937 start = page->index << PAGE_SHIFT; 938 end = start + PAGE_SIZE - 1; 939 min = min(min, start); 940 max = max(max, end); 941 } 942 943 if (min < max) { 944 y1 = min / info->fix.line_length; 945 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), 946 info->var.yres); 947 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); 948 } 949 } 950 EXPORT_SYMBOL(drm_fb_helper_deferred_io); 951 952 /** 953 * drm_fb_helper_sys_read - wrapper around fb_sys_read 954 * @info: fb_info struct pointer 955 * @buf: userspace buffer to read from framebuffer memory 956 * @count: number of bytes to read from framebuffer memory 957 * @ppos: read offset within framebuffer memory 958 * 959 * A wrapper around fb_sys_read implemented by fbdev core 960 */ 961 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 962 size_t count, loff_t *ppos) 963 { 964 return fb_sys_read(info, buf, count, ppos); 965 } 966 EXPORT_SYMBOL(drm_fb_helper_sys_read); 967 968 /** 969 * drm_fb_helper_sys_write - wrapper around fb_sys_write 970 * @info: fb_info struct pointer 971 * @buf: userspace buffer to write to framebuffer memory 972 * @count: number of bytes to write to framebuffer memory 973 * @ppos: write offset within framebuffer memory 974 * 975 * A wrapper around fb_sys_write implemented by fbdev core 976 */ 977 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 978 size_t count, loff_t *ppos) 979 { 980 ssize_t ret; 981 982 ret = fb_sys_write(info, buf, count, ppos); 983 if (ret > 0) 984 drm_fb_helper_dirty(info, 0, 0, info->var.xres, 985 info->var.yres); 986 987 return ret; 988 } 989 EXPORT_SYMBOL(drm_fb_helper_sys_write); 990 991 /** 992 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect 993 * @info: fbdev registered by the helper 994 * @rect: info about rectangle to fill 995 * 996 * A wrapper around sys_fillrect implemented by fbdev core 997 */ 998 void drm_fb_helper_sys_fillrect(struct fb_info *info, 999 const struct fb_fillrect *rect) 1000 { 1001 sys_fillrect(info, rect); 1002 drm_fb_helper_dirty(info, rect->dx, rect->dy, 1003 rect->width, rect->height); 1004 } 1005 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); 1006 1007 /** 1008 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea 1009 * @info: fbdev registered by the helper 1010 * @area: info about area to copy 1011 * 1012 * A wrapper around sys_copyarea implemented by fbdev core 1013 */ 1014 void drm_fb_helper_sys_copyarea(struct fb_info *info, 1015 const struct fb_copyarea *area) 1016 { 1017 sys_copyarea(info, area); 1018 drm_fb_helper_dirty(info, area->dx, area->dy, 1019 area->width, area->height); 1020 } 1021 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); 1022 1023 /** 1024 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit 1025 * @info: fbdev registered by the helper 1026 * @image: info about image to blit 1027 * 1028 * A wrapper around sys_imageblit implemented by fbdev core 1029 */ 1030 void drm_fb_helper_sys_imageblit(struct fb_info *info, 1031 const struct fb_image *image) 1032 { 1033 sys_imageblit(info, image); 1034 drm_fb_helper_dirty(info, image->dx, image->dy, 1035 image->width, image->height); 1036 } 1037 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); 1038 1039 /** 1040 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect 1041 * @info: fbdev registered by the helper 1042 * @rect: info about rectangle to fill 1043 * 1044 * A wrapper around cfb_imageblit implemented by fbdev core 1045 */ 1046 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 1047 const struct fb_fillrect *rect) 1048 { 1049 cfb_fillrect(info, rect); 1050 drm_fb_helper_dirty(info, rect->dx, rect->dy, 1051 rect->width, rect->height); 1052 } 1053 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); 1054 1055 /** 1056 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea 1057 * @info: fbdev registered by the helper 1058 * @area: info about area to copy 1059 * 1060 * A wrapper around cfb_copyarea implemented by fbdev core 1061 */ 1062 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 1063 const struct fb_copyarea *area) 1064 { 1065 cfb_copyarea(info, area); 1066 drm_fb_helper_dirty(info, area->dx, area->dy, 1067 area->width, area->height); 1068 } 1069 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); 1070 1071 /** 1072 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit 1073 * @info: fbdev registered by the helper 1074 * @image: info about image to blit 1075 * 1076 * A wrapper around cfb_imageblit implemented by fbdev core 1077 */ 1078 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 1079 const struct fb_image *image) 1080 { 1081 cfb_imageblit(info, image); 1082 drm_fb_helper_dirty(info, image->dx, image->dy, 1083 image->width, image->height); 1084 } 1085 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); 1086 1087 /** 1088 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 1089 * @fb_helper: driver-allocated fbdev helper 1090 * @state: desired state, zero to resume, non-zero to suspend 1091 * 1092 * A wrapper around fb_set_suspend implemented by fbdev core. 1093 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take 1094 * the lock yourself 1095 */ 1096 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, int state) 1097 { 1098 #if 0 1099 if (fb_helper && fb_helper->fbdev) 1100 fb_set_suspend(fb_helper->fbdev, state); 1101 #endif 1102 } 1103 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 1104 1105 /** 1106 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also 1107 * takes the console lock 1108 * @fb_helper: driver-allocated fbdev helper 1109 * @suspend: whether to suspend or resume 1110 * 1111 * A wrapper around fb_set_suspend() that takes the console lock. If the lock 1112 * isn't available on resume, a worker is tasked with waiting for the lock 1113 * to become available. The console lock can be pretty contented on resume 1114 * due to all the printk activity. 1115 * 1116 * This function can be called multiple times with the same state since 1117 * &fb_info->state is checked to see if fbdev is running or not before locking. 1118 * 1119 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. 1120 */ 1121 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 1122 bool suspend) 1123 { 1124 if (!fb_helper || !fb_helper->fbdev) 1125 return; 1126 1127 /* make sure there's no pending/ongoing resume */ 1128 flush_work(&fb_helper->resume_work); 1129 1130 if (suspend) { 1131 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) 1132 return; 1133 1134 #if 0 1135 console_lock(); 1136 #endif 1137 1138 } else { 1139 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) 1140 return; 1141 1142 if (!console_trylock()) { 1143 schedule_work(&fb_helper->resume_work); 1144 return; 1145 } 1146 } 1147 1148 #if 0 1149 fb_set_suspend(fb_helper->fbdev, suspend); 1150 console_unlock(); 1151 #endif 1152 } 1153 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); 1154 1155 static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, 1156 u16 blue, u16 regno, struct fb_info *info) 1157 { 1158 struct drm_fb_helper *fb_helper = info->par; 1159 struct drm_framebuffer *fb = fb_helper->fb; 1160 1161 if (info->fix.visual == FB_VISUAL_TRUECOLOR) { 1162 u32 *palette; 1163 u32 value; 1164 /* place color in psuedopalette */ 1165 if (regno > 16) 1166 return -EINVAL; 1167 palette = (u32 *)info->pseudo_palette; 1168 red >>= (16 - info->var.red.length); 1169 green >>= (16 - info->var.green.length); 1170 blue >>= (16 - info->var.blue.length); 1171 value = (red << info->var.red.offset) | 1172 (green << info->var.green.offset) | 1173 (blue << info->var.blue.offset); 1174 if (info->var.transp.length > 0) { 1175 u32 mask = (1 << info->var.transp.length) - 1; 1176 mask <<= info->var.transp.offset; 1177 value |= mask; 1178 } 1179 palette[regno] = value; 1180 return 0; 1181 } 1182 1183 /* 1184 * The driver really shouldn't advertise pseudo/directcolor 1185 * visuals if it can't deal with the palette. 1186 */ 1187 if (WARN_ON(!fb_helper->funcs->gamma_set || 1188 !fb_helper->funcs->gamma_get)) 1189 return -EINVAL; 1190 1191 WARN_ON(fb->bits_per_pixel != 8); 1192 1193 fb_helper->funcs->gamma_set(crtc, red, green, blue, regno); 1194 1195 return 0; 1196 } 1197 1198 /** 1199 * drm_fb_helper_setcmap - implementation for ->fb_setcmap 1200 * @cmap: cmap to set 1201 * @info: fbdev registered by the helper 1202 */ 1203 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 1204 { 1205 struct drm_fb_helper *fb_helper = info->par; 1206 struct drm_device *dev = fb_helper->dev; 1207 const struct drm_crtc_helper_funcs *crtc_funcs; 1208 u16 *red, *green, *blue, *transp; 1209 struct drm_crtc *crtc; 1210 int i, j, rc = 0; 1211 int start; 1212 1213 if (oops_in_progress) 1214 return -EBUSY; 1215 1216 drm_modeset_lock_all(dev); 1217 if (!drm_fb_helper_is_bound(fb_helper)) { 1218 drm_modeset_unlock_all(dev); 1219 return -EBUSY; 1220 } 1221 1222 for (i = 0; i < fb_helper->crtc_count; i++) { 1223 crtc = fb_helper->crtc_info[i].mode_set.crtc; 1224 crtc_funcs = crtc->helper_private; 1225 1226 red = cmap->red; 1227 green = cmap->green; 1228 blue = cmap->blue; 1229 transp = cmap->transp; 1230 start = cmap->start; 1231 1232 for (j = 0; j < cmap->len; j++) { 1233 u16 hred, hgreen, hblue, htransp = 0xffff; 1234 1235 hred = *red++; 1236 hgreen = *green++; 1237 hblue = *blue++; 1238 1239 if (transp) 1240 htransp = *transp++; 1241 1242 rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); 1243 if (rc) 1244 goto out; 1245 } 1246 if (crtc_funcs->load_lut) 1247 crtc_funcs->load_lut(crtc); 1248 } 1249 out: 1250 drm_modeset_unlock_all(dev); 1251 return rc; 1252 } 1253 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1254 1255 /** 1256 * drm_fb_helper_check_var - implementation for ->fb_check_var 1257 * @var: screeninfo to check 1258 * @info: fbdev registered by the helper 1259 */ 1260 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1261 struct fb_info *info) 1262 { 1263 struct drm_fb_helper *fb_helper = info->par; 1264 struct drm_framebuffer *fb = fb_helper->fb; 1265 int depth; 1266 1267 if (var->pixclock != 0 || in_dbg_master()) 1268 return -EINVAL; 1269 1270 /* Need to resize the fb object !!! */ 1271 if (var->bits_per_pixel > fb->bits_per_pixel || 1272 var->xres > fb->width || var->yres > fb->height || 1273 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1274 DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " 1275 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1276 var->xres, var->yres, var->bits_per_pixel, 1277 var->xres_virtual, var->yres_virtual, 1278 fb->width, fb->height, fb->bits_per_pixel); 1279 return -EINVAL; 1280 } 1281 1282 switch (var->bits_per_pixel) { 1283 case 16: 1284 depth = (var->green.length == 6) ? 16 : 15; 1285 break; 1286 case 32: 1287 depth = (var->transp.length > 0) ? 32 : 24; 1288 break; 1289 default: 1290 depth = var->bits_per_pixel; 1291 break; 1292 } 1293 1294 switch (depth) { 1295 case 8: 1296 var->red.offset = 0; 1297 var->green.offset = 0; 1298 var->blue.offset = 0; 1299 var->red.length = 8; 1300 var->green.length = 8; 1301 var->blue.length = 8; 1302 var->transp.length = 0; 1303 var->transp.offset = 0; 1304 break; 1305 case 15: 1306 var->red.offset = 10; 1307 var->green.offset = 5; 1308 var->blue.offset = 0; 1309 var->red.length = 5; 1310 var->green.length = 5; 1311 var->blue.length = 5; 1312 var->transp.length = 1; 1313 var->transp.offset = 15; 1314 break; 1315 case 16: 1316 var->red.offset = 11; 1317 var->green.offset = 5; 1318 var->blue.offset = 0; 1319 var->red.length = 5; 1320 var->green.length = 6; 1321 var->blue.length = 5; 1322 var->transp.length = 0; 1323 var->transp.offset = 0; 1324 break; 1325 case 24: 1326 var->red.offset = 16; 1327 var->green.offset = 8; 1328 var->blue.offset = 0; 1329 var->red.length = 8; 1330 var->green.length = 8; 1331 var->blue.length = 8; 1332 var->transp.length = 0; 1333 var->transp.offset = 0; 1334 break; 1335 case 32: 1336 var->red.offset = 16; 1337 var->green.offset = 8; 1338 var->blue.offset = 0; 1339 var->red.length = 8; 1340 var->green.length = 8; 1341 var->blue.length = 8; 1342 var->transp.length = 8; 1343 var->transp.offset = 24; 1344 break; 1345 default: 1346 return -EINVAL; 1347 } 1348 return 0; 1349 } 1350 EXPORT_SYMBOL(drm_fb_helper_check_var); 1351 #endif 1352 1353 /** 1354 * drm_fb_helper_set_par - implementation for ->fb_set_par 1355 * @info: fbdev registered by the helper 1356 * 1357 * This will let fbcon do the mode init and is called at initialization time by 1358 * the fbdev core when registering the driver, and later on through the hotplug 1359 * callback. 1360 */ 1361 int drm_fb_helper_set_par(struct fb_info *info) 1362 { 1363 struct drm_fb_helper *fb_helper = info->par; 1364 #if 0 1365 struct fb_var_screeninfo *var = &info->var; 1366 #endif 1367 1368 #ifdef __DragonFly__ 1369 if (panicstr) 1370 return -EBUSY; 1371 #else 1372 if (oops_in_progress) 1373 return -EBUSY; 1374 #endif 1375 1376 #if 0 1377 if (var->pixclock != 0) { 1378 DRM_ERROR("PIXEL CLOCK SET\n"); 1379 return -EINVAL; 1380 } 1381 #endif 1382 1383 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 1384 1385 return 0; 1386 } 1387 EXPORT_SYMBOL(drm_fb_helper_set_par); 1388 1389 #if 0 1390 static int pan_display_atomic(struct fb_var_screeninfo *var, 1391 struct fb_info *info) 1392 { 1393 struct drm_fb_helper *fb_helper = info->par; 1394 struct drm_device *dev = fb_helper->dev; 1395 struct drm_atomic_state *state; 1396 struct drm_plane *plane; 1397 int i, ret; 1398 unsigned plane_mask; 1399 1400 state = drm_atomic_state_alloc(dev); 1401 if (!state) 1402 return -ENOMEM; 1403 1404 state->acquire_ctx = dev->mode_config.acquire_ctx; 1405 retry: 1406 plane_mask = 0; 1407 for(i = 0; i < fb_helper->crtc_count; i++) { 1408 struct drm_mode_set *mode_set; 1409 1410 mode_set = &fb_helper->crtc_info[i].mode_set; 1411 1412 mode_set->x = var->xoffset; 1413 mode_set->y = var->yoffset; 1414 1415 ret = __drm_atomic_helper_set_config(mode_set, state); 1416 if (ret != 0) 1417 goto fail; 1418 1419 plane = mode_set->crtc->primary; 1420 plane_mask |= (1 << drm_plane_index(plane)); 1421 plane->old_fb = plane->fb; 1422 } 1423 1424 ret = drm_atomic_commit(state); 1425 if (ret != 0) 1426 goto fail; 1427 1428 info->var.xoffset = var->xoffset; 1429 info->var.yoffset = var->yoffset; 1430 1431 1432 fail: 1433 drm_atomic_clean_old_fb(dev, plane_mask, ret); 1434 1435 if (ret == -EDEADLK) 1436 goto backoff; 1437 1438 if (ret != 0) 1439 drm_atomic_state_free(state); 1440 1441 return ret; 1442 1443 backoff: 1444 drm_atomic_state_clear(state); 1445 drm_atomic_legacy_backoff(state); 1446 1447 goto retry; 1448 } 1449 1450 /** 1451 * drm_fb_helper_pan_display - implementation for ->fb_pan_display 1452 * @var: updated screen information 1453 * @info: fbdev registered by the helper 1454 */ 1455 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1456 struct fb_info *info) 1457 { 1458 struct drm_fb_helper *fb_helper = info->par; 1459 struct drm_device *dev = fb_helper->dev; 1460 struct drm_mode_set *modeset; 1461 int ret = 0; 1462 int i; 1463 1464 if (oops_in_progress) 1465 return -EBUSY; 1466 1467 drm_modeset_lock_all(dev); 1468 if (!drm_fb_helper_is_bound(fb_helper)) { 1469 drm_modeset_unlock_all(dev); 1470 return -EBUSY; 1471 } 1472 1473 if (dev->mode_config.funcs->atomic_commit) { 1474 ret = pan_display_atomic(var, info); 1475 goto unlock; 1476 } 1477 1478 for (i = 0; i < fb_helper->crtc_count; i++) { 1479 modeset = &fb_helper->crtc_info[i].mode_set; 1480 1481 modeset->x = var->xoffset; 1482 modeset->y = var->yoffset; 1483 1484 if (modeset->num_connectors) { 1485 ret = drm_mode_set_config_internal(modeset); 1486 if (!ret) { 1487 info->var.xoffset = var->xoffset; 1488 info->var.yoffset = var->yoffset; 1489 } 1490 } 1491 } 1492 unlock: 1493 drm_modeset_unlock_all(dev); 1494 return ret; 1495 } 1496 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1497 #endif 1498 1499 /* 1500 * Allocates the backing storage and sets up the fbdev info structure through 1501 * the ->fb_probe callback and then registers the fbdev and sets up the panic 1502 * notifier. 1503 */ 1504 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1505 int preferred_bpp) 1506 { 1507 int ret = 0; 1508 int crtc_count = 0; 1509 int i; 1510 struct fb_info *info; 1511 struct drm_fb_helper_surface_size sizes; 1512 int gamma_size = 0; 1513 #ifdef __DragonFly__ 1514 int kms_console = 1; 1515 #endif 1516 1517 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1518 sizes.surface_depth = 24; 1519 sizes.surface_bpp = 32; 1520 sizes.fb_width = (unsigned)-1; 1521 sizes.fb_height = (unsigned)-1; 1522 1523 /* if driver picks 8 or 16 by default use that 1524 for both depth/bpp */ 1525 if (preferred_bpp != sizes.surface_bpp) 1526 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1527 1528 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1529 for (i = 0; i < fb_helper->connector_count; i++) { 1530 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 1531 struct drm_cmdline_mode *cmdline_mode; 1532 1533 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1534 1535 if (cmdline_mode->bpp_specified) { 1536 switch (cmdline_mode->bpp) { 1537 case 8: 1538 sizes.surface_depth = sizes.surface_bpp = 8; 1539 break; 1540 case 15: 1541 sizes.surface_depth = 15; 1542 sizes.surface_bpp = 16; 1543 break; 1544 case 16: 1545 sizes.surface_depth = sizes.surface_bpp = 16; 1546 break; 1547 case 24: 1548 sizes.surface_depth = sizes.surface_bpp = 24; 1549 break; 1550 case 32: 1551 sizes.surface_depth = 24; 1552 sizes.surface_bpp = 32; 1553 break; 1554 } 1555 break; 1556 } 1557 } 1558 1559 crtc_count = 0; 1560 for (i = 0; i < fb_helper->crtc_count; i++) { 1561 struct drm_display_mode *desired_mode; 1562 struct drm_mode_set *mode_set; 1563 int x, y, j; 1564 /* in case of tile group, are we the last tile vert or horiz? 1565 * If no tile group you are always the last one both vertically 1566 * and horizontally 1567 */ 1568 bool lastv = true, lasth = true; 1569 1570 desired_mode = fb_helper->crtc_info[i].desired_mode; 1571 mode_set = &fb_helper->crtc_info[i].mode_set; 1572 1573 if (!desired_mode) 1574 continue; 1575 1576 crtc_count++; 1577 1578 x = fb_helper->crtc_info[i].x; 1579 y = fb_helper->crtc_info[i].y; 1580 1581 if (gamma_size == 0) 1582 gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; 1583 1584 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); 1585 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); 1586 1587 for (j = 0; j < mode_set->num_connectors; j++) { 1588 struct drm_connector *connector = mode_set->connectors[j]; 1589 if (connector->has_tile) { 1590 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1591 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1592 /* cloning to multiple tiles is just crazy-talk, so: */ 1593 break; 1594 } 1595 } 1596 1597 if (lasth) 1598 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); 1599 if (lastv) 1600 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); 1601 } 1602 1603 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1604 /* hmm everyone went away - assume VGA cable just fell out 1605 and will come back later. */ 1606 DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n"); 1607 sizes.fb_width = sizes.surface_width = 1024; 1608 sizes.fb_height = sizes.surface_height = 768; 1609 } 1610 1611 /* push down into drivers */ 1612 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1613 if (ret < 0) 1614 return ret; 1615 1616 info = fb_helper->fbdev; 1617 1618 /* 1619 * Set the fb pointer - usually drm_setup_crtcs does this for hotplug 1620 * events, but at init time drm_setup_crtcs needs to be called before 1621 * the fb is allocated (since we need to figure out the desired size of 1622 * the fb before we can allocate it ...). Hence we need to fix things up 1623 * here again. 1624 */ 1625 for (i = 0; i < fb_helper->crtc_count; i++) 1626 if (fb_helper->crtc_info[i].mode_set.num_connectors) 1627 fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; 1628 1629 #ifdef __DragonFly__ 1630 TUNABLE_INT_FETCH("kern.kms_console", &kms_console); 1631 if (kms_console) { 1632 if (register_framebuffer(info) < 0) 1633 return -EINVAL; 1634 1635 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1636 } 1637 #else 1638 info->var.pixclock = 0; 1639 if (register_framebuffer(info) < 0) 1640 return -EINVAL; 1641 1642 dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n", 1643 info->node, info->fix.id); 1644 1645 if (list_empty(&kernel_fb_helper_list)) { 1646 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 1647 } 1648 1649 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 1650 #endif 1651 1652 return 0; 1653 } 1654 1655 #if 0 1656 /** 1657 * drm_fb_helper_fill_fix - initializes fixed fbdev information 1658 * @info: fbdev registered by the helper 1659 * @pitch: desired pitch 1660 * @depth: desired depth 1661 * 1662 * Helper to fill in the fixed fbdev information useful for a non-accelerated 1663 * fbdev emulations. Drivers which support acceleration methods which impose 1664 * additional constraints need to set up their own limits. 1665 * 1666 * Drivers should call this (or their equivalent setup code) from their 1667 * ->fb_probe callback. 1668 */ 1669 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1670 uint32_t depth) 1671 { 1672 info->fix.type = FB_TYPE_PACKED_PIXELS; 1673 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1674 FB_VISUAL_TRUECOLOR; 1675 info->fix.mmio_start = 0; 1676 info->fix.mmio_len = 0; 1677 info->fix.type_aux = 0; 1678 info->fix.xpanstep = 1; /* doing it in hw */ 1679 info->fix.ypanstep = 1; /* doing it in hw */ 1680 info->fix.ywrapstep = 0; 1681 info->fix.accel = FB_ACCEL_NONE; 1682 1683 info->fix.line_length = pitch; 1684 return; 1685 } 1686 EXPORT_SYMBOL(drm_fb_helper_fill_fix); 1687 1688 /** 1689 * drm_fb_helper_fill_var - initalizes variable fbdev information 1690 * @info: fbdev instance to set up 1691 * @fb_helper: fb helper instance to use as template 1692 * @fb_width: desired fb width 1693 * @fb_height: desired fb height 1694 * 1695 * Sets up the variable fbdev metainformation from the given fb helper instance 1696 * and the drm framebuffer allocated in fb_helper->fb. 1697 * 1698 * Drivers should call this (or their equivalent setup code) from their 1699 * ->fb_probe callback after having allocated the fbdev backing 1700 * storage framebuffer. 1701 */ 1702 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 1703 uint32_t fb_width, uint32_t fb_height) 1704 { 1705 struct drm_framebuffer *fb = fb_helper->fb; 1706 info->pseudo_palette = fb_helper->pseudo_palette; 1707 info->var.xres_virtual = fb->width; 1708 info->var.yres_virtual = fb->height; 1709 info->var.bits_per_pixel = fb->bits_per_pixel; 1710 info->var.accel_flags = FB_ACCELF_TEXT; 1711 info->var.xoffset = 0; 1712 info->var.yoffset = 0; 1713 info->var.activate = FB_ACTIVATE_NOW; 1714 info->var.height = -1; 1715 info->var.width = -1; 1716 1717 switch (fb->depth) { 1718 case 8: 1719 info->var.red.offset = 0; 1720 info->var.green.offset = 0; 1721 info->var.blue.offset = 0; 1722 info->var.red.length = 8; /* 8bit DAC */ 1723 info->var.green.length = 8; 1724 info->var.blue.length = 8; 1725 info->var.transp.offset = 0; 1726 info->var.transp.length = 0; 1727 break; 1728 case 15: 1729 info->var.red.offset = 10; 1730 info->var.green.offset = 5; 1731 info->var.blue.offset = 0; 1732 info->var.red.length = 5; 1733 info->var.green.length = 5; 1734 info->var.blue.length = 5; 1735 info->var.transp.offset = 15; 1736 info->var.transp.length = 1; 1737 break; 1738 case 16: 1739 info->var.red.offset = 11; 1740 info->var.green.offset = 5; 1741 info->var.blue.offset = 0; 1742 info->var.red.length = 5; 1743 info->var.green.length = 6; 1744 info->var.blue.length = 5; 1745 info->var.transp.offset = 0; 1746 break; 1747 case 24: 1748 info->var.red.offset = 16; 1749 info->var.green.offset = 8; 1750 info->var.blue.offset = 0; 1751 info->var.red.length = 8; 1752 info->var.green.length = 8; 1753 info->var.blue.length = 8; 1754 info->var.transp.offset = 0; 1755 info->var.transp.length = 0; 1756 break; 1757 case 32: 1758 info->var.red.offset = 16; 1759 info->var.green.offset = 8; 1760 info->var.blue.offset = 0; 1761 info->var.red.length = 8; 1762 info->var.green.length = 8; 1763 info->var.blue.length = 8; 1764 info->var.transp.offset = 24; 1765 info->var.transp.length = 8; 1766 break; 1767 default: 1768 break; 1769 } 1770 1771 info->var.xres = fb_width; 1772 info->var.yres = fb_height; 1773 } 1774 EXPORT_SYMBOL(drm_fb_helper_fill_var); 1775 #endif 1776 1777 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1778 uint32_t maxX, 1779 uint32_t maxY) 1780 { 1781 struct drm_connector *connector; 1782 int count = 0; 1783 int i; 1784 1785 for (i = 0; i < fb_helper->connector_count; i++) { 1786 connector = fb_helper->connector_info[i]->connector; 1787 count += connector->funcs->fill_modes(connector, maxX, maxY); 1788 } 1789 1790 return count; 1791 } 1792 1793 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1794 { 1795 struct drm_display_mode *mode; 1796 1797 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1798 if (mode->hdisplay > width || 1799 mode->vdisplay > height) 1800 continue; 1801 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1802 return mode; 1803 } 1804 return NULL; 1805 } 1806 EXPORT_SYMBOL(drm_has_preferred_mode); 1807 1808 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1809 { 1810 return fb_connector->connector->cmdline_mode.specified; 1811 } 1812 1813 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, 1814 int width, int height) 1815 { 1816 struct drm_cmdline_mode *cmdline_mode; 1817 struct drm_display_mode *mode; 1818 bool prefer_non_interlace; 1819 1820 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1821 if (cmdline_mode->specified == false) 1822 return NULL; 1823 1824 /* attempt to find a matching mode in the list of modes 1825 * we have gotten so far, if not add a CVT mode that conforms 1826 */ 1827 if (cmdline_mode->rb || cmdline_mode->margins) 1828 goto create_mode; 1829 1830 prefer_non_interlace = !cmdline_mode->interlace; 1831 again: 1832 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1833 /* check width/height */ 1834 if (mode->hdisplay != cmdline_mode->xres || 1835 mode->vdisplay != cmdline_mode->yres) 1836 continue; 1837 1838 if (cmdline_mode->refresh_specified) { 1839 if (mode->vrefresh != cmdline_mode->refresh) 1840 continue; 1841 } 1842 1843 if (cmdline_mode->interlace) { 1844 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1845 continue; 1846 } else if (prefer_non_interlace) { 1847 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1848 continue; 1849 } 1850 return mode; 1851 } 1852 1853 if (prefer_non_interlace) { 1854 prefer_non_interlace = false; 1855 goto again; 1856 } 1857 1858 create_mode: 1859 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 1860 cmdline_mode); 1861 list_add(&mode->head, &fb_helper_conn->connector->modes); 1862 return mode; 1863 } 1864 EXPORT_SYMBOL(drm_pick_cmdline_mode); 1865 1866 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1867 { 1868 bool enable; 1869 1870 if (strict) 1871 enable = connector->status == connector_status_connected; 1872 else 1873 enable = connector->status != connector_status_disconnected; 1874 1875 return enable; 1876 } 1877 1878 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1879 bool *enabled) 1880 { 1881 bool any_enabled = false; 1882 struct drm_connector *connector; 1883 int i = 0; 1884 1885 for (i = 0; i < fb_helper->connector_count; i++) { 1886 connector = fb_helper->connector_info[i]->connector; 1887 enabled[i] = drm_connector_enabled(connector, true); 1888 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 1889 enabled[i] ? "yes" : "no"); 1890 any_enabled |= enabled[i]; 1891 } 1892 1893 if (any_enabled) 1894 return; 1895 1896 for (i = 0; i < fb_helper->connector_count; i++) { 1897 connector = fb_helper->connector_info[i]->connector; 1898 enabled[i] = drm_connector_enabled(connector, false); 1899 } 1900 } 1901 1902 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 1903 struct drm_display_mode **modes, 1904 struct drm_fb_offset *offsets, 1905 bool *enabled, int width, int height) 1906 { 1907 int count, i, j; 1908 bool can_clone = false; 1909 struct drm_fb_helper_connector *fb_helper_conn; 1910 struct drm_display_mode *dmt_mode, *mode; 1911 1912 /* only contemplate cloning in the single crtc case */ 1913 if (fb_helper->crtc_count > 1) 1914 return false; 1915 1916 count = 0; 1917 for (i = 0; i < fb_helper->connector_count; i++) { 1918 if (enabled[i]) 1919 count++; 1920 } 1921 1922 /* only contemplate cloning if more than one connector is enabled */ 1923 if (count <= 1) 1924 return false; 1925 1926 /* check the command line or if nothing common pick 1024x768 */ 1927 can_clone = true; 1928 for (i = 0; i < fb_helper->connector_count; i++) { 1929 if (!enabled[i]) 1930 continue; 1931 fb_helper_conn = fb_helper->connector_info[i]; 1932 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 1933 if (!modes[i]) { 1934 can_clone = false; 1935 break; 1936 } 1937 for (j = 0; j < i; j++) { 1938 if (!enabled[j]) 1939 continue; 1940 if (!drm_mode_equal(modes[j], modes[i])) 1941 can_clone = false; 1942 } 1943 } 1944 1945 if (can_clone) { 1946 DRM_DEBUG_KMS("can clone using command line\n"); 1947 return true; 1948 } 1949 1950 /* try and find a 1024x768 mode on each connector */ 1951 can_clone = true; 1952 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 1953 1954 for (i = 0; i < fb_helper->connector_count; i++) { 1955 1956 if (!enabled[i]) 1957 continue; 1958 1959 fb_helper_conn = fb_helper->connector_info[i]; 1960 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1961 if (drm_mode_equal(mode, dmt_mode)) 1962 modes[i] = mode; 1963 } 1964 if (!modes[i]) 1965 can_clone = false; 1966 } 1967 1968 if (can_clone) { 1969 DRM_DEBUG_KMS("can clone using 1024x768\n"); 1970 return true; 1971 } 1972 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 1973 return false; 1974 } 1975 1976 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, 1977 struct drm_display_mode **modes, 1978 struct drm_fb_offset *offsets, 1979 int idx, 1980 int h_idx, int v_idx) 1981 { 1982 struct drm_fb_helper_connector *fb_helper_conn; 1983 int i; 1984 int hoffset = 0, voffset = 0; 1985 1986 for (i = 0; i < fb_helper->connector_count; i++) { 1987 fb_helper_conn = fb_helper->connector_info[i]; 1988 if (!fb_helper_conn->connector->has_tile) 1989 continue; 1990 1991 if (!modes[i] && (h_idx || v_idx)) { 1992 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, 1993 fb_helper_conn->connector->base.id); 1994 continue; 1995 } 1996 if (fb_helper_conn->connector->tile_h_loc < h_idx) 1997 hoffset += modes[i]->hdisplay; 1998 1999 if (fb_helper_conn->connector->tile_v_loc < v_idx) 2000 voffset += modes[i]->vdisplay; 2001 } 2002 offsets[idx].x = hoffset; 2003 offsets[idx].y = voffset; 2004 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); 2005 return 0; 2006 } 2007 2008 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 2009 struct drm_display_mode **modes, 2010 struct drm_fb_offset *offsets, 2011 bool *enabled, int width, int height) 2012 { 2013 struct drm_fb_helper_connector *fb_helper_conn; 2014 int i; 2015 uint64_t conn_configured = 0, mask; 2016 int tile_pass = 0; 2017 mask = (1 << fb_helper->connector_count) - 1; 2018 retry: 2019 for (i = 0; i < fb_helper->connector_count; i++) { 2020 fb_helper_conn = fb_helper->connector_info[i]; 2021 2022 if (conn_configured & (1 << i)) 2023 continue; 2024 2025 if (enabled[i] == false) { 2026 conn_configured |= (1 << i); 2027 continue; 2028 } 2029 2030 /* first pass over all the untiled connectors */ 2031 if (tile_pass == 0 && fb_helper_conn->connector->has_tile) 2032 continue; 2033 2034 if (tile_pass == 1) { 2035 if (fb_helper_conn->connector->tile_h_loc != 0 || 2036 fb_helper_conn->connector->tile_v_loc != 0) 2037 continue; 2038 2039 } else { 2040 if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 && 2041 fb_helper_conn->connector->tile_v_loc != tile_pass - 1) 2042 /* if this tile_pass doesn't cover any of the tiles - keep going */ 2043 continue; 2044 2045 /* find the tile offsets for this pass - need 2046 to find all tiles left and above */ 2047 drm_get_tile_offsets(fb_helper, modes, offsets, 2048 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); 2049 } 2050 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 2051 fb_helper_conn->connector->base.id); 2052 2053 /* got for command line mode first */ 2054 modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); 2055 if (!modes[i]) { 2056 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", 2057 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); 2058 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 2059 } 2060 /* No preferred modes, pick one off the list */ 2061 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 2062 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 2063 break; 2064 } 2065 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 2066 "none"); 2067 conn_configured |= (1 << i); 2068 } 2069 2070 if ((conn_configured & mask) != mask) { 2071 tile_pass++; 2072 goto retry; 2073 } 2074 return true; 2075 } 2076 2077 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 2078 struct drm_fb_helper_crtc **best_crtcs, 2079 struct drm_display_mode **modes, 2080 int n, int width, int height) 2081 { 2082 int c, o; 2083 struct drm_connector *connector; 2084 const struct drm_connector_helper_funcs *connector_funcs; 2085 struct drm_encoder *encoder; 2086 int my_score, best_score, score; 2087 struct drm_fb_helper_crtc **crtcs, *crtc; 2088 struct drm_fb_helper_connector *fb_helper_conn; 2089 2090 if (n == fb_helper->connector_count) 2091 return 0; 2092 2093 fb_helper_conn = fb_helper->connector_info[n]; 2094 connector = fb_helper_conn->connector; 2095 2096 best_crtcs[n] = NULL; 2097 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 2098 if (modes[n] == NULL) 2099 return best_score; 2100 2101 crtcs = kzalloc(fb_helper->connector_count * 2102 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2103 if (!crtcs) 2104 return best_score; 2105 2106 my_score = 1; 2107 if (connector->status == connector_status_connected) 2108 my_score++; 2109 if (drm_has_cmdline_mode(fb_helper_conn)) 2110 my_score++; 2111 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 2112 my_score++; 2113 2114 connector_funcs = connector->helper_private; 2115 2116 /* 2117 * If the DRM device implements atomic hooks and ->best_encoder() is 2118 * NULL we fallback to the default drm_atomic_helper_best_encoder() 2119 * helper. 2120 */ 2121 if (fb_helper->dev->mode_config.funcs->atomic_commit && 2122 !connector_funcs->best_encoder) 2123 encoder = drm_atomic_helper_best_encoder(connector); 2124 else 2125 encoder = connector_funcs->best_encoder(connector); 2126 2127 if (!encoder) 2128 goto out; 2129 2130 /* select a crtc for this connector and then attempt to configure 2131 remaining connectors */ 2132 for (c = 0; c < fb_helper->crtc_count; c++) { 2133 crtc = &fb_helper->crtc_info[c]; 2134 2135 if ((encoder->possible_crtcs & (1 << c)) == 0) 2136 continue; 2137 2138 for (o = 0; o < n; o++) 2139 if (best_crtcs[o] == crtc) 2140 break; 2141 2142 if (o < n) { 2143 /* ignore cloning unless only a single crtc */ 2144 if (fb_helper->crtc_count > 1) 2145 continue; 2146 2147 if (!drm_mode_equal(modes[o], modes[n])) 2148 continue; 2149 } 2150 2151 crtcs[n] = crtc; 2152 memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); 2153 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 2154 width, height); 2155 if (score > best_score) { 2156 best_score = score; 2157 memcpy(best_crtcs, crtcs, 2158 fb_helper->connector_count * 2159 sizeof(struct drm_fb_helper_crtc *)); 2160 } 2161 } 2162 out: 2163 kfree(crtcs); 2164 return best_score; 2165 } 2166 2167 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) 2168 { 2169 struct drm_device *dev = fb_helper->dev; 2170 struct drm_fb_helper_crtc **crtcs; 2171 struct drm_display_mode **modes; 2172 struct drm_fb_offset *offsets; 2173 bool *enabled; 2174 int width, height; 2175 int i; 2176 2177 DRM_DEBUG_KMS("\n"); 2178 2179 width = dev->mode_config.max_width; 2180 height = dev->mode_config.max_height; 2181 2182 crtcs = kcalloc(fb_helper->connector_count, 2183 sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); 2184 modes = kcalloc(fb_helper->connector_count, 2185 sizeof(struct drm_display_mode *), GFP_KERNEL); 2186 offsets = kcalloc(fb_helper->connector_count, 2187 sizeof(struct drm_fb_offset), GFP_KERNEL); 2188 enabled = kcalloc(fb_helper->connector_count, 2189 sizeof(bool), GFP_KERNEL); 2190 if (!crtcs || !modes || !enabled || !offsets) { 2191 DRM_ERROR("Memory allocation failed\n"); 2192 goto out; 2193 } 2194 2195 2196 drm_enable_connectors(fb_helper, enabled); 2197 2198 if (!(fb_helper->funcs->initial_config && 2199 fb_helper->funcs->initial_config(fb_helper, crtcs, modes, 2200 offsets, 2201 enabled, width, height))) { 2202 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); 2203 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); 2204 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); 2205 2206 if (!drm_target_cloned(fb_helper, modes, offsets, 2207 enabled, width, height) && 2208 !drm_target_preferred(fb_helper, modes, offsets, 2209 enabled, width, height)) 2210 DRM_ERROR("Unable to find initial modes\n"); 2211 2212 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 2213 width, height); 2214 2215 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 2216 } 2217 2218 /* need to set the modesets up here for use later */ 2219 /* fill out the connector<->crtc mappings into the modesets */ 2220 for (i = 0; i < fb_helper->crtc_count; i++) 2221 drm_fb_helper_modeset_release(fb_helper, 2222 &fb_helper->crtc_info[i].mode_set); 2223 2224 for (i = 0; i < fb_helper->connector_count; i++) { 2225 struct drm_display_mode *mode = modes[i]; 2226 struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; 2227 struct drm_fb_offset *offset = &offsets[i]; 2228 struct drm_mode_set *modeset = &fb_crtc->mode_set; 2229 2230 if (mode && fb_crtc) { 2231 struct drm_connector *connector = 2232 fb_helper->connector_info[i]->connector; 2233 2234 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", 2235 mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y); 2236 2237 fb_crtc->desired_mode = mode; 2238 fb_crtc->x = offset->x; 2239 fb_crtc->y = offset->y; 2240 modeset->mode = drm_mode_duplicate(dev, 2241 fb_crtc->desired_mode); 2242 drm_connector_reference(connector); 2243 modeset->connectors[modeset->num_connectors++] = connector; 2244 modeset->fb = fb_helper->fb; 2245 modeset->x = offset->x; 2246 modeset->y = offset->y; 2247 } 2248 } 2249 out: 2250 kfree(crtcs); 2251 kfree(modes); 2252 kfree(offsets); 2253 kfree(enabled); 2254 } 2255 2256 /** 2257 * drm_fb_helper_initial_config - setup a sane initial connector configuration 2258 * @fb_helper: fb_helper device struct 2259 * @bpp_sel: bpp value to use for the framebuffer configuration 2260 * 2261 * Scans the CRTCs and connectors and tries to put together an initial setup. 2262 * At the moment, this is a cloned configuration across all heads with 2263 * a new framebuffer object as the backing store. 2264 * 2265 * Note that this also registers the fbdev and so allows userspace to call into 2266 * the driver through the fbdev interfaces. 2267 * 2268 * This function will call down into the ->fb_probe callback to let 2269 * the driver allocate and initialize the fbdev info structure and the drm 2270 * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and 2271 * drm_fb_helper_fill_fix() are provided as helpers to setup simple default 2272 * values for the fbdev info structure. 2273 * 2274 * HANG DEBUGGING: 2275 * 2276 * When you have fbcon support built-in or already loaded, this function will do 2277 * a full modeset to setup the fbdev console. Due to locking misdesign in the 2278 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 2279 * console_lock. Until console_unlock is called no dmesg lines will be sent out 2280 * to consoles, not even serial console. This means when your driver crashes, 2281 * you will see absolutely nothing else but a system stuck in this function, 2282 * with no further output. Any kind of printk() you place within your own driver 2283 * or in the drm core modeset code will also never show up. 2284 * 2285 * Standard debug practice is to run the fbcon setup without taking the 2286 * console_lock as a hack, to be able to see backtraces and crashes on the 2287 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 2288 * cmdline option. 2289 * 2290 * The other option is to just disable fbdev emulation since very likely the 2291 * first modeset from userspace will crash in the same way, and is even easier 2292 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 2293 * kernel cmdline option. 2294 * 2295 * RETURNS: 2296 * Zero if everything went ok, nonzero otherwise. 2297 */ 2298 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 2299 { 2300 struct drm_device *dev = fb_helper->dev; 2301 int count = 0; 2302 2303 if (!drm_fbdev_emulation) 2304 return 0; 2305 2306 mutex_lock(&dev->mode_config.mutex); 2307 count = drm_fb_helper_probe_connector_modes(fb_helper, 2308 dev->mode_config.max_width, 2309 dev->mode_config.max_height); 2310 mutex_unlock(&dev->mode_config.mutex); 2311 /* 2312 * we shouldn't end up with no modes here. 2313 */ 2314 if (count == 0) 2315 dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n"); 2316 2317 drm_setup_crtcs(fb_helper); 2318 2319 return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 2320 } 2321 EXPORT_SYMBOL(drm_fb_helper_initial_config); 2322 2323 /** 2324 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 2325 * probing all the outputs attached to the fb 2326 * @fb_helper: the drm_fb_helper 2327 * 2328 * Scan the connectors attached to the fb_helper and try to put together a 2329 * setup after notification of a change in output configuration. 2330 * 2331 * Called at runtime, takes the mode config locks to be able to check/change the 2332 * modeset configuration. Must be run from process context (which usually means 2333 * either the output polling work or a work item launched from the driver's 2334 * hotplug interrupt). 2335 * 2336 * Note that drivers may call this even before calling 2337 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 2338 * for a race-free fbcon setup and will make sure that the fbdev emulation will 2339 * not miss any hotplug events. 2340 * 2341 * RETURNS: 2342 * 0 on success and a non-zero error code otherwise. 2343 */ 2344 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 2345 { 2346 struct drm_device *dev = fb_helper->dev; 2347 u32 max_width, max_height; 2348 2349 if (!drm_fbdev_emulation) 2350 return 0; 2351 2352 mutex_lock(&fb_helper->dev->mode_config.mutex); 2353 if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) { 2354 fb_helper->delayed_hotplug = true; 2355 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2356 return 0; 2357 } 2358 DRM_DEBUG_KMS("\n"); 2359 2360 max_width = fb_helper->fb->width; 2361 max_height = fb_helper->fb->height; 2362 2363 drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height); 2364 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2365 2366 drm_modeset_lock_all(dev); 2367 drm_setup_crtcs(fb_helper); 2368 drm_modeset_unlock_all(dev); 2369 drm_fb_helper_set_par(fb_helper->fbdev); 2370 2371 return 0; 2372 } 2373 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2374 2375 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 2376 * but the module doesn't depend on any fb console symbols. At least 2377 * attempt to load fbcon to avoid leaving the system without a usable console. 2378 */ 2379 int __init drm_fb_helper_modinit(void) 2380 { 2381 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 2382 const char *name = "fbcon"; 2383 struct module *fbcon; 2384 2385 mutex_lock(&module_mutex); 2386 fbcon = find_module(name); 2387 mutex_unlock(&module_mutex); 2388 2389 if (!fbcon) 2390 request_module_nowait(name); 2391 #endif 2392 return 0; 2393 } 2394 EXPORT_SYMBOL(drm_fb_helper_modinit); 2395