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