blob: 848b463a0af516ebd9894cf7917cff8eeb2cde9e [file] [log] [blame]
Daniel Vetter3ed43512017-05-31 11:21:46 +02001/*
2 * Copyright 2016 Intel Corp.
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24#ifndef _DRM_VBLANK_H_
25#define _DRM_VBLANK_H_
26
27#include <linux/seqlock.h>
28#include <linux/idr.h>
29#include <linux/poll.h>
30
31#include <drm/drm_file.h>
32#include <drm/drm_modes.h>
33#include <uapi/drm/drm.h>
34
35struct drm_device;
36struct drm_crtc;
37
38/**
39 * struct drm_pending_vblank_event - pending vblank event tracking
40 */
41struct drm_pending_vblank_event {
42 /**
43 * @base: Base structure for tracking pending DRM events.
44 */
45 struct drm_pending_event base;
46 /**
47 * @pipe: drm_crtc_index() of the &drm_crtc this event is for.
48 */
49 unsigned int pipe;
50 /**
Keith Packard570e8692017-10-12 11:57:49 -070051 * @sequence: frame event should be triggered at
52 */
53 u64 sequence;
54 /**
Daniel Vetter3ed43512017-05-31 11:21:46 +020055 * @event: Actual event which will be sent to userspace.
56 */
Keith Packardbd386e52017-07-05 14:34:23 -070057 union {
58 struct drm_event base;
59 struct drm_event_vblank vbl;
Keith Packard3064abf2017-06-29 22:49:31 -070060 struct drm_event_crtc_sequence seq;
Keith Packardbd386e52017-07-05 14:34:23 -070061 } event;
Daniel Vetter3ed43512017-05-31 11:21:46 +020062};
63
64/**
65 * struct drm_vblank_crtc - vblank tracking for a CRTC
66 *
67 * This structure tracks the vblank state for one CRTC.
68 *
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.
73 */
74struct drm_vblank_crtc {
75 /**
76 * @dev: Pointer to the &drm_device.
77 */
78 struct drm_device *dev;
79 /**
80 * @queue: Wait queue for vblank waiters.
81 */
82 wait_queue_head_t queue; /**< VBLANK wait queue */
83 /**
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.
88 */
89 struct timer_list disable_timer;
90
91 /**
92 * @seqlock: Protect vblank count and time.
93 */
94 seqlock_t seqlock; /* protects vblank count and time */
95
96 /**
97 * @count: Current software vblank counter.
98 */
Keith Packard570e8692017-10-12 11:57:49 -070099 u64 count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200100 /**
101 * @time: Vblank timestamp corresponding to @count.
102 */
Arnd Bergmann67680d32017-10-11 17:20:12 +0200103 ktime_t time;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200104
105 /**
106 * @refcount: Number of users/waiters of the vblank interrupt. Only when
107 * this refcount reaches 0 can the hardware interrupt be disabled using
108 * @disable_timer.
109 */
110 atomic_t refcount; /* number of users of vblank interruptsper crtc */
111 /**
112 * @last: Protected by &drm_device.vbl_lock, used for wraparound handling.
113 */
114 u32 last;
115 /**
116 * @inmodeset: Tracks whether the vblank is disabled due to a modeset.
117 * For legacy driver bit 2 additionally tracks whether an additional
118 * temporary vblank reference has been acquired to paper over the
119 * hardware counter resetting/jumping. KMS drivers should instead just
120 * call drm_crtc_vblank_off() and drm_crtc_vblank_on(), which explicitly
121 * save and restore the vblank count.
122 */
123 unsigned int inmodeset; /* Display driver is setting mode */
124 /**
125 * @pipe: drm_crtc_index() of the &drm_crtc corresponding to this
126 * structure.
127 */
128 unsigned int pipe;
129 /**
130 * @framedur_ns: Frame/Field duration in ns, used by
131 * drm_calc_vbltimestamp_from_scanoutpos() and computed by
132 * drm_calc_timestamping_constants().
133 */
134 int framedur_ns;
135 /**
136 * @linedur_ns: Line duration in ns, used by
137 * drm_calc_vbltimestamp_from_scanoutpos() and computed by
138 * drm_calc_timestamping_constants().
139 */
140 int linedur_ns;
141
142 /**
143 * @hwmode:
144 *
145 * Cache of the current hardware display mode. Only valid when @enabled
146 * is set. This is used by helpers like
147 * drm_calc_vbltimestamp_from_scanoutpos(). We can't just access the
148 * hardware mode by e.g. looking at &drm_crtc_state.adjusted_mode,
149 * because that one is really hard to get from interrupt context.
150 */
151 struct drm_display_mode hwmode;
152
153 /**
154 * @enabled: Tracks the enabling state of the corresponding &drm_crtc to
155 * avoid double-disabling and hence corrupting saved state. Needed by
156 * drivers not using atomic KMS, since those might go through their CRTC
157 * disabling functions multiple times.
158 */
159 bool enabled;
160};
161
162int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs);
Keith Packard570e8692017-10-12 11:57:49 -0700163u64 drm_crtc_vblank_count(struct drm_crtc *crtc);
164u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200165 ktime_t *vblanktime);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200166void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
167 struct drm_pending_vblank_event *e);
168void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
169 struct drm_pending_vblank_event *e);
Keith Packardbd386e52017-07-05 14:34:23 -0700170void drm_vblank_set_event(struct drm_pending_vblank_event *e,
171 u64 *seq,
172 ktime_t *now);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200173bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe);
174bool drm_crtc_handle_vblank(struct drm_crtc *crtc);
175int drm_crtc_vblank_get(struct drm_crtc *crtc);
176void drm_crtc_vblank_put(struct drm_crtc *crtc);
177void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe);
178void drm_crtc_wait_one_vblank(struct drm_crtc *crtc);
179void drm_crtc_vblank_off(struct drm_crtc *crtc);
180void drm_crtc_vblank_reset(struct drm_crtc *crtc);
181void drm_crtc_vblank_on(struct drm_crtc *crtc);
Daniel Vetterca814b22017-05-24 16:51:47 +0200182u32 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200183
184bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
185 unsigned int pipe, int *max_error,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200186 ktime_t *vblank_time,
Daniel Vetter3ed43512017-05-31 11:21:46 +0200187 bool in_vblank_irq);
188void drm_calc_timestamping_constants(struct drm_crtc *crtc,
189 const struct drm_display_mode *mode);
190wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc);
191#endif