drm_fb_helper.h 17 KB

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