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