xref: /dragonfly/sys/dev/drm/drm_vblank.c (revision 3f2dd94a)
1*3f2dd94aSFrançois Tigeot /*
2*3f2dd94aSFrançois Tigeot  * drm_irq.c IRQ and vblank support
3*3f2dd94aSFrançois Tigeot  *
4*3f2dd94aSFrançois Tigeot  * \author Rickard E. (Rik) Faith <faith@valinux.com>
5*3f2dd94aSFrançois Tigeot  * \author Gareth Hughes <gareth@valinux.com>
6*3f2dd94aSFrançois Tigeot  *
7*3f2dd94aSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
8*3f2dd94aSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
9*3f2dd94aSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
10*3f2dd94aSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11*3f2dd94aSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
12*3f2dd94aSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
13*3f2dd94aSFrançois Tigeot  *
14*3f2dd94aSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
15*3f2dd94aSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
16*3f2dd94aSFrançois Tigeot  * Software.
17*3f2dd94aSFrançois Tigeot  *
18*3f2dd94aSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*3f2dd94aSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*3f2dd94aSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21*3f2dd94aSFrançois Tigeot  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22*3f2dd94aSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23*3f2dd94aSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24*3f2dd94aSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
25*3f2dd94aSFrançois Tigeot  */
26*3f2dd94aSFrançois Tigeot 
27*3f2dd94aSFrançois Tigeot #include <drm/drm_vblank.h>
28*3f2dd94aSFrançois Tigeot #include <drm/drmP.h>
29*3f2dd94aSFrançois Tigeot #include <linux/export.h>
30*3f2dd94aSFrançois Tigeot 
31*3f2dd94aSFrançois Tigeot #include "drm_trace.h"
32*3f2dd94aSFrançois Tigeot #include "drm_internal.h"
33*3f2dd94aSFrançois Tigeot 
34*3f2dd94aSFrançois Tigeot /**
35*3f2dd94aSFrançois Tigeot  * DOC: vblank handling
36*3f2dd94aSFrançois Tigeot  *
37*3f2dd94aSFrançois Tigeot  * Vertical blanking plays a major role in graphics rendering. To achieve
38*3f2dd94aSFrançois Tigeot  * tear-free display, users must synchronize page flips and/or rendering to
39*3f2dd94aSFrançois Tigeot  * vertical blanking. The DRM API offers ioctls to perform page flips
40*3f2dd94aSFrançois Tigeot  * synchronized to vertical blanking and wait for vertical blanking.
41*3f2dd94aSFrançois Tigeot  *
42*3f2dd94aSFrançois Tigeot  * The DRM core handles most of the vertical blanking management logic, which
43*3f2dd94aSFrançois Tigeot  * involves filtering out spurious interrupts, keeping race-free blanking
44*3f2dd94aSFrançois Tigeot  * counters, coping with counter wrap-around and resets and keeping use counts.
45*3f2dd94aSFrançois Tigeot  * It relies on the driver to generate vertical blanking interrupts and
46*3f2dd94aSFrançois Tigeot  * optionally provide a hardware vertical blanking counter.
47*3f2dd94aSFrançois Tigeot  *
48*3f2dd94aSFrançois Tigeot  * Drivers must initialize the vertical blanking handling core with a call to
49*3f2dd94aSFrançois Tigeot  * drm_vblank_init(). Minimally, a driver needs to implement
50*3f2dd94aSFrançois Tigeot  * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
51*3f2dd94aSFrançois Tigeot  * drm_crtc_handle_vblank() in it's vblank interrupt handler for working vblank
52*3f2dd94aSFrançois Tigeot  * support.
53*3f2dd94aSFrançois Tigeot  *
54*3f2dd94aSFrançois Tigeot  * Vertical blanking interrupts can be enabled by the DRM core or by drivers
55*3f2dd94aSFrançois Tigeot  * themselves (for instance to handle page flipping operations).  The DRM core
56*3f2dd94aSFrançois Tigeot  * maintains a vertical blanking use count to ensure that the interrupts are not
57*3f2dd94aSFrançois Tigeot  * disabled while a user still needs them. To increment the use count, drivers
58*3f2dd94aSFrançois Tigeot  * call drm_crtc_vblank_get() and release the vblank reference again with
59*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
60*3f2dd94aSFrançois Tigeot  * guaranteed to be enabled.
61*3f2dd94aSFrançois Tigeot  *
62*3f2dd94aSFrançois Tigeot  * On many hardware disabling the vblank interrupt cannot be done in a race-free
63*3f2dd94aSFrançois Tigeot  * manner, see &drm_driver.vblank_disable_immediate and
64*3f2dd94aSFrançois Tigeot  * &drm_driver.max_vblank_count. In that case the vblank core only disables the
65*3f2dd94aSFrançois Tigeot  * vblanks after a timer has expired, which can be configured through the
66*3f2dd94aSFrançois Tigeot  * ``vblankoffdelay`` module parameter.
67*3f2dd94aSFrançois Tigeot  */
68*3f2dd94aSFrançois Tigeot 
69*3f2dd94aSFrançois Tigeot /* Retry timestamp calculation up to 3 times to satisfy
70*3f2dd94aSFrançois Tigeot  * drm_timestamp_precision before giving up.
71*3f2dd94aSFrançois Tigeot  */
72*3f2dd94aSFrançois Tigeot #define DRM_TIMESTAMP_MAXRETRIES 3
73*3f2dd94aSFrançois Tigeot 
74*3f2dd94aSFrançois Tigeot /* Threshold in nanoseconds for detection of redundant
75*3f2dd94aSFrançois Tigeot  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
76*3f2dd94aSFrançois Tigeot  */
77*3f2dd94aSFrançois Tigeot #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
78*3f2dd94aSFrançois Tigeot 
79*3f2dd94aSFrançois Tigeot static bool
80*3f2dd94aSFrançois Tigeot drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
81*3f2dd94aSFrançois Tigeot 			  ktime_t *tvblank, bool in_vblank_irq);
82*3f2dd94aSFrançois Tigeot 
83*3f2dd94aSFrançois Tigeot unsigned int drm_timestamp_precision = 20;  /* Default to 20 usecs. */
84*3f2dd94aSFrançois Tigeot 
85*3f2dd94aSFrançois Tigeot int drm_vblank_offdelay = 5000;    /* Default to 5000 msecs. */
86*3f2dd94aSFrançois Tigeot 
87*3f2dd94aSFrançois Tigeot module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
88*3f2dd94aSFrançois Tigeot module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
89*3f2dd94aSFrançois Tigeot MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
90*3f2dd94aSFrançois Tigeot MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
91*3f2dd94aSFrançois Tigeot 
store_vblank(struct drm_device * dev,unsigned int pipe,u32 vblank_count_inc,ktime_t t_vblank,u32 last)92*3f2dd94aSFrançois Tigeot static void store_vblank(struct drm_device *dev, unsigned int pipe,
93*3f2dd94aSFrançois Tigeot 			 u32 vblank_count_inc,
94*3f2dd94aSFrançois Tigeot 			 ktime_t t_vblank, u32 last)
95*3f2dd94aSFrançois Tigeot {
96*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
97*3f2dd94aSFrançois Tigeot 
98*3f2dd94aSFrançois Tigeot 	assert_spin_locked(&dev->vblank_time_lock);
99*3f2dd94aSFrançois Tigeot 
100*3f2dd94aSFrançois Tigeot 	vblank->last = last;
101*3f2dd94aSFrançois Tigeot 
102*3f2dd94aSFrançois Tigeot 	write_seqlock(&vblank->seqlock);
103*3f2dd94aSFrançois Tigeot 	vblank->time = t_vblank;
104*3f2dd94aSFrançois Tigeot 	vblank->count += vblank_count_inc;
105*3f2dd94aSFrançois Tigeot 	write_sequnlock(&vblank->seqlock);
106*3f2dd94aSFrançois Tigeot }
107*3f2dd94aSFrançois Tigeot 
108*3f2dd94aSFrançois Tigeot /*
109*3f2dd94aSFrançois Tigeot  * "No hw counter" fallback implementation of .get_vblank_counter() hook,
110*3f2dd94aSFrançois Tigeot  * if there is no useable hardware frame counter available.
111*3f2dd94aSFrançois Tigeot  */
drm_vblank_no_hw_counter(struct drm_device * dev,unsigned int pipe)112*3f2dd94aSFrançois Tigeot static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
113*3f2dd94aSFrançois Tigeot {
114*3f2dd94aSFrançois Tigeot 	WARN_ON_ONCE(dev->max_vblank_count != 0);
115*3f2dd94aSFrançois Tigeot 	return 0;
116*3f2dd94aSFrançois Tigeot }
117*3f2dd94aSFrançois Tigeot 
__get_vblank_counter(struct drm_device * dev,unsigned int pipe)118*3f2dd94aSFrançois Tigeot static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
119*3f2dd94aSFrançois Tigeot {
120*3f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
121*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
122*3f2dd94aSFrançois Tigeot 
123*3f2dd94aSFrançois Tigeot 		if (crtc->funcs->get_vblank_counter)
124*3f2dd94aSFrançois Tigeot 			return crtc->funcs->get_vblank_counter(crtc);
125*3f2dd94aSFrançois Tigeot 	}
126*3f2dd94aSFrançois Tigeot 
127*3f2dd94aSFrançois Tigeot 	if (dev->driver->get_vblank_counter)
128*3f2dd94aSFrançois Tigeot 		return dev->driver->get_vblank_counter(dev, pipe);
129*3f2dd94aSFrançois Tigeot 
130*3f2dd94aSFrançois Tigeot 	return drm_vblank_no_hw_counter(dev, pipe);
131*3f2dd94aSFrançois Tigeot }
132*3f2dd94aSFrançois Tigeot 
133*3f2dd94aSFrançois Tigeot /*
134*3f2dd94aSFrançois Tigeot  * Reset the stored timestamp for the current vblank count to correspond
135*3f2dd94aSFrançois Tigeot  * to the last vblank occurred.
136*3f2dd94aSFrançois Tigeot  *
137*3f2dd94aSFrançois Tigeot  * Only to be called from drm_crtc_vblank_on().
138*3f2dd94aSFrançois Tigeot  *
139*3f2dd94aSFrançois Tigeot  * Note: caller must hold &drm_device.vbl_lock since this reads & writes
140*3f2dd94aSFrançois Tigeot  * device vblank fields.
141*3f2dd94aSFrançois Tigeot  */
drm_reset_vblank_timestamp(struct drm_device * dev,unsigned int pipe)142*3f2dd94aSFrançois Tigeot static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
143*3f2dd94aSFrançois Tigeot {
144*3f2dd94aSFrançois Tigeot 	u32 cur_vblank;
145*3f2dd94aSFrançois Tigeot 	bool rc;
146*3f2dd94aSFrançois Tigeot 	ktime_t t_vblank;
147*3f2dd94aSFrançois Tigeot 	int count = DRM_TIMESTAMP_MAXRETRIES;
148*3f2dd94aSFrançois Tigeot 
149*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
150*3f2dd94aSFrançois Tigeot 
151*3f2dd94aSFrançois Tigeot 	/*
152*3f2dd94aSFrançois Tigeot 	 * sample the current counter to avoid random jumps
153*3f2dd94aSFrançois Tigeot 	 * when drm_vblank_enable() applies the diff
154*3f2dd94aSFrançois Tigeot 	 */
155*3f2dd94aSFrançois Tigeot 	do {
156*3f2dd94aSFrançois Tigeot 		cur_vblank = __get_vblank_counter(dev, pipe);
157*3f2dd94aSFrançois Tigeot 		rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
158*3f2dd94aSFrançois Tigeot 	} while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
159*3f2dd94aSFrançois Tigeot 
160*3f2dd94aSFrançois Tigeot 	/*
161*3f2dd94aSFrançois Tigeot 	 * Only reinitialize corresponding vblank timestamp if high-precision query
162*3f2dd94aSFrançois Tigeot 	 * available and didn't fail. Otherwise reinitialize delayed at next vblank
163*3f2dd94aSFrançois Tigeot 	 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
164*3f2dd94aSFrançois Tigeot 	 */
165*3f2dd94aSFrançois Tigeot 	if (!rc)
166*3f2dd94aSFrançois Tigeot 		t_vblank = 0;
167*3f2dd94aSFrançois Tigeot 
168*3f2dd94aSFrançois Tigeot 	/*
169*3f2dd94aSFrançois Tigeot 	 * +1 to make sure user will never see the same
170*3f2dd94aSFrançois Tigeot 	 * vblank counter value before and after a modeset
171*3f2dd94aSFrançois Tigeot 	 */
172*3f2dd94aSFrançois Tigeot 	store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
173*3f2dd94aSFrançois Tigeot 
174*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vblank_time_lock, LK_RELEASE);
175*3f2dd94aSFrançois Tigeot }
176*3f2dd94aSFrançois Tigeot 
177*3f2dd94aSFrançois Tigeot /*
178*3f2dd94aSFrançois Tigeot  * Call back into the driver to update the appropriate vblank counter
179*3f2dd94aSFrançois Tigeot  * (specified by @pipe).  Deal with wraparound, if it occurred, and
180*3f2dd94aSFrançois Tigeot  * update the last read value so we can deal with wraparound on the next
181*3f2dd94aSFrançois Tigeot  * call if necessary.
182*3f2dd94aSFrançois Tigeot  *
183*3f2dd94aSFrançois Tigeot  * Only necessary when going from off->on, to account for frames we
184*3f2dd94aSFrançois Tigeot  * didn't get an interrupt for.
185*3f2dd94aSFrançois Tigeot  *
186*3f2dd94aSFrançois Tigeot  * Note: caller must hold &drm_device.vbl_lock since this reads & writes
187*3f2dd94aSFrançois Tigeot  * device vblank fields.
188*3f2dd94aSFrançois Tigeot  */
drm_update_vblank_count(struct drm_device * dev,unsigned int pipe,bool in_vblank_irq)189*3f2dd94aSFrançois Tigeot static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
190*3f2dd94aSFrançois Tigeot 				    bool in_vblank_irq)
191*3f2dd94aSFrançois Tigeot {
192*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
193*3f2dd94aSFrançois Tigeot 	u32 cur_vblank, diff;
194*3f2dd94aSFrançois Tigeot 	bool rc;
195*3f2dd94aSFrançois Tigeot 	ktime_t t_vblank;
196*3f2dd94aSFrançois Tigeot 	int count = DRM_TIMESTAMP_MAXRETRIES;
197*3f2dd94aSFrançois Tigeot 	int framedur_ns = vblank->framedur_ns;
198*3f2dd94aSFrançois Tigeot 
199*3f2dd94aSFrançois Tigeot 	/*
200*3f2dd94aSFrançois Tigeot 	 * Interrupts were disabled prior to this call, so deal with counter
201*3f2dd94aSFrançois Tigeot 	 * wrap if needed.
202*3f2dd94aSFrançois Tigeot 	 * NOTE!  It's possible we lost a full dev->max_vblank_count + 1 events
203*3f2dd94aSFrançois Tigeot 	 * here if the register is small or we had vblank interrupts off for
204*3f2dd94aSFrançois Tigeot 	 * a long time.
205*3f2dd94aSFrançois Tigeot 	 *
206*3f2dd94aSFrançois Tigeot 	 * We repeat the hardware vblank counter & timestamp query until
207*3f2dd94aSFrançois Tigeot 	 * we get consistent results. This to prevent races between gpu
208*3f2dd94aSFrançois Tigeot 	 * updating its hardware counter while we are retrieving the
209*3f2dd94aSFrançois Tigeot 	 * corresponding vblank timestamp.
210*3f2dd94aSFrançois Tigeot 	 */
211*3f2dd94aSFrançois Tigeot 	do {
212*3f2dd94aSFrançois Tigeot 		cur_vblank = __get_vblank_counter(dev, pipe);
213*3f2dd94aSFrançois Tigeot 		rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
214*3f2dd94aSFrançois Tigeot 	} while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
215*3f2dd94aSFrançois Tigeot 
216*3f2dd94aSFrançois Tigeot 	if (dev->max_vblank_count != 0) {
217*3f2dd94aSFrançois Tigeot 		/* trust the hw counter when it's around */
218*3f2dd94aSFrançois Tigeot 		diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
219*3f2dd94aSFrançois Tigeot 	} else if (rc && framedur_ns) {
220*3f2dd94aSFrançois Tigeot 		u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
221*3f2dd94aSFrançois Tigeot 
222*3f2dd94aSFrançois Tigeot 		/*
223*3f2dd94aSFrançois Tigeot 		 * Figure out how many vblanks we've missed based
224*3f2dd94aSFrançois Tigeot 		 * on the difference in the timestamps and the
225*3f2dd94aSFrançois Tigeot 		 * frame/field duration.
226*3f2dd94aSFrançois Tigeot 		 */
227*3f2dd94aSFrançois Tigeot 		diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
228*3f2dd94aSFrançois Tigeot 
229*3f2dd94aSFrançois Tigeot 		if (diff == 0 && in_vblank_irq)
230*3f2dd94aSFrançois Tigeot 			DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
231*3f2dd94aSFrançois Tigeot 				      " diff_ns = %lld, framedur_ns = %d)\n",
232*3f2dd94aSFrançois Tigeot 				      pipe, (long long) diff_ns, framedur_ns);
233*3f2dd94aSFrançois Tigeot 	} else {
234*3f2dd94aSFrançois Tigeot 		/* some kind of default for drivers w/o accurate vbl timestamping */
235*3f2dd94aSFrançois Tigeot 		diff = in_vblank_irq ? 1 : 0;
236*3f2dd94aSFrançois Tigeot 	}
237*3f2dd94aSFrançois Tigeot 
238*3f2dd94aSFrançois Tigeot 	/*
239*3f2dd94aSFrançois Tigeot 	 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
240*3f2dd94aSFrançois Tigeot 	 * interval? If so then vblank irqs keep running and it will likely
241*3f2dd94aSFrançois Tigeot 	 * happen that the hardware vblank counter is not trustworthy as it
242*3f2dd94aSFrançois Tigeot 	 * might reset at some point in that interval and vblank timestamps
243*3f2dd94aSFrançois Tigeot 	 * are not trustworthy either in that interval. Iow. this can result
244*3f2dd94aSFrançois Tigeot 	 * in a bogus diff >> 1 which must be avoided as it would cause
245*3f2dd94aSFrançois Tigeot 	 * random large forward jumps of the software vblank counter.
246*3f2dd94aSFrançois Tigeot 	 */
247*3f2dd94aSFrançois Tigeot 	if (diff > 1 && (vblank->inmodeset & 0x2)) {
248*3f2dd94aSFrançois Tigeot 		DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
249*3f2dd94aSFrançois Tigeot 			      " due to pre-modeset.\n", pipe, diff);
250*3f2dd94aSFrançois Tigeot 		diff = 1;
251*3f2dd94aSFrançois Tigeot 	}
252*3f2dd94aSFrançois Tigeot 
253*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_VBL("updating vblank count on crtc %u:"
254*3f2dd94aSFrançois Tigeot 		      " current=%llu, diff=%u, hw=%u hw_last=%u\n",
255*3f2dd94aSFrançois Tigeot 		      pipe, vblank->count, diff, cur_vblank, vblank->last);
256*3f2dd94aSFrançois Tigeot 
257*3f2dd94aSFrançois Tigeot 	if (diff == 0) {
258*3f2dd94aSFrançois Tigeot 		WARN_ON_ONCE(cur_vblank != vblank->last);
259*3f2dd94aSFrançois Tigeot 		return;
260*3f2dd94aSFrançois Tigeot 	}
261*3f2dd94aSFrançois Tigeot 
262*3f2dd94aSFrançois Tigeot 	/*
263*3f2dd94aSFrançois Tigeot 	 * Only reinitialize corresponding vblank timestamp if high-precision query
264*3f2dd94aSFrançois Tigeot 	 * available and didn't fail, or we were called from the vblank interrupt.
265*3f2dd94aSFrançois Tigeot 	 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
266*3f2dd94aSFrançois Tigeot 	 * for now, to mark the vblanktimestamp as invalid.
267*3f2dd94aSFrançois Tigeot 	 */
268*3f2dd94aSFrançois Tigeot 	if (!rc && !in_vblank_irq)
269*3f2dd94aSFrançois Tigeot 		t_vblank = 0;
270*3f2dd94aSFrançois Tigeot 
271*3f2dd94aSFrançois Tigeot 	store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
272*3f2dd94aSFrançois Tigeot }
273*3f2dd94aSFrançois Tigeot 
drm_vblank_count(struct drm_device * dev,unsigned int pipe)274*3f2dd94aSFrançois Tigeot static u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
275*3f2dd94aSFrançois Tigeot {
276*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
277*3f2dd94aSFrançois Tigeot 
278*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
279*3f2dd94aSFrançois Tigeot 		return 0;
280*3f2dd94aSFrançois Tigeot 
281*3f2dd94aSFrançois Tigeot 	return vblank->count;
282*3f2dd94aSFrançois Tigeot }
283*3f2dd94aSFrançois Tigeot 
284*3f2dd94aSFrançois Tigeot /**
285*3f2dd94aSFrançois Tigeot  * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
286*3f2dd94aSFrançois Tigeot  * @crtc: which counter to retrieve
287*3f2dd94aSFrançois Tigeot  *
288*3f2dd94aSFrançois Tigeot  * This function is similar to drm_crtc_vblank_count() but this function
289*3f2dd94aSFrançois Tigeot  * interpolates to handle a race with vblank interrupts using the high precision
290*3f2dd94aSFrançois Tigeot  * timestamping support.
291*3f2dd94aSFrançois Tigeot  *
292*3f2dd94aSFrançois Tigeot  * This is mostly useful for hardware that can obtain the scanout position, but
293*3f2dd94aSFrançois Tigeot  * doesn't have a hardware frame counter.
294*3f2dd94aSFrançois Tigeot  */
drm_crtc_accurate_vblank_count(struct drm_crtc * crtc)295*3f2dd94aSFrançois Tigeot u32 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
296*3f2dd94aSFrançois Tigeot {
297*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
298*3f2dd94aSFrançois Tigeot 	unsigned int pipe = drm_crtc_index(crtc);
299*3f2dd94aSFrançois Tigeot 	u32 vblank;
300*3f2dd94aSFrançois Tigeot 	unsigned long flags;
301*3f2dd94aSFrançois Tigeot 
302*3f2dd94aSFrançois Tigeot 	WARN_ONCE(drm_debug & DRM_UT_VBL && !dev->driver->get_vblank_timestamp,
303*3f2dd94aSFrançois Tigeot 		  "This function requires support for accurate vblank timestamps.");
304*3f2dd94aSFrançois Tigeot 
305*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->vblank_time_lock, flags);
306*3f2dd94aSFrançois Tigeot 
307*3f2dd94aSFrançois Tigeot 	drm_update_vblank_count(dev, pipe, false);
308*3f2dd94aSFrançois Tigeot 	vblank = drm_vblank_count(dev, pipe);
309*3f2dd94aSFrançois Tigeot 
310*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
311*3f2dd94aSFrançois Tigeot 
312*3f2dd94aSFrançois Tigeot 	return vblank;
313*3f2dd94aSFrançois Tigeot }
314*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
315*3f2dd94aSFrançois Tigeot 
__disable_vblank(struct drm_device * dev,unsigned int pipe)316*3f2dd94aSFrançois Tigeot static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
317*3f2dd94aSFrançois Tigeot {
318*3f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
319*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
320*3f2dd94aSFrançois Tigeot 
321*3f2dd94aSFrançois Tigeot 		if (crtc->funcs->disable_vblank) {
322*3f2dd94aSFrançois Tigeot 			crtc->funcs->disable_vblank(crtc);
323*3f2dd94aSFrançois Tigeot 			return;
324*3f2dd94aSFrançois Tigeot 		}
325*3f2dd94aSFrançois Tigeot 	}
326*3f2dd94aSFrançois Tigeot 
327*3f2dd94aSFrançois Tigeot 	dev->driver->disable_vblank(dev, pipe);
328*3f2dd94aSFrançois Tigeot }
329*3f2dd94aSFrançois Tigeot 
330*3f2dd94aSFrançois Tigeot /*
331*3f2dd94aSFrançois Tigeot  * Disable vblank irq's on crtc, make sure that last vblank count
332*3f2dd94aSFrançois Tigeot  * of hardware and corresponding consistent software vblank counter
333*3f2dd94aSFrançois Tigeot  * are preserved, even if there are any spurious vblank irq's after
334*3f2dd94aSFrançois Tigeot  * disable.
335*3f2dd94aSFrançois Tigeot  */
drm_vblank_disable_and_save(struct drm_device * dev,unsigned int pipe)336*3f2dd94aSFrançois Tigeot void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
337*3f2dd94aSFrançois Tigeot {
338*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
339*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
340*3f2dd94aSFrançois Tigeot 
341*3f2dd94aSFrançois Tigeot 	assert_spin_locked(&dev->vbl_lock);
342*3f2dd94aSFrançois Tigeot 
343*3f2dd94aSFrançois Tigeot 	/* Prevent vblank irq processing while disabling vblank irqs,
344*3f2dd94aSFrançois Tigeot 	 * so no updates of timestamps or count can happen after we've
345*3f2dd94aSFrançois Tigeot 	 * disabled. Needed to prevent races in case of delayed irq's.
346*3f2dd94aSFrançois Tigeot 	 */
347*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
348*3f2dd94aSFrançois Tigeot 
349*3f2dd94aSFrançois Tigeot 	/*
350*3f2dd94aSFrançois Tigeot 	 * Only disable vblank interrupts if they're enabled. This avoids
351*3f2dd94aSFrançois Tigeot 	 * calling the ->disable_vblank() operation in atomic context with the
352*3f2dd94aSFrançois Tigeot 	 * hardware potentially runtime suspended.
353*3f2dd94aSFrançois Tigeot 	 */
354*3f2dd94aSFrançois Tigeot 	if (vblank->enabled) {
355*3f2dd94aSFrançois Tigeot 		__disable_vblank(dev, pipe);
356*3f2dd94aSFrançois Tigeot 		vblank->enabled = false;
357*3f2dd94aSFrançois Tigeot 	}
358*3f2dd94aSFrançois Tigeot 
359*3f2dd94aSFrançois Tigeot 	/*
360*3f2dd94aSFrançois Tigeot 	 * Always update the count and timestamp to maintain the
361*3f2dd94aSFrançois Tigeot 	 * appearance that the counter has been ticking all along until
362*3f2dd94aSFrançois Tigeot 	 * this time. This makes the count account for the entire time
363*3f2dd94aSFrançois Tigeot 	 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
364*3f2dd94aSFrançois Tigeot 	 */
365*3f2dd94aSFrançois Tigeot 	drm_update_vblank_count(dev, pipe, false);
366*3f2dd94aSFrançois Tigeot 
367*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
368*3f2dd94aSFrançois Tigeot }
369*3f2dd94aSFrançois Tigeot 
vblank_disable_fn(struct timer_list * t)370*3f2dd94aSFrançois Tigeot static void vblank_disable_fn(struct timer_list *t)
371*3f2dd94aSFrançois Tigeot {
372*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = from_timer(vblank, t, disable_timer);
373*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = vblank->dev;
374*3f2dd94aSFrançois Tigeot 	unsigned int pipe = vblank->pipe;
375*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
376*3f2dd94aSFrançois Tigeot 
377*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
378*3f2dd94aSFrançois Tigeot 	if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
379*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
380*3f2dd94aSFrançois Tigeot 		drm_vblank_disable_and_save(dev, pipe);
381*3f2dd94aSFrançois Tigeot 	}
382*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
383*3f2dd94aSFrançois Tigeot }
384*3f2dd94aSFrançois Tigeot 
drm_vblank_cleanup(struct drm_device * dev)385*3f2dd94aSFrançois Tigeot void drm_vblank_cleanup(struct drm_device *dev)
386*3f2dd94aSFrançois Tigeot {
387*3f2dd94aSFrançois Tigeot 	unsigned int pipe;
388*3f2dd94aSFrançois Tigeot 
389*3f2dd94aSFrançois Tigeot 	/* Bail if the driver didn't call drm_vblank_init() */
390*3f2dd94aSFrançois Tigeot 	if (dev->num_crtcs == 0)
391*3f2dd94aSFrançois Tigeot 		return;
392*3f2dd94aSFrançois Tigeot 
393*3f2dd94aSFrançois Tigeot 	for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
394*3f2dd94aSFrançois Tigeot 		struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
395*3f2dd94aSFrançois Tigeot 
396*3f2dd94aSFrançois Tigeot 		WARN_ON(READ_ONCE(vblank->enabled) &&
397*3f2dd94aSFrançois Tigeot 			drm_core_check_feature(dev, DRIVER_MODESET));
398*3f2dd94aSFrançois Tigeot 
399*3f2dd94aSFrançois Tigeot 		del_timer_sync(&vblank->disable_timer);
400*3f2dd94aSFrançois Tigeot 	}
401*3f2dd94aSFrançois Tigeot 
402*3f2dd94aSFrançois Tigeot 	kfree(dev->vblank);
403*3f2dd94aSFrançois Tigeot 
404*3f2dd94aSFrançois Tigeot 	dev->num_crtcs = 0;
405*3f2dd94aSFrançois Tigeot }
406*3f2dd94aSFrançois Tigeot 
407*3f2dd94aSFrançois Tigeot /**
408*3f2dd94aSFrançois Tigeot  * drm_vblank_init - initialize vblank support
409*3f2dd94aSFrançois Tigeot  * @dev: DRM device
410*3f2dd94aSFrançois Tigeot  * @num_crtcs: number of CRTCs supported by @dev
411*3f2dd94aSFrançois Tigeot  *
412*3f2dd94aSFrançois Tigeot  * This function initializes vblank support for @num_crtcs display pipelines.
413*3f2dd94aSFrançois Tigeot  * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
414*3f2dd94aSFrançois Tigeot  * drivers with a &drm_driver.release callback.
415*3f2dd94aSFrançois Tigeot  *
416*3f2dd94aSFrançois Tigeot  * Returns:
417*3f2dd94aSFrançois Tigeot  * Zero on success or a negative error code on failure.
418*3f2dd94aSFrançois Tigeot  */
drm_vblank_init(struct drm_device * dev,unsigned int num_crtcs)419*3f2dd94aSFrançois Tigeot int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
420*3f2dd94aSFrançois Tigeot {
421*3f2dd94aSFrançois Tigeot 	int ret = -ENOMEM;
422*3f2dd94aSFrançois Tigeot 	unsigned int i;
423*3f2dd94aSFrançois Tigeot 
424*3f2dd94aSFrançois Tigeot 	lockinit(&dev->vbl_lock, "drmvbl", 0, 0);
425*3f2dd94aSFrançois Tigeot 	lockinit(&dev->vblank_time_lock, "drmvtl", 0, 0);
426*3f2dd94aSFrançois Tigeot 
427*3f2dd94aSFrançois Tigeot 	dev->num_crtcs = num_crtcs;
428*3f2dd94aSFrançois Tigeot 
429*3f2dd94aSFrançois Tigeot 	dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
430*3f2dd94aSFrançois Tigeot 	if (!dev->vblank)
431*3f2dd94aSFrançois Tigeot 		goto err;
432*3f2dd94aSFrançois Tigeot 
433*3f2dd94aSFrançois Tigeot 	for (i = 0; i < num_crtcs; i++) {
434*3f2dd94aSFrançois Tigeot 		struct drm_vblank_crtc *vblank = &dev->vblank[i];
435*3f2dd94aSFrançois Tigeot 
436*3f2dd94aSFrançois Tigeot 		vblank->dev = dev;
437*3f2dd94aSFrançois Tigeot 		vblank->pipe = i;
438*3f2dd94aSFrançois Tigeot 		init_waitqueue_head(&vblank->queue);
439*3f2dd94aSFrançois Tigeot 		timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
440*3f2dd94aSFrançois Tigeot 		seqlock_init(&vblank->seqlock);
441*3f2dd94aSFrançois Tigeot 	}
442*3f2dd94aSFrançois Tigeot 
443*3f2dd94aSFrançois Tigeot 	DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
444*3f2dd94aSFrançois Tigeot 
445*3f2dd94aSFrançois Tigeot 	/* Driver specific high-precision vblank timestamping supported? */
446*3f2dd94aSFrançois Tigeot 	if (dev->driver->get_vblank_timestamp)
447*3f2dd94aSFrançois Tigeot 		DRM_INFO("Driver supports precise vblank timestamp query.\n");
448*3f2dd94aSFrançois Tigeot 	else
449*3f2dd94aSFrançois Tigeot 		DRM_INFO("No driver support for vblank timestamp query.\n");
450*3f2dd94aSFrançois Tigeot 
451*3f2dd94aSFrançois Tigeot 	/* Must have precise timestamping for reliable vblank instant disable */
452*3f2dd94aSFrançois Tigeot 	if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
453*3f2dd94aSFrançois Tigeot 		dev->vblank_disable_immediate = false;
454*3f2dd94aSFrançois Tigeot 		DRM_INFO("Setting vblank_disable_immediate to false because "
455*3f2dd94aSFrançois Tigeot 			 "get_vblank_timestamp == NULL\n");
456*3f2dd94aSFrançois Tigeot 	}
457*3f2dd94aSFrançois Tigeot 
458*3f2dd94aSFrançois Tigeot 	return 0;
459*3f2dd94aSFrançois Tigeot 
460*3f2dd94aSFrançois Tigeot err:
461*3f2dd94aSFrançois Tigeot 	dev->num_crtcs = 0;
462*3f2dd94aSFrançois Tigeot 	return ret;
463*3f2dd94aSFrançois Tigeot }
464*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_vblank_init);
465*3f2dd94aSFrançois Tigeot 
466*3f2dd94aSFrançois Tigeot /**
467*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
468*3f2dd94aSFrançois Tigeot  * @crtc: which CRTC's vblank waitqueue to retrieve
469*3f2dd94aSFrançois Tigeot  *
470*3f2dd94aSFrançois Tigeot  * This function returns a pointer to the vblank waitqueue for the CRTC.
471*3f2dd94aSFrançois Tigeot  * Drivers can use this to implement vblank waits using wait_event() and related
472*3f2dd94aSFrançois Tigeot  * functions.
473*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_waitqueue(struct drm_crtc * crtc)474*3f2dd94aSFrançois Tigeot wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
475*3f2dd94aSFrançois Tigeot {
476*3f2dd94aSFrançois Tigeot 	return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
477*3f2dd94aSFrançois Tigeot }
478*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
479*3f2dd94aSFrançois Tigeot 
480*3f2dd94aSFrançois Tigeot 
481*3f2dd94aSFrançois Tigeot /**
482*3f2dd94aSFrançois Tigeot  * drm_calc_timestamping_constants - calculate vblank timestamp constants
483*3f2dd94aSFrançois Tigeot  * @crtc: drm_crtc whose timestamp constants should be updated.
484*3f2dd94aSFrançois Tigeot  * @mode: display mode containing the scanout timings
485*3f2dd94aSFrançois Tigeot  *
486*3f2dd94aSFrançois Tigeot  * Calculate and store various constants which are later needed by vblank and
487*3f2dd94aSFrançois Tigeot  * swap-completion timestamping, e.g, by
488*3f2dd94aSFrançois Tigeot  * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true
489*3f2dd94aSFrançois Tigeot  * scanout timing, so they take things like panel scaling or other adjustments
490*3f2dd94aSFrançois Tigeot  * into account.
491*3f2dd94aSFrançois Tigeot  */
drm_calc_timestamping_constants(struct drm_crtc * crtc,const struct drm_display_mode * mode)492*3f2dd94aSFrançois Tigeot void drm_calc_timestamping_constants(struct drm_crtc *crtc,
493*3f2dd94aSFrançois Tigeot 				     const struct drm_display_mode *mode)
494*3f2dd94aSFrançois Tigeot {
495*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
496*3f2dd94aSFrançois Tigeot 	unsigned int pipe = drm_crtc_index(crtc);
497*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
498*3f2dd94aSFrançois Tigeot 	int linedur_ns = 0, framedur_ns = 0;
499*3f2dd94aSFrançois Tigeot 	int dotclock = mode->crtc_clock;
500*3f2dd94aSFrançois Tigeot 
501*3f2dd94aSFrançois Tigeot 	if (!dev->num_crtcs)
502*3f2dd94aSFrançois Tigeot 		return;
503*3f2dd94aSFrançois Tigeot 
504*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
505*3f2dd94aSFrançois Tigeot 		return;
506*3f2dd94aSFrançois Tigeot 
507*3f2dd94aSFrançois Tigeot 	/* Valid dotclock? */
508*3f2dd94aSFrançois Tigeot 	if (dotclock > 0) {
509*3f2dd94aSFrançois Tigeot 		int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
510*3f2dd94aSFrançois Tigeot 
511*3f2dd94aSFrançois Tigeot 		/*
512*3f2dd94aSFrançois Tigeot 		 * Convert scanline length in pixels and video
513*3f2dd94aSFrançois Tigeot 		 * dot clock to line duration and frame duration
514*3f2dd94aSFrançois Tigeot 		 * in nanoseconds:
515*3f2dd94aSFrançois Tigeot 		 */
516*3f2dd94aSFrançois Tigeot 		linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
517*3f2dd94aSFrançois Tigeot 		framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
518*3f2dd94aSFrançois Tigeot 
519*3f2dd94aSFrançois Tigeot 		/*
520*3f2dd94aSFrançois Tigeot 		 * Fields of interlaced scanout modes are only half a frame duration.
521*3f2dd94aSFrançois Tigeot 		 */
522*3f2dd94aSFrançois Tigeot 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
523*3f2dd94aSFrançois Tigeot 			framedur_ns /= 2;
524*3f2dd94aSFrançois Tigeot 	} else
525*3f2dd94aSFrançois Tigeot 		DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
526*3f2dd94aSFrançois Tigeot 			  crtc->base.id);
527*3f2dd94aSFrançois Tigeot 
528*3f2dd94aSFrançois Tigeot 	vblank->linedur_ns  = linedur_ns;
529*3f2dd94aSFrançois Tigeot 	vblank->framedur_ns = framedur_ns;
530*3f2dd94aSFrançois Tigeot 	vblank->hwmode = *mode;
531*3f2dd94aSFrançois Tigeot 
532*3f2dd94aSFrançois Tigeot 	DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
533*3f2dd94aSFrançois Tigeot 		  crtc->base.id, mode->crtc_htotal,
534*3f2dd94aSFrançois Tigeot 		  mode->crtc_vtotal, mode->crtc_vdisplay);
535*3f2dd94aSFrançois Tigeot 	DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
536*3f2dd94aSFrançois Tigeot 		  crtc->base.id, dotclock, framedur_ns, linedur_ns);
537*3f2dd94aSFrançois Tigeot }
538*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_calc_timestamping_constants);
539*3f2dd94aSFrançois Tigeot 
540*3f2dd94aSFrançois Tigeot /**
541*3f2dd94aSFrançois Tigeot  * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
542*3f2dd94aSFrançois Tigeot  * @dev: DRM device
543*3f2dd94aSFrançois Tigeot  * @pipe: index of CRTC whose vblank timestamp to retrieve
544*3f2dd94aSFrançois Tigeot  * @max_error: Desired maximum allowable error in timestamps (nanosecs)
545*3f2dd94aSFrançois Tigeot  *             On return contains true maximum error of timestamp
546*3f2dd94aSFrançois Tigeot  * @vblank_time: Pointer to time which should receive the timestamp
547*3f2dd94aSFrançois Tigeot  * @in_vblank_irq:
548*3f2dd94aSFrançois Tigeot  *     True when called from drm_crtc_handle_vblank().  Some drivers
549*3f2dd94aSFrançois Tigeot  *     need to apply some workarounds for gpu-specific vblank irq quirks
550*3f2dd94aSFrançois Tigeot  *     if flag is set.
551*3f2dd94aSFrançois Tigeot  *
552*3f2dd94aSFrançois Tigeot  * Implements calculation of exact vblank timestamps from given drm_display_mode
553*3f2dd94aSFrançois Tigeot  * timings and current video scanout position of a CRTC. This can be directly
554*3f2dd94aSFrançois Tigeot  * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver
555*3f2dd94aSFrançois Tigeot  * if &drm_driver.get_scanout_position is implemented.
556*3f2dd94aSFrançois Tigeot  *
557*3f2dd94aSFrançois Tigeot  * The current implementation only handles standard video modes. For double scan
558*3f2dd94aSFrançois Tigeot  * and interlaced modes the driver is supposed to adjust the hardware mode
559*3f2dd94aSFrançois Tigeot  * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
560*3f2dd94aSFrançois Tigeot  * match the scanout position reported.
561*3f2dd94aSFrançois Tigeot  *
562*3f2dd94aSFrançois Tigeot  * Note that atomic drivers must call drm_calc_timestamping_constants() before
563*3f2dd94aSFrançois Tigeot  * enabling a CRTC. The atomic helpers already take care of that in
564*3f2dd94aSFrançois Tigeot  * drm_atomic_helper_update_legacy_modeset_state().
565*3f2dd94aSFrançois Tigeot  *
566*3f2dd94aSFrançois Tigeot  * Returns:
567*3f2dd94aSFrançois Tigeot  *
568*3f2dd94aSFrançois Tigeot  * Returns true on success, and false on failure, i.e. when no accurate
569*3f2dd94aSFrançois Tigeot  * timestamp could be acquired.
570*3f2dd94aSFrançois Tigeot  */
drm_calc_vbltimestamp_from_scanoutpos(struct drm_device * dev,unsigned int pipe,int * max_error,ktime_t * vblank_time,bool in_vblank_irq)571*3f2dd94aSFrançois Tigeot bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
572*3f2dd94aSFrançois Tigeot 					   unsigned int pipe,
573*3f2dd94aSFrançois Tigeot 					   int *max_error,
574*3f2dd94aSFrançois Tigeot 					   ktime_t *vblank_time,
575*3f2dd94aSFrançois Tigeot 					   bool in_vblank_irq)
576*3f2dd94aSFrançois Tigeot {
577*3f2dd94aSFrançois Tigeot 	struct timespec64 ts_etime, ts_vblank_time;
578*3f2dd94aSFrançois Tigeot 	ktime_t stime, etime;
579*3f2dd94aSFrançois Tigeot 	bool vbl_status;
580*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc;
581*3f2dd94aSFrançois Tigeot 	const struct drm_display_mode *mode;
582*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
583*3f2dd94aSFrançois Tigeot 	int vpos, hpos, i;
584*3f2dd94aSFrançois Tigeot 	int delta_ns, duration_ns;
585*3f2dd94aSFrançois Tigeot 
586*3f2dd94aSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
587*3f2dd94aSFrançois Tigeot 		return false;
588*3f2dd94aSFrançois Tigeot 
589*3f2dd94aSFrançois Tigeot 	crtc = drm_crtc_from_index(dev, pipe);
590*3f2dd94aSFrançois Tigeot 
591*3f2dd94aSFrançois Tigeot 	if (pipe >= dev->num_crtcs || !crtc) {
592*3f2dd94aSFrançois Tigeot 		DRM_ERROR("Invalid crtc %u\n", pipe);
593*3f2dd94aSFrançois Tigeot 		return false;
594*3f2dd94aSFrançois Tigeot 	}
595*3f2dd94aSFrançois Tigeot 
596*3f2dd94aSFrançois Tigeot 	/* Scanout position query not supported? Should not happen. */
597*3f2dd94aSFrançois Tigeot 	if (!dev->driver->get_scanout_position) {
598*3f2dd94aSFrançois Tigeot 		DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
599*3f2dd94aSFrançois Tigeot 		return false;
600*3f2dd94aSFrançois Tigeot 	}
601*3f2dd94aSFrançois Tigeot 
602*3f2dd94aSFrançois Tigeot 	if (drm_drv_uses_atomic_modeset(dev))
603*3f2dd94aSFrançois Tigeot 		mode = &vblank->hwmode;
604*3f2dd94aSFrançois Tigeot 	else
605*3f2dd94aSFrançois Tigeot 		mode = &crtc->hwmode;
606*3f2dd94aSFrançois Tigeot 
607*3f2dd94aSFrançois Tigeot 	/* If mode timing undefined, just return as no-op:
608*3f2dd94aSFrançois Tigeot 	 * Happens during initial modesetting of a crtc.
609*3f2dd94aSFrançois Tigeot 	 */
610*3f2dd94aSFrançois Tigeot 	if (mode->crtc_clock == 0) {
611*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
612*3f2dd94aSFrançois Tigeot 		WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev));
613*3f2dd94aSFrançois Tigeot 
614*3f2dd94aSFrançois Tigeot 		return false;
615*3f2dd94aSFrançois Tigeot 	}
616*3f2dd94aSFrançois Tigeot 
617*3f2dd94aSFrançois Tigeot 	/* Get current scanout position with system timestamp.
618*3f2dd94aSFrançois Tigeot 	 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
619*3f2dd94aSFrançois Tigeot 	 * if single query takes longer than max_error nanoseconds.
620*3f2dd94aSFrançois Tigeot 	 *
621*3f2dd94aSFrançois Tigeot 	 * This guarantees a tight bound on maximum error if
622*3f2dd94aSFrançois Tigeot 	 * code gets preempted or delayed for some reason.
623*3f2dd94aSFrançois Tigeot 	 */
624*3f2dd94aSFrançois Tigeot 	for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
625*3f2dd94aSFrançois Tigeot 		/*
626*3f2dd94aSFrançois Tigeot 		 * Get vertical and horizontal scanout position vpos, hpos,
627*3f2dd94aSFrançois Tigeot 		 * and bounding timestamps stime, etime, pre/post query.
628*3f2dd94aSFrançois Tigeot 		 */
629*3f2dd94aSFrançois Tigeot 		vbl_status = dev->driver->get_scanout_position(dev, pipe,
630*3f2dd94aSFrançois Tigeot 							       in_vblank_irq,
631*3f2dd94aSFrançois Tigeot 							       &vpos, &hpos,
632*3f2dd94aSFrançois Tigeot 							       &stime, &etime,
633*3f2dd94aSFrançois Tigeot 							       mode);
634*3f2dd94aSFrançois Tigeot 
635*3f2dd94aSFrançois Tigeot 		/* Return as no-op if scanout query unsupported or failed. */
636*3f2dd94aSFrançois Tigeot 		if (!vbl_status) {
637*3f2dd94aSFrançois Tigeot 			DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
638*3f2dd94aSFrançois Tigeot 				  pipe);
639*3f2dd94aSFrançois Tigeot 			return false;
640*3f2dd94aSFrançois Tigeot 		}
641*3f2dd94aSFrançois Tigeot 
642*3f2dd94aSFrançois Tigeot 		/* Compute uncertainty in timestamp of scanout position query. */
643*3f2dd94aSFrançois Tigeot 		duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
644*3f2dd94aSFrançois Tigeot 
645*3f2dd94aSFrançois Tigeot 		/* Accept result with <  max_error nsecs timing uncertainty. */
646*3f2dd94aSFrançois Tigeot 		if (duration_ns <= *max_error)
647*3f2dd94aSFrançois Tigeot 			break;
648*3f2dd94aSFrançois Tigeot 	}
649*3f2dd94aSFrançois Tigeot 
650*3f2dd94aSFrançois Tigeot 	/* Noisy system timing? */
651*3f2dd94aSFrançois Tigeot 	if (i == DRM_TIMESTAMP_MAXRETRIES) {
652*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
653*3f2dd94aSFrançois Tigeot 			  pipe, duration_ns/1000, *max_error/1000, i);
654*3f2dd94aSFrançois Tigeot 	}
655*3f2dd94aSFrançois Tigeot 
656*3f2dd94aSFrançois Tigeot 	/* Return upper bound of timestamp precision error. */
657*3f2dd94aSFrançois Tigeot 	*max_error = duration_ns;
658*3f2dd94aSFrançois Tigeot 
659*3f2dd94aSFrançois Tigeot 	/* Convert scanout position into elapsed time at raw_time query
660*3f2dd94aSFrançois Tigeot 	 * since start of scanout at first display scanline. delta_ns
661*3f2dd94aSFrançois Tigeot 	 * can be negative if start of scanout hasn't happened yet.
662*3f2dd94aSFrançois Tigeot 	 */
663*3f2dd94aSFrançois Tigeot 	delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
664*3f2dd94aSFrançois Tigeot 			   mode->crtc_clock);
665*3f2dd94aSFrançois Tigeot 
666*3f2dd94aSFrançois Tigeot 	/* Subtract time delta from raw timestamp to get final
667*3f2dd94aSFrançois Tigeot 	 * vblank_time timestamp for end of vblank.
668*3f2dd94aSFrançois Tigeot 	 */
669*3f2dd94aSFrançois Tigeot 	*vblank_time = ktime_sub_ns(etime, delta_ns);
670*3f2dd94aSFrançois Tigeot 
671*3f2dd94aSFrançois Tigeot 	if ((drm_debug & DRM_UT_VBL) == 0)
672*3f2dd94aSFrançois Tigeot 		return true;
673*3f2dd94aSFrançois Tigeot 
674*3f2dd94aSFrançois Tigeot 	ts_etime = ktime_to_timespec64(etime);
675*3f2dd94aSFrançois Tigeot 	ts_vblank_time = ktime_to_timespec64(*vblank_time);
676*3f2dd94aSFrançois Tigeot 
677*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
678*3f2dd94aSFrançois Tigeot 		      pipe, hpos, vpos,
679*3f2dd94aSFrançois Tigeot 		      (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
680*3f2dd94aSFrançois Tigeot 		      (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
681*3f2dd94aSFrançois Tigeot 		      duration_ns / 1000, i);
682*3f2dd94aSFrançois Tigeot 
683*3f2dd94aSFrançois Tigeot 	return true;
684*3f2dd94aSFrançois Tigeot }
685*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
686*3f2dd94aSFrançois Tigeot 
687*3f2dd94aSFrançois Tigeot /**
688*3f2dd94aSFrançois Tigeot  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
689*3f2dd94aSFrançois Tigeot  *                             vblank interval
690*3f2dd94aSFrançois Tigeot  * @dev: DRM device
691*3f2dd94aSFrançois Tigeot  * @pipe: index of CRTC whose vblank timestamp to retrieve
692*3f2dd94aSFrançois Tigeot  * @tvblank: Pointer to target time which should receive the timestamp
693*3f2dd94aSFrançois Tigeot  * @in_vblank_irq:
694*3f2dd94aSFrançois Tigeot  *     True when called from drm_crtc_handle_vblank().  Some drivers
695*3f2dd94aSFrançois Tigeot  *     need to apply some workarounds for gpu-specific vblank irq quirks
696*3f2dd94aSFrançois Tigeot  *     if flag is set.
697*3f2dd94aSFrançois Tigeot  *
698*3f2dd94aSFrançois Tigeot  * Fetches the system timestamp corresponding to the time of the most recent
699*3f2dd94aSFrançois Tigeot  * vblank interval on specified CRTC. May call into kms-driver to
700*3f2dd94aSFrançois Tigeot  * compute the timestamp with a high-precision GPU specific method.
701*3f2dd94aSFrançois Tigeot  *
702*3f2dd94aSFrançois Tigeot  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
703*3f2dd94aSFrançois Tigeot  * call, i.e., it isn't very precisely locked to the true vblank.
704*3f2dd94aSFrançois Tigeot  *
705*3f2dd94aSFrançois Tigeot  * Returns:
706*3f2dd94aSFrançois Tigeot  * True if timestamp is considered to be very precise, false otherwise.
707*3f2dd94aSFrançois Tigeot  */
708*3f2dd94aSFrançois Tigeot static bool
drm_get_last_vbltimestamp(struct drm_device * dev,unsigned int pipe,ktime_t * tvblank,bool in_vblank_irq)709*3f2dd94aSFrançois Tigeot drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
710*3f2dd94aSFrançois Tigeot 			  ktime_t *tvblank, bool in_vblank_irq)
711*3f2dd94aSFrançois Tigeot {
712*3f2dd94aSFrançois Tigeot 	bool ret = false;
713*3f2dd94aSFrançois Tigeot 
714*3f2dd94aSFrançois Tigeot 	/* Define requested maximum error on timestamps (nanoseconds). */
715*3f2dd94aSFrançois Tigeot 	int max_error = (int) drm_timestamp_precision * 1000;
716*3f2dd94aSFrançois Tigeot 
717*3f2dd94aSFrançois Tigeot 	/* Query driver if possible and precision timestamping enabled. */
718*3f2dd94aSFrançois Tigeot 	if (dev->driver->get_vblank_timestamp && (max_error > 0))
719*3f2dd94aSFrançois Tigeot 		ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
720*3f2dd94aSFrançois Tigeot 							tvblank, in_vblank_irq);
721*3f2dd94aSFrançois Tigeot 
722*3f2dd94aSFrançois Tigeot 	/* GPU high precision timestamp query unsupported or failed.
723*3f2dd94aSFrançois Tigeot 	 * Return current monotonic/gettimeofday timestamp as best estimate.
724*3f2dd94aSFrançois Tigeot 	 */
725*3f2dd94aSFrançois Tigeot 	if (!ret)
726*3f2dd94aSFrançois Tigeot 		*tvblank = ktime_get();
727*3f2dd94aSFrançois Tigeot 
728*3f2dd94aSFrançois Tigeot 	return ret;
729*3f2dd94aSFrançois Tigeot }
730*3f2dd94aSFrançois Tigeot 
731*3f2dd94aSFrançois Tigeot /**
732*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
733*3f2dd94aSFrançois Tigeot  * @crtc: which counter to retrieve
734*3f2dd94aSFrançois Tigeot  *
735*3f2dd94aSFrançois Tigeot  * Fetches the "cooked" vblank count value that represents the number of
736*3f2dd94aSFrançois Tigeot  * vblank events since the system was booted, including lost events due to
737*3f2dd94aSFrançois Tigeot  * modesetting activity. Note that this timer isn't correct against a racing
738*3f2dd94aSFrançois Tigeot  * vblank interrupt (since it only reports the software vblank counter), see
739*3f2dd94aSFrançois Tigeot  * drm_crtc_accurate_vblank_count() for such use-cases.
740*3f2dd94aSFrançois Tigeot  *
741*3f2dd94aSFrançois Tigeot  * Returns:
742*3f2dd94aSFrançois Tigeot  * The software vblank counter.
743*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_count(struct drm_crtc * crtc)744*3f2dd94aSFrançois Tigeot u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
745*3f2dd94aSFrançois Tigeot {
746*3f2dd94aSFrançois Tigeot 	return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
747*3f2dd94aSFrançois Tigeot }
748*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_count);
749*3f2dd94aSFrançois Tigeot 
750*3f2dd94aSFrançois Tigeot /**
751*3f2dd94aSFrançois Tigeot  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
752*3f2dd94aSFrançois Tigeot  *     system timestamp corresponding to that vblank counter value.
753*3f2dd94aSFrançois Tigeot  * @dev: DRM device
754*3f2dd94aSFrançois Tigeot  * @pipe: index of CRTC whose counter to retrieve
755*3f2dd94aSFrançois Tigeot  * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
756*3f2dd94aSFrançois Tigeot  *
757*3f2dd94aSFrançois Tigeot  * Fetches the "cooked" vblank count value that represents the number of
758*3f2dd94aSFrançois Tigeot  * vblank events since the system was booted, including lost events due to
759*3f2dd94aSFrançois Tigeot  * modesetting activity. Returns corresponding system timestamp of the time
760*3f2dd94aSFrançois Tigeot  * of the vblank interval that corresponds to the current vblank counter value.
761*3f2dd94aSFrançois Tigeot  *
762*3f2dd94aSFrançois Tigeot  * This is the legacy version of drm_crtc_vblank_count_and_time().
763*3f2dd94aSFrançois Tigeot  */
drm_vblank_count_and_time(struct drm_device * dev,unsigned int pipe,ktime_t * vblanktime)764*3f2dd94aSFrançois Tigeot static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
765*3f2dd94aSFrançois Tigeot 				     ktime_t *vblanktime)
766*3f2dd94aSFrançois Tigeot {
767*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
768*3f2dd94aSFrançois Tigeot 	u64 vblank_count;
769*3f2dd94aSFrançois Tigeot 	unsigned int seq;
770*3f2dd94aSFrançois Tigeot 
771*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs)) {
772*3f2dd94aSFrançois Tigeot 		*vblanktime = 0;
773*3f2dd94aSFrançois Tigeot 		return 0;
774*3f2dd94aSFrançois Tigeot 	}
775*3f2dd94aSFrançois Tigeot 
776*3f2dd94aSFrançois Tigeot 	do {
777*3f2dd94aSFrançois Tigeot 		seq = read_seqbegin(&vblank->seqlock);
778*3f2dd94aSFrançois Tigeot 		vblank_count = vblank->count;
779*3f2dd94aSFrançois Tigeot 		*vblanktime = vblank->time;
780*3f2dd94aSFrançois Tigeot 	} while (read_seqretry(&vblank->seqlock, seq));
781*3f2dd94aSFrançois Tigeot 
782*3f2dd94aSFrançois Tigeot 	return vblank_count;
783*3f2dd94aSFrançois Tigeot }
784*3f2dd94aSFrançois Tigeot 
785*3f2dd94aSFrançois Tigeot /**
786*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
787*3f2dd94aSFrançois Tigeot  *     and the system timestamp corresponding to that vblank counter value
788*3f2dd94aSFrançois Tigeot  * @crtc: which counter to retrieve
789*3f2dd94aSFrançois Tigeot  * @vblanktime: Pointer to time to receive the vblank timestamp.
790*3f2dd94aSFrançois Tigeot  *
791*3f2dd94aSFrançois Tigeot  * Fetches the "cooked" vblank count value that represents the number of
792*3f2dd94aSFrançois Tigeot  * vblank events since the system was booted, including lost events due to
793*3f2dd94aSFrançois Tigeot  * modesetting activity. Returns corresponding system timestamp of the time
794*3f2dd94aSFrançois Tigeot  * of the vblank interval that corresponds to the current vblank counter value.
795*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_count_and_time(struct drm_crtc * crtc,ktime_t * vblanktime)796*3f2dd94aSFrançois Tigeot u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
797*3f2dd94aSFrançois Tigeot 				   ktime_t *vblanktime)
798*3f2dd94aSFrançois Tigeot {
799*3f2dd94aSFrançois Tigeot 	return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
800*3f2dd94aSFrançois Tigeot 					 vblanktime);
801*3f2dd94aSFrançois Tigeot }
802*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
803*3f2dd94aSFrançois Tigeot 
send_vblank_event(struct drm_device * dev,struct drm_pending_vblank_event * e,u64 seq,ktime_t now)804*3f2dd94aSFrançois Tigeot static void send_vblank_event(struct drm_device *dev,
805*3f2dd94aSFrançois Tigeot 		struct drm_pending_vblank_event *e,
806*3f2dd94aSFrançois Tigeot 		u64 seq, ktime_t now)
807*3f2dd94aSFrançois Tigeot {
808*3f2dd94aSFrançois Tigeot 	struct timespec64 tv;
809*3f2dd94aSFrançois Tigeot 
810*3f2dd94aSFrançois Tigeot 	switch (e->event.base.type) {
811*3f2dd94aSFrançois Tigeot 	case DRM_EVENT_VBLANK:
812*3f2dd94aSFrançois Tigeot 	case DRM_EVENT_FLIP_COMPLETE:
813*3f2dd94aSFrançois Tigeot 		tv = ktime_to_timespec64(now);
814*3f2dd94aSFrançois Tigeot 		e->event.vbl.sequence = seq;
815*3f2dd94aSFrançois Tigeot 		/*
816*3f2dd94aSFrançois Tigeot 		 * e->event is a user space structure, with hardcoded unsigned
817*3f2dd94aSFrançois Tigeot 		 * 32-bit seconds/microseconds. This is safe as we always use
818*3f2dd94aSFrançois Tigeot 		 * monotonic timestamps since linux-4.15
819*3f2dd94aSFrançois Tigeot 		 */
820*3f2dd94aSFrançois Tigeot 		e->event.vbl.tv_sec = tv.tv_sec;
821*3f2dd94aSFrançois Tigeot 		e->event.vbl.tv_usec = tv.tv_nsec / 1000;
822*3f2dd94aSFrançois Tigeot 		break;
823*3f2dd94aSFrançois Tigeot 	case DRM_EVENT_CRTC_SEQUENCE:
824*3f2dd94aSFrançois Tigeot 		if (seq)
825*3f2dd94aSFrançois Tigeot 			e->event.seq.sequence = seq;
826*3f2dd94aSFrançois Tigeot 		e->event.seq.time_ns = ktime_to_ns(now);
827*3f2dd94aSFrançois Tigeot 		break;
828*3f2dd94aSFrançois Tigeot 	}
829*3f2dd94aSFrançois Tigeot 	trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
830*3f2dd94aSFrançois Tigeot 	drm_send_event_locked(dev, &e->base);
831*3f2dd94aSFrançois Tigeot }
832*3f2dd94aSFrançois Tigeot 
833*3f2dd94aSFrançois Tigeot /**
834*3f2dd94aSFrançois Tigeot  * drm_crtc_arm_vblank_event - arm vblank event after pageflip
835*3f2dd94aSFrançois Tigeot  * @crtc: the source CRTC of the vblank event
836*3f2dd94aSFrançois Tigeot  * @e: the event to send
837*3f2dd94aSFrançois Tigeot  *
838*3f2dd94aSFrançois Tigeot  * A lot of drivers need to generate vblank events for the very next vblank
839*3f2dd94aSFrançois Tigeot  * interrupt. For example when the page flip interrupt happens when the page
840*3f2dd94aSFrançois Tigeot  * flip gets armed, but not when it actually executes within the next vblank
841*3f2dd94aSFrançois Tigeot  * period. This helper function implements exactly the required vblank arming
842*3f2dd94aSFrançois Tigeot  * behaviour.
843*3f2dd94aSFrançois Tigeot  *
844*3f2dd94aSFrançois Tigeot  * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
845*3f2dd94aSFrançois Tigeot  * atomic commit must ensure that the next vblank happens at exactly the same
846*3f2dd94aSFrançois Tigeot  * time as the atomic commit is committed to the hardware. This function itself
847*3f2dd94aSFrançois Tigeot  * does **not** protect against the next vblank interrupt racing with either this
848*3f2dd94aSFrançois Tigeot  * function call or the atomic commit operation. A possible sequence could be:
849*3f2dd94aSFrançois Tigeot  *
850*3f2dd94aSFrançois Tigeot  * 1. Driver commits new hardware state into vblank-synchronized registers.
851*3f2dd94aSFrançois Tigeot  * 2. A vblank happens, committing the hardware state. Also the corresponding
852*3f2dd94aSFrançois Tigeot  *    vblank interrupt is fired off and fully processed by the interrupt
853*3f2dd94aSFrançois Tigeot  *    handler.
854*3f2dd94aSFrançois Tigeot  * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
855*3f2dd94aSFrançois Tigeot  * 4. The event is only send out for the next vblank, which is wrong.
856*3f2dd94aSFrançois Tigeot  *
857*3f2dd94aSFrançois Tigeot  * An equivalent race can happen when the driver calls
858*3f2dd94aSFrançois Tigeot  * drm_crtc_arm_vblank_event() before writing out the new hardware state.
859*3f2dd94aSFrançois Tigeot  *
860*3f2dd94aSFrançois Tigeot  * The only way to make this work safely is to prevent the vblank from firing
861*3f2dd94aSFrançois Tigeot  * (and the hardware from committing anything else) until the entire atomic
862*3f2dd94aSFrançois Tigeot  * commit sequence has run to completion. If the hardware does not have such a
863*3f2dd94aSFrançois Tigeot  * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
864*3f2dd94aSFrançois Tigeot  * Instead drivers need to manually send out the event from their interrupt
865*3f2dd94aSFrançois Tigeot  * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
866*3f2dd94aSFrançois Tigeot  * possible race with the hardware committing the atomic update.
867*3f2dd94aSFrançois Tigeot  *
868*3f2dd94aSFrançois Tigeot  * Caller must hold a vblank reference for the event @e, which will be dropped
869*3f2dd94aSFrançois Tigeot  * when the next vblank arrives.
870*3f2dd94aSFrançois Tigeot  */
drm_crtc_arm_vblank_event(struct drm_crtc * crtc,struct drm_pending_vblank_event * e)871*3f2dd94aSFrançois Tigeot void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
872*3f2dd94aSFrançois Tigeot 			       struct drm_pending_vblank_event *e)
873*3f2dd94aSFrançois Tigeot {
874*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
875*3f2dd94aSFrançois Tigeot 	unsigned int pipe = drm_crtc_index(crtc);
876*3f2dd94aSFrançois Tigeot 
877*3f2dd94aSFrançois Tigeot 	assert_spin_locked(&dev->event_lock);
878*3f2dd94aSFrançois Tigeot 
879*3f2dd94aSFrançois Tigeot 	e->pipe = pipe;
880*3f2dd94aSFrançois Tigeot 	e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
881*3f2dd94aSFrançois Tigeot 	list_add_tail(&e->base.link, &dev->vblank_event_list);
882*3f2dd94aSFrançois Tigeot }
883*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
884*3f2dd94aSFrançois Tigeot 
885*3f2dd94aSFrançois Tigeot /**
886*3f2dd94aSFrançois Tigeot  * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
887*3f2dd94aSFrançois Tigeot  * @crtc: the source CRTC of the vblank event
888*3f2dd94aSFrançois Tigeot  * @e: the event to send
889*3f2dd94aSFrançois Tigeot  *
890*3f2dd94aSFrançois Tigeot  * Updates sequence # and timestamp on event for the most recently processed
891*3f2dd94aSFrançois Tigeot  * vblank, and sends it to userspace.  Caller must hold event lock.
892*3f2dd94aSFrançois Tigeot  *
893*3f2dd94aSFrançois Tigeot  * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
894*3f2dd94aSFrançois Tigeot  * situation, especially to send out events for atomic commit operations.
895*3f2dd94aSFrançois Tigeot  */
drm_crtc_send_vblank_event(struct drm_crtc * crtc,struct drm_pending_vblank_event * e)896*3f2dd94aSFrançois Tigeot void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
897*3f2dd94aSFrançois Tigeot 				struct drm_pending_vblank_event *e)
898*3f2dd94aSFrançois Tigeot {
899*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
900*3f2dd94aSFrançois Tigeot 	u64 seq;
901*3f2dd94aSFrançois Tigeot 	unsigned int pipe = drm_crtc_index(crtc);
902*3f2dd94aSFrançois Tigeot 	ktime_t now;
903*3f2dd94aSFrançois Tigeot 
904*3f2dd94aSFrançois Tigeot 	if (dev->num_crtcs > 0) {
905*3f2dd94aSFrançois Tigeot 		seq = drm_vblank_count_and_time(dev, pipe, &now);
906*3f2dd94aSFrançois Tigeot 	} else {
907*3f2dd94aSFrançois Tigeot 		seq = 0;
908*3f2dd94aSFrançois Tigeot 
909*3f2dd94aSFrançois Tigeot 		now = ktime_get();
910*3f2dd94aSFrançois Tigeot 	}
911*3f2dd94aSFrançois Tigeot 	e->pipe = pipe;
912*3f2dd94aSFrançois Tigeot 	send_vblank_event(dev, e, seq, now);
913*3f2dd94aSFrançois Tigeot }
914*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_send_vblank_event);
915*3f2dd94aSFrançois Tigeot 
__enable_vblank(struct drm_device * dev,unsigned int pipe)916*3f2dd94aSFrançois Tigeot static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
917*3f2dd94aSFrançois Tigeot {
918*3f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
919*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
920*3f2dd94aSFrançois Tigeot 
921*3f2dd94aSFrançois Tigeot 		if (crtc->funcs->enable_vblank)
922*3f2dd94aSFrançois Tigeot 			return crtc->funcs->enable_vblank(crtc);
923*3f2dd94aSFrançois Tigeot 	}
924*3f2dd94aSFrançois Tigeot 
925*3f2dd94aSFrançois Tigeot 	return dev->driver->enable_vblank(dev, pipe);
926*3f2dd94aSFrançois Tigeot }
927*3f2dd94aSFrançois Tigeot 
drm_vblank_enable(struct drm_device * dev,unsigned int pipe)928*3f2dd94aSFrançois Tigeot static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
929*3f2dd94aSFrançois Tigeot {
930*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
931*3f2dd94aSFrançois Tigeot 	int ret = 0;
932*3f2dd94aSFrançois Tigeot 
933*3f2dd94aSFrançois Tigeot 	assert_spin_locked(&dev->vbl_lock);
934*3f2dd94aSFrançois Tigeot 
935*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
936*3f2dd94aSFrançois Tigeot 
937*3f2dd94aSFrançois Tigeot 	if (!vblank->enabled) {
938*3f2dd94aSFrançois Tigeot 		/*
939*3f2dd94aSFrançois Tigeot 		 * Enable vblank irqs under vblank_time_lock protection.
940*3f2dd94aSFrançois Tigeot 		 * All vblank count & timestamp updates are held off
941*3f2dd94aSFrançois Tigeot 		 * until we are done reinitializing master counter and
942*3f2dd94aSFrançois Tigeot 		 * timestamps. Filtercode in drm_handle_vblank() will
943*3f2dd94aSFrançois Tigeot 		 * prevent double-accounting of same vblank interval.
944*3f2dd94aSFrançois Tigeot 		 */
945*3f2dd94aSFrançois Tigeot 		ret = __enable_vblank(dev, pipe);
946*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
947*3f2dd94aSFrançois Tigeot 		if (ret) {
948*3f2dd94aSFrançois Tigeot 			atomic_dec(&vblank->refcount);
949*3f2dd94aSFrançois Tigeot 		} else {
950*3f2dd94aSFrançois Tigeot 			drm_update_vblank_count(dev, pipe, 0);
951*3f2dd94aSFrançois Tigeot 			/* drm_update_vblank_count() includes a wmb so we just
952*3f2dd94aSFrançois Tigeot 			 * need to ensure that the compiler emits the write
953*3f2dd94aSFrançois Tigeot 			 * to mark the vblank as enabled after the call
954*3f2dd94aSFrançois Tigeot 			 * to drm_update_vblank_count().
955*3f2dd94aSFrançois Tigeot 			 */
956*3f2dd94aSFrançois Tigeot 			WRITE_ONCE(vblank->enabled, true);
957*3f2dd94aSFrançois Tigeot 		}
958*3f2dd94aSFrançois Tigeot 	}
959*3f2dd94aSFrançois Tigeot 
960*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vblank_time_lock, LK_RELEASE);
961*3f2dd94aSFrançois Tigeot 
962*3f2dd94aSFrançois Tigeot 	return ret;
963*3f2dd94aSFrançois Tigeot }
964*3f2dd94aSFrançois Tigeot 
drm_vblank_get(struct drm_device * dev,unsigned int pipe)965*3f2dd94aSFrançois Tigeot static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
966*3f2dd94aSFrançois Tigeot {
967*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
968*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
969*3f2dd94aSFrançois Tigeot 	int ret = 0;
970*3f2dd94aSFrançois Tigeot 
971*3f2dd94aSFrançois Tigeot 	if (!dev->num_crtcs)
972*3f2dd94aSFrançois Tigeot 		return -EINVAL;
973*3f2dd94aSFrançois Tigeot 
974*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
975*3f2dd94aSFrançois Tigeot 		return -EINVAL;
976*3f2dd94aSFrançois Tigeot 
977*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
978*3f2dd94aSFrançois Tigeot 	/* Going from 0->1 means we have to enable interrupts again */
979*3f2dd94aSFrançois Tigeot 	if (atomic_add_return(1, &vblank->refcount) == 1) {
980*3f2dd94aSFrançois Tigeot 		ret = drm_vblank_enable(dev, pipe);
981*3f2dd94aSFrançois Tigeot 	} else {
982*3f2dd94aSFrançois Tigeot 		if (!vblank->enabled) {
983*3f2dd94aSFrançois Tigeot 			atomic_dec(&vblank->refcount);
984*3f2dd94aSFrançois Tigeot 			ret = -EINVAL;
985*3f2dd94aSFrançois Tigeot 		}
986*3f2dd94aSFrançois Tigeot 	}
987*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
988*3f2dd94aSFrançois Tigeot 
989*3f2dd94aSFrançois Tigeot 	return ret;
990*3f2dd94aSFrançois Tigeot }
991*3f2dd94aSFrançois Tigeot 
992*3f2dd94aSFrançois Tigeot /**
993*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_get - get a reference count on vblank events
994*3f2dd94aSFrançois Tigeot  * @crtc: which CRTC to own
995*3f2dd94aSFrançois Tigeot  *
996*3f2dd94aSFrançois Tigeot  * Acquire a reference count on vblank events to avoid having them disabled
997*3f2dd94aSFrançois Tigeot  * while in use.
998*3f2dd94aSFrançois Tigeot  *
999*3f2dd94aSFrançois Tigeot  * Returns:
1000*3f2dd94aSFrançois Tigeot  * Zero on success or a negative error code on failure.
1001*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_get(struct drm_crtc * crtc)1002*3f2dd94aSFrançois Tigeot int drm_crtc_vblank_get(struct drm_crtc *crtc)
1003*3f2dd94aSFrançois Tigeot {
1004*3f2dd94aSFrançois Tigeot 	return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1005*3f2dd94aSFrançois Tigeot }
1006*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_get);
1007*3f2dd94aSFrançois Tigeot 
drm_vblank_put(struct drm_device * dev,unsigned int pipe)1008*3f2dd94aSFrançois Tigeot static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1009*3f2dd94aSFrançois Tigeot {
1010*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1011*3f2dd94aSFrançois Tigeot 
1012*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
1013*3f2dd94aSFrançois Tigeot 		return;
1014*3f2dd94aSFrançois Tigeot 
1015*3f2dd94aSFrançois Tigeot 	if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1016*3f2dd94aSFrançois Tigeot 		return;
1017*3f2dd94aSFrançois Tigeot 
1018*3f2dd94aSFrançois Tigeot 	/* Last user schedules interrupt disable */
1019*3f2dd94aSFrançois Tigeot 	if (atomic_dec_and_test(&vblank->refcount)) {
1020*3f2dd94aSFrançois Tigeot 		if (drm_vblank_offdelay == 0)
1021*3f2dd94aSFrançois Tigeot 			return;
1022*3f2dd94aSFrançois Tigeot 		else if (drm_vblank_offdelay < 0)
1023*3f2dd94aSFrançois Tigeot 			vblank_disable_fn(&vblank->disable_timer);
1024*3f2dd94aSFrançois Tigeot 		else if (!dev->vblank_disable_immediate)
1025*3f2dd94aSFrançois Tigeot 			mod_timer(&vblank->disable_timer,
1026*3f2dd94aSFrançois Tigeot 				  jiffies + ((drm_vblank_offdelay * HZ)/1000));
1027*3f2dd94aSFrançois Tigeot 	}
1028*3f2dd94aSFrançois Tigeot }
1029*3f2dd94aSFrançois Tigeot 
1030*3f2dd94aSFrançois Tigeot /**
1031*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_put - give up ownership of vblank events
1032*3f2dd94aSFrançois Tigeot  * @crtc: which counter to give up
1033*3f2dd94aSFrançois Tigeot  *
1034*3f2dd94aSFrançois Tigeot  * Release ownership of a given vblank counter, turning off interrupts
1035*3f2dd94aSFrançois Tigeot  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1036*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_put(struct drm_crtc * crtc)1037*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_put(struct drm_crtc *crtc)
1038*3f2dd94aSFrançois Tigeot {
1039*3f2dd94aSFrançois Tigeot 	drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1040*3f2dd94aSFrançois Tigeot }
1041*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_put);
1042*3f2dd94aSFrançois Tigeot 
1043*3f2dd94aSFrançois Tigeot /**
1044*3f2dd94aSFrançois Tigeot  * drm_wait_one_vblank - wait for one vblank
1045*3f2dd94aSFrançois Tigeot  * @dev: DRM device
1046*3f2dd94aSFrançois Tigeot  * @pipe: CRTC index
1047*3f2dd94aSFrançois Tigeot  *
1048*3f2dd94aSFrançois Tigeot  * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1049*3f2dd94aSFrançois Tigeot  * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1050*3f2dd94aSFrançois Tigeot  * due to lack of driver support or because the crtc is off.
1051*3f2dd94aSFrançois Tigeot  *
1052*3f2dd94aSFrançois Tigeot  * This is the legacy version of drm_crtc_wait_one_vblank().
1053*3f2dd94aSFrançois Tigeot  */
drm_wait_one_vblank(struct drm_device * dev,unsigned int pipe)1054*3f2dd94aSFrançois Tigeot void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1055*3f2dd94aSFrançois Tigeot {
1056*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1057*3f2dd94aSFrançois Tigeot 	int ret;
1058*3f2dd94aSFrançois Tigeot 	u32 last;
1059*3f2dd94aSFrançois Tigeot 
1060*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
1061*3f2dd94aSFrançois Tigeot 		return;
1062*3f2dd94aSFrançois Tigeot 
1063*3f2dd94aSFrançois Tigeot 	ret = drm_vblank_get(dev, pipe);
1064*3f2dd94aSFrançois Tigeot 	if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1065*3f2dd94aSFrançois Tigeot 		return;
1066*3f2dd94aSFrançois Tigeot 
1067*3f2dd94aSFrançois Tigeot 	last = drm_vblank_count(dev, pipe);
1068*3f2dd94aSFrançois Tigeot 
1069*3f2dd94aSFrançois Tigeot 	ret = wait_event_timeout(vblank->queue,
1070*3f2dd94aSFrançois Tigeot 				 last != drm_vblank_count(dev, pipe),
1071*3f2dd94aSFrançois Tigeot 				 msecs_to_jiffies(100));
1072*3f2dd94aSFrançois Tigeot 
1073*3f2dd94aSFrançois Tigeot 	WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1074*3f2dd94aSFrançois Tigeot 
1075*3f2dd94aSFrançois Tigeot 	drm_vblank_put(dev, pipe);
1076*3f2dd94aSFrançois Tigeot }
1077*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_wait_one_vblank);
1078*3f2dd94aSFrançois Tigeot 
1079*3f2dd94aSFrançois Tigeot /**
1080*3f2dd94aSFrançois Tigeot  * drm_crtc_wait_one_vblank - wait for one vblank
1081*3f2dd94aSFrançois Tigeot  * @crtc: DRM crtc
1082*3f2dd94aSFrançois Tigeot  *
1083*3f2dd94aSFrançois Tigeot  * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1084*3f2dd94aSFrançois Tigeot  * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1085*3f2dd94aSFrançois Tigeot  * due to lack of driver support or because the crtc is off.
1086*3f2dd94aSFrançois Tigeot  */
drm_crtc_wait_one_vblank(struct drm_crtc * crtc)1087*3f2dd94aSFrançois Tigeot void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1088*3f2dd94aSFrançois Tigeot {
1089*3f2dd94aSFrançois Tigeot 	drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1090*3f2dd94aSFrançois Tigeot }
1091*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1092*3f2dd94aSFrançois Tigeot 
1093*3f2dd94aSFrançois Tigeot /**
1094*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_off - disable vblank events on a CRTC
1095*3f2dd94aSFrançois Tigeot  * @crtc: CRTC in question
1096*3f2dd94aSFrançois Tigeot  *
1097*3f2dd94aSFrançois Tigeot  * Drivers can use this function to shut down the vblank interrupt handling when
1098*3f2dd94aSFrançois Tigeot  * disabling a crtc. This function ensures that the latest vblank frame count is
1099*3f2dd94aSFrançois Tigeot  * stored so that drm_vblank_on can restore it again.
1100*3f2dd94aSFrançois Tigeot  *
1101*3f2dd94aSFrançois Tigeot  * Drivers must use this function when the hardware vblank counter can get
1102*3f2dd94aSFrançois Tigeot  * reset, e.g. when suspending or disabling the @crtc in general.
1103*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_off(struct drm_crtc * crtc)1104*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_off(struct drm_crtc *crtc)
1105*3f2dd94aSFrançois Tigeot {
1106*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
1107*3f2dd94aSFrançois Tigeot 	unsigned int pipe = drm_crtc_index(crtc);
1108*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1109*3f2dd94aSFrançois Tigeot 	struct drm_pending_vblank_event *e, *t;
1110*3f2dd94aSFrançois Tigeot 
1111*3f2dd94aSFrançois Tigeot 	ktime_t now;
1112*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
1113*3f2dd94aSFrançois Tigeot 	u64 seq;
1114*3f2dd94aSFrançois Tigeot 
1115*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
1116*3f2dd94aSFrançois Tigeot 		return;
1117*3f2dd94aSFrançois Tigeot 
1118*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, irqflags);
1119*3f2dd94aSFrançois Tigeot 
1120*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vbl_lock, LK_EXCLUSIVE);
1121*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1122*3f2dd94aSFrançois Tigeot 		      pipe, vblank->enabled, vblank->inmodeset);
1123*3f2dd94aSFrançois Tigeot 
1124*3f2dd94aSFrançois Tigeot 	/* Avoid redundant vblank disables without previous
1125*3f2dd94aSFrançois Tigeot 	 * drm_crtc_vblank_on(). */
1126*3f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1127*3f2dd94aSFrançois Tigeot 		drm_vblank_disable_and_save(dev, pipe);
1128*3f2dd94aSFrançois Tigeot 
1129*3f2dd94aSFrançois Tigeot 	wake_up(&vblank->queue);
1130*3f2dd94aSFrançois Tigeot 
1131*3f2dd94aSFrançois Tigeot 	/*
1132*3f2dd94aSFrançois Tigeot 	 * Prevent subsequent drm_vblank_get() from re-enabling
1133*3f2dd94aSFrançois Tigeot 	 * the vblank interrupt by bumping the refcount.
1134*3f2dd94aSFrançois Tigeot 	 */
1135*3f2dd94aSFrançois Tigeot 	if (!vblank->inmodeset) {
1136*3f2dd94aSFrançois Tigeot 		atomic_inc(&vblank->refcount);
1137*3f2dd94aSFrançois Tigeot 		vblank->inmodeset = 1;
1138*3f2dd94aSFrançois Tigeot 	}
1139*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vbl_lock, LK_RELEASE);
1140*3f2dd94aSFrançois Tigeot 
1141*3f2dd94aSFrançois Tigeot 	/* Send any queued vblank events, lest the natives grow disquiet */
1142*3f2dd94aSFrançois Tigeot 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1143*3f2dd94aSFrançois Tigeot 
1144*3f2dd94aSFrançois Tigeot 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1145*3f2dd94aSFrançois Tigeot 		if (e->pipe != pipe)
1146*3f2dd94aSFrançois Tigeot 			continue;
1147*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("Sending premature vblank event on disable: "
1148*3f2dd94aSFrançois Tigeot 			  "wanted %llu, current %llu\n",
1149*3f2dd94aSFrançois Tigeot 			  e->sequence, seq);
1150*3f2dd94aSFrançois Tigeot 		list_del(&e->base.link);
1151*3f2dd94aSFrançois Tigeot 		drm_vblank_put(dev, pipe);
1152*3f2dd94aSFrançois Tigeot 		send_vblank_event(dev, e, seq, now);
1153*3f2dd94aSFrançois Tigeot 	}
1154*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
1155*3f2dd94aSFrançois Tigeot 
1156*3f2dd94aSFrançois Tigeot 	/* Will be reset by the modeset helpers when re-enabling the crtc by
1157*3f2dd94aSFrançois Tigeot 	 * calling drm_calc_timestamping_constants(). */
1158*3f2dd94aSFrançois Tigeot 	vblank->hwmode.crtc_clock = 0;
1159*3f2dd94aSFrançois Tigeot }
1160*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_off);
1161*3f2dd94aSFrançois Tigeot 
1162*3f2dd94aSFrançois Tigeot /**
1163*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1164*3f2dd94aSFrançois Tigeot  * @crtc: CRTC in question
1165*3f2dd94aSFrançois Tigeot  *
1166*3f2dd94aSFrançois Tigeot  * Drivers can use this function to reset the vblank state to off at load time.
1167*3f2dd94aSFrançois Tigeot  * Drivers should use this together with the drm_crtc_vblank_off() and
1168*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_on() functions. The difference compared to
1169*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1170*3f2dd94aSFrançois Tigeot  * and hence doesn't need to call any driver hooks.
1171*3f2dd94aSFrançois Tigeot  *
1172*3f2dd94aSFrançois Tigeot  * This is useful for recovering driver state e.g. on driver load, or on resume.
1173*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_reset(struct drm_crtc * crtc)1174*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1175*3f2dd94aSFrançois Tigeot {
1176*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
1177*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
1178*3f2dd94aSFrançois Tigeot 	unsigned int pipe = drm_crtc_index(crtc);
1179*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1180*3f2dd94aSFrançois Tigeot 
1181*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
1182*3f2dd94aSFrançois Tigeot 	/*
1183*3f2dd94aSFrançois Tigeot 	 * Prevent subsequent drm_vblank_get() from enabling the vblank
1184*3f2dd94aSFrançois Tigeot 	 * interrupt by bumping the refcount.
1185*3f2dd94aSFrançois Tigeot 	 */
1186*3f2dd94aSFrançois Tigeot 	if (!vblank->inmodeset) {
1187*3f2dd94aSFrançois Tigeot 		atomic_inc(&vblank->refcount);
1188*3f2dd94aSFrançois Tigeot 		vblank->inmodeset = 1;
1189*3f2dd94aSFrançois Tigeot 	}
1190*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1191*3f2dd94aSFrançois Tigeot 
1192*3f2dd94aSFrançois Tigeot 	WARN_ON(!list_empty(&dev->vblank_event_list));
1193*3f2dd94aSFrançois Tigeot }
1194*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_reset);
1195*3f2dd94aSFrançois Tigeot 
1196*3f2dd94aSFrançois Tigeot /**
1197*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_on - enable vblank events on a CRTC
1198*3f2dd94aSFrançois Tigeot  * @crtc: CRTC in question
1199*3f2dd94aSFrançois Tigeot  *
1200*3f2dd94aSFrançois Tigeot  * This functions restores the vblank interrupt state captured with
1201*3f2dd94aSFrançois Tigeot  * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1202*3f2dd94aSFrançois Tigeot  * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1203*3f2dd94aSFrançois Tigeot  * unbalanced and so can also be unconditionally called in driver load code to
1204*3f2dd94aSFrançois Tigeot  * reflect the current hardware state of the crtc.
1205*3f2dd94aSFrançois Tigeot  */
drm_crtc_vblank_on(struct drm_crtc * crtc)1206*3f2dd94aSFrançois Tigeot void drm_crtc_vblank_on(struct drm_crtc *crtc)
1207*3f2dd94aSFrançois Tigeot {
1208*3f2dd94aSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
1209*3f2dd94aSFrançois Tigeot 	unsigned int pipe = drm_crtc_index(crtc);
1210*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1211*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
1212*3f2dd94aSFrançois Tigeot 
1213*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
1214*3f2dd94aSFrançois Tigeot 		return;
1215*3f2dd94aSFrançois Tigeot 
1216*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
1217*3f2dd94aSFrançois Tigeot 	DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1218*3f2dd94aSFrançois Tigeot 		      pipe, vblank->enabled, vblank->inmodeset);
1219*3f2dd94aSFrançois Tigeot 
1220*3f2dd94aSFrançois Tigeot 	/* Drop our private "prevent drm_vblank_get" refcount */
1221*3f2dd94aSFrançois Tigeot 	if (vblank->inmodeset) {
1222*3f2dd94aSFrançois Tigeot 		atomic_dec(&vblank->refcount);
1223*3f2dd94aSFrançois Tigeot 		vblank->inmodeset = 0;
1224*3f2dd94aSFrançois Tigeot 	}
1225*3f2dd94aSFrançois Tigeot 
1226*3f2dd94aSFrançois Tigeot 	drm_reset_vblank_timestamp(dev, pipe);
1227*3f2dd94aSFrançois Tigeot 
1228*3f2dd94aSFrançois Tigeot 	/*
1229*3f2dd94aSFrançois Tigeot 	 * re-enable interrupts if there are users left, or the
1230*3f2dd94aSFrançois Tigeot 	 * user wishes vblank interrupts to be enabled all the time.
1231*3f2dd94aSFrançois Tigeot 	 */
1232*3f2dd94aSFrançois Tigeot 	if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1233*3f2dd94aSFrançois Tigeot 		WARN_ON(drm_vblank_enable(dev, pipe));
1234*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1235*3f2dd94aSFrançois Tigeot }
1236*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_vblank_on);
1237*3f2dd94aSFrançois Tigeot 
drm_legacy_vblank_pre_modeset(struct drm_device * dev,unsigned int pipe)1238*3f2dd94aSFrançois Tigeot static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
1239*3f2dd94aSFrançois Tigeot 					  unsigned int pipe)
1240*3f2dd94aSFrançois Tigeot {
1241*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1242*3f2dd94aSFrançois Tigeot 
1243*3f2dd94aSFrançois Tigeot 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
1244*3f2dd94aSFrançois Tigeot 	if (!dev->num_crtcs)
1245*3f2dd94aSFrançois Tigeot 		return;
1246*3f2dd94aSFrançois Tigeot 
1247*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
1248*3f2dd94aSFrançois Tigeot 		return;
1249*3f2dd94aSFrançois Tigeot 
1250*3f2dd94aSFrançois Tigeot 	/*
1251*3f2dd94aSFrançois Tigeot 	 * To avoid all the problems that might happen if interrupts
1252*3f2dd94aSFrançois Tigeot 	 * were enabled/disabled around or between these calls, we just
1253*3f2dd94aSFrançois Tigeot 	 * have the kernel take a reference on the CRTC (just once though
1254*3f2dd94aSFrançois Tigeot 	 * to avoid corrupting the count if multiple, mismatch calls occur),
1255*3f2dd94aSFrançois Tigeot 	 * so that interrupts remain enabled in the interim.
1256*3f2dd94aSFrançois Tigeot 	 */
1257*3f2dd94aSFrançois Tigeot 	if (!vblank->inmodeset) {
1258*3f2dd94aSFrançois Tigeot 		vblank->inmodeset = 0x1;
1259*3f2dd94aSFrançois Tigeot 		if (drm_vblank_get(dev, pipe) == 0)
1260*3f2dd94aSFrançois Tigeot 			vblank->inmodeset |= 0x2;
1261*3f2dd94aSFrançois Tigeot 	}
1262*3f2dd94aSFrançois Tigeot }
1263*3f2dd94aSFrançois Tigeot 
drm_legacy_vblank_post_modeset(struct drm_device * dev,unsigned int pipe)1264*3f2dd94aSFrançois Tigeot static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
1265*3f2dd94aSFrançois Tigeot 					   unsigned int pipe)
1266*3f2dd94aSFrançois Tigeot {
1267*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1268*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
1269*3f2dd94aSFrançois Tigeot 
1270*3f2dd94aSFrançois Tigeot 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
1271*3f2dd94aSFrançois Tigeot 	if (!dev->num_crtcs)
1272*3f2dd94aSFrançois Tigeot 		return;
1273*3f2dd94aSFrançois Tigeot 
1274*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
1275*3f2dd94aSFrançois Tigeot 		return;
1276*3f2dd94aSFrançois Tigeot 
1277*3f2dd94aSFrançois Tigeot 	if (vblank->inmodeset) {
1278*3f2dd94aSFrançois Tigeot 		spin_lock_irqsave(&dev->vbl_lock, irqflags);
1279*3f2dd94aSFrançois Tigeot 		drm_reset_vblank_timestamp(dev, pipe);
1280*3f2dd94aSFrançois Tigeot 		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1281*3f2dd94aSFrançois Tigeot 
1282*3f2dd94aSFrançois Tigeot 		if (vblank->inmodeset & 0x2)
1283*3f2dd94aSFrançois Tigeot 			drm_vblank_put(dev, pipe);
1284*3f2dd94aSFrançois Tigeot 
1285*3f2dd94aSFrançois Tigeot 		vblank->inmodeset = 0;
1286*3f2dd94aSFrançois Tigeot 	}
1287*3f2dd94aSFrançois Tigeot }
1288*3f2dd94aSFrançois Tigeot 
drm_legacy_modeset_ctl_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1289*3f2dd94aSFrançois Tigeot int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
1290*3f2dd94aSFrançois Tigeot 				 struct drm_file *file_priv)
1291*3f2dd94aSFrançois Tigeot {
1292*3f2dd94aSFrançois Tigeot 	struct drm_modeset_ctl *modeset = data;
1293*3f2dd94aSFrançois Tigeot 	unsigned int pipe;
1294*3f2dd94aSFrançois Tigeot 
1295*3f2dd94aSFrançois Tigeot 	/* If drm_vblank_init() hasn't been called yet, just no-op */
1296*3f2dd94aSFrançois Tigeot 	if (!dev->num_crtcs)
1297*3f2dd94aSFrançois Tigeot 		return 0;
1298*3f2dd94aSFrançois Tigeot 
1299*3f2dd94aSFrançois Tigeot 	/* KMS drivers handle this internally */
1300*3f2dd94aSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1301*3f2dd94aSFrançois Tigeot 		return 0;
1302*3f2dd94aSFrançois Tigeot 
1303*3f2dd94aSFrançois Tigeot 	pipe = modeset->crtc;
1304*3f2dd94aSFrançois Tigeot 	if (pipe >= dev->num_crtcs)
1305*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1306*3f2dd94aSFrançois Tigeot 
1307*3f2dd94aSFrançois Tigeot 	switch (modeset->cmd) {
1308*3f2dd94aSFrançois Tigeot 	case _DRM_PRE_MODESET:
1309*3f2dd94aSFrançois Tigeot 		drm_legacy_vblank_pre_modeset(dev, pipe);
1310*3f2dd94aSFrançois Tigeot 		break;
1311*3f2dd94aSFrançois Tigeot 	case _DRM_POST_MODESET:
1312*3f2dd94aSFrançois Tigeot 		drm_legacy_vblank_post_modeset(dev, pipe);
1313*3f2dd94aSFrançois Tigeot 		break;
1314*3f2dd94aSFrançois Tigeot 	default:
1315*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1316*3f2dd94aSFrançois Tigeot 	}
1317*3f2dd94aSFrançois Tigeot 
1318*3f2dd94aSFrançois Tigeot 	return 0;
1319*3f2dd94aSFrançois Tigeot }
1320*3f2dd94aSFrançois Tigeot 
vblank_passed(u64 seq,u64 ref)1321*3f2dd94aSFrançois Tigeot static inline bool vblank_passed(u64 seq, u64 ref)
1322*3f2dd94aSFrançois Tigeot {
1323*3f2dd94aSFrançois Tigeot 	return (seq - ref) <= (1 << 23);
1324*3f2dd94aSFrançois Tigeot }
1325*3f2dd94aSFrançois Tigeot 
drm_queue_vblank_event(struct drm_device * dev,unsigned int pipe,u64 req_seq,union drm_wait_vblank * vblwait,struct drm_file * file_priv)1326*3f2dd94aSFrançois Tigeot static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
1327*3f2dd94aSFrançois Tigeot 				  u64 req_seq,
1328*3f2dd94aSFrançois Tigeot 				  union drm_wait_vblank *vblwait,
1329*3f2dd94aSFrançois Tigeot 				  struct drm_file *file_priv)
1330*3f2dd94aSFrançois Tigeot {
1331*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1332*3f2dd94aSFrançois Tigeot 	struct drm_pending_vblank_event *e;
1333*3f2dd94aSFrançois Tigeot 	ktime_t now;
1334*3f2dd94aSFrançois Tigeot 	unsigned long flags;
1335*3f2dd94aSFrançois Tigeot 	u64 seq;
1336*3f2dd94aSFrançois Tigeot 	int ret;
1337*3f2dd94aSFrançois Tigeot 
1338*3f2dd94aSFrançois Tigeot 	e = kzalloc(sizeof(*e), GFP_KERNEL);
1339*3f2dd94aSFrançois Tigeot 	if (e == NULL) {
1340*3f2dd94aSFrançois Tigeot 		ret = -ENOMEM;
1341*3f2dd94aSFrançois Tigeot 		goto err_put;
1342*3f2dd94aSFrançois Tigeot 	}
1343*3f2dd94aSFrançois Tigeot 
1344*3f2dd94aSFrançois Tigeot 	e->pipe = pipe;
1345*3f2dd94aSFrançois Tigeot 	e->event.base.type = DRM_EVENT_VBLANK;
1346*3f2dd94aSFrançois Tigeot 	e->event.base.length = sizeof(e->event.vbl);
1347*3f2dd94aSFrançois Tigeot 	e->event.vbl.user_data = vblwait->request.signal;
1348*3f2dd94aSFrançois Tigeot 	e->event.vbl.crtc_id = 0;
1349*3f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1350*3f2dd94aSFrançois Tigeot 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1351*3f2dd94aSFrançois Tigeot 		if (crtc)
1352*3f2dd94aSFrançois Tigeot 			e->event.vbl.crtc_id = crtc->base.id;
1353*3f2dd94aSFrançois Tigeot 	}
1354*3f2dd94aSFrançois Tigeot 
1355*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, flags);
1356*3f2dd94aSFrançois Tigeot 
1357*3f2dd94aSFrançois Tigeot 	/*
1358*3f2dd94aSFrançois Tigeot 	 * drm_crtc_vblank_off() might have been called after we called
1359*3f2dd94aSFrançois Tigeot 	 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1360*3f2dd94aSFrançois Tigeot 	 * vblank disable, so no need for further locking.  The reference from
1361*3f2dd94aSFrançois Tigeot 	 * drm_vblank_get() protects against vblank disable from another source.
1362*3f2dd94aSFrançois Tigeot 	 */
1363*3f2dd94aSFrançois Tigeot 	if (!READ_ONCE(vblank->enabled)) {
1364*3f2dd94aSFrançois Tigeot 		ret = -EINVAL;
1365*3f2dd94aSFrançois Tigeot 		goto err_unlock;
1366*3f2dd94aSFrançois Tigeot 	}
1367*3f2dd94aSFrançois Tigeot 
1368*3f2dd94aSFrançois Tigeot 	ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1369*3f2dd94aSFrançois Tigeot 					    &e->event.base);
1370*3f2dd94aSFrançois Tigeot 
1371*3f2dd94aSFrançois Tigeot 	if (ret)
1372*3f2dd94aSFrançois Tigeot 		goto err_unlock;
1373*3f2dd94aSFrançois Tigeot 
1374*3f2dd94aSFrançois Tigeot 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1375*3f2dd94aSFrançois Tigeot 
1376*3f2dd94aSFrançois Tigeot 	DRM_DEBUG("event on vblank count %llu, current %llu, crtc %u\n",
1377*3f2dd94aSFrançois Tigeot 		  req_seq, seq, pipe);
1378*3f2dd94aSFrançois Tigeot 
1379*3f2dd94aSFrançois Tigeot 	trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
1380*3f2dd94aSFrançois Tigeot 
1381*3f2dd94aSFrançois Tigeot 	e->sequence = req_seq;
1382*3f2dd94aSFrançois Tigeot 	if (vblank_passed(seq, req_seq)) {
1383*3f2dd94aSFrançois Tigeot 		drm_vblank_put(dev, pipe);
1384*3f2dd94aSFrançois Tigeot 		send_vblank_event(dev, e, seq, now);
1385*3f2dd94aSFrançois Tigeot 		vblwait->reply.sequence = seq;
1386*3f2dd94aSFrançois Tigeot 	} else {
1387*3f2dd94aSFrançois Tigeot 		/* drm_handle_vblank_events will call drm_vblank_put */
1388*3f2dd94aSFrançois Tigeot 		list_add_tail(&e->base.link, &dev->vblank_event_list);
1389*3f2dd94aSFrançois Tigeot 		vblwait->reply.sequence = req_seq;
1390*3f2dd94aSFrançois Tigeot 	}
1391*3f2dd94aSFrançois Tigeot 
1392*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, flags);
1393*3f2dd94aSFrançois Tigeot 
1394*3f2dd94aSFrançois Tigeot 	return 0;
1395*3f2dd94aSFrançois Tigeot 
1396*3f2dd94aSFrançois Tigeot err_unlock:
1397*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, flags);
1398*3f2dd94aSFrançois Tigeot 	kfree(e);
1399*3f2dd94aSFrançois Tigeot err_put:
1400*3f2dd94aSFrançois Tigeot 	drm_vblank_put(dev, pipe);
1401*3f2dd94aSFrançois Tigeot 	return ret;
1402*3f2dd94aSFrançois Tigeot }
1403*3f2dd94aSFrançois Tigeot 
drm_wait_vblank_is_query(union drm_wait_vblank * vblwait)1404*3f2dd94aSFrançois Tigeot static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
1405*3f2dd94aSFrançois Tigeot {
1406*3f2dd94aSFrançois Tigeot 	if (vblwait->request.sequence)
1407*3f2dd94aSFrançois Tigeot 		return false;
1408*3f2dd94aSFrançois Tigeot 
1409*3f2dd94aSFrançois Tigeot 	return _DRM_VBLANK_RELATIVE ==
1410*3f2dd94aSFrançois Tigeot 		(vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
1411*3f2dd94aSFrançois Tigeot 					  _DRM_VBLANK_EVENT |
1412*3f2dd94aSFrançois Tigeot 					  _DRM_VBLANK_NEXTONMISS));
1413*3f2dd94aSFrançois Tigeot }
1414*3f2dd94aSFrançois Tigeot 
1415*3f2dd94aSFrançois Tigeot /*
1416*3f2dd94aSFrançois Tigeot  * Widen a 32-bit param to 64-bits.
1417*3f2dd94aSFrançois Tigeot  *
1418*3f2dd94aSFrançois Tigeot  * \param narrow 32-bit value (missing upper 32 bits)
1419*3f2dd94aSFrançois Tigeot  * \param near 64-bit value that should be 'close' to near
1420*3f2dd94aSFrançois Tigeot  *
1421*3f2dd94aSFrançois Tigeot  * This function returns a 64-bit value using the lower 32-bits from
1422*3f2dd94aSFrançois Tigeot  * 'narrow' and constructing the upper 32-bits so that the result is
1423*3f2dd94aSFrançois Tigeot  * as close as possible to 'near'.
1424*3f2dd94aSFrançois Tigeot  */
1425*3f2dd94aSFrançois Tigeot 
widen_32_to_64(u32 narrow,u64 near)1426*3f2dd94aSFrançois Tigeot static u64 widen_32_to_64(u32 narrow, u64 near)
1427*3f2dd94aSFrançois Tigeot {
1428*3f2dd94aSFrançois Tigeot 	return near + (s32) (narrow - near);
1429*3f2dd94aSFrançois Tigeot }
1430*3f2dd94aSFrançois Tigeot 
drm_wait_vblank_reply(struct drm_device * dev,unsigned int pipe,struct drm_wait_vblank_reply * reply)1431*3f2dd94aSFrançois Tigeot static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
1432*3f2dd94aSFrançois Tigeot 				  struct drm_wait_vblank_reply *reply)
1433*3f2dd94aSFrançois Tigeot {
1434*3f2dd94aSFrançois Tigeot 	ktime_t now;
1435*3f2dd94aSFrançois Tigeot 	struct timespec64 ts;
1436*3f2dd94aSFrançois Tigeot 
1437*3f2dd94aSFrançois Tigeot 	/*
1438*3f2dd94aSFrançois Tigeot 	 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
1439*3f2dd94aSFrançois Tigeot 	 * to store the seconds. This is safe as we always use monotonic
1440*3f2dd94aSFrançois Tigeot 	 * timestamps since linux-4.15.
1441*3f2dd94aSFrançois Tigeot 	 */
1442*3f2dd94aSFrançois Tigeot 	reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1443*3f2dd94aSFrançois Tigeot 	ts = ktime_to_timespec64(now);
1444*3f2dd94aSFrançois Tigeot 	reply->tval_sec = (u32)ts.tv_sec;
1445*3f2dd94aSFrançois Tigeot 	reply->tval_usec = ts.tv_nsec / 1000;
1446*3f2dd94aSFrançois Tigeot }
1447*3f2dd94aSFrançois Tigeot 
drm_wait_vblank_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1448*3f2dd94aSFrançois Tigeot int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
1449*3f2dd94aSFrançois Tigeot 			  struct drm_file *file_priv)
1450*3f2dd94aSFrançois Tigeot {
1451*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc;
1452*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank;
1453*3f2dd94aSFrançois Tigeot 	union drm_wait_vblank *vblwait = data;
1454*3f2dd94aSFrançois Tigeot 	int ret;
1455*3f2dd94aSFrançois Tigeot 	u64 req_seq, seq;
1456*3f2dd94aSFrançois Tigeot 	unsigned int pipe_index;
1457*3f2dd94aSFrançois Tigeot 	unsigned int flags, pipe, high_pipe;
1458*3f2dd94aSFrançois Tigeot 
1459*3f2dd94aSFrançois Tigeot 	if (!dev->irq_enabled)
1460*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1461*3f2dd94aSFrançois Tigeot 
1462*3f2dd94aSFrançois Tigeot 	if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1463*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1464*3f2dd94aSFrançois Tigeot 
1465*3f2dd94aSFrançois Tigeot 	if (vblwait->request.type &
1466*3f2dd94aSFrançois Tigeot 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1467*3f2dd94aSFrançois Tigeot 	      _DRM_VBLANK_HIGH_CRTC_MASK)) {
1468*3f2dd94aSFrançois Tigeot 		DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1469*3f2dd94aSFrançois Tigeot 			  vblwait->request.type,
1470*3f2dd94aSFrançois Tigeot 			  (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1471*3f2dd94aSFrançois Tigeot 			   _DRM_VBLANK_HIGH_CRTC_MASK));
1472*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1473*3f2dd94aSFrançois Tigeot 	}
1474*3f2dd94aSFrançois Tigeot 
1475*3f2dd94aSFrançois Tigeot 	flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1476*3f2dd94aSFrançois Tigeot 	high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1477*3f2dd94aSFrançois Tigeot 	if (high_pipe)
1478*3f2dd94aSFrançois Tigeot 		pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1479*3f2dd94aSFrançois Tigeot 	else
1480*3f2dd94aSFrançois Tigeot 		pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1481*3f2dd94aSFrançois Tigeot 
1482*3f2dd94aSFrançois Tigeot 	/* Convert lease-relative crtc index into global crtc index */
1483*3f2dd94aSFrançois Tigeot 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1484*3f2dd94aSFrançois Tigeot 		pipe = 0;
1485*3f2dd94aSFrançois Tigeot 		drm_for_each_crtc(crtc, dev) {
1486*3f2dd94aSFrançois Tigeot 			if (drm_lease_held(file_priv, crtc->base.id)) {
1487*3f2dd94aSFrançois Tigeot 				if (pipe_index == 0)
1488*3f2dd94aSFrançois Tigeot 					break;
1489*3f2dd94aSFrançois Tigeot 				pipe_index--;
1490*3f2dd94aSFrançois Tigeot 			}
1491*3f2dd94aSFrançois Tigeot 			pipe++;
1492*3f2dd94aSFrançois Tigeot 		}
1493*3f2dd94aSFrançois Tigeot 	} else {
1494*3f2dd94aSFrançois Tigeot 		pipe = pipe_index;
1495*3f2dd94aSFrançois Tigeot 	}
1496*3f2dd94aSFrançois Tigeot 
1497*3f2dd94aSFrançois Tigeot 	if (pipe >= dev->num_crtcs)
1498*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1499*3f2dd94aSFrançois Tigeot 
1500*3f2dd94aSFrançois Tigeot 	vblank = &dev->vblank[pipe];
1501*3f2dd94aSFrançois Tigeot 
1502*3f2dd94aSFrançois Tigeot 	/* If the counter is currently enabled and accurate, short-circuit
1503*3f2dd94aSFrançois Tigeot 	 * queries to return the cached timestamp of the last vblank.
1504*3f2dd94aSFrançois Tigeot 	 */
1505*3f2dd94aSFrançois Tigeot 	if (dev->vblank_disable_immediate &&
1506*3f2dd94aSFrançois Tigeot 	    drm_wait_vblank_is_query(vblwait) &&
1507*3f2dd94aSFrançois Tigeot 	    READ_ONCE(vblank->enabled)) {
1508*3f2dd94aSFrançois Tigeot 		drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
1509*3f2dd94aSFrançois Tigeot 		return 0;
1510*3f2dd94aSFrançois Tigeot 	}
1511*3f2dd94aSFrançois Tigeot 
1512*3f2dd94aSFrançois Tigeot 	ret = drm_vblank_get(dev, pipe);
1513*3f2dd94aSFrançois Tigeot 	if (ret) {
1514*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1515*3f2dd94aSFrançois Tigeot 		return ret;
1516*3f2dd94aSFrançois Tigeot 	}
1517*3f2dd94aSFrançois Tigeot 	seq = drm_vblank_count(dev, pipe);
1518*3f2dd94aSFrançois Tigeot 
1519*3f2dd94aSFrançois Tigeot 	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1520*3f2dd94aSFrançois Tigeot 	case _DRM_VBLANK_RELATIVE:
1521*3f2dd94aSFrançois Tigeot 		req_seq = seq + vblwait->request.sequence;
1522*3f2dd94aSFrançois Tigeot 		vblwait->request.sequence = req_seq;
1523*3f2dd94aSFrançois Tigeot 		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1524*3f2dd94aSFrançois Tigeot 		break;
1525*3f2dd94aSFrançois Tigeot 	case _DRM_VBLANK_ABSOLUTE:
1526*3f2dd94aSFrançois Tigeot 		req_seq = widen_32_to_64(vblwait->request.sequence, seq);
1527*3f2dd94aSFrançois Tigeot 		break;
1528*3f2dd94aSFrançois Tigeot 	default:
1529*3f2dd94aSFrançois Tigeot 		ret = -EINVAL;
1530*3f2dd94aSFrançois Tigeot 		goto done;
1531*3f2dd94aSFrançois Tigeot 	}
1532*3f2dd94aSFrançois Tigeot 
1533*3f2dd94aSFrançois Tigeot 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1534*3f2dd94aSFrançois Tigeot 	    vblank_passed(seq, req_seq)) {
1535*3f2dd94aSFrançois Tigeot 		req_seq = seq + 1;
1536*3f2dd94aSFrançois Tigeot 		vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
1537*3f2dd94aSFrançois Tigeot 		vblwait->request.sequence = req_seq;
1538*3f2dd94aSFrançois Tigeot 	}
1539*3f2dd94aSFrançois Tigeot 
1540*3f2dd94aSFrançois Tigeot 	if (flags & _DRM_VBLANK_EVENT) {
1541*3f2dd94aSFrançois Tigeot 		/* must hold on to the vblank ref until the event fires
1542*3f2dd94aSFrançois Tigeot 		 * drm_vblank_put will be called asynchronously
1543*3f2dd94aSFrançois Tigeot 		 */
1544*3f2dd94aSFrançois Tigeot 		return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
1545*3f2dd94aSFrançois Tigeot 	}
1546*3f2dd94aSFrançois Tigeot 
1547*3f2dd94aSFrançois Tigeot 	if (req_seq != seq) {
1548*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("waiting on vblank count %llu, crtc %u\n",
1549*3f2dd94aSFrançois Tigeot 			  req_seq, pipe);
1550*3f2dd94aSFrançois Tigeot 		DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1551*3f2dd94aSFrançois Tigeot 			    vblank_passed(drm_vblank_count(dev, pipe),
1552*3f2dd94aSFrançois Tigeot 					  req_seq) ||
1553*3f2dd94aSFrançois Tigeot 			    !READ_ONCE(vblank->enabled));
1554*3f2dd94aSFrançois Tigeot 	}
1555*3f2dd94aSFrançois Tigeot 
1556*3f2dd94aSFrançois Tigeot 	if (ret != -EINTR) {
1557*3f2dd94aSFrançois Tigeot 		drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
1558*3f2dd94aSFrançois Tigeot 
1559*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("crtc %d returning %u to client\n",
1560*3f2dd94aSFrançois Tigeot 			  pipe, vblwait->reply.sequence);
1561*3f2dd94aSFrançois Tigeot 	} else {
1562*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
1563*3f2dd94aSFrançois Tigeot 	}
1564*3f2dd94aSFrançois Tigeot 
1565*3f2dd94aSFrançois Tigeot done:
1566*3f2dd94aSFrançois Tigeot 	drm_vblank_put(dev, pipe);
1567*3f2dd94aSFrançois Tigeot 	return ret;
1568*3f2dd94aSFrançois Tigeot }
1569*3f2dd94aSFrançois Tigeot 
drm_handle_vblank_events(struct drm_device * dev,unsigned int pipe)1570*3f2dd94aSFrançois Tigeot static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1571*3f2dd94aSFrançois Tigeot {
1572*3f2dd94aSFrançois Tigeot 	struct drm_pending_vblank_event *e, *t;
1573*3f2dd94aSFrançois Tigeot 	ktime_t now;
1574*3f2dd94aSFrançois Tigeot 	u64 seq;
1575*3f2dd94aSFrançois Tigeot 
1576*3f2dd94aSFrançois Tigeot 	assert_spin_locked(&dev->event_lock);
1577*3f2dd94aSFrançois Tigeot 
1578*3f2dd94aSFrançois Tigeot 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1579*3f2dd94aSFrançois Tigeot 
1580*3f2dd94aSFrançois Tigeot 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1581*3f2dd94aSFrançois Tigeot 		if (e->pipe != pipe)
1582*3f2dd94aSFrançois Tigeot 			continue;
1583*3f2dd94aSFrançois Tigeot 		if (!vblank_passed(seq, e->sequence))
1584*3f2dd94aSFrançois Tigeot 			continue;
1585*3f2dd94aSFrançois Tigeot 
1586*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("vblank event on %llu, current %llu\n",
1587*3f2dd94aSFrançois Tigeot 			  e->sequence, seq);
1588*3f2dd94aSFrançois Tigeot 
1589*3f2dd94aSFrançois Tigeot 		list_del(&e->base.link);
1590*3f2dd94aSFrançois Tigeot 		drm_vblank_put(dev, pipe);
1591*3f2dd94aSFrançois Tigeot 		send_vblank_event(dev, e, seq, now);
1592*3f2dd94aSFrançois Tigeot 	}
1593*3f2dd94aSFrançois Tigeot 
1594*3f2dd94aSFrançois Tigeot 	trace_drm_vblank_event(pipe, seq);
1595*3f2dd94aSFrançois Tigeot }
1596*3f2dd94aSFrançois Tigeot 
1597*3f2dd94aSFrançois Tigeot /**
1598*3f2dd94aSFrançois Tigeot  * drm_handle_vblank - handle a vblank event
1599*3f2dd94aSFrançois Tigeot  * @dev: DRM device
1600*3f2dd94aSFrançois Tigeot  * @pipe: index of CRTC where this event occurred
1601*3f2dd94aSFrançois Tigeot  *
1602*3f2dd94aSFrançois Tigeot  * Drivers should call this routine in their vblank interrupt handlers to
1603*3f2dd94aSFrançois Tigeot  * update the vblank counter and send any signals that may be pending.
1604*3f2dd94aSFrançois Tigeot  *
1605*3f2dd94aSFrançois Tigeot  * This is the legacy version of drm_crtc_handle_vblank().
1606*3f2dd94aSFrançois Tigeot  */
drm_handle_vblank(struct drm_device * dev,unsigned int pipe)1607*3f2dd94aSFrançois Tigeot bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1608*3f2dd94aSFrançois Tigeot {
1609*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1610*3f2dd94aSFrançois Tigeot 	unsigned long irqflags;
1611*3f2dd94aSFrançois Tigeot 	bool disable_irq;
1612*3f2dd94aSFrançois Tigeot 
1613*3f2dd94aSFrançois Tigeot 	if (WARN_ON_ONCE(!dev->num_crtcs))
1614*3f2dd94aSFrançois Tigeot 		return false;
1615*3f2dd94aSFrançois Tigeot 
1616*3f2dd94aSFrançois Tigeot 	if (WARN_ON(pipe >= dev->num_crtcs))
1617*3f2dd94aSFrançois Tigeot 		return false;
1618*3f2dd94aSFrançois Tigeot 
1619*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, irqflags);
1620*3f2dd94aSFrançois Tigeot 
1621*3f2dd94aSFrançois Tigeot 	/* Need timestamp lock to prevent concurrent execution with
1622*3f2dd94aSFrançois Tigeot 	 * vblank enable/disable, as this would cause inconsistent
1623*3f2dd94aSFrançois Tigeot 	 * or corrupted timestamps and vblank counts.
1624*3f2dd94aSFrançois Tigeot 	 */
1625*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
1626*3f2dd94aSFrançois Tigeot 
1627*3f2dd94aSFrançois Tigeot 	/* Vblank irq handling disabled. Nothing to do. */
1628*3f2dd94aSFrançois Tigeot 	if (!vblank->enabled) {
1629*3f2dd94aSFrançois Tigeot 		lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1630*3f2dd94aSFrançois Tigeot 		spin_unlock_irqrestore(&dev->event_lock, irqflags);
1631*3f2dd94aSFrançois Tigeot 		return false;
1632*3f2dd94aSFrançois Tigeot 	}
1633*3f2dd94aSFrançois Tigeot 
1634*3f2dd94aSFrançois Tigeot 	drm_update_vblank_count(dev, pipe, true);
1635*3f2dd94aSFrançois Tigeot 
1636*3f2dd94aSFrançois Tigeot 	lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1637*3f2dd94aSFrançois Tigeot 
1638*3f2dd94aSFrançois Tigeot 	wake_up(&vblank->queue);
1639*3f2dd94aSFrançois Tigeot 
1640*3f2dd94aSFrançois Tigeot 	/* With instant-off, we defer disabling the interrupt until after
1641*3f2dd94aSFrançois Tigeot 	 * we finish processing the following vblank after all events have
1642*3f2dd94aSFrançois Tigeot 	 * been signaled. The disable has to be last (after
1643*3f2dd94aSFrançois Tigeot 	 * drm_handle_vblank_events) so that the timestamp is always accurate.
1644*3f2dd94aSFrançois Tigeot 	 */
1645*3f2dd94aSFrançois Tigeot 	disable_irq = (dev->vblank_disable_immediate &&
1646*3f2dd94aSFrançois Tigeot 		       drm_vblank_offdelay > 0 &&
1647*3f2dd94aSFrançois Tigeot 		       !atomic_read(&vblank->refcount));
1648*3f2dd94aSFrançois Tigeot 
1649*3f2dd94aSFrançois Tigeot 	drm_handle_vblank_events(dev, pipe);
1650*3f2dd94aSFrançois Tigeot 
1651*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
1652*3f2dd94aSFrançois Tigeot 
1653*3f2dd94aSFrançois Tigeot 	if (disable_irq)
1654*3f2dd94aSFrançois Tigeot 		vblank_disable_fn(&vblank->disable_timer);
1655*3f2dd94aSFrançois Tigeot 
1656*3f2dd94aSFrançois Tigeot 	return true;
1657*3f2dd94aSFrançois Tigeot }
1658*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_handle_vblank);
1659*3f2dd94aSFrançois Tigeot 
1660*3f2dd94aSFrançois Tigeot /**
1661*3f2dd94aSFrançois Tigeot  * drm_crtc_handle_vblank - handle a vblank event
1662*3f2dd94aSFrançois Tigeot  * @crtc: where this event occurred
1663*3f2dd94aSFrançois Tigeot  *
1664*3f2dd94aSFrançois Tigeot  * Drivers should call this routine in their vblank interrupt handlers to
1665*3f2dd94aSFrançois Tigeot  * update the vblank counter and send any signals that may be pending.
1666*3f2dd94aSFrançois Tigeot  *
1667*3f2dd94aSFrançois Tigeot  * This is the native KMS version of drm_handle_vblank().
1668*3f2dd94aSFrançois Tigeot  *
1669*3f2dd94aSFrançois Tigeot  * Returns:
1670*3f2dd94aSFrançois Tigeot  * True if the event was successfully handled, false on failure.
1671*3f2dd94aSFrançois Tigeot  */
drm_crtc_handle_vblank(struct drm_crtc * crtc)1672*3f2dd94aSFrançois Tigeot bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1673*3f2dd94aSFrançois Tigeot {
1674*3f2dd94aSFrançois Tigeot 	return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1675*3f2dd94aSFrançois Tigeot }
1676*3f2dd94aSFrançois Tigeot EXPORT_SYMBOL(drm_crtc_handle_vblank);
1677*3f2dd94aSFrançois Tigeot 
1678*3f2dd94aSFrançois Tigeot /*
1679*3f2dd94aSFrançois Tigeot  * Get crtc VBLANK count.
1680*3f2dd94aSFrançois Tigeot  *
1681*3f2dd94aSFrançois Tigeot  * \param dev DRM device
1682*3f2dd94aSFrançois Tigeot  * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
1683*3f2dd94aSFrançois Tigeot  * \param file_priv drm file private for the user's open file descriptor
1684*3f2dd94aSFrançois Tigeot  */
1685*3f2dd94aSFrançois Tigeot 
drm_crtc_get_sequence_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1686*3f2dd94aSFrançois Tigeot int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
1687*3f2dd94aSFrançois Tigeot 				struct drm_file *file_priv)
1688*3f2dd94aSFrançois Tigeot {
1689*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc;
1690*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank;
1691*3f2dd94aSFrançois Tigeot 	int pipe;
1692*3f2dd94aSFrançois Tigeot 	struct drm_crtc_get_sequence *get_seq = data;
1693*3f2dd94aSFrançois Tigeot 	ktime_t now;
1694*3f2dd94aSFrançois Tigeot 	bool vblank_enabled;
1695*3f2dd94aSFrançois Tigeot 	int ret;
1696*3f2dd94aSFrançois Tigeot 
1697*3f2dd94aSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1698*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1699*3f2dd94aSFrançois Tigeot 
1700*3f2dd94aSFrançois Tigeot 	if (!dev->irq_enabled)
1701*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1702*3f2dd94aSFrançois Tigeot 
1703*3f2dd94aSFrançois Tigeot 	crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
1704*3f2dd94aSFrançois Tigeot 	if (!crtc)
1705*3f2dd94aSFrançois Tigeot 		return -ENOENT;
1706*3f2dd94aSFrançois Tigeot 
1707*3f2dd94aSFrançois Tigeot 	pipe = drm_crtc_index(crtc);
1708*3f2dd94aSFrançois Tigeot 
1709*3f2dd94aSFrançois Tigeot 	vblank = &dev->vblank[pipe];
1710*3f2dd94aSFrançois Tigeot 	vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
1711*3f2dd94aSFrançois Tigeot 
1712*3f2dd94aSFrançois Tigeot 	if (!vblank_enabled) {
1713*3f2dd94aSFrançois Tigeot 		ret = drm_crtc_vblank_get(crtc);
1714*3f2dd94aSFrançois Tigeot 		if (ret) {
1715*3f2dd94aSFrançois Tigeot 			DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1716*3f2dd94aSFrançois Tigeot 			return ret;
1717*3f2dd94aSFrançois Tigeot 		}
1718*3f2dd94aSFrançois Tigeot 	}
1719*3f2dd94aSFrançois Tigeot 	drm_modeset_lock(&crtc->mutex, NULL);
1720*3f2dd94aSFrançois Tigeot 	if (crtc->state)
1721*3f2dd94aSFrançois Tigeot 		get_seq->active = crtc->state->enable;
1722*3f2dd94aSFrançois Tigeot 	else
1723*3f2dd94aSFrançois Tigeot 		get_seq->active = crtc->enabled;
1724*3f2dd94aSFrançois Tigeot 	drm_modeset_unlock(&crtc->mutex);
1725*3f2dd94aSFrançois Tigeot 	get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1726*3f2dd94aSFrançois Tigeot 	get_seq->sequence_ns = ktime_to_ns(now);
1727*3f2dd94aSFrançois Tigeot 	if (!vblank_enabled)
1728*3f2dd94aSFrançois Tigeot 		drm_crtc_vblank_put(crtc);
1729*3f2dd94aSFrançois Tigeot 	return 0;
1730*3f2dd94aSFrançois Tigeot }
1731*3f2dd94aSFrançois Tigeot 
1732*3f2dd94aSFrançois Tigeot /*
1733*3f2dd94aSFrançois Tigeot  * Queue a event for VBLANK sequence
1734*3f2dd94aSFrançois Tigeot  *
1735*3f2dd94aSFrançois Tigeot  * \param dev DRM device
1736*3f2dd94aSFrançois Tigeot  * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
1737*3f2dd94aSFrançois Tigeot  * \param file_priv drm file private for the user's open file descriptor
1738*3f2dd94aSFrançois Tigeot  */
1739*3f2dd94aSFrançois Tigeot 
drm_crtc_queue_sequence_ioctl(struct drm_device * dev,void * data,struct drm_file * file_priv)1740*3f2dd94aSFrançois Tigeot int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
1741*3f2dd94aSFrançois Tigeot 				  struct drm_file *file_priv)
1742*3f2dd94aSFrançois Tigeot {
1743*3f2dd94aSFrançois Tigeot 	struct drm_crtc *crtc;
1744*3f2dd94aSFrançois Tigeot 	struct drm_vblank_crtc *vblank;
1745*3f2dd94aSFrançois Tigeot 	int pipe;
1746*3f2dd94aSFrançois Tigeot 	struct drm_crtc_queue_sequence *queue_seq = data;
1747*3f2dd94aSFrançois Tigeot 	ktime_t now;
1748*3f2dd94aSFrançois Tigeot 	struct drm_pending_vblank_event *e;
1749*3f2dd94aSFrançois Tigeot 	u32 flags;
1750*3f2dd94aSFrançois Tigeot 	u64 seq;
1751*3f2dd94aSFrançois Tigeot 	u64 req_seq;
1752*3f2dd94aSFrançois Tigeot 	int ret;
1753*3f2dd94aSFrançois Tigeot 	unsigned long spin_flags;
1754*3f2dd94aSFrançois Tigeot 
1755*3f2dd94aSFrançois Tigeot 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
1756*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1757*3f2dd94aSFrançois Tigeot 
1758*3f2dd94aSFrançois Tigeot 	if (!dev->irq_enabled)
1759*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1760*3f2dd94aSFrançois Tigeot 
1761*3f2dd94aSFrançois Tigeot 	crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
1762*3f2dd94aSFrançois Tigeot 	if (!crtc)
1763*3f2dd94aSFrançois Tigeot 		return -ENOENT;
1764*3f2dd94aSFrançois Tigeot 
1765*3f2dd94aSFrançois Tigeot 	flags = queue_seq->flags;
1766*3f2dd94aSFrançois Tigeot 	/* Check valid flag bits */
1767*3f2dd94aSFrançois Tigeot 	if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
1768*3f2dd94aSFrançois Tigeot 		      DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
1769*3f2dd94aSFrançois Tigeot 		return -EINVAL;
1770*3f2dd94aSFrançois Tigeot 
1771*3f2dd94aSFrançois Tigeot 	pipe = drm_crtc_index(crtc);
1772*3f2dd94aSFrançois Tigeot 
1773*3f2dd94aSFrançois Tigeot 	vblank = &dev->vblank[pipe];
1774*3f2dd94aSFrançois Tigeot 
1775*3f2dd94aSFrançois Tigeot 	e = kzalloc(sizeof(*e), GFP_KERNEL);
1776*3f2dd94aSFrançois Tigeot 	if (e == NULL)
1777*3f2dd94aSFrançois Tigeot 		return -ENOMEM;
1778*3f2dd94aSFrançois Tigeot 
1779*3f2dd94aSFrançois Tigeot 	ret = drm_crtc_vblank_get(crtc);
1780*3f2dd94aSFrançois Tigeot 	if (ret) {
1781*3f2dd94aSFrançois Tigeot 		DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
1782*3f2dd94aSFrançois Tigeot 		goto err_free;
1783*3f2dd94aSFrançois Tigeot 	}
1784*3f2dd94aSFrançois Tigeot 
1785*3f2dd94aSFrançois Tigeot 	seq = drm_vblank_count_and_time(dev, pipe, &now);
1786*3f2dd94aSFrançois Tigeot 	req_seq = queue_seq->sequence;
1787*3f2dd94aSFrançois Tigeot 
1788*3f2dd94aSFrançois Tigeot 	if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
1789*3f2dd94aSFrançois Tigeot 		req_seq += seq;
1790*3f2dd94aSFrançois Tigeot 
1791*3f2dd94aSFrançois Tigeot 	if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && vblank_passed(seq, req_seq))
1792*3f2dd94aSFrançois Tigeot 		req_seq = seq + 1;
1793*3f2dd94aSFrançois Tigeot 
1794*3f2dd94aSFrançois Tigeot 	e->pipe = pipe;
1795*3f2dd94aSFrançois Tigeot 	e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
1796*3f2dd94aSFrançois Tigeot 	e->event.base.length = sizeof(e->event.seq);
1797*3f2dd94aSFrançois Tigeot 	e->event.seq.user_data = queue_seq->user_data;
1798*3f2dd94aSFrançois Tigeot 
1799*3f2dd94aSFrançois Tigeot 	spin_lock_irqsave(&dev->event_lock, spin_flags);
1800*3f2dd94aSFrançois Tigeot 
1801*3f2dd94aSFrançois Tigeot 	/*
1802*3f2dd94aSFrançois Tigeot 	 * drm_crtc_vblank_off() might have been called after we called
1803*3f2dd94aSFrançois Tigeot 	 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1804*3f2dd94aSFrançois Tigeot 	 * vblank disable, so no need for further locking.  The reference from
1805*3f2dd94aSFrançois Tigeot 	 * drm_crtc_vblank_get() protects against vblank disable from another source.
1806*3f2dd94aSFrançois Tigeot 	 */
1807*3f2dd94aSFrançois Tigeot 	if (!READ_ONCE(vblank->enabled)) {
1808*3f2dd94aSFrançois Tigeot 		ret = -EINVAL;
1809*3f2dd94aSFrançois Tigeot 		goto err_unlock;
1810*3f2dd94aSFrançois Tigeot 	}
1811*3f2dd94aSFrançois Tigeot 
1812*3f2dd94aSFrançois Tigeot 	ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1813*3f2dd94aSFrançois Tigeot 					    &e->event.base);
1814*3f2dd94aSFrançois Tigeot 
1815*3f2dd94aSFrançois Tigeot 	if (ret)
1816*3f2dd94aSFrançois Tigeot 		goto err_unlock;
1817*3f2dd94aSFrançois Tigeot 
1818*3f2dd94aSFrançois Tigeot 	e->sequence = req_seq;
1819*3f2dd94aSFrançois Tigeot 
1820*3f2dd94aSFrançois Tigeot 	if (vblank_passed(seq, req_seq)) {
1821*3f2dd94aSFrançois Tigeot 		drm_crtc_vblank_put(crtc);
1822*3f2dd94aSFrançois Tigeot 		send_vblank_event(dev, e, seq, now);
1823*3f2dd94aSFrançois Tigeot 		queue_seq->sequence = seq;
1824*3f2dd94aSFrançois Tigeot 	} else {
1825*3f2dd94aSFrançois Tigeot 		/* drm_handle_vblank_events will call drm_vblank_put */
1826*3f2dd94aSFrançois Tigeot 		list_add_tail(&e->base.link, &dev->vblank_event_list);
1827*3f2dd94aSFrançois Tigeot 		queue_seq->sequence = req_seq;
1828*3f2dd94aSFrançois Tigeot 	}
1829*3f2dd94aSFrançois Tigeot 
1830*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, spin_flags);
1831*3f2dd94aSFrançois Tigeot 	return 0;
1832*3f2dd94aSFrançois Tigeot 
1833*3f2dd94aSFrançois Tigeot err_unlock:
1834*3f2dd94aSFrançois Tigeot 	spin_unlock_irqrestore(&dev->event_lock, spin_flags);
1835*3f2dd94aSFrançois Tigeot 	drm_crtc_vblank_put(crtc);
1836*3f2dd94aSFrançois Tigeot err_free:
1837*3f2dd94aSFrançois Tigeot 	kfree(e);
1838*3f2dd94aSFrançois Tigeot 	return ret;
1839*3f2dd94aSFrançois Tigeot }
1840