drm_fb_helper.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. struct drm_clip_rect;
  33. struct drm_fb_helper;
  34. #include <linux/fb.h>
  35. #include <drm/drm_client.h>
  36. /**
  37. * struct drm_fb_helper_surface_size - describes fbdev size and scanout surface size
  38. * @fb_width: fbdev width
  39. * @fb_height: fbdev height
  40. * @surface_width: scanout buffer width
  41. * @surface_height: scanout buffer height
  42. * @surface_bpp: scanout buffer bpp
  43. * @surface_depth: scanout buffer depth
  44. *
  45. * Note that the scanout surface width/height may be larger than the fbdev
  46. * width/height. In case of multiple displays, the scanout surface is sized
  47. * according to the largest width/height (so it is large enough for all CRTCs
  48. * to scanout). But the fbdev width/height is sized to the minimum width/
  49. * height of all the displays. This ensures that fbcon fits on the smallest
  50. * of the attached displays. fb_width/fb_height is used by
  51. * drm_fb_helper_fill_info() to fill out the &fb_info.var structure.
  52. */
  53. struct drm_fb_helper_surface_size {
  54. u32 fb_width;
  55. u32 fb_height;
  56. u32 surface_width;
  57. u32 surface_height;
  58. u32 surface_bpp;
  59. u32 surface_depth;
  60. };
  61. /**
  62. * struct drm_fb_helper_funcs - driver callbacks for the fbdev emulation library
  63. *
  64. * Driver callbacks used by the fbdev emulation helper library.
  65. */
  66. struct drm_fb_helper_funcs {
  67. /**
  68. * @fb_dirty:
  69. *
  70. * Driver callback to update the framebuffer memory. If set, fbdev
  71. * emulation will invoke this callback in regular intervals after
  72. * the framebuffer has been written.
  73. *
  74. * This callback is optional.
  75. *
  76. * Returns:
  77. * 0 on success, or an error code otherwise.
  78. */
  79. int (*fb_dirty)(struct drm_fb_helper *helper, struct drm_clip_rect *clip);
  80. /**
  81. * @fb_restore:
  82. *
  83. * Driver callback to restore internal fbdev state. If set, fbdev
  84. * emulation will invoke this callback after restoring the display
  85. * mode.
  86. *
  87. * Only for i915. Do not use in new code.
  88. *
  89. * TODO: Fix i915 to not require this callback.
  90. */
  91. void (*fb_restore)(struct drm_fb_helper *helper);
  92. /**
  93. * @fb_set_suspend:
  94. *
  95. * Driver callback to suspend or resume, if set, fbdev emulation will
  96. * invoke this callback during suspend and resume. Driver should call
  97. * fb_set_suspend() from their implementation. If not set, fbdev
  98. * emulation will invoke fb_set_suspend() directly.
  99. *
  100. * Only for i915. Do not use in new code.
  101. *
  102. * TODO: Fix i915 to not require this callback.
  103. */
  104. void (*fb_set_suspend)(struct drm_fb_helper *helper, bool suspend);
  105. };
  106. /**
  107. * struct drm_fb_helper - main structure to emulate fbdev on top of KMS
  108. * @fb: Scanout framebuffer object
  109. * @dev: DRM device
  110. * @funcs: driver callbacks for fb helper
  111. * @info: emulated fbdev device info struct
  112. * @pseudo_palette: fake palette of 16 colors
  113. * @damage_clip: clip rectangle used with deferred_io to accumulate damage to
  114. * the screen buffer
  115. * @damage_lock: spinlock protecting @damage_clip
  116. * @damage_work: worker used to flush the framebuffer
  117. * @resume_work: worker used during resume if the console lock is already taken
  118. *
  119. * This is the main structure used by the fbdev helpers. Drivers supporting
  120. * fbdev emulation should embedded this into their overall driver structure.
  121. * Drivers must also fill out a &struct drm_fb_helper_funcs with a few
  122. * operations.
  123. */
  124. struct drm_fb_helper {
  125. /**
  126. * @client:
  127. *
  128. * DRM client used by the generic fbdev emulation.
  129. */
  130. struct drm_client_dev client;
  131. /**
  132. * @buffer:
  133. *
  134. * Framebuffer used by the generic fbdev emulation.
  135. */
  136. struct drm_client_buffer *buffer;
  137. struct drm_framebuffer *fb;
  138. struct drm_device *dev;
  139. const struct drm_fb_helper_funcs *funcs;
  140. struct fb_info *info;
  141. u32 pseudo_palette[17];
  142. struct drm_clip_rect damage_clip;
  143. spinlock_t damage_lock;
  144. struct work_struct damage_work;
  145. struct work_struct resume_work;
  146. /**
  147. * @lock:
  148. *
  149. * Top-level FBDEV helper lock. This protects all internal data
  150. * structures and lists, such as @connector_info and @crtc_info.
  151. *
  152. * FIXME: fbdev emulation locking is a mess and long term we want to
  153. * protect all helper internal state with this lock as well as reduce
  154. * core KMS locking as much as possible.
  155. */
  156. struct mutex lock;
  157. /**
  158. * @delayed_hotplug:
  159. *
  160. * A hotplug was received while fbdev wasn't in control of the DRM
  161. * device, i.e. another KMS master was active. The output configuration
  162. * needs to be reprobe when fbdev is in control again.
  163. */
  164. bool delayed_hotplug;
  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. * @preferred_bpp:
  178. *
  179. * Temporary storage for the driver's preferred BPP setting passed to
  180. * FB helper initialization. This needs to be tracked so that deferred
  181. * FB helper setup can pass this on.
  182. *
  183. * See also: @deferred_setup
  184. */
  185. int preferred_bpp;
  186. #ifdef CONFIG_FB_DEFERRED_IO
  187. /**
  188. * @fbdefio:
  189. *
  190. * Temporary storage for the driver's FB deferred I/O handler. If the
  191. * driver uses the DRM fbdev emulation layer, this is set by the core
  192. * to a generic deferred I/O handler if a driver is preferring to use
  193. * a shadow buffer.
  194. */
  195. struct fb_deferred_io fbdefio;
  196. #endif
  197. };
  198. static inline struct drm_fb_helper *
  199. drm_fb_helper_from_client(struct drm_client_dev *client)
  200. {
  201. return container_of(client, struct drm_fb_helper, client);
  202. }
  203. /**
  204. * define DRM_FB_HELPER_DEFAULT_OPS - helper define for drm drivers
  205. *
  206. * Helper define to register default implementations of drm_fb_helper
  207. * functions. To be used in struct fb_ops of drm drivers.
  208. */
  209. #define DRM_FB_HELPER_DEFAULT_OPS \
  210. .fb_check_var = drm_fb_helper_check_var, \
  211. .fb_set_par = drm_fb_helper_set_par, \
  212. .fb_setcmap = drm_fb_helper_setcmap, \
  213. .fb_blank = drm_fb_helper_blank, \
  214. .fb_pan_display = drm_fb_helper_pan_display, \
  215. .fb_ioctl = drm_fb_helper_ioctl
  216. #ifdef CONFIG_DRM_FBDEV_EMULATION
  217. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  218. unsigned int preferred_bpp,
  219. const struct drm_fb_helper_funcs *funcs);
  220. void drm_fb_helper_unprepare(struct drm_fb_helper *fb_helper);
  221. int drm_fb_helper_init(struct drm_device *dev, struct drm_fb_helper *helper);
  222. void drm_fb_helper_fini(struct drm_fb_helper *helper);
  223. int drm_fb_helper_blank(int blank, struct fb_info *info);
  224. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  225. struct fb_info *info);
  226. int drm_fb_helper_set_par(struct fb_info *info);
  227. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  228. struct fb_info *info);
  229. int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper,
  230. bool force);
  231. void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper);
  232. void drm_fb_helper_fill_info(struct fb_info *info,
  233. struct drm_fb_helper *fb_helper,
  234. struct drm_fb_helper_surface_size *sizes);
  235. void drm_fb_helper_damage_range(struct fb_info *info, off_t off, size_t len);
  236. void drm_fb_helper_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height);
  237. #ifdef CONFIG_FB_DEFERRED_IO
  238. void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagereflist);
  239. #endif
  240. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend);
  241. void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
  242. bool suspend);
  243. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info);
  244. int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
  245. unsigned long arg);
  246. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper);
  247. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper);
  248. #else
  249. static inline void drm_fb_helper_prepare(struct drm_device *dev,
  250. struct drm_fb_helper *helper,
  251. unsigned int preferred_bpp,
  252. const struct drm_fb_helper_funcs *funcs)
  253. {
  254. }
  255. static inline void drm_fb_helper_unprepare(struct drm_fb_helper *fb_helper)
  256. {
  257. }
  258. static inline int drm_fb_helper_init(struct drm_device *dev,
  259. struct drm_fb_helper *helper)
  260. {
  261. /* So drivers can use it to free the struct */
  262. helper->dev = dev;
  263. dev->fb_helper = helper;
  264. return 0;
  265. }
  266. static inline void drm_fb_helper_fini(struct drm_fb_helper *helper)
  267. {
  268. if (helper && helper->dev)
  269. helper->dev->fb_helper = NULL;
  270. }
  271. static inline int drm_fb_helper_blank(int blank, struct fb_info *info)
  272. {
  273. return 0;
  274. }
  275. static inline int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  276. struct fb_info *info)
  277. {
  278. return 0;
  279. }
  280. static inline int drm_fb_helper_set_par(struct fb_info *info)
  281. {
  282. return 0;
  283. }
  284. static inline int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  285. struct fb_info *info)
  286. {
  287. return 0;
  288. }
  289. static inline int
  290. drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  291. {
  292. return 0;
  293. }
  294. static inline void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper)
  295. {
  296. }
  297. static inline void
  298. drm_fb_helper_fill_info(struct fb_info *info,
  299. struct drm_fb_helper *fb_helper,
  300. struct drm_fb_helper_surface_size *sizes)
  301. {
  302. }
  303. static inline int drm_fb_helper_setcmap(struct fb_cmap *cmap,
  304. struct fb_info *info)
  305. {
  306. return 0;
  307. }
  308. static inline int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
  309. unsigned long arg)
  310. {
  311. return 0;
  312. }
  313. #ifdef CONFIG_FB_DEFERRED_IO
  314. static inline void drm_fb_helper_deferred_io(struct fb_info *info,
  315. struct list_head *pagelist)
  316. {
  317. }
  318. #endif
  319. static inline void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper,
  320. bool suspend)
  321. {
  322. }
  323. static inline void
  324. drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper, bool suspend)
  325. {
  326. }
  327. static inline int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  328. {
  329. return 0;
  330. }
  331. static inline int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper)
  332. {
  333. return 0;
  334. }
  335. #endif
  336. #endif