2 * Copyright 2016 Intel Corp.
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:
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
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.
24 #ifndef _DRM_VBLANK_H_
25 #define _DRM_VBLANK_H_
27 #include <linux/seqlock.h>
28 #include <linux/idr.h>
29 #include <linux/poll.h>
31 #include <drm/drm_file.h>
32 #include <drm/drm_modes.h>
33 #include <uapi/drm/drm.h>
39 * struct drm_pending_vblank_event - pending vblank event tracking
41 struct drm_pending_vblank_event
{
43 * @base: Base structure for tracking pending DRM events.
45 struct drm_pending_event base
;
47 * @pipe: drm_crtc_index() of the &drm_crtc this event is for.
51 * @sequence: frame event should be triggered at
55 * @event: Actual event which will be sent to userspace.
58 struct drm_event base
;
59 struct drm_event_vblank vbl
;
60 struct drm_event_crtc_sequence seq
;
65 * struct drm_vblank_crtc - vblank tracking for a CRTC
67 * This structure tracks the vblank state for one CRTC.
69 * Note that for historical reasons - the vblank handling code is still shared
70 * with legacy/non-kms drivers - this is a free-standing structure not directly
71 * connected to &struct drm_crtc. But all public interface functions are taking
72 * a &struct drm_crtc to hide this implementation detail.
74 struct drm_vblank_crtc
{
76 * @dev: Pointer to the &drm_device.
78 struct drm_device
*dev
;
80 * @queue: Wait queue for vblank waiters.
82 wait_queue_head_t queue
; /**< VBLANK wait queue */
84 * @disable_timer: Disable timer for the delayed vblank disabling
85 * hysteresis logic. Vblank disabling is controlled through the
86 * drm_vblank_offdelay module option and the setting of the
87 * &drm_device.max_vblank_count value.
89 struct timer_list disable_timer
;
92 * @seqlock: Protect vblank count and time.
94 seqlock_t seqlock
; /* protects vblank count and time */
97 * @count: Current software vblank counter.
101 * @time: Vblank timestamp corresponding to @count.
106 * @refcount: Number of users/waiters of the vblank interrupt. Only when
107 * this refcount reaches 0 can the hardware interrupt be disabled using
110 atomic_t refcount
; /* number of users of vblank interruptsper crtc */
112 * @last: Protected by &drm_device.vbl_lock, used for wraparound handling.
118 * Maximum value of the vblank registers for this crtc. This value +1
119 * will result in a wrap-around of the vblank register. It is used
120 * by the vblank core to handle wrap-arounds.
122 * If set to zero the vblank core will try to guess the elapsed vblanks
123 * between times when the vblank interrupt is disabled through
124 * high-precision timestamps. That approach is suffering from small
125 * races and imprecision over longer time periods, hence exposing a
126 * hardware vblank counter is always recommended.
128 * This is the runtime configurable per-crtc maximum set through
129 * drm_crtc_set_max_vblank_count(). If this is used the driver
130 * must leave the device wide &drm_device.max_vblank_count at zero.
132 * If non-zero, &drm_crtc_funcs.get_vblank_counter must be set.
134 u32 max_vblank_count
;
136 * @inmodeset: Tracks whether the vblank is disabled due to a modeset.
137 * For legacy driver bit 2 additionally tracks whether an additional
138 * temporary vblank reference has been acquired to paper over the
139 * hardware counter resetting/jumping. KMS drivers should instead just
140 * call drm_crtc_vblank_off() and drm_crtc_vblank_on(), which explicitly
141 * save and restore the vblank count.
143 unsigned int inmodeset
; /* Display driver is setting mode */
145 * @pipe: drm_crtc_index() of the &drm_crtc corresponding to this
150 * @framedur_ns: Frame/Field duration in ns, used by
151 * drm_calc_vbltimestamp_from_scanoutpos() and computed by
152 * drm_calc_timestamping_constants().
156 * @linedur_ns: Line duration in ns, used by
157 * drm_calc_vbltimestamp_from_scanoutpos() and computed by
158 * drm_calc_timestamping_constants().
165 * Cache of the current hardware display mode. Only valid when @enabled
166 * is set. This is used by helpers like
167 * drm_calc_vbltimestamp_from_scanoutpos(). We can't just access the
168 * hardware mode by e.g. looking at &drm_crtc_state.adjusted_mode,
169 * because that one is really hard to get from interrupt context.
171 struct drm_display_mode hwmode
;
174 * @enabled: Tracks the enabling state of the corresponding &drm_crtc to
175 * avoid double-disabling and hence corrupting saved state. Needed by
176 * drivers not using atomic KMS, since those might go through their CRTC
177 * disabling functions multiple times.
182 int drm_vblank_init(struct drm_device
*dev
, unsigned int num_crtcs
);
183 u64
drm_crtc_vblank_count(struct drm_crtc
*crtc
);
184 u64
drm_crtc_vblank_count_and_time(struct drm_crtc
*crtc
,
185 ktime_t
*vblanktime
);
186 void drm_crtc_send_vblank_event(struct drm_crtc
*crtc
,
187 struct drm_pending_vblank_event
*e
);
188 void drm_crtc_arm_vblank_event(struct drm_crtc
*crtc
,
189 struct drm_pending_vblank_event
*e
);
190 void drm_vblank_set_event(struct drm_pending_vblank_event
*e
,
193 bool drm_handle_vblank(struct drm_device
*dev
, unsigned int pipe
);
194 bool drm_crtc_handle_vblank(struct drm_crtc
*crtc
);
195 int drm_crtc_vblank_get(struct drm_crtc
*crtc
);
196 void drm_crtc_vblank_put(struct drm_crtc
*crtc
);
197 void drm_wait_one_vblank(struct drm_device
*dev
, unsigned int pipe
);
198 void drm_crtc_wait_one_vblank(struct drm_crtc
*crtc
);
199 void drm_crtc_vblank_off(struct drm_crtc
*crtc
);
200 void drm_crtc_vblank_reset(struct drm_crtc
*crtc
);
201 void drm_crtc_vblank_on(struct drm_crtc
*crtc
);
202 u64
drm_crtc_accurate_vblank_count(struct drm_crtc
*crtc
);
204 bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device
*dev
,
205 unsigned int pipe
, int *max_error
,
206 ktime_t
*vblank_time
,
208 void drm_calc_timestamping_constants(struct drm_crtc
*crtc
,
209 const struct drm_display_mode
*mode
);
210 wait_queue_head_t
*drm_crtc_vblank_waitqueue(struct drm_crtc
*crtc
);
211 void drm_crtc_set_max_vblank_count(struct drm_crtc
*crtc
,
212 u32 max_vblank_count
);