1*3f2dd94aSFrançois Tigeot /* 2*3f2dd94aSFrançois Tigeot * Copyright 2016 Intel Corp. 3*3f2dd94aSFrançois Tigeot * 4*3f2dd94aSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a 5*3f2dd94aSFrançois Tigeot * copy of this software and associated documentation files (the "Software"), 6*3f2dd94aSFrançois Tigeot * to deal in the Software without restriction, including without limitation 7*3f2dd94aSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8*3f2dd94aSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the 9*3f2dd94aSFrançois Tigeot * Software is furnished to do so, subject to the following conditions: 10*3f2dd94aSFrançois Tigeot * 11*3f2dd94aSFrançois Tigeot * The above copyright notice and this permission notice (including the next 12*3f2dd94aSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the 13*3f2dd94aSFrançois Tigeot * Software. 14*3f2dd94aSFrançois Tigeot * 15*3f2dd94aSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16*3f2dd94aSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17*3f2dd94aSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18*3f2dd94aSFrançois Tigeot * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 19*3f2dd94aSFrançois Tigeot * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20*3f2dd94aSFrançois Tigeot * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21*3f2dd94aSFrançois Tigeot * OTHER DEALINGS IN THE SOFTWARE. 22*3f2dd94aSFrançois Tigeot */ 23*3f2dd94aSFrançois Tigeot 24*3f2dd94aSFrançois Tigeot #ifndef _DRM_VBLANK_H_ 25*3f2dd94aSFrançois Tigeot #define _DRM_VBLANK_H_ 26*3f2dd94aSFrançois Tigeot 27*3f2dd94aSFrançois Tigeot #include <linux/seqlock.h> 28*3f2dd94aSFrançois Tigeot #include <linux/idr.h> 29*3f2dd94aSFrançois Tigeot #include <linux/poll.h> 30*3f2dd94aSFrançois Tigeot 31*3f2dd94aSFrançois Tigeot #include <drm/drm_file.h> 32*3f2dd94aSFrançois Tigeot #include <drm/drm_modes.h> 33*3f2dd94aSFrançois Tigeot #include <uapi/drm/drm.h> 34*3f2dd94aSFrançois Tigeot 35*3f2dd94aSFrançois Tigeot struct drm_device; 36*3f2dd94aSFrançois Tigeot struct drm_crtc; 37*3f2dd94aSFrançois Tigeot 38*3f2dd94aSFrançois Tigeot /** 39*3f2dd94aSFrançois Tigeot * struct drm_pending_vblank_event - pending vblank event tracking 40*3f2dd94aSFrançois Tigeot */ 41*3f2dd94aSFrançois Tigeot struct drm_pending_vblank_event { 42*3f2dd94aSFrançois Tigeot /** 43*3f2dd94aSFrançois Tigeot * @base: Base structure for tracking pending DRM events. 44*3f2dd94aSFrançois Tigeot */ 45*3f2dd94aSFrançois Tigeot struct drm_pending_event base; 46*3f2dd94aSFrançois Tigeot /** 47*3f2dd94aSFrançois Tigeot * @pipe: drm_crtc_index() of the &drm_crtc this event is for. 48*3f2dd94aSFrançois Tigeot */ 49*3f2dd94aSFrançois Tigeot unsigned int pipe; 50*3f2dd94aSFrançois Tigeot /** 51*3f2dd94aSFrançois Tigeot * @sequence: frame event should be triggered at 52*3f2dd94aSFrançois Tigeot */ 53*3f2dd94aSFrançois Tigeot u64 sequence; 54*3f2dd94aSFrançois Tigeot /** 55*3f2dd94aSFrançois Tigeot * @event: Actual event which will be sent to userspace. 56*3f2dd94aSFrançois Tigeot */ 57*3f2dd94aSFrançois Tigeot union { 58*3f2dd94aSFrançois Tigeot struct drm_event base; 59*3f2dd94aSFrançois Tigeot struct drm_event_vblank vbl; 60*3f2dd94aSFrançois Tigeot struct drm_event_crtc_sequence seq; 61*3f2dd94aSFrançois Tigeot } event; 62*3f2dd94aSFrançois Tigeot }; 63*3f2dd94aSFrançois Tigeot 64*3f2dd94aSFrançois Tigeot /** 65*3f2dd94aSFrançois Tigeot * struct drm_vblank_crtc - vblank tracking for a CRTC 66*3f2dd94aSFrançois Tigeot * 67*3f2dd94aSFrançois Tigeot * This structure tracks the vblank state for one CRTC. 68*3f2dd94aSFrançois Tigeot * 69*3f2dd94aSFrançois Tigeot * Note that for historical reasons - the vblank handling code is still shared 70*3f2dd94aSFrançois Tigeot * with legacy/non-kms drivers - this is a free-standing structure not directly 71*3f2dd94aSFrançois Tigeot * connected to &struct drm_crtc. But all public interface functions are taking 72*3f2dd94aSFrançois Tigeot * a &struct drm_crtc to hide this implementation detail. 73*3f2dd94aSFrançois Tigeot */ 74*3f2dd94aSFrançois Tigeot struct drm_vblank_crtc { 75*3f2dd94aSFrançois Tigeot /** 76*3f2dd94aSFrançois Tigeot * @dev: Pointer to the &drm_device. 77*3f2dd94aSFrançois Tigeot */ 78*3f2dd94aSFrançois Tigeot struct drm_device *dev; 79*3f2dd94aSFrançois Tigeot /** 80*3f2dd94aSFrançois Tigeot * @queue: Wait queue for vblank waiters. 81*3f2dd94aSFrançois Tigeot */ 82*3f2dd94aSFrançois Tigeot wait_queue_head_t queue; /**< VBLANK wait queue */ 83*3f2dd94aSFrançois Tigeot /** 84*3f2dd94aSFrançois Tigeot * @disable_timer: Disable timer for the delayed vblank disabling 85*3f2dd94aSFrançois Tigeot * hysteresis logic. Vblank disabling is controlled through the 86*3f2dd94aSFrançois Tigeot * drm_vblank_offdelay module option and the setting of the 87*3f2dd94aSFrançois Tigeot * &drm_device.max_vblank_count value. 88*3f2dd94aSFrançois Tigeot */ 89*3f2dd94aSFrançois Tigeot struct timer_list disable_timer; 90*3f2dd94aSFrançois Tigeot 91*3f2dd94aSFrançois Tigeot /** 92*3f2dd94aSFrançois Tigeot * @seqlock: Protect vblank count and time. 93*3f2dd94aSFrançois Tigeot */ 94*3f2dd94aSFrançois Tigeot seqlock_t seqlock; /* protects vblank count and time */ 95*3f2dd94aSFrançois Tigeot 96*3f2dd94aSFrançois Tigeot /** 97*3f2dd94aSFrançois Tigeot * @count: Current software vblank counter. 98*3f2dd94aSFrançois Tigeot */ 99*3f2dd94aSFrançois Tigeot u64 count; 100*3f2dd94aSFrançois Tigeot /** 101*3f2dd94aSFrançois Tigeot * @time: Vblank timestamp corresponding to @count. 102*3f2dd94aSFrançois Tigeot */ 103*3f2dd94aSFrançois Tigeot ktime_t time; 104*3f2dd94aSFrançois Tigeot 105*3f2dd94aSFrançois Tigeot /** 106*3f2dd94aSFrançois Tigeot * @refcount: Number of users/waiters of the vblank interrupt. Only when 107*3f2dd94aSFrançois Tigeot * this refcount reaches 0 can the hardware interrupt be disabled using 108*3f2dd94aSFrançois Tigeot * @disable_timer. 109*3f2dd94aSFrançois Tigeot */ 110*3f2dd94aSFrançois Tigeot atomic_t refcount; /* number of users of vblank interruptsper crtc */ 111*3f2dd94aSFrançois Tigeot /** 112*3f2dd94aSFrançois Tigeot * @last: Protected by &drm_device.vbl_lock, used for wraparound handling. 113*3f2dd94aSFrançois Tigeot */ 114*3f2dd94aSFrançois Tigeot u32 last; 115*3f2dd94aSFrançois Tigeot /** 116*3f2dd94aSFrançois Tigeot * @inmodeset: Tracks whether the vblank is disabled due to a modeset. 117*3f2dd94aSFrançois Tigeot * For legacy driver bit 2 additionally tracks whether an additional 118*3f2dd94aSFrançois Tigeot * temporary vblank reference has been acquired to paper over the 119*3f2dd94aSFrançois Tigeot * hardware counter resetting/jumping. KMS drivers should instead just 120*3f2dd94aSFrançois Tigeot * call drm_crtc_vblank_off() and drm_crtc_vblank_on(), which explicitly 121*3f2dd94aSFrançois Tigeot * save and restore the vblank count. 122*3f2dd94aSFrançois Tigeot */ 123*3f2dd94aSFrançois Tigeot unsigned int inmodeset; /* Display driver is setting mode */ 124*3f2dd94aSFrançois Tigeot /** 125*3f2dd94aSFrançois Tigeot * @pipe: drm_crtc_index() of the &drm_crtc corresponding to this 126*3f2dd94aSFrançois Tigeot * structure. 127*3f2dd94aSFrançois Tigeot */ 128*3f2dd94aSFrançois Tigeot unsigned int pipe; 129*3f2dd94aSFrançois Tigeot /** 130*3f2dd94aSFrançois Tigeot * @framedur_ns: Frame/Field duration in ns, used by 131*3f2dd94aSFrançois Tigeot * drm_calc_vbltimestamp_from_scanoutpos() and computed by 132*3f2dd94aSFrançois Tigeot * drm_calc_timestamping_constants(). 133*3f2dd94aSFrançois Tigeot */ 134*3f2dd94aSFrançois Tigeot int framedur_ns; 135*3f2dd94aSFrançois Tigeot /** 136*3f2dd94aSFrançois Tigeot * @linedur_ns: Line duration in ns, used by 137*3f2dd94aSFrançois Tigeot * drm_calc_vbltimestamp_from_scanoutpos() and computed by 138*3f2dd94aSFrançois Tigeot * drm_calc_timestamping_constants(). 139*3f2dd94aSFrançois Tigeot */ 140*3f2dd94aSFrançois Tigeot int linedur_ns; 141*3f2dd94aSFrançois Tigeot 142*3f2dd94aSFrançois Tigeot /** 143*3f2dd94aSFrançois Tigeot * @hwmode: 144*3f2dd94aSFrançois Tigeot * 145*3f2dd94aSFrançois Tigeot * Cache of the current hardware display mode. Only valid when @enabled 146*3f2dd94aSFrançois Tigeot * is set. This is used by helpers like 147*3f2dd94aSFrançois Tigeot * drm_calc_vbltimestamp_from_scanoutpos(). We can't just access the 148*3f2dd94aSFrançois Tigeot * hardware mode by e.g. looking at &drm_crtc_state.adjusted_mode, 149*3f2dd94aSFrançois Tigeot * because that one is really hard to get from interrupt context. 150*3f2dd94aSFrançois Tigeot */ 151*3f2dd94aSFrançois Tigeot struct drm_display_mode hwmode; 152*3f2dd94aSFrançois Tigeot 153*3f2dd94aSFrançois Tigeot /** 154*3f2dd94aSFrançois Tigeot * @enabled: Tracks the enabling state of the corresponding &drm_crtc to 155*3f2dd94aSFrançois Tigeot * avoid double-disabling and hence corrupting saved state. Needed by 156*3f2dd94aSFrançois Tigeot * drivers not using atomic KMS, since those might go through their CRTC 157*3f2dd94aSFrançois Tigeot * disabling functions multiple times. 158*3f2dd94aSFrançois Tigeot */ 159*3f2dd94aSFrançois Tigeot bool enabled; 160*3f2dd94aSFrançois Tigeot }; 161*3f2dd94aSFrançois Tigeot 162*3f2dd94aSFrançois Tigeot int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs); 163*3f2dd94aSFrançois Tigeot u64 drm_crtc_vblank_count(struct drm_crtc *crtc); 164*3f2dd94aSFrançois Tigeot u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc, 165*3f2dd94aSFrançois Tigeot ktime_t *vblanktime); 166*3f2dd94aSFrançois Tigeot void drm_crtc_send_vblank_event(struct drm_crtc *crtc, 167*3f2dd94aSFrançois Tigeot struct drm_pending_vblank_event *e); 168*3f2dd94aSFrançois Tigeot void drm_crtc_arm_vblank_event(struct drm_crtc *crtc, 169*3f2dd94aSFrançois Tigeot struct drm_pending_vblank_event *e); 170*3f2dd94aSFrançois Tigeot void drm_vblank_set_event(struct drm_pending_vblank_event *e, 171*3f2dd94aSFrançois Tigeot u64 *seq, 172*3f2dd94aSFrançois Tigeot ktime_t *now); 173*3f2dd94aSFrançois Tigeot bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe); 174*3f2dd94aSFrançois Tigeot bool drm_crtc_handle_vblank(struct drm_crtc *crtc); 175*3f2dd94aSFrançois Tigeot int drm_crtc_vblank_get(struct drm_crtc *crtc); 176*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_put(struct drm_crtc *crtc); 177*3f2dd94aSFrançois Tigeot void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe); 178*3f2dd94aSFrançois Tigeot void drm_crtc_wait_one_vblank(struct drm_crtc *crtc); 179*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_off(struct drm_crtc *crtc); 180*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_reset(struct drm_crtc *crtc); 181*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_on(struct drm_crtc *crtc); 182*3f2dd94aSFrançois Tigeot u32 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc); 183*3f2dd94aSFrançois Tigeot 184*3f2dd94aSFrançois Tigeot bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, 185*3f2dd94aSFrançois Tigeot unsigned int pipe, int *max_error, 186*3f2dd94aSFrançois Tigeot ktime_t *vblank_time, 187*3f2dd94aSFrançois Tigeot bool in_vblank_irq); 188*3f2dd94aSFrançois Tigeot void drm_calc_timestamping_constants(struct drm_crtc *crtc, 189*3f2dd94aSFrançois Tigeot const struct drm_display_mode *mode); 190*3f2dd94aSFrançois Tigeot wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc); 191*3f2dd94aSFrançois Tigeot #endif 192