drm_vblank.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_config - vblank configuration for a CRTC
  75. */
  76. struct drm_vblank_crtc_config {
  77. /**
  78. * @offdelay_ms: Vblank off delay in ms, used to determine how long
  79. * &drm_vblank_crtc.disable_timer waits before disabling.
  80. *
  81. * Defaults to the value of drm_vblank_offdelay in drm_crtc_vblank_on().
  82. */
  83. int offdelay_ms;
  84. /**
  85. * @disable_immediate: See &drm_device.vblank_disable_immediate
  86. * for the exact semantics of immediate vblank disabling.
  87. *
  88. * Additionally, this tracks the disable immediate value per crtc, just
  89. * in case it needs to differ from the default value for a given device.
  90. *
  91. * Defaults to the value of &drm_device.vblank_disable_immediate in
  92. * drm_crtc_vblank_on().
  93. */
  94. bool disable_immediate;
  95. };
  96. /**
  97. * struct drm_vblank_crtc - vblank tracking for a CRTC
  98. *
  99. * This structure tracks the vblank state for one CRTC.
  100. *
  101. * Note that for historical reasons - the vblank handling code is still shared
  102. * with legacy/non-kms drivers - this is a free-standing structure not directly
  103. * connected to &struct drm_crtc. But all public interface functions are taking
  104. * a &struct drm_crtc to hide this implementation detail.
  105. */
  106. struct drm_vblank_crtc {
  107. /**
  108. * @dev: Pointer to the &drm_device.
  109. */
  110. struct drm_device *dev;
  111. /**
  112. * @queue: Wait queue for vblank waiters.
  113. */
  114. wait_queue_head_t queue;
  115. /**
  116. * @disable_timer: Disable timer for the delayed vblank disabling
  117. * hysteresis logic. Vblank disabling is controlled through
  118. * &drm_vblank_crtc_config.offdelay_ms and the setting of the
  119. * &drm_device.max_vblank_count value.
  120. */
  121. struct timer_list disable_timer;
  122. /**
  123. * @seqlock: Protect vblank count and time.
  124. */
  125. seqlock_t seqlock;
  126. /**
  127. * @count:
  128. *
  129. * Current software vblank counter.
  130. *
  131. * Note that for a given vblank counter value drm_crtc_handle_vblank()
  132. * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
  133. * provide a barrier: Any writes done before calling
  134. * drm_crtc_handle_vblank() will be visible to callers of the later
  135. * functions, iff the vblank count is the same or a later one.
  136. *
  137. * IMPORTANT: This guarantee requires barriers, therefor never access
  138. * this field directly. Use drm_crtc_vblank_count() instead.
  139. */
  140. atomic64_t count;
  141. /**
  142. * @time: Vblank timestamp corresponding to @count.
  143. */
  144. ktime_t time;
  145. /**
  146. * @refcount: Number of users/waiters of the vblank interrupt. Only when
  147. * this refcount reaches 0 can the hardware interrupt be disabled using
  148. * @disable_timer.
  149. */
  150. atomic_t refcount;
  151. /**
  152. * @last: Protected by &drm_device.vbl_lock, used for wraparound handling.
  153. */
  154. u32 last;
  155. /**
  156. * @max_vblank_count:
  157. *
  158. * Maximum value of the vblank registers for this crtc. This value +1
  159. * will result in a wrap-around of the vblank register. It is used
  160. * by the vblank core to handle wrap-arounds.
  161. *
  162. * If set to zero the vblank core will try to guess the elapsed vblanks
  163. * between times when the vblank interrupt is disabled through
  164. * high-precision timestamps. That approach is suffering from small
  165. * races and imprecision over longer time periods, hence exposing a
  166. * hardware vblank counter is always recommended.
  167. *
  168. * This is the runtime configurable per-crtc maximum set through
  169. * drm_crtc_set_max_vblank_count(). If this is used the driver
  170. * must leave the device wide &drm_device.max_vblank_count at zero.
  171. *
  172. * If non-zero, &drm_crtc_funcs.get_vblank_counter must be set.
  173. */
  174. u32 max_vblank_count;
  175. /**
  176. * @inmodeset: Tracks whether the vblank is disabled due to a modeset.
  177. * For legacy driver bit 2 additionally tracks whether an additional
  178. * temporary vblank reference has been acquired to paper over the
  179. * hardware counter resetting/jumping. KMS drivers should instead just
  180. * call drm_crtc_vblank_off() and drm_crtc_vblank_on(), which explicitly
  181. * save and restore the vblank count.
  182. */
  183. unsigned int inmodeset;
  184. /**
  185. * @pipe: drm_crtc_index() of the &drm_crtc corresponding to this
  186. * structure.
  187. */
  188. unsigned int pipe;
  189. /**
  190. * @framedur_ns: Frame/Field duration in ns, used by
  191. * drm_crtc_vblank_helper_get_vblank_timestamp() and computed by
  192. * drm_calc_timestamping_constants().
  193. */
  194. int framedur_ns;
  195. /**
  196. * @linedur_ns: Line duration in ns, used by
  197. * drm_crtc_vblank_helper_get_vblank_timestamp() and computed by
  198. * drm_calc_timestamping_constants().
  199. */
  200. int linedur_ns;
  201. /**
  202. * @hwmode:
  203. *
  204. * Cache of the current hardware display mode. Only valid when @enabled
  205. * is set. This is used by helpers like
  206. * drm_crtc_vblank_helper_get_vblank_timestamp(). We can't just access
  207. * the hardware mode by e.g. looking at &drm_crtc_state.adjusted_mode,
  208. * because that one is really hard to get from interrupt context.
  209. */
  210. struct drm_display_mode hwmode;
  211. /**
  212. * @config: Stores vblank configuration values for a given CRTC.
  213. * Also, see drm_crtc_vblank_on_config().
  214. */
  215. struct drm_vblank_crtc_config config;
  216. /**
  217. * @enabled: Tracks the enabling state of the corresponding &drm_crtc to
  218. * avoid double-disabling and hence corrupting saved state. Needed by
  219. * drivers not using atomic KMS, since those might go through their CRTC
  220. * disabling functions multiple times.
  221. */
  222. bool enabled;
  223. /**
  224. * @worker: The &kthread_worker used for executing vblank works.
  225. */
  226. struct kthread_worker *worker;
  227. /**
  228. * @pending_work: A list of scheduled &drm_vblank_work items that are
  229. * waiting for a future vblank.
  230. */
  231. struct list_head pending_work;
  232. /**
  233. * @work_wait_queue: The wait queue used for signaling that a
  234. * &drm_vblank_work item has either finished executing, or was
  235. * cancelled.
  236. */
  237. wait_queue_head_t work_wait_queue;
  238. };
  239. struct drm_vblank_crtc *drm_crtc_vblank_crtc(struct drm_crtc *crtc);
  240. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
  241. bool drm_dev_has_vblank(const struct drm_device *dev);
  242. u64 drm_crtc_vblank_count(struct drm_crtc *crtc);
  243. u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  244. ktime_t *vblanktime);
  245. int drm_crtc_next_vblank_start(struct drm_crtc *crtc, ktime_t *vblanktime);
  246. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  247. struct drm_pending_vblank_event *e);
  248. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  249. struct drm_pending_vblank_event *e);
  250. void drm_vblank_set_event(struct drm_pending_vblank_event *e,
  251. u64 *seq,
  252. ktime_t *now);
  253. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
  254. bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
  255. int drm_crtc_vblank_get(struct drm_crtc *crtc);
  256. void drm_crtc_vblank_put(struct drm_crtc *crtc);
  257. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
  258. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
  259. void drm_crtc_vblank_off(struct drm_crtc *crtc);
  260. void drm_crtc_vblank_reset(struct drm_crtc *crtc);
  261. void drm_crtc_vblank_on_config(struct drm_crtc *crtc,
  262. const struct drm_vblank_crtc_config *config);
  263. void drm_crtc_vblank_on(struct drm_crtc *crtc);
  264. u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc);
  265. void drm_crtc_vblank_restore(struct drm_crtc *crtc);
  266. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  267. const struct drm_display_mode *mode);
  268. wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc);
  269. void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
  270. u32 max_vblank_count);
  271. /*
  272. * Helpers for struct drm_crtc_funcs
  273. */
  274. typedef bool (*drm_vblank_get_scanout_position_func)(struct drm_crtc *crtc,
  275. bool in_vblank_irq,
  276. int *vpos, int *hpos,
  277. ktime_t *stime,
  278. ktime_t *etime,
  279. const struct drm_display_mode *mode);
  280. bool
  281. drm_crtc_vblank_helper_get_vblank_timestamp_internal(struct drm_crtc *crtc,
  282. int *max_error,
  283. ktime_t *vblank_time,
  284. bool in_vblank_irq,
  285. drm_vblank_get_scanout_position_func get_scanout_position);
  286. bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
  287. int *max_error,
  288. ktime_t *vblank_time,
  289. bool in_vblank_irq);
  290. #endif