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