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 #else 291 static inline void drm_fb_helper_prepare(struct drm_device *dev, 292 struct drm_fb_helper *helper, 293 const struct drm_fb_helper_funcs *funcs) 294 { 295 } 296 297 static inline int drm_fb_helper_init(struct drm_device *dev, 298 struct drm_fb_helper *helper) 299 { 300 /* So drivers can use it to free the struct */ 301 helper->dev = dev; 302 dev->fb_helper = helper; 303 304 return 0; 305 } 306 307 static inline void drm_fb_helper_fini(struct drm_fb_helper *helper) 308 { 309 if (helper && helper->dev) 310 helper->dev->fb_helper = NULL; 311 } 312 313 static inline int drm_fb_helper_blank(int blank, struct fb_info *info) 314 { 315 return 0; 316 } 317 318 static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, 319 struct fb_info *info) 320 { 321 return 0; 322 } 323 324 static inline int drm_fb_helper_set_par(struct fb_info *info) 325 { 326 return 0; 327 } 328 329 static inline int drm_fb_helper_check_var(struct fb_var_screeninfo *var, 330 struct fb_info *info) 331 { 332 return 0; 333 } 334 335 static inline int 336 drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper) 337 { 338 return 0; 339 } 340 341 static inline struct fb_info * 342 drm_fb_helper_alloc_info(struct drm_fb_helper *fb_helper) 343 { 344 return NULL; 345 } 346 347 static inline void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper) 348 { 349 } 350 351 static inline void 352 drm_fb_helper_fill_info(struct fb_info *info, 353 struct drm_fb_helper *fb_helper, 354 struct drm_fb_helper_surface_size *sizes) 355 { 356 } 357 358 static inline int drm_fb_helper_setcmap(struct fb_cmap *cmap, 359 struct fb_info *info) 360 { 361 return 0; 362 } 363 364 static inline int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd, 365 unsigned long arg) 366 { 367 return 0; 368 } 369 370 static inline void drm_fb_helper_deferred_io(struct fb_info *info, 371 struct list_head *pagelist) 372 { 373 } 374 375 static inline int drm_fb_helper_defio_init(struct drm_fb_helper *fb_helper) 376 { 377 return -ENODEV; 378 } 379 380 static inline ssize_t drm_fb_helper_sys_read(struct fb_info *info, 381 char __user *buf, size_t count, 382 loff_t *ppos) 383 { 384 return -ENODEV; 385 } 386 387 static inline ssize_t drm_fb_helper_sys_write(struct fb_info *info, 388 const char __user *buf, 389 size_t count, loff_t *ppos) 390 { 391 return -ENODEV; 392 } 393 394 static inline void drm_fb_helper_sys_fillrect(struct fb_info *info, 395 const struct fb_fillrect *rect) 396 { 397 } 398 399 static inline void drm_fb_helper_sys_copyarea(struct fb_info *info, 400 const struct fb_copyarea *area) 401 { 402 } 403 404 static inline void drm_fb_helper_sys_imageblit(struct fb_info *info, 405 const struct fb_image *image) 406 { 407 } 408 409 static inline ssize_t drm_fb_helper_cfb_read(struct fb_info *info, char __user *buf, 410 size_t count, loff_t *ppos) 411 { 412 return -ENODEV; 413 } 414 415 static inline ssize_t drm_fb_helper_cfb_write(struct fb_info *info, const char __user *buf, 416 size_t count, loff_t *ppos) 417 { 418 return -ENODEV; 419 } 420 421 static inline void drm_fb_helper_cfb_fillrect(struct fb_info *info, 422 const struct fb_fillrect *rect) 423 { 424 } 425 426 static inline void drm_fb_helper_cfb_copyarea(struct fb_info *info, 427 const struct fb_copyarea *area) 428 { 429 } 430 431 static inline void drm_fb_helper_cfb_imageblit(struct fb_info *info, 432 const struct fb_image *image) 433 { 434 } 435 436 static inline void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, 437 bool suspend) 438 { 439 } 440 441 static inline void 442 drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, bool suspend) 443 { 444 } 445 446 static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper) 447 { 448 return 0; 449 } 450 451 static inline int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, 452 int bpp_sel) 453 { 454 return 0; 455 } 456 457 static inline int drm_fb_helper_debug_enter(struct fb_info *info) 458 { 459 return 0; 460 } 461 462 static inline int drm_fb_helper_debug_leave(struct fb_info *info) 463 { 464 return 0; 465 } 466 467 static inline void drm_fb_helper_lastclose(struct drm_device *dev) 468 { 469 } 470 471 static inline void drm_fb_helper_output_poll_changed(struct drm_device *dev) 472 { 473 } 474 #endif 475 476 #endif 477