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_clip_rect; 34 struct drm_fb_helper; 35 36 #include <linux/fb.h> 37 38 #include <drm/drm_client.h> 39 40 enum mode_set_atomic { 41 LEAVE_ATOMIC_MODE_SET, 42 ENTER_ATOMIC_MODE_SET, 43 }; 44 45 /** 46 * struct drm_fb_helper_surface_size - describes fbdev size and scanout surface size 47 * @fb_width: fbdev width 48 * @fb_height: fbdev height 49 * @surface_width: scanout buffer width 50 * @surface_height: scanout buffer height 51 * @surface_bpp: scanout buffer bpp 52 * @surface_depth: scanout buffer depth 53 * 54 * Note that the scanout surface width/height may be larger than the fbdev 55 * width/height. In case of multiple displays, the scanout surface is sized 56 * according to the largest width/height (so it is large enough for all CRTCs 57 * to scanout). But the fbdev width/height is sized to the minimum width/ 58 * height of all the displays. This ensures that fbcon fits on the smallest 59 * of the attached displays. fb_width/fb_height is used by 60 * drm_fb_helper_fill_info() to fill out the &fb_info.var structure. 61 */ 62 struct drm_fb_helper_surface_size { 63 u32 fb_width; 64 u32 fb_height; 65 u32 surface_width; 66 u32 surface_height; 67 u32 surface_bpp; 68 u32 surface_depth; 69 }; 70 71 /** 72 * struct drm_fb_helper_funcs - driver callbacks for the fbdev emulation library 73 * 74 * Driver callbacks used by the fbdev emulation helper library. 75 */ 76 struct drm_fb_helper_funcs { 77 /** 78 * @fb_probe: 79 * 80 * Driver callback to allocate and initialize the fbdev info structure. 81 * Furthermore it also needs to allocate the DRM framebuffer used to 82 * back the fbdev. 83 * 84 * This callback is mandatory. 85 * 86 * RETURNS: 87 * 88 * The driver should return 0 on success and a negative error code on 89 * failure. 90 */ 91 int (*fb_probe)(struct drm_fb_helper *helper, 92 struct drm_fb_helper_surface_size *sizes); 93 94 /** 95 * @fb_dirty: 96 * 97 * Driver callback to update the framebuffer memory. If set, fbdev 98 * emulation will invoke this callback in regular intervals after 99 * the framebuffer has been written. 100 * 101 * This callback is optional. 102 * 103 * Returns: 104 * 0 on success, or an error code otherwise. 105 */ 106 int (*fb_dirty)(struct drm_fb_helper *helper, struct drm_clip_rect *clip); 107 }; 108 109 /** 110 * struct drm_fb_helper - main structure to emulate fbdev on top of KMS 111 * @fb: Scanout framebuffer object 112 * @dev: DRM device 113 * @funcs: driver callbacks for fb helper 114 * @info: emulated fbdev device info struct 115 * @pseudo_palette: fake palette of 16 colors 116 * @damage_clip: clip rectangle used with deferred_io to accumulate damage to 117 * the screen buffer 118 * @damage_lock: spinlock protecting @damage_clip 119 * @damage_work: worker used to flush the framebuffer 120 * @resume_work: worker used during resume if the console lock is already taken 121 * 122 * This is the main structure used by the fbdev helpers. Drivers supporting 123 * fbdev emulation should embedded this into their overall driver structure. 124 * Drivers must also fill out a &struct drm_fb_helper_funcs with a few 125 * operations. 126 */ 127 struct drm_fb_helper { 128 /** 129 * @client: 130 * 131 * DRM client used by the generic fbdev emulation. 132 */ 133 struct drm_client_dev client; 134 135 /** 136 * @buffer: 137 * 138 * Framebuffer used by the generic fbdev emulation. 139 */ 140 struct drm_client_buffer *buffer; 141 142 struct drm_framebuffer *fb; 143 struct drm_device *dev; 144 const struct drm_fb_helper_funcs *funcs; 145 struct fb_info *info; 146 u32 pseudo_palette[17]; 147 struct drm_clip_rect damage_clip; 148 spinlock_t damage_lock; 149 struct work_struct damage_work; 150 struct work_struct resume_work; 151 152 /** 153 * @lock: 154 * 155 * Top-level FBDEV helper lock. This protects all internal data 156 * structures and lists, such as @connector_info and @crtc_info. 157 * 158 * FIXME: fbdev emulation locking is a mess and long term we want to 159 * protect all helper internal state with this lock as well as reduce 160 * core KMS locking as much as possible. 161 */ 162 struct mutex lock; 163 164 /** 165 * @kernel_fb_list: 166 * 167 * Entry on the global kernel_fb_helper_list, used for kgdb entry/exit. 168 */ 169 struct list_head kernel_fb_list; 170 171 /** 172 * @delayed_hotplug: 173 * 174 * A hotplug was received while fbdev wasn't in control of the DRM 175 * device, i.e. another KMS master was active. The output configuration 176 * needs to be reprobe when fbdev is in control again. 177 */ 178 bool delayed_hotplug; 179 180 /** 181 * @deferred_setup: 182 * 183 * If no outputs are connected (disconnected or unknown) the FB helper 184 * code will defer setup until at least one of the outputs shows up. 185 * This field keeps track of the status so that setup can be retried 186 * at every hotplug event until it succeeds eventually. 187 * 188 * Protected by @lock. 189 */ 190 bool deferred_setup; 191 192 /** 193 * @preferred_bpp: 194 * 195 * Temporary storage for the driver's preferred BPP setting passed to 196 * FB helper initialization. This needs to be tracked so that deferred 197 * FB helper setup can pass this on. 198 * 199 * See also: @deferred_setup 200 */ 201 int preferred_bpp; 202 203 bool hint_leak_smem_start; 204 }; 205 206 static inline struct drm_fb_helper * 207 drm_fb_helper_from_client(struct drm_client_dev *client) 208 { 209 return container_of(client, struct drm_fb_helper, client); 210 } 211 212 /** 213 * define DRM_FB_HELPER_DEFAULT_OPS - helper define for drm drivers 214 * 215 * Helper define to register default implementations of drm_fb_helper 216 * functions. To be used in struct fb_ops of drm drivers. 217 */ 218 #define DRM_FB_HELPER_DEFAULT_OPS \ 219 .fb_check_var = drm_fb_helper_check_var, \ 220 .fb_set_par = drm_fb_helper_set_par, \ 221 .fb_setcmap = drm_fb_helper_setcmap, \ 222 .fb_blank = drm_fb_helper_blank, \ 223 .fb_pan_display = drm_fb_helper_pan_display, \ 224 .fb_debug_enter = drm_fb_helper_debug_enter, \ 225 .fb_debug_leave = drm_fb_helper_debug_leave, \ 226 .fb_ioctl = drm_fb_helper_ioctl 227 228 #ifdef CONFIG_DRM_FBDEV_EMULATION 229 void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper, 230 const struct drm_fb_helper_funcs *funcs); 231 int drm_fb_helper_init(struct drm_device *dev, struct drm_fb_helper *helper); 232 void drm_fb_helper_fini(struct drm_fb_helper *helper); 233 int drm_fb_helper_blank(int blank, struct fb_info *info); 234 int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 235 struct fb_info *info); 236 int drm_fb_helper_set_par(struct fb_info *info); 237 int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 238 struct fb_info *info); 239 240 int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper); 241 242 struct fb_info *drm_fb_helper_alloc_info(struct drm_fb_helper *fb_helper); 243 void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper); 244 void drm_fb_helper_fill_info(struct fb_info *info, 245 struct drm_fb_helper *fb_helper, 246 struct drm_fb_helper_surface_size *sizes); 247 248 void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagereflist); 249 250 ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf, 251 size_t count, loff_t *ppos); 252 ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf, 253 size_t count, loff_t *ppos); 254 255 void drm_fb_helper_sys_fillrect(struct fb_info *info, 256 const struct fb_fillrect *rect); 257 void drm_fb_helper_sys_copyarea(struct fb_info *info, 258 const struct fb_copyarea *area); 259 void drm_fb_helper_sys_imageblit(struct fb_info *info, 260 const struct fb_image *image); 261 262 ssize_t drm_fb_helper_cfb_read(struct fb_info *info, char __user *buf, 263 size_t count, loff_t *ppos); 264 ssize_t drm_fb_helper_cfb_write(struct fb_info *info, const char __user *buf, 265 size_t count, loff_t *ppos); 266 267 void drm_fb_helper_cfb_fillrect(struct fb_info *info, 268 const struct fb_fillrect *rect); 269 void drm_fb_helper_cfb_copyarea(struct fb_info *info, 270 const struct fb_copyarea *area); 271 void drm_fb_helper_cfb_imageblit(struct fb_info *info, 272 const struct fb_image *image); 273 274 void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend); 275 void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, 276 bool suspend); 277 278 int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info); 279 280 int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 281 unsigned long arg); 282 283 int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper); 284 int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel); 285 int drm_fb_helper_debug_enter(struct fb_info *info); 286 int drm_fb_helper_debug_leave(struct fb_info *info); 287 288 void drm_fb_helper_lastclose(struct drm_device *dev); 289 void drm_fb_helper_output_poll_changed(struct drm_device *dev); 290 291 void drm_fbdev_generic_setup(struct drm_device *dev, 292 unsigned int preferred_bpp); 293 #else 294 static inline void drm_fb_helper_prepare(struct drm_device *dev, 295 struct drm_fb_helper *helper, 296 const struct drm_fb_helper_funcs *funcs) 297 { 298 } 299 300 static inline int drm_fb_helper_init(struct drm_device *dev, 301 struct drm_fb_helper *helper) 302 { 303 /* So drivers can use it to free the struct */ 304 helper->dev = dev; 305 dev->fb_helper = helper; 306 307 return 0; 308 } 309 310 static inline void drm_fb_helper_fini(struct drm_fb_helper *helper) 311 { 312 if (helper && helper->dev) 313 helper->dev->fb_helper = NULL; 314 } 315 316 static inline int drm_fb_helper_blank(int blank, struct fb_info *info) 317 { 318 return 0; 319 } 320 321 static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 322 struct fb_info *info) 323 { 324 return 0; 325 } 326 327 static inline int drm_fb_helper_set_par(struct fb_info *info) 328 { 329 return 0; 330 } 331 332 static inline int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 333 struct fb_info *info) 334 { 335 return 0; 336 } 337 338 static inline int 339 drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 340 { 341 return 0; 342 } 343 344 static inline struct fb_info * 345 drm_fb_helper_alloc_info(struct drm_fb_helper *fb_helper) 346 { 347 return NULL; 348 } 349 350 static inline void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper) 351 { 352 } 353 354 static inline void 355 drm_fb_helper_fill_info(struct fb_info *info, 356 struct drm_fb_helper *fb_helper, 357 struct drm_fb_helper_surface_size *sizes) 358 { 359 } 360 361 static inline int drm_fb_helper_setcmap(struct fb_cmap *cmap, 362 struct fb_info *info) 363 { 364 return 0; 365 } 366 367 static inline int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 368 unsigned long arg) 369 { 370 return 0; 371 } 372 373 static inline void drm_fb_helper_deferred_io(struct fb_info *info, 374 struct list_head *pagelist) 375 { 376 } 377 378 static inline int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper) 379 { 380 return -ENODEV; 381 } 382 383 static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info, 384 char __user *buf, size_t count, 385 loff_t *ppos) 386 { 387 return -ENODEV; 388 } 389 390 static inline ssize_t drm_fb_helper_sys_write(struct fb_info *info, 391 const char __user *buf, 392 size_t count, loff_t *ppos) 393 { 394 return -ENODEV; 395 } 396 397 static inline void drm_fb_helper_sys_fillrect(struct fb_info *info, 398 const struct fb_fillrect *rect) 399 { 400 } 401 402 static inline void drm_fb_helper_sys_copyarea(struct fb_info *info, 403 const struct fb_copyarea *area) 404 { 405 } 406 407 static inline void drm_fb_helper_sys_imageblit(struct fb_info *info, 408 const struct fb_image *image) 409 { 410 } 411 412 static inline ssize_t drm_fb_helper_cfb_read(struct fb_info *info, char __user *buf, 413 size_t count, loff_t *ppos) 414 { 415 return -ENODEV; 416 } 417 418 static inline ssize_t drm_fb_helper_cfb_write(struct fb_info *info, const char __user *buf, 419 size_t count, loff_t *ppos) 420 { 421 return -ENODEV; 422 } 423 424 static inline void drm_fb_helper_cfb_fillrect(struct fb_info *info, 425 const struct fb_fillrect *rect) 426 { 427 } 428 429 static inline void drm_fb_helper_cfb_copyarea(struct fb_info *info, 430 const struct fb_copyarea *area) 431 { 432 } 433 434 static inline void drm_fb_helper_cfb_imageblit(struct fb_info *info, 435 const struct fb_image *image) 436 { 437 } 438 439 static inline void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, 440 bool suspend) 441 { 442 } 443 444 static inline void 445 drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, bool suspend) 446 { 447 } 448 449 static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 450 { 451 return 0; 452 } 453 454 static inline int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, 455 int bpp_sel) 456 { 457 return 0; 458 } 459 460 static inline int drm_fb_helper_debug_enter(struct fb_info *info) 461 { 462 return 0; 463 } 464 465 static inline int drm_fb_helper_debug_leave(struct fb_info *info) 466 { 467 return 0; 468 } 469 470 static inline void drm_fb_helper_lastclose(struct drm_device *dev) 471 { 472 } 473 474 static inline void drm_fb_helper_output_poll_changed(struct drm_device *dev) 475 { 476 } 477 478 static inline void 479 drm_fbdev_generic_setup(struct drm_device *dev, unsigned int preferred_bpp) 480 { 481 } 482 483 #endif 484 485 #endif 486