drm_fb_helper.h 15 KB

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