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