blob: f135b79593dd206149223336550a51a2a52f489f [file] [log] [blame]
Daniel Vetter3ed43512017-05-31 11:21:46 +02001/*
2 * drm_irq.c IRQ and vblank support
3 *
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
Daniel Vetter3ed43512017-05-31 11:21:46 +020027#include <linux/export.h>
Lyude Paul5e6c2b42020-04-17 15:33:13 -040028#include <linux/kthread.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020029#include <linux/moduleparam.h>
Daniel Vetter3ed43512017-05-31 11:21:46 +020030
Sam Ravnborg0500c042019-05-26 19:35:35 +020031#include <drm/drm_crtc.h>
32#include <drm/drm_drv.h>
33#include <drm/drm_framebuffer.h>
Daniel Vetterc23d6862020-03-23 15:49:23 +010034#include <drm/drm_managed.h>
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +010035#include <drm/drm_modeset_helper_vtables.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020036#include <drm/drm_print.h>
Sam Ravnborg0500c042019-05-26 19:35:35 +020037#include <drm/drm_vblank.h>
38
Daniel Vetter3ed43512017-05-31 11:21:46 +020039#include "drm_internal.h"
Sam Ravnborg0500c042019-05-26 19:35:35 +020040#include "drm_trace.h"
Daniel Vetter3ed43512017-05-31 11:21:46 +020041
Daniel Vetter57d30232017-05-24 16:51:45 +020042/**
43 * DOC: vblank handling
44 *
Sam Ravnborg56b165f2020-04-06 21:47:44 +020045 * From the computer's perspective, every time the monitor displays
46 * a new frame the scanout engine has "scanned out" the display image
47 * from top to bottom, one row of pixels at a time. The current row
48 * of pixels is referred to as the current scanline.
49 *
50 * In addition to the display's visible area, there's usually a couple of
51 * extra scanlines which aren't actually displayed on the screen.
52 * These extra scanlines don't contain image data and are occasionally used
53 * for features like audio and infoframes. The region made up of these
54 * scanlines is referred to as the vertical blanking region, or vblank for
55 * short.
56 *
57 * For historical reference, the vertical blanking period was designed to
58 * give the electron gun (on CRTs) enough time to move back to the top of
59 * the screen to start scanning out the next frame. Similar for horizontal
60 * blanking periods. They were designed to give the electron gun enough
61 * time to move back to the other side of the screen to start scanning the
62 * next scanline.
63 *
64 * ::
65 *
66 *
67 * physical → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
68 * top of | |
69 * display | |
70 * | New frame |
71 * | |
72 * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓|
73 * |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| ← Scanline,
74 * |↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓| updates the
75 * | | frame as it
76 * | | travels down
77 * | | ("sacn out")
78 * | Old frame |
79 * | |
80 * | |
81 * | |
82 * | | physical
83 * | | bottom of
84 * vertical |⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽| ← display
85 * blanking ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
86 * region → ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
87 * ┆xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx┆
88 * start of → ⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽⎽
89 * new frame
90 *
91 * "Physical top of display" is the reference point for the high-precision/
92 * corrected timestamp.
93 *
94 * On a lot of display hardware, programming needs to take effect during the
95 * vertical blanking period so that settings like gamma, the image buffer
96 * buffer to be scanned out, etc. can safely be changed without showing
97 * any visual artifacts on the screen. In some unforgiving hardware, some of
98 * this programming has to both start and end in the same vblank. To help
99 * with the timing of the hardware programming, an interrupt is usually
100 * available to notify the driver when it can start the updating of registers.
101 * The interrupt is in this context named the vblank interrupt.
102 *
103 * The vblank interrupt may be fired at different points depending on the
104 * hardware. Some hardware implementations will fire the interrupt when the
105 * new frame start, other implementations will fire the interrupt at different
106 * points in time.
107 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200108 * Vertical blanking plays a major role in graphics rendering. To achieve
109 * tear-free display, users must synchronize page flips and/or rendering to
110 * vertical blanking. The DRM API offers ioctls to perform page flips
111 * synchronized to vertical blanking and wait for vertical blanking.
112 *
113 * The DRM core handles most of the vertical blanking management logic, which
114 * involves filtering out spurious interrupts, keeping race-free blanking
115 * counters, coping with counter wrap-around and resets and keeping use counts.
116 * It relies on the driver to generate vertical blanking interrupts and
117 * optionally provide a hardware vertical blanking counter.
118 *
119 * Drivers must initialize the vertical blanking handling core with a call to
120 * drm_vblank_init(). Minimally, a driver needs to implement
121 * &drm_crtc_funcs.enable_vblank and &drm_crtc_funcs.disable_vblank plus call
Matt Roper1e55a532019-02-01 17:23:26 -0800122 * drm_crtc_handle_vblank() in its vblank interrupt handler for working vblank
Daniel Vetter57d30232017-05-24 16:51:45 +0200123 * support.
124 *
125 * Vertical blanking interrupts can be enabled by the DRM core or by drivers
126 * themselves (for instance to handle page flipping operations). The DRM core
127 * maintains a vertical blanking use count to ensure that the interrupts are not
128 * disabled while a user still needs them. To increment the use count, drivers
129 * call drm_crtc_vblank_get() and release the vblank reference again with
130 * drm_crtc_vblank_put(). In between these two calls vblank interrupts are
131 * guaranteed to be enabled.
132 *
133 * On many hardware disabling the vblank interrupt cannot be done in a race-free
134 * manner, see &drm_driver.vblank_disable_immediate and
135 * &drm_driver.max_vblank_count. In that case the vblank core only disables the
136 * vblanks after a timer has expired, which can be configured through the
137 * ``vblankoffdelay`` module parameter.
Thomas Zimmermann7beb6912020-01-29 13:05:17 +0100138 *
139 * Drivers for hardware without support for vertical-blanking interrupts
140 * must not call drm_vblank_init(). For such drivers, atomic helpers will
141 * automatically generate fake vblank events as part of the display update.
142 * This functionality also can be controlled by the driver by enabling and
143 * disabling struct drm_crtc_state.no_vblank.
Daniel Vetter57d30232017-05-24 16:51:45 +0200144 */
145
Daniel Vetter3ed43512017-05-31 11:21:46 +0200146/* Retry timestamp calculation up to 3 times to satisfy
147 * drm_timestamp_precision before giving up.
148 */
149#define DRM_TIMESTAMP_MAXRETRIES 3
150
151/* Threshold in nanoseconds for detection of redundant
152 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
153 */
154#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
155
156static bool
157drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200158 ktime_t *tvblank, bool in_vblank_irq);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200159
160static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
161
Daniel Vetter3ed43512017-05-31 11:21:46 +0200162static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
163
164module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
165module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200166MODULE_PARM_DESC(vblankoffdelay, "Delay until vblank irq auto-disable [msecs] (0: never disable, <0: disable immediately)");
167MODULE_PARM_DESC(timestamp_precision_usec, "Max. error on timestamps [usecs]");
Daniel Vetter3ed43512017-05-31 11:21:46 +0200168
169static void store_vblank(struct drm_device *dev, unsigned int pipe,
170 u32 vblank_count_inc,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200171 ktime_t t_vblank, u32 last)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200172{
173 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
174
175 assert_spin_locked(&dev->vblank_time_lock);
176
177 vblank->last = last;
178
179 write_seqlock(&vblank->seqlock);
Arnd Bergmann67680d32017-10-11 17:20:12 +0200180 vblank->time = t_vblank;
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200181 atomic64_add(vblank_count_inc, &vblank->count);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200182 write_sequnlock(&vblank->seqlock);
183}
184
Ville Syrjäläed201512018-11-27 20:20:04 +0200185static u32 drm_max_vblank_count(struct drm_device *dev, unsigned int pipe)
186{
187 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
188
189 return vblank->max_vblank_count ?: dev->max_vblank_count;
190}
191
Daniel Vetter3ed43512017-05-31 11:21:46 +0200192/*
193 * "No hw counter" fallback implementation of .get_vblank_counter() hook,
194 * if there is no useable hardware frame counter available.
195 */
196static u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
197{
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200198 drm_WARN_ON_ONCE(dev, drm_max_vblank_count(dev, pipe) != 0);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200199 return 0;
200}
201
202static u32 __get_vblank_counter(struct drm_device *dev, unsigned int pipe)
203{
204 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
205 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
206
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200207 if (drm_WARN_ON(dev, !crtc))
Joe Moriarty4ffb8de2018-02-20 14:11:56 -0500208 return 0;
209
Daniel Vetter3ed43512017-05-31 11:21:46 +0200210 if (crtc->funcs->get_vblank_counter)
211 return crtc->funcs->get_vblank_counter(crtc);
Thomas Zimmermannf397d662020-01-23 14:59:42 +0100212 } else if (dev->driver->get_vblank_counter) {
Daniel Vetter3ed43512017-05-31 11:21:46 +0200213 return dev->driver->get_vblank_counter(dev, pipe);
Thomas Zimmermannf397d662020-01-23 14:59:42 +0100214 }
Daniel Vetter3ed43512017-05-31 11:21:46 +0200215
216 return drm_vblank_no_hw_counter(dev, pipe);
217}
218
219/*
220 * Reset the stored timestamp for the current vblank count to correspond
221 * to the last vblank occurred.
222 *
223 * Only to be called from drm_crtc_vblank_on().
224 *
225 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
226 * device vblank fields.
227 */
228static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
229{
230 u32 cur_vblank;
231 bool rc;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200232 ktime_t t_vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200233 int count = DRM_TIMESTAMP_MAXRETRIES;
234
235 spin_lock(&dev->vblank_time_lock);
236
237 /*
238 * sample the current counter to avoid random jumps
239 * when drm_vblank_enable() applies the diff
240 */
241 do {
242 cur_vblank = __get_vblank_counter(dev, pipe);
243 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
244 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
245
246 /*
247 * Only reinitialize corresponding vblank timestamp if high-precision query
248 * available and didn't fail. Otherwise reinitialize delayed at next vblank
249 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
250 */
251 if (!rc)
Arnd Bergmann67680d32017-10-11 17:20:12 +0200252 t_vblank = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200253
254 /*
255 * +1 to make sure user will never see the same
256 * vblank counter value before and after a modeset
257 */
Arnd Bergmann67680d32017-10-11 17:20:12 +0200258 store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200259
260 spin_unlock(&dev->vblank_time_lock);
261}
262
263/*
264 * Call back into the driver to update the appropriate vblank counter
265 * (specified by @pipe). Deal with wraparound, if it occurred, and
266 * update the last read value so we can deal with wraparound on the next
267 * call if necessary.
268 *
269 * Only necessary when going from off->on, to account for frames we
270 * didn't get an interrupt for.
271 *
272 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
273 * device vblank fields.
274 */
275static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
276 bool in_vblank_irq)
277{
278 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
279 u32 cur_vblank, diff;
280 bool rc;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200281 ktime_t t_vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200282 int count = DRM_TIMESTAMP_MAXRETRIES;
283 int framedur_ns = vblank->framedur_ns;
Ville Syrjäläed201512018-11-27 20:20:04 +0200284 u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200285
286 /*
287 * Interrupts were disabled prior to this call, so deal with counter
288 * wrap if needed.
289 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
290 * here if the register is small or we had vblank interrupts off for
291 * a long time.
292 *
293 * We repeat the hardware vblank counter & timestamp query until
294 * we get consistent results. This to prevent races between gpu
295 * updating its hardware counter while we are retrieving the
296 * corresponding vblank timestamp.
297 */
298 do {
299 cur_vblank = __get_vblank_counter(dev, pipe);
300 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
301 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
302
Ville Syrjäläed201512018-11-27 20:20:04 +0200303 if (max_vblank_count) {
Daniel Vetter3ed43512017-05-31 11:21:46 +0200304 /* trust the hw counter when it's around */
Ville Syrjäläed201512018-11-27 20:20:04 +0200305 diff = (cur_vblank - vblank->last) & max_vblank_count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200306 } else if (rc && framedur_ns) {
Arnd Bergmann67680d32017-10-11 17:20:12 +0200307 u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
Daniel Vetter3ed43512017-05-31 11:21:46 +0200308
309 /*
310 * Figure out how many vblanks we've missed based
311 * on the difference in the timestamps and the
312 * frame/field duration.
313 */
Oleg Vasilev41ff5bd2019-06-13 15:18:00 +0300314
Sam Ravnborg02149a72020-05-23 09:12:23 +0200315 drm_dbg_vbl(dev, "crtc %u: Calculating number of vblanks."
316 " diff_ns = %lld, framedur_ns = %d)\n",
317 pipe, (long long)diff_ns, framedur_ns);
Oleg Vasilev41ff5bd2019-06-13 15:18:00 +0300318
Daniel Vetter3ed43512017-05-31 11:21:46 +0200319 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
320
321 if (diff == 0 && in_vblank_irq)
Sam Ravnborg02149a72020-05-23 09:12:23 +0200322 drm_dbg_vbl(dev, "crtc %u: Redundant vblirq ignored\n",
323 pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200324 } else {
325 /* some kind of default for drivers w/o accurate vbl timestamping */
326 diff = in_vblank_irq ? 1 : 0;
327 }
328
329 /*
330 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
331 * interval? If so then vblank irqs keep running and it will likely
332 * happen that the hardware vblank counter is not trustworthy as it
333 * might reset at some point in that interval and vblank timestamps
334 * are not trustworthy either in that interval. Iow. this can result
335 * in a bogus diff >> 1 which must be avoided as it would cause
336 * random large forward jumps of the software vblank counter.
337 */
338 if (diff > 1 && (vblank->inmodeset & 0x2)) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200339 drm_dbg_vbl(dev,
340 "clamping vblank bump to 1 on crtc %u: diffr=%u"
341 " due to pre-modeset.\n", pipe, diff);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200342 diff = 1;
343 }
344
Sam Ravnborg02149a72020-05-23 09:12:23 +0200345 drm_dbg_vbl(dev, "updating vblank count on crtc %u:"
346 " current=%llu, diff=%u, hw=%u hw_last=%u\n",
347 pipe, (unsigned long long)atomic64_read(&vblank->count),
348 diff, cur_vblank, vblank->last);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200349
350 if (diff == 0) {
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200351 drm_WARN_ON_ONCE(dev, cur_vblank != vblank->last);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200352 return;
353 }
354
355 /*
356 * Only reinitialize corresponding vblank timestamp if high-precision query
357 * available and didn't fail, or we were called from the vblank interrupt.
358 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
359 * for now, to mark the vblanktimestamp as invalid.
360 */
Laurent Pinchart138b87f2017-06-29 15:37:20 +0300361 if (!rc && !in_vblank_irq)
Arnd Bergmann67680d32017-10-11 17:20:12 +0200362 t_vblank = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200363
Arnd Bergmann67680d32017-10-11 17:20:12 +0200364 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200365}
366
Lyude Paul5e6c2b42020-04-17 15:33:13 -0400367u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200368{
369 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200370 u64 count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200371
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200372 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +0200373 return 0;
374
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200375 count = atomic64_read(&vblank->count);
376
377 /*
378 * This read barrier corresponds to the implicit write barrier of the
379 * write seqlock in store_vblank(). Note that this is the only place
380 * where we need an explicit barrier, since all other access goes
381 * through drm_vblank_count_and_time(), which already has the required
382 * read barrier curtesy of the read seqlock.
383 */
384 smp_rmb();
385
386 return count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200387}
388
389/**
Daniel Vetterca814b22017-05-24 16:51:47 +0200390 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
Daniel Vetter3ed43512017-05-31 11:21:46 +0200391 * @crtc: which counter to retrieve
392 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200393 * This function is similar to drm_crtc_vblank_count() but this function
394 * interpolates to handle a race with vblank interrupts using the high precision
395 * timestamping support.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200396 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200397 * This is mostly useful for hardware that can obtain the scanout position, but
398 * doesn't have a hardware frame counter.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200399 */
Dhinakaran Pandiyan3b765c02018-02-02 21:12:53 -0800400u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200401{
402 struct drm_device *dev = crtc->dev;
403 unsigned int pipe = drm_crtc_index(crtc);
Dhinakaran Pandiyan3b765c02018-02-02 21:12:53 -0800404 u64 vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200405 unsigned long flags;
406
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200407 drm_WARN_ONCE(dev, drm_debug_enabled(DRM_UT_VBL) &&
408 !crtc->funcs->get_vblank_timestamp,
409 "This function requires support for accurate vblank timestamps.");
Daniel Vetter3ed43512017-05-31 11:21:46 +0200410
411 spin_lock_irqsave(&dev->vblank_time_lock, flags);
412
413 drm_update_vblank_count(dev, pipe, false);
414 vblank = drm_vblank_count(dev, pipe);
415
416 spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
417
418 return vblank;
419}
Daniel Vetterca814b22017-05-24 16:51:47 +0200420EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200421
422static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
423{
424 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
425 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
426
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200427 if (drm_WARN_ON(dev, !crtc))
Joe Moriarty4ffb8de2018-02-20 14:11:56 -0500428 return;
429
Thomas Zimmermannf397d662020-01-23 14:59:42 +0100430 if (crtc->funcs->disable_vblank)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200431 crtc->funcs->disable_vblank(crtc);
Thomas Zimmermannf397d662020-01-23 14:59:42 +0100432 } else {
433 dev->driver->disable_vblank(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200434 }
Daniel Vetter3ed43512017-05-31 11:21:46 +0200435}
436
437/*
438 * Disable vblank irq's on crtc, make sure that last vblank count
439 * of hardware and corresponding consistent software vblank counter
440 * are preserved, even if there are any spurious vblank irq's after
441 * disable.
442 */
443void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
444{
445 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
446 unsigned long irqflags;
447
448 assert_spin_locked(&dev->vbl_lock);
449
450 /* Prevent vblank irq processing while disabling vblank irqs,
451 * so no updates of timestamps or count can happen after we've
452 * disabled. Needed to prevent races in case of delayed irq's.
453 */
454 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
455
456 /*
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800457 * Update vblank count and disable vblank interrupts only if the
458 * interrupts were enabled. This avoids calling the ->disable_vblank()
459 * operation in atomic context with the hardware potentially runtime
460 * suspended.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200461 */
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800462 if (!vblank->enabled)
463 goto out;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200464
465 /*
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800466 * Update the count and timestamp to maintain the
Daniel Vetter3ed43512017-05-31 11:21:46 +0200467 * appearance that the counter has been ticking all along until
468 * this time. This makes the count account for the entire time
469 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
470 */
471 drm_update_vblank_count(dev, pipe, false);
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800472 __disable_vblank(dev, pipe);
473 vblank->enabled = false;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200474
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800475out:
Daniel Vetter3ed43512017-05-31 11:21:46 +0200476 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
477}
478
Kees Cooke99e88a2017-10-16 14:43:17 -0700479static void vblank_disable_fn(struct timer_list *t)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200480{
Kees Cooke99e88a2017-10-16 14:43:17 -0700481 struct drm_vblank_crtc *vblank = from_timer(vblank, t, disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200482 struct drm_device *dev = vblank->dev;
483 unsigned int pipe = vblank->pipe;
484 unsigned long irqflags;
485
486 spin_lock_irqsave(&dev->vbl_lock, irqflags);
487 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200488 drm_dbg_core(dev, "disabling vblank on crtc %u\n", pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200489 drm_vblank_disable_and_save(dev, pipe);
490 }
491 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
492}
493
Daniel Vetterc23d6862020-03-23 15:49:23 +0100494static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200495{
Lyude Paula7e5e062020-04-14 12:34:52 -0400496 struct drm_vblank_crtc *vblank = ptr;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200497
Lyude Paula7e5e062020-04-14 12:34:52 -0400498 drm_WARN_ON(dev, READ_ONCE(vblank->enabled) &&
499 drm_core_check_feature(dev, DRIVER_MODESET));
Daniel Vetter3ed43512017-05-31 11:21:46 +0200500
Lyude Paul5e6c2b42020-04-17 15:33:13 -0400501 drm_vblank_destroy_worker(vblank);
Lyude Paula7e5e062020-04-14 12:34:52 -0400502 del_timer_sync(&vblank->disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200503}
Daniel Vetter3ed43512017-05-31 11:21:46 +0200504
505/**
506 * drm_vblank_init - initialize vblank support
507 * @dev: DRM device
508 * @num_crtcs: number of CRTCs supported by @dev
509 *
510 * This function initializes vblank support for @num_crtcs display pipelines.
Daniel Vetterc23d6862020-03-23 15:49:23 +0100511 * Cleanup is handled automatically through a cleanup function added with
Lyude Paula7e5e062020-04-14 12:34:52 -0400512 * drmm_add_action_or_reset().
Daniel Vetter3ed43512017-05-31 11:21:46 +0200513 *
514 * Returns:
515 * Zero on success or a negative error code on failure.
516 */
517int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
518{
Daniel Vetterc23d6862020-03-23 15:49:23 +0100519 int ret;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200520 unsigned int i;
521
522 spin_lock_init(&dev->vbl_lock);
523 spin_lock_init(&dev->vblank_time_lock);
524
Daniel Vetterc23d6862020-03-23 15:49:23 +0100525 dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
526 if (!dev->vblank)
527 return -ENOMEM;
528
Daniel Vetter3ed43512017-05-31 11:21:46 +0200529 dev->num_crtcs = num_crtcs;
530
Daniel Vetter3ed43512017-05-31 11:21:46 +0200531 for (i = 0; i < num_crtcs; i++) {
532 struct drm_vblank_crtc *vblank = &dev->vblank[i];
533
534 vblank->dev = dev;
535 vblank->pipe = i;
536 init_waitqueue_head(&vblank->queue);
Kees Cooke99e88a2017-10-16 14:43:17 -0700537 timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200538 seqlock_init(&vblank->seqlock);
Lyude Paula7e5e062020-04-14 12:34:52 -0400539
540 ret = drmm_add_action_or_reset(dev, drm_vblank_init_release,
541 vblank);
542 if (ret)
543 return ret;
Lyude Paul5e6c2b42020-04-17 15:33:13 -0400544
545 ret = drm_vblank_worker_init(vblank);
546 if (ret)
547 return ret;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200548 }
549
Daniel Vetter3ed43512017-05-31 11:21:46 +0200550 return 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200551}
552EXPORT_SYMBOL(drm_vblank_init);
553
554/**
Thomas Zimmermann7beb6912020-01-29 13:05:17 +0100555 * drm_dev_has_vblank - test if vblanking has been initialized for
556 * a device
557 * @dev: the device
558 *
559 * Drivers may call this function to test if vblank support is
560 * initialized for a device. For most hardware this means that vblanking
561 * can also be enabled.
562 *
563 * Atomic helpers use this function to initialize
564 * &drm_crtc_state.no_vblank. See also drm_atomic_helper_check_modeset().
565 *
566 * Returns:
567 * True if vblanking has been initialized for the given device, false
568 * otherwise.
569 */
570bool drm_dev_has_vblank(const struct drm_device *dev)
571{
572 return dev->num_crtcs != 0;
573}
574EXPORT_SYMBOL(drm_dev_has_vblank);
575
576/**
Daniel Vetter3ed43512017-05-31 11:21:46 +0200577 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
578 * @crtc: which CRTC's vblank waitqueue to retrieve
579 *
580 * This function returns a pointer to the vblank waitqueue for the CRTC.
581 * Drivers can use this to implement vblank waits using wait_event() and related
582 * functions.
583 */
584wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
585{
586 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
587}
588EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
589
590
591/**
592 * drm_calc_timestamping_constants - calculate vblank timestamp constants
593 * @crtc: drm_crtc whose timestamp constants should be updated.
594 * @mode: display mode containing the scanout timings
595 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200596 * Calculate and store various constants which are later needed by vblank and
597 * swap-completion timestamping, e.g, by
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100598 * drm_crtc_vblank_helper_get_vblank_timestamp(). They are derived from
599 * CRTC's true scanout timing, so they take things like panel scaling or
600 * other adjustments into account.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200601 */
602void drm_calc_timestamping_constants(struct drm_crtc *crtc,
603 const struct drm_display_mode *mode)
604{
605 struct drm_device *dev = crtc->dev;
606 unsigned int pipe = drm_crtc_index(crtc);
607 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
608 int linedur_ns = 0, framedur_ns = 0;
609 int dotclock = mode->crtc_clock;
610
Daniel Vetter60150022020-05-27 13:11:34 +0200611 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +0200612 return;
613
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200614 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +0200615 return;
616
617 /* Valid dotclock? */
618 if (dotclock > 0) {
619 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
620
621 /*
622 * Convert scanline length in pixels and video
623 * dot clock to line duration and frame duration
624 * in nanoseconds:
625 */
626 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
627 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
628
629 /*
630 * Fields of interlaced scanout modes are only half a frame duration.
631 */
632 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
633 framedur_ns /= 2;
Sam Ravnborg02149a72020-05-23 09:12:23 +0200634 } else {
635 drm_err(dev, "crtc %u: Can't calculate constants, dotclock = 0!\n",
636 crtc->base.id);
637 }
Daniel Vetter3ed43512017-05-31 11:21:46 +0200638
639 vblank->linedur_ns = linedur_ns;
640 vblank->framedur_ns = framedur_ns;
641 vblank->hwmode = *mode;
642
Sam Ravnborg02149a72020-05-23 09:12:23 +0200643 drm_dbg_core(dev,
644 "crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
645 crtc->base.id, mode->crtc_htotal,
646 mode->crtc_vtotal, mode->crtc_vdisplay);
647 drm_dbg_core(dev, "crtc %u: clock %d kHz framedur %d linedur %d\n",
648 crtc->base.id, dotclock, framedur_ns, linedur_ns);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200649}
650EXPORT_SYMBOL(drm_calc_timestamping_constants);
651
652/**
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100653 * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
654 * timestamp helper
Thomas Zimmermann099d7052020-03-03 08:31:35 +0100655 * @crtc: CRTC whose vblank timestamp to retrieve
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100656 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
657 * On return contains true maximum error of timestamp
658 * @vblank_time: Pointer to time which should receive the timestamp
659 * @in_vblank_irq:
660 * True when called from drm_crtc_handle_vblank(). Some drivers
661 * need to apply some workarounds for gpu-specific vblank irq quirks
662 * if flag is set.
663 * @get_scanout_position:
664 * Callback function to retrieve the scanout position. See
665 * @struct drm_crtc_helper_funcs.get_scanout_position.
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100666 *
667 * Implements calculation of exact vblank timestamps from given drm_display_mode
668 * timings and current video scanout position of a CRTC.
669 *
670 * The current implementation only handles standard video modes. For double scan
671 * and interlaced modes the driver is supposed to adjust the hardware mode
672 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
673 * match the scanout position reported.
674 *
675 * Note that atomic drivers must call drm_calc_timestamping_constants() before
676 * enabling a CRTC. The atomic helpers already take care of that in
Ville Syrjälä441959e2020-09-07 15:00:25 +0300677 * drm_atomic_helper_calc_timestamping_constants().
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100678 *
679 * Returns:
680 *
681 * Returns true on success, and false on failure, i.e. when no accurate
682 * timestamp could be acquired.
683 */
684bool
685drm_crtc_vblank_helper_get_vblank_timestamp_internal(
686 struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time,
687 bool in_vblank_irq,
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100688 drm_vblank_get_scanout_position_func get_scanout_position)
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100689{
690 struct drm_device *dev = crtc->dev;
691 unsigned int pipe = crtc->index;
692 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
693 struct timespec64 ts_etime, ts_vblank_time;
694 ktime_t stime, etime;
695 bool vbl_status;
696 const struct drm_display_mode *mode;
697 int vpos, hpos, i;
698 int delta_ns, duration_ns;
699
700 if (pipe >= dev->num_crtcs) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200701 drm_err(dev, "Invalid crtc %u\n", pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200702 return false;
703 }
704
705 /* Scanout position query not supported? Should not happen. */
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100706 if (!get_scanout_position) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200707 drm_err(dev, "Called from CRTC w/o get_scanout_position()!?\n");
Daniel Vetter3ed43512017-05-31 11:21:46 +0200708 return false;
709 }
710
711 if (drm_drv_uses_atomic_modeset(dev))
712 mode = &vblank->hwmode;
713 else
714 mode = &crtc->hwmode;
715
716 /* If mode timing undefined, just return as no-op:
717 * Happens during initial modesetting of a crtc.
718 */
719 if (mode->crtc_clock == 0) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200720 drm_dbg_core(dev, "crtc %u: Noop due to uninitialized mode.\n",
721 pipe);
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200722 drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
Daniel Vetter3ed43512017-05-31 11:21:46 +0200723 return false;
724 }
725
726 /* Get current scanout position with system timestamp.
727 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
728 * if single query takes longer than max_error nanoseconds.
729 *
730 * This guarantees a tight bound on maximum error if
731 * code gets preempted or delayed for some reason.
732 */
733 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
734 /*
735 * Get vertical and horizontal scanout position vpos, hpos,
736 * and bounding timestamps stime, etime, pre/post query.
737 */
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100738 vbl_status = get_scanout_position(crtc, in_vblank_irq,
739 &vpos, &hpos,
740 &stime, &etime,
741 mode);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200742
743 /* Return as no-op if scanout query unsupported or failed. */
744 if (!vbl_status) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200745 drm_dbg_core(dev,
746 "crtc %u : scanoutpos query failed.\n",
747 pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200748 return false;
749 }
750
751 /* Compute uncertainty in timestamp of scanout position query. */
752 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
753
754 /* Accept result with < max_error nsecs timing uncertainty. */
755 if (duration_ns <= *max_error)
756 break;
757 }
758
759 /* Noisy system timing? */
760 if (i == DRM_TIMESTAMP_MAXRETRIES) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200761 drm_dbg_core(dev,
762 "crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
763 pipe, duration_ns / 1000, *max_error / 1000, i);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200764 }
765
766 /* Return upper bound of timestamp precision error. */
767 *max_error = duration_ns;
768
769 /* Convert scanout position into elapsed time at raw_time query
770 * since start of scanout at first display scanline. delta_ns
771 * can be negative if start of scanout hasn't happened yet.
772 */
773 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
774 mode->crtc_clock);
775
Daniel Vetter3ed43512017-05-31 11:21:46 +0200776 /* Subtract time delta from raw timestamp to get final
777 * vblank_time timestamp for end of vblank.
778 */
Ville Syrjäläbcbec312017-11-13 17:02:10 +0200779 *vblank_time = ktime_sub_ns(etime, delta_ns);
780
Jani Nikulaf0a8f532019-10-01 17:06:14 +0300781 if (!drm_debug_enabled(DRM_UT_VBL))
Ville Syrjäläbcbec312017-11-13 17:02:10 +0200782 return true;
783
784 ts_etime = ktime_to_timespec64(etime);
785 ts_vblank_time = ktime_to_timespec64(*vblank_time);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200786
Sam Ravnborg02149a72020-05-23 09:12:23 +0200787 drm_dbg_vbl(dev,
788 "crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
789 pipe, hpos, vpos,
790 (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
791 (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
792 duration_ns / 1000, i);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200793
794 return true;
795}
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100796EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200797
Daniel Vetter3ed43512017-05-31 11:21:46 +0200798/**
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100799 * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
800 * helper
801 * @crtc: CRTC whose vblank timestamp to retrieve
802 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
803 * On return contains true maximum error of timestamp
804 * @vblank_time: Pointer to time which should receive the timestamp
805 * @in_vblank_irq:
806 * True when called from drm_crtc_handle_vblank(). Some drivers
807 * need to apply some workarounds for gpu-specific vblank irq quirks
808 * if flag is set.
809 *
810 * Implements calculation of exact vblank timestamps from given drm_display_mode
811 * timings and current video scanout position of a CRTC. This can be directly
812 * used as the &drm_crtc_funcs.get_vblank_timestamp implementation of a kms
813 * driver if &drm_crtc_helper_funcs.get_scanout_position is implemented.
814 *
815 * The current implementation only handles standard video modes. For double scan
816 * and interlaced modes the driver is supposed to adjust the hardware mode
817 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
818 * match the scanout position reported.
819 *
820 * Note that atomic drivers must call drm_calc_timestamping_constants() before
821 * enabling a CRTC. The atomic helpers already take care of that in
Ville Syrjälä441959e2020-09-07 15:00:25 +0300822 * drm_atomic_helper_calc_timestamping_constants().
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100823 *
824 * Returns:
825 *
826 * Returns true on success, and false on failure, i.e. when no accurate
827 * timestamp could be acquired.
828 */
829bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
830 int *max_error,
831 ktime_t *vblank_time,
832 bool in_vblank_irq)
833{
834 return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
835 crtc, max_error, vblank_time, in_vblank_irq,
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100836 crtc->helper_private->get_scanout_position);
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100837}
838EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);
839
840/**
Daniel Vetter3ed43512017-05-31 11:21:46 +0200841 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
842 * vblank interval
843 * @dev: DRM device
844 * @pipe: index of CRTC whose vblank timestamp to retrieve
Arnd Bergmann67680d32017-10-11 17:20:12 +0200845 * @tvblank: Pointer to target time which should receive the timestamp
Daniel Vetter3ed43512017-05-31 11:21:46 +0200846 * @in_vblank_irq:
847 * True when called from drm_crtc_handle_vblank(). Some drivers
848 * need to apply some workarounds for gpu-specific vblank irq quirks
849 * if flag is set.
850 *
851 * Fetches the system timestamp corresponding to the time of the most recent
852 * vblank interval on specified CRTC. May call into kms-driver to
853 * compute the timestamp with a high-precision GPU specific method.
854 *
855 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
856 * call, i.e., it isn't very precisely locked to the true vblank.
857 *
858 * Returns:
859 * True if timestamp is considered to be very precise, false otherwise.
860 */
861static bool
862drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200863 ktime_t *tvblank, bool in_vblank_irq)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200864{
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100865 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200866 bool ret = false;
867
868 /* Define requested maximum error on timestamps (nanoseconds). */
869 int max_error = (int) drm_timestamp_precision * 1000;
870
871 /* Query driver if possible and precision timestamping enabled. */
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100872 if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {
873 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
874
875 ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,
876 tvblank, in_vblank_irq);
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100877 }
Daniel Vetter3ed43512017-05-31 11:21:46 +0200878
879 /* GPU high precision timestamp query unsupported or failed.
880 * Return current monotonic/gettimeofday timestamp as best estimate.
881 */
882 if (!ret)
Arnd Bergmann25e1a792017-10-11 17:20:13 +0200883 *tvblank = ktime_get();
Daniel Vetter3ed43512017-05-31 11:21:46 +0200884
885 return ret;
886}
887
888/**
889 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
890 * @crtc: which counter to retrieve
891 *
892 * Fetches the "cooked" vblank count value that represents the number of
893 * vblank events since the system was booted, including lost events due to
Daniel Vetter57d30232017-05-24 16:51:45 +0200894 * modesetting activity. Note that this timer isn't correct against a racing
895 * vblank interrupt (since it only reports the software vblank counter), see
Daniel Vetterca814b22017-05-24 16:51:47 +0200896 * drm_crtc_accurate_vblank_count() for such use-cases.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200897 *
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200898 * Note that for a given vblank counter value drm_crtc_handle_vblank()
899 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
900 * provide a barrier: Any writes done before calling
901 * drm_crtc_handle_vblank() will be visible to callers of the later
902 * functions, iff the vblank count is the same or a later one.
903 *
904 * See also &drm_vblank_crtc.count.
905 *
Daniel Vetter3ed43512017-05-31 11:21:46 +0200906 * Returns:
907 * The software vblank counter.
908 */
Keith Packard570e8692017-10-12 11:57:49 -0700909u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200910{
911 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
912}
913EXPORT_SYMBOL(drm_crtc_vblank_count);
914
Keith Packard570e8692017-10-12 11:57:49 -0700915/**
916 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
917 * system timestamp corresponding to that vblank counter value.
918 * @dev: DRM device
919 * @pipe: index of CRTC whose counter to retrieve
920 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
921 *
922 * Fetches the "cooked" vblank count value that represents the number of
923 * vblank events since the system was booted, including lost events due to
924 * modesetting activity. Returns corresponding system timestamp of the time
925 * of the vblank interval that corresponds to the current vblank counter value.
926 *
927 * This is the legacy version of drm_crtc_vblank_count_and_time().
928 */
929static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200930 ktime_t *vblanktime)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200931{
932 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Keith Packard570e8692017-10-12 11:57:49 -0700933 u64 vblank_count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200934 unsigned int seq;
935
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200936 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs)) {
Arnd Bergmann67680d32017-10-11 17:20:12 +0200937 *vblanktime = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200938 return 0;
939 }
940
941 do {
942 seq = read_seqbegin(&vblank->seqlock);
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200943 vblank_count = atomic64_read(&vblank->count);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200944 *vblanktime = vblank->time;
945 } while (read_seqretry(&vblank->seqlock, seq));
946
947 return vblank_count;
948}
949
950/**
951 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
952 * and the system timestamp corresponding to that vblank counter value
953 * @crtc: which counter to retrieve
Arnd Bergmann67680d32017-10-11 17:20:12 +0200954 * @vblanktime: Pointer to time to receive the vblank timestamp.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200955 *
956 * Fetches the "cooked" vblank count value that represents the number of
957 * vblank events since the system was booted, including lost events due to
958 * modesetting activity. Returns corresponding system timestamp of the time
959 * of the vblank interval that corresponds to the current vblank counter value.
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200960 *
961 * Note that for a given vblank counter value drm_crtc_handle_vblank()
962 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
963 * provide a barrier: Any writes done before calling
964 * drm_crtc_handle_vblank() will be visible to callers of the later
965 * functions, iff the vblank count is the same or a later one.
966 *
967 * See also &drm_vblank_crtc.count.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200968 */
Keith Packard570e8692017-10-12 11:57:49 -0700969u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200970 ktime_t *vblanktime)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200971{
972 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
973 vblanktime);
974}
975EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
976
977static void send_vblank_event(struct drm_device *dev,
978 struct drm_pending_vblank_event *e,
Keith Packard570e8692017-10-12 11:57:49 -0700979 u64 seq, ktime_t now)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200980{
Keith Packardbd386e52017-07-05 14:34:23 -0700981 struct timespec64 tv;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200982
Keith Packardbd386e52017-07-05 14:34:23 -0700983 switch (e->event.base.type) {
984 case DRM_EVENT_VBLANK:
985 case DRM_EVENT_FLIP_COMPLETE:
986 tv = ktime_to_timespec64(now);
987 e->event.vbl.sequence = seq;
988 /*
989 * e->event is a user space structure, with hardcoded unsigned
990 * 32-bit seconds/microseconds. This is safe as we always use
991 * monotonic timestamps since linux-4.15
992 */
993 e->event.vbl.tv_sec = tv.tv_sec;
994 e->event.vbl.tv_usec = tv.tv_nsec / 1000;
995 break;
Keith Packard3064abf2017-06-29 22:49:31 -0700996 case DRM_EVENT_CRTC_SEQUENCE:
997 if (seq)
998 e->event.seq.sequence = seq;
999 e->event.seq.time_ns = ktime_to_ns(now);
1000 break;
Keith Packardbd386e52017-07-05 14:34:23 -07001001 }
1002 trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001003 drm_send_event_locked(dev, &e->base);
1004}
1005
1006/**
1007 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1008 * @crtc: the source CRTC of the vblank event
1009 * @e: the event to send
1010 *
1011 * A lot of drivers need to generate vblank events for the very next vblank
1012 * interrupt. For example when the page flip interrupt happens when the page
1013 * flip gets armed, but not when it actually executes within the next vblank
1014 * period. This helper function implements exactly the required vblank arming
1015 * behaviour.
1016 *
1017 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
1018 * atomic commit must ensure that the next vblank happens at exactly the same
1019 * time as the atomic commit is committed to the hardware. This function itself
Daniel Vettere13a0582017-07-05 06:56:29 +02001020 * does **not** protect against the next vblank interrupt racing with either this
Daniel Vetter3ed43512017-05-31 11:21:46 +02001021 * function call or the atomic commit operation. A possible sequence could be:
1022 *
1023 * 1. Driver commits new hardware state into vblank-synchronized registers.
1024 * 2. A vblank happens, committing the hardware state. Also the corresponding
1025 * vblank interrupt is fired off and fully processed by the interrupt
1026 * handler.
1027 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
1028 * 4. The event is only send out for the next vblank, which is wrong.
1029 *
1030 * An equivalent race can happen when the driver calls
1031 * drm_crtc_arm_vblank_event() before writing out the new hardware state.
1032 *
1033 * The only way to make this work safely is to prevent the vblank from firing
1034 * (and the hardware from committing anything else) until the entire atomic
1035 * commit sequence has run to completion. If the hardware does not have such a
1036 * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
1037 * Instead drivers need to manually send out the event from their interrupt
1038 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
1039 * possible race with the hardware committing the atomic update.
1040 *
Stefan Agnere7502182018-08-07 22:11:43 +02001041 * Caller must hold a vblank reference for the event @e acquired by a
1042 * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001043 */
1044void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1045 struct drm_pending_vblank_event *e)
1046{
1047 struct drm_device *dev = crtc->dev;
1048 unsigned int pipe = drm_crtc_index(crtc);
1049
1050 assert_spin_locked(&dev->event_lock);
1051
1052 e->pipe = pipe;
Keith Packard570e8692017-10-12 11:57:49 -07001053 e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001054 list_add_tail(&e->base.link, &dev->vblank_event_list);
1055}
1056EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1057
1058/**
1059 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1060 * @crtc: the source CRTC of the vblank event
1061 * @e: the event to send
1062 *
1063 * Updates sequence # and timestamp on event for the most recently processed
1064 * vblank, and sends it to userspace. Caller must hold event lock.
1065 *
1066 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
1067 * situation, especially to send out events for atomic commit operations.
1068 */
1069void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1070 struct drm_pending_vblank_event *e)
1071{
1072 struct drm_device *dev = crtc->dev;
Keith Packard570e8692017-10-12 11:57:49 -07001073 u64 seq;
1074 unsigned int pipe = drm_crtc_index(crtc);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001075 ktime_t now;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001076
Daniel Vetter60150022020-05-27 13:11:34 +02001077 if (drm_dev_has_vblank(dev)) {
Daniel Vetter3ed43512017-05-31 11:21:46 +02001078 seq = drm_vblank_count_and_time(dev, pipe, &now);
1079 } else {
1080 seq = 0;
1081
Arnd Bergmann25e1a792017-10-11 17:20:13 +02001082 now = ktime_get();
Daniel Vetter3ed43512017-05-31 11:21:46 +02001083 }
1084 e->pipe = pipe;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001085 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001086}
1087EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1088
1089static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
1090{
1091 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1092 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1093
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001094 if (drm_WARN_ON(dev, !crtc))
Joe Moriarty4ffb8de2018-02-20 14:11:56 -05001095 return 0;
1096
Daniel Vetter3ed43512017-05-31 11:21:46 +02001097 if (crtc->funcs->enable_vblank)
1098 return crtc->funcs->enable_vblank(crtc);
Thomas Zimmermannf397d662020-01-23 14:59:42 +01001099 } else if (dev->driver->enable_vblank) {
1100 return dev->driver->enable_vblank(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001101 }
1102
Thomas Zimmermannf397d662020-01-23 14:59:42 +01001103 return -EINVAL;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001104}
1105
Daniel Vetter3ed43512017-05-31 11:21:46 +02001106static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1107{
1108 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1109 int ret = 0;
1110
1111 assert_spin_locked(&dev->vbl_lock);
1112
1113 spin_lock(&dev->vblank_time_lock);
1114
1115 if (!vblank->enabled) {
1116 /*
1117 * Enable vblank irqs under vblank_time_lock protection.
1118 * All vblank count & timestamp updates are held off
1119 * until we are done reinitializing master counter and
1120 * timestamps. Filtercode in drm_handle_vblank() will
1121 * prevent double-accounting of same vblank interval.
1122 */
1123 ret = __enable_vblank(dev, pipe);
Sam Ravnborg02149a72020-05-23 09:12:23 +02001124 drm_dbg_core(dev, "enabling vblank on crtc %u, ret: %d\n",
1125 pipe, ret);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001126 if (ret) {
1127 atomic_dec(&vblank->refcount);
1128 } else {
1129 drm_update_vblank_count(dev, pipe, 0);
1130 /* drm_update_vblank_count() includes a wmb so we just
1131 * need to ensure that the compiler emits the write
1132 * to mark the vblank as enabled after the call
1133 * to drm_update_vblank_count().
1134 */
1135 WRITE_ONCE(vblank->enabled, true);
1136 }
1137 }
1138
1139 spin_unlock(&dev->vblank_time_lock);
1140
1141 return ret;
1142}
1143
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001144int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001145{
1146 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1147 unsigned long irqflags;
1148 int ret = 0;
1149
Daniel Vetter60150022020-05-27 13:11:34 +02001150 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001151 return -EINVAL;
1152
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001153 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001154 return -EINVAL;
1155
1156 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1157 /* Going from 0->1 means we have to enable interrupts again */
1158 if (atomic_add_return(1, &vblank->refcount) == 1) {
1159 ret = drm_vblank_enable(dev, pipe);
1160 } else {
1161 if (!vblank->enabled) {
1162 atomic_dec(&vblank->refcount);
1163 ret = -EINVAL;
1164 }
1165 }
1166 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1167
1168 return ret;
1169}
1170
1171/**
1172 * drm_crtc_vblank_get - get a reference count on vblank events
1173 * @crtc: which CRTC to own
1174 *
1175 * Acquire a reference count on vblank events to avoid having them disabled
1176 * while in use.
1177 *
1178 * Returns:
1179 * Zero on success or a negative error code on failure.
1180 */
1181int drm_crtc_vblank_get(struct drm_crtc *crtc)
1182{
1183 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1184}
1185EXPORT_SYMBOL(drm_crtc_vblank_get);
1186
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001187void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001188{
1189 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1190
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001191 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001192 return;
1193
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001194 if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001195 return;
1196
1197 /* Last user schedules interrupt disable */
1198 if (atomic_dec_and_test(&vblank->refcount)) {
1199 if (drm_vblank_offdelay == 0)
1200 return;
1201 else if (drm_vblank_offdelay < 0)
Kees Cooke99e88a2017-10-16 14:43:17 -07001202 vblank_disable_fn(&vblank->disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001203 else if (!dev->vblank_disable_immediate)
1204 mod_timer(&vblank->disable_timer,
1205 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1206 }
1207}
1208
1209/**
1210 * drm_crtc_vblank_put - give up ownership of vblank events
1211 * @crtc: which counter to give up
1212 *
1213 * Release ownership of a given vblank counter, turning off interrupts
1214 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1215 */
1216void drm_crtc_vblank_put(struct drm_crtc *crtc)
1217{
1218 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1219}
1220EXPORT_SYMBOL(drm_crtc_vblank_put);
1221
1222/**
1223 * drm_wait_one_vblank - wait for one vblank
1224 * @dev: DRM device
1225 * @pipe: CRTC index
1226 *
1227 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1228 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1229 * due to lack of driver support or because the crtc is off.
Daniel Vetter57d30232017-05-24 16:51:45 +02001230 *
1231 * This is the legacy version of drm_crtc_wait_one_vblank().
Daniel Vetter3ed43512017-05-31 11:21:46 +02001232 */
1233void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1234{
1235 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1236 int ret;
Dhinakaran Pandiyan3b765c02018-02-02 21:12:53 -08001237 u64 last;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001238
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001239 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001240 return;
1241
1242 ret = drm_vblank_get(dev, pipe);
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001243 if (drm_WARN(dev, ret, "vblank not available on crtc %i, ret=%i\n",
1244 pipe, ret))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001245 return;
1246
1247 last = drm_vblank_count(dev, pipe);
1248
1249 ret = wait_event_timeout(vblank->queue,
1250 last != drm_vblank_count(dev, pipe),
1251 msecs_to_jiffies(100));
1252
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001253 drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001254
1255 drm_vblank_put(dev, pipe);
1256}
1257EXPORT_SYMBOL(drm_wait_one_vblank);
1258
1259/**
1260 * drm_crtc_wait_one_vblank - wait for one vblank
1261 * @crtc: DRM crtc
1262 *
1263 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1264 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1265 * due to lack of driver support or because the crtc is off.
1266 */
1267void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1268{
1269 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1270}
1271EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1272
1273/**
1274 * drm_crtc_vblank_off - disable vblank events on a CRTC
1275 * @crtc: CRTC in question
1276 *
1277 * Drivers can use this function to shut down the vblank interrupt handling when
1278 * disabling a crtc. This function ensures that the latest vblank frame count is
1279 * stored so that drm_vblank_on can restore it again.
1280 *
1281 * Drivers must use this function when the hardware vblank counter can get
Daniel Vetter57d30232017-05-24 16:51:45 +02001282 * reset, e.g. when suspending or disabling the @crtc in general.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001283 */
1284void drm_crtc_vblank_off(struct drm_crtc *crtc)
1285{
1286 struct drm_device *dev = crtc->dev;
1287 unsigned int pipe = drm_crtc_index(crtc);
1288 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1289 struct drm_pending_vblank_event *e, *t;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001290 ktime_t now;
Keith Packard570e8692017-10-12 11:57:49 -07001291 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001292
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001293 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001294 return;
1295
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001296 /*
1297 * Grab event_lock early to prevent vblank work from being scheduled
1298 * while we're in the middle of shutting down vblank interrupts
1299 */
Lyude Paula7e3ad52020-06-23 16:24:00 -04001300 spin_lock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001301
1302 spin_lock(&dev->vbl_lock);
Sam Ravnborg02149a72020-05-23 09:12:23 +02001303 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1304 pipe, vblank->enabled, vblank->inmodeset);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001305
1306 /* Avoid redundant vblank disables without previous
1307 * drm_crtc_vblank_on(). */
1308 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1309 drm_vblank_disable_and_save(dev, pipe);
1310
1311 wake_up(&vblank->queue);
1312
1313 /*
1314 * Prevent subsequent drm_vblank_get() from re-enabling
1315 * the vblank interrupt by bumping the refcount.
1316 */
1317 if (!vblank->inmodeset) {
1318 atomic_inc(&vblank->refcount);
1319 vblank->inmodeset = 1;
1320 }
1321 spin_unlock(&dev->vbl_lock);
1322
1323 /* Send any queued vblank events, lest the natives grow disquiet */
1324 seq = drm_vblank_count_and_time(dev, pipe, &now);
1325
1326 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1327 if (e->pipe != pipe)
1328 continue;
Sam Ravnborg02149a72020-05-23 09:12:23 +02001329 drm_dbg_core(dev, "Sending premature vblank event on disable: "
1330 "wanted %llu, current %llu\n",
1331 e->sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001332 list_del(&e->base.link);
1333 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001334 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001335 }
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001336
1337 /* Cancel any leftover pending vblank work */
1338 drm_vblank_cancel_pending_works(vblank);
1339
Lyude Paula7e3ad52020-06-23 16:24:00 -04001340 spin_unlock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001341
1342 /* Will be reset by the modeset helpers when re-enabling the crtc by
1343 * calling drm_calc_timestamping_constants(). */
1344 vblank->hwmode.crtc_clock = 0;
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001345
1346 /* Wait for any vblank work that's still executing to finish */
1347 drm_vblank_flush_worker(vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001348}
1349EXPORT_SYMBOL(drm_crtc_vblank_off);
1350
1351/**
1352 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1353 * @crtc: CRTC in question
1354 *
1355 * Drivers can use this function to reset the vblank state to off at load time.
1356 * Drivers should use this together with the drm_crtc_vblank_off() and
1357 * drm_crtc_vblank_on() functions. The difference compared to
1358 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1359 * and hence doesn't need to call any driver hooks.
Daniel Vetter57d30232017-05-24 16:51:45 +02001360 *
1361 * This is useful for recovering driver state e.g. on driver load, or on resume.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001362 */
1363void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1364{
1365 struct drm_device *dev = crtc->dev;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001366 unsigned int pipe = drm_crtc_index(crtc);
1367 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1368
Lyude Paulfbc678e2020-07-20 15:07:32 -04001369 spin_lock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001370 /*
1371 * Prevent subsequent drm_vblank_get() from enabling the vblank
1372 * interrupt by bumping the refcount.
1373 */
1374 if (!vblank->inmodeset) {
1375 atomic_inc(&vblank->refcount);
1376 vblank->inmodeset = 1;
1377 }
Lyude Paulfbc678e2020-07-20 15:07:32 -04001378 spin_unlock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001379
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001380 drm_WARN_ON(dev, !list_empty(&dev->vblank_event_list));
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001381 drm_WARN_ON(dev, !list_empty(&vblank->pending_work));
Daniel Vetter3ed43512017-05-31 11:21:46 +02001382}
1383EXPORT_SYMBOL(drm_crtc_vblank_reset);
1384
1385/**
Ville Syrjäläed201512018-11-27 20:20:04 +02001386 * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
1387 * @crtc: CRTC in question
1388 * @max_vblank_count: max hardware vblank counter value
1389 *
1390 * Update the maximum hardware vblank counter value for @crtc
1391 * at runtime. Useful for hardware where the operation of the
1392 * hardware vblank counter depends on the currently active
1393 * display configuration.
1394 *
1395 * For example, if the hardware vblank counter does not work
1396 * when a specific connector is active the maximum can be set
1397 * to zero. And when that specific connector isn't active the
1398 * maximum can again be set to the appropriate non-zero value.
1399 *
1400 * If used, must be called before drm_vblank_on().
1401 */
1402void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
1403 u32 max_vblank_count)
1404{
1405 struct drm_device *dev = crtc->dev;
1406 unsigned int pipe = drm_crtc_index(crtc);
1407 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1408
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001409 drm_WARN_ON(dev, dev->max_vblank_count);
1410 drm_WARN_ON(dev, !READ_ONCE(vblank->inmodeset));
Ville Syrjäläed201512018-11-27 20:20:04 +02001411
1412 vblank->max_vblank_count = max_vblank_count;
1413}
1414EXPORT_SYMBOL(drm_crtc_set_max_vblank_count);
1415
1416/**
Daniel Vetter3ed43512017-05-31 11:21:46 +02001417 * drm_crtc_vblank_on - enable vblank events on a CRTC
1418 * @crtc: CRTC in question
1419 *
1420 * This functions restores the vblank interrupt state captured with
Daniel Vetter57d30232017-05-24 16:51:45 +02001421 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1422 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1423 * unbalanced and so can also be unconditionally called in driver load code to
1424 * reflect the current hardware state of the crtc.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001425 */
1426void drm_crtc_vblank_on(struct drm_crtc *crtc)
1427{
1428 struct drm_device *dev = crtc->dev;
1429 unsigned int pipe = drm_crtc_index(crtc);
1430 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Daniel Vetter3ed43512017-05-31 11:21:46 +02001431
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001432 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001433 return;
1434
Lyude Paul92cc68e2020-07-20 15:07:33 -04001435 spin_lock_irq(&dev->vbl_lock);
Sam Ravnborg02149a72020-05-23 09:12:23 +02001436 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1437 pipe, vblank->enabled, vblank->inmodeset);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001438
1439 /* Drop our private "prevent drm_vblank_get" refcount */
1440 if (vblank->inmodeset) {
1441 atomic_dec(&vblank->refcount);
1442 vblank->inmodeset = 0;
1443 }
1444
1445 drm_reset_vblank_timestamp(dev, pipe);
1446
1447 /*
1448 * re-enable interrupts if there are users left, or the
1449 * user wishes vblank interrupts to be enabled all the time.
1450 */
1451 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001452 drm_WARN_ON(dev, drm_vblank_enable(dev, pipe));
Lyude Paul92cc68e2020-07-20 15:07:33 -04001453 spin_unlock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001454}
1455EXPORT_SYMBOL(drm_crtc_vblank_on);
1456
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001457/**
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001458 * drm_vblank_restore - estimate missed vblanks and update vblank count.
1459 * @dev: DRM device
1460 * @pipe: CRTC index
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001461 *
1462 * Power manamement features can cause frame counter resets between vblank
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001463 * disable and enable. Drivers can use this function in their
1464 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1465 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1466 * vblank counter.
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001467 *
1468 * This function is the legacy version of drm_crtc_vblank_restore().
1469 */
1470void drm_vblank_restore(struct drm_device *dev, unsigned int pipe)
1471{
1472 ktime_t t_vblank;
1473 struct drm_vblank_crtc *vblank;
1474 int framedur_ns;
1475 u64 diff_ns;
1476 u32 cur_vblank, diff = 1;
1477 int count = DRM_TIMESTAMP_MAXRETRIES;
1478
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001479 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001480 return;
1481
1482 assert_spin_locked(&dev->vbl_lock);
1483 assert_spin_locked(&dev->vblank_time_lock);
1484
1485 vblank = &dev->vblank[pipe];
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001486 drm_WARN_ONCE(dev,
1487 drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
1488 "Cannot compute missed vblanks without frame duration\n");
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001489 framedur_ns = vblank->framedur_ns;
1490
1491 do {
1492 cur_vblank = __get_vblank_counter(dev, pipe);
1493 drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
1494 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
1495
1496 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
1497 if (framedur_ns)
1498 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
1499
1500
Sam Ravnborg02149a72020-05-23 09:12:23 +02001501 drm_dbg_vbl(dev,
1502 "missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1503 diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001504 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
1505}
1506EXPORT_SYMBOL(drm_vblank_restore);
1507
1508/**
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001509 * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
1510 * @crtc: CRTC in question
1511 *
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001512 * Power manamement features can cause frame counter resets between vblank
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001513 * disable and enable. Drivers can use this function in their
1514 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1515 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1516 * vblank counter.
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001517 */
1518void drm_crtc_vblank_restore(struct drm_crtc *crtc)
1519{
1520 drm_vblank_restore(crtc->dev, drm_crtc_index(crtc));
1521}
1522EXPORT_SYMBOL(drm_crtc_vblank_restore);
1523
Daniel Vetter3ed43512017-05-31 11:21:46 +02001524static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
1525 unsigned int pipe)
1526{
1527 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1528
1529 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Daniel Vetter60150022020-05-27 13:11:34 +02001530 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001531 return;
1532
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001533 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001534 return;
1535
1536 /*
1537 * To avoid all the problems that might happen if interrupts
1538 * were enabled/disabled around or between these calls, we just
1539 * have the kernel take a reference on the CRTC (just once though
1540 * to avoid corrupting the count if multiple, mismatch calls occur),
1541 * so that interrupts remain enabled in the interim.
1542 */
1543 if (!vblank->inmodeset) {
1544 vblank->inmodeset = 0x1;
1545 if (drm_vblank_get(dev, pipe) == 0)
1546 vblank->inmodeset |= 0x2;
1547 }
1548}
1549
1550static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
1551 unsigned int pipe)
1552{
1553 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Daniel Vetter3ed43512017-05-31 11:21:46 +02001554
1555 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Daniel Vetter60150022020-05-27 13:11:34 +02001556 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001557 return;
1558
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001559 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001560 return;
1561
1562 if (vblank->inmodeset) {
Lyude Pauleb2ee0e2020-07-20 15:07:34 -04001563 spin_lock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001564 drm_reset_vblank_timestamp(dev, pipe);
Lyude Pauleb2ee0e2020-07-20 15:07:34 -04001565 spin_unlock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001566
1567 if (vblank->inmodeset & 0x2)
1568 drm_vblank_put(dev, pipe);
1569
1570 vblank->inmodeset = 0;
1571 }
1572}
1573
Daniel Vetterb6dcaaac2017-05-24 16:51:46 +02001574int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
1575 struct drm_file *file_priv)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001576{
1577 struct drm_modeset_ctl *modeset = data;
1578 unsigned int pipe;
1579
1580 /* If drm_vblank_init() hasn't been called yet, just no-op */
Daniel Vetter60150022020-05-27 13:11:34 +02001581 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001582 return 0;
1583
1584 /* KMS drivers handle this internally */
1585 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1586 return 0;
1587
1588 pipe = modeset->crtc;
1589 if (pipe >= dev->num_crtcs)
1590 return -EINVAL;
1591
1592 switch (modeset->cmd) {
1593 case _DRM_PRE_MODESET:
1594 drm_legacy_vblank_pre_modeset(dev, pipe);
1595 break;
1596 case _DRM_POST_MODESET:
1597 drm_legacy_vblank_post_modeset(dev, pipe);
1598 break;
1599 default:
1600 return -EINVAL;
1601 }
1602
1603 return 0;
1604}
1605
Daniel Vetter3ed43512017-05-31 11:21:46 +02001606static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
Keith Packard570e8692017-10-12 11:57:49 -07001607 u64 req_seq,
Daniel Vetter3ed43512017-05-31 11:21:46 +02001608 union drm_wait_vblank *vblwait,
1609 struct drm_file *file_priv)
1610{
1611 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1612 struct drm_pending_vblank_event *e;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001613 ktime_t now;
Keith Packard570e8692017-10-12 11:57:49 -07001614 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001615 int ret;
1616
1617 e = kzalloc(sizeof(*e), GFP_KERNEL);
1618 if (e == NULL) {
1619 ret = -ENOMEM;
1620 goto err_put;
1621 }
1622
1623 e->pipe = pipe;
1624 e->event.base.type = DRM_EVENT_VBLANK;
Keith Packardbd386e52017-07-05 14:34:23 -07001625 e->event.base.length = sizeof(e->event.vbl);
1626 e->event.vbl.user_data = vblwait->request.signal;
1627 e->event.vbl.crtc_id = 0;
1628 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1629 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301630
Keith Packardbd386e52017-07-05 14:34:23 -07001631 if (crtc)
1632 e->event.vbl.crtc_id = crtc->base.id;
1633 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001634
Lyude Paul7f07c352020-07-20 15:07:35 -04001635 spin_lock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001636
1637 /*
1638 * drm_crtc_vblank_off() might have been called after we called
1639 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1640 * vblank disable, so no need for further locking. The reference from
1641 * drm_vblank_get() protects against vblank disable from another source.
1642 */
1643 if (!READ_ONCE(vblank->enabled)) {
1644 ret = -EINVAL;
1645 goto err_unlock;
1646 }
1647
1648 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1649 &e->event.base);
1650
1651 if (ret)
1652 goto err_unlock;
1653
1654 seq = drm_vblank_count_and_time(dev, pipe, &now);
1655
Sam Ravnborg02149a72020-05-23 09:12:23 +02001656 drm_dbg_core(dev, "event on vblank count %llu, current %llu, crtc %u\n",
1657 req_seq, seq, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001658
Keith Packard570e8692017-10-12 11:57:49 -07001659 trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001660
Keith Packard570e8692017-10-12 11:57:49 -07001661 e->sequence = req_seq;
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001662 if (drm_vblank_passed(seq, req_seq)) {
Daniel Vetter3ed43512017-05-31 11:21:46 +02001663 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001664 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001665 vblwait->reply.sequence = seq;
1666 } else {
1667 /* drm_handle_vblank_events will call drm_vblank_put */
1668 list_add_tail(&e->base.link, &dev->vblank_event_list);
Keith Packard570e8692017-10-12 11:57:49 -07001669 vblwait->reply.sequence = req_seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001670 }
1671
Lyude Paul7f07c352020-07-20 15:07:35 -04001672 spin_unlock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001673
1674 return 0;
1675
1676err_unlock:
Lyude Paul7f07c352020-07-20 15:07:35 -04001677 spin_unlock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001678 kfree(e);
1679err_put:
1680 drm_vblank_put(dev, pipe);
1681 return ret;
1682}
1683
1684static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
1685{
1686 if (vblwait->request.sequence)
1687 return false;
1688
1689 return _DRM_VBLANK_RELATIVE ==
1690 (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
1691 _DRM_VBLANK_EVENT |
1692 _DRM_VBLANK_NEXTONMISS));
1693}
1694
Keith Packard570e8692017-10-12 11:57:49 -07001695/*
1696 * Widen a 32-bit param to 64-bits.
1697 *
1698 * \param narrow 32-bit value (missing upper 32 bits)
1699 * \param near 64-bit value that should be 'close' to near
1700 *
1701 * This function returns a 64-bit value using the lower 32-bits from
1702 * 'narrow' and constructing the upper 32-bits so that the result is
1703 * as close as possible to 'near'.
1704 */
1705
1706static u64 widen_32_to_64(u32 narrow, u64 near)
1707{
1708 return near + (s32) (narrow - near);
1709}
1710
Arnd Bergmann67680d32017-10-11 17:20:12 +02001711static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
1712 struct drm_wait_vblank_reply *reply)
1713{
1714 ktime_t now;
1715 struct timespec64 ts;
1716
1717 /*
1718 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
Arnd Bergmann25e1a792017-10-11 17:20:13 +02001719 * to store the seconds. This is safe as we always use monotonic
1720 * timestamps since linux-4.15.
Arnd Bergmann67680d32017-10-11 17:20:12 +02001721 */
1722 reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1723 ts = ktime_to_timespec64(now);
1724 reply->tval_sec = (u32)ts.tv_sec;
1725 reply->tval_usec = ts.tv_nsec / 1000;
1726}
1727
Daniel Vetterb6dcaaac2017-05-24 16:51:46 +02001728int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
1729 struct drm_file *file_priv)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001730{
Keith Packard7de440d2017-04-09 22:35:34 -06001731 struct drm_crtc *crtc;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001732 struct drm_vblank_crtc *vblank;
1733 union drm_wait_vblank *vblwait = data;
1734 int ret;
Keith Packard570e8692017-10-12 11:57:49 -07001735 u64 req_seq, seq;
Keith Packard7de440d2017-04-09 22:35:34 -06001736 unsigned int pipe_index;
Keith Packard570e8692017-10-12 11:57:49 -07001737 unsigned int flags, pipe, high_pipe;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001738
1739 if (!dev->irq_enabled)
Rodrigo Siqueiraaed61052019-10-02 11:05:16 -03001740 return -EOPNOTSUPP;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001741
1742 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1743 return -EINVAL;
1744
1745 if (vblwait->request.type &
1746 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1747 _DRM_VBLANK_HIGH_CRTC_MASK)) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02001748 drm_dbg_core(dev,
1749 "Unsupported type value 0x%x, supported mask 0x%x\n",
1750 vblwait->request.type,
1751 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1752 _DRM_VBLANK_HIGH_CRTC_MASK));
Daniel Vetter3ed43512017-05-31 11:21:46 +02001753 return -EINVAL;
1754 }
1755
1756 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1757 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1758 if (high_pipe)
Keith Packard7de440d2017-04-09 22:35:34 -06001759 pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001760 else
Keith Packard7de440d2017-04-09 22:35:34 -06001761 pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1762
1763 /* Convert lease-relative crtc index into global crtc index */
1764 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1765 pipe = 0;
1766 drm_for_each_crtc(crtc, dev) {
1767 if (drm_lease_held(file_priv, crtc->base.id)) {
1768 if (pipe_index == 0)
1769 break;
1770 pipe_index--;
1771 }
1772 pipe++;
1773 }
1774 } else {
1775 pipe = pipe_index;
1776 }
1777
Daniel Vetter3ed43512017-05-31 11:21:46 +02001778 if (pipe >= dev->num_crtcs)
1779 return -EINVAL;
1780
1781 vblank = &dev->vblank[pipe];
1782
1783 /* If the counter is currently enabled and accurate, short-circuit
1784 * queries to return the cached timestamp of the last vblank.
1785 */
1786 if (dev->vblank_disable_immediate &&
1787 drm_wait_vblank_is_query(vblwait) &&
1788 READ_ONCE(vblank->enabled)) {
Arnd Bergmann67680d32017-10-11 17:20:12 +02001789 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001790 return 0;
1791 }
1792
1793 ret = drm_vblank_get(dev, pipe);
1794 if (ret) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02001795 drm_dbg_core(dev,
1796 "crtc %d failed to acquire vblank counter, %d\n",
1797 pipe, ret);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001798 return ret;
1799 }
1800 seq = drm_vblank_count(dev, pipe);
1801
1802 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1803 case _DRM_VBLANK_RELATIVE:
Keith Packard570e8692017-10-12 11:57:49 -07001804 req_seq = seq + vblwait->request.sequence;
1805 vblwait->request.sequence = req_seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001806 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Keith Packard570e8692017-10-12 11:57:49 -07001807 break;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001808 case _DRM_VBLANK_ABSOLUTE:
Keith Packard570e8692017-10-12 11:57:49 -07001809 req_seq = widen_32_to_64(vblwait->request.sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001810 break;
1811 default:
1812 ret = -EINVAL;
1813 goto done;
1814 }
1815
1816 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001817 drm_vblank_passed(seq, req_seq)) {
Keith Packard570e8692017-10-12 11:57:49 -07001818 req_seq = seq + 1;
1819 vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
1820 vblwait->request.sequence = req_seq;
1821 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001822
1823 if (flags & _DRM_VBLANK_EVENT) {
1824 /* must hold on to the vblank ref until the event fires
1825 * drm_vblank_put will be called asynchronously
1826 */
Keith Packard570e8692017-10-12 11:57:49 -07001827 return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001828 }
1829
Keith Packard570e8692017-10-12 11:57:49 -07001830 if (req_seq != seq) {
Sam Ravnborg05103ea2019-07-26 23:06:58 +02001831 int wait;
1832
Sam Ravnborg02149a72020-05-23 09:12:23 +02001833 drm_dbg_core(dev, "waiting on vblank count %llu, crtc %u\n",
1834 req_seq, pipe);
Sam Ravnborg05103ea2019-07-26 23:06:58 +02001835 wait = wait_event_interruptible_timeout(vblank->queue,
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001836 drm_vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
Sam Ravnborg05103ea2019-07-26 23:06:58 +02001837 !READ_ONCE(vblank->enabled),
1838 msecs_to_jiffies(3000));
1839
1840 switch (wait) {
1841 case 0:
1842 /* timeout */
1843 ret = -EBUSY;
1844 break;
1845 case -ERESTARTSYS:
1846 /* interrupted by signal */
1847 ret = -EINTR;
1848 break;
1849 default:
1850 ret = 0;
1851 break;
1852 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001853 }
1854
1855 if (ret != -EINTR) {
Arnd Bergmann67680d32017-10-11 17:20:12 +02001856 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001857
Sam Ravnborg02149a72020-05-23 09:12:23 +02001858 drm_dbg_core(dev, "crtc %d returning %u to client\n",
1859 pipe, vblwait->reply.sequence);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001860 } else {
Sam Ravnborg02149a72020-05-23 09:12:23 +02001861 drm_dbg_core(dev, "crtc %d vblank wait interrupted by signal\n",
1862 pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001863 }
1864
1865done:
1866 drm_vblank_put(dev, pipe);
1867 return ret;
1868}
1869
1870static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1871{
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +01001872 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1873 bool high_prec = false;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001874 struct drm_pending_vblank_event *e, *t;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001875 ktime_t now;
Keith Packard570e8692017-10-12 11:57:49 -07001876 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001877
1878 assert_spin_locked(&dev->event_lock);
1879
1880 seq = drm_vblank_count_and_time(dev, pipe, &now);
1881
1882 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1883 if (e->pipe != pipe)
1884 continue;
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001885 if (!drm_vblank_passed(seq, e->sequence))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001886 continue;
1887
Sam Ravnborg02149a72020-05-23 09:12:23 +02001888 drm_dbg_core(dev, "vblank event on %llu, current %llu\n",
1889 e->sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001890
1891 list_del(&e->base.link);
1892 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001893 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001894 }
1895
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +01001896 if (crtc && crtc->funcs->get_vblank_timestamp)
1897 high_prec = true;
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +01001898
1899 trace_drm_vblank_event(pipe, seq, now, high_prec);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001900}
1901
1902/**
1903 * drm_handle_vblank - handle a vblank event
1904 * @dev: DRM device
1905 * @pipe: index of CRTC where this event occurred
1906 *
1907 * Drivers should call this routine in their vblank interrupt handlers to
1908 * update the vblank counter and send any signals that may be pending.
1909 *
1910 * This is the legacy version of drm_crtc_handle_vblank().
1911 */
1912bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1913{
1914 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1915 unsigned long irqflags;
1916 bool disable_irq;
1917
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001918 if (drm_WARN_ON_ONCE(dev, !drm_dev_has_vblank(dev)))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001919 return false;
1920
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001921 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001922 return false;
1923
1924 spin_lock_irqsave(&dev->event_lock, irqflags);
1925
1926 /* Need timestamp lock to prevent concurrent execution with
1927 * vblank enable/disable, as this would cause inconsistent
1928 * or corrupted timestamps and vblank counts.
1929 */
1930 spin_lock(&dev->vblank_time_lock);
1931
1932 /* Vblank irq handling disabled. Nothing to do. */
1933 if (!vblank->enabled) {
1934 spin_unlock(&dev->vblank_time_lock);
1935 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1936 return false;
1937 }
1938
1939 drm_update_vblank_count(dev, pipe, true);
1940
1941 spin_unlock(&dev->vblank_time_lock);
1942
1943 wake_up(&vblank->queue);
1944
1945 /* With instant-off, we defer disabling the interrupt until after
1946 * we finish processing the following vblank after all events have
1947 * been signaled. The disable has to be last (after
1948 * drm_handle_vblank_events) so that the timestamp is always accurate.
1949 */
1950 disable_irq = (dev->vblank_disable_immediate &&
1951 drm_vblank_offdelay > 0 &&
1952 !atomic_read(&vblank->refcount));
1953
1954 drm_handle_vblank_events(dev, pipe);
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001955 drm_handle_vblank_works(vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001956
1957 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1958
1959 if (disable_irq)
Kees Cooke99e88a2017-10-16 14:43:17 -07001960 vblank_disable_fn(&vblank->disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001961
1962 return true;
1963}
1964EXPORT_SYMBOL(drm_handle_vblank);
1965
1966/**
1967 * drm_crtc_handle_vblank - handle a vblank event
1968 * @crtc: where this event occurred
1969 *
1970 * Drivers should call this routine in their vblank interrupt handlers to
1971 * update the vblank counter and send any signals that may be pending.
1972 *
1973 * This is the native KMS version of drm_handle_vblank().
1974 *
Daniel Vetterbd7e3f32019-07-23 15:13:37 +02001975 * Note that for a given vblank counter value drm_crtc_handle_vblank()
1976 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
1977 * provide a barrier: Any writes done before calling
1978 * drm_crtc_handle_vblank() will be visible to callers of the later
1979 * functions, iff the vblank count is the same or a later one.
1980 *
1981 * See also &drm_vblank_crtc.count.
1982 *
Daniel Vetter3ed43512017-05-31 11:21:46 +02001983 * Returns:
1984 * True if the event was successfully handled, false on failure.
1985 */
1986bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1987{
1988 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1989}
1990EXPORT_SYMBOL(drm_crtc_handle_vblank);
Keith Packard3064abf2017-06-29 22:49:31 -07001991
1992/*
1993 * Get crtc VBLANK count.
1994 *
1995 * \param dev DRM device
1996 * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
1997 * \param file_priv drm file private for the user's open file descriptor
1998 */
1999
2000int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
2001 struct drm_file *file_priv)
2002{
2003 struct drm_crtc *crtc;
2004 struct drm_vblank_crtc *vblank;
2005 int pipe;
2006 struct drm_crtc_get_sequence *get_seq = data;
2007 ktime_t now;
2008 bool vblank_enabled;
2009 int ret;
2010
2011 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson69fdf422018-09-13 20:20:50 +01002012 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002013
2014 if (!dev->irq_enabled)
Rodrigo Siqueiraaed61052019-10-02 11:05:16 -03002015 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002016
2017 crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
2018 if (!crtc)
2019 return -ENOENT;
2020
2021 pipe = drm_crtc_index(crtc);
2022
2023 vblank = &dev->vblank[pipe];
2024 vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
2025
2026 if (!vblank_enabled) {
2027 ret = drm_crtc_vblank_get(crtc);
2028 if (ret) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02002029 drm_dbg_core(dev,
2030 "crtc %d failed to acquire vblank counter, %d\n",
2031 pipe, ret);
Keith Packard3064abf2017-06-29 22:49:31 -07002032 return ret;
2033 }
2034 }
2035 drm_modeset_lock(&crtc->mutex, NULL);
2036 if (crtc->state)
2037 get_seq->active = crtc->state->enable;
2038 else
2039 get_seq->active = crtc->enabled;
2040 drm_modeset_unlock(&crtc->mutex);
2041 get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
2042 get_seq->sequence_ns = ktime_to_ns(now);
2043 if (!vblank_enabled)
2044 drm_crtc_vblank_put(crtc);
2045 return 0;
2046}
2047
2048/*
2049 * Queue a event for VBLANK sequence
2050 *
2051 * \param dev DRM device
2052 * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
2053 * \param file_priv drm file private for the user's open file descriptor
2054 */
2055
2056int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
2057 struct drm_file *file_priv)
2058{
2059 struct drm_crtc *crtc;
2060 struct drm_vblank_crtc *vblank;
2061 int pipe;
2062 struct drm_crtc_queue_sequence *queue_seq = data;
2063 ktime_t now;
2064 struct drm_pending_vblank_event *e;
2065 u32 flags;
2066 u64 seq;
2067 u64 req_seq;
2068 int ret;
Keith Packard3064abf2017-06-29 22:49:31 -07002069
2070 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson69fdf422018-09-13 20:20:50 +01002071 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002072
2073 if (!dev->irq_enabled)
Rodrigo Siqueiraaed61052019-10-02 11:05:16 -03002074 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002075
2076 crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
2077 if (!crtc)
2078 return -ENOENT;
2079
2080 flags = queue_seq->flags;
2081 /* Check valid flag bits */
2082 if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
2083 DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
2084 return -EINVAL;
2085
2086 pipe = drm_crtc_index(crtc);
2087
2088 vblank = &dev->vblank[pipe];
2089
2090 e = kzalloc(sizeof(*e), GFP_KERNEL);
2091 if (e == NULL)
2092 return -ENOMEM;
2093
2094 ret = drm_crtc_vblank_get(crtc);
2095 if (ret) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02002096 drm_dbg_core(dev,
2097 "crtc %d failed to acquire vblank counter, %d\n",
2098 pipe, ret);
Keith Packard3064abf2017-06-29 22:49:31 -07002099 goto err_free;
2100 }
2101
2102 seq = drm_vblank_count_and_time(dev, pipe, &now);
2103 req_seq = queue_seq->sequence;
2104
2105 if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
2106 req_seq += seq;
2107
Lyude Paul5e6c2b42020-04-17 15:33:13 -04002108 if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && drm_vblank_passed(seq, req_seq))
Keith Packard3064abf2017-06-29 22:49:31 -07002109 req_seq = seq + 1;
2110
2111 e->pipe = pipe;
2112 e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
2113 e->event.base.length = sizeof(e->event.seq);
2114 e->event.seq.user_data = queue_seq->user_data;
2115
Lyude Paul5887a2a2020-07-20 15:07:36 -04002116 spin_lock_irq(&dev->event_lock);
Keith Packard3064abf2017-06-29 22:49:31 -07002117
2118 /*
2119 * drm_crtc_vblank_off() might have been called after we called
2120 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
2121 * vblank disable, so no need for further locking. The reference from
2122 * drm_crtc_vblank_get() protects against vblank disable from another source.
2123 */
2124 if (!READ_ONCE(vblank->enabled)) {
2125 ret = -EINVAL;
2126 goto err_unlock;
2127 }
2128
2129 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
2130 &e->event.base);
2131
2132 if (ret)
2133 goto err_unlock;
2134
2135 e->sequence = req_seq;
2136
Lyude Paul5e6c2b42020-04-17 15:33:13 -04002137 if (drm_vblank_passed(seq, req_seq)) {
Keith Packard3064abf2017-06-29 22:49:31 -07002138 drm_crtc_vblank_put(crtc);
2139 send_vblank_event(dev, e, seq, now);
2140 queue_seq->sequence = seq;
2141 } else {
2142 /* drm_handle_vblank_events will call drm_vblank_put */
2143 list_add_tail(&e->base.link, &dev->vblank_event_list);
2144 queue_seq->sequence = req_seq;
2145 }
2146
Lyude Paul5887a2a2020-07-20 15:07:36 -04002147 spin_unlock_irq(&dev->event_lock);
Keith Packard3064abf2017-06-29 22:49:31 -07002148 return 0;
2149
2150err_unlock:
Lyude Paul5887a2a2020-07-20 15:07:36 -04002151 spin_unlock_irq(&dev->event_lock);
Keith Packard3064abf2017-06-29 22:49:31 -07002152 drm_crtc_vblank_put(crtc);
2153err_free:
2154 kfree(e);
2155 return ret;
2156}
Lyude Paul5e6c2b42020-04-17 15:33:13 -04002157