drm_vblank.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2016 Intel Corp.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef _DRM_VBLANK_H_
  24. #define _DRM_VBLANK_H_
  25. #include <linux/seqlock.h>
  26. #include <linux/idr.h>
  27. #include <linux/poll.h>
  28. #include <linux/kthread.h>
  29. #include <drm/drm_file.h>
  30. #include <drm/drm_modes.h>
  31. struct drm_device;
  32. struct drm_crtc;
  33. struct drm_vblank_work;
  34. /**
  35. * struct drm_pending_vblank_event - pending vblank event tracking
  36. */
  37. struct drm_pending_vblank_event {
  38. /**
  39. * @base: Base structure for tracking pending DRM events.
  40. */
  41. struct drm_pending_event base;
  42. /**
  43. * @pipe: drm_crtc_index() of the &drm_crtc this event is for.
  44. */
  45. unsigned int pipe;
  46. /**
  47. * @sequence: frame event should be triggered at
  48. */
  49. u64 sequence;
  50. /**
  51. * @event: Actual event which will be sent to userspace.
  52. */
  53. union {
  54. /**
  55. * @event.base: DRM event base class.
  56. */
  57. struct drm_event base;
  58. /**
  59. * @event.vbl:
  60. *
  61. * Event payload for vblank events, requested through
  62. * either the MODE_PAGE_FLIP or MODE_ATOMIC IOCTL. Also
  63. * generated by the legacy WAIT_VBLANK IOCTL, but new userspace
  64. * should use MODE_QUEUE_SEQUENCE and &event.seq instead.
  65. */
  66. struct drm_event_vblank vbl;
  67. /**
  68. * @event.seq: Event payload for the MODE_QUEUEU_SEQUENCE IOCTL.
  69. */
  70. struct drm_event_crtc_sequence seq;
  71. } event;
  72. };
  73. /**
  74. * struct drm_vblank_crtc - vblank tracking for a CRTC
  75. *
  76. * This structure tracks the vblank state for one CRTC.
  77. *
  78. * Note that for historical reasons - the vblank handling code is still shared
  79. * with legacy/non-kms drivers - this is a free-standing structure not directly
  80. * connected to &struct drm_crtc. But all public interface functions are taking
  81. * a &struct drm_crtc to hide this implementation detail.
  82. */
  83. struct drm_vblank_crtc {
  84. /**
  85. * @dev: Pointer to the &drm_device.
  86. */
  87. struct drm_device *dev;
  88. /**
  89. * @queue: Wait queue for vblank waiters.
  90. */
  91. wait_queue_head_t queue;
  92. /**
  93. * @disable_timer: Disable timer for the delayed vblank disabling
  94. * hysteresis logic. Vblank disabling is controlled through the
  95. * drm_vblank_offdelay module option and the setting of the
  96. * &drm_device.max_vblank_count value.
  97. */
  98. struct timer_list disable_timer;
  99. /**
  100. * @seqlock: Protect vblank count and time.
  101. */
  102. seqlock_t seqlock;
  103. /**
  104. * @count:
  105. *
  106. * Current software vblank counter.
  107. *
  108. * Note that for a given vblank counter value drm_crtc_handle_vblank()
  109. * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
  110. * provide a barrier: Any writes done before calling
  111. * drm_crtc_handle_vblank() will be visible to callers of the later
  112. * functions, iff the vblank count is the same or a later one.
  113. *
  114. * IMPORTANT: This guarantee requires barriers, therefor never access
  115. * this field directly. Use drm_crtc_vblank_count() instead.
  116. */
  117. atomic64_t count;
  118. /**
  119. * @time: Vblank timestamp corresponding to @count.
  120. */
  121. ktime_t time;
  122. /**
  123. * @refcount: Number of users/waiters of the vblank interrupt. Only when
  124. * this refcount reaches 0 can the hardware interrupt be disabled using
  125. * @disable_timer.
  126. */
  127. atomic_t refcount;
  128. /**
  129. * @last: Protected by &drm_device.vbl_lock, used for wraparound handling.
  130. */
  131. u32 last;
  132. /**
  133. * @max_vblank_count:
  134. *
  135. * Maximum value of the vblank registers for this crtc. This value +1
  136. * will result in a wrap-around of the vblank register. It is used
  137. * by the vblank core to handle wrap-arounds.
  138. *
  139. * If set to zero the vblank core will try to guess the elapsed vblanks
  140. * between times when the vblank interrupt is disabled through
  141. * high-precision timestamps. That approach is suffering from small
  142. * races and imprecision over longer time periods, hence exposing a
  143. * hardware vblank counter is always recommended.
  144. *
  145. * This is the runtime configurable per-crtc maximum set through
  146. * drm_crtc_set_max_vblank_count(). If this is used the driver
  147. * must leave the device wide &drm_device.max_vblank_count at zero.
  148. *
  149. * If non-zero, &drm_crtc_funcs.get_vblank_counter must be set.
  150. */
  151. u32 max_vblank_count;
  152. /**
  153. * @inmodeset: Tracks whether the vblank is disabled due to a modeset.
  154. * For legacy driver bit 2 additionally tracks whether an additional
  155. * temporary vblank reference has been acquired to paper over the
  156. * hardware counter resetting/jumping. KMS drivers should instead just
  157. * call drm_crtc_vblank_off() and drm_crtc_vblank_on(), which explicitly
  158. * save and restore the vblank count.
  159. */
  160. unsigned int inmodeset;
  161. /**
  162. * @pipe: drm_crtc_index() of the &drm_crtc corresponding to this
  163. * structure.
  164. */
  165. unsigned int pipe;
  166. /**
  167. * @framedur_ns: Frame/Field duration in ns, used by
  168. * drm_crtc_vblank_helper_get_vblank_timestamp() and computed by
  169. * drm_calc_timestamping_constants().
  170. */
  171. int framedur_ns;
  172. /**
  173. * @linedur_ns: Line duration in ns, used by
  174. * drm_crtc_vblank_helper_get_vblank_timestamp() and computed by
  175. * drm_calc_timestamping_constants().
  176. */
  177. int linedur_ns;
  178. /**
  179. * @hwmode:
  180. *
  181. * Cache of the current hardware display mode. Only valid when @enabled
  182. * is set. This is used by helpers like
  183. * drm_crtc_vblank_helper_get_vblank_timestamp(). We can't just access
  184. * the hardware mode by e.g. looking at &drm_crtc_state.adjusted_mode,
  185. * because that one is really hard to get from interrupt context.
  186. */
  187. struct drm_display_mode hwmode;
  188. /**
  189. * @enabled: Tracks the enabling state of the corresponding &drm_crtc to
  190. * avoid double-disabling and hence corrupting saved state. Needed by
  191. * drivers not using atomic KMS, since those might go through their CRTC
  192. * disabling functions multiple times.
  193. */
  194. bool enabled;
  195. /**
  196. * @worker: The &kthread_worker used for executing vblank works.
  197. */
  198. struct kthread_worker *worker;
  199. /**
  200. * @pending_work: A list of scheduled &drm_vblank_work items that are
  201. * waiting for a future vblank.
  202. */
  203. struct list_head pending_work;
  204. /**
  205. * @work_wait_queue: The wait queue used for signaling that a
  206. * &drm_vblank_work item has either finished executing, or was
  207. * cancelled.
  208. */
  209. wait_queue_head_t work_wait_queue;
  210. };
  211. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
  212. bool drm_dev_has_vblank(const struct drm_device *dev);
  213. u64 drm_crtc_vblank_count(struct drm_crtc *crtc);
  214. u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  215. ktime_t *vblanktime);
  216. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  217. struct drm_pending_vblank_event *e);
  218. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  219. struct drm_pending_vblank_event *e);
  220. void drm_vblank_set_event(struct drm_pending_vblank_event *e,
  221. u64 *seq,
  222. ktime_t *now);
  223. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
  224. bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
  225. int drm_crtc_vblank_get(struct drm_crtc *crtc);
  226. void drm_crtc_vblank_put(struct drm_crtc *crtc);
  227. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
  228. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
  229. void drm_crtc_vblank_off(struct drm_crtc *crtc);
  230. void drm_crtc_vblank_reset(struct drm_crtc *crtc);
  231. void drm_crtc_vblank_on(struct drm_crtc *crtc);
  232. u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc);
  233. void drm_vblank_restore(struct drm_device *dev, unsigned int pipe);
  234. void drm_crtc_vblank_restore(struct drm_crtc *crtc);
  235. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  236. const struct drm_display_mode *mode);
  237. wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc);
  238. void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
  239. u32 max_vblank_count);
  240. /*
  241. * Helpers for struct drm_crtc_funcs
  242. */
  243. typedef bool (*drm_vblank_get_scanout_position_func)(struct drm_crtc *crtc,
  244. bool in_vblank_irq,
  245. int *vpos, int *hpos,
  246. ktime_t *stime,
  247. ktime_t *etime,
  248. const struct drm_display_mode *mode);
  249. bool
  250. drm_crtc_vblank_helper_get_vblank_timestamp_internal(struct drm_crtc *crtc,
  251. int *max_error,
  252. ktime_t *vblank_time,
  253. bool in_vblank_irq,
  254. drm_vblank_get_scanout_position_func get_scanout_position);
  255. bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
  256. int *max_error,
  257. ktime_t *vblank_time,
  258. bool in_vblank_irq);
  259. #endif