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