drm_vblank.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <drm/drm_file.h>
  29. #include <drm/drm_modes.h>
  30. #include <uapi/drm/drm.h>
  31. struct drm_device;
  32. struct drm_crtc;
  33. /**
  34. * struct drm_pending_vblank_event - pending vblank event tracking
  35. */
  36. struct drm_pending_vblank_event {
  37. /**
  38. * @base: Base structure for tracking pending DRM events.
  39. */
  40. struct drm_pending_event base;
  41. /**
  42. * @pipe: drm_crtc_index() of the &drm_crtc this event is for.
  43. */
  44. unsigned int pipe;
  45. /**
  46. * @sequence: frame event should be triggered at
  47. */
  48. u64 sequence;
  49. /**
  50. * @event: Actual event which will be sent to userspace.
  51. */
  52. union {
  53. /**
  54. * @event.base: DRM event base class.
  55. */
  56. struct drm_event base;
  57. /**
  58. * @event.vbl:
  59. *
  60. * Event payload for vblank events, requested through
  61. * either the MODE_PAGE_FLIP or MODE_ATOMIC IOCTL. Also
  62. * generated by the legacy WAIT_VBLANK IOCTL, but new userspace
  63. * should use MODE_QUEUE_SEQUENCE and &event.seq instead.
  64. */
  65. struct drm_event_vblank vbl;
  66. /**
  67. * @event.seq: Event payload for the MODE_QUEUEU_SEQUENCE IOCTL.
  68. */
  69. struct drm_event_crtc_sequence seq;
  70. } event;
  71. };
  72. /**
  73. * struct drm_vblank_crtc - vblank tracking for a CRTC
  74. *
  75. * This structure tracks the vblank state for one CRTC.
  76. *
  77. * Note that for historical reasons - the vblank handling code is still shared
  78. * with legacy/non-kms drivers - this is a free-standing structure not directly
  79. * connected to &struct drm_crtc. But all public interface functions are taking
  80. * a &struct drm_crtc to hide this implementation detail.
  81. */
  82. struct drm_vblank_crtc {
  83. /**
  84. * @dev: Pointer to the &drm_device.
  85. */
  86. struct drm_device *dev;
  87. /**
  88. * @queue: Wait queue for vblank waiters.
  89. */
  90. wait_queue_head_t queue; /**< VBLANK wait queue */
  91. /**
  92. * @disable_timer: Disable timer for the delayed vblank disabling
  93. * hysteresis logic. Vblank disabling is controlled through the
  94. * drm_vblank_offdelay module option and the setting of the
  95. * &drm_device.max_vblank_count value.
  96. */
  97. struct timer_list disable_timer;
  98. /**
  99. * @seqlock: Protect vblank count and time.
  100. */
  101. seqlock_t seqlock; /* protects vblank count and time */
  102. /**
  103. * @count: Current software vblank counter.
  104. */
  105. u64 count;
  106. /**
  107. * @time: Vblank timestamp corresponding to @count.
  108. */
  109. ktime_t time;
  110. /**
  111. * @refcount: Number of users/waiters of the vblank interrupt. Only when
  112. * this refcount reaches 0 can the hardware interrupt be disabled using
  113. * @disable_timer.
  114. */
  115. atomic_t refcount; /* number of users of vblank interruptsper crtc */
  116. /**
  117. * @last: Protected by &drm_device.vbl_lock, used for wraparound handling.
  118. */
  119. u32 last;
  120. /**
  121. * @max_vblank_count:
  122. *
  123. * Maximum value of the vblank registers for this crtc. This value +1
  124. * will result in a wrap-around of the vblank register. It is used
  125. * by the vblank core to handle wrap-arounds.
  126. *
  127. * If set to zero the vblank core will try to guess the elapsed vblanks
  128. * between times when the vblank interrupt is disabled through
  129. * high-precision timestamps. That approach is suffering from small
  130. * races and imprecision over longer time periods, hence exposing a
  131. * hardware vblank counter is always recommended.
  132. *
  133. * This is the runtime configurable per-crtc maximum set through
  134. * drm_crtc_set_max_vblank_count(). If this is used the driver
  135. * must leave the device wide &drm_device.max_vblank_count at zero.
  136. *
  137. * If non-zero, &drm_crtc_funcs.get_vblank_counter must be set.
  138. */
  139. u32 max_vblank_count;
  140. /**
  141. * @inmodeset: Tracks whether the vblank is disabled due to a modeset.
  142. * For legacy driver bit 2 additionally tracks whether an additional
  143. * temporary vblank reference has been acquired to paper over the
  144. * hardware counter resetting/jumping. KMS drivers should instead just
  145. * call drm_crtc_vblank_off() and drm_crtc_vblank_on(), which explicitly
  146. * save and restore the vblank count.
  147. */
  148. unsigned int inmodeset; /* Display driver is setting mode */
  149. /**
  150. * @pipe: drm_crtc_index() of the &drm_crtc corresponding to this
  151. * structure.
  152. */
  153. unsigned int pipe;
  154. /**
  155. * @framedur_ns: Frame/Field duration in ns, used by
  156. * drm_calc_vbltimestamp_from_scanoutpos() and computed by
  157. * drm_calc_timestamping_constants().
  158. */
  159. int framedur_ns;
  160. /**
  161. * @linedur_ns: Line duration in ns, used by
  162. * drm_calc_vbltimestamp_from_scanoutpos() and computed by
  163. * drm_calc_timestamping_constants().
  164. */
  165. int linedur_ns;
  166. /**
  167. * @hwmode:
  168. *
  169. * Cache of the current hardware display mode. Only valid when @enabled
  170. * is set. This is used by helpers like
  171. * drm_calc_vbltimestamp_from_scanoutpos(). We can't just access the
  172. * hardware mode by e.g. looking at &drm_crtc_state.adjusted_mode,
  173. * because that one is really hard to get from interrupt context.
  174. */
  175. struct drm_display_mode hwmode;
  176. /**
  177. * @enabled: Tracks the enabling state of the corresponding &drm_crtc to
  178. * avoid double-disabling and hence corrupting saved state. Needed by
  179. * drivers not using atomic KMS, since those might go through their CRTC
  180. * disabling functions multiple times.
  181. */
  182. bool enabled;
  183. };
  184. int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
  185. u64 drm_crtc_vblank_count(struct drm_crtc *crtc);
  186. u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
  187. ktime_t *vblanktime);
  188. void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
  189. struct drm_pending_vblank_event *e);
  190. void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
  191. struct drm_pending_vblank_event *e);
  192. void drm_vblank_set_event(struct drm_pending_vblank_event *e,
  193. u64 *seq,
  194. ktime_t *now);
  195. bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
  196. bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
  197. int drm_crtc_vblank_get(struct drm_crtc *crtc);
  198. void drm_crtc_vblank_put(struct drm_crtc *crtc);
  199. void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
  200. void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
  201. void drm_crtc_vblank_off(struct drm_crtc *crtc);
  202. void drm_crtc_vblank_reset(struct drm_crtc *crtc);
  203. void drm_crtc_vblank_on(struct drm_crtc *crtc);
  204. u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc);
  205. void drm_vblank_restore(struct drm_device *dev, unsigned int pipe);
  206. void drm_crtc_vblank_restore(struct drm_crtc *crtc);
  207. bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
  208. unsigned int pipe, int *max_error,
  209. ktime_t *vblank_time,
  210. bool in_vblank_irq);
  211. void drm_calc_timestamping_constants(struct drm_crtc *crtc,
  212. const struct drm_display_mode *mode);
  213. wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc);
  214. void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
  215. u32 max_vblank_count);
  216. #endif