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