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