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