Home | History | Annotate | Line # | Download | only in drm
      1  1.16  riastrad /*	$NetBSD: drm_vblank.c,v 1.16 2021/12/26 21:00:14 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.16  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_vblank.c,v 1.16 2021/12/26 21:00:14 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.12  riastrad  * Lock order: event_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.12  riastrad 	assert_spin_locked(&dev->event_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.12  riastrad 	assert_spin_locked(&dev->event_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.16  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.12  riastrad 	assert_spin_locked(&dev->event_lock);
    348  1.12  riastrad 
    349   1.1  riastrad 	WARN_ONCE(drm_debug_enabled(DRM_UT_VBL) && !dev->driver->get_vblank_timestamp,
    350   1.1  riastrad 		  "This function requires support for accurate vblank timestamps.");
    351   1.1  riastrad 
    352   1.1  riastrad 	spin_lock_irqsave(&dev->vblank_time_lock, flags);
    353   1.1  riastrad 
    354   1.1  riastrad 	drm_update_vblank_count(dev, pipe, false);
    355   1.1  riastrad 	vblank = drm_vblank_count(dev, pipe);
    356   1.1  riastrad 
    357   1.1  riastrad 	spin_unlock_irqrestore(&dev->vblank_time_lock, flags);
    358  1.12  riastrad 
    359  1.12  riastrad 	return vblank;
    360  1.12  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.12  riastrad 	assert_spin_locked(&dev->event_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.12  riastrad 	assert_spin_locked(&dev->event_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.12  riastrad 	spin_lock_irqsave(&dev->event_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.12  riastrad 	spin_unlock_irqrestore(&dev->event_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 		seqlock_destroy(&vblank->seqlock);
    467   1.1  riastrad 	}
    468   1.1  riastrad 
    469   1.1  riastrad 	kfree(dev->vblank);
    470   1.1  riastrad 
    471   1.1  riastrad 	dev->num_crtcs = 0;
    472  1.14  riastrad 
    473  1.14  riastrad 	spin_lock_destroy(&dev->vblank_time_lock);
    474   1.1  riastrad }
    475   1.1  riastrad 
    476   1.1  riastrad /**
    477   1.1  riastrad  * drm_vblank_init - initialize vblank support
    478   1.1  riastrad  * @dev: DRM device
    479   1.1  riastrad  * @num_crtcs: number of CRTCs supported by @dev
    480   1.1  riastrad  *
    481   1.1  riastrad  * This function initializes vblank support for @num_crtcs display pipelines.
    482   1.1  riastrad  * Cleanup is handled by the DRM core, or through calling drm_dev_fini() for
    483   1.1  riastrad  * drivers with a &drm_driver.release callback.
    484   1.1  riastrad  *
    485   1.1  riastrad  * Returns:
    486   1.1  riastrad  * Zero on success or a negative error code on failure.
    487   1.1  riastrad  */
    488   1.1  riastrad int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
    489   1.1  riastrad {
    490   1.1  riastrad 	int ret = -ENOMEM;
    491   1.1  riastrad 	unsigned int i;
    492   1.1  riastrad 
    493   1.1  riastrad 	spin_lock_init(&dev->vblank_time_lock);
    494   1.1  riastrad 
    495   1.1  riastrad 	dev->num_crtcs = num_crtcs;
    496   1.1  riastrad 
    497   1.1  riastrad 	dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
    498   1.1  riastrad 	if (!dev->vblank)
    499   1.1  riastrad 		goto err;
    500   1.1  riastrad 
    501   1.1  riastrad 	for (i = 0; i < num_crtcs; i++) {
    502   1.1  riastrad 		struct drm_vblank_crtc *vblank = &dev->vblank[i];
    503   1.1  riastrad 
    504   1.1  riastrad 		vblank->dev = dev;
    505   1.1  riastrad 		vblank->pipe = i;
    506   1.5  riastrad 		DRM_INIT_WAITQUEUE(&vblank->queue, "drmvblnq");
    507   1.1  riastrad 		timer_setup(&vblank->disable_timer, vblank_disable_fn, 0);
    508   1.1  riastrad 		seqlock_init(&vblank->seqlock);
    509   1.1  riastrad 	}
    510   1.1  riastrad 
    511   1.1  riastrad 	DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
    512   1.1  riastrad 
    513   1.1  riastrad 	/* Driver specific high-precision vblank timestamping supported? */
    514   1.1  riastrad 	if (dev->driver->get_vblank_timestamp)
    515   1.1  riastrad 		DRM_INFO("Driver supports precise vblank timestamp query.\n");
    516   1.1  riastrad 	else
    517   1.1  riastrad 		DRM_INFO("No driver support for vblank timestamp query.\n");
    518   1.1  riastrad 
    519   1.1  riastrad 	/* Must have precise timestamping for reliable vblank instant disable */
    520   1.1  riastrad 	if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
    521   1.1  riastrad 		dev->vblank_disable_immediate = false;
    522   1.1  riastrad 		DRM_INFO("Setting vblank_disable_immediate to false because "
    523   1.1  riastrad 			 "get_vblank_timestamp == NULL\n");
    524   1.1  riastrad 	}
    525   1.1  riastrad 
    526   1.1  riastrad 	return 0;
    527   1.1  riastrad 
    528   1.1  riastrad err:
    529   1.1  riastrad 	dev->num_crtcs = 0;
    530   1.1  riastrad 	return ret;
    531   1.1  riastrad }
    532   1.1  riastrad EXPORT_SYMBOL(drm_vblank_init);
    533   1.1  riastrad 
    534   1.1  riastrad /**
    535   1.1  riastrad  * drm_crtc_vblank_waitqueue - get vblank waitqueue for the CRTC
    536   1.1  riastrad  * @crtc: which CRTC's vblank waitqueue to retrieve
    537   1.1  riastrad  *
    538   1.1  riastrad  * This function returns a pointer to the vblank waitqueue for the CRTC.
    539   1.1  riastrad  * Drivers can use this to implement vblank waits using wait_event() and related
    540   1.1  riastrad  * functions.
    541   1.1  riastrad  */
    542   1.5  riastrad drm_waitqueue_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc)
    543   1.1  riastrad {
    544   1.1  riastrad 	return &crtc->dev->vblank[drm_crtc_index(crtc)].queue;
    545   1.1  riastrad }
    546   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_waitqueue);
    547   1.1  riastrad 
    548   1.1  riastrad 
    549   1.1  riastrad /**
    550   1.1  riastrad  * drm_calc_timestamping_constants - calculate vblank timestamp constants
    551   1.1  riastrad  * @crtc: drm_crtc whose timestamp constants should be updated.
    552   1.1  riastrad  * @mode: display mode containing the scanout timings
    553   1.1  riastrad  *
    554   1.1  riastrad  * Calculate and store various constants which are later needed by vblank and
    555   1.1  riastrad  * swap-completion timestamping, e.g, by
    556   1.1  riastrad  * drm_calc_vbltimestamp_from_scanoutpos(). They are derived from CRTC's true
    557   1.1  riastrad  * scanout timing, so they take things like panel scaling or other adjustments
    558   1.1  riastrad  * into account.
    559   1.1  riastrad  */
    560   1.1  riastrad void drm_calc_timestamping_constants(struct drm_crtc *crtc,
    561   1.1  riastrad 				     const struct drm_display_mode *mode)
    562   1.1  riastrad {
    563   1.1  riastrad 	struct drm_device *dev = crtc->dev;
    564   1.1  riastrad 	unsigned int pipe = drm_crtc_index(crtc);
    565   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
    566   1.1  riastrad 	int linedur_ns = 0, framedur_ns = 0;
    567   1.1  riastrad 	int dotclock = mode->crtc_clock;
    568   1.1  riastrad 
    569   1.1  riastrad 	if (!dev->num_crtcs)
    570   1.1  riastrad 		return;
    571   1.1  riastrad 
    572   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
    573   1.1  riastrad 		return;
    574   1.1  riastrad 
    575   1.1  riastrad 	/* Valid dotclock? */
    576   1.1  riastrad 	if (dotclock > 0) {
    577   1.1  riastrad 		int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
    578   1.1  riastrad 
    579   1.1  riastrad 		/*
    580   1.1  riastrad 		 * Convert scanline length in pixels and video
    581   1.1  riastrad 		 * dot clock to line duration and frame duration
    582   1.1  riastrad 		 * in nanoseconds:
    583   1.1  riastrad 		 */
    584   1.1  riastrad 		linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
    585   1.1  riastrad 		framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
    586   1.1  riastrad 
    587   1.1  riastrad 		/*
    588   1.1  riastrad 		 * Fields of interlaced scanout modes are only half a frame duration.
    589   1.1  riastrad 		 */
    590   1.1  riastrad 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
    591   1.1  riastrad 			framedur_ns /= 2;
    592   1.1  riastrad 	} else
    593   1.1  riastrad 		DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
    594   1.1  riastrad 			  crtc->base.id);
    595   1.1  riastrad 
    596   1.1  riastrad 	vblank->linedur_ns  = linedur_ns;
    597   1.1  riastrad 	vblank->framedur_ns = framedur_ns;
    598   1.1  riastrad 	vblank->hwmode = *mode;
    599   1.1  riastrad 
    600   1.1  riastrad 	DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
    601   1.1  riastrad 		  crtc->base.id, mode->crtc_htotal,
    602   1.1  riastrad 		  mode->crtc_vtotal, mode->crtc_vdisplay);
    603   1.1  riastrad 	DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
    604   1.1  riastrad 		  crtc->base.id, dotclock, framedur_ns, linedur_ns);
    605   1.1  riastrad }
    606   1.1  riastrad EXPORT_SYMBOL(drm_calc_timestamping_constants);
    607   1.1  riastrad 
    608   1.1  riastrad /**
    609   1.1  riastrad  * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
    610   1.1  riastrad  * @dev: DRM device
    611   1.1  riastrad  * @pipe: index of CRTC whose vblank timestamp to retrieve
    612   1.1  riastrad  * @max_error: Desired maximum allowable error in timestamps (nanosecs)
    613   1.1  riastrad  *             On return contains true maximum error of timestamp
    614   1.1  riastrad  * @vblank_time: Pointer to time which should receive the timestamp
    615   1.1  riastrad  * @in_vblank_irq:
    616   1.1  riastrad  *     True when called from drm_crtc_handle_vblank().  Some drivers
    617   1.1  riastrad  *     need to apply some workarounds for gpu-specific vblank irq quirks
    618   1.1  riastrad  *     if flag is set.
    619   1.1  riastrad  *
    620   1.1  riastrad  * Implements calculation of exact vblank timestamps from given drm_display_mode
    621   1.1  riastrad  * timings and current video scanout position of a CRTC. This can be directly
    622   1.1  riastrad  * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver
    623   1.1  riastrad  * if &drm_driver.get_scanout_position is implemented.
    624   1.1  riastrad  *
    625   1.1  riastrad  * The current implementation only handles standard video modes. For double scan
    626   1.1  riastrad  * and interlaced modes the driver is supposed to adjust the hardware mode
    627   1.1  riastrad  * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to
    628   1.1  riastrad  * match the scanout position reported.
    629   1.1  riastrad  *
    630   1.1  riastrad  * Note that atomic drivers must call drm_calc_timestamping_constants() before
    631   1.1  riastrad  * enabling a CRTC. The atomic helpers already take care of that in
    632   1.1  riastrad  * drm_atomic_helper_update_legacy_modeset_state().
    633   1.1  riastrad  *
    634   1.1  riastrad  * Returns:
    635   1.1  riastrad  *
    636   1.1  riastrad  * Returns true on success, and false on failure, i.e. when no accurate
    637   1.1  riastrad  * timestamp could be acquired.
    638   1.1  riastrad  */
    639   1.1  riastrad bool drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
    640   1.1  riastrad 					   unsigned int pipe,
    641   1.1  riastrad 					   int *max_error,
    642   1.1  riastrad 					   ktime_t *vblank_time,
    643   1.1  riastrad 					   bool in_vblank_irq)
    644   1.1  riastrad {
    645   1.1  riastrad 	struct timespec64 ts_etime, ts_vblank_time;
    646   1.1  riastrad 	ktime_t stime, etime;
    647   1.1  riastrad 	bool vbl_status;
    648   1.1  riastrad 	struct drm_crtc *crtc;
    649   1.1  riastrad 	const struct drm_display_mode *mode;
    650   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
    651   1.1  riastrad 	int vpos, hpos, i;
    652   1.1  riastrad 	int delta_ns, duration_ns;
    653   1.1  riastrad 
    654   1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
    655   1.1  riastrad 		return false;
    656   1.1  riastrad 
    657   1.1  riastrad 	crtc = drm_crtc_from_index(dev, pipe);
    658   1.1  riastrad 
    659   1.1  riastrad 	if (pipe >= dev->num_crtcs || !crtc) {
    660   1.1  riastrad 		DRM_ERROR("Invalid crtc %u\n", pipe);
    661   1.1  riastrad 		return false;
    662   1.1  riastrad 	}
    663   1.1  riastrad 
    664   1.1  riastrad 	/* Scanout position query not supported? Should not happen. */
    665   1.1  riastrad 	if (!dev->driver->get_scanout_position) {
    666   1.1  riastrad 		DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
    667   1.1  riastrad 		return false;
    668   1.1  riastrad 	}
    669   1.1  riastrad 
    670   1.1  riastrad 	if (drm_drv_uses_atomic_modeset(dev))
    671   1.1  riastrad 		mode = &vblank->hwmode;
    672   1.1  riastrad 	else
    673   1.1  riastrad 		mode = &crtc->hwmode;
    674   1.1  riastrad 
    675   1.1  riastrad 	/* If mode timing undefined, just return as no-op:
    676   1.1  riastrad 	 * Happens during initial modesetting of a crtc.
    677   1.1  riastrad 	 */
    678   1.1  riastrad 	if (mode->crtc_clock == 0) {
    679   1.1  riastrad 		DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
    680   1.1  riastrad 		WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev));
    681   1.1  riastrad 
    682   1.1  riastrad 		return false;
    683   1.1  riastrad 	}
    684   1.1  riastrad 
    685   1.1  riastrad 	/* Get current scanout position with system timestamp.
    686   1.1  riastrad 	 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
    687   1.1  riastrad 	 * if single query takes longer than max_error nanoseconds.
    688   1.1  riastrad 	 *
    689   1.1  riastrad 	 * This guarantees a tight bound on maximum error if
    690   1.1  riastrad 	 * code gets preempted or delayed for some reason.
    691   1.1  riastrad 	 */
    692   1.1  riastrad 	for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
    693   1.1  riastrad 		/*
    694   1.1  riastrad 		 * Get vertical and horizontal scanout position vpos, hpos,
    695   1.1  riastrad 		 * and bounding timestamps stime, etime, pre/post query.
    696   1.1  riastrad 		 */
    697   1.1  riastrad 		vbl_status = dev->driver->get_scanout_position(dev, pipe,
    698   1.1  riastrad 							       in_vblank_irq,
    699   1.1  riastrad 							       &vpos, &hpos,
    700   1.1  riastrad 							       &stime, &etime,
    701   1.1  riastrad 							       mode);
    702   1.1  riastrad 
    703   1.1  riastrad 		/* Return as no-op if scanout query unsupported or failed. */
    704   1.1  riastrad 		if (!vbl_status) {
    705   1.1  riastrad 			DRM_DEBUG("crtc %u : scanoutpos query failed.\n",
    706   1.1  riastrad 				  pipe);
    707   1.1  riastrad 			return false;
    708   1.1  riastrad 		}
    709   1.1  riastrad 
    710   1.1  riastrad 		/* Compute uncertainty in timestamp of scanout position query. */
    711   1.1  riastrad 		duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
    712   1.1  riastrad 
    713   1.1  riastrad 		/* Accept result with <  max_error nsecs timing uncertainty. */
    714   1.1  riastrad 		if (duration_ns <= *max_error)
    715   1.1  riastrad 			break;
    716   1.1  riastrad 	}
    717   1.1  riastrad 
    718   1.1  riastrad 	/* Noisy system timing? */
    719   1.1  riastrad 	if (i == DRM_TIMESTAMP_MAXRETRIES) {
    720   1.1  riastrad 		DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
    721   1.1  riastrad 			  pipe, duration_ns/1000, *max_error/1000, i);
    722   1.1  riastrad 	}
    723   1.1  riastrad 
    724   1.1  riastrad 	/* Return upper bound of timestamp precision error. */
    725   1.1  riastrad 	*max_error = duration_ns;
    726   1.1  riastrad 
    727   1.1  riastrad 	/* Convert scanout position into elapsed time at raw_time query
    728   1.1  riastrad 	 * since start of scanout at first display scanline. delta_ns
    729   1.1  riastrad 	 * can be negative if start of scanout hasn't happened yet.
    730   1.1  riastrad 	 */
    731   1.1  riastrad 	delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
    732   1.1  riastrad 			   mode->crtc_clock);
    733   1.1  riastrad 
    734   1.1  riastrad 	/* Subtract time delta from raw timestamp to get final
    735   1.1  riastrad 	 * vblank_time timestamp for end of vblank.
    736   1.1  riastrad 	 */
    737   1.1  riastrad 	*vblank_time = ktime_sub_ns(etime, delta_ns);
    738   1.1  riastrad 
    739   1.1  riastrad 	if (!drm_debug_enabled(DRM_UT_VBL))
    740   1.1  riastrad 		return true;
    741   1.1  riastrad 
    742   1.1  riastrad 	ts_etime = ktime_to_timespec64(etime);
    743   1.1  riastrad 	ts_vblank_time = ktime_to_timespec64(*vblank_time);
    744   1.1  riastrad 
    745   1.3  riastrad 	DRM_DEBUG_VBL("crtc %u : v p(%d,%d)@ %"PRId64".%06ld -> %"PRId64".%06ld [e %d us, %d rep]\n",
    746   1.1  riastrad 		      pipe, hpos, vpos,
    747   1.1  riastrad 		      (u64)ts_etime.tv_sec, ts_etime.tv_nsec / 1000,
    748   1.1  riastrad 		      (u64)ts_vblank_time.tv_sec, ts_vblank_time.tv_nsec / 1000,
    749   1.1  riastrad 		      duration_ns / 1000, i);
    750   1.1  riastrad 
    751   1.1  riastrad 	return true;
    752   1.1  riastrad }
    753   1.1  riastrad EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
    754   1.1  riastrad 
    755   1.1  riastrad /**
    756   1.1  riastrad  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
    757   1.1  riastrad  *                             vblank interval
    758   1.1  riastrad  * @dev: DRM device
    759   1.1  riastrad  * @pipe: index of CRTC whose vblank timestamp to retrieve
    760   1.1  riastrad  * @tvblank: Pointer to target time which should receive the timestamp
    761   1.1  riastrad  * @in_vblank_irq:
    762   1.1  riastrad  *     True when called from drm_crtc_handle_vblank().  Some drivers
    763   1.1  riastrad  *     need to apply some workarounds for gpu-specific vblank irq quirks
    764   1.1  riastrad  *     if flag is set.
    765   1.1  riastrad  *
    766   1.1  riastrad  * Fetches the system timestamp corresponding to the time of the most recent
    767   1.1  riastrad  * vblank interval on specified CRTC. May call into kms-driver to
    768   1.1  riastrad  * compute the timestamp with a high-precision GPU specific method.
    769   1.1  riastrad  *
    770   1.1  riastrad  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
    771   1.1  riastrad  * call, i.e., it isn't very precisely locked to the true vblank.
    772   1.1  riastrad  *
    773   1.1  riastrad  * Returns:
    774   1.1  riastrad  * True if timestamp is considered to be very precise, false otherwise.
    775   1.1  riastrad  */
    776   1.1  riastrad static bool
    777   1.1  riastrad drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
    778   1.1  riastrad 			  ktime_t *tvblank, bool in_vblank_irq)
    779   1.1  riastrad {
    780   1.1  riastrad 	bool ret = false;
    781   1.1  riastrad 
    782   1.1  riastrad 	/* Define requested maximum error on timestamps (nanoseconds). */
    783   1.1  riastrad 	int max_error = (int) drm_timestamp_precision * 1000;
    784   1.1  riastrad 
    785   1.1  riastrad 	/* Query driver if possible and precision timestamping enabled. */
    786   1.1  riastrad 	if (dev->driver->get_vblank_timestamp && (max_error > 0))
    787   1.1  riastrad 		ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
    788   1.1  riastrad 							tvblank, in_vblank_irq);
    789   1.1  riastrad 
    790   1.1  riastrad 	/* GPU high precision timestamp query unsupported or failed.
    791   1.1  riastrad 	 * Return current monotonic/gettimeofday timestamp as best estimate.
    792   1.1  riastrad 	 */
    793   1.1  riastrad 	if (!ret)
    794   1.1  riastrad 		*tvblank = ktime_get();
    795   1.1  riastrad 
    796   1.1  riastrad 	return ret;
    797   1.1  riastrad }
    798   1.1  riastrad 
    799   1.1  riastrad /**
    800   1.1  riastrad  * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
    801   1.1  riastrad  * @crtc: which counter to retrieve
    802   1.1  riastrad  *
    803   1.1  riastrad  * Fetches the "cooked" vblank count value that represents the number of
    804   1.1  riastrad  * vblank events since the system was booted, including lost events due to
    805   1.1  riastrad  * modesetting activity. Note that this timer isn't correct against a racing
    806   1.1  riastrad  * vblank interrupt (since it only reports the software vblank counter), see
    807   1.1  riastrad  * drm_crtc_accurate_vblank_count() for such use-cases.
    808   1.1  riastrad  *
    809   1.1  riastrad  * Note that for a given vblank counter value drm_crtc_handle_vblank()
    810   1.1  riastrad  * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
    811   1.1  riastrad  * provide a barrier: Any writes done before calling
    812   1.1  riastrad  * drm_crtc_handle_vblank() will be visible to callers of the later
    813   1.1  riastrad  * functions, iff the vblank count is the same or a later one.
    814   1.1  riastrad  *
    815   1.1  riastrad  * See also &drm_vblank_crtc.count.
    816   1.1  riastrad  *
    817   1.1  riastrad  * Returns:
    818   1.1  riastrad  * The software vblank counter.
    819   1.1  riastrad  */
    820   1.1  riastrad u64 drm_crtc_vblank_count(struct drm_crtc *crtc)
    821   1.1  riastrad {
    822   1.1  riastrad 	return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
    823   1.1  riastrad }
    824   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_count);
    825   1.1  riastrad 
    826   1.1  riastrad /**
    827   1.1  riastrad  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
    828   1.1  riastrad  *     system timestamp corresponding to that vblank counter value.
    829   1.1  riastrad  * @dev: DRM device
    830   1.1  riastrad  * @pipe: index of CRTC whose counter to retrieve
    831   1.1  riastrad  * @vblanktime: Pointer to ktime_t to receive the vblank timestamp.
    832   1.1  riastrad  *
    833   1.1  riastrad  * Fetches the "cooked" vblank count value that represents the number of
    834   1.1  riastrad  * vblank events since the system was booted, including lost events due to
    835   1.1  riastrad  * modesetting activity. Returns corresponding system timestamp of the time
    836   1.1  riastrad  * of the vblank interval that corresponds to the current vblank counter value.
    837   1.1  riastrad  *
    838   1.1  riastrad  * This is the legacy version of drm_crtc_vblank_count_and_time().
    839   1.1  riastrad  */
    840   1.1  riastrad static u64 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
    841   1.1  riastrad 				     ktime_t *vblanktime)
    842   1.1  riastrad {
    843   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
    844   1.1  riastrad 	u64 vblank_count;
    845   1.1  riastrad 	unsigned int seq;
    846   1.1  riastrad 
    847   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs)) {
    848   1.1  riastrad 		*vblanktime = 0;
    849   1.1  riastrad 		return 0;
    850   1.1  riastrad 	}
    851   1.1  riastrad 
    852   1.1  riastrad 	do {
    853   1.1  riastrad 		seq = read_seqbegin(&vblank->seqlock);
    854   1.1  riastrad 		vblank_count = atomic64_read(&vblank->count);
    855   1.1  riastrad 		*vblanktime = vblank->time;
    856   1.1  riastrad 	} while (read_seqretry(&vblank->seqlock, seq));
    857   1.1  riastrad 
    858   1.1  riastrad 	return vblank_count;
    859   1.1  riastrad }
    860   1.1  riastrad 
    861   1.1  riastrad /**
    862   1.1  riastrad  * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
    863   1.1  riastrad  *     and the system timestamp corresponding to that vblank counter value
    864   1.1  riastrad  * @crtc: which counter to retrieve
    865   1.1  riastrad  * @vblanktime: Pointer to time to receive the vblank timestamp.
    866   1.1  riastrad  *
    867   1.1  riastrad  * Fetches the "cooked" vblank count value that represents the number of
    868   1.1  riastrad  * vblank events since the system was booted, including lost events due to
    869   1.1  riastrad  * modesetting activity. Returns corresponding system timestamp of the time
    870   1.1  riastrad  * of the vblank interval that corresponds to the current vblank counter value.
    871   1.1  riastrad  *
    872   1.1  riastrad  * Note that for a given vblank counter value drm_crtc_handle_vblank()
    873   1.1  riastrad  * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
    874   1.1  riastrad  * provide a barrier: Any writes done before calling
    875   1.1  riastrad  * drm_crtc_handle_vblank() will be visible to callers of the later
    876   1.1  riastrad  * functions, iff the vblank count is the same or a later one.
    877   1.1  riastrad  *
    878   1.1  riastrad  * See also &drm_vblank_crtc.count.
    879   1.1  riastrad  */
    880   1.1  riastrad u64 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
    881   1.1  riastrad 				   ktime_t *vblanktime)
    882   1.1  riastrad {
    883   1.1  riastrad 	return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
    884   1.1  riastrad 					 vblanktime);
    885   1.1  riastrad }
    886   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
    887   1.1  riastrad 
    888   1.1  riastrad static void send_vblank_event(struct drm_device *dev,
    889   1.1  riastrad 		struct drm_pending_vblank_event *e,
    890   1.1  riastrad 		u64 seq, ktime_t now)
    891   1.1  riastrad {
    892   1.1  riastrad 	struct timespec64 tv;
    893   1.1  riastrad 
    894   1.1  riastrad 	switch (e->event.base.type) {
    895   1.1  riastrad 	case DRM_EVENT_VBLANK:
    896   1.1  riastrad 	case DRM_EVENT_FLIP_COMPLETE:
    897   1.1  riastrad 		tv = ktime_to_timespec64(now);
    898   1.1  riastrad 		e->event.vbl.sequence = seq;
    899   1.1  riastrad 		/*
    900   1.1  riastrad 		 * e->event is a user space structure, with hardcoded unsigned
    901   1.1  riastrad 		 * 32-bit seconds/microseconds. This is safe as we always use
    902   1.1  riastrad 		 * monotonic timestamps since linux-4.15
    903   1.1  riastrad 		 */
    904   1.1  riastrad 		e->event.vbl.tv_sec = tv.tv_sec;
    905   1.1  riastrad 		e->event.vbl.tv_usec = tv.tv_nsec / 1000;
    906   1.1  riastrad 		break;
    907   1.1  riastrad 	case DRM_EVENT_CRTC_SEQUENCE:
    908   1.1  riastrad 		if (seq)
    909   1.1  riastrad 			e->event.seq.sequence = seq;
    910   1.1  riastrad 		e->event.seq.time_ns = ktime_to_ns(now);
    911   1.1  riastrad 		break;
    912   1.1  riastrad 	}
    913   1.1  riastrad 	trace_drm_vblank_event_delivered(e->base.file_priv, e->pipe, seq);
    914   1.1  riastrad 	drm_send_event_locked(dev, &e->base);
    915   1.1  riastrad }
    916   1.1  riastrad 
    917   1.1  riastrad /**
    918   1.1  riastrad  * drm_crtc_arm_vblank_event - arm vblank event after pageflip
    919   1.1  riastrad  * @crtc: the source CRTC of the vblank event
    920   1.1  riastrad  * @e: the event to send
    921   1.1  riastrad  *
    922   1.1  riastrad  * A lot of drivers need to generate vblank events for the very next vblank
    923   1.1  riastrad  * interrupt. For example when the page flip interrupt happens when the page
    924   1.1  riastrad  * flip gets armed, but not when it actually executes within the next vblank
    925   1.1  riastrad  * period. This helper function implements exactly the required vblank arming
    926   1.1  riastrad  * behaviour.
    927   1.1  riastrad  *
    928   1.1  riastrad  * NOTE: Drivers using this to send out the &drm_crtc_state.event as part of an
    929   1.1  riastrad  * atomic commit must ensure that the next vblank happens at exactly the same
    930   1.1  riastrad  * time as the atomic commit is committed to the hardware. This function itself
    931   1.1  riastrad  * does **not** protect against the next vblank interrupt racing with either this
    932   1.1  riastrad  * function call or the atomic commit operation. A possible sequence could be:
    933   1.1  riastrad  *
    934   1.1  riastrad  * 1. Driver commits new hardware state into vblank-synchronized registers.
    935   1.1  riastrad  * 2. A vblank happens, committing the hardware state. Also the corresponding
    936   1.1  riastrad  *    vblank interrupt is fired off and fully processed by the interrupt
    937   1.1  riastrad  *    handler.
    938   1.1  riastrad  * 3. The atomic commit operation proceeds to call drm_crtc_arm_vblank_event().
    939   1.1  riastrad  * 4. The event is only send out for the next vblank, which is wrong.
    940   1.1  riastrad  *
    941   1.1  riastrad  * An equivalent race can happen when the driver calls
    942   1.1  riastrad  * drm_crtc_arm_vblank_event() before writing out the new hardware state.
    943   1.1  riastrad  *
    944   1.1  riastrad  * The only way to make this work safely is to prevent the vblank from firing
    945   1.1  riastrad  * (and the hardware from committing anything else) until the entire atomic
    946   1.1  riastrad  * commit sequence has run to completion. If the hardware does not have such a
    947   1.1  riastrad  * feature (e.g. using a "go" bit), then it is unsafe to use this functions.
    948   1.1  riastrad  * Instead drivers need to manually send out the event from their interrupt
    949   1.1  riastrad  * handler by calling drm_crtc_send_vblank_event() and make sure that there's no
    950   1.1  riastrad  * possible race with the hardware committing the atomic update.
    951   1.1  riastrad  *
    952   1.1  riastrad  * Caller must hold a vblank reference for the event @e acquired by a
    953   1.1  riastrad  * drm_crtc_vblank_get(), which will be dropped when the next vblank arrives.
    954   1.1  riastrad  */
    955   1.1  riastrad void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
    956   1.1  riastrad 			       struct drm_pending_vblank_event *e)
    957   1.1  riastrad {
    958   1.1  riastrad 	struct drm_device *dev = crtc->dev;
    959   1.1  riastrad 	unsigned int pipe = drm_crtc_index(crtc);
    960   1.1  riastrad 
    961   1.1  riastrad 	assert_spin_locked(&dev->event_lock);
    962   1.1  riastrad 
    963   1.1  riastrad 	e->pipe = pipe;
    964  1.16  riastrad 	e->sequence = drm_crtc_accurate_vblank_count(crtc) + 1;
    965   1.1  riastrad 	list_add_tail(&e->base.link, &dev->vblank_event_list);
    966   1.1  riastrad }
    967   1.1  riastrad EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
    968   1.1  riastrad 
    969   1.1  riastrad /**
    970   1.1  riastrad  * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
    971   1.1  riastrad  * @crtc: the source CRTC of the vblank event
    972   1.1  riastrad  * @e: the event to send
    973   1.1  riastrad  *
    974   1.1  riastrad  * Updates sequence # and timestamp on event for the most recently processed
    975   1.1  riastrad  * vblank, and sends it to userspace.  Caller must hold event lock.
    976   1.1  riastrad  *
    977   1.1  riastrad  * See drm_crtc_arm_vblank_event() for a helper which can be used in certain
    978   1.1  riastrad  * situation, especially to send out events for atomic commit operations.
    979   1.1  riastrad  */
    980   1.1  riastrad void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
    981   1.1  riastrad 				struct drm_pending_vblank_event *e)
    982   1.1  riastrad {
    983   1.1  riastrad 	struct drm_device *dev = crtc->dev;
    984   1.1  riastrad 	u64 seq;
    985   1.1  riastrad 	unsigned int pipe = drm_crtc_index(crtc);
    986   1.1  riastrad 	ktime_t now;
    987   1.1  riastrad 
    988   1.1  riastrad 	if (dev->num_crtcs > 0) {
    989   1.1  riastrad 		seq = drm_vblank_count_and_time(dev, pipe, &now);
    990   1.1  riastrad 	} else {
    991   1.1  riastrad 		seq = 0;
    992   1.1  riastrad 
    993   1.1  riastrad 		now = ktime_get();
    994   1.1  riastrad 	}
    995   1.1  riastrad 	e->pipe = pipe;
    996   1.1  riastrad 	send_vblank_event(dev, e, seq, now);
    997   1.1  riastrad }
    998   1.1  riastrad EXPORT_SYMBOL(drm_crtc_send_vblank_event);
    999   1.1  riastrad 
   1000   1.1  riastrad static int __enable_vblank(struct drm_device *dev, unsigned int pipe)
   1001   1.1  riastrad {
   1002   1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
   1003   1.1  riastrad 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
   1004   1.1  riastrad 
   1005   1.1  riastrad 		if (WARN_ON(!crtc))
   1006   1.1  riastrad 			return 0;
   1007   1.1  riastrad 
   1008   1.1  riastrad 		if (crtc->funcs->enable_vblank)
   1009   1.1  riastrad 			return crtc->funcs->enable_vblank(crtc);
   1010   1.1  riastrad 	}
   1011   1.1  riastrad 
   1012   1.1  riastrad 	return dev->driver->enable_vblank(dev, pipe);
   1013   1.1  riastrad }
   1014   1.1  riastrad 
   1015   1.1  riastrad static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
   1016   1.1  riastrad {
   1017   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1018   1.1  riastrad 	int ret = 0;
   1019   1.1  riastrad 
   1020  1.12  riastrad 	assert_spin_locked(&dev->event_lock);
   1021   1.1  riastrad 
   1022   1.1  riastrad 	spin_lock(&dev->vblank_time_lock);
   1023   1.1  riastrad 
   1024   1.1  riastrad 	if (!vblank->enabled) {
   1025   1.1  riastrad 		/*
   1026   1.1  riastrad 		 * Enable vblank irqs under vblank_time_lock protection.
   1027   1.1  riastrad 		 * All vblank count & timestamp updates are held off
   1028   1.1  riastrad 		 * until we are done reinitializing master counter and
   1029   1.1  riastrad 		 * timestamps. Filtercode in drm_handle_vblank() will
   1030   1.1  riastrad 		 * prevent double-accounting of same vblank interval.
   1031   1.1  riastrad 		 */
   1032   1.1  riastrad 		ret = __enable_vblank(dev, pipe);
   1033   1.1  riastrad 		DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
   1034   1.1  riastrad 		if (ret) {
   1035   1.1  riastrad 			atomic_dec(&vblank->refcount);
   1036   1.1  riastrad 		} else {
   1037   1.1  riastrad 			drm_update_vblank_count(dev, pipe, 0);
   1038   1.1  riastrad 			/* drm_update_vblank_count() includes a wmb so we just
   1039   1.1  riastrad 			 * need to ensure that the compiler emits the write
   1040   1.1  riastrad 			 * to mark the vblank as enabled after the call
   1041   1.1  riastrad 			 * to drm_update_vblank_count().
   1042   1.1  riastrad 			 */
   1043   1.1  riastrad 			WRITE_ONCE(vblank->enabled, true);
   1044   1.1  riastrad 		}
   1045   1.1  riastrad 	}
   1046   1.1  riastrad 
   1047   1.1  riastrad 	spin_unlock(&dev->vblank_time_lock);
   1048   1.1  riastrad 
   1049   1.1  riastrad 	return ret;
   1050   1.1  riastrad }
   1051   1.1  riastrad 
   1052  1.12  riastrad static int drm_vblank_get_locked(struct drm_device *dev, unsigned int pipe)
   1053   1.1  riastrad {
   1054   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1055   1.1  riastrad 	int ret = 0;
   1056   1.1  riastrad 
   1057  1.12  riastrad 	assert_spin_locked(&dev->event_lock);
   1058  1.12  riastrad 
   1059   1.1  riastrad 	if (!dev->num_crtcs)
   1060   1.1  riastrad 		return -EINVAL;
   1061   1.1  riastrad 
   1062   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1063   1.1  riastrad 		return -EINVAL;
   1064   1.1  riastrad 
   1065   1.1  riastrad 	/* Going from 0->1 means we have to enable interrupts again */
   1066   1.1  riastrad 	if (atomic_add_return(1, &vblank->refcount) == 1) {
   1067   1.1  riastrad 		ret = drm_vblank_enable(dev, pipe);
   1068   1.1  riastrad 	} else {
   1069   1.1  riastrad 		if (!vblank->enabled) {
   1070   1.1  riastrad 			atomic_dec(&vblank->refcount);
   1071   1.1  riastrad 			ret = -EINVAL;
   1072   1.1  riastrad 		}
   1073   1.1  riastrad 	}
   1074  1.12  riastrad 
   1075  1.12  riastrad 	return ret;
   1076  1.12  riastrad }
   1077  1.12  riastrad 
   1078  1.12  riastrad static int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
   1079  1.12  riastrad {
   1080  1.12  riastrad 	int ret;
   1081  1.12  riastrad 
   1082  1.12  riastrad 	spin_lock(&dev->event_lock);
   1083  1.12  riastrad 	ret = drm_vblank_get_locked(dev, pipe);
   1084  1.12  riastrad 	spin_unlock(&dev->event_lock);
   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.12  riastrad int drm_crtc_vblank_get_locked(struct drm_crtc *crtc)
   1106  1.12  riastrad {
   1107  1.12  riastrad 	return drm_vblank_get_locked(crtc->dev, drm_crtc_index(crtc));
   1108  1.12  riastrad }
   1109  1.12  riastrad EXPORT_SYMBOL(drm_crtc_vblank_get_locked);
   1110  1.12  riastrad 
   1111   1.8  riastrad static void drm_vblank_put_locked(struct drm_device *dev, unsigned int pipe)
   1112   1.8  riastrad {
   1113   1.8  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1114   1.8  riastrad 
   1115  1.12  riastrad 	assert_spin_locked(&dev->event_lock);
   1116   1.8  riastrad 
   1117   1.8  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1118   1.8  riastrad 		return;
   1119   1.8  riastrad 
   1120   1.8  riastrad 	if (WARN_ON(atomic_read(&vblank->refcount) == 0))
   1121   1.8  riastrad 		return;
   1122   1.8  riastrad 
   1123   1.8  riastrad 	/* Last user schedules interrupt disable */
   1124   1.8  riastrad 	if (atomic_dec_and_test(&vblank->refcount)) {
   1125   1.8  riastrad 		if (drm_vblank_offdelay == 0)
   1126   1.8  riastrad 			return;
   1127   1.8  riastrad 		else if (drm_vblank_offdelay < 0)
   1128   1.8  riastrad 			vblank_disable_locked(vblank, dev, pipe);
   1129   1.8  riastrad 		else if (!dev->vblank_disable_immediate)
   1130   1.8  riastrad 			mod_timer(&vblank->disable_timer,
   1131   1.8  riastrad 				  jiffies + ((drm_vblank_offdelay * HZ)/1000));
   1132   1.8  riastrad 	}
   1133   1.8  riastrad }
   1134   1.8  riastrad 
   1135   1.1  riastrad static void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
   1136   1.1  riastrad {
   1137   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1138   1.1  riastrad 
   1139   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1140   1.1  riastrad 		return;
   1141   1.1  riastrad 
   1142   1.1  riastrad 	if (WARN_ON(atomic_read(&vblank->refcount) == 0))
   1143   1.1  riastrad 		return;
   1144   1.1  riastrad 
   1145   1.1  riastrad 	/* Last user schedules interrupt disable */
   1146   1.1  riastrad 	if (atomic_dec_and_test(&vblank->refcount)) {
   1147   1.1  riastrad 		if (drm_vblank_offdelay == 0)
   1148   1.1  riastrad 			return;
   1149   1.1  riastrad 		else if (drm_vblank_offdelay < 0)
   1150   1.1  riastrad 			vblank_disable_fn(&vblank->disable_timer);
   1151   1.1  riastrad 		else if (!dev->vblank_disable_immediate)
   1152   1.1  riastrad 			mod_timer(&vblank->disable_timer,
   1153   1.1  riastrad 				  jiffies + ((drm_vblank_offdelay * HZ)/1000));
   1154   1.1  riastrad 	}
   1155   1.1  riastrad }
   1156   1.1  riastrad 
   1157   1.1  riastrad /**
   1158   1.1  riastrad  * drm_crtc_vblank_put - give up ownership of vblank events
   1159   1.1  riastrad  * @crtc: which counter to give up
   1160   1.1  riastrad  *
   1161   1.1  riastrad  * Release ownership of a given vblank counter, turning off interrupts
   1162   1.1  riastrad  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
   1163   1.1  riastrad  */
   1164   1.1  riastrad void drm_crtc_vblank_put(struct drm_crtc *crtc)
   1165   1.1  riastrad {
   1166   1.1  riastrad 	drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
   1167   1.1  riastrad }
   1168   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_put);
   1169   1.1  riastrad 
   1170   1.8  riastrad void drm_crtc_vblank_put_locked(struct drm_crtc *crtc)
   1171   1.8  riastrad {
   1172   1.8  riastrad 	drm_vblank_put_locked(crtc->dev, drm_crtc_index(crtc));
   1173   1.8  riastrad }
   1174   1.8  riastrad 
   1175   1.1  riastrad /**
   1176   1.1  riastrad  * drm_wait_one_vblank - wait for one vblank
   1177   1.1  riastrad  * @dev: DRM device
   1178   1.1  riastrad  * @pipe: CRTC index
   1179   1.1  riastrad  *
   1180   1.1  riastrad  * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
   1181   1.1  riastrad  * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
   1182   1.1  riastrad  * due to lack of driver support or because the crtc is off.
   1183   1.1  riastrad  *
   1184   1.1  riastrad  * This is the legacy version of drm_crtc_wait_one_vblank().
   1185   1.1  riastrad  */
   1186   1.1  riastrad void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
   1187   1.1  riastrad {
   1188   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1189   1.1  riastrad 	int ret;
   1190   1.1  riastrad 	u64 last;
   1191   1.1  riastrad 
   1192   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1193   1.1  riastrad 		return;
   1194   1.1  riastrad 
   1195  1.13  riastrad 	spin_lock(&dev->event_lock);
   1196  1.13  riastrad 
   1197  1.13  riastrad 	ret = drm_vblank_get_locked(dev, pipe);
   1198   1.1  riastrad 	if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
   1199  1.13  riastrad 		goto out;
   1200   1.1  riastrad 
   1201   1.5  riastrad 	last = drm_vblank_count(dev, pipe);
   1202  1.12  riastrad 	DRM_SPIN_TIMED_WAIT_UNTIL(ret, &vblank->queue, &dev->event_lock,
   1203   1.5  riastrad 	    msecs_to_jiffies(100),
   1204   1.5  riastrad 	    last != drm_vblank_count(dev, pipe));
   1205   1.1  riastrad 
   1206   1.1  riastrad 	WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
   1207   1.1  riastrad 
   1208  1.13  riastrad 	drm_vblank_put_locked(dev, pipe);
   1209  1.13  riastrad out:	spin_unlock(&dev->event_lock);
   1210   1.1  riastrad }
   1211   1.1  riastrad EXPORT_SYMBOL(drm_wait_one_vblank);
   1212   1.1  riastrad 
   1213   1.1  riastrad /**
   1214   1.1  riastrad  * drm_crtc_wait_one_vblank - wait for one vblank
   1215   1.1  riastrad  * @crtc: DRM crtc
   1216   1.1  riastrad  *
   1217   1.1  riastrad  * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
   1218   1.1  riastrad  * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
   1219   1.1  riastrad  * due to lack of driver support or because the crtc is off.
   1220   1.1  riastrad  */
   1221   1.1  riastrad void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
   1222   1.1  riastrad {
   1223   1.1  riastrad 	drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
   1224   1.1  riastrad }
   1225   1.1  riastrad EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
   1226   1.1  riastrad 
   1227   1.1  riastrad /**
   1228   1.1  riastrad  * drm_crtc_vblank_off - disable vblank events on a CRTC
   1229   1.1  riastrad  * @crtc: CRTC in question
   1230   1.1  riastrad  *
   1231   1.1  riastrad  * Drivers can use this function to shut down the vblank interrupt handling when
   1232   1.1  riastrad  * disabling a crtc. This function ensures that the latest vblank frame count is
   1233   1.1  riastrad  * stored so that drm_vblank_on can restore it again.
   1234   1.1  riastrad  *
   1235   1.1  riastrad  * Drivers must use this function when the hardware vblank counter can get
   1236   1.1  riastrad  * reset, e.g. when suspending or disabling the @crtc in general.
   1237   1.1  riastrad  */
   1238   1.1  riastrad void drm_crtc_vblank_off(struct drm_crtc *crtc)
   1239   1.1  riastrad {
   1240   1.1  riastrad 	struct drm_device *dev = crtc->dev;
   1241   1.1  riastrad 	unsigned int pipe = drm_crtc_index(crtc);
   1242   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1243   1.1  riastrad 	struct drm_pending_vblank_event *e, *t;
   1244   1.1  riastrad 
   1245   1.1  riastrad 	ktime_t now;
   1246   1.1  riastrad 	unsigned long irqflags;
   1247   1.1  riastrad 	u64 seq;
   1248   1.1  riastrad 
   1249   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1250   1.1  riastrad 		return;
   1251   1.1  riastrad 
   1252   1.1  riastrad 	spin_lock_irqsave(&dev->event_lock, irqflags);
   1253   1.1  riastrad 
   1254   1.1  riastrad 	DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
   1255   1.1  riastrad 		      pipe, vblank->enabled, vblank->inmodeset);
   1256   1.1  riastrad 
   1257   1.1  riastrad 	/* Avoid redundant vblank disables without previous
   1258   1.1  riastrad 	 * drm_crtc_vblank_on(). */
   1259   1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
   1260   1.1  riastrad 		drm_vblank_disable_and_save(dev, pipe);
   1261   1.1  riastrad 
   1262  1.12  riastrad 	DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->event_lock);
   1263   1.1  riastrad 
   1264   1.1  riastrad 	/*
   1265   1.1  riastrad 	 * Prevent subsequent drm_vblank_get() from re-enabling
   1266   1.1  riastrad 	 * the vblank interrupt by bumping the refcount.
   1267   1.1  riastrad 	 */
   1268   1.1  riastrad 	if (!vblank->inmodeset) {
   1269   1.1  riastrad 		atomic_inc(&vblank->refcount);
   1270   1.1  riastrad 		vblank->inmodeset = 1;
   1271   1.1  riastrad 	}
   1272   1.1  riastrad 
   1273   1.1  riastrad 	/* Send any queued vblank events, lest the natives grow disquiet */
   1274   1.1  riastrad 	seq = drm_vblank_count_and_time(dev, pipe, &now);
   1275   1.1  riastrad 
   1276   1.1  riastrad 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
   1277   1.1  riastrad 		if (e->pipe != pipe)
   1278   1.1  riastrad 			continue;
   1279   1.1  riastrad 		DRM_DEBUG("Sending premature vblank event on disable: "
   1280   1.3  riastrad 			  "wanted %"PRIu64", current %"PRIu64"\n",
   1281   1.1  riastrad 			  e->sequence, seq);
   1282   1.1  riastrad 		list_del(&e->base.link);
   1283   1.1  riastrad 		drm_vblank_put(dev, pipe);
   1284   1.1  riastrad 		send_vblank_event(dev, e, seq, now);
   1285   1.1  riastrad 	}
   1286   1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
   1287   1.1  riastrad 
   1288   1.1  riastrad 	/* Will be reset by the modeset helpers when re-enabling the crtc by
   1289   1.1  riastrad 	 * calling drm_calc_timestamping_constants(). */
   1290   1.1  riastrad 	vblank->hwmode.crtc_clock = 0;
   1291   1.1  riastrad }
   1292   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_off);
   1293   1.1  riastrad 
   1294   1.1  riastrad /**
   1295   1.1  riastrad  * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
   1296   1.1  riastrad  * @crtc: CRTC in question
   1297   1.1  riastrad  *
   1298   1.1  riastrad  * Drivers can use this function to reset the vblank state to off at load time.
   1299   1.1  riastrad  * Drivers should use this together with the drm_crtc_vblank_off() and
   1300   1.1  riastrad  * drm_crtc_vblank_on() functions. The difference compared to
   1301   1.1  riastrad  * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
   1302   1.1  riastrad  * and hence doesn't need to call any driver hooks.
   1303   1.1  riastrad  *
   1304   1.1  riastrad  * This is useful for recovering driver state e.g. on driver load, or on resume.
   1305   1.1  riastrad  */
   1306   1.1  riastrad void drm_crtc_vblank_reset(struct drm_crtc *crtc)
   1307   1.1  riastrad {
   1308   1.1  riastrad 	struct drm_device *dev = crtc->dev;
   1309   1.1  riastrad 	unsigned long irqflags;
   1310   1.1  riastrad 	unsigned int pipe = drm_crtc_index(crtc);
   1311   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1312   1.1  riastrad 
   1313  1.12  riastrad 	spin_lock_irqsave(&dev->event_lock, irqflags);
   1314   1.1  riastrad 	/*
   1315   1.1  riastrad 	 * Prevent subsequent drm_vblank_get() from enabling the vblank
   1316   1.1  riastrad 	 * interrupt by bumping the refcount.
   1317   1.1  riastrad 	 */
   1318   1.1  riastrad 	if (!vblank->inmodeset) {
   1319   1.1  riastrad 		atomic_inc(&vblank->refcount);
   1320   1.1  riastrad 		vblank->inmodeset = 1;
   1321   1.1  riastrad 	}
   1322   1.1  riastrad 	WARN_ON(!list_empty(&dev->vblank_event_list));
   1323  1.12  riastrad 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
   1324   1.1  riastrad }
   1325   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_reset);
   1326   1.1  riastrad 
   1327   1.1  riastrad /**
   1328   1.1  riastrad  * drm_crtc_set_max_vblank_count - configure the hw max vblank counter value
   1329   1.1  riastrad  * @crtc: CRTC in question
   1330   1.1  riastrad  * @max_vblank_count: max hardware vblank counter value
   1331   1.1  riastrad  *
   1332   1.1  riastrad  * Update the maximum hardware vblank counter value for @crtc
   1333   1.1  riastrad  * at runtime. Useful for hardware where the operation of the
   1334   1.1  riastrad  * hardware vblank counter depends on the currently active
   1335   1.1  riastrad  * display configuration.
   1336   1.1  riastrad  *
   1337   1.1  riastrad  * For example, if the hardware vblank counter does not work
   1338   1.1  riastrad  * when a specific connector is active the maximum can be set
   1339   1.1  riastrad  * to zero. And when that specific connector isn't active the
   1340   1.1  riastrad  * maximum can again be set to the appropriate non-zero value.
   1341   1.1  riastrad  *
   1342   1.1  riastrad  * If used, must be called before drm_vblank_on().
   1343   1.1  riastrad  */
   1344   1.1  riastrad void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc,
   1345   1.1  riastrad 				   u32 max_vblank_count)
   1346   1.1  riastrad {
   1347   1.1  riastrad 	struct drm_device *dev = crtc->dev;
   1348   1.1  riastrad 	unsigned int pipe = drm_crtc_index(crtc);
   1349   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1350   1.1  riastrad 
   1351   1.1  riastrad 	WARN_ON(dev->max_vblank_count);
   1352   1.1  riastrad 	WARN_ON(!READ_ONCE(vblank->inmodeset));
   1353   1.1  riastrad 
   1354   1.1  riastrad 	vblank->max_vblank_count = max_vblank_count;
   1355   1.1  riastrad }
   1356   1.1  riastrad EXPORT_SYMBOL(drm_crtc_set_max_vblank_count);
   1357   1.1  riastrad 
   1358   1.1  riastrad /**
   1359   1.1  riastrad  * drm_crtc_vblank_on - enable vblank events on a CRTC
   1360   1.1  riastrad  * @crtc: CRTC in question
   1361   1.1  riastrad  *
   1362   1.1  riastrad  * This functions restores the vblank interrupt state captured with
   1363   1.1  riastrad  * drm_crtc_vblank_off() again and is generally called when enabling @crtc. Note
   1364   1.1  riastrad  * that calls to drm_crtc_vblank_on() and drm_crtc_vblank_off() can be
   1365   1.1  riastrad  * unbalanced and so can also be unconditionally called in driver load code to
   1366   1.1  riastrad  * reflect the current hardware state of the crtc.
   1367   1.1  riastrad  */
   1368   1.1  riastrad void drm_crtc_vblank_on(struct drm_crtc *crtc)
   1369   1.1  riastrad {
   1370   1.1  riastrad 	struct drm_device *dev = crtc->dev;
   1371   1.1  riastrad 	unsigned int pipe = drm_crtc_index(crtc);
   1372   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1373   1.1  riastrad 	unsigned long irqflags;
   1374   1.1  riastrad 
   1375   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1376   1.1  riastrad 		return;
   1377   1.1  riastrad 
   1378  1.12  riastrad 	spin_lock_irqsave(&dev->event_lock, irqflags);
   1379   1.1  riastrad 	DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
   1380   1.1  riastrad 		      pipe, vblank->enabled, vblank->inmodeset);
   1381   1.1  riastrad 
   1382   1.1  riastrad 	/* Drop our private "prevent drm_vblank_get" refcount */
   1383   1.1  riastrad 	if (vblank->inmodeset) {
   1384   1.1  riastrad 		atomic_dec(&vblank->refcount);
   1385   1.1  riastrad 		vblank->inmodeset = 0;
   1386   1.1  riastrad 	}
   1387   1.1  riastrad 
   1388   1.1  riastrad 	drm_reset_vblank_timestamp(dev, pipe);
   1389   1.1  riastrad 
   1390   1.1  riastrad 	/*
   1391   1.1  riastrad 	 * re-enable interrupts if there are users left, or the
   1392   1.1  riastrad 	 * user wishes vblank interrupts to be enabled all the time.
   1393   1.1  riastrad 	 */
   1394   1.1  riastrad 	if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
   1395   1.1  riastrad 		WARN_ON(drm_vblank_enable(dev, pipe));
   1396  1.12  riastrad 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
   1397   1.1  riastrad }
   1398   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_on);
   1399   1.1  riastrad 
   1400   1.1  riastrad /**
   1401   1.1  riastrad  * drm_vblank_restore - estimate missed vblanks and update vblank count.
   1402   1.1  riastrad  * @dev: DRM device
   1403   1.1  riastrad  * @pipe: CRTC index
   1404   1.1  riastrad  *
   1405   1.1  riastrad  * Power manamement features can cause frame counter resets between vblank
   1406   1.1  riastrad  * disable and enable. Drivers can use this function in their
   1407   1.1  riastrad  * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
   1408   1.1  riastrad  * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
   1409   1.1  riastrad  * vblank counter.
   1410   1.1  riastrad  *
   1411   1.1  riastrad  * This function is the legacy version of drm_crtc_vblank_restore().
   1412   1.1  riastrad  */
   1413   1.1  riastrad void drm_vblank_restore(struct drm_device *dev, unsigned int pipe)
   1414   1.1  riastrad {
   1415   1.1  riastrad 	ktime_t t_vblank;
   1416   1.1  riastrad 	struct drm_vblank_crtc *vblank;
   1417   1.1  riastrad 	int framedur_ns;
   1418   1.1  riastrad 	u64 diff_ns;
   1419   1.1  riastrad 	u32 cur_vblank, diff = 1;
   1420   1.1  riastrad 	int count = DRM_TIMESTAMP_MAXRETRIES;
   1421   1.1  riastrad 
   1422   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1423   1.1  riastrad 		return;
   1424   1.1  riastrad 
   1425  1.12  riastrad 	assert_spin_locked(&dev->event_lock);
   1426   1.1  riastrad 	assert_spin_locked(&dev->vblank_time_lock);
   1427   1.1  riastrad 
   1428   1.1  riastrad 	vblank = &dev->vblank[pipe];
   1429   1.1  riastrad 	WARN_ONCE(drm_debug_enabled(DRM_UT_VBL) && !vblank->framedur_ns,
   1430   1.1  riastrad 		  "Cannot compute missed vblanks without frame duration\n");
   1431   1.1  riastrad 	framedur_ns = vblank->framedur_ns;
   1432   1.1  riastrad 
   1433   1.1  riastrad 	do {
   1434   1.1  riastrad 		cur_vblank = __get_vblank_counter(dev, pipe);
   1435   1.1  riastrad 		drm_get_last_vbltimestamp(dev, pipe, &t_vblank, false);
   1436   1.1  riastrad 	} while (cur_vblank != __get_vblank_counter(dev, pipe) && --count > 0);
   1437   1.1  riastrad 
   1438   1.1  riastrad 	diff_ns = ktime_to_ns(ktime_sub(t_vblank, vblank->time));
   1439   1.1  riastrad 	if (framedur_ns)
   1440   1.1  riastrad 		diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
   1441   1.1  riastrad 
   1442   1.1  riastrad 
   1443   1.3  riastrad 	DRM_DEBUG_VBL("missed %d vblanks in %"PRId64" ns, frame duration=%d ns, hw_diff=%d\n",
   1444   1.1  riastrad 		      diff, diff_ns, framedur_ns, cur_vblank - vblank->last);
   1445   1.1  riastrad 	store_vblank(dev, pipe, diff, t_vblank, cur_vblank);
   1446   1.1  riastrad }
   1447   1.1  riastrad EXPORT_SYMBOL(drm_vblank_restore);
   1448   1.1  riastrad 
   1449   1.1  riastrad /**
   1450   1.1  riastrad  * drm_crtc_vblank_restore - estimate missed vblanks and update vblank count.
   1451   1.1  riastrad  * @crtc: CRTC in question
   1452   1.1  riastrad  *
   1453   1.1  riastrad  * Power manamement features can cause frame counter resets between vblank
   1454   1.1  riastrad  * disable and enable. Drivers can use this function in their
   1455   1.1  riastrad  * &drm_crtc_funcs.enable_vblank implementation to estimate missed vblanks since
   1456   1.1  riastrad  * the last &drm_crtc_funcs.disable_vblank using timestamps and update the
   1457   1.1  riastrad  * vblank counter.
   1458   1.1  riastrad  */
   1459   1.1  riastrad void drm_crtc_vblank_restore(struct drm_crtc *crtc)
   1460   1.1  riastrad {
   1461   1.1  riastrad 	drm_vblank_restore(crtc->dev, drm_crtc_index(crtc));
   1462   1.1  riastrad }
   1463   1.1  riastrad EXPORT_SYMBOL(drm_crtc_vblank_restore);
   1464   1.1  riastrad 
   1465   1.1  riastrad static void drm_legacy_vblank_pre_modeset(struct drm_device *dev,
   1466   1.1  riastrad 					  unsigned int pipe)
   1467   1.1  riastrad {
   1468   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1469   1.1  riastrad 
   1470   1.1  riastrad 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
   1471   1.1  riastrad 	if (!dev->num_crtcs)
   1472   1.1  riastrad 		return;
   1473   1.1  riastrad 
   1474   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1475   1.1  riastrad 		return;
   1476   1.1  riastrad 
   1477   1.1  riastrad 	/*
   1478   1.1  riastrad 	 * To avoid all the problems that might happen if interrupts
   1479   1.1  riastrad 	 * were enabled/disabled around or between these calls, we just
   1480   1.1  riastrad 	 * have the kernel take a reference on the CRTC (just once though
   1481   1.1  riastrad 	 * to avoid corrupting the count if multiple, mismatch calls occur),
   1482   1.1  riastrad 	 * so that interrupts remain enabled in the interim.
   1483   1.1  riastrad 	 */
   1484   1.1  riastrad 	if (!vblank->inmodeset) {
   1485   1.1  riastrad 		vblank->inmodeset = 0x1;
   1486   1.1  riastrad 		if (drm_vblank_get(dev, pipe) == 0)
   1487   1.1  riastrad 			vblank->inmodeset |= 0x2;
   1488   1.1  riastrad 	}
   1489   1.1  riastrad }
   1490   1.1  riastrad 
   1491   1.1  riastrad static void drm_legacy_vblank_post_modeset(struct drm_device *dev,
   1492   1.1  riastrad 					   unsigned int pipe)
   1493   1.1  riastrad {
   1494   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1495   1.1  riastrad 	unsigned long irqflags;
   1496   1.1  riastrad 
   1497   1.1  riastrad 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
   1498   1.1  riastrad 	if (!dev->num_crtcs)
   1499   1.1  riastrad 		return;
   1500   1.1  riastrad 
   1501   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1502   1.1  riastrad 		return;
   1503   1.1  riastrad 
   1504   1.1  riastrad 	if (vblank->inmodeset) {
   1505  1.12  riastrad 		spin_lock_irqsave(&dev->event_lock, irqflags);
   1506   1.1  riastrad 		drm_reset_vblank_timestamp(dev, pipe);
   1507  1.12  riastrad 		spin_unlock_irqrestore(&dev->event_lock, irqflags);
   1508   1.1  riastrad 
   1509   1.1  riastrad 		if (vblank->inmodeset & 0x2)
   1510   1.1  riastrad 			drm_vblank_put(dev, pipe);
   1511   1.1  riastrad 
   1512   1.1  riastrad 		vblank->inmodeset = 0;
   1513   1.1  riastrad 	}
   1514   1.1  riastrad }
   1515   1.1  riastrad 
   1516   1.1  riastrad int drm_legacy_modeset_ctl_ioctl(struct drm_device *dev, void *data,
   1517   1.1  riastrad 				 struct drm_file *file_priv)
   1518   1.1  riastrad {
   1519   1.1  riastrad 	struct drm_modeset_ctl *modeset = data;
   1520   1.1  riastrad 	unsigned int pipe;
   1521   1.1  riastrad 
   1522   1.1  riastrad 	/* If drm_vblank_init() hasn't been called yet, just no-op */
   1523   1.1  riastrad 	if (!dev->num_crtcs)
   1524   1.1  riastrad 		return 0;
   1525   1.1  riastrad 
   1526   1.1  riastrad 	/* KMS drivers handle this internally */
   1527   1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_LEGACY))
   1528   1.1  riastrad 		return 0;
   1529   1.1  riastrad 
   1530   1.1  riastrad 	pipe = modeset->crtc;
   1531   1.1  riastrad 	if (pipe >= dev->num_crtcs)
   1532   1.1  riastrad 		return -EINVAL;
   1533   1.1  riastrad 
   1534   1.1  riastrad 	switch (modeset->cmd) {
   1535   1.1  riastrad 	case _DRM_PRE_MODESET:
   1536   1.1  riastrad 		drm_legacy_vblank_pre_modeset(dev, pipe);
   1537   1.1  riastrad 		break;
   1538   1.1  riastrad 	case _DRM_POST_MODESET:
   1539   1.1  riastrad 		drm_legacy_vblank_post_modeset(dev, pipe);
   1540   1.1  riastrad 		break;
   1541   1.1  riastrad 	default:
   1542   1.1  riastrad 		return -EINVAL;
   1543   1.1  riastrad 	}
   1544   1.1  riastrad 
   1545   1.1  riastrad 	return 0;
   1546   1.1  riastrad }
   1547   1.1  riastrad 
   1548   1.1  riastrad static inline bool vblank_passed(u64 seq, u64 ref)
   1549   1.1  riastrad {
   1550   1.1  riastrad 	return (seq - ref) <= (1 << 23);
   1551   1.1  riastrad }
   1552   1.1  riastrad 
   1553   1.1  riastrad static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
   1554   1.1  riastrad 				  u64 req_seq,
   1555   1.1  riastrad 				  union drm_wait_vblank *vblwait,
   1556   1.1  riastrad 				  struct drm_file *file_priv)
   1557   1.1  riastrad {
   1558   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1559   1.1  riastrad 	struct drm_pending_vblank_event *e;
   1560   1.1  riastrad 	ktime_t now;
   1561   1.1  riastrad 	unsigned long flags;
   1562   1.1  riastrad 	u64 seq;
   1563   1.1  riastrad 	int ret;
   1564   1.1  riastrad 
   1565   1.1  riastrad 	e = kzalloc(sizeof(*e), GFP_KERNEL);
   1566   1.1  riastrad 	if (e == NULL) {
   1567   1.1  riastrad 		ret = -ENOMEM;
   1568   1.1  riastrad 		goto err_put;
   1569   1.1  riastrad 	}
   1570   1.1  riastrad 
   1571   1.1  riastrad 	e->pipe = pipe;
   1572   1.1  riastrad 	e->event.base.type = DRM_EVENT_VBLANK;
   1573   1.1  riastrad 	e->event.base.length = sizeof(e->event.vbl);
   1574   1.1  riastrad 	e->event.vbl.user_data = vblwait->request.signal;
   1575   1.1  riastrad 	e->event.vbl.crtc_id = 0;
   1576   1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
   1577   1.1  riastrad 		struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
   1578   1.1  riastrad 		if (crtc)
   1579   1.1  riastrad 			e->event.vbl.crtc_id = crtc->base.id;
   1580   1.1  riastrad 	}
   1581   1.1  riastrad 
   1582   1.1  riastrad 	spin_lock_irqsave(&dev->event_lock, flags);
   1583   1.1  riastrad 
   1584   1.1  riastrad 	/*
   1585   1.1  riastrad 	 * drm_crtc_vblank_off() might have been called after we called
   1586   1.1  riastrad 	 * drm_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
   1587   1.1  riastrad 	 * vblank disable, so no need for further locking.  The reference from
   1588   1.1  riastrad 	 * drm_vblank_get() protects against vblank disable from another source.
   1589   1.1  riastrad 	 */
   1590   1.1  riastrad 	if (!READ_ONCE(vblank->enabled)) {
   1591   1.1  riastrad 		ret = -EINVAL;
   1592   1.1  riastrad 		goto err_unlock;
   1593   1.1  riastrad 	}
   1594   1.1  riastrad 
   1595   1.1  riastrad 	ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
   1596   1.1  riastrad 					    &e->event.base);
   1597   1.1  riastrad 
   1598   1.1  riastrad 	if (ret)
   1599   1.1  riastrad 		goto err_unlock;
   1600   1.1  riastrad 
   1601   1.1  riastrad 	seq = drm_vblank_count_and_time(dev, pipe, &now);
   1602   1.1  riastrad 
   1603   1.3  riastrad 	DRM_DEBUG("event on vblank count %"PRIu64", current %"PRIu64", crtc %u\n",
   1604   1.1  riastrad 		  req_seq, seq, pipe);
   1605   1.1  riastrad 
   1606   1.1  riastrad 	trace_drm_vblank_event_queued(file_priv, pipe, req_seq);
   1607   1.1  riastrad 
   1608   1.1  riastrad 	e->sequence = req_seq;
   1609   1.1  riastrad 	if (vblank_passed(seq, req_seq)) {
   1610   1.1  riastrad 		drm_vblank_put(dev, pipe);
   1611   1.1  riastrad 		send_vblank_event(dev, e, seq, now);
   1612   1.1  riastrad 		vblwait->reply.sequence = seq;
   1613   1.1  riastrad 	} else {
   1614   1.1  riastrad 		/* drm_handle_vblank_events will call drm_vblank_put */
   1615   1.1  riastrad 		list_add_tail(&e->base.link, &dev->vblank_event_list);
   1616   1.1  riastrad 		vblwait->reply.sequence = req_seq;
   1617   1.1  riastrad 	}
   1618   1.1  riastrad 
   1619   1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, flags);
   1620   1.1  riastrad 
   1621   1.1  riastrad 	return 0;
   1622   1.1  riastrad 
   1623   1.1  riastrad err_unlock:
   1624   1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, flags);
   1625   1.1  riastrad 	kfree(e);
   1626   1.1  riastrad err_put:
   1627   1.1  riastrad 	drm_vblank_put(dev, pipe);
   1628   1.1  riastrad 	return ret;
   1629   1.1  riastrad }
   1630   1.1  riastrad 
   1631   1.1  riastrad static bool drm_wait_vblank_is_query(union drm_wait_vblank *vblwait)
   1632   1.1  riastrad {
   1633   1.1  riastrad 	if (vblwait->request.sequence)
   1634   1.1  riastrad 		return false;
   1635   1.1  riastrad 
   1636   1.1  riastrad 	return _DRM_VBLANK_RELATIVE ==
   1637   1.1  riastrad 		(vblwait->request.type & (_DRM_VBLANK_TYPES_MASK |
   1638   1.1  riastrad 					  _DRM_VBLANK_EVENT |
   1639   1.1  riastrad 					  _DRM_VBLANK_NEXTONMISS));
   1640   1.1  riastrad }
   1641   1.1  riastrad 
   1642   1.1  riastrad /*
   1643   1.1  riastrad  * Widen a 32-bit param to 64-bits.
   1644   1.1  riastrad  *
   1645   1.1  riastrad  * \param narrow 32-bit value (missing upper 32 bits)
   1646   1.1  riastrad  * \param near 64-bit value that should be 'close' to near
   1647   1.1  riastrad  *
   1648   1.1  riastrad  * This function returns a 64-bit value using the lower 32-bits from
   1649   1.1  riastrad  * 'narrow' and constructing the upper 32-bits so that the result is
   1650   1.1  riastrad  * as close as possible to 'near'.
   1651   1.1  riastrad  */
   1652   1.1  riastrad 
   1653   1.1  riastrad static u64 widen_32_to_64(u32 narrow, u64 near)
   1654   1.1  riastrad {
   1655   1.1  riastrad 	return near + (s32) (narrow - near);
   1656   1.1  riastrad }
   1657   1.1  riastrad 
   1658   1.1  riastrad static void drm_wait_vblank_reply(struct drm_device *dev, unsigned int pipe,
   1659   1.1  riastrad 				  struct drm_wait_vblank_reply *reply)
   1660   1.1  riastrad {
   1661   1.1  riastrad 	ktime_t now;
   1662   1.1  riastrad 	struct timespec64 ts;
   1663   1.1  riastrad 
   1664   1.1  riastrad 	/*
   1665   1.1  riastrad 	 * drm_wait_vblank_reply is a UAPI structure that uses 'long'
   1666   1.1  riastrad 	 * to store the seconds. This is safe as we always use monotonic
   1667   1.1  riastrad 	 * timestamps since linux-4.15.
   1668   1.1  riastrad 	 */
   1669   1.1  riastrad 	reply->sequence = drm_vblank_count_and_time(dev, pipe, &now);
   1670   1.1  riastrad 	ts = ktime_to_timespec64(now);
   1671   1.1  riastrad 	reply->tval_sec = (u32)ts.tv_sec;
   1672   1.1  riastrad 	reply->tval_usec = ts.tv_nsec / 1000;
   1673   1.1  riastrad }
   1674   1.1  riastrad 
   1675   1.1  riastrad int drm_wait_vblank_ioctl(struct drm_device *dev, void *data,
   1676   1.1  riastrad 			  struct drm_file *file_priv)
   1677   1.1  riastrad {
   1678   1.1  riastrad 	struct drm_crtc *crtc;
   1679   1.1  riastrad 	struct drm_vblank_crtc *vblank;
   1680   1.1  riastrad 	union drm_wait_vblank *vblwait = data;
   1681   1.1  riastrad 	int ret;
   1682   1.1  riastrad 	u64 req_seq, seq;
   1683   1.1  riastrad 	unsigned int pipe_index;
   1684   1.1  riastrad 	unsigned int flags, pipe, high_pipe;
   1685   1.1  riastrad 
   1686   1.1  riastrad 	if (!dev->irq_enabled)
   1687   1.1  riastrad 		return -EOPNOTSUPP;
   1688   1.1  riastrad 
   1689   1.1  riastrad 	if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
   1690   1.1  riastrad 		return -EINVAL;
   1691   1.1  riastrad 
   1692   1.1  riastrad 	if (vblwait->request.type &
   1693   1.1  riastrad 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
   1694   1.1  riastrad 	      _DRM_VBLANK_HIGH_CRTC_MASK)) {
   1695   1.1  riastrad 		DRM_DEBUG("Unsupported type value 0x%x, supported mask 0x%x\n",
   1696   1.1  riastrad 			  vblwait->request.type,
   1697   1.1  riastrad 			  (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
   1698   1.1  riastrad 			   _DRM_VBLANK_HIGH_CRTC_MASK));
   1699   1.1  riastrad 		return -EINVAL;
   1700   1.1  riastrad 	}
   1701   1.1  riastrad 
   1702   1.1  riastrad 	flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
   1703   1.1  riastrad 	high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
   1704   1.1  riastrad 	if (high_pipe)
   1705   1.1  riastrad 		pipe_index = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
   1706   1.1  riastrad 	else
   1707   1.1  riastrad 		pipe_index = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
   1708   1.1  riastrad 
   1709   1.1  riastrad 	/* Convert lease-relative crtc index into global crtc index */
   1710   1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET)) {
   1711   1.1  riastrad 		pipe = 0;
   1712   1.1  riastrad 		drm_for_each_crtc(crtc, dev) {
   1713   1.1  riastrad 			if (drm_lease_held(file_priv, crtc->base.id)) {
   1714   1.1  riastrad 				if (pipe_index == 0)
   1715   1.1  riastrad 					break;
   1716   1.1  riastrad 				pipe_index--;
   1717   1.1  riastrad 			}
   1718   1.1  riastrad 			pipe++;
   1719   1.1  riastrad 		}
   1720   1.1  riastrad 	} else {
   1721   1.1  riastrad 		pipe = pipe_index;
   1722   1.1  riastrad 	}
   1723   1.1  riastrad 
   1724   1.1  riastrad 	if (pipe >= dev->num_crtcs)
   1725   1.1  riastrad 		return -EINVAL;
   1726   1.1  riastrad 
   1727   1.1  riastrad 	vblank = &dev->vblank[pipe];
   1728   1.1  riastrad 
   1729   1.1  riastrad 	/* If the counter is currently enabled and accurate, short-circuit
   1730   1.1  riastrad 	 * queries to return the cached timestamp of the last vblank.
   1731   1.1  riastrad 	 */
   1732   1.1  riastrad 	if (dev->vblank_disable_immediate &&
   1733   1.1  riastrad 	    drm_wait_vblank_is_query(vblwait) &&
   1734   1.1  riastrad 	    READ_ONCE(vblank->enabled)) {
   1735   1.1  riastrad 		drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
   1736   1.1  riastrad 		return 0;
   1737   1.1  riastrad 	}
   1738   1.1  riastrad 
   1739   1.1  riastrad 	ret = drm_vblank_get(dev, pipe);
   1740   1.1  riastrad 	if (ret) {
   1741   1.1  riastrad 		DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
   1742   1.1  riastrad 		return ret;
   1743   1.1  riastrad 	}
   1744   1.1  riastrad 	seq = drm_vblank_count(dev, pipe);
   1745   1.1  riastrad 
   1746   1.1  riastrad 	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
   1747   1.1  riastrad 	case _DRM_VBLANK_RELATIVE:
   1748   1.1  riastrad 		req_seq = seq + vblwait->request.sequence;
   1749   1.1  riastrad 		vblwait->request.sequence = req_seq;
   1750   1.1  riastrad 		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
   1751   1.1  riastrad 		break;
   1752   1.1  riastrad 	case _DRM_VBLANK_ABSOLUTE:
   1753   1.1  riastrad 		req_seq = widen_32_to_64(vblwait->request.sequence, seq);
   1754   1.1  riastrad 		break;
   1755   1.1  riastrad 	default:
   1756   1.1  riastrad 		ret = -EINVAL;
   1757   1.1  riastrad 		goto done;
   1758   1.1  riastrad 	}
   1759   1.1  riastrad 
   1760   1.1  riastrad 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
   1761   1.1  riastrad 	    vblank_passed(seq, req_seq)) {
   1762   1.1  riastrad 		req_seq = seq + 1;
   1763   1.1  riastrad 		vblwait->request.type &= ~_DRM_VBLANK_NEXTONMISS;
   1764   1.1  riastrad 		vblwait->request.sequence = req_seq;
   1765   1.1  riastrad 	}
   1766   1.1  riastrad 
   1767   1.1  riastrad 	if (flags & _DRM_VBLANK_EVENT) {
   1768   1.1  riastrad 		/* must hold on to the vblank ref until the event fires
   1769   1.1  riastrad 		 * drm_vblank_put will be called asynchronously
   1770   1.1  riastrad 		 */
   1771   1.1  riastrad 		return drm_queue_vblank_event(dev, pipe, req_seq, vblwait, file_priv);
   1772   1.1  riastrad 	}
   1773   1.1  riastrad 
   1774   1.1  riastrad 	if (req_seq != seq) {
   1775   1.1  riastrad 		int wait;
   1776   1.1  riastrad 
   1777   1.3  riastrad 		DRM_DEBUG("waiting on vblank count %"PRIu64", crtc %u\n",
   1778   1.1  riastrad 			  req_seq, pipe);
   1779  1.15  riastrad 		spin_lock(&dev->event_lock);
   1780   1.5  riastrad 		DRM_SPIN_TIMED_WAIT_UNTIL(wait, &vblank->queue,
   1781  1.12  riastrad 		    &dev->event_lock, msecs_to_jiffies(3000),
   1782  1.10  riastrad 		    (vblank_passed(drm_vblank_count(dev, pipe), req_seq) ||
   1783  1.10  riastrad 			!READ_ONCE(vblank->enabled)));
   1784  1.15  riastrad 		spin_unlock(&dev->event_lock);
   1785   1.1  riastrad 
   1786   1.1  riastrad 		switch (wait) {
   1787   1.1  riastrad 		case 0:
   1788   1.1  riastrad 			/* timeout */
   1789   1.1  riastrad 			ret = -EBUSY;
   1790   1.1  riastrad 			break;
   1791   1.1  riastrad 		case -ERESTARTSYS:
   1792   1.1  riastrad 			/* interrupted by signal */
   1793   1.1  riastrad 			ret = -EINTR;
   1794   1.1  riastrad 			break;
   1795   1.1  riastrad 		default:
   1796   1.1  riastrad 			ret = 0;
   1797   1.1  riastrad 			break;
   1798   1.1  riastrad 		}
   1799   1.1  riastrad 	}
   1800   1.1  riastrad 
   1801   1.1  riastrad 	if (ret != -EINTR) {
   1802   1.1  riastrad 		drm_wait_vblank_reply(dev, pipe, &vblwait->reply);
   1803   1.1  riastrad 
   1804   1.1  riastrad 		DRM_DEBUG("crtc %d returning %u to client\n",
   1805   1.1  riastrad 			  pipe, vblwait->reply.sequence);
   1806   1.1  riastrad 	} else {
   1807   1.1  riastrad 		DRM_DEBUG("crtc %d vblank wait interrupted by signal\n", pipe);
   1808   1.1  riastrad 	}
   1809   1.1  riastrad 
   1810   1.1  riastrad done:
   1811   1.1  riastrad 	drm_vblank_put(dev, pipe);
   1812   1.1  riastrad 	return ret;
   1813   1.1  riastrad }
   1814   1.1  riastrad 
   1815   1.1  riastrad static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
   1816   1.1  riastrad {
   1817   1.1  riastrad 	struct drm_pending_vblank_event *e, *t;
   1818   1.1  riastrad 	ktime_t now;
   1819   1.1  riastrad 	u64 seq;
   1820   1.1  riastrad 
   1821   1.1  riastrad 	assert_spin_locked(&dev->event_lock);
   1822   1.1  riastrad 
   1823   1.1  riastrad 	seq = drm_vblank_count_and_time(dev, pipe, &now);
   1824   1.1  riastrad 
   1825   1.1  riastrad 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
   1826   1.1  riastrad 		if (e->pipe != pipe)
   1827   1.1  riastrad 			continue;
   1828   1.1  riastrad 		if (!vblank_passed(seq, e->sequence))
   1829   1.1  riastrad 			continue;
   1830   1.1  riastrad 
   1831   1.3  riastrad 		DRM_DEBUG("vblank event on %"PRIu64", current %"PRIu64"\n",
   1832   1.1  riastrad 			  e->sequence, seq);
   1833   1.1  riastrad 
   1834   1.1  riastrad 		list_del(&e->base.link);
   1835   1.1  riastrad 		drm_vblank_put(dev, pipe);
   1836   1.1  riastrad 		send_vblank_event(dev, e, seq, now);
   1837   1.1  riastrad 	}
   1838   1.1  riastrad 
   1839   1.1  riastrad 	trace_drm_vblank_event(pipe, seq, now,
   1840   1.1  riastrad 			dev->driver->get_vblank_timestamp != NULL);
   1841   1.1  riastrad }
   1842   1.1  riastrad 
   1843   1.1  riastrad /**
   1844   1.1  riastrad  * drm_handle_vblank - handle a vblank event
   1845   1.1  riastrad  * @dev: DRM device
   1846   1.1  riastrad  * @pipe: index of CRTC where this event occurred
   1847   1.1  riastrad  *
   1848   1.1  riastrad  * Drivers should call this routine in their vblank interrupt handlers to
   1849   1.1  riastrad  * update the vblank counter and send any signals that may be pending.
   1850   1.1  riastrad  *
   1851   1.1  riastrad  * This is the legacy version of drm_crtc_handle_vblank().
   1852   1.1  riastrad  */
   1853   1.1  riastrad bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
   1854   1.1  riastrad {
   1855   1.1  riastrad 	struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
   1856   1.1  riastrad 	unsigned long irqflags;
   1857   1.1  riastrad 	bool disable_irq;
   1858   1.1  riastrad 
   1859   1.1  riastrad 	if (WARN_ON_ONCE(!dev->num_crtcs))
   1860   1.1  riastrad 		return false;
   1861   1.1  riastrad 
   1862   1.1  riastrad 	if (WARN_ON(pipe >= dev->num_crtcs))
   1863   1.1  riastrad 		return false;
   1864   1.1  riastrad 
   1865   1.1  riastrad 	spin_lock_irqsave(&dev->event_lock, irqflags);
   1866   1.1  riastrad 
   1867   1.1  riastrad 	/* Need timestamp lock to prevent concurrent execution with
   1868   1.1  riastrad 	 * vblank enable/disable, as this would cause inconsistent
   1869   1.1  riastrad 	 * or corrupted timestamps and vblank counts.
   1870   1.1  riastrad 	 */
   1871   1.1  riastrad 	spin_lock(&dev->vblank_time_lock);
   1872   1.1  riastrad 
   1873   1.1  riastrad 	/* Vblank irq handling disabled. Nothing to do. */
   1874   1.1  riastrad 	if (!vblank->enabled) {
   1875   1.1  riastrad 		spin_unlock(&dev->vblank_time_lock);
   1876   1.1  riastrad 		spin_unlock_irqrestore(&dev->event_lock, irqflags);
   1877   1.1  riastrad 		return false;
   1878   1.1  riastrad 	}
   1879   1.1  riastrad 
   1880   1.1  riastrad 	drm_update_vblank_count(dev, pipe, true);
   1881   1.1  riastrad 
   1882   1.1  riastrad 	spin_unlock(&dev->vblank_time_lock);
   1883   1.1  riastrad 
   1884  1.12  riastrad 	DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->event_lock);
   1885   1.1  riastrad 
   1886   1.1  riastrad 	/* With instant-off, we defer disabling the interrupt until after
   1887   1.1  riastrad 	 * we finish processing the following vblank after all events have
   1888   1.1  riastrad 	 * been signaled. The disable has to be last (after
   1889   1.1  riastrad 	 * drm_handle_vblank_events) so that the timestamp is always accurate.
   1890   1.1  riastrad 	 */
   1891   1.1  riastrad 	disable_irq = (dev->vblank_disable_immediate &&
   1892   1.1  riastrad 		       drm_vblank_offdelay > 0 &&
   1893   1.1  riastrad 		       !atomic_read(&vblank->refcount));
   1894   1.1  riastrad 
   1895   1.1  riastrad 	drm_handle_vblank_events(dev, pipe);
   1896   1.1  riastrad 
   1897   1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, irqflags);
   1898   1.1  riastrad 
   1899   1.1  riastrad 	if (disable_irq)
   1900   1.1  riastrad 		vblank_disable_fn(&vblank->disable_timer);
   1901   1.1  riastrad 
   1902   1.1  riastrad 	return true;
   1903   1.1  riastrad }
   1904   1.1  riastrad EXPORT_SYMBOL(drm_handle_vblank);
   1905   1.1  riastrad 
   1906   1.1  riastrad /**
   1907   1.1  riastrad  * drm_crtc_handle_vblank - handle a vblank event
   1908   1.1  riastrad  * @crtc: where this event occurred
   1909   1.1  riastrad  *
   1910   1.1  riastrad  * Drivers should call this routine in their vblank interrupt handlers to
   1911   1.1  riastrad  * update the vblank counter and send any signals that may be pending.
   1912   1.1  riastrad  *
   1913   1.1  riastrad  * This is the native KMS version of drm_handle_vblank().
   1914   1.1  riastrad  *
   1915   1.1  riastrad  * Note that for a given vblank counter value drm_crtc_handle_vblank()
   1916   1.1  riastrad  * and drm_crtc_vblank_count() or drm_crtc_vblank_count_and_time()
   1917   1.1  riastrad  * provide a barrier: Any writes done before calling
   1918   1.1  riastrad  * drm_crtc_handle_vblank() will be visible to callers of the later
   1919   1.1  riastrad  * functions, iff the vblank count is the same or a later one.
   1920   1.1  riastrad  *
   1921   1.1  riastrad  * See also &drm_vblank_crtc.count.
   1922   1.1  riastrad  *
   1923   1.1  riastrad  * Returns:
   1924   1.1  riastrad  * True if the event was successfully handled, false on failure.
   1925   1.1  riastrad  */
   1926   1.1  riastrad bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
   1927   1.1  riastrad {
   1928   1.1  riastrad 	return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
   1929   1.1  riastrad }
   1930   1.1  riastrad EXPORT_SYMBOL(drm_crtc_handle_vblank);
   1931   1.1  riastrad 
   1932   1.1  riastrad /*
   1933   1.1  riastrad  * Get crtc VBLANK count.
   1934   1.1  riastrad  *
   1935   1.1  riastrad  * \param dev DRM device
   1936   1.1  riastrad  * \param data user arguement, pointing to a drm_crtc_get_sequence structure.
   1937   1.1  riastrad  * \param file_priv drm file private for the user's open file descriptor
   1938   1.1  riastrad  */
   1939   1.1  riastrad 
   1940   1.1  riastrad int drm_crtc_get_sequence_ioctl(struct drm_device *dev, void *data,
   1941   1.1  riastrad 				struct drm_file *file_priv)
   1942   1.1  riastrad {
   1943   1.1  riastrad 	struct drm_crtc *crtc;
   1944   1.1  riastrad 	struct drm_vblank_crtc *vblank;
   1945   1.1  riastrad 	int pipe;
   1946   1.1  riastrad 	struct drm_crtc_get_sequence *get_seq = data;
   1947   1.1  riastrad 	ktime_t now;
   1948   1.1  riastrad 	bool vblank_enabled;
   1949   1.1  riastrad 	int ret;
   1950   1.1  riastrad 
   1951   1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1952   1.1  riastrad 		return -EOPNOTSUPP;
   1953   1.1  riastrad 
   1954   1.1  riastrad 	if (!dev->irq_enabled)
   1955   1.1  riastrad 		return -EOPNOTSUPP;
   1956   1.1  riastrad 
   1957   1.1  riastrad 	crtc = drm_crtc_find(dev, file_priv, get_seq->crtc_id);
   1958   1.1  riastrad 	if (!crtc)
   1959   1.1  riastrad 		return -ENOENT;
   1960   1.1  riastrad 
   1961   1.1  riastrad 	pipe = drm_crtc_index(crtc);
   1962   1.1  riastrad 
   1963   1.1  riastrad 	vblank = &dev->vblank[pipe];
   1964   1.1  riastrad 	vblank_enabled = dev->vblank_disable_immediate && READ_ONCE(vblank->enabled);
   1965   1.1  riastrad 
   1966   1.1  riastrad 	if (!vblank_enabled) {
   1967   1.1  riastrad 		ret = drm_crtc_vblank_get(crtc);
   1968   1.1  riastrad 		if (ret) {
   1969   1.1  riastrad 			DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
   1970   1.1  riastrad 			return ret;
   1971   1.1  riastrad 		}
   1972   1.1  riastrad 	}
   1973   1.1  riastrad 	drm_modeset_lock(&crtc->mutex, NULL);
   1974   1.1  riastrad 	if (crtc->state)
   1975   1.1  riastrad 		get_seq->active = crtc->state->enable;
   1976   1.1  riastrad 	else
   1977   1.1  riastrad 		get_seq->active = crtc->enabled;
   1978   1.1  riastrad 	drm_modeset_unlock(&crtc->mutex);
   1979   1.1  riastrad 	get_seq->sequence = drm_vblank_count_and_time(dev, pipe, &now);
   1980   1.1  riastrad 	get_seq->sequence_ns = ktime_to_ns(now);
   1981   1.1  riastrad 	if (!vblank_enabled)
   1982   1.1  riastrad 		drm_crtc_vblank_put(crtc);
   1983   1.1  riastrad 	return 0;
   1984   1.1  riastrad }
   1985   1.1  riastrad 
   1986   1.1  riastrad /*
   1987   1.1  riastrad  * Queue a event for VBLANK sequence
   1988   1.1  riastrad  *
   1989   1.1  riastrad  * \param dev DRM device
   1990   1.1  riastrad  * \param data user arguement, pointing to a drm_crtc_queue_sequence structure.
   1991   1.1  riastrad  * \param file_priv drm file private for the user's open file descriptor
   1992   1.1  riastrad  */
   1993   1.1  riastrad 
   1994   1.1  riastrad int drm_crtc_queue_sequence_ioctl(struct drm_device *dev, void *data,
   1995   1.1  riastrad 				  struct drm_file *file_priv)
   1996   1.1  riastrad {
   1997   1.1  riastrad 	struct drm_crtc *crtc;
   1998   1.1  riastrad 	struct drm_vblank_crtc *vblank;
   1999   1.1  riastrad 	int pipe;
   2000   1.1  riastrad 	struct drm_crtc_queue_sequence *queue_seq = data;
   2001   1.1  riastrad 	ktime_t now;
   2002   1.1  riastrad 	struct drm_pending_vblank_event *e;
   2003   1.1  riastrad 	u32 flags;
   2004   1.1  riastrad 	u64 seq;
   2005   1.1  riastrad 	u64 req_seq;
   2006   1.1  riastrad 	int ret;
   2007   1.1  riastrad 	unsigned long spin_flags;
   2008   1.1  riastrad 
   2009   1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2010   1.1  riastrad 		return -EOPNOTSUPP;
   2011   1.1  riastrad 
   2012   1.1  riastrad 	if (!dev->irq_enabled)
   2013   1.1  riastrad 		return -EOPNOTSUPP;
   2014   1.1  riastrad 
   2015   1.1  riastrad 	crtc = drm_crtc_find(dev, file_priv, queue_seq->crtc_id);
   2016   1.1  riastrad 	if (!crtc)
   2017   1.1  riastrad 		return -ENOENT;
   2018   1.1  riastrad 
   2019   1.1  riastrad 	flags = queue_seq->flags;
   2020   1.1  riastrad 	/* Check valid flag bits */
   2021   1.1  riastrad 	if (flags & ~(DRM_CRTC_SEQUENCE_RELATIVE|
   2022   1.1  riastrad 		      DRM_CRTC_SEQUENCE_NEXT_ON_MISS))
   2023   1.1  riastrad 		return -EINVAL;
   2024   1.1  riastrad 
   2025   1.1  riastrad 	pipe = drm_crtc_index(crtc);
   2026   1.1  riastrad 
   2027   1.1  riastrad 	vblank = &dev->vblank[pipe];
   2028   1.1  riastrad 
   2029   1.1  riastrad 	e = kzalloc(sizeof(*e), GFP_KERNEL);
   2030   1.1  riastrad 	if (e == NULL)
   2031   1.1  riastrad 		return -ENOMEM;
   2032   1.1  riastrad 
   2033   1.1  riastrad 	ret = drm_crtc_vblank_get(crtc);
   2034   1.1  riastrad 	if (ret) {
   2035   1.1  riastrad 		DRM_DEBUG("crtc %d failed to acquire vblank counter, %d\n", pipe, ret);
   2036   1.1  riastrad 		goto err_free;
   2037   1.1  riastrad 	}
   2038   1.1  riastrad 
   2039   1.1  riastrad 	seq = drm_vblank_count_and_time(dev, pipe, &now);
   2040   1.1  riastrad 	req_seq = queue_seq->sequence;
   2041   1.1  riastrad 
   2042   1.1  riastrad 	if (flags & DRM_CRTC_SEQUENCE_RELATIVE)
   2043   1.1  riastrad 		req_seq += seq;
   2044   1.1  riastrad 
   2045   1.1  riastrad 	if ((flags & DRM_CRTC_SEQUENCE_NEXT_ON_MISS) && vblank_passed(seq, req_seq))
   2046   1.1  riastrad 		req_seq = seq + 1;
   2047   1.1  riastrad 
   2048   1.1  riastrad 	e->pipe = pipe;
   2049   1.1  riastrad 	e->event.base.type = DRM_EVENT_CRTC_SEQUENCE;
   2050   1.1  riastrad 	e->event.base.length = sizeof(e->event.seq);
   2051   1.1  riastrad 	e->event.seq.user_data = queue_seq->user_data;
   2052   1.1  riastrad 
   2053   1.1  riastrad 	spin_lock_irqsave(&dev->event_lock, spin_flags);
   2054   1.1  riastrad 
   2055   1.1  riastrad 	/*
   2056   1.1  riastrad 	 * drm_crtc_vblank_off() might have been called after we called
   2057   1.1  riastrad 	 * drm_crtc_vblank_get(). drm_crtc_vblank_off() holds event_lock around the
   2058   1.1  riastrad 	 * vblank disable, so no need for further locking.  The reference from
   2059   1.1  riastrad 	 * drm_crtc_vblank_get() protects against vblank disable from another source.
   2060   1.1  riastrad 	 */
   2061   1.1  riastrad 	if (!READ_ONCE(vblank->enabled)) {
   2062   1.1  riastrad 		ret = -EINVAL;
   2063   1.1  riastrad 		goto err_unlock;
   2064   1.1  riastrad 	}
   2065   1.1  riastrad 
   2066   1.1  riastrad 	ret = drm_event_reserve_init_locked(dev, file_priv, &e->base,
   2067   1.1  riastrad 					    &e->event.base);
   2068   1.1  riastrad 
   2069   1.1  riastrad 	if (ret)
   2070   1.1  riastrad 		goto err_unlock;
   2071   1.1  riastrad 
   2072   1.1  riastrad 	e->sequence = req_seq;
   2073   1.1  riastrad 
   2074   1.1  riastrad 	if (vblank_passed(seq, req_seq)) {
   2075   1.1  riastrad 		drm_crtc_vblank_put(crtc);
   2076   1.1  riastrad 		send_vblank_event(dev, e, seq, now);
   2077   1.1  riastrad 		queue_seq->sequence = seq;
   2078   1.1  riastrad 	} else {
   2079   1.1  riastrad 		/* drm_handle_vblank_events will call drm_vblank_put */
   2080   1.1  riastrad 		list_add_tail(&e->base.link, &dev->vblank_event_list);
   2081   1.1  riastrad 		queue_seq->sequence = req_seq;
   2082   1.1  riastrad 	}
   2083   1.1  riastrad 
   2084   1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, spin_flags);
   2085   1.1  riastrad 	return 0;
   2086   1.1  riastrad 
   2087   1.1  riastrad err_unlock:
   2088   1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, spin_flags);
   2089   1.1  riastrad 	drm_crtc_vblank_put(crtc);
   2090   1.1  riastrad err_free:
   2091   1.1  riastrad 	kfree(e);
   2092   1.1  riastrad 	return ret;
   2093   1.1  riastrad }
   2094