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 #ifndef DRM_FB_HELPER_H 31 #define DRM_FB_HELPER_H 32 33 struct drm_fb_helper; 34 35 #include <drm/drm_client.h> 36 #include <drm/drm_crtc.h> 37 #include <drm/drm_device.h> 38 #include <linux/kgdb.h> 39 #include <linux/vgaarb.h> 40 41 enum mode_set_atomic { 42 LEAVE_ATOMIC_MODE_SET, 43 ENTER_ATOMIC_MODE_SET, 44 }; 45 46 struct drm_fb_offset { 47 int x, y; 48 }; 49 50 struct drm_fb_helper_crtc { 51 struct drm_mode_set mode_set; 52 struct drm_display_mode *desired_mode; 53 int x, y; 54 int rotation; 55 }; 56 57 /** 58 * struct drm_fb_helper_surface_size - describes fbdev size and scanout surface size 59 * @fb_width: fbdev width 60 * @fb_height: fbdev height 61 * @surface_width: scanout buffer width 62 * @surface_height: scanout buffer height 63 * @surface_bpp: scanout buffer bpp 64 * @surface_depth: scanout buffer depth 65 * 66 * Note that the scanout surface width/height may be larger than the fbdev 67 * width/height. In case of multiple displays, the scanout surface is sized 68 * according to the largest width/height (so it is large enough for all CRTCs 69 * to scanout). But the fbdev width/height is sized to the minimum width/ 70 * height of all the displays. This ensures that fbcon fits on the smallest 71 * of the attached displays. 72 * 73 * So what is passed to drm_fb_helper_fill_var() should be fb_width/fb_height, 74 * rather than the surface size. 75 */ 76 struct drm_fb_helper_surface_size { 77 u32 fb_width; 78 u32 fb_height; 79 u32 surface_width; 80 u32 surface_height; 81 u32 surface_bpp; 82 u32 surface_depth; 83 }; 84 85 /** 86 * struct drm_fb_helper_funcs - driver callbacks for the fbdev emulation library 87 * 88 * Driver callbacks used by the fbdev emulation helper library. 89 */ 90 struct drm_fb_helper_funcs { 91 /** 92 * @fb_probe: 93 * 94 * Driver callback to allocate and initialize the fbdev info structure. 95 * Furthermore it also needs to allocate the DRM framebuffer used to 96 * back the fbdev. 97 * 98 * This callback is mandatory. 99 * 100 * RETURNS: 101 * 102 * The driver should return 0 on success and a negative error code on 103 * failure. 104 */ 105 int (*fb_probe)(struct drm_fb_helper *helper, 106 struct drm_fb_helper_surface_size *sizes); 107 108 /** 109 * @initial_config: 110 * 111 * Driver callback to setup an initial fbdev display configuration. 112 * Drivers can use this callback to tell the fbdev emulation what the 113 * preferred initial configuration is. This is useful to implement 114 * smooth booting where the fbdev (and subsequently all userspace) never 115 * changes the mode, but always inherits the existing configuration. 116 * 117 * This callback is optional. 118 * 119 * RETURNS: 120 * 121 * The driver should return true if a suitable initial configuration has 122 * been filled out and false when the fbdev helper should fall back to 123 * the default probing logic. 124 */ 125 bool (*initial_config)(struct drm_fb_helper *fb_helper, 126 struct drm_fb_helper_crtc **crtcs, 127 struct drm_display_mode **modes, 128 struct drm_fb_offset *offsets, 129 bool *enabled, int width, int height); 130 }; 131 132 struct drm_fb_helper_connector { 133 struct drm_connector *connector; 134 }; 135 136 /** 137 * struct drm_fb_helper - main structure to emulate fbdev on top of KMS 138 * @fb: Scanout framebuffer object 139 * @dev: DRM device 140 * @crtc_count: number of possible CRTCs 141 * @crtc_info: per-CRTC helper state (mode, x/y offset, etc) 142 * @connector_count: number of connected connectors 143 * @connector_info_alloc_count: size of connector_info 144 * @funcs: driver callbacks for fb helper 145 * @fbdev: emulated fbdev device info struct 146 * @pseudo_palette: fake palette of 16 colors 147 * @dirty_clip: clip rectangle used with deferred_io to accumulate damage to 148 * the screen buffer 149 * @dirty_lock: spinlock protecting @dirty_clip 150 * @dirty_work: worker used to flush the framebuffer 151 * @resume_work: worker used during resume if the console lock is already taken 152 * 153 * This is the main structure used by the fbdev helpers. Drivers supporting 154 * fbdev emulation should embedded this into their overall driver structure. 155 * Drivers must also fill out a &struct drm_fb_helper_funcs with a few 156 * operations. 157 */ 158 struct drm_fb_helper { 159 /** 160 * @client: 161 * 162 * DRM client used by the generic fbdev emulation. 163 */ 164 struct drm_client_dev client; 165 166 /** 167 * @buffer: 168 * 169 * Framebuffer used by the generic fbdev emulation. 170 */ 171 struct drm_client_buffer *buffer; 172 173 struct drm_framebuffer *fb; 174 struct drm_device *dev; 175 int crtc_count; 176 struct drm_fb_helper_crtc *crtc_info; 177 int connector_count; 178 int connector_info_alloc_count; 179 /** 180 * @sw_rotations: 181 * Bitmask of all rotations requested for panel-orientation which 182 * could not be handled in hardware. If only one bit is set 183 * fbdev->fbcon_rotate_hint gets set to the requested rotation. 184 */ 185 int sw_rotations; 186 /** 187 * @connector_info: 188 * 189 * Array of per-connector information. Do not iterate directly, but use 190 * drm_fb_helper_for_each_connector. 191 */ 192 struct drm_fb_helper_connector **connector_info; 193 const struct drm_fb_helper_funcs *funcs; 194 struct fb_info *fbdev; 195 u32 pseudo_palette[17]; 196 struct drm_clip_rect dirty_clip; 197 spinlock_t dirty_lock; 198 struct work_struct dirty_work; 199 struct work_struct resume_work; 200 201 /** 202 * @lock: 203 * 204 * Top-level FBDEV helper lock. This protects all internal data 205 * structures and lists, such as @connector_info and @crtc_info. 206 * 207 * FIXME: fbdev emulation locking is a mess and long term we want to 208 * protect all helper internal state with this lock as well as reduce 209 * core KMS locking as much as possible. 210 */ 211 struct mutex lock; 212 213 /** 214 * @kernel_fb_list: 215 * 216 * Entry on the global kernel_fb_helper_list, used for kgdb entry/exit. 217 */ 218 struct list_head kernel_fb_list; 219 220 /** 221 * @delayed_hotplug: 222 * 223 * A hotplug was received while fbdev wasn't in control of the DRM 224 * device, i.e. another KMS master was active. The output configuration 225 * needs to be reprobe when fbdev is in control again. 226 */ 227 bool delayed_hotplug; 228 229 /** 230 * @deferred_setup: 231 * 232 * If no outputs are connected (disconnected or unknown) the FB helper 233 * code will defer setup until at least one of the outputs shows up. 234 * This field keeps track of the status so that setup can be retried 235 * at every hotplug event until it succeeds eventually. 236 * 237 * Protected by @lock. 238 */ 239 bool deferred_setup; 240 241 /** 242 * @preferred_bpp: 243 * 244 * Temporary storage for the driver's preferred BPP setting passed to 245 * FB helper initialization. This needs to be tracked so that deferred 246 * FB helper setup can pass this on. 247 * 248 * See also: @deferred_setup 249 */ 250 int preferred_bpp; 251 }; 252 253 static inline struct drm_fb_helper * 254 drm_fb_helper_from_client(struct drm_client_dev *client) 255 { 256 return container_of(client, struct drm_fb_helper, client); 257 } 258 259 /** 260 * define DRM_FB_HELPER_DEFAULT_OPS - helper define for drm drivers 261 * 262 * Helper define to register default implementations of drm_fb_helper 263 * functions. To be used in struct fb_ops of drm drivers. 264 */ 265 #define DRM_FB_HELPER_DEFAULT_OPS \ 266 .fb_check_var = drm_fb_helper_check_var, \ 267 .fb_set_par = drm_fb_helper_set_par, \ 268 .fb_setcmap = drm_fb_helper_setcmap, \ 269 .fb_blank = drm_fb_helper_blank, \ 270 .fb_pan_display = drm_fb_helper_pan_display, \ 271 .fb_debug_enter = drm_fb_helper_debug_enter, \ 272 .fb_debug_leave = drm_fb_helper_debug_leave, \ 273 .fb_ioctl = drm_fb_helper_ioctl 274 275 #ifdef CONFIG_DRM_FBDEV_EMULATION 276 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 277 const struct drm_fb_helper_funcs *funcs); 278 int drm_fb_helper_init(struct drm_device *dev, 279 struct drm_fb_helper *helper, int max_conn); 280 void drm_fb_helper_fini(struct drm_fb_helper *helper); 281 int drm_fb_helper_blank(int blank, struct fb_info *info); 282 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 283 struct fb_info *info); 284 int drm_fb_helper_set_par(struct fb_info *info); 285 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 286 struct fb_info *info); 287 288 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper); 289 290 struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper); 291 void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper); 292 void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, 293 uint32_t fb_width, uint32_t fb_height); 294 void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 295 uint32_t depth); 296 297 void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper); 298 299 void drm_fb_helper_deferred_io(struct fb_info *info, 300 struct list_head *pagelist); 301 int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper); 302 303 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 304 size_t count, loff_t *ppos); 305 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 306 size_t count, loff_t *ppos); 307 308 void drm_fb_helper_sys_fillrect(struct fb_info *info, 309 const struct fb_fillrect *rect); 310 void drm_fb_helper_sys_copyarea(struct fb_info *info, 311 const struct fb_copyarea *area); 312 void drm_fb_helper_sys_imageblit(struct fb_info *info, 313 const struct fb_image *image); 314 315 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 316 const struct fb_fillrect *rect); 317 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 318 const struct fb_copyarea *area); 319 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 320 const struct fb_image *image); 321 322 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend); 323 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 324 bool suspend); 325 326 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info); 327 328 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 329 unsigned long arg); 330 331 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper); 332 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel); 333 int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper); 334 int drm_fb_helper_debug_enter(struct fb_info *info); 335 int drm_fb_helper_debug_leave(struct fb_info *info); 336 struct drm_display_mode * 337 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, 338 int width, int height); 339 struct drm_display_mode * 340 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn); 341 342 int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, struct drm_connector *connector); 343 int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 344 struct drm_connector *connector); 345 346 int drm_fb_helper_fbdev_setup(struct drm_device *dev, 347 struct drm_fb_helper *fb_helper, 348 const struct drm_fb_helper_funcs *funcs, 349 unsigned int preferred_bpp, 350 unsigned int max_conn_count); 351 void drm_fb_helper_fbdev_teardown(struct drm_device *dev); 352 353 void drm_fb_helper_lastclose(struct drm_device *dev); 354 void drm_fb_helper_output_poll_changed(struct drm_device *dev); 355 356 int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, 357 struct drm_fb_helper_surface_size *sizes); 358 int drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp); 359 #else 360 static inline void drm_fb_helper_prepare(struct drm_device *dev, 361 struct drm_fb_helper *helper, 362 const struct drm_fb_helper_funcs *funcs) 363 { 364 } 365 366 static inline int drm_fb_helper_init(struct drm_device *dev, 367 struct drm_fb_helper *helper, 368 int max_conn) 369 { 370 /* So drivers can use it to free the struct */ 371 helper->dev = dev; 372 dev->fb_helper = helper; 373 374 return 0; 375 } 376 377 static inline void drm_fb_helper_fini(struct drm_fb_helper *helper) 378 { 379 if (helper && helper->dev) 380 helper->dev->fb_helper = NULL; 381 } 382 383 static inline int drm_fb_helper_blank(int blank, struct fb_info *info) 384 { 385 return 0; 386 } 387 388 static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 389 struct fb_info *info) 390 { 391 return 0; 392 } 393 394 static inline int drm_fb_helper_set_par(struct fb_info *info) 395 { 396 return 0; 397 } 398 399 static inline int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 400 struct fb_info *info) 401 { 402 return 0; 403 } 404 405 static inline int 406 drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 407 { 408 return 0; 409 } 410 411 static inline struct fb_info * 412 drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper) 413 { 414 return NULL; 415 } 416 417 static inline void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper) 418 { 419 } 420 421 static inline void drm_fb_helper_fill_var(struct fb_info *info, 422 struct drm_fb_helper *fb_helper, 423 uint32_t fb_width, uint32_t fb_height) 424 { 425 } 426 427 static inline void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, 428 uint32_t depth) 429 { 430 } 431 432 static inline int drm_fb_helper_setcmap(struct fb_cmap *cmap, 433 struct fb_info *info) 434 { 435 return 0; 436 } 437 438 static inline int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 439 unsigned long arg) 440 { 441 return 0; 442 } 443 444 static inline void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper) 445 { 446 } 447 448 static inline void drm_fb_helper_deferred_io(struct fb_info *info, 449 struct list_head *pagelist) 450 { 451 } 452 453 static inline int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper) 454 { 455 return -ENODEV; 456 } 457 458 static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info, 459 char __user *buf, size_t count, 460 loff_t *ppos) 461 { 462 return -ENODEV; 463 } 464 465 static inline ssize_t drm_fb_helper_sys_write(struct fb_info *info, 466 const char __user *buf, 467 size_t count, loff_t *ppos) 468 { 469 return -ENODEV; 470 } 471 472 static inline void drm_fb_helper_sys_fillrect(struct fb_info *info, 473 const struct fb_fillrect *rect) 474 { 475 } 476 477 static inline void drm_fb_helper_sys_copyarea(struct fb_info *info, 478 const struct fb_copyarea *area) 479 { 480 } 481 482 static inline void drm_fb_helper_sys_imageblit(struct fb_info *info, 483 const struct fb_image *image) 484 { 485 } 486 487 static inline void drm_fb_helper_cfb_fillrect(struct fb_info *info, 488 const struct fb_fillrect *rect) 489 { 490 } 491 492 static inline void drm_fb_helper_cfb_copyarea(struct fb_info *info, 493 const struct fb_copyarea *area) 494 { 495 } 496 497 static inline void drm_fb_helper_cfb_imageblit(struct fb_info *info, 498 const struct fb_image *image) 499 { 500 } 501 502 static inline void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, 503 bool suspend) 504 { 505 } 506 507 static inline void 508 drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, bool suspend) 509 { 510 } 511 512 static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 513 { 514 return 0; 515 } 516 517 static inline int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, 518 int bpp_sel) 519 { 520 return 0; 521 } 522 523 static inline int 524 drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) 525 { 526 return 0; 527 } 528 529 static inline int drm_fb_helper_debug_enter(struct fb_info *info) 530 { 531 return 0; 532 } 533 534 static inline int drm_fb_helper_debug_leave(struct fb_info *info) 535 { 536 return 0; 537 } 538 539 static inline struct drm_display_mode * 540 drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, 541 int width, int height) 542 { 543 return NULL; 544 } 545 546 static inline struct drm_display_mode * 547 drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, 548 int width, int height) 549 { 550 return NULL; 551 } 552 553 static inline int 554 drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper, 555 struct drm_connector *connector) 556 { 557 return 0; 558 } 559 560 static inline int 561 drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper, 562 struct drm_connector *connector) 563 { 564 return 0; 565 } 566 567 static inline int 568 drm_fb_helper_fbdev_setup(struct drm_device *dev, 569 struct drm_fb_helper *fb_helper, 570 const struct drm_fb_helper_funcs *funcs, 571 unsigned int preferred_bpp, 572 unsigned int max_conn_count) 573 { 574 /* So drivers can use it to free the struct */ 575 dev->fb_helper = fb_helper; 576 577 return 0; 578 } 579 580 static inline void drm_fb_helper_fbdev_teardown(struct drm_device *dev) 581 { 582 dev->fb_helper = NULL; 583 } 584 585 static inline void drm_fb_helper_lastclose(struct drm_device *dev) 586 { 587 } 588 589 static inline void drm_fb_helper_output_poll_changed(struct drm_device *dev) 590 { 591 } 592 593 static inline int 594 drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper, 595 struct drm_fb_helper_surface_size *sizes) 596 { 597 return 0; 598 } 599 600 static inline int 601 drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) 602 { 603 return 0; 604 } 605 606 #endif 607 608 /** 609 * drm_fb_helper_remove_conflicting_framebuffers - remove firmware-configured framebuffers 610 * @a: memory range, users of which are to be removed 611 * @name: requesting driver name 612 * @primary: also kick vga16fb if present 613 * 614 * This function removes framebuffer devices (initialized by firmware/bootloader) 615 * which use memory range described by @a. If @a is NULL all such devices are 616 * removed. 617 */ 618 static inline int 619 drm_fb_helper_remove_conflicting_framebuffers(struct apertures_struct *a, 620 const char *name, bool primary) 621 { 622 #if IS_REACHABLE(CONFIG_FB) 623 return remove_conflicting_framebuffers(a, name, primary); 624 #else 625 return 0; 626 #endif 627 } 628 629 /** 630 * drm_fb_helper_remove_conflicting_pci_framebuffers - remove firmware-configured framebuffers for PCI devices 631 * @pdev: PCI device 632 * @resource_id: index of PCI BAR configuring framebuffer memory 633 * @name: requesting driver name 634 * 635 * This function removes framebuffer devices (eg. initialized by firmware) 636 * using memory range configured for @pdev's BAR @resource_id. 637 * 638 * The function assumes that PCI device with shadowed ROM drives a primary 639 * display and so kicks out vga16fb. 640 */ 641 static inline int 642 drm_fb_helper_remove_conflicting_pci_framebuffers(struct pci_dev *pdev, 643 int resource_id, 644 const char *name) 645 { 646 int ret = 0; 647 648 /* 649 * WARNING: Apparently we must kick fbdev drivers before vgacon, 650 * otherwise the vga fbdev driver falls over. 651 */ 652 #if IS_REACHABLE(CONFIG_FB) 653 ret = remove_conflicting_pci_framebuffers(pdev, resource_id, name); 654 #endif 655 if (ret == 0) 656 ret = vga_remove_vgacon(pdev); 657 return ret; 658 } 659 660 #endif 661