Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 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 | |
| 24 | #ifndef _DRM_VBLANK_H_ |
| 25 | #define _DRM_VBLANK_H_ |
| 26 | |
| 27 | #include <linux/seqlock.h> |
| 28 | #include <linux/idr.h> |
| 29 | #include <linux/poll.h> |
Lyude Paul | 5e6c2b4 | 2020-04-17 15:33:13 -0400 | [diff] [blame] | 30 | #include <linux/kthread.h> |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 31 | |
| 32 | #include <drm/drm_file.h> |
| 33 | #include <drm/drm_modes.h> |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 34 | |
| 35 | struct drm_device; |
| 36 | struct drm_crtc; |
Lyude Paul | 5e6c2b4 | 2020-04-17 15:33:13 -0400 | [diff] [blame] | 37 | struct drm_vblank_work; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * struct drm_pending_vblank_event - pending vblank event tracking |
| 41 | */ |
| 42 | struct drm_pending_vblank_event { |
| 43 | /** |
| 44 | * @base: Base structure for tracking pending DRM events. |
| 45 | */ |
| 46 | struct drm_pending_event base; |
| 47 | /** |
| 48 | * @pipe: drm_crtc_index() of the &drm_crtc this event is for. |
| 49 | */ |
| 50 | unsigned int pipe; |
| 51 | /** |
Keith Packard | 570e869 | 2017-10-12 11:57:49 -0700 | [diff] [blame] | 52 | * @sequence: frame event should be triggered at |
| 53 | */ |
| 54 | u64 sequence; |
| 55 | /** |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 56 | * @event: Actual event which will be sent to userspace. |
| 57 | */ |
Keith Packard | bd386e5 | 2017-07-05 14:34:23 -0700 | [diff] [blame] | 58 | union { |
Daniel Vetter | 83a7dff | 2018-02-19 23:53:56 +0100 | [diff] [blame] | 59 | /** |
| 60 | * @event.base: DRM event base class. |
| 61 | */ |
Keith Packard | bd386e5 | 2017-07-05 14:34:23 -0700 | [diff] [blame] | 62 | struct drm_event base; |
Daniel Vetter | 83a7dff | 2018-02-19 23:53:56 +0100 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * @event.vbl: |
| 66 | * |
| 67 | * Event payload for vblank events, requested through |
| 68 | * either the MODE_PAGE_FLIP or MODE_ATOMIC IOCTL. Also |
| 69 | * generated by the legacy WAIT_VBLANK IOCTL, but new userspace |
| 70 | * should use MODE_QUEUE_SEQUENCE and &event.seq instead. |
| 71 | */ |
Keith Packard | bd386e5 | 2017-07-05 14:34:23 -0700 | [diff] [blame] | 72 | struct drm_event_vblank vbl; |
Daniel Vetter | 83a7dff | 2018-02-19 23:53:56 +0100 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * @event.seq: Event payload for the MODE_QUEUEU_SEQUENCE IOCTL. |
| 76 | */ |
Keith Packard | 3064abf | 2017-06-29 22:49:31 -0700 | [diff] [blame] | 77 | struct drm_event_crtc_sequence seq; |
Keith Packard | bd386e5 | 2017-07-05 14:34:23 -0700 | [diff] [blame] | 78 | } event; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | /** |
| 82 | * struct drm_vblank_crtc - vblank tracking for a CRTC |
| 83 | * |
| 84 | * This structure tracks the vblank state for one CRTC. |
| 85 | * |
| 86 | * Note that for historical reasons - the vblank handling code is still shared |
| 87 | * with legacy/non-kms drivers - this is a free-standing structure not directly |
| 88 | * connected to &struct drm_crtc. But all public interface functions are taking |
| 89 | * a &struct drm_crtc to hide this implementation detail. |
| 90 | */ |
| 91 | struct drm_vblank_crtc { |
| 92 | /** |
| 93 | * @dev: Pointer to the &drm_device. |
| 94 | */ |
| 95 | struct drm_device *dev; |
| 96 | /** |
| 97 | * @queue: Wait queue for vblank waiters. |
| 98 | */ |
Daniel Vetter | 9e37ee7 | 2018-10-05 09:36:36 +0200 | [diff] [blame] | 99 | wait_queue_head_t queue; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 100 | /** |
| 101 | * @disable_timer: Disable timer for the delayed vblank disabling |
| 102 | * hysteresis logic. Vblank disabling is controlled through the |
| 103 | * drm_vblank_offdelay module option and the setting of the |
| 104 | * &drm_device.max_vblank_count value. |
| 105 | */ |
| 106 | struct timer_list disable_timer; |
| 107 | |
| 108 | /** |
| 109 | * @seqlock: Protect vblank count and time. |
| 110 | */ |
Daniel Vetter | 9e37ee7 | 2018-10-05 09:36:36 +0200 | [diff] [blame] | 111 | seqlock_t seqlock; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 112 | |
| 113 | /** |
Daniel Vetter | bd7e3f3 | 2019-07-23 15:13:37 +0200 | [diff] [blame] | 114 | * @count: |
| 115 | * |
| 116 | * Current software vblank counter. |
| 117 | * |
| 118 | * Note that for a given vblank counter value drm_crtc_handle_vblank() |
| 119 | * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time() |
| 120 | * provide a barrier: Any writes done before calling |
| 121 | * drm_crtc_handle_vblank() will be visible to callers of the later |
| 122 | * functions, iff the vblank count is the same or a later one. |
| 123 | * |
| 124 | * IMPORTANT: This guarantee requires barriers, therefor never access |
| 125 | * this field directly. Use drm_crtc_vblank_count() instead. |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 126 | */ |
Daniel Vetter | bd7e3f3 | 2019-07-23 15:13:37 +0200 | [diff] [blame] | 127 | atomic64_t count; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 128 | /** |
| 129 | * @time: Vblank timestamp corresponding to @count. |
| 130 | */ |
Arnd Bergmann | 67680d3 | 2017-10-11 17:20:12 +0200 | [diff] [blame] | 131 | ktime_t time; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 132 | |
| 133 | /** |
| 134 | * @refcount: Number of users/waiters of the vblank interrupt. Only when |
| 135 | * this refcount reaches 0 can the hardware interrupt be disabled using |
| 136 | * @disable_timer. |
| 137 | */ |
Daniel Vetter | 9e37ee7 | 2018-10-05 09:36:36 +0200 | [diff] [blame] | 138 | atomic_t refcount; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 139 | /** |
| 140 | * @last: Protected by &drm_device.vbl_lock, used for wraparound handling. |
| 141 | */ |
| 142 | u32 last; |
| 143 | /** |
Ville Syrjälä | ed20151 | 2018-11-27 20:20:04 +0200 | [diff] [blame] | 144 | * @max_vblank_count: |
| 145 | * |
| 146 | * Maximum value of the vblank registers for this crtc. This value +1 |
| 147 | * will result in a wrap-around of the vblank register. It is used |
| 148 | * by the vblank core to handle wrap-arounds. |
| 149 | * |
| 150 | * If set to zero the vblank core will try to guess the elapsed vblanks |
| 151 | * between times when the vblank interrupt is disabled through |
| 152 | * high-precision timestamps. That approach is suffering from small |
| 153 | * races and imprecision over longer time periods, hence exposing a |
| 154 | * hardware vblank counter is always recommended. |
| 155 | * |
| 156 | * This is the runtime configurable per-crtc maximum set through |
| 157 | * drm_crtc_set_max_vblank_count(). If this is used the driver |
| 158 | * must leave the device wide &drm_device.max_vblank_count at zero. |
| 159 | * |
| 160 | * If non-zero, &drm_crtc_funcs.get_vblank_counter must be set. |
| 161 | */ |
| 162 | u32 max_vblank_count; |
| 163 | /** |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 164 | * @inmodeset: Tracks whether the vblank is disabled due to a modeset. |
| 165 | * For legacy driver bit 2 additionally tracks whether an additional |
| 166 | * temporary vblank reference has been acquired to paper over the |
| 167 | * hardware counter resetting/jumping. KMS drivers should instead just |
| 168 | * call drm_crtc_vblank_off() and drm_crtc_vblank_on(), which explicitly |
| 169 | * save and restore the vblank count. |
| 170 | */ |
Daniel Vetter | 9e37ee7 | 2018-10-05 09:36:36 +0200 | [diff] [blame] | 171 | unsigned int inmodeset; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 172 | /** |
| 173 | * @pipe: drm_crtc_index() of the &drm_crtc corresponding to this |
| 174 | * structure. |
| 175 | */ |
| 176 | unsigned int pipe; |
| 177 | /** |
| 178 | * @framedur_ns: Frame/Field duration in ns, used by |
Thomas Zimmermann | 7fe3f0d | 2020-01-23 14:59:24 +0100 | [diff] [blame] | 179 | * drm_crtc_vblank_helper_get_vblank_timestamp() and computed by |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 180 | * drm_calc_timestamping_constants(). |
| 181 | */ |
| 182 | int framedur_ns; |
| 183 | /** |
| 184 | * @linedur_ns: Line duration in ns, used by |
Thomas Zimmermann | 7fe3f0d | 2020-01-23 14:59:24 +0100 | [diff] [blame] | 185 | * drm_crtc_vblank_helper_get_vblank_timestamp() and computed by |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 186 | * drm_calc_timestamping_constants(). |
| 187 | */ |
| 188 | int linedur_ns; |
| 189 | |
| 190 | /** |
| 191 | * @hwmode: |
| 192 | * |
| 193 | * Cache of the current hardware display mode. Only valid when @enabled |
| 194 | * is set. This is used by helpers like |
Thomas Zimmermann | 7fe3f0d | 2020-01-23 14:59:24 +0100 | [diff] [blame] | 195 | * drm_crtc_vblank_helper_get_vblank_timestamp(). We can't just access |
| 196 | * the hardware mode by e.g. looking at &drm_crtc_state.adjusted_mode, |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 197 | * because that one is really hard to get from interrupt context. |
| 198 | */ |
| 199 | struct drm_display_mode hwmode; |
| 200 | |
| 201 | /** |
| 202 | * @enabled: Tracks the enabling state of the corresponding &drm_crtc to |
| 203 | * avoid double-disabling and hence corrupting saved state. Needed by |
| 204 | * drivers not using atomic KMS, since those might go through their CRTC |
| 205 | * disabling functions multiple times. |
| 206 | */ |
| 207 | bool enabled; |
Lyude Paul | 5e6c2b4 | 2020-04-17 15:33:13 -0400 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * @worker: The &kthread_worker used for executing vblank works. |
| 211 | */ |
| 212 | struct kthread_worker *worker; |
| 213 | |
| 214 | /** |
| 215 | * @pending_work: A list of scheduled &drm_vblank_work items that are |
| 216 | * waiting for a future vblank. |
| 217 | */ |
| 218 | struct list_head pending_work; |
| 219 | |
| 220 | /** |
| 221 | * @work_wait_queue: The wait queue used for signaling that a |
| 222 | * &drm_vblank_work item has either finished executing, or was |
| 223 | * cancelled. |
| 224 | */ |
| 225 | wait_queue_head_t work_wait_queue; |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs); |
Thomas Zimmermann | 7beb691 | 2020-01-29 13:05:17 +0100 | [diff] [blame] | 229 | bool drm_dev_has_vblank(const struct drm_device *dev); |
Keith Packard | 570e869 | 2017-10-12 11:57:49 -0700 | [diff] [blame] | 230 | u64 drm_crtc_vblank_count(struct drm_crtc *crtc); |
| 231 | u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, |
Arnd Bergmann | 67680d3 | 2017-10-11 17:20:12 +0200 | [diff] [blame] | 232 | ktime_t *vblanktime); |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 233 | void drm_crtc_send_vblank_event(struct drm_crtc *crtc, |
| 234 | struct drm_pending_vblank_event *e); |
| 235 | void drm_crtc_arm_vblank_event(struct drm_crtc *crtc, |
| 236 | struct drm_pending_vblank_event *e); |
Keith Packard | bd386e5 | 2017-07-05 14:34:23 -0700 | [diff] [blame] | 237 | void drm_vblank_set_event(struct drm_pending_vblank_event *e, |
| 238 | u64 *seq, |
| 239 | ktime_t *now); |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 240 | bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe); |
| 241 | bool drm_crtc_handle_vblank(struct drm_crtc *crtc); |
| 242 | int drm_crtc_vblank_get(struct drm_crtc *crtc); |
| 243 | void drm_crtc_vblank_put(struct drm_crtc *crtc); |
| 244 | void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe); |
| 245 | void drm_crtc_wait_one_vblank(struct drm_crtc *crtc); |
| 246 | void drm_crtc_vblank_off(struct drm_crtc *crtc); |
| 247 | void drm_crtc_vblank_reset(struct drm_crtc *crtc); |
| 248 | void drm_crtc_vblank_on(struct drm_crtc *crtc); |
Dhinakaran Pandiyan | 3b765c0 | 2018-02-02 21:12:53 -0800 | [diff] [blame] | 249 | u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc); |
Dhinakaran Pandiyan | d0bb96b | 2018-02-02 21:13:01 -0800 | [diff] [blame] | 250 | void drm_vblank_restore(struct drm_device *dev, unsigned int pipe); |
| 251 | void drm_crtc_vblank_restore(struct drm_crtc *crtc); |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 252 | |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 253 | void drm_calc_timestamping_constants(struct drm_crtc *crtc, |
| 254 | const struct drm_display_mode *mode); |
| 255 | wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc); |
Ville Syrjälä | ed20151 | 2018-11-27 20:20:04 +0200 | [diff] [blame] | 256 | void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc, |
| 257 | u32 max_vblank_count); |
Thomas Zimmermann | f1e2b63 | 2020-01-23 14:59:23 +0100 | [diff] [blame] | 258 | |
Thomas Zimmermann | 7fe3f0d | 2020-01-23 14:59:24 +0100 | [diff] [blame] | 259 | /* |
| 260 | * Helpers for struct drm_crtc_funcs |
| 261 | */ |
| 262 | |
Thomas Zimmermann | f1e2b63 | 2020-01-23 14:59:23 +0100 | [diff] [blame] | 263 | typedef bool (*drm_vblank_get_scanout_position_func)(struct drm_crtc *crtc, |
| 264 | bool in_vblank_irq, |
| 265 | int *vpos, int *hpos, |
| 266 | ktime_t *stime, |
| 267 | ktime_t *etime, |
| 268 | const struct drm_display_mode *mode); |
| 269 | |
Thomas Zimmermann | f1e2b63 | 2020-01-23 14:59:23 +0100 | [diff] [blame] | 270 | bool |
| 271 | drm_crtc_vblank_helper_get_vblank_timestamp_internal(struct drm_crtc *crtc, |
| 272 | int *max_error, |
| 273 | ktime_t *vblank_time, |
| 274 | bool in_vblank_irq, |
Thomas Zimmermann | 48e678076 | 2020-01-23 14:59:43 +0100 | [diff] [blame] | 275 | drm_vblank_get_scanout_position_func get_scanout_position); |
Thomas Zimmermann | 7fe3f0d | 2020-01-23 14:59:24 +0100 | [diff] [blame] | 276 | bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc, |
| 277 | int *max_error, |
| 278 | ktime_t *vblank_time, |
| 279 | bool in_vblank_irq); |
Thomas Zimmermann | f1e2b63 | 2020-01-23 14:59:23 +0100 | [diff] [blame] | 280 | |
Daniel Vetter | 3ed4351 | 2017-05-31 11:21:46 +0200 | [diff] [blame] | 281 | #endif |