Home | History | Annotate | Line # | Download | only in drm
drm_irq.c revision 1.1.1.2
      1      1.1  riastrad /**
      2      1.1  riastrad  * \file drm_irq.c
      3      1.1  riastrad  * IRQ support
      4      1.1  riastrad  *
      5      1.1  riastrad  * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
      6      1.1  riastrad  * \author Gareth Hughes <gareth (at) valinux.com>
      7      1.1  riastrad  */
      8      1.1  riastrad 
      9      1.1  riastrad /*
     10      1.1  riastrad  * Created: Fri Mar 19 14:30:16 1999 by faith (at) valinux.com
     11      1.1  riastrad  *
     12      1.1  riastrad  * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
     13      1.1  riastrad  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
     14      1.1  riastrad  * All Rights Reserved.
     15      1.1  riastrad  *
     16      1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     17      1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
     18      1.1  riastrad  * to deal in the Software without restriction, including without limitation
     19      1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     20      1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     21      1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     22      1.1  riastrad  *
     23      1.1  riastrad  * The above copyright notice and this permission notice (including the next
     24      1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     25      1.1  riastrad  * Software.
     26      1.1  riastrad  *
     27      1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     28      1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     29      1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     30      1.1  riastrad  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
     31      1.1  riastrad  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
     32      1.1  riastrad  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
     33      1.1  riastrad  * OTHER DEALINGS IN THE SOFTWARE.
     34      1.1  riastrad  */
     35      1.1  riastrad 
     36      1.1  riastrad #include <drm/drmP.h>
     37      1.1  riastrad #include "drm_trace.h"
     38      1.1  riastrad 
     39      1.1  riastrad #include <linux/interrupt.h>	/* For task queue support */
     40      1.1  riastrad #include <linux/slab.h>
     41      1.1  riastrad 
     42      1.1  riastrad #include <linux/vgaarb.h>
     43      1.1  riastrad #include <linux/export.h>
     44      1.1  riastrad 
     45      1.1  riastrad /* Access macro for slots in vblank timestamp ringbuffer. */
     46  1.1.1.2  riastrad #define vblanktimestamp(dev, crtc, count) \
     47  1.1.1.2  riastrad 	((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
     48      1.1  riastrad 
     49      1.1  riastrad /* Retry timestamp calculation up to 3 times to satisfy
     50      1.1  riastrad  * drm_timestamp_precision before giving up.
     51      1.1  riastrad  */
     52      1.1  riastrad #define DRM_TIMESTAMP_MAXRETRIES 3
     53      1.1  riastrad 
     54      1.1  riastrad /* Threshold in nanoseconds for detection of redundant
     55      1.1  riastrad  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
     56      1.1  riastrad  */
     57      1.1  riastrad #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
     58      1.1  riastrad 
     59      1.1  riastrad /**
     60      1.1  riastrad  * Get interrupt from bus id.
     61      1.1  riastrad  *
     62      1.1  riastrad  * \param inode device inode.
     63      1.1  riastrad  * \param file_priv DRM file private.
     64      1.1  riastrad  * \param cmd command.
     65      1.1  riastrad  * \param arg user argument, pointing to a drm_irq_busid structure.
     66      1.1  riastrad  * \return zero on success or a negative number on failure.
     67      1.1  riastrad  *
     68      1.1  riastrad  * Finds the PCI device with the specified bus id and gets its IRQ number.
     69      1.1  riastrad  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
     70      1.1  riastrad  * to that of the device that this DRM instance attached to.
     71      1.1  riastrad  */
     72      1.1  riastrad int drm_irq_by_busid(struct drm_device *dev, void *data,
     73      1.1  riastrad 		     struct drm_file *file_priv)
     74      1.1  riastrad {
     75      1.1  riastrad 	struct drm_irq_busid *p = data;
     76      1.1  riastrad 
     77      1.1  riastrad 	if (!dev->driver->bus->irq_by_busid)
     78      1.1  riastrad 		return -EINVAL;
     79      1.1  riastrad 
     80      1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
     81      1.1  riastrad 		return -EINVAL;
     82      1.1  riastrad 
     83      1.1  riastrad 	return dev->driver->bus->irq_by_busid(dev, p);
     84      1.1  riastrad }
     85      1.1  riastrad 
     86      1.1  riastrad /*
     87      1.1  riastrad  * Clear vblank timestamp buffer for a crtc.
     88      1.1  riastrad  */
     89      1.1  riastrad static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
     90      1.1  riastrad {
     91  1.1.1.2  riastrad 	memset(dev->vblank[crtc].time, 0, sizeof(dev->vblank[crtc].time));
     92      1.1  riastrad }
     93      1.1  riastrad 
     94      1.1  riastrad /*
     95      1.1  riastrad  * Disable vblank irq's on crtc, make sure that last vblank count
     96      1.1  riastrad  * of hardware and corresponding consistent software vblank counter
     97      1.1  riastrad  * are preserved, even if there are any spurious vblank irq's after
     98      1.1  riastrad  * disable.
     99      1.1  riastrad  */
    100      1.1  riastrad static void vblank_disable_and_save(struct drm_device *dev, int crtc)
    101      1.1  riastrad {
    102      1.1  riastrad 	unsigned long irqflags;
    103      1.1  riastrad 	u32 vblcount;
    104      1.1  riastrad 	s64 diff_ns;
    105      1.1  riastrad 	int vblrc;
    106      1.1  riastrad 	struct timeval tvblank;
    107      1.1  riastrad 	int count = DRM_TIMESTAMP_MAXRETRIES;
    108      1.1  riastrad 
    109      1.1  riastrad 	/* Prevent vblank irq processing while disabling vblank irqs,
    110      1.1  riastrad 	 * so no updates of timestamps or count can happen after we've
    111      1.1  riastrad 	 * disabled. Needed to prevent races in case of delayed irq's.
    112      1.1  riastrad 	 */
    113      1.1  riastrad 	spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
    114      1.1  riastrad 
    115      1.1  riastrad 	dev->driver->disable_vblank(dev, crtc);
    116  1.1.1.2  riastrad 	dev->vblank[crtc].enabled = false;
    117      1.1  riastrad 
    118      1.1  riastrad 	/* No further vblank irq's will be processed after
    119      1.1  riastrad 	 * this point. Get current hardware vblank count and
    120      1.1  riastrad 	 * vblank timestamp, repeat until they are consistent.
    121      1.1  riastrad 	 *
    122      1.1  riastrad 	 * FIXME: There is still a race condition here and in
    123      1.1  riastrad 	 * drm_update_vblank_count() which can cause off-by-one
    124      1.1  riastrad 	 * reinitialization of software vblank counter. If gpu
    125      1.1  riastrad 	 * vblank counter doesn't increment exactly at the leading
    126      1.1  riastrad 	 * edge of a vblank interval, then we can lose 1 count if
    127      1.1  riastrad 	 * we happen to execute between start of vblank and the
    128      1.1  riastrad 	 * delayed gpu counter increment.
    129      1.1  riastrad 	 */
    130      1.1  riastrad 	do {
    131  1.1.1.2  riastrad 		dev->vblank[crtc].last = dev->driver->get_vblank_counter(dev, crtc);
    132      1.1  riastrad 		vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
    133  1.1.1.2  riastrad 	} while (dev->vblank[crtc].last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
    134      1.1  riastrad 
    135      1.1  riastrad 	if (!count)
    136      1.1  riastrad 		vblrc = 0;
    137      1.1  riastrad 
    138      1.1  riastrad 	/* Compute time difference to stored timestamp of last vblank
    139      1.1  riastrad 	 * as updated by last invocation of drm_handle_vblank() in vblank irq.
    140      1.1  riastrad 	 */
    141  1.1.1.2  riastrad 	vblcount = atomic_read(&dev->vblank[crtc].count);
    142      1.1  riastrad 	diff_ns = timeval_to_ns(&tvblank) -
    143      1.1  riastrad 		  timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
    144      1.1  riastrad 
    145      1.1  riastrad 	/* If there is at least 1 msec difference between the last stored
    146      1.1  riastrad 	 * timestamp and tvblank, then we are currently executing our
    147      1.1  riastrad 	 * disable inside a new vblank interval, the tvblank timestamp
    148      1.1  riastrad 	 * corresponds to this new vblank interval and the irq handler
    149      1.1  riastrad 	 * for this vblank didn't run yet and won't run due to our disable.
    150      1.1  riastrad 	 * Therefore we need to do the job of drm_handle_vblank() and
    151      1.1  riastrad 	 * increment the vblank counter by one to account for this vblank.
    152      1.1  riastrad 	 *
    153      1.1  riastrad 	 * Skip this step if there isn't any high precision timestamp
    154      1.1  riastrad 	 * available. In that case we can't account for this and just
    155      1.1  riastrad 	 * hope for the best.
    156      1.1  riastrad 	 */
    157      1.1  riastrad 	if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
    158  1.1.1.2  riastrad 		atomic_inc(&dev->vblank[crtc].count);
    159      1.1  riastrad 		smp_mb__after_atomic_inc();
    160      1.1  riastrad 	}
    161      1.1  riastrad 
    162      1.1  riastrad 	/* Invalidate all timestamps while vblank irq's are off. */
    163      1.1  riastrad 	clear_vblank_timestamps(dev, crtc);
    164      1.1  riastrad 
    165      1.1  riastrad 	spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
    166      1.1  riastrad }
    167      1.1  riastrad 
    168      1.1  riastrad static void vblank_disable_fn(unsigned long arg)
    169      1.1  riastrad {
    170      1.1  riastrad 	struct drm_device *dev = (struct drm_device *)arg;
    171      1.1  riastrad 	unsigned long irqflags;
    172      1.1  riastrad 	int i;
    173      1.1  riastrad 
    174      1.1  riastrad 	if (!dev->vblank_disable_allowed)
    175      1.1  riastrad 		return;
    176      1.1  riastrad 
    177      1.1  riastrad 	for (i = 0; i < dev->num_crtcs; i++) {
    178      1.1  riastrad 		spin_lock_irqsave(&dev->vbl_lock, irqflags);
    179  1.1.1.2  riastrad 		if (atomic_read(&dev->vblank[i].refcount) == 0 &&
    180  1.1.1.2  riastrad 		    dev->vblank[i].enabled) {
    181      1.1  riastrad 			DRM_DEBUG("disabling vblank on crtc %d\n", i);
    182      1.1  riastrad 			vblank_disable_and_save(dev, i);
    183      1.1  riastrad 		}
    184      1.1  riastrad 		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
    185      1.1  riastrad 	}
    186      1.1  riastrad }
    187      1.1  riastrad 
    188      1.1  riastrad void drm_vblank_cleanup(struct drm_device *dev)
    189      1.1  riastrad {
    190      1.1  riastrad 	/* Bail if the driver didn't call drm_vblank_init() */
    191      1.1  riastrad 	if (dev->num_crtcs == 0)
    192      1.1  riastrad 		return;
    193      1.1  riastrad 
    194      1.1  riastrad 	del_timer_sync(&dev->vblank_disable_timer);
    195      1.1  riastrad 
    196      1.1  riastrad 	vblank_disable_fn((unsigned long)dev);
    197      1.1  riastrad 
    198  1.1.1.2  riastrad 	kfree(dev->vblank);
    199      1.1  riastrad 
    200      1.1  riastrad 	dev->num_crtcs = 0;
    201      1.1  riastrad }
    202      1.1  riastrad EXPORT_SYMBOL(drm_vblank_cleanup);
    203      1.1  riastrad 
    204      1.1  riastrad int drm_vblank_init(struct drm_device *dev, int num_crtcs)
    205      1.1  riastrad {
    206      1.1  riastrad 	int i, ret = -ENOMEM;
    207      1.1  riastrad 
    208      1.1  riastrad 	setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
    209      1.1  riastrad 		    (unsigned long)dev);
    210      1.1  riastrad 	spin_lock_init(&dev->vbl_lock);
    211      1.1  riastrad 	spin_lock_init(&dev->vblank_time_lock);
    212      1.1  riastrad 
    213      1.1  riastrad 	dev->num_crtcs = num_crtcs;
    214      1.1  riastrad 
    215  1.1.1.2  riastrad 	dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
    216  1.1.1.2  riastrad 	if (!dev->vblank)
    217      1.1  riastrad 		goto err;
    218      1.1  riastrad 
    219  1.1.1.2  riastrad 	for (i = 0; i < num_crtcs; i++)
    220  1.1.1.2  riastrad 		init_waitqueue_head(&dev->vblank[i].queue);
    221      1.1  riastrad 
    222  1.1.1.2  riastrad 	DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
    223      1.1  riastrad 
    224      1.1  riastrad 	/* Driver specific high-precision vblank timestamping supported? */
    225      1.1  riastrad 	if (dev->driver->get_vblank_timestamp)
    226      1.1  riastrad 		DRM_INFO("Driver supports precise vblank timestamp query.\n");
    227      1.1  riastrad 	else
    228      1.1  riastrad 		DRM_INFO("No driver support for vblank timestamp query.\n");
    229      1.1  riastrad 
    230  1.1.1.2  riastrad 	dev->vblank_disable_allowed = false;
    231      1.1  riastrad 
    232      1.1  riastrad 	return 0;
    233      1.1  riastrad 
    234      1.1  riastrad err:
    235      1.1  riastrad 	drm_vblank_cleanup(dev);
    236      1.1  riastrad 	return ret;
    237      1.1  riastrad }
    238      1.1  riastrad EXPORT_SYMBOL(drm_vblank_init);
    239      1.1  riastrad 
    240      1.1  riastrad static void drm_irq_vgaarb_nokms(void *cookie, bool state)
    241      1.1  riastrad {
    242      1.1  riastrad 	struct drm_device *dev = cookie;
    243      1.1  riastrad 
    244      1.1  riastrad 	if (dev->driver->vgaarb_irq) {
    245      1.1  riastrad 		dev->driver->vgaarb_irq(dev, state);
    246      1.1  riastrad 		return;
    247      1.1  riastrad 	}
    248      1.1  riastrad 
    249      1.1  riastrad 	if (!dev->irq_enabled)
    250      1.1  riastrad 		return;
    251      1.1  riastrad 
    252      1.1  riastrad 	if (state) {
    253      1.1  riastrad 		if (dev->driver->irq_uninstall)
    254      1.1  riastrad 			dev->driver->irq_uninstall(dev);
    255      1.1  riastrad 	} else {
    256      1.1  riastrad 		if (dev->driver->irq_preinstall)
    257      1.1  riastrad 			dev->driver->irq_preinstall(dev);
    258      1.1  riastrad 		if (dev->driver->irq_postinstall)
    259      1.1  riastrad 			dev->driver->irq_postinstall(dev);
    260      1.1  riastrad 	}
    261      1.1  riastrad }
    262      1.1  riastrad 
    263      1.1  riastrad /**
    264      1.1  riastrad  * Install IRQ handler.
    265      1.1  riastrad  *
    266      1.1  riastrad  * \param dev DRM device.
    267      1.1  riastrad  *
    268      1.1  riastrad  * Initializes the IRQ related data. Installs the handler, calling the driver
    269      1.1  riastrad  * \c irq_preinstall() and \c irq_postinstall() functions
    270      1.1  riastrad  * before and after the installation.
    271      1.1  riastrad  */
    272      1.1  riastrad int drm_irq_install(struct drm_device *dev)
    273      1.1  riastrad {
    274      1.1  riastrad 	int ret;
    275      1.1  riastrad 	unsigned long sh_flags = 0;
    276      1.1  riastrad 	char *irqname;
    277      1.1  riastrad 
    278      1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
    279      1.1  riastrad 		return -EINVAL;
    280      1.1  riastrad 
    281      1.1  riastrad 	if (drm_dev_to_irq(dev) == 0)
    282      1.1  riastrad 		return -EINVAL;
    283      1.1  riastrad 
    284      1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    285      1.1  riastrad 
    286      1.1  riastrad 	/* Driver must have been initialized */
    287      1.1  riastrad 	if (!dev->dev_private) {
    288      1.1  riastrad 		mutex_unlock(&dev->struct_mutex);
    289      1.1  riastrad 		return -EINVAL;
    290      1.1  riastrad 	}
    291      1.1  riastrad 
    292      1.1  riastrad 	if (dev->irq_enabled) {
    293      1.1  riastrad 		mutex_unlock(&dev->struct_mutex);
    294      1.1  riastrad 		return -EBUSY;
    295      1.1  riastrad 	}
    296  1.1.1.2  riastrad 	dev->irq_enabled = true;
    297      1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
    298      1.1  riastrad 
    299      1.1  riastrad 	DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
    300      1.1  riastrad 
    301      1.1  riastrad 	/* Before installing handler */
    302      1.1  riastrad 	if (dev->driver->irq_preinstall)
    303      1.1  riastrad 		dev->driver->irq_preinstall(dev);
    304      1.1  riastrad 
    305      1.1  riastrad 	/* Install handler */
    306      1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
    307      1.1  riastrad 		sh_flags = IRQF_SHARED;
    308      1.1  riastrad 
    309      1.1  riastrad 	if (dev->devname)
    310      1.1  riastrad 		irqname = dev->devname;
    311      1.1  riastrad 	else
    312      1.1  riastrad 		irqname = dev->driver->name;
    313      1.1  riastrad 
    314      1.1  riastrad 	ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
    315      1.1  riastrad 			  sh_flags, irqname, dev);
    316      1.1  riastrad 
    317      1.1  riastrad 	if (ret < 0) {
    318      1.1  riastrad 		mutex_lock(&dev->struct_mutex);
    319  1.1.1.2  riastrad 		dev->irq_enabled = false;
    320      1.1  riastrad 		mutex_unlock(&dev->struct_mutex);
    321      1.1  riastrad 		return ret;
    322      1.1  riastrad 	}
    323      1.1  riastrad 
    324      1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
    325      1.1  riastrad 		vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
    326      1.1  riastrad 
    327      1.1  riastrad 	/* After installing handler */
    328      1.1  riastrad 	if (dev->driver->irq_postinstall)
    329      1.1  riastrad 		ret = dev->driver->irq_postinstall(dev);
    330      1.1  riastrad 
    331      1.1  riastrad 	if (ret < 0) {
    332      1.1  riastrad 		mutex_lock(&dev->struct_mutex);
    333  1.1.1.2  riastrad 		dev->irq_enabled = false;
    334      1.1  riastrad 		mutex_unlock(&dev->struct_mutex);
    335      1.1  riastrad 		if (!drm_core_check_feature(dev, DRIVER_MODESET))
    336      1.1  riastrad 			vga_client_register(dev->pdev, NULL, NULL, NULL);
    337      1.1  riastrad 		free_irq(drm_dev_to_irq(dev), dev);
    338      1.1  riastrad 	}
    339      1.1  riastrad 
    340      1.1  riastrad 	return ret;
    341      1.1  riastrad }
    342      1.1  riastrad EXPORT_SYMBOL(drm_irq_install);
    343      1.1  riastrad 
    344      1.1  riastrad /**
    345      1.1  riastrad  * Uninstall the IRQ handler.
    346      1.1  riastrad  *
    347      1.1  riastrad  * \param dev DRM device.
    348      1.1  riastrad  *
    349      1.1  riastrad  * Calls the driver's \c irq_uninstall() function, and stops the irq.
    350      1.1  riastrad  */
    351      1.1  riastrad int drm_irq_uninstall(struct drm_device *dev)
    352      1.1  riastrad {
    353      1.1  riastrad 	unsigned long irqflags;
    354  1.1.1.2  riastrad 	bool irq_enabled;
    355  1.1.1.2  riastrad 	int i;
    356      1.1  riastrad 
    357      1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
    358      1.1  riastrad 		return -EINVAL;
    359      1.1  riastrad 
    360      1.1  riastrad 	mutex_lock(&dev->struct_mutex);
    361      1.1  riastrad 	irq_enabled = dev->irq_enabled;
    362  1.1.1.2  riastrad 	dev->irq_enabled = false;
    363      1.1  riastrad 	mutex_unlock(&dev->struct_mutex);
    364      1.1  riastrad 
    365      1.1  riastrad 	/*
    366      1.1  riastrad 	 * Wake up any waiters so they don't hang.
    367      1.1  riastrad 	 */
    368      1.1  riastrad 	if (dev->num_crtcs) {
    369      1.1  riastrad 		spin_lock_irqsave(&dev->vbl_lock, irqflags);
    370      1.1  riastrad 		for (i = 0; i < dev->num_crtcs; i++) {
    371  1.1.1.2  riastrad 			wake_up(&dev->vblank[i].queue);
    372  1.1.1.2  riastrad 			dev->vblank[i].enabled = false;
    373  1.1.1.2  riastrad 			dev->vblank[i].last =
    374      1.1  riastrad 				dev->driver->get_vblank_counter(dev, i);
    375      1.1  riastrad 		}
    376      1.1  riastrad 		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
    377      1.1  riastrad 	}
    378      1.1  riastrad 
    379      1.1  riastrad 	if (!irq_enabled)
    380      1.1  riastrad 		return -EINVAL;
    381      1.1  riastrad 
    382      1.1  riastrad 	DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
    383      1.1  riastrad 
    384      1.1  riastrad 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
    385      1.1  riastrad 		vga_client_register(dev->pdev, NULL, NULL, NULL);
    386      1.1  riastrad 
    387      1.1  riastrad 	if (dev->driver->irq_uninstall)
    388      1.1  riastrad 		dev->driver->irq_uninstall(dev);
    389      1.1  riastrad 
    390      1.1  riastrad 	free_irq(drm_dev_to_irq(dev), dev);
    391      1.1  riastrad 
    392      1.1  riastrad 	return 0;
    393      1.1  riastrad }
    394      1.1  riastrad EXPORT_SYMBOL(drm_irq_uninstall);
    395      1.1  riastrad 
    396      1.1  riastrad /**
    397      1.1  riastrad  * IRQ control ioctl.
    398      1.1  riastrad  *
    399      1.1  riastrad  * \param inode device inode.
    400      1.1  riastrad  * \param file_priv DRM file private.
    401      1.1  riastrad  * \param cmd command.
    402      1.1  riastrad  * \param arg user argument, pointing to a drm_control structure.
    403      1.1  riastrad  * \return zero on success or a negative number on failure.
    404      1.1  riastrad  *
    405      1.1  riastrad  * Calls irq_install() or irq_uninstall() according to \p arg.
    406      1.1  riastrad  */
    407      1.1  riastrad int drm_control(struct drm_device *dev, void *data,
    408      1.1  riastrad 		struct drm_file *file_priv)
    409      1.1  riastrad {
    410      1.1  riastrad 	struct drm_control *ctl = data;
    411      1.1  riastrad 
    412      1.1  riastrad 	/* if we haven't irq we fallback for compatibility reasons -
    413      1.1  riastrad 	 * this used to be a separate function in drm_dma.h
    414      1.1  riastrad 	 */
    415      1.1  riastrad 
    416      1.1  riastrad 
    417      1.1  riastrad 	switch (ctl->func) {
    418      1.1  riastrad 	case DRM_INST_HANDLER:
    419      1.1  riastrad 		if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
    420      1.1  riastrad 			return 0;
    421      1.1  riastrad 		if (drm_core_check_feature(dev, DRIVER_MODESET))
    422      1.1  riastrad 			return 0;
    423      1.1  riastrad 		if (dev->if_version < DRM_IF_VERSION(1, 2) &&
    424      1.1  riastrad 		    ctl->irq != drm_dev_to_irq(dev))
    425      1.1  riastrad 			return -EINVAL;
    426      1.1  riastrad 		return drm_irq_install(dev);
    427      1.1  riastrad 	case DRM_UNINST_HANDLER:
    428      1.1  riastrad 		if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
    429      1.1  riastrad 			return 0;
    430      1.1  riastrad 		if (drm_core_check_feature(dev, DRIVER_MODESET))
    431      1.1  riastrad 			return 0;
    432      1.1  riastrad 		return drm_irq_uninstall(dev);
    433      1.1  riastrad 	default:
    434      1.1  riastrad 		return -EINVAL;
    435      1.1  riastrad 	}
    436      1.1  riastrad }
    437      1.1  riastrad 
    438      1.1  riastrad /**
    439  1.1.1.2  riastrad  * drm_calc_timestamping_constants - Calculate vblank timestamp constants
    440      1.1  riastrad  *
    441      1.1  riastrad  * @crtc drm_crtc whose timestamp constants should be updated.
    442  1.1.1.2  riastrad  * @mode display mode containing the scanout timings
    443      1.1  riastrad  *
    444  1.1.1.2  riastrad  * Calculate and store various constants which are later
    445  1.1.1.2  riastrad  * needed by vblank and swap-completion timestamping, e.g,
    446  1.1.1.2  riastrad  * by drm_calc_vbltimestamp_from_scanoutpos(). They are
    447  1.1.1.2  riastrad  * derived from crtc's true scanout timing, so they take
    448  1.1.1.2  riastrad  * things like panel scaling or other adjustments into account.
    449      1.1  riastrad  */
    450  1.1.1.2  riastrad void drm_calc_timestamping_constants(struct drm_crtc *crtc,
    451  1.1.1.2  riastrad 				     const struct drm_display_mode *mode)
    452      1.1  riastrad {
    453  1.1.1.2  riastrad 	int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
    454  1.1.1.2  riastrad 	int dotclock = mode->crtc_clock;
    455      1.1  riastrad 
    456      1.1  riastrad 	/* Valid dotclock? */
    457      1.1  riastrad 	if (dotclock > 0) {
    458  1.1.1.2  riastrad 		int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
    459  1.1.1.2  riastrad 
    460  1.1.1.2  riastrad 		/*
    461  1.1.1.2  riastrad 		 * Convert scanline length in pixels and video
    462  1.1.1.2  riastrad 		 * dot clock to line duration, frame duration
    463  1.1.1.2  riastrad 		 * and pixel duration in nanoseconds:
    464      1.1  riastrad 		 */
    465  1.1.1.2  riastrad 		pixeldur_ns = 1000000 / dotclock;
    466  1.1.1.2  riastrad 		linedur_ns  = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
    467  1.1.1.2  riastrad 		framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
    468  1.1.1.2  riastrad 
    469  1.1.1.2  riastrad 		/*
    470  1.1.1.2  riastrad 		 * Fields of interlaced scanout modes are only half a frame duration.
    471  1.1.1.2  riastrad 		 */
    472  1.1.1.2  riastrad 		if (mode->flags & DRM_MODE_FLAG_INTERLACE)
    473  1.1.1.2  riastrad 			framedur_ns /= 2;
    474      1.1  riastrad 	} else
    475      1.1  riastrad 		DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
    476      1.1  riastrad 			  crtc->base.id);
    477      1.1  riastrad 
    478      1.1  riastrad 	crtc->pixeldur_ns = pixeldur_ns;
    479      1.1  riastrad 	crtc->linedur_ns  = linedur_ns;
    480      1.1  riastrad 	crtc->framedur_ns = framedur_ns;
    481      1.1  riastrad 
    482      1.1  riastrad 	DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
    483  1.1.1.2  riastrad 		  crtc->base.id, mode->crtc_htotal,
    484  1.1.1.2  riastrad 		  mode->crtc_vtotal, mode->crtc_vdisplay);
    485      1.1  riastrad 	DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
    486  1.1.1.2  riastrad 		  crtc->base.id, dotclock, framedur_ns,
    487  1.1.1.2  riastrad 		  linedur_ns, pixeldur_ns);
    488      1.1  riastrad }
    489      1.1  riastrad EXPORT_SYMBOL(drm_calc_timestamping_constants);
    490      1.1  riastrad 
    491      1.1  riastrad /**
    492      1.1  riastrad  * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
    493      1.1  riastrad  * drivers. Implements calculation of exact vblank timestamps from
    494      1.1  riastrad  * given drm_display_mode timings and current video scanout position
    495      1.1  riastrad  * of a crtc. This can be called from within get_vblank_timestamp()
    496      1.1  riastrad  * implementation of a kms driver to implement the actual timestamping.
    497      1.1  riastrad  *
    498      1.1  riastrad  * Should return timestamps conforming to the OML_sync_control OpenML
    499      1.1  riastrad  * extension specification. The timestamp corresponds to the end of
    500      1.1  riastrad  * the vblank interval, aka start of scanout of topmost-leftmost display
    501      1.1  riastrad  * pixel in the following video frame.
    502      1.1  riastrad  *
    503      1.1  riastrad  * Requires support for optional dev->driver->get_scanout_position()
    504      1.1  riastrad  * in kms driver, plus a bit of setup code to provide a drm_display_mode
    505      1.1  riastrad  * that corresponds to the true scanout timing.
    506      1.1  riastrad  *
    507      1.1  riastrad  * The current implementation only handles standard video modes. It
    508      1.1  riastrad  * returns as no operation if a doublescan or interlaced video mode is
    509      1.1  riastrad  * active. Higher level code is expected to handle this.
    510      1.1  riastrad  *
    511      1.1  riastrad  * @dev: DRM device.
    512      1.1  riastrad  * @crtc: Which crtc's vblank timestamp to retrieve.
    513      1.1  riastrad  * @max_error: Desired maximum allowable error in timestamps (nanosecs).
    514      1.1  riastrad  *             On return contains true maximum error of timestamp.
    515      1.1  riastrad  * @vblank_time: Pointer to struct timeval which should receive the timestamp.
    516      1.1  riastrad  * @flags: Flags to pass to driver:
    517      1.1  riastrad  *         0 = Default.
    518      1.1  riastrad  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
    519      1.1  riastrad  * @refcrtc: drm_crtc* of crtc which defines scanout timing.
    520  1.1.1.2  riastrad  * @mode: mode which defines the scanout timings
    521      1.1  riastrad  *
    522      1.1  riastrad  * Returns negative value on error, failure or if not supported in current
    523      1.1  riastrad  * video mode:
    524      1.1  riastrad  *
    525      1.1  riastrad  * -EINVAL   - Invalid crtc.
    526      1.1  riastrad  * -EAGAIN   - Temporary unavailable, e.g., called before initial modeset.
    527      1.1  riastrad  * -ENOTSUPP - Function not supported in current display mode.
    528      1.1  riastrad  * -EIO      - Failed, e.g., due to failed scanout position query.
    529      1.1  riastrad  *
    530      1.1  riastrad  * Returns or'ed positive status flags on success:
    531      1.1  riastrad  *
    532      1.1  riastrad  * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
    533      1.1  riastrad  * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
    534      1.1  riastrad  *
    535      1.1  riastrad  */
    536      1.1  riastrad int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
    537      1.1  riastrad 					  int *max_error,
    538      1.1  riastrad 					  struct timeval *vblank_time,
    539      1.1  riastrad 					  unsigned flags,
    540  1.1.1.2  riastrad 					  const struct drm_crtc *refcrtc,
    541  1.1.1.2  riastrad 					  const struct drm_display_mode *mode)
    542      1.1  riastrad {
    543      1.1  riastrad 	ktime_t stime, etime, mono_time_offset;
    544      1.1  riastrad 	struct timeval tv_etime;
    545  1.1.1.2  riastrad 	int vbl_status;
    546      1.1  riastrad 	int vpos, hpos, i;
    547  1.1.1.2  riastrad 	int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
    548      1.1  riastrad 	bool invbl;
    549      1.1  riastrad 
    550      1.1  riastrad 	if (crtc < 0 || crtc >= dev->num_crtcs) {
    551      1.1  riastrad 		DRM_ERROR("Invalid crtc %d\n", crtc);
    552      1.1  riastrad 		return -EINVAL;
    553      1.1  riastrad 	}
    554      1.1  riastrad 
    555      1.1  riastrad 	/* Scanout position query not supported? Should not happen. */
    556      1.1  riastrad 	if (!dev->driver->get_scanout_position) {
    557      1.1  riastrad 		DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
    558      1.1  riastrad 		return -EIO;
    559      1.1  riastrad 	}
    560      1.1  riastrad 
    561      1.1  riastrad 	/* Durations of frames, lines, pixels in nanoseconds. */
    562      1.1  riastrad 	framedur_ns = refcrtc->framedur_ns;
    563      1.1  riastrad 	linedur_ns  = refcrtc->linedur_ns;
    564      1.1  riastrad 	pixeldur_ns = refcrtc->pixeldur_ns;
    565      1.1  riastrad 
    566      1.1  riastrad 	/* If mode timing undefined, just return as no-op:
    567      1.1  riastrad 	 * Happens during initial modesetting of a crtc.
    568      1.1  riastrad 	 */
    569  1.1.1.2  riastrad 	if (framedur_ns == 0) {
    570      1.1  riastrad 		DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
    571      1.1  riastrad 		return -EAGAIN;
    572      1.1  riastrad 	}
    573      1.1  riastrad 
    574      1.1  riastrad 	/* Get current scanout position with system timestamp.
    575      1.1  riastrad 	 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
    576      1.1  riastrad 	 * if single query takes longer than max_error nanoseconds.
    577      1.1  riastrad 	 *
    578      1.1  riastrad 	 * This guarantees a tight bound on maximum error if
    579      1.1  riastrad 	 * code gets preempted or delayed for some reason.
    580      1.1  riastrad 	 */
    581      1.1  riastrad 	for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
    582  1.1.1.2  riastrad 		/*
    583  1.1.1.2  riastrad 		 * Get vertical and horizontal scanout position vpos, hpos,
    584  1.1.1.2  riastrad 		 * and bounding timestamps stime, etime, pre/post query.
    585      1.1  riastrad 		 */
    586  1.1.1.2  riastrad 		vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
    587  1.1.1.2  riastrad 							       &hpos, &stime, &etime);
    588      1.1  riastrad 
    589  1.1.1.2  riastrad 		/*
    590  1.1.1.2  riastrad 		 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
    591  1.1.1.2  riastrad 		 * CLOCK_REALTIME is requested.
    592  1.1.1.2  riastrad 		 */
    593      1.1  riastrad 		if (!drm_timestamp_monotonic)
    594      1.1  riastrad 			mono_time_offset = ktime_get_monotonic_offset();
    595      1.1  riastrad 
    596      1.1  riastrad 		/* Return as no-op if scanout query unsupported or failed. */
    597      1.1  riastrad 		if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
    598      1.1  riastrad 			DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
    599      1.1  riastrad 				  crtc, vbl_status);
    600      1.1  riastrad 			return -EIO;
    601      1.1  riastrad 		}
    602      1.1  riastrad 
    603  1.1.1.2  riastrad 		/* Compute uncertainty in timestamp of scanout position query. */
    604      1.1  riastrad 		duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
    605      1.1  riastrad 
    606      1.1  riastrad 		/* Accept result with <  max_error nsecs timing uncertainty. */
    607  1.1.1.2  riastrad 		if (duration_ns <= *max_error)
    608      1.1  riastrad 			break;
    609      1.1  riastrad 	}
    610      1.1  riastrad 
    611      1.1  riastrad 	/* Noisy system timing? */
    612      1.1  riastrad 	if (i == DRM_TIMESTAMP_MAXRETRIES) {
    613      1.1  riastrad 		DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
    614  1.1.1.2  riastrad 			  crtc, duration_ns/1000, *max_error/1000, i);
    615      1.1  riastrad 	}
    616      1.1  riastrad 
    617      1.1  riastrad 	/* Return upper bound of timestamp precision error. */
    618  1.1.1.2  riastrad 	*max_error = duration_ns;
    619      1.1  riastrad 
    620      1.1  riastrad 	/* Check if in vblank area:
    621      1.1  riastrad 	 * vpos is >=0 in video scanout area, but negative
    622      1.1  riastrad 	 * within vblank area, counting down the number of lines until
    623      1.1  riastrad 	 * start of scanout.
    624      1.1  riastrad 	 */
    625      1.1  riastrad 	invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
    626      1.1  riastrad 
    627      1.1  riastrad 	/* Convert scanout position into elapsed time at raw_time query
    628      1.1  riastrad 	 * since start of scanout at first display scanline. delta_ns
    629      1.1  riastrad 	 * can be negative if start of scanout hasn't happened yet.
    630      1.1  riastrad 	 */
    631  1.1.1.2  riastrad 	delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
    632      1.1  riastrad 
    633      1.1  riastrad 	if (!drm_timestamp_monotonic)
    634      1.1  riastrad 		etime = ktime_sub(etime, mono_time_offset);
    635      1.1  riastrad 
    636      1.1  riastrad 	/* save this only for debugging purposes */
    637      1.1  riastrad 	tv_etime = ktime_to_timeval(etime);
    638      1.1  riastrad 	/* Subtract time delta from raw timestamp to get final
    639      1.1  riastrad 	 * vblank_time timestamp for end of vblank.
    640      1.1  riastrad 	 */
    641  1.1.1.2  riastrad 	if (delta_ns < 0)
    642  1.1.1.2  riastrad 		etime = ktime_add_ns(etime, -delta_ns);
    643  1.1.1.2  riastrad 	else
    644  1.1.1.2  riastrad 		etime = ktime_sub_ns(etime, delta_ns);
    645      1.1  riastrad 	*vblank_time = ktime_to_timeval(etime);
    646      1.1  riastrad 
    647      1.1  riastrad 	DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
    648      1.1  riastrad 		  crtc, (int)vbl_status, hpos, vpos,
    649      1.1  riastrad 		  (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
    650      1.1  riastrad 		  (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
    651  1.1.1.2  riastrad 		  duration_ns/1000, i);
    652      1.1  riastrad 
    653      1.1  riastrad 	vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
    654      1.1  riastrad 	if (invbl)
    655      1.1  riastrad 		vbl_status |= DRM_VBLANKTIME_INVBL;
    656      1.1  riastrad 
    657      1.1  riastrad 	return vbl_status;
    658      1.1  riastrad }
    659      1.1  riastrad EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
    660      1.1  riastrad 
    661      1.1  riastrad static struct timeval get_drm_timestamp(void)
    662      1.1  riastrad {
    663      1.1  riastrad 	ktime_t now;
    664      1.1  riastrad 
    665      1.1  riastrad 	now = ktime_get();
    666      1.1  riastrad 	if (!drm_timestamp_monotonic)
    667      1.1  riastrad 		now = ktime_sub(now, ktime_get_monotonic_offset());
    668      1.1  riastrad 
    669      1.1  riastrad 	return ktime_to_timeval(now);
    670      1.1  riastrad }
    671      1.1  riastrad 
    672      1.1  riastrad /**
    673      1.1  riastrad  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
    674      1.1  riastrad  * vblank interval.
    675      1.1  riastrad  *
    676      1.1  riastrad  * @dev: DRM device
    677      1.1  riastrad  * @crtc: which crtc's vblank timestamp to retrieve
    678      1.1  riastrad  * @tvblank: Pointer to target struct timeval which should receive the timestamp
    679      1.1  riastrad  * @flags: Flags to pass to driver:
    680      1.1  riastrad  *         0 = Default.
    681      1.1  riastrad  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
    682      1.1  riastrad  *
    683      1.1  riastrad  * Fetches the system timestamp corresponding to the time of the most recent
    684      1.1  riastrad  * vblank interval on specified crtc. May call into kms-driver to
    685      1.1  riastrad  * compute the timestamp with a high-precision GPU specific method.
    686      1.1  riastrad  *
    687      1.1  riastrad  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
    688      1.1  riastrad  * call, i.e., it isn't very precisely locked to the true vblank.
    689      1.1  riastrad  *
    690      1.1  riastrad  * Returns non-zero if timestamp is considered to be very precise.
    691      1.1  riastrad  */
    692      1.1  riastrad u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
    693      1.1  riastrad 			      struct timeval *tvblank, unsigned flags)
    694      1.1  riastrad {
    695      1.1  riastrad 	int ret;
    696      1.1  riastrad 
    697      1.1  riastrad 	/* Define requested maximum error on timestamps (nanoseconds). */
    698      1.1  riastrad 	int max_error = (int) drm_timestamp_precision * 1000;
    699      1.1  riastrad 
    700      1.1  riastrad 	/* Query driver if possible and precision timestamping enabled. */
    701      1.1  riastrad 	if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
    702      1.1  riastrad 		ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
    703      1.1  riastrad 							tvblank, flags);
    704      1.1  riastrad 		if (ret > 0)
    705      1.1  riastrad 			return (u32) ret;
    706      1.1  riastrad 	}
    707      1.1  riastrad 
    708      1.1  riastrad 	/* GPU high precision timestamp query unsupported or failed.
    709      1.1  riastrad 	 * Return current monotonic/gettimeofday timestamp as best estimate.
    710      1.1  riastrad 	 */
    711      1.1  riastrad 	*tvblank = get_drm_timestamp();
    712      1.1  riastrad 
    713      1.1  riastrad 	return 0;
    714      1.1  riastrad }
    715      1.1  riastrad EXPORT_SYMBOL(drm_get_last_vbltimestamp);
    716      1.1  riastrad 
    717      1.1  riastrad /**
    718      1.1  riastrad  * drm_vblank_count - retrieve "cooked" vblank counter value
    719      1.1  riastrad  * @dev: DRM device
    720      1.1  riastrad  * @crtc: which counter to retrieve
    721      1.1  riastrad  *
    722      1.1  riastrad  * Fetches the "cooked" vblank count value that represents the number of
    723      1.1  riastrad  * vblank events since the system was booted, including lost events due to
    724      1.1  riastrad  * modesetting activity.
    725      1.1  riastrad  */
    726      1.1  riastrad u32 drm_vblank_count(struct drm_device *dev, int crtc)
    727      1.1  riastrad {
    728  1.1.1.2  riastrad 	return atomic_read(&dev->vblank[crtc].count);
    729      1.1  riastrad }
    730      1.1  riastrad EXPORT_SYMBOL(drm_vblank_count);
    731      1.1  riastrad 
    732      1.1  riastrad /**
    733      1.1  riastrad  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
    734      1.1  riastrad  * and the system timestamp corresponding to that vblank counter value.
    735      1.1  riastrad  *
    736      1.1  riastrad  * @dev: DRM device
    737      1.1  riastrad  * @crtc: which counter to retrieve
    738      1.1  riastrad  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
    739      1.1  riastrad  *
    740      1.1  riastrad  * Fetches the "cooked" vblank count value that represents the number of
    741      1.1  riastrad  * vblank events since the system was booted, including lost events due to
    742      1.1  riastrad  * modesetting activity. Returns corresponding system timestamp of the time
    743      1.1  riastrad  * of the vblank interval that corresponds to the current value vblank counter
    744      1.1  riastrad  * value.
    745      1.1  riastrad  */
    746      1.1  riastrad u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
    747      1.1  riastrad 			      struct timeval *vblanktime)
    748      1.1  riastrad {
    749      1.1  riastrad 	u32 cur_vblank;
    750      1.1  riastrad 
    751      1.1  riastrad 	/* Read timestamp from slot of _vblank_time ringbuffer
    752      1.1  riastrad 	 * that corresponds to current vblank count. Retry if
    753      1.1  riastrad 	 * count has incremented during readout. This works like
    754      1.1  riastrad 	 * a seqlock.
    755      1.1  riastrad 	 */
    756      1.1  riastrad 	do {
    757  1.1.1.2  riastrad 		cur_vblank = atomic_read(&dev->vblank[crtc].count);
    758      1.1  riastrad 		*vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
    759      1.1  riastrad 		smp_rmb();
    760  1.1.1.2  riastrad 	} while (cur_vblank != atomic_read(&dev->vblank[crtc].count));
    761      1.1  riastrad 
    762      1.1  riastrad 	return cur_vblank;
    763      1.1  riastrad }
    764      1.1  riastrad EXPORT_SYMBOL(drm_vblank_count_and_time);
    765      1.1  riastrad 
    766      1.1  riastrad static void send_vblank_event(struct drm_device *dev,
    767      1.1  riastrad 		struct drm_pending_vblank_event *e,
    768      1.1  riastrad 		unsigned long seq, struct timeval *now)
    769      1.1  riastrad {
    770      1.1  riastrad 	WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
    771      1.1  riastrad 	e->event.sequence = seq;
    772      1.1  riastrad 	e->event.tv_sec = now->tv_sec;
    773      1.1  riastrad 	e->event.tv_usec = now->tv_usec;
    774      1.1  riastrad 
    775      1.1  riastrad 	list_add_tail(&e->base.link,
    776      1.1  riastrad 		      &e->base.file_priv->event_list);
    777      1.1  riastrad 	wake_up_interruptible(&e->base.file_priv->event_wait);
    778      1.1  riastrad 	trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
    779      1.1  riastrad 					 e->event.sequence);
    780      1.1  riastrad }
    781      1.1  riastrad 
    782      1.1  riastrad /**
    783      1.1  riastrad  * drm_send_vblank_event - helper to send vblank event after pageflip
    784      1.1  riastrad  * @dev: DRM device
    785      1.1  riastrad  * @crtc: CRTC in question
    786      1.1  riastrad  * @e: the event to send
    787      1.1  riastrad  *
    788      1.1  riastrad  * Updates sequence # and timestamp on event, and sends it to userspace.
    789      1.1  riastrad  * Caller must hold event lock.
    790      1.1  riastrad  */
    791      1.1  riastrad void drm_send_vblank_event(struct drm_device *dev, int crtc,
    792      1.1  riastrad 		struct drm_pending_vblank_event *e)
    793      1.1  riastrad {
    794      1.1  riastrad 	struct timeval now;
    795      1.1  riastrad 	unsigned int seq;
    796      1.1  riastrad 	if (crtc >= 0) {
    797      1.1  riastrad 		seq = drm_vblank_count_and_time(dev, crtc, &now);
    798      1.1  riastrad 	} else {
    799      1.1  riastrad 		seq = 0;
    800      1.1  riastrad 
    801      1.1  riastrad 		now = get_drm_timestamp();
    802      1.1  riastrad 	}
    803  1.1.1.2  riastrad 	e->pipe = crtc;
    804      1.1  riastrad 	send_vblank_event(dev, e, seq, &now);
    805      1.1  riastrad }
    806      1.1  riastrad EXPORT_SYMBOL(drm_send_vblank_event);
    807      1.1  riastrad 
    808      1.1  riastrad /**
    809      1.1  riastrad  * drm_update_vblank_count - update the master vblank counter
    810      1.1  riastrad  * @dev: DRM device
    811      1.1  riastrad  * @crtc: counter to update
    812      1.1  riastrad  *
    813      1.1  riastrad  * Call back into the driver to update the appropriate vblank counter
    814      1.1  riastrad  * (specified by @crtc).  Deal with wraparound, if it occurred, and
    815      1.1  riastrad  * update the last read value so we can deal with wraparound on the next
    816      1.1  riastrad  * call if necessary.
    817      1.1  riastrad  *
    818      1.1  riastrad  * Only necessary when going from off->on, to account for frames we
    819      1.1  riastrad  * didn't get an interrupt for.
    820      1.1  riastrad  *
    821      1.1  riastrad  * Note: caller must hold dev->vbl_lock since this reads & writes
    822      1.1  riastrad  * device vblank fields.
    823      1.1  riastrad  */
    824      1.1  riastrad static void drm_update_vblank_count(struct drm_device *dev, int crtc)
    825      1.1  riastrad {
    826      1.1  riastrad 	u32 cur_vblank, diff, tslot, rc;
    827      1.1  riastrad 	struct timeval t_vblank;
    828      1.1  riastrad 
    829      1.1  riastrad 	/*
    830      1.1  riastrad 	 * Interrupts were disabled prior to this call, so deal with counter
    831      1.1  riastrad 	 * wrap if needed.
    832      1.1  riastrad 	 * NOTE!  It's possible we lost a full dev->max_vblank_count events
    833      1.1  riastrad 	 * here if the register is small or we had vblank interrupts off for
    834      1.1  riastrad 	 * a long time.
    835      1.1  riastrad 	 *
    836      1.1  riastrad 	 * We repeat the hardware vblank counter & timestamp query until
    837      1.1  riastrad 	 * we get consistent results. This to prevent races between gpu
    838      1.1  riastrad 	 * updating its hardware counter while we are retrieving the
    839      1.1  riastrad 	 * corresponding vblank timestamp.
    840      1.1  riastrad 	 */
    841      1.1  riastrad 	do {
    842      1.1  riastrad 		cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
    843      1.1  riastrad 		rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
    844      1.1  riastrad 	} while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
    845      1.1  riastrad 
    846      1.1  riastrad 	/* Deal with counter wrap */
    847  1.1.1.2  riastrad 	diff = cur_vblank - dev->vblank[crtc].last;
    848  1.1.1.2  riastrad 	if (cur_vblank < dev->vblank[crtc].last) {
    849      1.1  riastrad 		diff += dev->max_vblank_count;
    850      1.1  riastrad 
    851      1.1  riastrad 		DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
    852  1.1.1.2  riastrad 			  crtc, dev->vblank[crtc].last, cur_vblank, diff);
    853      1.1  riastrad 	}
    854      1.1  riastrad 
    855      1.1  riastrad 	DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
    856      1.1  riastrad 		  crtc, diff);
    857      1.1  riastrad 
    858      1.1  riastrad 	/* Reinitialize corresponding vblank timestamp if high-precision query
    859      1.1  riastrad 	 * available. Skip this step if query unsupported or failed. Will
    860      1.1  riastrad 	 * reinitialize delayed at next vblank interrupt in that case.
    861      1.1  riastrad 	 */
    862      1.1  riastrad 	if (rc) {
    863  1.1.1.2  riastrad 		tslot = atomic_read(&dev->vblank[crtc].count) + diff;
    864      1.1  riastrad 		vblanktimestamp(dev, crtc, tslot) = t_vblank;
    865      1.1  riastrad 	}
    866      1.1  riastrad 
    867      1.1  riastrad 	smp_mb__before_atomic_inc();
    868  1.1.1.2  riastrad 	atomic_add(diff, &dev->vblank[crtc].count);
    869      1.1  riastrad 	smp_mb__after_atomic_inc();
    870      1.1  riastrad }
    871      1.1  riastrad 
    872      1.1  riastrad /**
    873      1.1  riastrad  * drm_vblank_get - get a reference count on vblank events
    874      1.1  riastrad  * @dev: DRM device
    875      1.1  riastrad  * @crtc: which CRTC to own
    876      1.1  riastrad  *
    877      1.1  riastrad  * Acquire a reference count on vblank events to avoid having them disabled
    878      1.1  riastrad  * while in use.
    879      1.1  riastrad  *
    880      1.1  riastrad  * RETURNS
    881      1.1  riastrad  * Zero on success, nonzero on failure.
    882      1.1  riastrad  */
    883      1.1  riastrad int drm_vblank_get(struct drm_device *dev, int crtc)
    884      1.1  riastrad {
    885      1.1  riastrad 	unsigned long irqflags, irqflags2;
    886      1.1  riastrad 	int ret = 0;
    887      1.1  riastrad 
    888      1.1  riastrad 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
    889      1.1  riastrad 	/* Going from 0->1 means we have to enable interrupts again */
    890  1.1.1.2  riastrad 	if (atomic_add_return(1, &dev->vblank[crtc].refcount) == 1) {
    891      1.1  riastrad 		spin_lock_irqsave(&dev->vblank_time_lock, irqflags2);
    892  1.1.1.2  riastrad 		if (!dev->vblank[crtc].enabled) {
    893      1.1  riastrad 			/* Enable vblank irqs under vblank_time_lock protection.
    894      1.1  riastrad 			 * All vblank count & timestamp updates are held off
    895      1.1  riastrad 			 * until we are done reinitializing master counter and
    896      1.1  riastrad 			 * timestamps. Filtercode in drm_handle_vblank() will
    897      1.1  riastrad 			 * prevent double-accounting of same vblank interval.
    898      1.1  riastrad 			 */
    899      1.1  riastrad 			ret = dev->driver->enable_vblank(dev, crtc);
    900      1.1  riastrad 			DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
    901      1.1  riastrad 				  crtc, ret);
    902      1.1  riastrad 			if (ret)
    903  1.1.1.2  riastrad 				atomic_dec(&dev->vblank[crtc].refcount);
    904      1.1  riastrad 			else {
    905  1.1.1.2  riastrad 				dev->vblank[crtc].enabled = true;
    906      1.1  riastrad 				drm_update_vblank_count(dev, crtc);
    907      1.1  riastrad 			}
    908      1.1  riastrad 		}
    909      1.1  riastrad 		spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags2);
    910      1.1  riastrad 	} else {
    911  1.1.1.2  riastrad 		if (!dev->vblank[crtc].enabled) {
    912  1.1.1.2  riastrad 			atomic_dec(&dev->vblank[crtc].refcount);
    913      1.1  riastrad 			ret = -EINVAL;
    914      1.1  riastrad 		}
    915      1.1  riastrad 	}
    916      1.1  riastrad 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
    917      1.1  riastrad 
    918      1.1  riastrad 	return ret;
    919      1.1  riastrad }
    920      1.1  riastrad EXPORT_SYMBOL(drm_vblank_get);
    921      1.1  riastrad 
    922      1.1  riastrad /**
    923      1.1  riastrad  * drm_vblank_put - give up ownership of vblank events
    924      1.1  riastrad  * @dev: DRM device
    925      1.1  riastrad  * @crtc: which counter to give up
    926      1.1  riastrad  *
    927      1.1  riastrad  * Release ownership of a given vblank counter, turning off interrupts
    928      1.1  riastrad  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
    929      1.1  riastrad  */
    930      1.1  riastrad void drm_vblank_put(struct drm_device *dev, int crtc)
    931      1.1  riastrad {
    932  1.1.1.2  riastrad 	BUG_ON(atomic_read(&dev->vblank[crtc].refcount) == 0);
    933      1.1  riastrad 
    934      1.1  riastrad 	/* Last user schedules interrupt disable */
    935  1.1.1.2  riastrad 	if (atomic_dec_and_test(&dev->vblank[crtc].refcount) &&
    936      1.1  riastrad 	    (drm_vblank_offdelay > 0))
    937      1.1  riastrad 		mod_timer(&dev->vblank_disable_timer,
    938  1.1.1.2  riastrad 			  jiffies + ((drm_vblank_offdelay * HZ)/1000));
    939      1.1  riastrad }
    940      1.1  riastrad EXPORT_SYMBOL(drm_vblank_put);
    941      1.1  riastrad 
    942      1.1  riastrad /**
    943      1.1  riastrad  * drm_vblank_off - disable vblank events on a CRTC
    944      1.1  riastrad  * @dev: DRM device
    945      1.1  riastrad  * @crtc: CRTC in question
    946      1.1  riastrad  *
    947      1.1  riastrad  * Caller must hold event lock.
    948      1.1  riastrad  */
    949      1.1  riastrad void drm_vblank_off(struct drm_device *dev, int crtc)
    950      1.1  riastrad {
    951      1.1  riastrad 	struct drm_pending_vblank_event *e, *t;
    952      1.1  riastrad 	struct timeval now;
    953      1.1  riastrad 	unsigned long irqflags;
    954      1.1  riastrad 	unsigned int seq;
    955      1.1  riastrad 
    956      1.1  riastrad 	spin_lock_irqsave(&dev->vbl_lock, irqflags);
    957      1.1  riastrad 	vblank_disable_and_save(dev, crtc);
    958  1.1.1.2  riastrad 	wake_up(&dev->vblank[crtc].queue);
    959      1.1  riastrad 
    960      1.1  riastrad 	/* Send any queued vblank events, lest the natives grow disquiet */
    961      1.1  riastrad 	seq = drm_vblank_count_and_time(dev, crtc, &now);
    962      1.1  riastrad 
    963      1.1  riastrad 	spin_lock(&dev->event_lock);
    964      1.1  riastrad 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
    965      1.1  riastrad 		if (e->pipe != crtc)
    966      1.1  riastrad 			continue;
    967      1.1  riastrad 		DRM_DEBUG("Sending premature vblank event on disable: \
    968      1.1  riastrad 			  wanted %d, current %d\n",
    969      1.1  riastrad 			  e->event.sequence, seq);
    970      1.1  riastrad 		list_del(&e->base.link);
    971      1.1  riastrad 		drm_vblank_put(dev, e->pipe);
    972      1.1  riastrad 		send_vblank_event(dev, e, seq, &now);
    973      1.1  riastrad 	}
    974      1.1  riastrad 	spin_unlock(&dev->event_lock);
    975      1.1  riastrad 
    976      1.1  riastrad 	spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
    977      1.1  riastrad }
    978      1.1  riastrad EXPORT_SYMBOL(drm_vblank_off);
    979      1.1  riastrad 
    980      1.1  riastrad /**
    981      1.1  riastrad  * drm_vblank_pre_modeset - account for vblanks across mode sets
    982      1.1  riastrad  * @dev: DRM device
    983      1.1  riastrad  * @crtc: CRTC in question
    984      1.1  riastrad  *
    985      1.1  riastrad  * Account for vblank events across mode setting events, which will likely
    986      1.1  riastrad  * reset the hardware frame counter.
    987      1.1  riastrad  */
    988      1.1  riastrad void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
    989      1.1  riastrad {
    990  1.1.1.2  riastrad 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
    991      1.1  riastrad 	if (!dev->num_crtcs)
    992      1.1  riastrad 		return;
    993      1.1  riastrad 	/*
    994      1.1  riastrad 	 * To avoid all the problems that might happen if interrupts
    995      1.1  riastrad 	 * were enabled/disabled around or between these calls, we just
    996      1.1  riastrad 	 * have the kernel take a reference on the CRTC (just once though
    997      1.1  riastrad 	 * to avoid corrupting the count if multiple, mismatch calls occur),
    998      1.1  riastrad 	 * so that interrupts remain enabled in the interim.
    999      1.1  riastrad 	 */
   1000  1.1.1.2  riastrad 	if (!dev->vblank[crtc].inmodeset) {
   1001  1.1.1.2  riastrad 		dev->vblank[crtc].inmodeset = 0x1;
   1002      1.1  riastrad 		if (drm_vblank_get(dev, crtc) == 0)
   1003  1.1.1.2  riastrad 			dev->vblank[crtc].inmodeset |= 0x2;
   1004      1.1  riastrad 	}
   1005      1.1  riastrad }
   1006      1.1  riastrad EXPORT_SYMBOL(drm_vblank_pre_modeset);
   1007      1.1  riastrad 
   1008      1.1  riastrad void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
   1009      1.1  riastrad {
   1010      1.1  riastrad 	unsigned long irqflags;
   1011      1.1  riastrad 
   1012  1.1.1.2  riastrad 	/* vblank is not initialized (IRQ not installed ?), or has been freed */
   1013  1.1.1.2  riastrad 	if (!dev->num_crtcs)
   1014  1.1.1.2  riastrad 		return;
   1015  1.1.1.2  riastrad 
   1016  1.1.1.2  riastrad 	if (dev->vblank[crtc].inmodeset) {
   1017      1.1  riastrad 		spin_lock_irqsave(&dev->vbl_lock, irqflags);
   1018  1.1.1.2  riastrad 		dev->vblank_disable_allowed = true;
   1019      1.1  riastrad 		spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
   1020      1.1  riastrad 
   1021  1.1.1.2  riastrad 		if (dev->vblank[crtc].inmodeset & 0x2)
   1022      1.1  riastrad 			drm_vblank_put(dev, crtc);
   1023      1.1  riastrad 
   1024  1.1.1.2  riastrad 		dev->vblank[crtc].inmodeset = 0;
   1025      1.1  riastrad 	}
   1026      1.1  riastrad }
   1027      1.1  riastrad EXPORT_SYMBOL(drm_vblank_post_modeset);
   1028      1.1  riastrad 
   1029      1.1  riastrad /**
   1030      1.1  riastrad  * drm_modeset_ctl - handle vblank event counter changes across mode switch
   1031      1.1  riastrad  * @DRM_IOCTL_ARGS: standard ioctl arguments
   1032      1.1  riastrad  *
   1033      1.1  riastrad  * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
   1034      1.1  riastrad  * ioctls around modesetting so that any lost vblank events are accounted for.
   1035      1.1  riastrad  *
   1036      1.1  riastrad  * Generally the counter will reset across mode sets.  If interrupts are
   1037      1.1  riastrad  * enabled around this call, we don't have to do anything since the counter
   1038      1.1  riastrad  * will have already been incremented.
   1039      1.1  riastrad  */
   1040      1.1  riastrad int drm_modeset_ctl(struct drm_device *dev, void *data,
   1041      1.1  riastrad 		    struct drm_file *file_priv)
   1042      1.1  riastrad {
   1043      1.1  riastrad 	struct drm_modeset_ctl *modeset = data;
   1044      1.1  riastrad 	unsigned int crtc;
   1045      1.1  riastrad 
   1046      1.1  riastrad 	/* If drm_vblank_init() hasn't been called yet, just no-op */
   1047      1.1  riastrad 	if (!dev->num_crtcs)
   1048      1.1  riastrad 		return 0;
   1049      1.1  riastrad 
   1050      1.1  riastrad 	/* KMS drivers handle this internally */
   1051      1.1  riastrad 	if (drm_core_check_feature(dev, DRIVER_MODESET))
   1052      1.1  riastrad 		return 0;
   1053      1.1  riastrad 
   1054      1.1  riastrad 	crtc = modeset->crtc;
   1055      1.1  riastrad 	if (crtc >= dev->num_crtcs)
   1056      1.1  riastrad 		return -EINVAL;
   1057      1.1  riastrad 
   1058      1.1  riastrad 	switch (modeset->cmd) {
   1059      1.1  riastrad 	case _DRM_PRE_MODESET:
   1060      1.1  riastrad 		drm_vblank_pre_modeset(dev, crtc);
   1061      1.1  riastrad 		break;
   1062      1.1  riastrad 	case _DRM_POST_MODESET:
   1063      1.1  riastrad 		drm_vblank_post_modeset(dev, crtc);
   1064      1.1  riastrad 		break;
   1065      1.1  riastrad 	default:
   1066      1.1  riastrad 		return -EINVAL;
   1067      1.1  riastrad 	}
   1068      1.1  riastrad 
   1069      1.1  riastrad 	return 0;
   1070      1.1  riastrad }
   1071      1.1  riastrad 
   1072      1.1  riastrad static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
   1073      1.1  riastrad 				  union drm_wait_vblank *vblwait,
   1074      1.1  riastrad 				  struct drm_file *file_priv)
   1075      1.1  riastrad {
   1076      1.1  riastrad 	struct drm_pending_vblank_event *e;
   1077      1.1  riastrad 	struct timeval now;
   1078      1.1  riastrad 	unsigned long flags;
   1079      1.1  riastrad 	unsigned int seq;
   1080      1.1  riastrad 	int ret;
   1081      1.1  riastrad 
   1082      1.1  riastrad 	e = kzalloc(sizeof *e, GFP_KERNEL);
   1083      1.1  riastrad 	if (e == NULL) {
   1084      1.1  riastrad 		ret = -ENOMEM;
   1085      1.1  riastrad 		goto err_put;
   1086      1.1  riastrad 	}
   1087      1.1  riastrad 
   1088      1.1  riastrad 	e->pipe = pipe;
   1089      1.1  riastrad 	e->base.pid = current->pid;
   1090      1.1  riastrad 	e->event.base.type = DRM_EVENT_VBLANK;
   1091      1.1  riastrad 	e->event.base.length = sizeof e->event;
   1092      1.1  riastrad 	e->event.user_data = vblwait->request.signal;
   1093      1.1  riastrad 	e->base.event = &e->event.base;
   1094      1.1  riastrad 	e->base.file_priv = file_priv;
   1095      1.1  riastrad 	e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
   1096      1.1  riastrad 
   1097      1.1  riastrad 	spin_lock_irqsave(&dev->event_lock, flags);
   1098      1.1  riastrad 
   1099      1.1  riastrad 	if (file_priv->event_space < sizeof e->event) {
   1100      1.1  riastrad 		ret = -EBUSY;
   1101      1.1  riastrad 		goto err_unlock;
   1102      1.1  riastrad 	}
   1103      1.1  riastrad 
   1104      1.1  riastrad 	file_priv->event_space -= sizeof e->event;
   1105      1.1  riastrad 	seq = drm_vblank_count_and_time(dev, pipe, &now);
   1106      1.1  riastrad 
   1107      1.1  riastrad 	if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
   1108      1.1  riastrad 	    (seq - vblwait->request.sequence) <= (1 << 23)) {
   1109      1.1  riastrad 		vblwait->request.sequence = seq + 1;
   1110      1.1  riastrad 		vblwait->reply.sequence = vblwait->request.sequence;
   1111      1.1  riastrad 	}
   1112      1.1  riastrad 
   1113      1.1  riastrad 	DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
   1114      1.1  riastrad 		  vblwait->request.sequence, seq, pipe);
   1115      1.1  riastrad 
   1116      1.1  riastrad 	trace_drm_vblank_event_queued(current->pid, pipe,
   1117      1.1  riastrad 				      vblwait->request.sequence);
   1118      1.1  riastrad 
   1119      1.1  riastrad 	e->event.sequence = vblwait->request.sequence;
   1120      1.1  riastrad 	if ((seq - vblwait->request.sequence) <= (1 << 23)) {
   1121      1.1  riastrad 		drm_vblank_put(dev, pipe);
   1122      1.1  riastrad 		send_vblank_event(dev, e, seq, &now);
   1123      1.1  riastrad 		vblwait->reply.sequence = seq;
   1124      1.1  riastrad 	} else {
   1125      1.1  riastrad 		/* drm_handle_vblank_events will call drm_vblank_put */
   1126      1.1  riastrad 		list_add_tail(&e->base.link, &dev->vblank_event_list);
   1127      1.1  riastrad 		vblwait->reply.sequence = vblwait->request.sequence;
   1128      1.1  riastrad 	}
   1129      1.1  riastrad 
   1130      1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, flags);
   1131      1.1  riastrad 
   1132      1.1  riastrad 	return 0;
   1133      1.1  riastrad 
   1134      1.1  riastrad err_unlock:
   1135      1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, flags);
   1136      1.1  riastrad 	kfree(e);
   1137      1.1  riastrad err_put:
   1138      1.1  riastrad 	drm_vblank_put(dev, pipe);
   1139      1.1  riastrad 	return ret;
   1140      1.1  riastrad }
   1141      1.1  riastrad 
   1142      1.1  riastrad /**
   1143      1.1  riastrad  * Wait for VBLANK.
   1144      1.1  riastrad  *
   1145      1.1  riastrad  * \param inode device inode.
   1146      1.1  riastrad  * \param file_priv DRM file private.
   1147      1.1  riastrad  * \param cmd command.
   1148      1.1  riastrad  * \param data user argument, pointing to a drm_wait_vblank structure.
   1149      1.1  riastrad  * \return zero on success or a negative number on failure.
   1150      1.1  riastrad  *
   1151      1.1  riastrad  * This function enables the vblank interrupt on the pipe requested, then
   1152      1.1  riastrad  * sleeps waiting for the requested sequence number to occur, and drops
   1153      1.1  riastrad  * the vblank interrupt refcount afterwards. (vblank irq disable follows that
   1154      1.1  riastrad  * after a timeout with no further vblank waits scheduled).
   1155      1.1  riastrad  */
   1156      1.1  riastrad int drm_wait_vblank(struct drm_device *dev, void *data,
   1157      1.1  riastrad 		    struct drm_file *file_priv)
   1158      1.1  riastrad {
   1159      1.1  riastrad 	union drm_wait_vblank *vblwait = data;
   1160      1.1  riastrad 	int ret;
   1161      1.1  riastrad 	unsigned int flags, seq, crtc, high_crtc;
   1162      1.1  riastrad 
   1163  1.1.1.2  riastrad 	if (drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
   1164  1.1.1.2  riastrad 		if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled))
   1165  1.1.1.2  riastrad 			return -EINVAL;
   1166      1.1  riastrad 
   1167      1.1  riastrad 	if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
   1168      1.1  riastrad 		return -EINVAL;
   1169      1.1  riastrad 
   1170      1.1  riastrad 	if (vblwait->request.type &
   1171      1.1  riastrad 	    ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
   1172      1.1  riastrad 	      _DRM_VBLANK_HIGH_CRTC_MASK)) {
   1173      1.1  riastrad 		DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
   1174      1.1  riastrad 			  vblwait->request.type,
   1175      1.1  riastrad 			  (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
   1176      1.1  riastrad 			   _DRM_VBLANK_HIGH_CRTC_MASK));
   1177      1.1  riastrad 		return -EINVAL;
   1178      1.1  riastrad 	}
   1179      1.1  riastrad 
   1180      1.1  riastrad 	flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
   1181      1.1  riastrad 	high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
   1182      1.1  riastrad 	if (high_crtc)
   1183      1.1  riastrad 		crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
   1184      1.1  riastrad 	else
   1185      1.1  riastrad 		crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
   1186      1.1  riastrad 	if (crtc >= dev->num_crtcs)
   1187      1.1  riastrad 		return -EINVAL;
   1188      1.1  riastrad 
   1189      1.1  riastrad 	ret = drm_vblank_get(dev, crtc);
   1190      1.1  riastrad 	if (ret) {
   1191      1.1  riastrad 		DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
   1192      1.1  riastrad 		return ret;
   1193      1.1  riastrad 	}
   1194      1.1  riastrad 	seq = drm_vblank_count(dev, crtc);
   1195      1.1  riastrad 
   1196      1.1  riastrad 	switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
   1197      1.1  riastrad 	case _DRM_VBLANK_RELATIVE:
   1198      1.1  riastrad 		vblwait->request.sequence += seq;
   1199      1.1  riastrad 		vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
   1200      1.1  riastrad 	case _DRM_VBLANK_ABSOLUTE:
   1201      1.1  riastrad 		break;
   1202      1.1  riastrad 	default:
   1203      1.1  riastrad 		ret = -EINVAL;
   1204      1.1  riastrad 		goto done;
   1205      1.1  riastrad 	}
   1206      1.1  riastrad 
   1207      1.1  riastrad 	if (flags & _DRM_VBLANK_EVENT) {
   1208      1.1  riastrad 		/* must hold on to the vblank ref until the event fires
   1209      1.1  riastrad 		 * drm_vblank_put will be called asynchronously
   1210      1.1  riastrad 		 */
   1211      1.1  riastrad 		return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
   1212      1.1  riastrad 	}
   1213      1.1  riastrad 
   1214      1.1  riastrad 	if ((flags & _DRM_VBLANK_NEXTONMISS) &&
   1215      1.1  riastrad 	    (seq - vblwait->request.sequence) <= (1<<23)) {
   1216      1.1  riastrad 		vblwait->request.sequence = seq + 1;
   1217      1.1  riastrad 	}
   1218      1.1  riastrad 
   1219      1.1  riastrad 	DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
   1220      1.1  riastrad 		  vblwait->request.sequence, crtc);
   1221  1.1.1.2  riastrad 	dev->vblank[crtc].last_wait = vblwait->request.sequence;
   1222  1.1.1.2  riastrad 	DRM_WAIT_ON(ret, dev->vblank[crtc].queue, 3 * HZ,
   1223      1.1  riastrad 		    (((drm_vblank_count(dev, crtc) -
   1224      1.1  riastrad 		       vblwait->request.sequence) <= (1 << 23)) ||
   1225      1.1  riastrad 		     !dev->irq_enabled));
   1226      1.1  riastrad 
   1227      1.1  riastrad 	if (ret != -EINTR) {
   1228      1.1  riastrad 		struct timeval now;
   1229      1.1  riastrad 
   1230      1.1  riastrad 		vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
   1231      1.1  riastrad 		vblwait->reply.tval_sec = now.tv_sec;
   1232      1.1  riastrad 		vblwait->reply.tval_usec = now.tv_usec;
   1233      1.1  riastrad 
   1234      1.1  riastrad 		DRM_DEBUG("returning %d to client\n",
   1235      1.1  riastrad 			  vblwait->reply.sequence);
   1236      1.1  riastrad 	} else {
   1237      1.1  riastrad 		DRM_DEBUG("vblank wait interrupted by signal\n");
   1238      1.1  riastrad 	}
   1239      1.1  riastrad 
   1240      1.1  riastrad done:
   1241      1.1  riastrad 	drm_vblank_put(dev, crtc);
   1242      1.1  riastrad 	return ret;
   1243      1.1  riastrad }
   1244      1.1  riastrad 
   1245      1.1  riastrad static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
   1246      1.1  riastrad {
   1247      1.1  riastrad 	struct drm_pending_vblank_event *e, *t;
   1248      1.1  riastrad 	struct timeval now;
   1249      1.1  riastrad 	unsigned long flags;
   1250      1.1  riastrad 	unsigned int seq;
   1251      1.1  riastrad 
   1252      1.1  riastrad 	seq = drm_vblank_count_and_time(dev, crtc, &now);
   1253      1.1  riastrad 
   1254      1.1  riastrad 	spin_lock_irqsave(&dev->event_lock, flags);
   1255      1.1  riastrad 
   1256      1.1  riastrad 	list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
   1257      1.1  riastrad 		if (e->pipe != crtc)
   1258      1.1  riastrad 			continue;
   1259      1.1  riastrad 		if ((seq - e->event.sequence) > (1<<23))
   1260      1.1  riastrad 			continue;
   1261      1.1  riastrad 
   1262      1.1  riastrad 		DRM_DEBUG("vblank event on %d, current %d\n",
   1263      1.1  riastrad 			  e->event.sequence, seq);
   1264      1.1  riastrad 
   1265      1.1  riastrad 		list_del(&e->base.link);
   1266      1.1  riastrad 		drm_vblank_put(dev, e->pipe);
   1267      1.1  riastrad 		send_vblank_event(dev, e, seq, &now);
   1268      1.1  riastrad 	}
   1269      1.1  riastrad 
   1270      1.1  riastrad 	spin_unlock_irqrestore(&dev->event_lock, flags);
   1271      1.1  riastrad 
   1272      1.1  riastrad 	trace_drm_vblank_event(crtc, seq);
   1273      1.1  riastrad }
   1274      1.1  riastrad 
   1275      1.1  riastrad /**
   1276      1.1  riastrad  * drm_handle_vblank - handle a vblank event
   1277      1.1  riastrad  * @dev: DRM device
   1278      1.1  riastrad  * @crtc: where this event occurred
   1279      1.1  riastrad  *
   1280      1.1  riastrad  * Drivers should call this routine in their vblank interrupt handlers to
   1281      1.1  riastrad  * update the vblank counter and send any signals that may be pending.
   1282      1.1  riastrad  */
   1283      1.1  riastrad bool drm_handle_vblank(struct drm_device *dev, int crtc)
   1284      1.1  riastrad {
   1285      1.1  riastrad 	u32 vblcount;
   1286      1.1  riastrad 	s64 diff_ns;
   1287      1.1  riastrad 	struct timeval tvblank;
   1288      1.1  riastrad 	unsigned long irqflags;
   1289      1.1  riastrad 
   1290      1.1  riastrad 	if (!dev->num_crtcs)
   1291      1.1  riastrad 		return false;
   1292      1.1  riastrad 
   1293      1.1  riastrad 	/* Need timestamp lock to prevent concurrent execution with
   1294      1.1  riastrad 	 * vblank enable/disable, as this would cause inconsistent
   1295      1.1  riastrad 	 * or corrupted timestamps and vblank counts.
   1296      1.1  riastrad 	 */
   1297      1.1  riastrad 	spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
   1298      1.1  riastrad 
   1299      1.1  riastrad 	/* Vblank irq handling disabled. Nothing to do. */
   1300  1.1.1.2  riastrad 	if (!dev->vblank[crtc].enabled) {
   1301      1.1  riastrad 		spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
   1302      1.1  riastrad 		return false;
   1303      1.1  riastrad 	}
   1304      1.1  riastrad 
   1305      1.1  riastrad 	/* Fetch corresponding timestamp for this vblank interval from
   1306      1.1  riastrad 	 * driver and store it in proper slot of timestamp ringbuffer.
   1307      1.1  riastrad 	 */
   1308      1.1  riastrad 
   1309      1.1  riastrad 	/* Get current timestamp and count. */
   1310  1.1.1.2  riastrad 	vblcount = atomic_read(&dev->vblank[crtc].count);
   1311      1.1  riastrad 	drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
   1312      1.1  riastrad 
   1313      1.1  riastrad 	/* Compute time difference to timestamp of last vblank */
   1314      1.1  riastrad 	diff_ns = timeval_to_ns(&tvblank) -
   1315      1.1  riastrad 		  timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
   1316      1.1  riastrad 
   1317      1.1  riastrad 	/* Update vblank timestamp and count if at least
   1318      1.1  riastrad 	 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
   1319      1.1  riastrad 	 * difference between last stored timestamp and current
   1320      1.1  riastrad 	 * timestamp. A smaller difference means basically
   1321      1.1  riastrad 	 * identical timestamps. Happens if this vblank has
   1322      1.1  riastrad 	 * been already processed and this is a redundant call,
   1323      1.1  riastrad 	 * e.g., due to spurious vblank interrupts. We need to
   1324      1.1  riastrad 	 * ignore those for accounting.
   1325      1.1  riastrad 	 */
   1326      1.1  riastrad 	if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
   1327      1.1  riastrad 		/* Store new timestamp in ringbuffer. */
   1328      1.1  riastrad 		vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
   1329      1.1  riastrad 
   1330      1.1  riastrad 		/* Increment cooked vblank count. This also atomically commits
   1331      1.1  riastrad 		 * the timestamp computed above.
   1332      1.1  riastrad 		 */
   1333      1.1  riastrad 		smp_mb__before_atomic_inc();
   1334  1.1.1.2  riastrad 		atomic_inc(&dev->vblank[crtc].count);
   1335      1.1  riastrad 		smp_mb__after_atomic_inc();
   1336      1.1  riastrad 	} else {
   1337      1.1  riastrad 		DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
   1338      1.1  riastrad 			  crtc, (int) diff_ns);
   1339      1.1  riastrad 	}
   1340      1.1  riastrad 
   1341  1.1.1.2  riastrad 	wake_up(&dev->vblank[crtc].queue);
   1342      1.1  riastrad 	drm_handle_vblank_events(dev, crtc);
   1343      1.1  riastrad 
   1344      1.1  riastrad 	spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
   1345      1.1  riastrad 	return true;
   1346      1.1  riastrad }
   1347      1.1  riastrad EXPORT_SYMBOL(drm_handle_vblank);
   1348