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