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