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