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