1 /* 2 * Copyright (c) 2006-2009 Red Hat Inc. 3 * Copyright (c) 2006-2008 Intel Corporation 4 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> 5 * 6 * DRM framebuffer helper functions 7 * 8 * Permission to use, copy, modify, distribute, and sell this software and its 9 * documentation for any purpose is hereby granted without fee, provided that 10 * the above copyright notice appear in all copies and that both that copyright 11 * notice and this permission notice appear in supporting documentation, and 12 * that the name of the copyright holders not be used in advertising or 13 * publicity pertaining to distribution of the software without specific, 14 * written prior permission. The copyright holders make no representations 15 * about the suitability of this software for any purpose. It is provided "as 16 * is" without express or implied warranty. 17 * 18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 24 * OF THIS SOFTWARE. 25 * 26 * Authors: 27 * Dave Airlie <airlied@linux.ie> 28 * Jesse Barnes <jesse.barnes@intel.com> 29 */ 30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 31 32 #include <linux/console.h> 33 #include <linux/dma-buf.h> 34 #include <linux/kernel.h> 35 #include <linux/module.h> 36 #include <linux/slab.h> 37 #include <linux/sysrq.h> 38 #include <linux/vmalloc.h> 39 40 #include <drm/drm_atomic.h> 41 #include <drm/drm_crtc.h> 42 #include <drm/drm_crtc_helper.h> 43 #include <drm/drm_drv.h> 44 #include <drm/drm_fb_helper.h> 45 #include <drm/drm_fourcc.h> 46 #include <drm/drm_print.h> 47 #include <drm/drm_vblank.h> 48 49 #include "drm_internal.h" 50 51 static bool drm_fbdev_emulation = true; 52 module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600); 53 MODULE_PARM_DESC(fbdev_emulation, 54 "Enable legacy fbdev emulation [default=true]"); 55 56 static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC; 57 module_param(drm_fbdev_overalloc, int, 0444); 58 MODULE_PARM_DESC(drm_fbdev_overalloc, 59 "Overallocation of the fbdev buffer (%) [default=" 60 __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]"); 61 62 /* 63 * In order to keep user-space compatibility, we want in certain use-cases 64 * to keep leaking the fbdev physical address to the user-space program 65 * handling the fbdev buffer. 66 * This is a bad habit essentially kept into closed source opengl driver 67 * that should really be moved into open-source upstream projects instead 68 * of using legacy physical addresses in user space to communicate with 69 * other out-of-tree kernel modules. 70 * 71 * This module_param *should* be removed as soon as possible and be 72 * considered as a broken and legacy behaviour from a modern fbdev device. 73 */ 74 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 75 static bool drm_leak_fbdev_smem = false; 76 module_param_unsafe(drm_leak_fbdev_smem, bool, 0600); 77 MODULE_PARM_DESC(drm_leak_fbdev_smem, 78 "Allow unsafe leaking fbdev physical smem address [default=false]"); 79 #endif 80 81 static LIST_HEAD(kernel_fb_helper_list); 82 static DEFINE_MUTEX(kernel_fb_helper_lock); 83 84 /** 85 * DOC: fbdev helpers 86 * 87 * The fb helper functions are useful to provide an fbdev on top of a drm kernel 88 * mode setting driver. They can be used mostly independently from the crtc 89 * helper functions used by many drivers to implement the kernel mode setting 90 * interfaces. 91 * 92 * Drivers that support a dumb buffer with a virtual address and mmap support, 93 * should try out the generic fbdev emulation using drm_fbdev_generic_setup(). 94 * 95 * Setup fbdev emulation by calling drm_fb_helper_fbdev_setup() and tear it 96 * down by calling drm_fb_helper_fbdev_teardown(). 97 * 98 * Drivers that need to handle connector hotplugging (e.g. dp mst) can't use 99 * the setup helper and will need to do the whole four-step setup process with 100 * drm_fb_helper_prepare(), drm_fb_helper_init(), 101 * drm_fb_helper_single_add_all_connectors(), enable hotplugging and 102 * drm_fb_helper_initial_config() to avoid a possible race window. 103 * 104 * At runtime drivers should restore the fbdev console by using 105 * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback. 106 * They should also notify the fb helper code from updates to the output 107 * configuration by using drm_fb_helper_output_poll_changed() as their 108 * &drm_mode_config_funcs.output_poll_changed callback. 109 * 110 * For suspend/resume consider using drm_mode_config_helper_suspend() and 111 * drm_mode_config_helper_resume() which takes care of fbdev as well. 112 * 113 * All other functions exported by the fb helper library can be used to 114 * implement the fbdev driver interface by the driver. 115 * 116 * It is possible, though perhaps somewhat tricky, to implement race-free 117 * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare() 118 * helper must be called first to initialize the minimum required to make 119 * hotplug detection work. Drivers also need to make sure to properly set up 120 * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init() 121 * it is safe to enable interrupts and start processing hotplug events. At the 122 * same time, drivers should initialize all modeset objects such as CRTCs, 123 * encoders and connectors. To finish up the fbdev helper initialization, the 124 * drm_fb_helper_init() function is called. To probe for all attached displays 125 * and set up an initial configuration using the detected hardware, drivers 126 * should call drm_fb_helper_single_add_all_connectors() followed by 127 * drm_fb_helper_initial_config(). 128 * 129 * If &drm_framebuffer_funcs.dirty is set, the 130 * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will 131 * accumulate changes and schedule &drm_fb_helper.dirty_work to run right 132 * away. This worker then calls the dirty() function ensuring that it will 133 * always run in process context since the fb_*() function could be running in 134 * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io 135 * callback it will also schedule dirty_work with the damage collected from the 136 * mmap page writes. Drivers can use drm_fb_helper_defio_init() to setup 137 * deferred I/O (coupled with drm_fb_helper_fbdev_teardown()). 138 */ 139 140 #define drm_fb_helper_for_each_connector(fbh, i__) \ 141 for (({ lockdep_assert_held(&(fbh)->lock); }), \ 142 i__ = 0; i__ < (fbh)->connector_count; i__++) 143 144 static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, 145 struct drm_connector *connector) 146 { 147 struct drm_fb_helper_connector *fb_conn; 148 struct drm_fb_helper_connector **temp; 149 unsigned int count; 150 151 if (!drm_fbdev_emulation) 152 return 0; 153 154 lockdep_assert_held(&fb_helper->lock); 155 156 count = fb_helper->connector_count + 1; 157 158 if (count > fb_helper->connector_info_alloc_count) { 159 size_t size = count * sizeof(fb_conn); 160 161 temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL); 162 if (!temp) 163 return -ENOMEM; 164 165 fb_helper->connector_info_alloc_count = count; 166 fb_helper->connector_info = temp; 167 } 168 169 fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL); 170 if (!fb_conn) 171 return -ENOMEM; 172 173 drm_connector_get(connector); 174 fb_conn->connector = connector; 175 fb_helper->connector_info[fb_helper->connector_count++] = fb_conn; 176 177 return 0; 178 } 179 180 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, 181 struct drm_connector *connector) 182 { 183 int err; 184 185 if (!fb_helper) 186 return 0; 187 188 mutex_lock(&fb_helper->lock); 189 err = __drm_fb_helper_add_one_connector(fb_helper, connector); 190 mutex_unlock(&fb_helper->lock); 191 192 return err; 193 } 194 EXPORT_SYMBOL(drm_fb_helper_add_one_connector); 195 196 /** 197 * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev 198 * emulation helper 199 * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL 200 * 201 * This functions adds all the available connectors for use with the given 202 * fb_helper. This is a separate step to allow drivers to freely assign 203 * connectors to the fbdev, e.g. if some are reserved for special purposes or 204 * not adequate to be used for the fbcon. 205 * 206 * This function is protected against concurrent connector hotadds/removals 207 * using drm_fb_helper_add_one_connector() and 208 * drm_fb_helper_remove_one_connector(). 209 */ 210 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) 211 { 212 struct drm_device *dev; 213 struct drm_connector *connector; 214 struct drm_connector_list_iter conn_iter; 215 int i, ret = 0; 216 217 if (!drm_fbdev_emulation || !fb_helper) 218 return 0; 219 220 dev = fb_helper->dev; 221 222 mutex_lock(&fb_helper->lock); 223 drm_connector_list_iter_begin(dev, &conn_iter); 224 drm_for_each_connector_iter(connector, &conn_iter) { 225 if (connector->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) 226 continue; 227 228 ret = __drm_fb_helper_add_one_connector(fb_helper, connector); 229 if (ret) 230 goto fail; 231 } 232 goto out; 233 234 fail: 235 drm_fb_helper_for_each_connector(fb_helper, i) { 236 struct drm_fb_helper_connector *fb_helper_connector = 237 fb_helper->connector_info[i]; 238 239 drm_connector_put(fb_helper_connector->connector); 240 241 kfree(fb_helper_connector); 242 fb_helper->connector_info[i] = NULL; 243 } 244 fb_helper->connector_count = 0; 245 out: 246 drm_connector_list_iter_end(&conn_iter); 247 mutex_unlock(&fb_helper->lock); 248 249 return ret; 250 } 251 EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); 252 253 static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 254 struct drm_connector *connector) 255 { 256 struct drm_fb_helper_connector *fb_helper_connector; 257 int i, j; 258 259 if (!drm_fbdev_emulation) 260 return 0; 261 262 lockdep_assert_held(&fb_helper->lock); 263 264 drm_fb_helper_for_each_connector(fb_helper, i) { 265 if (fb_helper->connector_info[i]->connector == connector) 266 break; 267 } 268 269 if (i == fb_helper->connector_count) 270 return -EINVAL; 271 fb_helper_connector = fb_helper->connector_info[i]; 272 drm_connector_put(fb_helper_connector->connector); 273 274 for (j = i + 1; j < fb_helper->connector_count; j++) 275 fb_helper->connector_info[j - 1] = fb_helper->connector_info[j]; 276 277 fb_helper->connector_count--; 278 kfree(fb_helper_connector); 279 280 return 0; 281 } 282 283 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 284 struct drm_connector *connector) 285 { 286 int err; 287 288 if (!fb_helper) 289 return 0; 290 291 mutex_lock(&fb_helper->lock); 292 err = __drm_fb_helper_remove_one_connector(fb_helper, connector); 293 mutex_unlock(&fb_helper->lock); 294 295 return err; 296 } 297 EXPORT_SYMBOL(drm_fb_helper_remove_one_connector); 298 299 static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc) 300 { 301 uint16_t *r_base, *g_base, *b_base; 302 303 if (crtc->funcs->gamma_set == NULL) 304 return; 305 306 r_base = crtc->gamma_store; 307 g_base = r_base + crtc->gamma_size; 308 b_base = g_base + crtc->gamma_size; 309 310 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 311 crtc->gamma_size, NULL); 312 } 313 314 /** 315 * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter 316 * @info: fbdev registered by the helper 317 */ 318 int drm_fb_helper_debug_enter(struct fb_info *info) 319 { 320 struct drm_fb_helper *helper = info->par; 321 const struct drm_crtc_helper_funcs *funcs; 322 struct drm_mode_set *mode_set; 323 324 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 325 mutex_lock(&helper->client.modeset_mutex); 326 drm_client_for_each_modeset(mode_set, &helper->client) { 327 if (!mode_set->crtc->enabled) 328 continue; 329 330 funcs = mode_set->crtc->helper_private; 331 if (funcs->mode_set_base_atomic == NULL) 332 continue; 333 334 if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev)) 335 continue; 336 337 funcs->mode_set_base_atomic(mode_set->crtc, 338 mode_set->fb, 339 mode_set->x, 340 mode_set->y, 341 ENTER_ATOMIC_MODE_SET); 342 } 343 mutex_unlock(&helper->client.modeset_mutex); 344 } 345 346 return 0; 347 } 348 EXPORT_SYMBOL(drm_fb_helper_debug_enter); 349 350 /** 351 * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave 352 * @info: fbdev registered by the helper 353 */ 354 int drm_fb_helper_debug_leave(struct fb_info *info) 355 { 356 struct drm_fb_helper *helper = info->par; 357 struct drm_client_dev *client = &helper->client; 358 struct drm_crtc *crtc; 359 const struct drm_crtc_helper_funcs *funcs; 360 struct drm_mode_set *mode_set; 361 struct drm_framebuffer *fb; 362 363 mutex_lock(&client->modeset_mutex); 364 drm_client_for_each_modeset(mode_set, client) { 365 crtc = mode_set->crtc; 366 if (drm_drv_uses_atomic_modeset(crtc->dev)) 367 continue; 368 369 funcs = crtc->helper_private; 370 fb = crtc->primary->fb; 371 372 if (!crtc->enabled) 373 continue; 374 375 if (!fb) { 376 DRM_ERROR("no fb to restore??\n"); 377 continue; 378 } 379 380 if (funcs->mode_set_base_atomic == NULL) 381 continue; 382 383 drm_fb_helper_restore_lut_atomic(mode_set->crtc); 384 funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x, 385 crtc->y, LEAVE_ATOMIC_MODE_SET); 386 } 387 mutex_unlock(&client->modeset_mutex); 388 389 return 0; 390 } 391 EXPORT_SYMBOL(drm_fb_helper_debug_leave); 392 393 /** 394 * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration 395 * @fb_helper: driver-allocated fbdev helper, can be NULL 396 * 397 * This should be called from driver's drm &drm_driver.lastclose callback 398 * when implementing an fbcon on top of kms using this helper. This ensures that 399 * the user isn't greeted with a black screen when e.g. X dies. 400 * 401 * RETURNS: 402 * Zero if everything went ok, negative error code otherwise. 403 */ 404 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 405 { 406 bool do_delayed; 407 int ret; 408 409 if (!drm_fbdev_emulation || !fb_helper) 410 return -ENODEV; 411 412 if (READ_ONCE(fb_helper->deferred_setup)) 413 return 0; 414 415 mutex_lock(&fb_helper->lock); 416 /* 417 * TODO: 418 * We should bail out here if there is a master by dropping _force. 419 * Currently these igt tests fail if we do that: 420 * - kms_fbcon_fbt@psr 421 * - kms_fbcon_fbt@psr-suspend 422 * 423 * So first these tests need to be fixed so they drop master or don't 424 * have an fd open. 425 */ 426 ret = drm_client_modeset_commit_force(&fb_helper->client); 427 428 do_delayed = fb_helper->delayed_hotplug; 429 if (do_delayed) 430 fb_helper->delayed_hotplug = false; 431 mutex_unlock(&fb_helper->lock); 432 433 if (do_delayed) 434 drm_fb_helper_hotplug_event(fb_helper); 435 436 return ret; 437 } 438 EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked); 439 440 #ifdef CONFIG_MAGIC_SYSRQ 441 /* 442 * restore fbcon display for all kms driver's using this helper, used for sysrq 443 * and panic handling. 444 */ 445 static bool drm_fb_helper_force_kernel_mode(void) 446 { 447 bool ret, error = false; 448 struct drm_fb_helper *helper; 449 450 if (list_empty(&kernel_fb_helper_list)) 451 return false; 452 453 list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { 454 struct drm_device *dev = helper->dev; 455 456 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 457 continue; 458 459 mutex_lock(&helper->lock); 460 ret = drm_client_modeset_commit_force(&helper->client); 461 if (ret) 462 error = true; 463 mutex_unlock(&helper->lock); 464 } 465 return error; 466 } 467 468 static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) 469 { 470 bool ret; 471 472 ret = drm_fb_helper_force_kernel_mode(); 473 if (ret == true) 474 DRM_ERROR("Failed to restore crtc configuration\n"); 475 } 476 static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); 477 478 static void drm_fb_helper_sysrq(int dummy1) 479 { 480 schedule_work(&drm_fb_helper_restore_work); 481 } 482 483 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { 484 .handler = drm_fb_helper_sysrq, 485 .help_msg = "force-fb(V)", 486 .action_msg = "Restore framebuffer console", 487 }; 488 #else 489 static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; 490 #endif 491 492 static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode) 493 { 494 struct drm_fb_helper *fb_helper = info->par; 495 496 mutex_lock(&fb_helper->lock); 497 drm_client_modeset_dpms(&fb_helper->client, dpms_mode); 498 mutex_unlock(&fb_helper->lock); 499 } 500 501 /** 502 * drm_fb_helper_blank - implementation for &fb_ops.fb_blank 503 * @blank: desired blanking state 504 * @info: fbdev registered by the helper 505 */ 506 int drm_fb_helper_blank(int blank, struct fb_info *info) 507 { 508 if (oops_in_progress) 509 return -EBUSY; 510 511 switch (blank) { 512 /* Display: On; HSync: On, VSync: On */ 513 case FB_BLANK_UNBLANK: 514 drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON); 515 break; 516 /* Display: Off; HSync: On, VSync: On */ 517 case FB_BLANK_NORMAL: 518 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 519 break; 520 /* Display: Off; HSync: Off, VSync: On */ 521 case FB_BLANK_HSYNC_SUSPEND: 522 drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY); 523 break; 524 /* Display: Off; HSync: On, VSync: Off */ 525 case FB_BLANK_VSYNC_SUSPEND: 526 drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND); 527 break; 528 /* Display: Off; HSync: Off, VSync: Off */ 529 case FB_BLANK_POWERDOWN: 530 drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF); 531 break; 532 } 533 return 0; 534 } 535 EXPORT_SYMBOL(drm_fb_helper_blank); 536 537 static void drm_fb_helper_resume_worker(struct work_struct *work) 538 { 539 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 540 resume_work); 541 542 console_lock(); 543 fb_set_suspend(helper->fbdev, 0); 544 console_unlock(); 545 } 546 547 static void drm_fb_helper_dirty_blit_real(struct drm_fb_helper *fb_helper, 548 struct drm_clip_rect *clip) 549 { 550 struct drm_framebuffer *fb = fb_helper->fb; 551 unsigned int cpp = fb->format->cpp[0]; 552 size_t offset = clip->y1 * fb->pitches[0] + clip->x1 * cpp; 553 void *src = fb_helper->fbdev->screen_buffer + offset; 554 void *dst = fb_helper->buffer->vaddr + offset; 555 size_t len = (clip->x2 - clip->x1) * cpp; 556 unsigned int y; 557 558 for (y = clip->y1; y < clip->y2; y++) { 559 memcpy(dst, src, len); 560 src += fb->pitches[0]; 561 dst += fb->pitches[0]; 562 } 563 } 564 565 static void drm_fb_helper_dirty_work(struct work_struct *work) 566 { 567 struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, 568 dirty_work); 569 struct drm_clip_rect *clip = &helper->dirty_clip; 570 struct drm_clip_rect clip_copy; 571 unsigned long flags; 572 573 spin_lock_irqsave(&helper->dirty_lock, flags); 574 clip_copy = *clip; 575 clip->x1 = clip->y1 = ~0; 576 clip->x2 = clip->y2 = 0; 577 spin_unlock_irqrestore(&helper->dirty_lock, flags); 578 579 /* call dirty callback only when it has been really touched */ 580 if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2) { 581 /* Generic fbdev uses a shadow buffer */ 582 if (helper->buffer) 583 drm_fb_helper_dirty_blit_real(helper, &clip_copy); 584 helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1); 585 } 586 } 587 588 /** 589 * drm_fb_helper_prepare - setup a drm_fb_helper structure 590 * @dev: DRM device 591 * @helper: driver-allocated fbdev helper structure to set up 592 * @funcs: pointer to structure of functions associate with this helper 593 * 594 * Sets up the bare minimum to make the framebuffer helper usable. This is 595 * useful to implement race-free initialization of the polling helpers. 596 */ 597 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 598 const struct drm_fb_helper_funcs *funcs) 599 { 600 INIT_LIST_HEAD(&helper->kernel_fb_list); 601 spin_lock_init(&helper->dirty_lock); 602 INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker); 603 INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work); 604 helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0; 605 mutex_init(&helper->lock); 606 helper->funcs = funcs; 607 helper->dev = dev; 608 } 609 EXPORT_SYMBOL(drm_fb_helper_prepare); 610 611 /** 612 * drm_fb_helper_init - initialize a &struct drm_fb_helper 613 * @dev: drm device 614 * @fb_helper: driver-allocated fbdev helper structure to initialize 615 * @max_conn_count: max connector count (not used) 616 * 617 * This allocates the structures for the fbdev helper with the given limits. 618 * Note that this won't yet touch the hardware (through the driver interfaces) 619 * nor register the fbdev. This is only done in drm_fb_helper_initial_config() 620 * to allow driver writes more control over the exact init sequence. 621 * 622 * Drivers must call drm_fb_helper_prepare() before calling this function. 623 * 624 * RETURNS: 625 * Zero if everything went ok, nonzero otherwise. 626 */ 627 int drm_fb_helper_init(struct drm_device *dev, 628 struct drm_fb_helper *fb_helper, 629 int max_conn_count) 630 { 631 int ret; 632 633 if (!drm_fbdev_emulation) { 634 dev->fb_helper = fb_helper; 635 return 0; 636 } 637 638 /* 639 * If this is not the generic fbdev client, initialize a drm_client 640 * without callbacks so we can use the modesets. 641 */ 642 if (!fb_helper->client.funcs) { 643 ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL); 644 if (ret) 645 return ret; 646 } 647 648 fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); 649 if (!fb_helper->connector_info) 650 goto out_free; 651 652 fb_helper->connector_info_alloc_count = dev->mode_config.num_connector; 653 fb_helper->connector_count = 0; 654 655 dev->fb_helper = fb_helper; 656 657 return 0; 658 out_free: 659 drm_client_release(&fb_helper->client); 660 661 return -ENOMEM; 662 } 663 EXPORT_SYMBOL(drm_fb_helper_init); 664 665 /** 666 * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members 667 * @fb_helper: driver-allocated fbdev helper 668 * 669 * A helper to alloc fb_info and the members cmap and apertures. Called 670 * by the driver within the fb_probe fb_helper callback function. Drivers do not 671 * need to release the allocated fb_info structure themselves, this is 672 * automatically done when calling drm_fb_helper_fini(). 673 * 674 * RETURNS: 675 * fb_info pointer if things went okay, pointer containing error code 676 * otherwise 677 */ 678 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) 679 { 680 struct device *dev = fb_helper->dev->dev; 681 struct fb_info *info; 682 int ret; 683 684 info = framebuffer_alloc(0, dev); 685 if (!info) 686 return ERR_PTR(-ENOMEM); 687 688 ret = fb_alloc_cmap(&info->cmap, 256, 0); 689 if (ret) 690 goto err_release; 691 692 info->apertures = alloc_apertures(1); 693 if (!info->apertures) { 694 ret = -ENOMEM; 695 goto err_free_cmap; 696 } 697 698 fb_helper->fbdev = info; 699 info->skip_vt_switch = true; 700 701 return info; 702 703 err_free_cmap: 704 fb_dealloc_cmap(&info->cmap); 705 err_release: 706 framebuffer_release(info); 707 return ERR_PTR(ret); 708 } 709 EXPORT_SYMBOL(drm_fb_helper_alloc_fbi); 710 711 /** 712 * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device 713 * @fb_helper: driver-allocated fbdev helper, can be NULL 714 * 715 * A wrapper around unregister_framebuffer, to release the fb_info 716 * framebuffer device. This must be called before releasing all resources for 717 * @fb_helper by calling drm_fb_helper_fini(). 718 */ 719 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 720 { 721 if (fb_helper && fb_helper->fbdev) 722 unregister_framebuffer(fb_helper->fbdev); 723 } 724 EXPORT_SYMBOL(drm_fb_helper_unregister_fbi); 725 726 /** 727 * drm_fb_helper_fini - finialize a &struct drm_fb_helper 728 * @fb_helper: driver-allocated fbdev helper, can be NULL 729 * 730 * This cleans up all remaining resources associated with @fb_helper. Must be 731 * called after drm_fb_helper_unlink_fbi() was called. 732 */ 733 void drm_fb_helper_fini(struct drm_fb_helper *fb_helper) 734 { 735 struct fb_info *info; 736 int i; 737 738 if (!fb_helper) 739 return; 740 741 fb_helper->dev->fb_helper = NULL; 742 743 if (!drm_fbdev_emulation) 744 return; 745 746 cancel_work_sync(&fb_helper->resume_work); 747 cancel_work_sync(&fb_helper->dirty_work); 748 749 info = fb_helper->fbdev; 750 if (info) { 751 if (info->cmap.len) 752 fb_dealloc_cmap(&info->cmap); 753 framebuffer_release(info); 754 } 755 fb_helper->fbdev = NULL; 756 757 mutex_lock(&kernel_fb_helper_lock); 758 if (!list_empty(&fb_helper->kernel_fb_list)) { 759 list_del(&fb_helper->kernel_fb_list); 760 if (list_empty(&kernel_fb_helper_list)) 761 unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 762 } 763 mutex_unlock(&kernel_fb_helper_lock); 764 765 mutex_destroy(&fb_helper->lock); 766 767 if (!fb_helper->client.funcs) 768 drm_client_release(&fb_helper->client); 769 770 for (i = 0; i < fb_helper->connector_count; i++) { 771 drm_connector_put(fb_helper->connector_info[i]->connector); 772 kfree(fb_helper->connector_info[i]); 773 } 774 kfree(fb_helper->connector_info); 775 } 776 EXPORT_SYMBOL(drm_fb_helper_fini); 777 778 /** 779 * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer 780 * @fb_helper: driver-allocated fbdev helper, can be NULL 781 * 782 * A wrapper around unlink_framebuffer implemented by fbdev core 783 */ 784 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) 785 { 786 if (fb_helper && fb_helper->fbdev) 787 unlink_framebuffer(fb_helper->fbdev); 788 } 789 EXPORT_SYMBOL(drm_fb_helper_unlink_fbi); 790 791 static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y, 792 u32 width, u32 height) 793 { 794 struct drm_fb_helper *helper = info->par; 795 struct drm_clip_rect *clip = &helper->dirty_clip; 796 unsigned long flags; 797 798 if (!helper->fb->funcs->dirty) 799 return; 800 801 spin_lock_irqsave(&helper->dirty_lock, flags); 802 clip->x1 = min_t(u32, clip->x1, x); 803 clip->y1 = min_t(u32, clip->y1, y); 804 clip->x2 = max_t(u32, clip->x2, x + width); 805 clip->y2 = max_t(u32, clip->y2, y + height); 806 spin_unlock_irqrestore(&helper->dirty_lock, flags); 807 808 schedule_work(&helper->dirty_work); 809 } 810 811 /** 812 * drm_fb_helper_deferred_io() - fbdev deferred_io callback function 813 * @info: fb_info struct pointer 814 * @pagelist: list of dirty mmap framebuffer pages 815 * 816 * This function is used as the &fb_deferred_io.deferred_io 817 * callback function for flushing the fbdev mmap writes. 818 */ 819 void drm_fb_helper_deferred_io(struct fb_info *info, 820 struct list_head *pagelist) 821 { 822 unsigned long start, end, min, max; 823 struct page *page; 824 u32 y1, y2; 825 826 min = ULONG_MAX; 827 max = 0; 828 list_for_each_entry(page, pagelist, lru) { 829 start = page->index << PAGE_SHIFT; 830 end = start + PAGE_SIZE - 1; 831 min = min(min, start); 832 max = max(max, end); 833 } 834 835 if (min < max) { 836 y1 = min / info->fix.line_length; 837 y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length), 838 info->var.yres); 839 drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1); 840 } 841 } 842 EXPORT_SYMBOL(drm_fb_helper_deferred_io); 843 844 /** 845 * drm_fb_helper_defio_init - fbdev deferred I/O initialization 846 * @fb_helper: driver-allocated fbdev helper 847 * 848 * This function allocates &fb_deferred_io, sets callback to 849 * drm_fb_helper_deferred_io(), delay to 50ms and calls fb_deferred_io_init(). 850 * It should be called from the &drm_fb_helper_funcs->fb_probe callback. 851 * drm_fb_helper_fbdev_teardown() cleans up deferred I/O. 852 * 853 * NOTE: A copy of &fb_ops is made and assigned to &info->fbops. This is done 854 * because fb_deferred_io_cleanup() clears &fbops->fb_mmap and would thereby 855 * affect other instances of that &fb_ops. 856 * 857 * Returns: 858 * 0 on success or a negative error code on failure. 859 */ 860 int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper) 861 { 862 struct fb_info *info = fb_helper->fbdev; 863 struct fb_deferred_io *fbdefio; 864 struct fb_ops *fbops; 865 866 fbdefio = kzalloc(sizeof(*fbdefio), GFP_KERNEL); 867 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); 868 if (!fbdefio || !fbops) { 869 kfree(fbdefio); 870 kfree(fbops); 871 return -ENOMEM; 872 } 873 874 info->fbdefio = fbdefio; 875 fbdefio->delay = msecs_to_jiffies(50); 876 fbdefio->deferred_io = drm_fb_helper_deferred_io; 877 878 *fbops = *info->fbops; 879 info->fbops = fbops; 880 881 fb_deferred_io_init(info); 882 883 return 0; 884 } 885 EXPORT_SYMBOL(drm_fb_helper_defio_init); 886 887 /** 888 * drm_fb_helper_sys_read - wrapper around fb_sys_read 889 * @info: fb_info struct pointer 890 * @buf: userspace buffer to read from framebuffer memory 891 * @count: number of bytes to read from framebuffer memory 892 * @ppos: read offset within framebuffer memory 893 * 894 * A wrapper around fb_sys_read implemented by fbdev core 895 */ 896 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 897 size_t count, loff_t *ppos) 898 { 899 return fb_sys_read(info, buf, count, ppos); 900 } 901 EXPORT_SYMBOL(drm_fb_helper_sys_read); 902 903 /** 904 * drm_fb_helper_sys_write - wrapper around fb_sys_write 905 * @info: fb_info struct pointer 906 * @buf: userspace buffer to write to framebuffer memory 907 * @count: number of bytes to write to framebuffer memory 908 * @ppos: write offset within framebuffer memory 909 * 910 * A wrapper around fb_sys_write implemented by fbdev core 911 */ 912 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 913 size_t count, loff_t *ppos) 914 { 915 ssize_t ret; 916 917 ret = fb_sys_write(info, buf, count, ppos); 918 if (ret > 0) 919 drm_fb_helper_dirty(info, 0, 0, info->var.xres, 920 info->var.yres); 921 922 return ret; 923 } 924 EXPORT_SYMBOL(drm_fb_helper_sys_write); 925 926 /** 927 * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect 928 * @info: fbdev registered by the helper 929 * @rect: info about rectangle to fill 930 * 931 * A wrapper around sys_fillrect implemented by fbdev core 932 */ 933 void drm_fb_helper_sys_fillrect(struct fb_info *info, 934 const struct fb_fillrect *rect) 935 { 936 sys_fillrect(info, rect); 937 drm_fb_helper_dirty(info, rect->dx, rect->dy, 938 rect->width, rect->height); 939 } 940 EXPORT_SYMBOL(drm_fb_helper_sys_fillrect); 941 942 /** 943 * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea 944 * @info: fbdev registered by the helper 945 * @area: info about area to copy 946 * 947 * A wrapper around sys_copyarea implemented by fbdev core 948 */ 949 void drm_fb_helper_sys_copyarea(struct fb_info *info, 950 const struct fb_copyarea *area) 951 { 952 sys_copyarea(info, area); 953 drm_fb_helper_dirty(info, area->dx, area->dy, 954 area->width, area->height); 955 } 956 EXPORT_SYMBOL(drm_fb_helper_sys_copyarea); 957 958 /** 959 * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit 960 * @info: fbdev registered by the helper 961 * @image: info about image to blit 962 * 963 * A wrapper around sys_imageblit implemented by fbdev core 964 */ 965 void drm_fb_helper_sys_imageblit(struct fb_info *info, 966 const struct fb_image *image) 967 { 968 sys_imageblit(info, image); 969 drm_fb_helper_dirty(info, image->dx, image->dy, 970 image->width, image->height); 971 } 972 EXPORT_SYMBOL(drm_fb_helper_sys_imageblit); 973 974 /** 975 * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect 976 * @info: fbdev registered by the helper 977 * @rect: info about rectangle to fill 978 * 979 * A wrapper around cfb_fillrect implemented by fbdev core 980 */ 981 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 982 const struct fb_fillrect *rect) 983 { 984 cfb_fillrect(info, rect); 985 drm_fb_helper_dirty(info, rect->dx, rect->dy, 986 rect->width, rect->height); 987 } 988 EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect); 989 990 /** 991 * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea 992 * @info: fbdev registered by the helper 993 * @area: info about area to copy 994 * 995 * A wrapper around cfb_copyarea implemented by fbdev core 996 */ 997 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 998 const struct fb_copyarea *area) 999 { 1000 cfb_copyarea(info, area); 1001 drm_fb_helper_dirty(info, area->dx, area->dy, 1002 area->width, area->height); 1003 } 1004 EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea); 1005 1006 /** 1007 * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit 1008 * @info: fbdev registered by the helper 1009 * @image: info about image to blit 1010 * 1011 * A wrapper around cfb_imageblit implemented by fbdev core 1012 */ 1013 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 1014 const struct fb_image *image) 1015 { 1016 cfb_imageblit(info, image); 1017 drm_fb_helper_dirty(info, image->dx, image->dy, 1018 image->width, image->height); 1019 } 1020 EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit); 1021 1022 /** 1023 * drm_fb_helper_set_suspend - wrapper around fb_set_suspend 1024 * @fb_helper: driver-allocated fbdev helper, can be NULL 1025 * @suspend: whether to suspend or resume 1026 * 1027 * A wrapper around fb_set_suspend implemented by fbdev core. 1028 * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take 1029 * the lock yourself 1030 */ 1031 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend) 1032 { 1033 if (fb_helper && fb_helper->fbdev) 1034 fb_set_suspend(fb_helper->fbdev, suspend); 1035 } 1036 EXPORT_SYMBOL(drm_fb_helper_set_suspend); 1037 1038 /** 1039 * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also 1040 * takes the console lock 1041 * @fb_helper: driver-allocated fbdev helper, can be NULL 1042 * @suspend: whether to suspend or resume 1043 * 1044 * A wrapper around fb_set_suspend() that takes the console lock. If the lock 1045 * isn't available on resume, a worker is tasked with waiting for the lock 1046 * to become available. The console lock can be pretty contented on resume 1047 * due to all the printk activity. 1048 * 1049 * This function can be called multiple times with the same state since 1050 * &fb_info.state is checked to see if fbdev is running or not before locking. 1051 * 1052 * Use drm_fb_helper_set_suspend() if you need to take the lock yourself. 1053 */ 1054 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 1055 bool suspend) 1056 { 1057 if (!fb_helper || !fb_helper->fbdev) 1058 return; 1059 1060 /* make sure there's no pending/ongoing resume */ 1061 flush_work(&fb_helper->resume_work); 1062 1063 if (suspend) { 1064 if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING) 1065 return; 1066 1067 console_lock(); 1068 1069 } else { 1070 if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING) 1071 return; 1072 1073 if (!console_trylock()) { 1074 schedule_work(&fb_helper->resume_work); 1075 return; 1076 } 1077 } 1078 1079 fb_set_suspend(fb_helper->fbdev, suspend); 1080 console_unlock(); 1081 } 1082 EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked); 1083 1084 static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info) 1085 { 1086 u32 *palette = (u32 *)info->pseudo_palette; 1087 int i; 1088 1089 if (cmap->start + cmap->len > 16) 1090 return -EINVAL; 1091 1092 for (i = 0; i < cmap->len; ++i) { 1093 u16 red = cmap->red[i]; 1094 u16 green = cmap->green[i]; 1095 u16 blue = cmap->blue[i]; 1096 u32 value; 1097 1098 red >>= 16 - info->var.red.length; 1099 green >>= 16 - info->var.green.length; 1100 blue >>= 16 - info->var.blue.length; 1101 value = (red << info->var.red.offset) | 1102 (green << info->var.green.offset) | 1103 (blue << info->var.blue.offset); 1104 if (info->var.transp.length > 0) { 1105 u32 mask = (1 << info->var.transp.length) - 1; 1106 1107 mask <<= info->var.transp.offset; 1108 value |= mask; 1109 } 1110 palette[cmap->start + i] = value; 1111 } 1112 1113 return 0; 1114 } 1115 1116 static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info) 1117 { 1118 struct drm_fb_helper *fb_helper = info->par; 1119 struct drm_mode_set *modeset; 1120 struct drm_crtc *crtc; 1121 u16 *r, *g, *b; 1122 int ret = 0; 1123 1124 drm_modeset_lock_all(fb_helper->dev); 1125 drm_client_for_each_modeset(modeset, &fb_helper->client) { 1126 crtc = modeset->crtc; 1127 if (!crtc->funcs->gamma_set || !crtc->gamma_size) 1128 return -EINVAL; 1129 1130 if (cmap->start + cmap->len > crtc->gamma_size) 1131 return -EINVAL; 1132 1133 r = crtc->gamma_store; 1134 g = r + crtc->gamma_size; 1135 b = g + crtc->gamma_size; 1136 1137 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 1138 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 1139 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 1140 1141 ret = crtc->funcs->gamma_set(crtc, r, g, b, 1142 crtc->gamma_size, NULL); 1143 if (ret) 1144 return ret; 1145 } 1146 drm_modeset_unlock_all(fb_helper->dev); 1147 1148 return ret; 1149 } 1150 1151 static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc, 1152 struct fb_cmap *cmap) 1153 { 1154 struct drm_device *dev = crtc->dev; 1155 struct drm_property_blob *gamma_lut; 1156 struct drm_color_lut *lut; 1157 int size = crtc->gamma_size; 1158 int i; 1159 1160 if (!size || cmap->start + cmap->len > size) 1161 return ERR_PTR(-EINVAL); 1162 1163 gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL); 1164 if (IS_ERR(gamma_lut)) 1165 return gamma_lut; 1166 1167 lut = gamma_lut->data; 1168 if (cmap->start || cmap->len != size) { 1169 u16 *r = crtc->gamma_store; 1170 u16 *g = r + crtc->gamma_size; 1171 u16 *b = g + crtc->gamma_size; 1172 1173 for (i = 0; i < cmap->start; i++) { 1174 lut[i].red = r[i]; 1175 lut[i].green = g[i]; 1176 lut[i].blue = b[i]; 1177 } 1178 for (i = cmap->start + cmap->len; i < size; i++) { 1179 lut[i].red = r[i]; 1180 lut[i].green = g[i]; 1181 lut[i].blue = b[i]; 1182 } 1183 } 1184 1185 for (i = 0; i < cmap->len; i++) { 1186 lut[cmap->start + i].red = cmap->red[i]; 1187 lut[cmap->start + i].green = cmap->green[i]; 1188 lut[cmap->start + i].blue = cmap->blue[i]; 1189 } 1190 1191 return gamma_lut; 1192 } 1193 1194 static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info) 1195 { 1196 struct drm_fb_helper *fb_helper = info->par; 1197 struct drm_device *dev = fb_helper->dev; 1198 struct drm_property_blob *gamma_lut = NULL; 1199 struct drm_modeset_acquire_ctx ctx; 1200 struct drm_crtc_state *crtc_state; 1201 struct drm_atomic_state *state; 1202 struct drm_mode_set *modeset; 1203 struct drm_crtc *crtc; 1204 u16 *r, *g, *b; 1205 bool replaced; 1206 int ret = 0; 1207 1208 drm_modeset_acquire_init(&ctx, 0); 1209 1210 state = drm_atomic_state_alloc(dev); 1211 if (!state) { 1212 ret = -ENOMEM; 1213 goto out_ctx; 1214 } 1215 1216 state->acquire_ctx = &ctx; 1217 retry: 1218 drm_client_for_each_modeset(modeset, &fb_helper->client) { 1219 crtc = modeset->crtc; 1220 1221 if (!gamma_lut) 1222 gamma_lut = setcmap_new_gamma_lut(crtc, cmap); 1223 if (IS_ERR(gamma_lut)) { 1224 ret = PTR_ERR(gamma_lut); 1225 gamma_lut = NULL; 1226 goto out_state; 1227 } 1228 1229 crtc_state = drm_atomic_get_crtc_state(state, crtc); 1230 if (IS_ERR(crtc_state)) { 1231 ret = PTR_ERR(crtc_state); 1232 goto out_state; 1233 } 1234 1235 replaced = drm_property_replace_blob(&crtc_state->degamma_lut, 1236 NULL); 1237 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL); 1238 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut, 1239 gamma_lut); 1240 crtc_state->color_mgmt_changed |= replaced; 1241 } 1242 1243 ret = drm_atomic_commit(state); 1244 if (ret) 1245 goto out_state; 1246 1247 drm_client_for_each_modeset(modeset, &fb_helper->client) { 1248 crtc = modeset->crtc; 1249 1250 r = crtc->gamma_store; 1251 g = r + crtc->gamma_size; 1252 b = g + crtc->gamma_size; 1253 1254 memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r)); 1255 memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g)); 1256 memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b)); 1257 } 1258 1259 out_state: 1260 if (ret == -EDEADLK) 1261 goto backoff; 1262 1263 drm_property_blob_put(gamma_lut); 1264 drm_atomic_state_put(state); 1265 out_ctx: 1266 drm_modeset_drop_locks(&ctx); 1267 drm_modeset_acquire_fini(&ctx); 1268 1269 return ret; 1270 1271 backoff: 1272 drm_atomic_state_clear(state); 1273 drm_modeset_backoff(&ctx); 1274 goto retry; 1275 } 1276 1277 /** 1278 * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap 1279 * @cmap: cmap to set 1280 * @info: fbdev registered by the helper 1281 */ 1282 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) 1283 { 1284 struct drm_fb_helper *fb_helper = info->par; 1285 struct drm_device *dev = fb_helper->dev; 1286 int ret; 1287 1288 if (oops_in_progress) 1289 return -EBUSY; 1290 1291 mutex_lock(&fb_helper->lock); 1292 1293 if (!drm_master_internal_acquire(dev)) { 1294 ret = -EBUSY; 1295 goto unlock; 1296 } 1297 1298 mutex_lock(&fb_helper->client.modeset_mutex); 1299 if (info->fix.visual == FB_VISUAL_TRUECOLOR) 1300 ret = setcmap_pseudo_palette(cmap, info); 1301 else if (drm_drv_uses_atomic_modeset(fb_helper->dev)) 1302 ret = setcmap_atomic(cmap, info); 1303 else 1304 ret = setcmap_legacy(cmap, info); 1305 mutex_unlock(&fb_helper->client.modeset_mutex); 1306 1307 drm_master_internal_release(dev); 1308 unlock: 1309 mutex_unlock(&fb_helper->lock); 1310 1311 return ret; 1312 } 1313 EXPORT_SYMBOL(drm_fb_helper_setcmap); 1314 1315 /** 1316 * drm_fb_helper_ioctl - legacy ioctl implementation 1317 * @info: fbdev registered by the helper 1318 * @cmd: ioctl command 1319 * @arg: ioctl argument 1320 * 1321 * A helper to implement the standard fbdev ioctl. Only 1322 * FBIO_WAITFORVSYNC is implemented for now. 1323 */ 1324 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 1325 unsigned long arg) 1326 { 1327 struct drm_fb_helper *fb_helper = info->par; 1328 struct drm_device *dev = fb_helper->dev; 1329 struct drm_crtc *crtc; 1330 int ret = 0; 1331 1332 mutex_lock(&fb_helper->lock); 1333 if (!drm_master_internal_acquire(dev)) { 1334 ret = -EBUSY; 1335 goto unlock; 1336 } 1337 1338 switch (cmd) { 1339 case FBIO_WAITFORVSYNC: 1340 /* 1341 * Only consider the first CRTC. 1342 * 1343 * This ioctl is supposed to take the CRTC number as 1344 * an argument, but in fbdev times, what that number 1345 * was supposed to be was quite unclear, different 1346 * drivers were passing that argument differently 1347 * (some by reference, some by value), and most of the 1348 * userspace applications were just hardcoding 0 as an 1349 * argument. 1350 * 1351 * The first CRTC should be the integrated panel on 1352 * most drivers, so this is the best choice we can 1353 * make. If we're not smart enough here, one should 1354 * just consider switch the userspace to KMS. 1355 */ 1356 crtc = fb_helper->client.modesets[0].crtc; 1357 1358 /* 1359 * Only wait for a vblank event if the CRTC is 1360 * enabled, otherwise just don't do anythintg, 1361 * not even report an error. 1362 */ 1363 ret = drm_crtc_vblank_get(crtc); 1364 if (!ret) { 1365 drm_crtc_wait_one_vblank(crtc); 1366 drm_crtc_vblank_put(crtc); 1367 } 1368 1369 ret = 0; 1370 break; 1371 default: 1372 ret = -ENOTTY; 1373 } 1374 1375 drm_master_internal_release(dev); 1376 unlock: 1377 mutex_unlock(&fb_helper->lock); 1378 return ret; 1379 } 1380 EXPORT_SYMBOL(drm_fb_helper_ioctl); 1381 1382 static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1, 1383 const struct fb_var_screeninfo *var_2) 1384 { 1385 return var_1->bits_per_pixel == var_2->bits_per_pixel && 1386 var_1->grayscale == var_2->grayscale && 1387 var_1->red.offset == var_2->red.offset && 1388 var_1->red.length == var_2->red.length && 1389 var_1->red.msb_right == var_2->red.msb_right && 1390 var_1->green.offset == var_2->green.offset && 1391 var_1->green.length == var_2->green.length && 1392 var_1->green.msb_right == var_2->green.msb_right && 1393 var_1->blue.offset == var_2->blue.offset && 1394 var_1->blue.length == var_2->blue.length && 1395 var_1->blue.msb_right == var_2->blue.msb_right && 1396 var_1->transp.offset == var_2->transp.offset && 1397 var_1->transp.length == var_2->transp.length && 1398 var_1->transp.msb_right == var_2->transp.msb_right; 1399 } 1400 1401 static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var, 1402 u8 depth) 1403 { 1404 switch (depth) { 1405 case 8: 1406 var->red.offset = 0; 1407 var->green.offset = 0; 1408 var->blue.offset = 0; 1409 var->red.length = 8; /* 8bit DAC */ 1410 var->green.length = 8; 1411 var->blue.length = 8; 1412 var->transp.offset = 0; 1413 var->transp.length = 0; 1414 break; 1415 case 15: 1416 var->red.offset = 10; 1417 var->green.offset = 5; 1418 var->blue.offset = 0; 1419 var->red.length = 5; 1420 var->green.length = 5; 1421 var->blue.length = 5; 1422 var->transp.offset = 15; 1423 var->transp.length = 1; 1424 break; 1425 case 16: 1426 var->red.offset = 11; 1427 var->green.offset = 5; 1428 var->blue.offset = 0; 1429 var->red.length = 5; 1430 var->green.length = 6; 1431 var->blue.length = 5; 1432 var->transp.offset = 0; 1433 break; 1434 case 24: 1435 var->red.offset = 16; 1436 var->green.offset = 8; 1437 var->blue.offset = 0; 1438 var->red.length = 8; 1439 var->green.length = 8; 1440 var->blue.length = 8; 1441 var->transp.offset = 0; 1442 var->transp.length = 0; 1443 break; 1444 case 32: 1445 var->red.offset = 16; 1446 var->green.offset = 8; 1447 var->blue.offset = 0; 1448 var->red.length = 8; 1449 var->green.length = 8; 1450 var->blue.length = 8; 1451 var->transp.offset = 24; 1452 var->transp.length = 8; 1453 break; 1454 default: 1455 break; 1456 } 1457 } 1458 1459 /** 1460 * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var 1461 * @var: screeninfo to check 1462 * @info: fbdev registered by the helper 1463 */ 1464 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 1465 struct fb_info *info) 1466 { 1467 struct drm_fb_helper *fb_helper = info->par; 1468 struct drm_framebuffer *fb = fb_helper->fb; 1469 1470 if (in_dbg_master()) 1471 return -EINVAL; 1472 1473 if (var->pixclock != 0) { 1474 DRM_DEBUG("fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n"); 1475 var->pixclock = 0; 1476 } 1477 1478 if ((drm_format_info_block_width(fb->format, 0) > 1) || 1479 (drm_format_info_block_height(fb->format, 0) > 1)) 1480 return -EINVAL; 1481 1482 /* 1483 * Changes struct fb_var_screeninfo are currently not pushed back 1484 * to KMS, hence fail if different settings are requested. 1485 */ 1486 if (var->bits_per_pixel != fb->format->cpp[0] * 8 || 1487 var->xres > fb->width || var->yres > fb->height || 1488 var->xres_virtual > fb->width || var->yres_virtual > fb->height) { 1489 DRM_DEBUG("fb requested width/height/bpp can't fit in current fb " 1490 "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n", 1491 var->xres, var->yres, var->bits_per_pixel, 1492 var->xres_virtual, var->yres_virtual, 1493 fb->width, fb->height, fb->format->cpp[0] * 8); 1494 return -EINVAL; 1495 } 1496 1497 /* 1498 * Workaround for SDL 1.2, which is known to be setting all pixel format 1499 * fields values to zero in some cases. We treat this situation as a 1500 * kind of "use some reasonable autodetected values". 1501 */ 1502 if (!var->red.offset && !var->green.offset && 1503 !var->blue.offset && !var->transp.offset && 1504 !var->red.length && !var->green.length && 1505 !var->blue.length && !var->transp.length && 1506 !var->red.msb_right && !var->green.msb_right && 1507 !var->blue.msb_right && !var->transp.msb_right) { 1508 drm_fb_helper_fill_pixel_fmt(var, fb->format->depth); 1509 } 1510 1511 /* 1512 * drm fbdev emulation doesn't support changing the pixel format at all, 1513 * so reject all pixel format changing requests. 1514 */ 1515 if (!drm_fb_pixel_format_equal(var, &info->var)) { 1516 DRM_DEBUG("fbdev emulation doesn't support changing the pixel format\n"); 1517 return -EINVAL; 1518 } 1519 1520 return 0; 1521 } 1522 EXPORT_SYMBOL(drm_fb_helper_check_var); 1523 1524 /** 1525 * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par 1526 * @info: fbdev registered by the helper 1527 * 1528 * This will let fbcon do the mode init and is called at initialization time by 1529 * the fbdev core when registering the driver, and later on through the hotplug 1530 * callback. 1531 */ 1532 int drm_fb_helper_set_par(struct fb_info *info) 1533 { 1534 struct drm_fb_helper *fb_helper = info->par; 1535 struct fb_var_screeninfo *var = &info->var; 1536 1537 if (oops_in_progress) 1538 return -EBUSY; 1539 1540 if (var->pixclock != 0) { 1541 DRM_ERROR("PIXEL CLOCK SET\n"); 1542 return -EINVAL; 1543 } 1544 1545 drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper); 1546 1547 return 0; 1548 } 1549 EXPORT_SYMBOL(drm_fb_helper_set_par); 1550 1551 static void pan_set(struct drm_fb_helper *fb_helper, int x, int y) 1552 { 1553 struct drm_mode_set *mode_set; 1554 1555 mutex_lock(&fb_helper->client.modeset_mutex); 1556 drm_client_for_each_modeset(mode_set, &fb_helper->client) { 1557 mode_set->x = x; 1558 mode_set->y = y; 1559 } 1560 mutex_unlock(&fb_helper->client.modeset_mutex); 1561 } 1562 1563 static int pan_display_atomic(struct fb_var_screeninfo *var, 1564 struct fb_info *info) 1565 { 1566 struct drm_fb_helper *fb_helper = info->par; 1567 int ret; 1568 1569 pan_set(fb_helper, var->xoffset, var->yoffset); 1570 1571 ret = drm_client_modeset_commit_force(&fb_helper->client); 1572 if (!ret) { 1573 info->var.xoffset = var->xoffset; 1574 info->var.yoffset = var->yoffset; 1575 } else 1576 pan_set(fb_helper, info->var.xoffset, info->var.yoffset); 1577 1578 return ret; 1579 } 1580 1581 static int pan_display_legacy(struct fb_var_screeninfo *var, 1582 struct fb_info *info) 1583 { 1584 struct drm_fb_helper *fb_helper = info->par; 1585 struct drm_client_dev *client = &fb_helper->client; 1586 struct drm_mode_set *modeset; 1587 int ret = 0; 1588 1589 drm_modeset_lock_all(fb_helper->dev); 1590 mutex_lock(&client->modeset_mutex); 1591 drm_client_for_each_modeset(modeset, client) { 1592 modeset->x = var->xoffset; 1593 modeset->y = var->yoffset; 1594 1595 if (modeset->num_connectors) { 1596 ret = drm_mode_set_config_internal(modeset); 1597 if (!ret) { 1598 info->var.xoffset = var->xoffset; 1599 info->var.yoffset = var->yoffset; 1600 } 1601 } 1602 } 1603 mutex_unlock(&client->modeset_mutex); 1604 drm_modeset_unlock_all(fb_helper->dev); 1605 1606 return ret; 1607 } 1608 1609 /** 1610 * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display 1611 * @var: updated screen information 1612 * @info: fbdev registered by the helper 1613 */ 1614 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 1615 struct fb_info *info) 1616 { 1617 struct drm_fb_helper *fb_helper = info->par; 1618 struct drm_device *dev = fb_helper->dev; 1619 int ret; 1620 1621 if (oops_in_progress) 1622 return -EBUSY; 1623 1624 mutex_lock(&fb_helper->lock); 1625 if (!drm_master_internal_acquire(dev)) { 1626 ret = -EBUSY; 1627 goto unlock; 1628 } 1629 1630 if (drm_drv_uses_atomic_modeset(dev)) 1631 ret = pan_display_atomic(var, info); 1632 else 1633 ret = pan_display_legacy(var, info); 1634 1635 drm_master_internal_release(dev); 1636 unlock: 1637 mutex_unlock(&fb_helper->lock); 1638 1639 return ret; 1640 } 1641 EXPORT_SYMBOL(drm_fb_helper_pan_display); 1642 1643 /* 1644 * Allocates the backing storage and sets up the fbdev info structure through 1645 * the ->fb_probe callback. 1646 */ 1647 static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, 1648 int preferred_bpp) 1649 { 1650 struct drm_client_dev *client = &fb_helper->client; 1651 int ret = 0; 1652 int crtc_count = 0; 1653 int i; 1654 struct drm_fb_helper_surface_size sizes; 1655 struct drm_mode_set *mode_set; 1656 int best_depth = 0; 1657 1658 memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); 1659 sizes.surface_depth = 24; 1660 sizes.surface_bpp = 32; 1661 sizes.fb_width = (u32)-1; 1662 sizes.fb_height = (u32)-1; 1663 1664 /* 1665 * If driver picks 8 or 16 by default use that for both depth/bpp 1666 * to begin with 1667 */ 1668 if (preferred_bpp != sizes.surface_bpp) 1669 sizes.surface_depth = sizes.surface_bpp = preferred_bpp; 1670 1671 drm_fb_helper_for_each_connector(fb_helper, i) { 1672 struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; 1673 struct drm_cmdline_mode *cmdline_mode; 1674 1675 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1676 1677 if (cmdline_mode->bpp_specified) { 1678 switch (cmdline_mode->bpp) { 1679 case 8: 1680 sizes.surface_depth = sizes.surface_bpp = 8; 1681 break; 1682 case 15: 1683 sizes.surface_depth = 15; 1684 sizes.surface_bpp = 16; 1685 break; 1686 case 16: 1687 sizes.surface_depth = sizes.surface_bpp = 16; 1688 break; 1689 case 24: 1690 sizes.surface_depth = sizes.surface_bpp = 24; 1691 break; 1692 case 32: 1693 sizes.surface_depth = 24; 1694 sizes.surface_bpp = 32; 1695 break; 1696 } 1697 break; 1698 } 1699 } 1700 1701 /* 1702 * If we run into a situation where, for example, the primary plane 1703 * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth 1704 * 16) we need to scale down the depth of the sizes we request. 1705 */ 1706 mutex_lock(&client->modeset_mutex); 1707 drm_client_for_each_modeset(mode_set, client) { 1708 struct drm_crtc *crtc = mode_set->crtc; 1709 struct drm_plane *plane = crtc->primary; 1710 int j; 1711 1712 DRM_DEBUG("test CRTC %u primary plane\n", drm_crtc_index(crtc)); 1713 1714 for (j = 0; j < plane->format_count; j++) { 1715 const struct drm_format_info *fmt; 1716 1717 fmt = drm_format_info(plane->format_types[j]); 1718 1719 /* 1720 * Do not consider YUV or other complicated formats 1721 * for framebuffers. This means only legacy formats 1722 * are supported (fmt->depth is a legacy field) but 1723 * the framebuffer emulation can only deal with such 1724 * formats, specifically RGB/BGA formats. 1725 */ 1726 if (fmt->depth == 0) 1727 continue; 1728 1729 /* We found a perfect fit, great */ 1730 if (fmt->depth == sizes.surface_depth) { 1731 best_depth = fmt->depth; 1732 break; 1733 } 1734 1735 /* Skip depths above what we're looking for */ 1736 if (fmt->depth > sizes.surface_depth) 1737 continue; 1738 1739 /* Best depth found so far */ 1740 if (fmt->depth > best_depth) 1741 best_depth = fmt->depth; 1742 } 1743 } 1744 if (sizes.surface_depth != best_depth && best_depth) { 1745 DRM_INFO("requested bpp %d, scaled depth down to %d", 1746 sizes.surface_bpp, best_depth); 1747 sizes.surface_depth = best_depth; 1748 } 1749 1750 /* first up get a count of crtcs now in use and new min/maxes width/heights */ 1751 crtc_count = 0; 1752 drm_client_for_each_modeset(mode_set, client) { 1753 struct drm_display_mode *desired_mode; 1754 int x, y, j; 1755 /* in case of tile group, are we the last tile vert or horiz? 1756 * If no tile group you are always the last one both vertically 1757 * and horizontally 1758 */ 1759 bool lastv = true, lasth = true; 1760 1761 desired_mode = mode_set->mode; 1762 1763 if (!desired_mode) 1764 continue; 1765 1766 crtc_count++; 1767 1768 x = mode_set->x; 1769 y = mode_set->y; 1770 1771 sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width); 1772 sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height); 1773 1774 for (j = 0; j < mode_set->num_connectors; j++) { 1775 struct drm_connector *connector = mode_set->connectors[j]; 1776 1777 if (connector->has_tile) { 1778 lasth = (connector->tile_h_loc == (connector->num_h_tile - 1)); 1779 lastv = (connector->tile_v_loc == (connector->num_v_tile - 1)); 1780 /* cloning to multiple tiles is just crazy-talk, so: */ 1781 break; 1782 } 1783 } 1784 1785 if (lasth) 1786 sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width); 1787 if (lastv) 1788 sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height); 1789 } 1790 mutex_unlock(&client->modeset_mutex); 1791 1792 if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { 1793 DRM_INFO("Cannot find any crtc or sizes\n"); 1794 1795 /* First time: disable all crtc's.. */ 1796 if (!fb_helper->deferred_setup) 1797 drm_client_modeset_commit(client); 1798 return -EAGAIN; 1799 } 1800 1801 /* Handle our overallocation */ 1802 sizes.surface_height *= drm_fbdev_overalloc; 1803 sizes.surface_height /= 100; 1804 1805 /* push down into drivers */ 1806 ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes); 1807 if (ret < 0) 1808 return ret; 1809 1810 strcpy(fb_helper->fb->comm, "[fbcon]"); 1811 return 0; 1812 } 1813 1814 static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 1815 uint32_t depth) 1816 { 1817 info->fix.type = FB_TYPE_PACKED_PIXELS; 1818 info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : 1819 FB_VISUAL_TRUECOLOR; 1820 info->fix.mmio_start = 0; 1821 info->fix.mmio_len = 0; 1822 info->fix.type_aux = 0; 1823 info->fix.xpanstep = 1; /* doing it in hw */ 1824 info->fix.ypanstep = 1; /* doing it in hw */ 1825 info->fix.ywrapstep = 0; 1826 info->fix.accel = FB_ACCEL_NONE; 1827 1828 info->fix.line_length = pitch; 1829 } 1830 1831 static void drm_fb_helper_fill_var(struct fb_info *info, 1832 struct drm_fb_helper *fb_helper, 1833 uint32_t fb_width, uint32_t fb_height) 1834 { 1835 struct drm_framebuffer *fb = fb_helper->fb; 1836 1837 WARN_ON((drm_format_info_block_width(fb->format, 0) > 1) || 1838 (drm_format_info_block_height(fb->format, 0) > 1)); 1839 info->pseudo_palette = fb_helper->pseudo_palette; 1840 info->var.xres_virtual = fb->width; 1841 info->var.yres_virtual = fb->height; 1842 info->var.bits_per_pixel = fb->format->cpp[0] * 8; 1843 info->var.accel_flags = FB_ACCELF_TEXT; 1844 info->var.xoffset = 0; 1845 info->var.yoffset = 0; 1846 info->var.activate = FB_ACTIVATE_NOW; 1847 1848 drm_fb_helper_fill_pixel_fmt(&info->var, fb->format->depth); 1849 1850 info->var.xres = fb_width; 1851 info->var.yres = fb_height; 1852 } 1853 1854 /** 1855 * drm_fb_helper_fill_info - initializes fbdev information 1856 * @info: fbdev instance to set up 1857 * @fb_helper: fb helper instance to use as template 1858 * @sizes: describes fbdev size and scanout surface size 1859 * 1860 * Sets up the variable and fixed fbdev metainformation from the given fb helper 1861 * instance and the drm framebuffer allocated in &drm_fb_helper.fb. 1862 * 1863 * Drivers should call this (or their equivalent setup code) from their 1864 * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev 1865 * backing storage framebuffer. 1866 */ 1867 void drm_fb_helper_fill_info(struct fb_info *info, 1868 struct drm_fb_helper *fb_helper, 1869 struct drm_fb_helper_surface_size *sizes) 1870 { 1871 struct drm_framebuffer *fb = fb_helper->fb; 1872 1873 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth); 1874 drm_fb_helper_fill_var(info, fb_helper, 1875 sizes->fb_width, sizes->fb_height); 1876 1877 info->par = fb_helper; 1878 snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb", 1879 fb_helper->dev->driver->name); 1880 1881 } 1882 EXPORT_SYMBOL(drm_fb_helper_fill_info); 1883 1884 static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, 1885 uint32_t maxX, 1886 uint32_t maxY) 1887 { 1888 struct drm_connector *connector; 1889 int i, count = 0; 1890 1891 drm_fb_helper_for_each_connector(fb_helper, i) { 1892 connector = fb_helper->connector_info[i]->connector; 1893 count += connector->funcs->fill_modes(connector, maxX, maxY); 1894 } 1895 1896 return count; 1897 } 1898 1899 struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) 1900 { 1901 struct drm_display_mode *mode; 1902 1903 list_for_each_entry(mode, &fb_connector->connector->modes, head) { 1904 if (mode->hdisplay > width || 1905 mode->vdisplay > height) 1906 continue; 1907 if (mode->type & DRM_MODE_TYPE_PREFERRED) 1908 return mode; 1909 } 1910 return NULL; 1911 } 1912 EXPORT_SYMBOL(drm_has_preferred_mode); 1913 1914 static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) 1915 { 1916 return fb_connector->connector->cmdline_mode.specified; 1917 } 1918 1919 struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn) 1920 { 1921 struct drm_cmdline_mode *cmdline_mode; 1922 struct drm_display_mode *mode; 1923 bool prefer_non_interlace; 1924 1925 cmdline_mode = &fb_helper_conn->connector->cmdline_mode; 1926 if (cmdline_mode->specified == false) 1927 return NULL; 1928 1929 /* attempt to find a matching mode in the list of modes 1930 * we have gotten so far, if not add a CVT mode that conforms 1931 */ 1932 if (cmdline_mode->rb || cmdline_mode->margins) 1933 goto create_mode; 1934 1935 prefer_non_interlace = !cmdline_mode->interlace; 1936 again: 1937 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 1938 /* check width/height */ 1939 if (mode->hdisplay != cmdline_mode->xres || 1940 mode->vdisplay != cmdline_mode->yres) 1941 continue; 1942 1943 if (cmdline_mode->refresh_specified) { 1944 if (mode->vrefresh != cmdline_mode->refresh) 1945 continue; 1946 } 1947 1948 if (cmdline_mode->interlace) { 1949 if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) 1950 continue; 1951 } else if (prefer_non_interlace) { 1952 if (mode->flags & DRM_MODE_FLAG_INTERLACE) 1953 continue; 1954 } 1955 return mode; 1956 } 1957 1958 if (prefer_non_interlace) { 1959 prefer_non_interlace = false; 1960 goto again; 1961 } 1962 1963 create_mode: 1964 mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev, 1965 cmdline_mode); 1966 list_add(&mode->head, &fb_helper_conn->connector->modes); 1967 return mode; 1968 } 1969 EXPORT_SYMBOL(drm_pick_cmdline_mode); 1970 1971 static bool drm_connector_enabled(struct drm_connector *connector, bool strict) 1972 { 1973 bool enable; 1974 1975 if (connector->display_info.non_desktop) 1976 return false; 1977 1978 if (strict) 1979 enable = connector->status == connector_status_connected; 1980 else 1981 enable = connector->status != connector_status_disconnected; 1982 1983 return enable; 1984 } 1985 1986 static void drm_enable_connectors(struct drm_fb_helper *fb_helper, 1987 bool *enabled) 1988 { 1989 bool any_enabled = false; 1990 struct drm_connector *connector; 1991 int i = 0; 1992 1993 drm_fb_helper_for_each_connector(fb_helper, i) { 1994 connector = fb_helper->connector_info[i]->connector; 1995 enabled[i] = drm_connector_enabled(connector, true); 1996 DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, 1997 connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no"); 1998 1999 any_enabled |= enabled[i]; 2000 } 2001 2002 if (any_enabled) 2003 return; 2004 2005 drm_fb_helper_for_each_connector(fb_helper, i) { 2006 connector = fb_helper->connector_info[i]->connector; 2007 enabled[i] = drm_connector_enabled(connector, false); 2008 } 2009 } 2010 2011 static bool drm_target_cloned(struct drm_fb_helper *fb_helper, 2012 struct drm_display_mode **modes, 2013 struct drm_fb_offset *offsets, 2014 bool *enabled, int width, int height) 2015 { 2016 int count, i, j; 2017 bool can_clone = false; 2018 struct drm_fb_helper_connector *fb_helper_conn; 2019 struct drm_display_mode *dmt_mode, *mode; 2020 2021 /* only contemplate cloning in the single crtc case */ 2022 if (fb_helper->dev->mode_config.num_crtc > 1) 2023 return false; 2024 2025 count = 0; 2026 drm_fb_helper_for_each_connector(fb_helper, i) { 2027 if (enabled[i]) 2028 count++; 2029 } 2030 2031 /* only contemplate cloning if more than one connector is enabled */ 2032 if (count <= 1) 2033 return false; 2034 2035 /* check the command line or if nothing common pick 1024x768 */ 2036 can_clone = true; 2037 drm_fb_helper_for_each_connector(fb_helper, i) { 2038 if (!enabled[i]) 2039 continue; 2040 fb_helper_conn = fb_helper->connector_info[i]; 2041 modes[i] = drm_pick_cmdline_mode(fb_helper_conn); 2042 if (!modes[i]) { 2043 can_clone = false; 2044 break; 2045 } 2046 for (j = 0; j < i; j++) { 2047 if (!enabled[j]) 2048 continue; 2049 if (!drm_mode_match(modes[j], modes[i], 2050 DRM_MODE_MATCH_TIMINGS | 2051 DRM_MODE_MATCH_CLOCK | 2052 DRM_MODE_MATCH_FLAGS | 2053 DRM_MODE_MATCH_3D_FLAGS)) 2054 can_clone = false; 2055 } 2056 } 2057 2058 if (can_clone) { 2059 DRM_DEBUG_KMS("can clone using command line\n"); 2060 return true; 2061 } 2062 2063 /* try and find a 1024x768 mode on each connector */ 2064 can_clone = true; 2065 dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false); 2066 2067 drm_fb_helper_for_each_connector(fb_helper, i) { 2068 if (!enabled[i]) 2069 continue; 2070 2071 fb_helper_conn = fb_helper->connector_info[i]; 2072 list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { 2073 if (drm_mode_match(mode, dmt_mode, 2074 DRM_MODE_MATCH_TIMINGS | 2075 DRM_MODE_MATCH_CLOCK | 2076 DRM_MODE_MATCH_FLAGS | 2077 DRM_MODE_MATCH_3D_FLAGS)) 2078 modes[i] = mode; 2079 } 2080 if (!modes[i]) 2081 can_clone = false; 2082 } 2083 2084 if (can_clone) { 2085 DRM_DEBUG_KMS("can clone using 1024x768\n"); 2086 return true; 2087 } 2088 DRM_INFO("kms: can't enable cloning when we probably wanted to.\n"); 2089 return false; 2090 } 2091 2092 static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper, 2093 struct drm_display_mode **modes, 2094 struct drm_fb_offset *offsets, 2095 int idx, 2096 int h_idx, int v_idx) 2097 { 2098 struct drm_fb_helper_connector *fb_helper_conn; 2099 int i; 2100 int hoffset = 0, voffset = 0; 2101 2102 drm_fb_helper_for_each_connector(fb_helper, i) { 2103 fb_helper_conn = fb_helper->connector_info[i]; 2104 if (!fb_helper_conn->connector->has_tile) 2105 continue; 2106 2107 if (!modes[i] && (h_idx || v_idx)) { 2108 DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i, 2109 fb_helper_conn->connector->base.id); 2110 continue; 2111 } 2112 if (fb_helper_conn->connector->tile_h_loc < h_idx) 2113 hoffset += modes[i]->hdisplay; 2114 2115 if (fb_helper_conn->connector->tile_v_loc < v_idx) 2116 voffset += modes[i]->vdisplay; 2117 } 2118 offsets[idx].x = hoffset; 2119 offsets[idx].y = voffset; 2120 DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx); 2121 return 0; 2122 } 2123 2124 static bool drm_target_preferred(struct drm_fb_helper *fb_helper, 2125 struct drm_display_mode **modes, 2126 struct drm_fb_offset *offsets, 2127 bool *enabled, int width, int height) 2128 { 2129 struct drm_fb_helper_connector *fb_helper_conn; 2130 const u64 mask = BIT_ULL(fb_helper->connector_count) - 1; 2131 u64 conn_configured = 0; 2132 int tile_pass = 0; 2133 int i; 2134 2135 retry: 2136 drm_fb_helper_for_each_connector(fb_helper, i) { 2137 fb_helper_conn = fb_helper->connector_info[i]; 2138 2139 if (conn_configured & BIT_ULL(i)) 2140 continue; 2141 2142 if (enabled[i] == false) { 2143 conn_configured |= BIT_ULL(i); 2144 continue; 2145 } 2146 2147 /* first pass over all the untiled connectors */ 2148 if (tile_pass == 0 && fb_helper_conn->connector->has_tile) 2149 continue; 2150 2151 if (tile_pass == 1) { 2152 if (fb_helper_conn->connector->tile_h_loc != 0 || 2153 fb_helper_conn->connector->tile_v_loc != 0) 2154 continue; 2155 2156 } else { 2157 if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 && 2158 fb_helper_conn->connector->tile_v_loc != tile_pass - 1) 2159 /* if this tile_pass doesn't cover any of the tiles - keep going */ 2160 continue; 2161 2162 /* 2163 * find the tile offsets for this pass - need to find 2164 * all tiles left and above 2165 */ 2166 drm_get_tile_offsets(fb_helper, modes, offsets, 2167 i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc); 2168 } 2169 DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", 2170 fb_helper_conn->connector->base.id); 2171 2172 /* got for command line mode first */ 2173 modes[i] = drm_pick_cmdline_mode(fb_helper_conn); 2174 if (!modes[i]) { 2175 DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n", 2176 fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0); 2177 modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); 2178 } 2179 /* No preferred modes, pick one off the list */ 2180 if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { 2181 list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) 2182 break; 2183 } 2184 DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : 2185 "none"); 2186 conn_configured |= BIT_ULL(i); 2187 } 2188 2189 if ((conn_configured & mask) != mask) { 2190 tile_pass++; 2191 goto retry; 2192 } 2193 return true; 2194 } 2195 2196 static bool connector_has_possible_crtc(struct drm_connector *connector, 2197 struct drm_crtc *crtc) 2198 { 2199 struct drm_encoder *encoder; 2200 int i; 2201 2202 drm_connector_for_each_possible_encoder(connector, encoder, i) { 2203 if (encoder->possible_crtcs & drm_crtc_mask(crtc)) 2204 return true; 2205 } 2206 2207 return false; 2208 } 2209 2210 static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, 2211 struct drm_crtc **best_crtcs, 2212 struct drm_display_mode **modes, 2213 int n, int width, int height) 2214 { 2215 struct drm_client_dev *client = &fb_helper->client; 2216 struct drm_connector *connector; 2217 int my_score, best_score, score; 2218 struct drm_crtc **crtcs, *crtc; 2219 struct drm_mode_set *modeset; 2220 struct drm_fb_helper_connector *fb_helper_conn; 2221 int o; 2222 2223 if (n == fb_helper->connector_count) 2224 return 0; 2225 2226 fb_helper_conn = fb_helper->connector_info[n]; 2227 connector = fb_helper_conn->connector; 2228 2229 best_crtcs[n] = NULL; 2230 best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); 2231 if (modes[n] == NULL) 2232 return best_score; 2233 2234 crtcs = kcalloc(fb_helper->connector_count, sizeof(*crtcs), GFP_KERNEL); 2235 if (!crtcs) 2236 return best_score; 2237 2238 my_score = 1; 2239 if (connector->status == connector_status_connected) 2240 my_score++; 2241 if (drm_has_cmdline_mode(fb_helper_conn)) 2242 my_score++; 2243 if (drm_has_preferred_mode(fb_helper_conn, width, height)) 2244 my_score++; 2245 2246 /* 2247 * select a crtc for this connector and then attempt to configure 2248 * remaining connectors 2249 */ 2250 drm_client_for_each_modeset(modeset, client) { 2251 crtc = modeset->crtc; 2252 2253 if (!connector_has_possible_crtc(connector, crtc)) 2254 continue; 2255 2256 for (o = 0; o < n; o++) 2257 if (best_crtcs[o] == crtc) 2258 break; 2259 2260 if (o < n) { 2261 /* ignore cloning unless only a single crtc */ 2262 if (fb_helper->dev->mode_config.num_crtc > 1) 2263 continue; 2264 2265 if (!drm_mode_equal(modes[o], modes[n])) 2266 continue; 2267 } 2268 2269 crtcs[n] = crtc; 2270 memcpy(crtcs, best_crtcs, n * sizeof(*crtcs)); 2271 score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, 2272 width, height); 2273 if (score > best_score) { 2274 best_score = score; 2275 memcpy(best_crtcs, crtcs, 2276 fb_helper->connector_count * sizeof(*crtcs)); 2277 } 2278 } 2279 2280 kfree(crtcs); 2281 return best_score; 2282 } 2283 2284 /* Try to read the BIOS display configuration and use it for the initial config */ 2285 static bool drm_fb_helper_firmware_config(struct drm_fb_helper *fb_helper, 2286 struct drm_crtc **crtcs, 2287 struct drm_display_mode **modes, 2288 struct drm_fb_offset *offsets, 2289 bool *enabled, int width, int height) 2290 { 2291 struct drm_device *dev = fb_helper->dev; 2292 unsigned int count = min(fb_helper->connector_count, BITS_PER_LONG); 2293 unsigned long conn_configured, conn_seq, mask; 2294 int i, j; 2295 bool *save_enabled; 2296 bool fallback = true, ret = true; 2297 int num_connectors_enabled = 0; 2298 int num_connectors_detected = 0; 2299 struct drm_modeset_acquire_ctx ctx; 2300 2301 if (!drm_drv_uses_atomic_modeset(dev)) 2302 return false; 2303 2304 save_enabled = kcalloc(count, sizeof(bool), GFP_KERNEL); 2305 if (!save_enabled) 2306 return false; 2307 2308 drm_modeset_acquire_init(&ctx, 0); 2309 2310 while (drm_modeset_lock_all_ctx(dev, &ctx) != 0) 2311 drm_modeset_backoff(&ctx); 2312 2313 memcpy(save_enabled, enabled, count); 2314 mask = GENMASK(count - 1, 0); 2315 conn_configured = 0; 2316 retry: 2317 conn_seq = conn_configured; 2318 for (i = 0; i < count; i++) { 2319 struct drm_fb_helper_connector *fb_conn; 2320 struct drm_connector *connector; 2321 struct drm_encoder *encoder; 2322 struct drm_crtc *new_crtc; 2323 2324 fb_conn = fb_helper->connector_info[i]; 2325 connector = fb_conn->connector; 2326 2327 if (conn_configured & BIT(i)) 2328 continue; 2329 2330 if (conn_seq == 0 && !connector->has_tile) 2331 continue; 2332 2333 if (connector->status == connector_status_connected) 2334 num_connectors_detected++; 2335 2336 if (!enabled[i]) { 2337 DRM_DEBUG_KMS("connector %s not enabled, skipping\n", 2338 connector->name); 2339 conn_configured |= BIT(i); 2340 continue; 2341 } 2342 2343 if (connector->force == DRM_FORCE_OFF) { 2344 DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n", 2345 connector->name); 2346 enabled[i] = false; 2347 continue; 2348 } 2349 2350 encoder = connector->state->best_encoder; 2351 if (!encoder || WARN_ON(!connector->state->crtc)) { 2352 if (connector->force > DRM_FORCE_OFF) 2353 goto bail; 2354 2355 DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n", 2356 connector->name); 2357 enabled[i] = false; 2358 conn_configured |= BIT(i); 2359 continue; 2360 } 2361 2362 num_connectors_enabled++; 2363 2364 new_crtc = connector->state->crtc; 2365 2366 /* 2367 * Make sure we're not trying to drive multiple connectors 2368 * with a single CRTC, since our cloning support may not 2369 * match the BIOS. 2370 */ 2371 for (j = 0; j < count; j++) { 2372 if (crtcs[j] == new_crtc) { 2373 DRM_DEBUG_KMS("fallback: cloned configuration\n"); 2374 goto bail; 2375 } 2376 } 2377 2378 DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n", 2379 connector->name); 2380 2381 /* go for command line mode first */ 2382 modes[i] = drm_pick_cmdline_mode(fb_conn); 2383 2384 /* try for preferred next */ 2385 if (!modes[i]) { 2386 DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n", 2387 connector->name, connector->has_tile); 2388 modes[i] = drm_has_preferred_mode(fb_conn, width, 2389 height); 2390 } 2391 2392 /* No preferred mode marked by the EDID? Are there any modes? */ 2393 if (!modes[i] && !list_empty(&connector->modes)) { 2394 DRM_DEBUG_KMS("using first mode listed on connector %s\n", 2395 connector->name); 2396 modes[i] = list_first_entry(&connector->modes, 2397 struct drm_display_mode, 2398 head); 2399 } 2400 2401 /* last resort: use current mode */ 2402 if (!modes[i]) { 2403 /* 2404 * IMPORTANT: We want to use the adjusted mode (i.e. 2405 * after the panel fitter upscaling) as the initial 2406 * config, not the input mode, which is what crtc->mode 2407 * usually contains. But since our current 2408 * code puts a mode derived from the post-pfit timings 2409 * into crtc->mode this works out correctly. 2410 * 2411 * This is crtc->mode and not crtc->state->mode for the 2412 * fastboot check to work correctly. 2413 */ 2414 DRM_DEBUG_KMS("looking for current mode on connector %s\n", 2415 connector->name); 2416 modes[i] = &connector->state->crtc->mode; 2417 } 2418 crtcs[i] = new_crtc; 2419 2420 DRM_DEBUG_KMS("connector %s on [CRTC:%d:%s]: %dx%d%s\n", 2421 connector->name, 2422 connector->state->crtc->base.id, 2423 connector->state->crtc->name, 2424 modes[i]->hdisplay, modes[i]->vdisplay, 2425 modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" : ""); 2426 2427 fallback = false; 2428 conn_configured |= BIT(i); 2429 } 2430 2431 if ((conn_configured & mask) != mask && conn_configured != conn_seq) 2432 goto retry; 2433 2434 /* 2435 * If the BIOS didn't enable everything it could, fall back to have the 2436 * same user experiencing of lighting up as much as possible like the 2437 * fbdev helper library. 2438 */ 2439 if (num_connectors_enabled != num_connectors_detected && 2440 num_connectors_enabled < dev->mode_config.num_crtc) { 2441 DRM_DEBUG_KMS("fallback: Not all outputs enabled\n"); 2442 DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled, 2443 num_connectors_detected); 2444 fallback = true; 2445 } 2446 2447 if (fallback) { 2448 bail: 2449 DRM_DEBUG_KMS("Not using firmware configuration\n"); 2450 memcpy(enabled, save_enabled, count); 2451 ret = false; 2452 } 2453 2454 drm_modeset_drop_locks(&ctx); 2455 drm_modeset_acquire_fini(&ctx); 2456 2457 kfree(save_enabled); 2458 return ret; 2459 } 2460 2461 static void drm_setup_crtcs(struct drm_fb_helper *fb_helper, 2462 u32 width, u32 height) 2463 { 2464 struct drm_client_dev *client = &fb_helper->client; 2465 struct drm_device *dev = fb_helper->dev; 2466 struct drm_display_mode **modes; 2467 struct drm_fb_offset *offsets; 2468 struct drm_crtc **crtcs; 2469 bool *enabled; 2470 int i; 2471 2472 DRM_DEBUG_KMS("\n"); 2473 /* prevent concurrent modification of connector_count by hotplug */ 2474 lockdep_assert_held(&fb_helper->lock); 2475 2476 crtcs = kcalloc(fb_helper->connector_count, sizeof(*crtcs), GFP_KERNEL); 2477 modes = kcalloc(fb_helper->connector_count, 2478 sizeof(struct drm_display_mode *), GFP_KERNEL); 2479 offsets = kcalloc(fb_helper->connector_count, 2480 sizeof(struct drm_fb_offset), GFP_KERNEL); 2481 enabled = kcalloc(fb_helper->connector_count, 2482 sizeof(bool), GFP_KERNEL); 2483 if (!crtcs || !modes || !enabled || !offsets) { 2484 DRM_ERROR("Memory allocation failed\n"); 2485 goto out; 2486 } 2487 2488 mutex_lock(&client->modeset_mutex); 2489 2490 mutex_lock(&fb_helper->dev->mode_config.mutex); 2491 if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0) 2492 DRM_DEBUG_KMS("No connectors reported connected with modes\n"); 2493 drm_enable_connectors(fb_helper, enabled); 2494 2495 if (!drm_fb_helper_firmware_config(fb_helper, crtcs, modes, offsets, 2496 enabled, width, height)) { 2497 memset(modes, 0, fb_helper->connector_count*sizeof(modes[0])); 2498 memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0])); 2499 memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0])); 2500 2501 if (!drm_target_cloned(fb_helper, modes, offsets, 2502 enabled, width, height) && 2503 !drm_target_preferred(fb_helper, modes, offsets, 2504 enabled, width, height)) 2505 DRM_ERROR("Unable to find initial modes\n"); 2506 2507 DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", 2508 width, height); 2509 2510 drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); 2511 } 2512 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2513 2514 drm_client_modeset_release(client); 2515 2516 drm_fb_helper_for_each_connector(fb_helper, i) { 2517 struct drm_display_mode *mode = modes[i]; 2518 struct drm_crtc *crtc = crtcs[i]; 2519 struct drm_fb_offset *offset = &offsets[i]; 2520 2521 if (mode && crtc) { 2522 struct drm_mode_set *modeset = drm_client_find_modeset(client, crtc); 2523 struct drm_connector *connector = 2524 fb_helper->connector_info[i]->connector; 2525 2526 DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n", 2527 mode->name, crtc->base.id, offset->x, offset->y); 2528 2529 if (WARN_ON_ONCE(modeset->num_connectors == DRM_CLIENT_MAX_CLONED_CONNECTORS || 2530 (dev->mode_config.num_crtc > 1 && modeset->num_connectors == 1))) 2531 break; 2532 2533 modeset->mode = drm_mode_duplicate(dev, mode); 2534 drm_connector_get(connector); 2535 modeset->connectors[modeset->num_connectors++] = connector; 2536 modeset->x = offset->x; 2537 modeset->y = offset->y; 2538 } 2539 } 2540 2541 mutex_unlock(&client->modeset_mutex); 2542 out: 2543 kfree(crtcs); 2544 kfree(modes); 2545 kfree(offsets); 2546 kfree(enabled); 2547 } 2548 2549 /* 2550 * This is a continuation of drm_setup_crtcs() that sets up anything related 2551 * to the framebuffer. During initialization, drm_setup_crtcs() is called before 2552 * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev). 2553 * So, any setup that touches those fields needs to be done here instead of in 2554 * drm_setup_crtcs(). 2555 */ 2556 static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper) 2557 { 2558 struct drm_client_dev *client = &fb_helper->client; 2559 struct fb_info *info = fb_helper->fbdev; 2560 unsigned int rotation, sw_rotations = 0; 2561 struct drm_mode_set *modeset; 2562 int i; 2563 2564 mutex_lock(&client->modeset_mutex); 2565 drm_client_for_each_modeset(modeset, client) { 2566 if (!modeset->num_connectors) 2567 continue; 2568 2569 modeset->fb = fb_helper->fb; 2570 2571 if (drm_client_panel_rotation(modeset, &rotation)) 2572 /* Rotating in hardware, fbcon should not rotate */ 2573 sw_rotations |= DRM_MODE_ROTATE_0; 2574 else 2575 sw_rotations |= rotation; 2576 } 2577 mutex_unlock(&client->modeset_mutex); 2578 2579 mutex_lock(&fb_helper->dev->mode_config.mutex); 2580 drm_fb_helper_for_each_connector(fb_helper, i) { 2581 struct drm_connector *connector = 2582 fb_helper->connector_info[i]->connector; 2583 2584 /* use first connected connector for the physical dimensions */ 2585 if (connector->status == connector_status_connected) { 2586 info->var.width = connector->display_info.width_mm; 2587 info->var.height = connector->display_info.height_mm; 2588 break; 2589 } 2590 } 2591 mutex_unlock(&fb_helper->dev->mode_config.mutex); 2592 2593 switch (sw_rotations) { 2594 case DRM_MODE_ROTATE_0: 2595 info->fbcon_rotate_hint = FB_ROTATE_UR; 2596 break; 2597 case DRM_MODE_ROTATE_90: 2598 info->fbcon_rotate_hint = FB_ROTATE_CCW; 2599 break; 2600 case DRM_MODE_ROTATE_180: 2601 info->fbcon_rotate_hint = FB_ROTATE_UD; 2602 break; 2603 case DRM_MODE_ROTATE_270: 2604 info->fbcon_rotate_hint = FB_ROTATE_CW; 2605 break; 2606 default: 2607 /* 2608 * Multiple bits are set / multiple rotations requested 2609 * fbcon cannot handle separate rotation settings per 2610 * output, so fallback to unrotated. 2611 */ 2612 info->fbcon_rotate_hint = FB_ROTATE_UR; 2613 } 2614 } 2615 2616 /* Note: Drops fb_helper->lock before returning. */ 2617 static int 2618 __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper, 2619 int bpp_sel) 2620 { 2621 struct drm_device *dev = fb_helper->dev; 2622 struct fb_info *info; 2623 unsigned int width, height; 2624 int ret; 2625 2626 width = dev->mode_config.max_width; 2627 height = dev->mode_config.max_height; 2628 2629 drm_setup_crtcs(fb_helper, width, height); 2630 ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel); 2631 if (ret < 0) { 2632 if (ret == -EAGAIN) { 2633 fb_helper->preferred_bpp = bpp_sel; 2634 fb_helper->deferred_setup = true; 2635 ret = 0; 2636 } 2637 mutex_unlock(&fb_helper->lock); 2638 2639 return ret; 2640 } 2641 drm_setup_crtcs_fb(fb_helper); 2642 2643 fb_helper->deferred_setup = false; 2644 2645 info = fb_helper->fbdev; 2646 info->var.pixclock = 0; 2647 /* Shamelessly allow physical address leaking to userspace */ 2648 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 2649 if (!drm_leak_fbdev_smem) 2650 #endif 2651 /* don't leak any physical addresses to userspace */ 2652 info->flags |= FBINFO_HIDE_SMEM_START; 2653 2654 /* Need to drop locks to avoid recursive deadlock in 2655 * register_framebuffer. This is ok because the only thing left to do is 2656 * register the fbdev emulation instance in kernel_fb_helper_list. */ 2657 mutex_unlock(&fb_helper->lock); 2658 2659 ret = register_framebuffer(info); 2660 if (ret < 0) 2661 return ret; 2662 2663 dev_info(dev->dev, "fb%d: %s frame buffer device\n", 2664 info->node, info->fix.id); 2665 2666 mutex_lock(&kernel_fb_helper_lock); 2667 if (list_empty(&kernel_fb_helper_list)) 2668 register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); 2669 2670 list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); 2671 mutex_unlock(&kernel_fb_helper_lock); 2672 2673 return 0; 2674 } 2675 2676 /** 2677 * drm_fb_helper_initial_config - setup a sane initial connector configuration 2678 * @fb_helper: fb_helper device struct 2679 * @bpp_sel: bpp value to use for the framebuffer configuration 2680 * 2681 * Scans the CRTCs and connectors and tries to put together an initial setup. 2682 * At the moment, this is a cloned configuration across all heads with 2683 * a new framebuffer object as the backing store. 2684 * 2685 * Note that this also registers the fbdev and so allows userspace to call into 2686 * the driver through the fbdev interfaces. 2687 * 2688 * This function will call down into the &drm_fb_helper_funcs.fb_probe callback 2689 * to let the driver allocate and initialize the fbdev info structure and the 2690 * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided 2691 * as a helper to setup simple default values for the fbdev info structure. 2692 * 2693 * HANG DEBUGGING: 2694 * 2695 * When you have fbcon support built-in or already loaded, this function will do 2696 * a full modeset to setup the fbdev console. Due to locking misdesign in the 2697 * VT/fbdev subsystem that entire modeset sequence has to be done while holding 2698 * console_lock. Until console_unlock is called no dmesg lines will be sent out 2699 * to consoles, not even serial console. This means when your driver crashes, 2700 * you will see absolutely nothing else but a system stuck in this function, 2701 * with no further output. Any kind of printk() you place within your own driver 2702 * or in the drm core modeset code will also never show up. 2703 * 2704 * Standard debug practice is to run the fbcon setup without taking the 2705 * console_lock as a hack, to be able to see backtraces and crashes on the 2706 * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel 2707 * cmdline option. 2708 * 2709 * The other option is to just disable fbdev emulation since very likely the 2710 * first modeset from userspace will crash in the same way, and is even easier 2711 * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0 2712 * kernel cmdline option. 2713 * 2714 * RETURNS: 2715 * Zero if everything went ok, nonzero otherwise. 2716 */ 2717 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel) 2718 { 2719 int ret; 2720 2721 if (!drm_fbdev_emulation) 2722 return 0; 2723 2724 mutex_lock(&fb_helper->lock); 2725 ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel); 2726 2727 return ret; 2728 } 2729 EXPORT_SYMBOL(drm_fb_helper_initial_config); 2730 2731 /** 2732 * drm_fb_helper_hotplug_event - respond to a hotplug notification by 2733 * probing all the outputs attached to the fb 2734 * @fb_helper: driver-allocated fbdev helper, can be NULL 2735 * 2736 * Scan the connectors attached to the fb_helper and try to put together a 2737 * setup after notification of a change in output configuration. 2738 * 2739 * Called at runtime, takes the mode config locks to be able to check/change the 2740 * modeset configuration. Must be run from process context (which usually means 2741 * either the output polling work or a work item launched from the driver's 2742 * hotplug interrupt). 2743 * 2744 * Note that drivers may call this even before calling 2745 * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows 2746 * for a race-free fbcon setup and will make sure that the fbdev emulation will 2747 * not miss any hotplug events. 2748 * 2749 * RETURNS: 2750 * 0 on success and a non-zero error code otherwise. 2751 */ 2752 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 2753 { 2754 int err = 0; 2755 2756 if (!drm_fbdev_emulation || !fb_helper) 2757 return 0; 2758 2759 mutex_lock(&fb_helper->lock); 2760 if (fb_helper->deferred_setup) { 2761 err = __drm_fb_helper_initial_config_and_unlock(fb_helper, 2762 fb_helper->preferred_bpp); 2763 return err; 2764 } 2765 2766 if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) { 2767 fb_helper->delayed_hotplug = true; 2768 mutex_unlock(&fb_helper->lock); 2769 return err; 2770 } 2771 2772 drm_master_internal_release(fb_helper->dev); 2773 2774 DRM_DEBUG_KMS("\n"); 2775 2776 drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height); 2777 drm_setup_crtcs_fb(fb_helper); 2778 mutex_unlock(&fb_helper->lock); 2779 2780 drm_fb_helper_set_par(fb_helper->fbdev); 2781 2782 return 0; 2783 } 2784 EXPORT_SYMBOL(drm_fb_helper_hotplug_event); 2785 2786 /** 2787 * drm_fb_helper_fbdev_setup() - Setup fbdev emulation 2788 * @dev: DRM device 2789 * @fb_helper: fbdev helper structure to set up 2790 * @funcs: fbdev helper functions 2791 * @preferred_bpp: Preferred bits per pixel for the device. 2792 * @dev->mode_config.preferred_depth is used if this is zero. 2793 * @max_conn_count: Maximum number of connectors (not used) 2794 * 2795 * This function sets up fbdev emulation and registers fbdev for access by 2796 * userspace. If all connectors are disconnected, setup is deferred to the next 2797 * time drm_fb_helper_hotplug_event() is called. 2798 * The caller must to provide a &drm_fb_helper_funcs->fb_probe callback 2799 * function. 2800 * 2801 * Use drm_fb_helper_fbdev_teardown() to destroy the fbdev. 2802 * 2803 * See also: drm_fb_helper_initial_config(), drm_fbdev_generic_setup(). 2804 * 2805 * Returns: 2806 * Zero on success or negative error code on failure. 2807 */ 2808 int drm_fb_helper_fbdev_setup(struct drm_device *dev, 2809 struct drm_fb_helper *fb_helper, 2810 const struct drm_fb_helper_funcs *funcs, 2811 unsigned int preferred_bpp, 2812 unsigned int max_conn_count) 2813 { 2814 int ret; 2815 2816 if (!preferred_bpp) 2817 preferred_bpp = dev->mode_config.preferred_depth; 2818 if (!preferred_bpp) 2819 preferred_bpp = 32; 2820 2821 drm_fb_helper_prepare(dev, fb_helper, funcs); 2822 2823 ret = drm_fb_helper_init(dev, fb_helper, 0); 2824 if (ret < 0) { 2825 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to initialize (ret=%d)\n", ret); 2826 return ret; 2827 } 2828 2829 ret = drm_fb_helper_single_add_all_connectors(fb_helper); 2830 if (ret < 0) { 2831 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to add connectors (ret=%d)\n", ret); 2832 goto err_drm_fb_helper_fini; 2833 } 2834 2835 if (!drm_drv_uses_atomic_modeset(dev)) 2836 drm_helper_disable_unused_functions(dev); 2837 2838 ret = drm_fb_helper_initial_config(fb_helper, preferred_bpp); 2839 if (ret < 0) { 2840 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to set configuration (ret=%d)\n", ret); 2841 goto err_drm_fb_helper_fini; 2842 } 2843 2844 return 0; 2845 2846 err_drm_fb_helper_fini: 2847 drm_fb_helper_fbdev_teardown(dev); 2848 2849 return ret; 2850 } 2851 EXPORT_SYMBOL(drm_fb_helper_fbdev_setup); 2852 2853 /** 2854 * drm_fb_helper_fbdev_teardown - Tear down fbdev emulation 2855 * @dev: DRM device 2856 * 2857 * This function unregisters fbdev if not already done and cleans up the 2858 * associated resources including the &drm_framebuffer. 2859 * The driver is responsible for freeing the &drm_fb_helper structure which is 2860 * stored in &drm_device->fb_helper. Do note that this pointer has been cleared 2861 * when this function returns. 2862 * 2863 * In order to support device removal/unplug while file handles are still open, 2864 * drm_fb_helper_unregister_fbi() should be called on device removal and 2865 * drm_fb_helper_fbdev_teardown() in the &drm_driver->release callback when 2866 * file handles are closed. 2867 */ 2868 void drm_fb_helper_fbdev_teardown(struct drm_device *dev) 2869 { 2870 struct drm_fb_helper *fb_helper = dev->fb_helper; 2871 struct fb_ops *fbops = NULL; 2872 2873 if (!fb_helper) 2874 return; 2875 2876 /* Unregister if it hasn't been done already */ 2877 if (fb_helper->fbdev && fb_helper->fbdev->dev) 2878 drm_fb_helper_unregister_fbi(fb_helper); 2879 2880 if (fb_helper->fbdev && fb_helper->fbdev->fbdefio) { 2881 fb_deferred_io_cleanup(fb_helper->fbdev); 2882 kfree(fb_helper->fbdev->fbdefio); 2883 fbops = fb_helper->fbdev->fbops; 2884 } 2885 2886 drm_fb_helper_fini(fb_helper); 2887 kfree(fbops); 2888 2889 if (fb_helper->fb) 2890 drm_framebuffer_remove(fb_helper->fb); 2891 } 2892 EXPORT_SYMBOL(drm_fb_helper_fbdev_teardown); 2893 2894 /** 2895 * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation 2896 * @dev: DRM device 2897 * 2898 * This function can be used as the &drm_driver->lastclose callback for drivers 2899 * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked(). 2900 */ 2901 void drm_fb_helper_lastclose(struct drm_device *dev) 2902 { 2903 drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper); 2904 } 2905 EXPORT_SYMBOL(drm_fb_helper_lastclose); 2906 2907 /** 2908 * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed 2909 * helper for fbdev emulation 2910 * @dev: DRM device 2911 * 2912 * This function can be used as the 2913 * &drm_mode_config_funcs.output_poll_changed callback for drivers that only 2914 * need to call drm_fb_helper_hotplug_event(). 2915 */ 2916 void drm_fb_helper_output_poll_changed(struct drm_device *dev) 2917 { 2918 drm_fb_helper_hotplug_event(dev->fb_helper); 2919 } 2920 EXPORT_SYMBOL(drm_fb_helper_output_poll_changed); 2921 2922 /* @user: 1=userspace, 0=fbcon */ 2923 static int drm_fbdev_fb_open(struct fb_info *info, int user) 2924 { 2925 struct drm_fb_helper *fb_helper = info->par; 2926 2927 /* No need to take a ref for fbcon because it unbinds on unregister */ 2928 if (user && !try_module_get(fb_helper->dev->driver->fops->owner)) 2929 return -ENODEV; 2930 2931 return 0; 2932 } 2933 2934 static int drm_fbdev_fb_release(struct fb_info *info, int user) 2935 { 2936 struct drm_fb_helper *fb_helper = info->par; 2937 2938 if (user) 2939 module_put(fb_helper->dev->driver->fops->owner); 2940 2941 return 0; 2942 } 2943 2944 static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper) 2945 { 2946 struct fb_info *fbi = fb_helper->fbdev; 2947 struct fb_ops *fbops = NULL; 2948 void *shadow = NULL; 2949 2950 if (!fb_helper->dev) 2951 return; 2952 2953 if (fbi && fbi->fbdefio) { 2954 fb_deferred_io_cleanup(fbi); 2955 shadow = fbi->screen_buffer; 2956 fbops = fbi->fbops; 2957 } 2958 2959 drm_fb_helper_fini(fb_helper); 2960 2961 if (shadow) { 2962 vfree(shadow); 2963 kfree(fbops); 2964 } 2965 2966 drm_client_framebuffer_delete(fb_helper->buffer); 2967 } 2968 2969 static void drm_fbdev_release(struct drm_fb_helper *fb_helper) 2970 { 2971 drm_fbdev_cleanup(fb_helper); 2972 drm_client_release(&fb_helper->client); 2973 kfree(fb_helper); 2974 } 2975 2976 /* 2977 * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of 2978 * unregister_framebuffer() or fb_release(). 2979 */ 2980 static void drm_fbdev_fb_destroy(struct fb_info *info) 2981 { 2982 drm_fbdev_release(info->par); 2983 } 2984 2985 static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma) 2986 { 2987 struct drm_fb_helper *fb_helper = info->par; 2988 2989 if (fb_helper->dev->driver->gem_prime_mmap) 2990 return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma); 2991 else 2992 return -ENODEV; 2993 } 2994 2995 static struct fb_ops drm_fbdev_fb_ops = { 2996 .owner = THIS_MODULE, 2997 DRM_FB_HELPER_DEFAULT_OPS, 2998 .fb_open = drm_fbdev_fb_open, 2999 .fb_release = drm_fbdev_fb_release, 3000 .fb_destroy = drm_fbdev_fb_destroy, 3001 .fb_mmap = drm_fbdev_fb_mmap, 3002 .fb_read = drm_fb_helper_sys_read, 3003 .fb_write = drm_fb_helper_sys_write, 3004 .fb_fillrect = drm_fb_helper_sys_fillrect, 3005 .fb_copyarea = drm_fb_helper_sys_copyarea, 3006 .fb_imageblit = drm_fb_helper_sys_imageblit, 3007 }; 3008 3009 static struct fb_deferred_io drm_fbdev_defio = { 3010 .delay = HZ / 20, 3011 .deferred_io = drm_fb_helper_deferred_io, 3012 }; 3013 3014 /** 3015 * drm_fb_helper_generic_probe - Generic fbdev emulation probe helper 3016 * @fb_helper: fbdev helper structure 3017 * @sizes: describes fbdev size and scanout surface size 3018 * 3019 * This function uses the client API to create a framebuffer backed by a dumb buffer. 3020 * 3021 * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect, 3022 * fb_copyarea, fb_imageblit. 3023 * 3024 * Returns: 3025 * Zero on success or negative error code on failure. 3026 */ 3027 int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, 3028 struct drm_fb_helper_surface_size *sizes) 3029 { 3030 struct drm_client_dev *client = &fb_helper->client; 3031 struct drm_client_buffer *buffer; 3032 struct drm_framebuffer *fb; 3033 struct fb_info *fbi; 3034 u32 format; 3035 3036 DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d)\n", 3037 sizes->surface_width, sizes->surface_height, 3038 sizes->surface_bpp); 3039 3040 format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth); 3041 buffer = drm_client_framebuffer_create(client, sizes->surface_width, 3042 sizes->surface_height, format); 3043 if (IS_ERR(buffer)) 3044 return PTR_ERR(buffer); 3045 3046 fb_helper->buffer = buffer; 3047 fb_helper->fb = buffer->fb; 3048 fb = buffer->fb; 3049 3050 fbi = drm_fb_helper_alloc_fbi(fb_helper); 3051 if (IS_ERR(fbi)) 3052 return PTR_ERR(fbi); 3053 3054 fbi->fbops = &drm_fbdev_fb_ops; 3055 fbi->screen_size = fb->height * fb->pitches[0]; 3056 fbi->fix.smem_len = fbi->screen_size; 3057 fbi->screen_buffer = buffer->vaddr; 3058 /* Shamelessly leak the physical address to user-space */ 3059 #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM) 3060 if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0) 3061 fbi->fix.smem_start = 3062 page_to_phys(virt_to_page(fbi->screen_buffer)); 3063 #endif 3064 drm_fb_helper_fill_info(fbi, fb_helper, sizes); 3065 3066 if (fb->funcs->dirty) { 3067 struct fb_ops *fbops; 3068 void *shadow; 3069 3070 /* 3071 * fb_deferred_io_cleanup() clears &fbops->fb_mmap so a per 3072 * instance version is necessary. 3073 */ 3074 fbops = kzalloc(sizeof(*fbops), GFP_KERNEL); 3075 shadow = vzalloc(fbi->screen_size); 3076 if (!fbops || !shadow) { 3077 kfree(fbops); 3078 vfree(shadow); 3079 return -ENOMEM; 3080 } 3081 3082 *fbops = *fbi->fbops; 3083 fbi->fbops = fbops; 3084 fbi->screen_buffer = shadow; 3085 fbi->fbdefio = &drm_fbdev_defio; 3086 3087 fb_deferred_io_init(fbi); 3088 } 3089 3090 return 0; 3091 } 3092 EXPORT_SYMBOL(drm_fb_helper_generic_probe); 3093 3094 static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = { 3095 .fb_probe = drm_fb_helper_generic_probe, 3096 }; 3097 3098 static void drm_fbdev_client_unregister(struct drm_client_dev *client) 3099 { 3100 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 3101 3102 if (fb_helper->fbdev) 3103 /* drm_fbdev_fb_destroy() takes care of cleanup */ 3104 drm_fb_helper_unregister_fbi(fb_helper); 3105 else 3106 drm_fbdev_release(fb_helper); 3107 } 3108 3109 static int drm_fbdev_client_restore(struct drm_client_dev *client) 3110 { 3111 drm_fb_helper_lastclose(client->dev); 3112 3113 return 0; 3114 } 3115 3116 static int drm_fbdev_client_hotplug(struct drm_client_dev *client) 3117 { 3118 struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client); 3119 struct drm_device *dev = client->dev; 3120 int ret; 3121 3122 /* Setup is not retried if it has failed */ 3123 if (!fb_helper->dev && fb_helper->funcs) 3124 return 0; 3125 3126 if (dev->fb_helper) 3127 return drm_fb_helper_hotplug_event(dev->fb_helper); 3128 3129 if (!dev->mode_config.num_connector) { 3130 DRM_DEV_DEBUG(dev->dev, "No connectors found, will not create framebuffer!\n"); 3131 return 0; 3132 } 3133 3134 drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs); 3135 3136 ret = drm_fb_helper_init(dev, fb_helper, 0); 3137 if (ret) 3138 goto err; 3139 3140 ret = drm_fb_helper_single_add_all_connectors(fb_helper); 3141 if (ret) 3142 goto err_cleanup; 3143 3144 if (!drm_drv_uses_atomic_modeset(dev)) 3145 drm_helper_disable_unused_functions(dev); 3146 3147 ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp); 3148 if (ret) 3149 goto err_cleanup; 3150 3151 return 0; 3152 3153 err_cleanup: 3154 drm_fbdev_cleanup(fb_helper); 3155 err: 3156 fb_helper->dev = NULL; 3157 fb_helper->fbdev = NULL; 3158 3159 DRM_DEV_ERROR(dev->dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret); 3160 3161 return ret; 3162 } 3163 3164 static const struct drm_client_funcs drm_fbdev_client_funcs = { 3165 .owner = THIS_MODULE, 3166 .unregister = drm_fbdev_client_unregister, 3167 .restore = drm_fbdev_client_restore, 3168 .hotplug = drm_fbdev_client_hotplug, 3169 }; 3170 3171 /** 3172 * drm_fbdev_generic_setup() - Setup generic fbdev emulation 3173 * @dev: DRM device 3174 * @preferred_bpp: Preferred bits per pixel for the device. 3175 * @dev->mode_config.preferred_depth is used if this is zero. 3176 * 3177 * This function sets up generic fbdev emulation for drivers that supports 3178 * dumb buffers with a virtual address and that can be mmap'ed. If the driver 3179 * does not support these functions, it could use drm_fb_helper_fbdev_setup(). 3180 * 3181 * Restore, hotplug events and teardown are all taken care of. Drivers that do 3182 * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves. 3183 * Simple drivers might use drm_mode_config_helper_suspend(). 3184 * 3185 * Drivers that set the dirty callback on their framebuffer will get a shadow 3186 * fbdev buffer that is blitted onto the real buffer. This is done in order to 3187 * make deferred I/O work with all kinds of buffers. 3188 * 3189 * This function is safe to call even when there are no connectors present. 3190 * Setup will be retried on the next hotplug event. 3191 * 3192 * The fbdev is destroyed by drm_dev_unregister(). 3193 * 3194 * Returns: 3195 * Zero on success or negative error code on failure. 3196 */ 3197 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) 3198 { 3199 struct drm_fb_helper *fb_helper; 3200 int ret; 3201 3202 WARN(dev->fb_helper, "fb_helper is already set!\n"); 3203 3204 if (!drm_fbdev_emulation) 3205 return 0; 3206 3207 fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL); 3208 if (!fb_helper) 3209 return -ENOMEM; 3210 3211 ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs); 3212 if (ret) { 3213 kfree(fb_helper); 3214 DRM_DEV_ERROR(dev->dev, "Failed to register client: %d\n", ret); 3215 return ret; 3216 } 3217 3218 if (!preferred_bpp) 3219 preferred_bpp = dev->mode_config.preferred_depth; 3220 if (!preferred_bpp) 3221 preferred_bpp = 32; 3222 fb_helper->preferred_bpp = preferred_bpp; 3223 3224 ret = drm_fbdev_client_hotplug(&fb_helper->client); 3225 if (ret) 3226 DRM_DEV_DEBUG(dev->dev, "client hotplug ret=%d\n", ret); 3227 3228 drm_client_register(&fb_helper->client); 3229 3230 return 0; 3231 } 3232 EXPORT_SYMBOL(drm_fbdev_generic_setup); 3233 3234 /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT) 3235 * but the module doesn't depend on any fb console symbols. At least 3236 * attempt to load fbcon to avoid leaving the system without a usable console. 3237 */ 3238 int __init drm_fb_helper_modinit(void) 3239 { 3240 #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT) 3241 const char name[] = "fbcon"; 3242 struct module *fbcon; 3243 3244 mutex_lock(&module_mutex); 3245 fbcon = find_module(name); 3246 mutex_unlock(&module_mutex); 3247 3248 if (!fbcon) 3249 request_module_nowait(name); 3250 #endif 3251 return 0; 3252 } 3253 EXPORT_SYMBOL(drm_fb_helper_modinit); 3254