drm_vblank.h 11 KB

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