drm_fb_helper.h 16 KB

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