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