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