blob: d30e2f2b8f3c47bf13d9e7bb531f676537ca2806 [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);
Daniel Vetter57bb1ee2020-11-04 11:04:21 +0100212 }
213#ifdef CONFIG_DRM_LEGACY
214 else if (dev->driver->get_vblank_counter) {
Daniel Vetter3ed43512017-05-31 11:21:46 +0200215 return dev->driver->get_vblank_counter(dev, pipe);
Thomas Zimmermannf397d662020-01-23 14:59:42 +0100216 }
Daniel Vetter57bb1ee2020-11-04 11:04:21 +0100217#endif
Daniel Vetter3ed43512017-05-31 11:21:46 +0200218
219 return drm_vblank_no_hw_counter(dev, pipe);
220}
221
222/*
223 * Reset the stored timestamp for the current vblank count to correspond
224 * to the last vblank occurred.
225 *
226 * Only to be called from drm_crtc_vblank_on().
227 *
228 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
229 * device vblank fields.
230 */
231static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
232{
233 u32 cur_vblank;
234 bool rc;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200235 ktime_t t_vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200236 int count = DRM_TIMESTAMP_MAXRETRIES;
237
238 spin_lock(&dev->vblank_time_lock);
239
240 /*
241 * sample the current counter to avoid random jumps
242 * when drm_vblank_enable() applies the diff
243 */
244 do {
245 cur_vblank = __get_vblank_counter(dev, pipe);
246 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
247 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
248
249 /*
250 * Only reinitialize corresponding vblank timestamp if high-precision query
251 * available and didn't fail. Otherwise reinitialize delayed at next vblank
252 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
253 */
254 if (!rc)
Arnd Bergmann67680d32017-10-11 17:20:12 +0200255 t_vblank = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200256
257 /*
258 * +1 to make sure user will never see the same
259 * vblank counter value before and after a modeset
260 */
Arnd Bergmann67680d32017-10-11 17:20:12 +0200261 store_vblank(dev, pipe, 1, t_vblank, cur_vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200262
263 spin_unlock(&dev->vblank_time_lock);
264}
265
266/*
267 * Call back into the driver to update the appropriate vblank counter
268 * (specified by @pipe). Deal with wraparound, if it occurred, and
269 * update the last read value so we can deal with wraparound on the next
270 * call if necessary.
271 *
272 * Only necessary when going from off->on, to account for frames we
273 * didn't get an interrupt for.
274 *
275 * Note: caller must hold &drm_device.vbl_lock since this reads & writes
276 * device vblank fields.
277 */
278static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
279 bool in_vblank_irq)
280{
281 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
282 u32 cur_vblank, diff;
283 bool rc;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200284 ktime_t t_vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200285 int count = DRM_TIMESTAMP_MAXRETRIES;
286 int framedur_ns = vblank->framedur_ns;
Ville Syrjäläed201512018-11-27 20:20:04 +0200287 u32 max_vblank_count = drm_max_vblank_count(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200288
289 /*
290 * Interrupts were disabled prior to this call, so deal with counter
291 * wrap if needed.
292 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
293 * here if the register is small or we had vblank interrupts off for
294 * a long time.
295 *
296 * We repeat the hardware vblank counter & timestamp query until
297 * we get consistent results. This to prevent races between gpu
298 * updating its hardware counter while we are retrieving the
299 * corresponding vblank timestamp.
300 */
301 do {
302 cur_vblank = __get_vblank_counter(dev, pipe);
303 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, in_vblank_irq);
304 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
305
Ville Syrjäläed201512018-11-27 20:20:04 +0200306 if (max_vblank_count) {
Daniel Vetter3ed43512017-05-31 11:21:46 +0200307 /* trust the hw counter when it's around */
Ville Syrjäläed201512018-11-27 20:20:04 +0200308 diff = (cur_vblank - vblank->last) & max_vblank_count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200309 } else if (rc && framedur_ns) {
Arnd Bergmann67680d32017-10-11 17:20:12 +0200310 u64 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
Daniel Vetter3ed43512017-05-31 11:21:46 +0200311
312 /*
313 * Figure out how many vblanks we've missed based
314 * on the difference in the timestamps and the
315 * frame/field duration.
316 */
Oleg Vasilev41ff5bd2019-06-13 15:18:00 +0300317
Sam Ravnborg02149a72020-05-23 09:12:23 +0200318 drm_dbg_vbl(dev, "crtc %u: Calculating number of vblanks."
319 " diff_ns = %lld, framedur_ns = %d)\n",
320 pipe, (long long)diff_ns, framedur_ns);
Oleg Vasilev41ff5bd2019-06-13 15:18:00 +0300321
Daniel Vetter3ed43512017-05-31 11:21:46 +0200322 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
323
324 if (diff == 0 && in_vblank_irq)
Sam Ravnborg02149a72020-05-23 09:12:23 +0200325 drm_dbg_vbl(dev, "crtc %u: Redundant vblirq ignored\n",
326 pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200327 } else {
328 /* some kind of default for drivers w/o accurate vbl timestamping */
329 diff = in_vblank_irq ? 1 : 0;
330 }
331
332 /*
333 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
334 * interval? If so then vblank irqs keep running and it will likely
335 * happen that the hardware vblank counter is not trustworthy as it
336 * might reset at some point in that interval and vblank timestamps
337 * are not trustworthy either in that interval. Iow. this can result
338 * in a bogus diff >> 1 which must be avoided as it would cause
339 * random large forward jumps of the software vblank counter.
340 */
341 if (diff > 1 && (vblank->inmodeset & 0x2)) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200342 drm_dbg_vbl(dev,
343 "clamping vblank bump to 1 on crtc %u: diffr=%u"
344 " due to pre-modeset.\n", pipe, diff);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200345 diff = 1;
346 }
347
Sam Ravnborg02149a72020-05-23 09:12:23 +0200348 drm_dbg_vbl(dev, "updating vblank count on crtc %u:"
349 " current=%llu, diff=%u, hw=%u hw_last=%u\n",
350 pipe, (unsigned long long)atomic64_read(&vblank->count),
351 diff, cur_vblank, vblank->last);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200352
353 if (diff == 0) {
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200354 drm_WARN_ON_ONCE(dev, cur_vblank != vblank->last);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200355 return;
356 }
357
358 /*
359 * Only reinitialize corresponding vblank timestamp if high-precision query
360 * available and didn't fail, or we were called from the vblank interrupt.
361 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
362 * for now, to mark the vblanktimestamp as invalid.
363 */
Laurent Pinchart138b87f2017-06-29 15:37:20 +0300364 if (!rc && !in_vblank_irq)
Arnd Bergmann67680d32017-10-11 17:20:12 +0200365 t_vblank = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200366
Arnd Bergmann67680d32017-10-11 17:20:12 +0200367 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200368}
369
Lyude Paul5e6c2b42020-04-17 15:33:13 -0400370u64 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200371{
372 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200373 u64 count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200374
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200375 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +0200376 return 0;
377
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200378 count = atomic64_read(&vblank->count);
379
380 /*
381 * This read barrier corresponds to the implicit write barrier of the
382 * write seqlock in store_vblank(). Note that this is the only place
383 * where we need an explicit barrier, since all other access goes
384 * through drm_vblank_count_and_time(), which already has the required
385 * read barrier curtesy of the read seqlock.
386 */
387 smp_rmb();
388
389 return count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200390}
391
392/**
Daniel Vetterca814b22017-05-24 16:51:47 +0200393 * drm_crtc_accurate_vblank_count - retrieve the master vblank counter
Daniel Vetter3ed43512017-05-31 11:21:46 +0200394 * @crtc: which counter to retrieve
395 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200396 * This function is similar to drm_crtc_vblank_count() but this function
397 * interpolates to handle a race with vblank interrupts using the high precision
398 * timestamping support.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200399 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200400 * This is mostly useful for hardware that can obtain the scanout position, but
401 * doesn't have a hardware frame counter.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200402 */
Dhinakaran Pandiyan3b765c02018-02-02 21:12:53 -0800403u64 drm_crtc_accurate_vblank_count(struct drm_crtc *crtc)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200404{
405 struct drm_device *dev = crtc->dev;
406 unsigned int pipe = drm_crtc_index(crtc);
Dhinakaran Pandiyan3b765c02018-02-02 21:12:53 -0800407 u64 vblank;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200408 unsigned long flags;
409
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200410 drm_WARN_ONCE(dev, drm_debug_enabled(DRM_UT_VBL) &&
411 !crtc->funcs->get_vblank_timestamp,
412 "This function requires support for accurate vblank timestamps.");
Daniel Vetter3ed43512017-05-31 11:21:46 +0200413
414 spin_lock_irqsave(&dev->vblank_time_lock, flags);
415
416 drm_update_vblank_count(dev, pipe, false);
417 vblank = drm_vblank_count(dev, pipe);
418
419 spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
420
421 return vblank;
422}
Daniel Vetterca814b22017-05-24 16:51:47 +0200423EXPORT_SYMBOL(drm_crtc_accurate_vblank_count);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200424
425static void __disable_vblank(struct drm_device *dev, unsigned int pipe)
426{
427 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
428 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
429
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200430 if (drm_WARN_ON(dev, !crtc))
Joe Moriarty4ffb8de2018-02-20 14:11:56 -0500431 return;
432
Thomas Zimmermannf397d662020-01-23 14:59:42 +0100433 if (crtc->funcs->disable_vblank)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200434 crtc->funcs->disable_vblank(crtc);
Daniel Vetter57bb1ee2020-11-04 11:04:21 +0100435 }
436#ifdef CONFIG_DRM_LEGACY
437 else {
Thomas Zimmermannf397d662020-01-23 14:59:42 +0100438 dev->driver->disable_vblank(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200439 }
Daniel Vetter57bb1ee2020-11-04 11:04:21 +0100440#endif
Daniel Vetter3ed43512017-05-31 11:21:46 +0200441}
442
443/*
444 * Disable vblank irq's on crtc, make sure that last vblank count
445 * of hardware and corresponding consistent software vblank counter
446 * are preserved, even if there are any spurious vblank irq's after
447 * disable.
448 */
449void drm_vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
450{
451 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
452 unsigned long irqflags;
453
454 assert_spin_locked(&dev->vbl_lock);
455
456 /* Prevent vblank irq processing while disabling vblank irqs,
457 * so no updates of timestamps or count can happen after we've
458 * disabled. Needed to prevent races in case of delayed irq's.
459 */
460 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
461
462 /*
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800463 * Update vblank count and disable vblank interrupts only if the
464 * interrupts were enabled. This avoids calling the ->disable_vblank()
465 * operation in atomic context with the hardware potentially runtime
466 * suspended.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200467 */
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800468 if (!vblank->enabled)
469 goto out;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200470
471 /*
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800472 * Update the count and timestamp to maintain the
Daniel Vetter3ed43512017-05-31 11:21:46 +0200473 * appearance that the counter has been ticking all along until
474 * this time. This makes the count account for the entire time
475 * between drm_crtc_vblank_on() and drm_crtc_vblank_off().
476 */
477 drm_update_vblank_count(dev, pipe, false);
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800478 __disable_vblank(dev, pipe);
479 vblank->enabled = false;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200480
Dhinakaran Pandiyan68036b02018-02-02 21:13:00 -0800481out:
Daniel Vetter3ed43512017-05-31 11:21:46 +0200482 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
483}
484
Kees Cooke99e88a2017-10-16 14:43:17 -0700485static void vblank_disable_fn(struct timer_list *t)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200486{
Kees Cooke99e88a2017-10-16 14:43:17 -0700487 struct drm_vblank_crtc *vblank = from_timer(vblank, t, disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200488 struct drm_device *dev = vblank->dev;
489 unsigned int pipe = vblank->pipe;
490 unsigned long irqflags;
491
492 spin_lock_irqsave(&dev->vbl_lock, irqflags);
493 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200494 drm_dbg_core(dev, "disabling vblank on crtc %u\n", pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200495 drm_vblank_disable_and_save(dev, pipe);
496 }
497 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
498}
499
Daniel Vetterc23d6862020-03-23 15:49:23 +0100500static void drm_vblank_init_release(struct drm_device *dev, void *ptr)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200501{
Lyude Paula7e5e062020-04-14 12:34:52 -0400502 struct drm_vblank_crtc *vblank = ptr;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200503
Lyude Paula7e5e062020-04-14 12:34:52 -0400504 drm_WARN_ON(dev, READ_ONCE(vblank->enabled) &&
505 drm_core_check_feature(dev, DRIVER_MODESET));
Daniel Vetter3ed43512017-05-31 11:21:46 +0200506
Lyude Paul5e6c2b42020-04-17 15:33:13 -0400507 drm_vblank_destroy_worker(vblank);
Lyude Paula7e5e062020-04-14 12:34:52 -0400508 del_timer_sync(&vblank->disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200509}
Daniel Vetter3ed43512017-05-31 11:21:46 +0200510
511/**
512 * drm_vblank_init - initialize vblank support
513 * @dev: DRM device
514 * @num_crtcs: number of CRTCs supported by @dev
515 *
516 * This function initializes vblank support for @num_crtcs display pipelines.
Daniel Vetterc23d6862020-03-23 15:49:23 +0100517 * Cleanup is handled automatically through a cleanup function added with
Lyude Paula7e5e062020-04-14 12:34:52 -0400518 * drmm_add_action_or_reset().
Daniel Vetter3ed43512017-05-31 11:21:46 +0200519 *
520 * Returns:
521 * Zero on success or a negative error code on failure.
522 */
523int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
524{
Daniel Vetterc23d6862020-03-23 15:49:23 +0100525 int ret;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200526 unsigned int i;
527
528 spin_lock_init(&dev->vbl_lock);
529 spin_lock_init(&dev->vblank_time_lock);
530
Daniel Vetterc23d6862020-03-23 15:49:23 +0100531 dev->vblank = drmm_kcalloc(dev, num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
532 if (!dev->vblank)
533 return -ENOMEM;
534
Daniel Vetter3ed43512017-05-31 11:21:46 +0200535 dev->num_crtcs = num_crtcs;
536
Daniel Vetter3ed43512017-05-31 11:21:46 +0200537 for (i = 0; i < num_crtcs; i++) {
538 struct drm_vblank_crtc *vblank = &dev->vblank[i];
539
540 vblank->dev = dev;
541 vblank->pipe = i;
542 init_waitqueue_head(&vblank->queue);
Kees Cooke99e88a2017-10-16 14:43:17 -0700543 timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200544 seqlock_init(&vblank->seqlock);
Lyude Paula7e5e062020-04-14 12:34:52 -0400545
546 ret = drmm_add_action_or_reset(dev, drm_vblank_init_release,
547 vblank);
548 if (ret)
549 return ret;
Lyude Paul5e6c2b42020-04-17 15:33:13 -0400550
551 ret = drm_vblank_worker_init(vblank);
552 if (ret)
553 return ret;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200554 }
555
Daniel Vetter3ed43512017-05-31 11:21:46 +0200556 return 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200557}
558EXPORT_SYMBOL(drm_vblank_init);
559
560/**
Thomas Zimmermann7beb6912020-01-29 13:05:17 +0100561 * drm_dev_has_vblank - test if vblanking has been initialized for
562 * a device
563 * @dev: the device
564 *
565 * Drivers may call this function to test if vblank support is
566 * initialized for a device. For most hardware this means that vblanking
567 * can also be enabled.
568 *
569 * Atomic helpers use this function to initialize
570 * &drm_crtc_state.no_vblank. See also drm_atomic_helper_check_modeset().
571 *
572 * Returns:
573 * True if vblanking has been initialized for the given device, false
574 * otherwise.
575 */
576bool drm_dev_has_vblank(const struct drm_device *dev)
577{
578 return dev->num_crtcs != 0;
579}
580EXPORT_SYMBOL(drm_dev_has_vblank);
581
582/**
Daniel Vetter3ed43512017-05-31 11:21:46 +0200583 * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
584 * @crtc: which CRTC's vblank waitqueue to retrieve
585 *
586 * This function returns a pointer to the vblank waitqueue for the CRTC.
587 * Drivers can use this to implement vblank waits using wait_event() and related
588 * functions.
589 */
590wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
591{
592 return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
593}
594EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
595
596
597/**
598 * drm_calc_timestamping_constants - calculate vblank timestamp constants
599 * @crtc: drm_crtc whose timestamp constants should be updated.
600 * @mode: display mode containing the scanout timings
601 *
Daniel Vetter57d30232017-05-24 16:51:45 +0200602 * Calculate and store various constants which are later needed by vblank and
603 * swap-completion timestamping, e.g, by
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100604 * drm_crtc_vblank_helper_get_vblank_timestamp(). They are derived from
605 * CRTC's true scanout timing, so they take things like panel scaling or
606 * other adjustments into account.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200607 */
608void drm_calc_timestamping_constants(struct drm_crtc *crtc,
609 const struct drm_display_mode *mode)
610{
611 struct drm_device *dev = crtc->dev;
612 unsigned int pipe = drm_crtc_index(crtc);
613 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
614 int linedur_ns = 0, framedur_ns = 0;
615 int dotclock = mode->crtc_clock;
616
Daniel Vetter60150022020-05-27 13:11:34 +0200617 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +0200618 return;
619
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200620 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +0200621 return;
622
623 /* Valid dotclock? */
624 if (dotclock > 0) {
625 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
626
627 /*
628 * Convert scanline length in pixels and video
629 * dot clock to line duration and frame duration
630 * in nanoseconds:
631 */
632 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
633 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
634
635 /*
636 * Fields of interlaced scanout modes are only half a frame duration.
637 */
638 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
639 framedur_ns /= 2;
Sam Ravnborg02149a72020-05-23 09:12:23 +0200640 } else {
641 drm_err(dev, "crtc %u: Can't calculate constants, dotclock = 0!\n",
642 crtc->base.id);
643 }
Daniel Vetter3ed43512017-05-31 11:21:46 +0200644
645 vblank->linedur_ns = linedur_ns;
646 vblank->framedur_ns = framedur_ns;
647 vblank->hwmode = *mode;
648
Sam Ravnborg02149a72020-05-23 09:12:23 +0200649 drm_dbg_core(dev,
650 "crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
651 crtc->base.id, mode->crtc_htotal,
652 mode->crtc_vtotal, mode->crtc_vdisplay);
653 drm_dbg_core(dev, "crtc %u: clock %d kHz framedur %d linedur %d\n",
654 crtc->base.id, dotclock, framedur_ns, linedur_ns);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200655}
656EXPORT_SYMBOL(drm_calc_timestamping_constants);
657
658/**
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100659 * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank
660 * timestamp helper
Thomas Zimmermann099d7052020-03-03 08:31:35 +0100661 * @crtc: CRTC whose vblank timestamp to retrieve
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100662 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
663 * On return contains true maximum error of timestamp
664 * @vblank_time: Pointer to time which should receive the timestamp
665 * @in_vblank_irq:
666 * True when called from drm_crtc_handle_vblank(). Some drivers
667 * need to apply some workarounds for gpu-specific vblank irq quirks
668 * if flag is set.
669 * @get_scanout_position:
670 * Callback function to retrieve the scanout position. See
671 * @struct drm_crtc_helper_funcs.get_scanout_position.
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100672 *
673 * Implements calculation of exact vblank timestamps from given drm_display_mode
674 * timings and current video scanout position of a CRTC.
675 *
676 * The current implementation only handles standard video modes. For double scan
677 * and interlaced modes the driver is supposed to adjust the hardware mode
678 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
679 * match the scanout position reported.
680 *
681 * Note that atomic drivers must call drm_calc_timestamping_constants() before
682 * enabling a CRTC. The atomic helpers already take care of that in
Ville Syrjälä441959e2020-09-07 15:00:25 +0300683 * drm_atomic_helper_calc_timestamping_constants().
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100684 *
685 * Returns:
686 *
687 * Returns true on success, and false on failure, i.e. when no accurate
688 * timestamp could be acquired.
689 */
690bool
691drm_crtc_vblank_helper_get_vblank_timestamp_internal(
692 struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time,
693 bool in_vblank_irq,
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100694 drm_vblank_get_scanout_position_func get_scanout_position)
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100695{
696 struct drm_device *dev = crtc->dev;
697 unsigned int pipe = crtc->index;
698 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
699 struct timespec64 ts_etime, ts_vblank_time;
700 ktime_t stime, etime;
701 bool vbl_status;
702 const struct drm_display_mode *mode;
703 int vpos, hpos, i;
704 int delta_ns, duration_ns;
705
706 if (pipe >= dev->num_crtcs) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200707 drm_err(dev, "Invalid crtc %u\n", pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200708 return false;
709 }
710
711 /* Scanout position query not supported? Should not happen. */
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100712 if (!get_scanout_position) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200713 drm_err(dev, "Called from CRTC w/o get_scanout_position()!?\n");
Daniel Vetter3ed43512017-05-31 11:21:46 +0200714 return false;
715 }
716
717 if (drm_drv_uses_atomic_modeset(dev))
718 mode = &vblank->hwmode;
719 else
720 mode = &crtc->hwmode;
721
722 /* If mode timing undefined, just return as no-op:
723 * Happens during initial modesetting of a crtc.
724 */
725 if (mode->crtc_clock == 0) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200726 drm_dbg_core(dev, "crtc %u: Noop due to uninitialized mode.\n",
727 pipe);
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200728 drm_WARN_ON_ONCE(dev, drm_drv_uses_atomic_modeset(dev));
Daniel Vetter3ed43512017-05-31 11:21:46 +0200729 return false;
730 }
731
732 /* Get current scanout position with system timestamp.
733 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
734 * if single query takes longer than max_error nanoseconds.
735 *
736 * This guarantees a tight bound on maximum error if
737 * code gets preempted or delayed for some reason.
738 */
739 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
740 /*
741 * Get vertical and horizontal scanout position vpos, hpos,
742 * and bounding timestamps stime, etime, pre/post query.
743 */
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100744 vbl_status = get_scanout_position(crtc, in_vblank_irq,
745 &vpos, &hpos,
746 &stime, &etime,
747 mode);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200748
749 /* Return as no-op if scanout query unsupported or failed. */
750 if (!vbl_status) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200751 drm_dbg_core(dev,
752 "crtc %u : scanoutpos query failed.\n",
753 pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200754 return false;
755 }
756
757 /* Compute uncertainty in timestamp of scanout position query. */
758 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
759
760 /* Accept result with < max_error nsecs timing uncertainty. */
761 if (duration_ns <= *max_error)
762 break;
763 }
764
765 /* Noisy system timing? */
766 if (i == DRM_TIMESTAMP_MAXRETRIES) {
Sam Ravnborg02149a72020-05-23 09:12:23 +0200767 drm_dbg_core(dev,
768 "crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
769 pipe, duration_ns / 1000, *max_error / 1000, i);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200770 }
771
772 /* Return upper bound of timestamp precision error. */
773 *max_error = duration_ns;
774
775 /* Convert scanout position into elapsed time at raw_time query
776 * since start of scanout at first display scanline. delta_ns
777 * can be negative if start of scanout hasn't happened yet.
778 */
779 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
780 mode->crtc_clock);
781
Daniel Vetter3ed43512017-05-31 11:21:46 +0200782 /* Subtract time delta from raw timestamp to get final
783 * vblank_time timestamp for end of vblank.
784 */
Ville Syrjäläbcbec312017-11-13 17:02:10 +0200785 *vblank_time = ktime_sub_ns(etime, delta_ns);
786
Jani Nikulaf0a8f532019-10-01 17:06:14 +0300787 if (!drm_debug_enabled(DRM_UT_VBL))
Ville Syrjäläbcbec312017-11-13 17:02:10 +0200788 return true;
789
790 ts_etime = ktime_to_timespec64(etime);
791 ts_vblank_time = ktime_to_timespec64(*vblank_time);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200792
Sam Ravnborg02149a72020-05-23 09:12:23 +0200793 drm_dbg_vbl(dev,
794 "crtc %u : v p(%d,%d)@ %lld.%06ld -> %lld.%06ld [e %d us, %d rep]\n",
795 pipe, hpos, vpos,
796 (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
797 (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
798 duration_ns / 1000, i);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200799
800 return true;
801}
Thomas Zimmermannf1e2b632020-01-23 14:59:23 +0100802EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200803
Daniel Vetter3ed43512017-05-31 11:21:46 +0200804/**
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100805 * drm_crtc_vblank_helper_get_vblank_timestamp - precise vblank timestamp
806 * helper
807 * @crtc: CRTC whose vblank timestamp to retrieve
808 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
809 * On return contains true maximum error of timestamp
810 * @vblank_time: Pointer to time which should receive the timestamp
811 * @in_vblank_irq:
812 * True when called from drm_crtc_handle_vblank(). Some drivers
813 * need to apply some workarounds for gpu-specific vblank irq quirks
814 * if flag is set.
815 *
816 * Implements calculation of exact vblank timestamps from given drm_display_mode
817 * timings and current video scanout position of a CRTC. This can be directly
818 * used as the &drm_crtc_funcs.get_vblank_timestamp implementation of a kms
819 * driver if &drm_crtc_helper_funcs.get_scanout_position is implemented.
820 *
821 * The current implementation only handles standard video modes. For double scan
822 * and interlaced modes the driver is supposed to adjust the hardware mode
823 * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
824 * match the scanout position reported.
825 *
826 * Note that atomic drivers must call drm_calc_timestamping_constants() before
827 * enabling a CRTC. The atomic helpers already take care of that in
Ville Syrjälä441959e2020-09-07 15:00:25 +0300828 * drm_atomic_helper_calc_timestamping_constants().
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100829 *
830 * Returns:
831 *
832 * Returns true on success, and false on failure, i.e. when no accurate
833 * timestamp could be acquired.
834 */
835bool drm_crtc_vblank_helper_get_vblank_timestamp(struct drm_crtc *crtc,
836 int *max_error,
837 ktime_t *vblank_time,
838 bool in_vblank_irq)
839{
840 return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
841 crtc, max_error, vblank_time, in_vblank_irq,
Thomas Zimmermann48e6780762020-01-23 14:59:43 +0100842 crtc->helper_private->get_scanout_position);
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100843}
844EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp);
845
846/**
Daniel Vetter3ed43512017-05-31 11:21:46 +0200847 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
848 * vblank interval
849 * @dev: DRM device
850 * @pipe: index of CRTC whose vblank timestamp to retrieve
Arnd Bergmann67680d32017-10-11 17:20:12 +0200851 * @tvblank: Pointer to target time which should receive the timestamp
Daniel Vetter3ed43512017-05-31 11:21:46 +0200852 * @in_vblank_irq:
853 * True when called from drm_crtc_handle_vblank(). Some drivers
854 * need to apply some workarounds for gpu-specific vblank irq quirks
855 * if flag is set.
856 *
857 * Fetches the system timestamp corresponding to the time of the most recent
858 * vblank interval on specified CRTC. May call into kms-driver to
859 * compute the timestamp with a high-precision GPU specific method.
860 *
861 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
862 * call, i.e., it isn't very precisely locked to the true vblank.
863 *
864 * Returns:
865 * True if timestamp is considered to be very precise, false otherwise.
866 */
867static bool
868drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200869 ktime_t *tvblank, bool in_vblank_irq)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200870{
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100871 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200872 bool ret = false;
873
874 /* Define requested maximum error on timestamps (nanoseconds). */
875 int max_error = (int) drm_timestamp_precision * 1000;
876
877 /* Query driver if possible and precision timestamping enabled. */
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100878 if (crtc && crtc->funcs->get_vblank_timestamp && max_error > 0) {
879 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
880
881 ret = crtc->funcs->get_vblank_timestamp(crtc, &max_error,
882 tvblank, in_vblank_irq);
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +0100883 }
Daniel Vetter3ed43512017-05-31 11:21:46 +0200884
885 /* GPU high precision timestamp query unsupported or failed.
886 * Return current monotonic/gettimeofday timestamp as best estimate.
887 */
888 if (!ret)
Arnd Bergmann25e1a792017-10-11 17:20:13 +0200889 *tvblank = ktime_get();
Daniel Vetter3ed43512017-05-31 11:21:46 +0200890
891 return ret;
892}
893
894/**
895 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
896 * @crtc: which counter to retrieve
897 *
898 * Fetches the "cooked" vblank count value that represents the number of
899 * vblank events since the system was booted, including lost events due to
Daniel Vetter57d30232017-05-24 16:51:45 +0200900 * modesetting activity. Note that this timer isn't correct against a racing
901 * vblank interrupt (since it only reports the software vblank counter), see
Daniel Vetterca814b22017-05-24 16:51:47 +0200902 * drm_crtc_accurate_vblank_count() for such use-cases.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200903 *
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200904 * Note that for a given vblank counter value drm_crtc_handle_vblank()
905 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
906 * provide a barrier: Any writes done before calling
907 * drm_crtc_handle_vblank() will be visible to callers of the later
908 * functions, iff the vblank count is the same or a later one.
909 *
910 * See also &drm_vblank_crtc.count.
911 *
Daniel Vetter3ed43512017-05-31 11:21:46 +0200912 * Returns:
913 * The software vblank counter.
914 */
Keith Packard570e8692017-10-12 11:57:49 -0700915u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200916{
917 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
918}
919EXPORT_SYMBOL(drm_crtc_vblank_count);
920
Keith Packard570e8692017-10-12 11:57:49 -0700921/**
922 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
923 * system timestamp corresponding to that vblank counter value.
924 * @dev: DRM device
925 * @pipe: index of CRTC whose counter to retrieve
926 * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
927 *
928 * Fetches the "cooked" vblank count value that represents the number of
929 * vblank events since the system was booted, including lost events due to
930 * modesetting activity. Returns corresponding system timestamp of the time
931 * of the vblank interval that corresponds to the current vblank counter value.
932 *
933 * This is the legacy version of drm_crtc_vblank_count_and_time().
934 */
935static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200936 ktime_t *vblanktime)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200937{
938 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Keith Packard570e8692017-10-12 11:57:49 -0700939 u64 vblank_count;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200940 unsigned int seq;
941
Sam Ravnborg5a4784f2020-05-23 09:12:24 +0200942 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs)) {
Arnd Bergmann67680d32017-10-11 17:20:12 +0200943 *vblanktime = 0;
Daniel Vetter3ed43512017-05-31 11:21:46 +0200944 return 0;
945 }
946
947 do {
948 seq = read_seqbegin(&vblank->seqlock);
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200949 vblank_count = atomic64_read(&vblank->count);
Daniel Vetter3ed43512017-05-31 11:21:46 +0200950 *vblanktime = vblank->time;
951 } while (read_seqretry(&vblank->seqlock, seq));
952
953 return vblank_count;
954}
955
956/**
957 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
958 * and the system timestamp corresponding to that vblank counter value
959 * @crtc: which counter to retrieve
Arnd Bergmann67680d32017-10-11 17:20:12 +0200960 * @vblanktime: Pointer to time to receive the vblank timestamp.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200961 *
962 * Fetches the "cooked" vblank count value that represents the number of
963 * vblank events since the system was booted, including lost events due to
964 * modesetting activity. Returns corresponding system timestamp of the time
965 * of the vblank interval that corresponds to the current vblank counter value.
Daniel Vetterbd7e3f32019-07-23 15:13:37 +0200966 *
967 * Note that for a given vblank counter value drm_crtc_handle_vblank()
968 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
969 * provide a barrier: Any writes done before calling
970 * drm_crtc_handle_vblank() will be visible to callers of the later
971 * functions, iff the vblank count is the same or a later one.
972 *
973 * See also &drm_vblank_crtc.count.
Daniel Vetter3ed43512017-05-31 11:21:46 +0200974 */
Keith Packard570e8692017-10-12 11:57:49 -0700975u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
Arnd Bergmann67680d32017-10-11 17:20:12 +0200976 ktime_t *vblanktime)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200977{
978 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
979 vblanktime);
980}
981EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
982
983static void send_vblank_event(struct drm_device *dev,
984 struct drm_pending_vblank_event *e,
Keith Packard570e8692017-10-12 11:57:49 -0700985 u64 seq, ktime_t now)
Daniel Vetter3ed43512017-05-31 11:21:46 +0200986{
Keith Packardbd386e52017-07-05 14:34:23 -0700987 struct timespec64 tv;
Arnd Bergmann67680d32017-10-11 17:20:12 +0200988
Keith Packardbd386e52017-07-05 14:34:23 -0700989 switch (e->event.base.type) {
990 case DRM_EVENT_VBLANK:
991 case DRM_EVENT_FLIP_COMPLETE:
992 tv = ktime_to_timespec64(now);
993 e->event.vbl.sequence = seq;
994 /*
995 * e->event is a user space structure, with hardcoded unsigned
996 * 32-bit seconds/microseconds. This is safe as we always use
997 * monotonic timestamps since linux-4.15
998 */
999 e->event.vbl.tv_sec = tv.tv_sec;
1000 e->event.vbl.tv_usec = tv.tv_nsec / 1000;
1001 break;
Keith Packard3064abf2017-06-29 22:49:31 -07001002 case DRM_EVENT_CRTC_SEQUENCE:
1003 if (seq)
1004 e->event.seq.sequence = seq;
1005 e->event.seq.time_ns = ktime_to_ns(now);
1006 break;
Keith Packardbd386e52017-07-05 14:34:23 -07001007 }
1008 trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001009 drm_send_event_locked(dev, &e->base);
1010}
1011
1012/**
1013 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1014 * @crtc: the source CRTC of the vblank event
1015 * @e: the event to send
1016 *
1017 * A lot of drivers need to generate vblank events for the very next vblank
1018 * interrupt. For example when the page flip interrupt happens when the page
1019 * flip gets armed, but not when it actually executes within the next vblank
1020 * period. This helper function implements exactly the required vblank arming
1021 * behaviour.
1022 *
1023 * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
1024 * atomic commit must ensure that the next vblank happens at exactly the same
1025 * time as the atomic commit is committed to the hardware. This function itself
Daniel Vettere13a0582017-07-05 06:56:29 +02001026 * does **not** protect against the next vblank interrupt racing with either this
Daniel Vetter3ed43512017-05-31 11:21:46 +02001027 * function call or the atomic commit operation. A possible sequence could be:
1028 *
1029 * 1. Driver commits new hardware state into vblank-synchronized registers.
1030 * 2. A vblank happens, committing the hardware state. Also the corresponding
1031 * vblank interrupt is fired off and fully processed by the interrupt
1032 * handler.
1033 * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
1034 * 4. The event is only send out for the next vblank, which is wrong.
1035 *
1036 * An equivalent race can happen when the driver calls
1037 * drm_crtc_arm_vblank_event() before writing out the new hardware state.
1038 *
1039 * The only way to make this work safely is to prevent the vblank from firing
1040 * (and the hardware from committing anything else) until the entire atomic
1041 * commit sequence has run to completion. If the hardware does not have such a
1042 * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
1043 * Instead drivers need to manually send out the event from their interrupt
1044 * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
1045 * possible race with the hardware committing the atomic update.
1046 *
Stefan Agnere7502182018-08-07 22:11:43 +02001047 * Caller must hold a vblank reference for the event @e acquired by a
1048 * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001049 */
1050void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1051 struct drm_pending_vblank_event *e)
1052{
1053 struct drm_device *dev = crtc->dev;
1054 unsigned int pipe = drm_crtc_index(crtc);
1055
1056 assert_spin_locked(&dev->event_lock);
1057
1058 e->pipe = pipe;
Keith Packard570e8692017-10-12 11:57:49 -07001059 e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001060 list_add_tail(&e->base.link, &dev->vblank_event_list);
1061}
1062EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1063
1064/**
1065 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1066 * @crtc: the source CRTC of the vblank event
1067 * @e: the event to send
1068 *
1069 * Updates sequence # and timestamp on event for the most recently processed
1070 * vblank, and sends it to userspace. Caller must hold event lock.
1071 *
1072 * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
1073 * situation, especially to send out events for atomic commit operations.
1074 */
1075void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1076 struct drm_pending_vblank_event *e)
1077{
1078 struct drm_device *dev = crtc->dev;
Keith Packard570e8692017-10-12 11:57:49 -07001079 u64 seq;
1080 unsigned int pipe = drm_crtc_index(crtc);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001081 ktime_t now;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001082
Daniel Vetter60150022020-05-27 13:11:34 +02001083 if (drm_dev_has_vblank(dev)) {
Daniel Vetter3ed43512017-05-31 11:21:46 +02001084 seq = drm_vblank_count_and_time(dev, pipe, &now);
1085 } else {
1086 seq = 0;
1087
Arnd Bergmann25e1a792017-10-11 17:20:13 +02001088 now = ktime_get();
Daniel Vetter3ed43512017-05-31 11:21:46 +02001089 }
1090 e->pipe = pipe;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001091 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001092}
1093EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1094
1095static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
1096{
1097 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1098 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1099
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001100 if (drm_WARN_ON(dev, !crtc))
Joe Moriarty4ffb8de2018-02-20 14:11:56 -05001101 return 0;
1102
Daniel Vetter3ed43512017-05-31 11:21:46 +02001103 if (crtc->funcs->enable_vblank)
1104 return crtc->funcs->enable_vblank(crtc);
Daniel Vetter57bb1ee2020-11-04 11:04:21 +01001105 }
1106#ifdef CONFIG_DRM_LEGACY
1107 else if (dev->driver->enable_vblank) {
Thomas Zimmermannf397d662020-01-23 14:59:42 +01001108 return dev->driver->enable_vblank(dev, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001109 }
Daniel Vetter57bb1ee2020-11-04 11:04:21 +01001110#endif
Daniel Vetter3ed43512017-05-31 11:21:46 +02001111
Thomas Zimmermannf397d662020-01-23 14:59:42 +01001112 return -EINVAL;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001113}
1114
Daniel Vetter3ed43512017-05-31 11:21:46 +02001115static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1116{
1117 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1118 int ret = 0;
1119
1120 assert_spin_locked(&dev->vbl_lock);
1121
1122 spin_lock(&dev->vblank_time_lock);
1123
1124 if (!vblank->enabled) {
1125 /*
1126 * Enable vblank irqs under vblank_time_lock protection.
1127 * All vblank count & timestamp updates are held off
1128 * until we are done reinitializing master counter and
1129 * timestamps. Filtercode in drm_handle_vblank() will
1130 * prevent double-accounting of same vblank interval.
1131 */
1132 ret = __enable_vblank(dev, pipe);
Sam Ravnborg02149a72020-05-23 09:12:23 +02001133 drm_dbg_core(dev, "enabling vblank on crtc %u, ret: %d\n",
1134 pipe, ret);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001135 if (ret) {
1136 atomic_dec(&vblank->refcount);
1137 } else {
1138 drm_update_vblank_count(dev, pipe, 0);
1139 /* drm_update_vblank_count() includes a wmb so we just
1140 * need to ensure that the compiler emits the write
1141 * to mark the vblank as enabled after the call
1142 * to drm_update_vblank_count().
1143 */
1144 WRITE_ONCE(vblank->enabled, true);
1145 }
1146 }
1147
1148 spin_unlock(&dev->vblank_time_lock);
1149
1150 return ret;
1151}
1152
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001153int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001154{
1155 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1156 unsigned long irqflags;
1157 int ret = 0;
1158
Daniel Vetter60150022020-05-27 13:11:34 +02001159 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001160 return -EINVAL;
1161
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001162 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001163 return -EINVAL;
1164
1165 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1166 /* Going from 0->1 means we have to enable interrupts again */
1167 if (atomic_add_return(1, &vblank->refcount) == 1) {
1168 ret = drm_vblank_enable(dev, pipe);
1169 } else {
1170 if (!vblank->enabled) {
1171 atomic_dec(&vblank->refcount);
1172 ret = -EINVAL;
1173 }
1174 }
1175 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1176
1177 return ret;
1178}
1179
1180/**
1181 * drm_crtc_vblank_get - get a reference count on vblank events
1182 * @crtc: which CRTC to own
1183 *
1184 * Acquire a reference count on vblank events to avoid having them disabled
1185 * while in use.
1186 *
1187 * Returns:
1188 * Zero on success or a negative error code on failure.
1189 */
1190int drm_crtc_vblank_get(struct drm_crtc *crtc)
1191{
1192 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1193}
1194EXPORT_SYMBOL(drm_crtc_vblank_get);
1195
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001196void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001197{
1198 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1199
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001200 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001201 return;
1202
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001203 if (drm_WARN_ON(dev, atomic_read(&vblank->refcount) == 0))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001204 return;
1205
1206 /* Last user schedules interrupt disable */
1207 if (atomic_dec_and_test(&vblank->refcount)) {
1208 if (drm_vblank_offdelay == 0)
1209 return;
1210 else if (drm_vblank_offdelay < 0)
Kees Cooke99e88a2017-10-16 14:43:17 -07001211 vblank_disable_fn(&vblank->disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001212 else if (!dev->vblank_disable_immediate)
1213 mod_timer(&vblank->disable_timer,
1214 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1215 }
1216}
1217
1218/**
1219 * drm_crtc_vblank_put - give up ownership of vblank events
1220 * @crtc: which counter to give up
1221 *
1222 * Release ownership of a given vblank counter, turning off interrupts
1223 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1224 */
1225void drm_crtc_vblank_put(struct drm_crtc *crtc)
1226{
1227 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1228}
1229EXPORT_SYMBOL(drm_crtc_vblank_put);
1230
1231/**
1232 * drm_wait_one_vblank - wait for one vblank
1233 * @dev: DRM device
1234 * @pipe: CRTC index
1235 *
1236 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1237 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1238 * due to lack of driver support or because the crtc is off.
Daniel Vetter57d30232017-05-24 16:51:45 +02001239 *
1240 * This is the legacy version of drm_crtc_wait_one_vblank().
Daniel Vetter3ed43512017-05-31 11:21:46 +02001241 */
1242void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1243{
1244 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1245 int ret;
Dhinakaran Pandiyan3b765c02018-02-02 21:12:53 -08001246 u64 last;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001247
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001248 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001249 return;
1250
1251 ret = drm_vblank_get(dev, pipe);
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001252 if (drm_WARN(dev, ret, "vblank not available on crtc %i, ret=%i\n",
1253 pipe, ret))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001254 return;
1255
1256 last = drm_vblank_count(dev, pipe);
1257
1258 ret = wait_event_timeout(vblank->queue,
1259 last != drm_vblank_count(dev, pipe),
1260 msecs_to_jiffies(100));
1261
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001262 drm_WARN(dev, ret == 0, "vblank wait timed out on crtc %i\n", pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001263
1264 drm_vblank_put(dev, pipe);
1265}
1266EXPORT_SYMBOL(drm_wait_one_vblank);
1267
1268/**
1269 * drm_crtc_wait_one_vblank - wait for one vblank
1270 * @crtc: DRM crtc
1271 *
1272 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1273 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1274 * due to lack of driver support or because the crtc is off.
1275 */
1276void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1277{
1278 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1279}
1280EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1281
1282/**
1283 * drm_crtc_vblank_off - disable vblank events on a CRTC
1284 * @crtc: CRTC in question
1285 *
1286 * Drivers can use this function to shut down the vblank interrupt handling when
1287 * disabling a crtc. This function ensures that the latest vblank frame count is
1288 * stored so that drm_vblank_on can restore it again.
1289 *
1290 * Drivers must use this function when the hardware vblank counter can get
Daniel Vetter57d30232017-05-24 16:51:45 +02001291 * reset, e.g. when suspending or disabling the @crtc in general.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001292 */
1293void drm_crtc_vblank_off(struct drm_crtc *crtc)
1294{
1295 struct drm_device *dev = crtc->dev;
1296 unsigned int pipe = drm_crtc_index(crtc);
1297 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1298 struct drm_pending_vblank_event *e, *t;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001299 ktime_t now;
Keith Packard570e8692017-10-12 11:57:49 -07001300 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001301
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001302 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001303 return;
1304
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001305 /*
1306 * Grab event_lock early to prevent vblank work from being scheduled
1307 * while we're in the middle of shutting down vblank interrupts
1308 */
Lyude Paula7e3ad52020-06-23 16:24:00 -04001309 spin_lock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001310
1311 spin_lock(&dev->vbl_lock);
Sam Ravnborg02149a72020-05-23 09:12:23 +02001312 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1313 pipe, vblank->enabled, vblank->inmodeset);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001314
1315 /* Avoid redundant vblank disables without previous
1316 * drm_crtc_vblank_on(). */
1317 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1318 drm_vblank_disable_and_save(dev, pipe);
1319
1320 wake_up(&vblank->queue);
1321
1322 /*
1323 * Prevent subsequent drm_vblank_get() from re-enabling
1324 * the vblank interrupt by bumping the refcount.
1325 */
1326 if (!vblank->inmodeset) {
1327 atomic_inc(&vblank->refcount);
1328 vblank->inmodeset = 1;
1329 }
1330 spin_unlock(&dev->vbl_lock);
1331
1332 /* Send any queued vblank events, lest the natives grow disquiet */
1333 seq = drm_vblank_count_and_time(dev, pipe, &now);
1334
1335 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1336 if (e->pipe != pipe)
1337 continue;
Sam Ravnborg02149a72020-05-23 09:12:23 +02001338 drm_dbg_core(dev, "Sending premature vblank event on disable: "
1339 "wanted %llu, current %llu\n",
1340 e->sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001341 list_del(&e->base.link);
1342 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001343 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001344 }
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001345
1346 /* Cancel any leftover pending vblank work */
1347 drm_vblank_cancel_pending_works(vblank);
1348
Lyude Paula7e3ad52020-06-23 16:24:00 -04001349 spin_unlock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001350
1351 /* Will be reset by the modeset helpers when re-enabling the crtc by
1352 * calling drm_calc_timestamping_constants(). */
1353 vblank->hwmode.crtc_clock = 0;
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001354
1355 /* Wait for any vblank work that's still executing to finish */
1356 drm_vblank_flush_worker(vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001357}
1358EXPORT_SYMBOL(drm_crtc_vblank_off);
1359
1360/**
1361 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1362 * @crtc: CRTC in question
1363 *
1364 * Drivers can use this function to reset the vblank state to off at load time.
1365 * Drivers should use this together with the drm_crtc_vblank_off() and
1366 * drm_crtc_vblank_on() functions. The difference compared to
1367 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1368 * and hence doesn't need to call any driver hooks.
Daniel Vetter57d30232017-05-24 16:51:45 +02001369 *
1370 * This is useful for recovering driver state e.g. on driver load, or on resume.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001371 */
1372void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1373{
1374 struct drm_device *dev = crtc->dev;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001375 unsigned int pipe = drm_crtc_index(crtc);
1376 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1377
Lyude Paulfbc678e2020-07-20 15:07:32 -04001378 spin_lock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001379 /*
1380 * Prevent subsequent drm_vblank_get() from enabling the vblank
1381 * interrupt by bumping the refcount.
1382 */
1383 if (!vblank->inmodeset) {
1384 atomic_inc(&vblank->refcount);
1385 vblank->inmodeset = 1;
1386 }
Lyude Paulfbc678e2020-07-20 15:07:32 -04001387 spin_unlock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001388
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001389 drm_WARN_ON(dev, !list_empty(&dev->vblank_event_list));
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001390 drm_WARN_ON(dev, !list_empty(&vblank->pending_work));
Daniel Vetter3ed43512017-05-31 11:21:46 +02001391}
1392EXPORT_SYMBOL(drm_crtc_vblank_reset);
1393
1394/**
Ville Syrjäläed201512018-11-27 20:20:04 +02001395 * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
1396 * @crtc: CRTC in question
1397 * @max_vblank_count: max hardware vblank counter value
1398 *
1399 * Update the maximum hardware vblank counter value for @crtc
1400 * at runtime. Useful for hardware where the operation of the
1401 * hardware vblank counter depends on the currently active
1402 * display configuration.
1403 *
1404 * For example, if the hardware vblank counter does not work
1405 * when a specific connector is active the maximum can be set
1406 * to zero. And when that specific connector isn't active the
1407 * maximum can again be set to the appropriate non-zero value.
1408 *
1409 * If used, must be called before drm_vblank_on().
1410 */
1411void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
1412 u32 max_vblank_count)
1413{
1414 struct drm_device *dev = crtc->dev;
1415 unsigned int pipe = drm_crtc_index(crtc);
1416 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1417
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001418 drm_WARN_ON(dev, dev->max_vblank_count);
1419 drm_WARN_ON(dev, !READ_ONCE(vblank->inmodeset));
Ville Syrjäläed201512018-11-27 20:20:04 +02001420
1421 vblank->max_vblank_count = max_vblank_count;
1422}
1423EXPORT_SYMBOL(drm_crtc_set_max_vblank_count);
1424
1425/**
Daniel Vetter3ed43512017-05-31 11:21:46 +02001426 * drm_crtc_vblank_on - enable vblank events on a CRTC
1427 * @crtc: CRTC in question
1428 *
1429 * This functions restores the vblank interrupt state captured with
Daniel Vetter57d30232017-05-24 16:51:45 +02001430 * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
1431 * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
1432 * unbalanced and so can also be unconditionally called in driver load code to
1433 * reflect the current hardware state of the crtc.
Daniel Vetter3ed43512017-05-31 11:21:46 +02001434 */
1435void drm_crtc_vblank_on(struct drm_crtc *crtc)
1436{
1437 struct drm_device *dev = crtc->dev;
1438 unsigned int pipe = drm_crtc_index(crtc);
1439 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Daniel Vetter3ed43512017-05-31 11:21:46 +02001440
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001441 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001442 return;
1443
Lyude Paul92cc68e2020-07-20 15:07:33 -04001444 spin_lock_irq(&dev->vbl_lock);
Sam Ravnborg02149a72020-05-23 09:12:23 +02001445 drm_dbg_vbl(dev, "crtc %d, vblank enabled %d, inmodeset %d\n",
1446 pipe, vblank->enabled, vblank->inmodeset);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001447
1448 /* Drop our private "prevent drm_vblank_get" refcount */
1449 if (vblank->inmodeset) {
1450 atomic_dec(&vblank->refcount);
1451 vblank->inmodeset = 0;
1452 }
1453
1454 drm_reset_vblank_timestamp(dev, pipe);
1455
1456 /*
1457 * re-enable interrupts if there are users left, or the
1458 * user wishes vblank interrupts to be enabled all the time.
1459 */
1460 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001461 drm_WARN_ON(dev, drm_vblank_enable(dev, pipe));
Lyude Paul92cc68e2020-07-20 15:07:33 -04001462 spin_unlock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001463}
1464EXPORT_SYMBOL(drm_crtc_vblank_on);
1465
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001466/**
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001467 * drm_vblank_restore - estimate missed vblanks and update vblank count.
1468 * @dev: DRM device
1469 * @pipe: CRTC index
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001470 *
1471 * Power manamement features can cause frame counter resets between vblank
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001472 * disable and enable. Drivers can use this function in their
1473 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1474 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1475 * vblank counter.
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001476 *
1477 * This function is the legacy version of drm_crtc_vblank_restore().
1478 */
1479void drm_vblank_restore(struct drm_device *dev, unsigned int pipe)
1480{
1481 ktime_t t_vblank;
1482 struct drm_vblank_crtc *vblank;
1483 int framedur_ns;
1484 u64 diff_ns;
1485 u32 cur_vblank, diff = 1;
1486 int count = DRM_TIMESTAMP_MAXRETRIES;
1487
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001488 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001489 return;
1490
1491 assert_spin_locked(&dev->vbl_lock);
1492 assert_spin_locked(&dev->vblank_time_lock);
1493
1494 vblank = &dev->vblank[pipe];
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001495 drm_WARN_ONCE(dev,
1496 drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
1497 "Cannot compute missed vblanks without frame duration\n");
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001498 framedur_ns = vblank->framedur_ns;
1499
1500 do {
1501 cur_vblank = __get_vblank_counter(dev, pipe);
1502 drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
1503 } while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
1504
1505 diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
1506 if (framedur_ns)
1507 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
1508
1509
Sam Ravnborg02149a72020-05-23 09:12:23 +02001510 drm_dbg_vbl(dev,
1511 "missed %d vblanks in %lld ns, frame duration=%d ns, hw_diff=%d\n",
1512 diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001513 store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
1514}
1515EXPORT_SYMBOL(drm_vblank_restore);
1516
1517/**
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001518 * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
1519 * @crtc: CRTC in question
1520 *
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001521 * Power manamement features can cause frame counter resets between vblank
Dhinakaran Pandiyanea3f0ef2018-02-20 23:39:08 -08001522 * disable and enable. Drivers can use this function in their
1523 * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
1524 * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
1525 * vblank counter.
Dhinakaran Pandiyand0bb96b2018-02-02 21:13:01 -08001526 */
1527void drm_crtc_vblank_restore(struct drm_crtc *crtc)
1528{
1529 drm_vblank_restore(crtc->dev, drm_crtc_index(crtc));
1530}
1531EXPORT_SYMBOL(drm_crtc_vblank_restore);
1532
Daniel Vetter3ed43512017-05-31 11:21:46 +02001533static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
1534 unsigned int pipe)
1535{
1536 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1537
1538 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Daniel Vetter60150022020-05-27 13:11:34 +02001539 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001540 return;
1541
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001542 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001543 return;
1544
1545 /*
1546 * To avoid all the problems that might happen if interrupts
1547 * were enabled/disabled around or between these calls, we just
1548 * have the kernel take a reference on the CRTC (just once though
1549 * to avoid corrupting the count if multiple, mismatch calls occur),
1550 * so that interrupts remain enabled in the interim.
1551 */
1552 if (!vblank->inmodeset) {
1553 vblank->inmodeset = 0x1;
1554 if (drm_vblank_get(dev, pipe) == 0)
1555 vblank->inmodeset |= 0x2;
1556 }
1557}
1558
1559static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
1560 unsigned int pipe)
1561{
1562 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
Daniel Vetter3ed43512017-05-31 11:21:46 +02001563
1564 /* vblank is not initialized (IRQ not installed ?), or has been freed */
Daniel Vetter60150022020-05-27 13:11:34 +02001565 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001566 return;
1567
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001568 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001569 return;
1570
1571 if (vblank->inmodeset) {
Lyude Pauleb2ee0e2020-07-20 15:07:34 -04001572 spin_lock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001573 drm_reset_vblank_timestamp(dev, pipe);
Lyude Pauleb2ee0e2020-07-20 15:07:34 -04001574 spin_unlock_irq(&dev->vbl_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001575
1576 if (vblank->inmodeset & 0x2)
1577 drm_vblank_put(dev, pipe);
1578
1579 vblank->inmodeset = 0;
1580 }
1581}
1582
Daniel Vetterb6dcaaac2017-05-24 16:51:46 +02001583int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
1584 struct drm_file *file_priv)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001585{
1586 struct drm_modeset_ctl *modeset = data;
1587 unsigned int pipe;
1588
1589 /* If drm_vblank_init() hasn't been called yet, just no-op */
Daniel Vetter60150022020-05-27 13:11:34 +02001590 if (!drm_dev_has_vblank(dev))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001591 return 0;
1592
1593 /* KMS drivers handle this internally */
1594 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1595 return 0;
1596
1597 pipe = modeset->crtc;
1598 if (pipe >= dev->num_crtcs)
1599 return -EINVAL;
1600
1601 switch (modeset->cmd) {
1602 case _DRM_PRE_MODESET:
1603 drm_legacy_vblank_pre_modeset(dev, pipe);
1604 break;
1605 case _DRM_POST_MODESET:
1606 drm_legacy_vblank_post_modeset(dev, pipe);
1607 break;
1608 default:
1609 return -EINVAL;
1610 }
1611
1612 return 0;
1613}
1614
Daniel Vetter3ed43512017-05-31 11:21:46 +02001615static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
Keith Packard570e8692017-10-12 11:57:49 -07001616 u64 req_seq,
Daniel Vetter3ed43512017-05-31 11:21:46 +02001617 union drm_wait_vblank *vblwait,
1618 struct drm_file *file_priv)
1619{
1620 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1621 struct drm_pending_vblank_event *e;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001622 ktime_t now;
Keith Packard570e8692017-10-12 11:57:49 -07001623 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001624 int ret;
1625
1626 e = kzalloc(sizeof(*e), GFP_KERNEL);
1627 if (e == NULL) {
1628 ret = -ENOMEM;
1629 goto err_put;
1630 }
1631
1632 e->pipe = pipe;
1633 e->event.base.type = DRM_EVENT_VBLANK;
Keith Packardbd386e52017-07-05 14:34:23 -07001634 e->event.base.length = sizeof(e->event.vbl);
1635 e->event.vbl.user_data = vblwait->request.signal;
1636 e->event.vbl.crtc_id = 0;
1637 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1638 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
Suraj Upadhyay948de8422020-07-02 18:53:32 +05301639
Keith Packardbd386e52017-07-05 14:34:23 -07001640 if (crtc)
1641 e->event.vbl.crtc_id = crtc->base.id;
1642 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001643
Lyude Paul7f07c352020-07-20 15:07:35 -04001644 spin_lock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001645
1646 /*
1647 * drm_crtc_vblank_off() might have been called after we called
1648 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
1649 * vblank disable, so no need for further locking. The reference from
1650 * drm_vblank_get() protects against vblank disable from another source.
1651 */
1652 if (!READ_ONCE(vblank->enabled)) {
1653 ret = -EINVAL;
1654 goto err_unlock;
1655 }
1656
1657 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
1658 &e->event.base);
1659
1660 if (ret)
1661 goto err_unlock;
1662
1663 seq = drm_vblank_count_and_time(dev, pipe, &now);
1664
Sam Ravnborg02149a72020-05-23 09:12:23 +02001665 drm_dbg_core(dev, "event on vblank count %llu, current %llu, crtc %u\n",
1666 req_seq, seq, pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001667
Keith Packard570e8692017-10-12 11:57:49 -07001668 trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001669
Keith Packard570e8692017-10-12 11:57:49 -07001670 e->sequence = req_seq;
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001671 if (drm_vblank_passed(seq, req_seq)) {
Daniel Vetter3ed43512017-05-31 11:21:46 +02001672 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001673 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001674 vblwait->reply.sequence = seq;
1675 } else {
1676 /* drm_handle_vblank_events will call drm_vblank_put */
1677 list_add_tail(&e->base.link, &dev->vblank_event_list);
Keith Packard570e8692017-10-12 11:57:49 -07001678 vblwait->reply.sequence = req_seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001679 }
1680
Lyude Paul7f07c352020-07-20 15:07:35 -04001681 spin_unlock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001682
1683 return 0;
1684
1685err_unlock:
Lyude Paul7f07c352020-07-20 15:07:35 -04001686 spin_unlock_irq(&dev->event_lock);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001687 kfree(e);
1688err_put:
1689 drm_vblank_put(dev, pipe);
1690 return ret;
1691}
1692
1693static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
1694{
1695 if (vblwait->request.sequence)
1696 return false;
1697
1698 return _DRM_VBLANK_RELATIVE ==
1699 (vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
1700 _DRM_VBLANK_EVENT |
1701 _DRM_VBLANK_NEXTONMISS));
1702}
1703
Keith Packard570e8692017-10-12 11:57:49 -07001704/*
1705 * Widen a 32-bit param to 64-bits.
1706 *
1707 * \param narrow 32-bit value (missing upper 32 bits)
1708 * \param near 64-bit value that should be 'close' to near
1709 *
1710 * This function returns a 64-bit value using the lower 32-bits from
1711 * 'narrow' and constructing the upper 32-bits so that the result is
1712 * as close as possible to 'near'.
1713 */
1714
1715static u64 widen_32_to_64(u32 narrow, u64 near)
1716{
1717 return near + (s32) (narrow - near);
1718}
1719
Arnd Bergmann67680d32017-10-11 17:20:12 +02001720static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
1721 struct drm_wait_vblank_reply *reply)
1722{
1723 ktime_t now;
1724 struct timespec64 ts;
1725
1726 /*
1727 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
Arnd Bergmann25e1a792017-10-11 17:20:13 +02001728 * to store the seconds. This is safe as we always use monotonic
1729 * timestamps since linux-4.15.
Arnd Bergmann67680d32017-10-11 17:20:12 +02001730 */
1731 reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
1732 ts = ktime_to_timespec64(now);
1733 reply->tval_sec = (u32)ts.tv_sec;
1734 reply->tval_usec = ts.tv_nsec / 1000;
1735}
1736
Daniel Vetterb6dcaaac2017-05-24 16:51:46 +02001737int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
1738 struct drm_file *file_priv)
Daniel Vetter3ed43512017-05-31 11:21:46 +02001739{
Keith Packard7de440d2017-04-09 22:35:34 -06001740 struct drm_crtc *crtc;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001741 struct drm_vblank_crtc *vblank;
1742 union drm_wait_vblank *vblwait = data;
1743 int ret;
Keith Packard570e8692017-10-12 11:57:49 -07001744 u64 req_seq, seq;
Keith Packard7de440d2017-04-09 22:35:34 -06001745 unsigned int pipe_index;
Keith Packard570e8692017-10-12 11:57:49 -07001746 unsigned int flags, pipe, high_pipe;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001747
1748 if (!dev->irq_enabled)
Rodrigo Siqueiraaed61052019-10-02 11:05:16 -03001749 return -EOPNOTSUPP;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001750
1751 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1752 return -EINVAL;
1753
1754 if (vblwait->request.type &
1755 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1756 _DRM_VBLANK_HIGH_CRTC_MASK)) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02001757 drm_dbg_core(dev,
1758 "Unsupported type value 0x%x, supported mask 0x%x\n",
1759 vblwait->request.type,
1760 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1761 _DRM_VBLANK_HIGH_CRTC_MASK));
Daniel Vetter3ed43512017-05-31 11:21:46 +02001762 return -EINVAL;
1763 }
1764
1765 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1766 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1767 if (high_pipe)
Keith Packard7de440d2017-04-09 22:35:34 -06001768 pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001769 else
Keith Packard7de440d2017-04-09 22:35:34 -06001770 pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1771
1772 /* Convert lease-relative crtc index into global crtc index */
1773 if (drm_core_check_feature(dev, DRIVER_MODESET)) {
1774 pipe = 0;
1775 drm_for_each_crtc(crtc, dev) {
1776 if (drm_lease_held(file_priv, crtc->base.id)) {
1777 if (pipe_index == 0)
1778 break;
1779 pipe_index--;
1780 }
1781 pipe++;
1782 }
1783 } else {
1784 pipe = pipe_index;
1785 }
1786
Daniel Vetter3ed43512017-05-31 11:21:46 +02001787 if (pipe >= dev->num_crtcs)
1788 return -EINVAL;
1789
1790 vblank = &dev->vblank[pipe];
1791
1792 /* If the counter is currently enabled and accurate, short-circuit
1793 * queries to return the cached timestamp of the last vblank.
1794 */
1795 if (dev->vblank_disable_immediate &&
1796 drm_wait_vblank_is_query(vblwait) &&
1797 READ_ONCE(vblank->enabled)) {
Arnd Bergmann67680d32017-10-11 17:20:12 +02001798 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001799 return 0;
1800 }
1801
1802 ret = drm_vblank_get(dev, pipe);
1803 if (ret) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02001804 drm_dbg_core(dev,
1805 "crtc %d failed to acquire vblank counter, %d\n",
1806 pipe, ret);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001807 return ret;
1808 }
1809 seq = drm_vblank_count(dev, pipe);
1810
1811 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1812 case _DRM_VBLANK_RELATIVE:
Keith Packard570e8692017-10-12 11:57:49 -07001813 req_seq = seq + vblwait->request.sequence;
1814 vblwait->request.sequence = req_seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001815 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Keith Packard570e8692017-10-12 11:57:49 -07001816 break;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001817 case _DRM_VBLANK_ABSOLUTE:
Keith Packard570e8692017-10-12 11:57:49 -07001818 req_seq = widen_32_to_64(vblwait->request.sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001819 break;
1820 default:
1821 ret = -EINVAL;
1822 goto done;
1823 }
1824
1825 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001826 drm_vblank_passed(seq, req_seq)) {
Keith Packard570e8692017-10-12 11:57:49 -07001827 req_seq = seq + 1;
1828 vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
1829 vblwait->request.sequence = req_seq;
1830 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001831
1832 if (flags & _DRM_VBLANK_EVENT) {
1833 /* must hold on to the vblank ref until the event fires
1834 * drm_vblank_put will be called asynchronously
1835 */
Keith Packard570e8692017-10-12 11:57:49 -07001836 return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001837 }
1838
Keith Packard570e8692017-10-12 11:57:49 -07001839 if (req_seq != seq) {
Sam Ravnborg05103ea2019-07-26 23:06:58 +02001840 int wait;
1841
Sam Ravnborg02149a72020-05-23 09:12:23 +02001842 drm_dbg_core(dev, "waiting on vblank count %llu, crtc %u\n",
1843 req_seq, pipe);
Sam Ravnborg05103ea2019-07-26 23:06:58 +02001844 wait = wait_event_interruptible_timeout(vblank->queue,
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001845 drm_vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
Sam Ravnborg05103ea2019-07-26 23:06:58 +02001846 !READ_ONCE(vblank->enabled),
1847 msecs_to_jiffies(3000));
1848
1849 switch (wait) {
1850 case 0:
1851 /* timeout */
1852 ret = -EBUSY;
1853 break;
1854 case -ERESTARTSYS:
1855 /* interrupted by signal */
1856 ret = -EINTR;
1857 break;
1858 default:
1859 ret = 0;
1860 break;
1861 }
Daniel Vetter3ed43512017-05-31 11:21:46 +02001862 }
1863
1864 if (ret != -EINTR) {
Arnd Bergmann67680d32017-10-11 17:20:12 +02001865 drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001866
Sam Ravnborg02149a72020-05-23 09:12:23 +02001867 drm_dbg_core(dev, "crtc %d returning %u to client\n",
1868 pipe, vblwait->reply.sequence);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001869 } else {
Sam Ravnborg02149a72020-05-23 09:12:23 +02001870 drm_dbg_core(dev, "crtc %d vblank wait interrupted by signal\n",
1871 pipe);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001872 }
1873
1874done:
1875 drm_vblank_put(dev, pipe);
1876 return ret;
1877}
1878
1879static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1880{
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +01001881 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
1882 bool high_prec = false;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001883 struct drm_pending_vblank_event *e, *t;
Arnd Bergmann67680d32017-10-11 17:20:12 +02001884 ktime_t now;
Keith Packard570e8692017-10-12 11:57:49 -07001885 u64 seq;
Daniel Vetter3ed43512017-05-31 11:21:46 +02001886
1887 assert_spin_locked(&dev->event_lock);
1888
1889 seq = drm_vblank_count_and_time(dev, pipe, &now);
1890
1891 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1892 if (e->pipe != pipe)
1893 continue;
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001894 if (!drm_vblank_passed(seq, e->sequence))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001895 continue;
1896
Sam Ravnborg02149a72020-05-23 09:12:23 +02001897 drm_dbg_core(dev, "vblank event on %llu, current %llu\n",
1898 e->sequence, seq);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001899
1900 list_del(&e->base.link);
1901 drm_vblank_put(dev, pipe);
Arnd Bergmann67680d32017-10-11 17:20:12 +02001902 send_vblank_event(dev, e, seq, now);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001903 }
1904
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +01001905 if (crtc && crtc->funcs->get_vblank_timestamp)
1906 high_prec = true;
Thomas Zimmermann7fe3f0d2020-01-23 14:59:24 +01001907
1908 trace_drm_vblank_event(pipe, seq, now, high_prec);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001909}
1910
1911/**
1912 * drm_handle_vblank - handle a vblank event
1913 * @dev: DRM device
1914 * @pipe: index of CRTC where this event occurred
1915 *
1916 * Drivers should call this routine in their vblank interrupt handlers to
1917 * update the vblank counter and send any signals that may be pending.
1918 *
1919 * This is the legacy version of drm_crtc_handle_vblank().
1920 */
1921bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1922{
1923 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1924 unsigned long irqflags;
1925 bool disable_irq;
1926
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001927 if (drm_WARN_ON_ONCE(dev, !drm_dev_has_vblank(dev)))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001928 return false;
1929
Sam Ravnborg5a4784f2020-05-23 09:12:24 +02001930 if (drm_WARN_ON(dev, pipe >= dev->num_crtcs))
Daniel Vetter3ed43512017-05-31 11:21:46 +02001931 return false;
1932
1933 spin_lock_irqsave(&dev->event_lock, irqflags);
1934
1935 /* Need timestamp lock to prevent concurrent execution with
1936 * vblank enable/disable, as this would cause inconsistent
1937 * or corrupted timestamps and vblank counts.
1938 */
1939 spin_lock(&dev->vblank_time_lock);
1940
1941 /* Vblank irq handling disabled. Nothing to do. */
1942 if (!vblank->enabled) {
1943 spin_unlock(&dev->vblank_time_lock);
1944 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1945 return false;
1946 }
1947
1948 drm_update_vblank_count(dev, pipe, true);
1949
1950 spin_unlock(&dev->vblank_time_lock);
1951
1952 wake_up(&vblank->queue);
1953
1954 /* With instant-off, we defer disabling the interrupt until after
1955 * we finish processing the following vblank after all events have
1956 * been signaled. The disable has to be last (after
1957 * drm_handle_vblank_events) so that the timestamp is always accurate.
1958 */
1959 disable_irq = (dev->vblank_disable_immediate &&
1960 drm_vblank_offdelay > 0 &&
1961 !atomic_read(&vblank->refcount));
1962
1963 drm_handle_vblank_events(dev, pipe);
Lyude Paul5e6c2b42020-04-17 15:33:13 -04001964 drm_handle_vblank_works(vblank);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001965
1966 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1967
1968 if (disable_irq)
Kees Cooke99e88a2017-10-16 14:43:17 -07001969 vblank_disable_fn(&vblank->disable_timer);
Daniel Vetter3ed43512017-05-31 11:21:46 +02001970
1971 return true;
1972}
1973EXPORT_SYMBOL(drm_handle_vblank);
1974
1975/**
1976 * drm_crtc_handle_vblank - handle a vblank event
1977 * @crtc: where this event occurred
1978 *
1979 * Drivers should call this routine in their vblank interrupt handlers to
1980 * update the vblank counter and send any signals that may be pending.
1981 *
1982 * This is the native KMS version of drm_handle_vblank().
1983 *
Daniel Vetterbd7e3f32019-07-23 15:13:37 +02001984 * Note that for a given vblank counter value drm_crtc_handle_vblank()
1985 * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
1986 * provide a barrier: Any writes done before calling
1987 * drm_crtc_handle_vblank() will be visible to callers of the later
1988 * functions, iff the vblank count is the same or a later one.
1989 *
1990 * See also &drm_vblank_crtc.count.
1991 *
Daniel Vetter3ed43512017-05-31 11:21:46 +02001992 * Returns:
1993 * True if the event was successfully handled, false on failure.
1994 */
1995bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
1996{
1997 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
1998}
1999EXPORT_SYMBOL(drm_crtc_handle_vblank);
Keith Packard3064abf2017-06-29 22:49:31 -07002000
2001/*
2002 * Get crtc VBLANK count.
2003 *
2004 * \param dev DRM device
2005 * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
2006 * \param file_priv drm file private for the user's open file descriptor
2007 */
2008
2009int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
2010 struct drm_file *file_priv)
2011{
2012 struct drm_crtc *crtc;
2013 struct drm_vblank_crtc *vblank;
2014 int pipe;
2015 struct drm_crtc_get_sequence *get_seq = data;
2016 ktime_t now;
2017 bool vblank_enabled;
2018 int ret;
2019
2020 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson69fdf422018-09-13 20:20:50 +01002021 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002022
2023 if (!dev->irq_enabled)
Rodrigo Siqueiraaed61052019-10-02 11:05:16 -03002024 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002025
2026 crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
2027 if (!crtc)
2028 return -ENOENT;
2029
2030 pipe = drm_crtc_index(crtc);
2031
2032 vblank = &dev->vblank[pipe];
2033 vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
2034
2035 if (!vblank_enabled) {
2036 ret = drm_crtc_vblank_get(crtc);
2037 if (ret) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02002038 drm_dbg_core(dev,
2039 "crtc %d failed to acquire vblank counter, %d\n",
2040 pipe, ret);
Keith Packard3064abf2017-06-29 22:49:31 -07002041 return ret;
2042 }
2043 }
2044 drm_modeset_lock(&crtc->mutex, NULL);
2045 if (crtc->state)
2046 get_seq->active = crtc->state->enable;
2047 else
2048 get_seq->active = crtc->enabled;
2049 drm_modeset_unlock(&crtc->mutex);
2050 get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
2051 get_seq->sequence_ns = ktime_to_ns(now);
2052 if (!vblank_enabled)
2053 drm_crtc_vblank_put(crtc);
2054 return 0;
2055}
2056
2057/*
2058 * Queue a event for VBLANK sequence
2059 *
2060 * \param dev DRM device
2061 * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
2062 * \param file_priv drm file private for the user's open file descriptor
2063 */
2064
2065int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
2066 struct drm_file *file_priv)
2067{
2068 struct drm_crtc *crtc;
2069 struct drm_vblank_crtc *vblank;
2070 int pipe;
2071 struct drm_crtc_queue_sequence *queue_seq = data;
2072 ktime_t now;
2073 struct drm_pending_vblank_event *e;
2074 u32 flags;
2075 u64 seq;
2076 u64 req_seq;
2077 int ret;
Keith Packard3064abf2017-06-29 22:49:31 -07002078
2079 if (!drm_core_check_feature(dev, DRIVER_MODESET))
Chris Wilson69fdf422018-09-13 20:20:50 +01002080 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002081
2082 if (!dev->irq_enabled)
Rodrigo Siqueiraaed61052019-10-02 11:05:16 -03002083 return -EOPNOTSUPP;
Keith Packard3064abf2017-06-29 22:49:31 -07002084
2085 crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
2086 if (!crtc)
2087 return -ENOENT;
2088
2089 flags = queue_seq->flags;
2090 /* Check valid flag bits */
2091 if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
2092 DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
2093 return -EINVAL;
2094
2095 pipe = drm_crtc_index(crtc);
2096
2097 vblank = &dev->vblank[pipe];
2098
2099 e = kzalloc(sizeof(*e), GFP_KERNEL);
2100 if (e == NULL)
2101 return -ENOMEM;
2102
2103 ret = drm_crtc_vblank_get(crtc);
2104 if (ret) {
Sam Ravnborg02149a72020-05-23 09:12:23 +02002105 drm_dbg_core(dev,
2106 "crtc %d failed to acquire vblank counter, %d\n",
2107 pipe, ret);
Keith Packard3064abf2017-06-29 22:49:31 -07002108 goto err_free;
2109 }
2110
2111 seq = drm_vblank_count_and_time(dev, pipe, &now);
2112 req_seq = queue_seq->sequence;
2113
2114 if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
2115 req_seq += seq;
2116
Lyude Paul5e6c2b42020-04-17 15:33:13 -04002117 if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && drm_vblank_passed(seq, req_seq))
Keith Packard3064abf2017-06-29 22:49:31 -07002118 req_seq = seq + 1;
2119
2120 e->pipe = pipe;
2121 e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
2122 e->event.base.length = sizeof(e->event.seq);
2123 e->event.seq.user_data = queue_seq->user_data;
2124
Lyude Paul5887a2a2020-07-20 15:07:36 -04002125 spin_lock_irq(&dev->event_lock);
Keith Packard3064abf2017-06-29 22:49:31 -07002126
2127 /*
2128 * drm_crtc_vblank_off() might have been called after we called
2129 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
2130 * vblank disable, so no need for further locking. The reference from
2131 * drm_crtc_vblank_get() protects against vblank disable from another source.
2132 */
2133 if (!READ_ONCE(vblank->enabled)) {
2134 ret = -EINVAL;
2135 goto err_unlock;
2136 }
2137
2138 ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
2139 &e->event.base);
2140
2141 if (ret)
2142 goto err_unlock;
2143
2144 e->sequence = req_seq;
2145
Lyude Paul5e6c2b42020-04-17 15:33:13 -04002146 if (drm_vblank_passed(seq, req_seq)) {
Keith Packard3064abf2017-06-29 22:49:31 -07002147 drm_crtc_vblank_put(crtc);
2148 send_vblank_event(dev, e, seq, now);
2149 queue_seq->sequence = seq;
2150 } else {
2151 /* drm_handle_vblank_events will call drm_vblank_put */
2152 list_add_tail(&e->base.link, &dev->vblank_event_list);
2153 queue_seq->sequence = req_seq;
2154 }
2155
Lyude Paul5887a2a2020-07-20 15:07:36 -04002156 spin_unlock_irq(&dev->event_lock);
Keith Packard3064abf2017-06-29 22:49:31 -07002157 return 0;
2158
2159err_unlock:
Lyude Paul5887a2a2020-07-20 15:07:36 -04002160 spin_unlock_irq(&dev->event_lock);
Keith Packard3064abf2017-06-29 22:49:31 -07002161 drm_crtc_vblank_put(crtc);
2162err_free:
2163 kfree(e);
2164 return ret;
2165}
Lyude Paul5e6c2b42020-04-17 15:33:13 -04002166