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