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