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